src/HOL/Library/Quotient_List.thy
author bulwahn
Tue Oct 19 12:26:37 2010 +0200 (2010-10-19)
changeset 40032 5f78dfb2fa7d
parent 39302 d7728f65b353
child 40463 75e544159549
permissions -rw-r--r--
removing something that probably slipped into the Quotient_List theory
wenzelm@35788
     1
(*  Title:      HOL/Library/Quotient_List.thy
kaliszyk@35222
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222
     3
*)
wenzelm@35788
     4
wenzelm@35788
     5
header {* Quotient infrastructure for the list type *}
wenzelm@35788
     6
kaliszyk@35222
     7
theory Quotient_List
kaliszyk@35222
     8
imports Main Quotient_Syntax
kaliszyk@35222
     9
begin
kaliszyk@35222
    10
kaliszyk@37492
    11
declare [[map list = (map, list_all2)]]
kaliszyk@35222
    12
kaliszyk@35222
    13
lemma split_list_all:
kaliszyk@35222
    14
  shows "(\<forall>x. P x) \<longleftrightarrow> P [] \<and> (\<forall>x xs. P (x#xs))"
kaliszyk@35222
    15
  apply(auto)
kaliszyk@35222
    16
  apply(case_tac x)
kaliszyk@35222
    17
  apply(simp_all)
kaliszyk@35222
    18
  done
kaliszyk@35222
    19
kaliszyk@35222
    20
lemma map_id[id_simps]:
kaliszyk@35222
    21
  shows "map id = id"
nipkow@39302
    22
  apply(simp add: fun_eq_iff)
kaliszyk@35222
    23
  apply(rule allI)
kaliszyk@35222
    24
  apply(induct_tac x)
kaliszyk@35222
    25
  apply(simp_all)
kaliszyk@35222
    26
  done
kaliszyk@35222
    27
kaliszyk@37492
    28
lemma list_all2_reflp:
kaliszyk@37492
    29
  shows "equivp R \<Longrightarrow> list_all2 R xs xs"
kaliszyk@37492
    30
  by (induct xs, simp_all add: equivp_reflp)
kaliszyk@35222
    31
kaliszyk@37492
    32
lemma list_all2_symp:
kaliszyk@35222
    33
  assumes a: "equivp R"
kaliszyk@37492
    34
  and b: "list_all2 R xs ys"
kaliszyk@37492
    35
  shows "list_all2 R ys xs"
kaliszyk@37492
    36
  using list_all2_lengthD[OF b] b
kaliszyk@37492
    37
  apply(induct xs ys rule: list_induct2)
kaliszyk@35222
    38
  apply(simp_all)
kaliszyk@35222
    39
  apply(rule equivp_symp[OF a])
kaliszyk@35222
    40
  apply(simp)
kaliszyk@35222
    41
  done
kaliszyk@35222
    42
kaliszyk@37492
    43
lemma list_all2_transp:
kaliszyk@35222
    44
  assumes a: "equivp R"
kaliszyk@37492
    45
  and b: "list_all2 R xs1 xs2"
kaliszyk@37492
    46
  and c: "list_all2 R xs2 xs3"
kaliszyk@37492
    47
  shows "list_all2 R xs1 xs3"
kaliszyk@37492
    48
  using list_all2_lengthD[OF b] list_all2_lengthD[OF c] b c
kaliszyk@37492
    49
  apply(induct rule: list_induct3)
kaliszyk@37492
    50
  apply(simp_all)
kaliszyk@37492
    51
  apply(auto intro: equivp_transp[OF a])
kaliszyk@35222
    52
  done
kaliszyk@35222
    53
kaliszyk@35222
    54
lemma list_equivp[quot_equiv]:
kaliszyk@35222
    55
  assumes a: "equivp R"
kaliszyk@37492
    56
  shows "equivp (list_all2 R)"
kaliszyk@37492
    57
  apply (intro equivpI)
kaliszyk@35222
    58
  unfolding reflp_def symp_def transp_def
kaliszyk@37492
    59
  apply(simp add: list_all2_reflp[OF a])
kaliszyk@37492
    60
  apply(blast intro: list_all2_symp[OF a])
kaliszyk@37492
    61
  apply(blast intro: list_all2_transp[OF a])
kaliszyk@35222
    62
  done
kaliszyk@35222
    63
kaliszyk@37492
    64
lemma list_all2_rel:
kaliszyk@35222
    65
  assumes q: "Quotient R Abs Rep"
kaliszyk@37492
    66
  shows "list_all2 R r s = (list_all2 R r r \<and> list_all2 R s s \<and> (map Abs r = map Abs s))"
kaliszyk@35222
    67
  apply(induct r s rule: list_induct2')
kaliszyk@35222
    68
  apply(simp_all)
kaliszyk@35222
    69
  using Quotient_rel[OF q]
kaliszyk@35222
    70
  apply(metis)
kaliszyk@35222
    71
  done
kaliszyk@35222
    72
kaliszyk@35222
    73
lemma list_quotient[quot_thm]:
kaliszyk@35222
    74
  assumes q: "Quotient R Abs Rep"
kaliszyk@37492
    75
  shows "Quotient (list_all2 R) (map Abs) (map Rep)"
kaliszyk@35222
    76
  unfolding Quotient_def
kaliszyk@35222
    77
  apply(subst split_list_all)
kaliszyk@35222
    78
  apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
kaliszyk@37492
    79
  apply(intro conjI allI)
kaliszyk@35222
    80
  apply(induct_tac a)
kaliszyk@37492
    81
  apply(simp_all add: Quotient_rep_reflp[OF q])
kaliszyk@37492
    82
  apply(rule list_all2_rel[OF q])
kaliszyk@35222
    83
  done
kaliszyk@35222
    84
kaliszyk@35222
    85
lemma cons_prs_aux:
kaliszyk@35222
    86
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    87
  shows "(map Abs) ((Rep h) # (map Rep t)) = h # t"
kaliszyk@35222
    88
  by (induct t) (simp_all add: Quotient_abs_rep[OF q])
kaliszyk@35222
    89
kaliszyk@35222
    90
lemma cons_prs[quot_preserve]:
kaliszyk@35222
    91
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    92
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
nipkow@39302
    93
  by (simp only: fun_eq_iff fun_map_def cons_prs_aux[OF q])
kaliszyk@35222
    94
     (simp)
kaliszyk@35222
    95
kaliszyk@35222
    96
lemma cons_rsp[quot_respect]:
kaliszyk@35222
    97
  assumes q: "Quotient R Abs Rep"
kaliszyk@37492
    98
  shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
kaliszyk@35222
    99
  by (auto)
kaliszyk@35222
   100
kaliszyk@35222
   101
lemma nil_prs[quot_preserve]:
kaliszyk@35222
   102
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   103
  shows "map Abs [] = []"
kaliszyk@35222
   104
  by simp
kaliszyk@35222
   105
kaliszyk@35222
   106
lemma nil_rsp[quot_respect]:
kaliszyk@35222
   107
  assumes q: "Quotient R Abs Rep"
kaliszyk@37492
   108
  shows "list_all2 R [] []"
kaliszyk@35222
   109
  by simp
kaliszyk@35222
   110
kaliszyk@35222
   111
lemma map_prs_aux:
kaliszyk@35222
   112
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   113
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   114
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
   115
  by (induct l)
kaliszyk@35222
   116
     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   117
kaliszyk@35222
   118
lemma map_prs[quot_preserve]:
kaliszyk@35222
   119
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   120
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   121
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216
   122
  and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
nipkow@39302
   123
  by (simp_all only: fun_eq_iff fun_map_def map_prs_aux[OF a b])
kaliszyk@36216
   124
     (simp_all add: Quotient_abs_rep[OF a])
kaliszyk@35222
   125
kaliszyk@35222
   126
lemma map_rsp[quot_respect]:
kaliszyk@35222
   127
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   128
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37492
   129
  shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
kaliszyk@37492
   130
  and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
kaliszyk@36216
   131
  apply simp_all
kaliszyk@36216
   132
  apply(rule_tac [!] allI)+
kaliszyk@36216
   133
  apply(rule_tac [!] impI)
kaliszyk@36216
   134
  apply(rule_tac [!] allI)+
kaliszyk@36216
   135
  apply (induct_tac [!] xa ya rule: list_induct2')
kaliszyk@35222
   136
  apply simp_all
kaliszyk@35222
   137
  done
kaliszyk@35222
   138
kaliszyk@35222
   139
lemma foldr_prs_aux:
kaliszyk@35222
   140
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   141
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   142
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kaliszyk@35222
   143
  by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   144
kaliszyk@35222
   145
lemma foldr_prs[quot_preserve]:
kaliszyk@35222
   146
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   147
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   148
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
nipkow@39302
   149
  by (simp only: fun_eq_iff fun_map_def foldr_prs_aux[OF a b])
kaliszyk@35222
   150
     (simp)
kaliszyk@35222
   151
kaliszyk@35222
   152
lemma foldl_prs_aux:
kaliszyk@35222
   153
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   154
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   155
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kaliszyk@35222
   156
  by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   157
kaliszyk@35222
   158
kaliszyk@35222
   159
lemma foldl_prs[quot_preserve]:
kaliszyk@35222
   160
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   161
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   162
  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
nipkow@39302
   163
  by (simp only: fun_eq_iff fun_map_def foldl_prs_aux[OF a b])
kaliszyk@35222
   164
     (simp)
kaliszyk@35222
   165
kaliszyk@37492
   166
lemma list_all2_empty:
kaliszyk@37492
   167
  shows "list_all2 R [] b \<Longrightarrow> length b = 0"
kaliszyk@35222
   168
  by (induct b) (simp_all)
kaliszyk@35222
   169
kaliszyk@35222
   170
(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222
   171
lemma foldl_rsp[quot_respect]:
kaliszyk@35222
   172
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   173
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37492
   174
  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
kaliszyk@35222
   175
  apply(auto)
kaliszyk@37492
   176
  apply (subgoal_tac "R1 xa ya \<longrightarrow> list_all2 R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
kaliszyk@35222
   177
  apply simp
kaliszyk@35222
   178
  apply (rule_tac x="xa" in spec)
kaliszyk@35222
   179
  apply (rule_tac x="ya" in spec)
kaliszyk@35222
   180
  apply (rule_tac xs="xb" and ys="yb" in list_induct2)
kaliszyk@37492
   181
  apply (rule list_all2_lengthD)
kaliszyk@35222
   182
  apply (simp_all)
kaliszyk@35222
   183
  done
kaliszyk@35222
   184
kaliszyk@35222
   185
lemma foldr_rsp[quot_respect]:
kaliszyk@35222
   186
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   187
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37492
   188
  shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
kaliszyk@35222
   189
  apply auto
kaliszyk@37492
   190
  apply(subgoal_tac "R2 xb yb \<longrightarrow> list_all2 R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
kaliszyk@35222
   191
  apply simp
kaliszyk@35222
   192
  apply (rule_tac xs="xa" and ys="ya" in list_induct2)
kaliszyk@37492
   193
  apply (rule list_all2_lengthD)
kaliszyk@35222
   194
  apply (simp_all)
kaliszyk@35222
   195
  done
kaliszyk@35222
   196
kaliszyk@37492
   197
lemma list_all2_rsp:
kaliszyk@36154
   198
  assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
kaliszyk@37492
   199
  and l1: "list_all2 R x y"
kaliszyk@37492
   200
  and l2: "list_all2 R a b"
kaliszyk@37492
   201
  shows "list_all2 S x a = list_all2 T y b"
kaliszyk@36154
   202
  proof -
kaliszyk@37492
   203
    have a: "length y = length x" by (rule list_all2_lengthD[OF l1, symmetric])
kaliszyk@37492
   204
    have c: "length a = length b" by (rule list_all2_lengthD[OF l2])
kaliszyk@36154
   205
    show ?thesis proof (cases "length x = length a")
kaliszyk@36154
   206
      case True
kaliszyk@36154
   207
      have b: "length x = length a" by fact
kaliszyk@36154
   208
      show ?thesis using a b c r l1 l2 proof (induct rule: list_induct4)
kaliszyk@36154
   209
        case Nil
kaliszyk@36154
   210
        show ?case using assms by simp
kaliszyk@36154
   211
      next
kaliszyk@36154
   212
        case (Cons h t)
kaliszyk@36154
   213
        then show ?case by auto
kaliszyk@36154
   214
      qed
kaliszyk@36154
   215
    next
kaliszyk@36154
   216
      case False
kaliszyk@36154
   217
      have d: "length x \<noteq> length a" by fact
kaliszyk@37492
   218
      then have e: "\<not>list_all2 S x a" using list_all2_lengthD by auto
kaliszyk@36154
   219
      have "length y \<noteq> length b" using d a c by simp
kaliszyk@37492
   220
      then have "\<not>list_all2 T y b" using list_all2_lengthD by auto
kaliszyk@36154
   221
      then show ?thesis using e by simp
kaliszyk@36154
   222
    qed
kaliszyk@36154
   223
  qed
kaliszyk@36154
   224
kaliszyk@36154
   225
lemma[quot_respect]:
kaliszyk@37492
   226
  "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
kaliszyk@37492
   227
  by (simp add: list_all2_rsp)
kaliszyk@36154
   228
kaliszyk@36154
   229
lemma[quot_preserve]:
kaliszyk@36154
   230
  assumes a: "Quotient R abs1 rep1"
kaliszyk@37492
   231
  shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
nipkow@39302
   232
  apply (simp add: fun_eq_iff)
kaliszyk@36154
   233
  apply clarify
kaliszyk@36154
   234
  apply (induct_tac xa xb rule: list_induct2')
kaliszyk@36154
   235
  apply (simp_all add: Quotient_abs_rep[OF a])
kaliszyk@36154
   236
  done
kaliszyk@36154
   237
kaliszyk@36154
   238
lemma[quot_preserve]:
kaliszyk@36154
   239
  assumes a: "Quotient R abs1 rep1"
kaliszyk@37492
   240
  shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kaliszyk@36154
   241
  by (induct l m rule: list_induct2') (simp_all add: Quotient_rel_rep[OF a])
kaliszyk@36154
   242
kaliszyk@37492
   243
lemma list_all2_eq[id_simps]:
kaliszyk@37492
   244
  shows "(list_all2 (op =)) = (op =)"
nipkow@39302
   245
  unfolding fun_eq_iff
kaliszyk@35222
   246
  apply(rule allI)+
kaliszyk@35222
   247
  apply(induct_tac x xa rule: list_induct2')
kaliszyk@35222
   248
  apply(simp_all)
kaliszyk@35222
   249
  done
kaliszyk@35222
   250
kaliszyk@37492
   251
lemma list_all2_find_element:
kaliszyk@36276
   252
  assumes a: "x \<in> set a"
kaliszyk@37492
   253
  and b: "list_all2 R a b"
kaliszyk@36276
   254
  shows "\<exists>y. (y \<in> set b \<and> R x y)"
kaliszyk@36276
   255
proof -
kaliszyk@37492
   256
  have "length a = length b" using b by (rule list_all2_lengthD)
kaliszyk@36276
   257
  then show ?thesis using a b by (induct a b rule: list_induct2) auto
kaliszyk@36276
   258
qed
kaliszyk@36276
   259
kaliszyk@37492
   260
lemma list_all2_refl:
kaliszyk@35222
   261
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@37492
   262
  shows "list_all2 R x x"
kaliszyk@35222
   263
  by (induct x) (auto simp add: a)
kaliszyk@35222
   264
kaliszyk@35222
   265
end