src/HOL/Library/Quotient_List.thy
author kuncar
Tue Apr 03 16:26:48 2012 +0200 (2012-04-03)
changeset 47308 9caab698dbe4
parent 47094 1a7ad2601cb5
child 47455 26315a545e26
permissions -rw-r--r--
new package Lifting - initial commit
kuncar@47308
     1
(*  Title:      HOL/Library/Quotient3_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
haftmann@40820
    11
lemma map_id [id_simps]:
haftmann@40820
    12
  "map id = id"
haftmann@46663
    13
  by (fact List.map.id)
kaliszyk@35222
    14
haftmann@40820
    15
lemma list_all2_eq [id_simps]:
haftmann@40820
    16
  "list_all2 (op =) = (op =)"
haftmann@40820
    17
proof (rule ext)+
haftmann@40820
    18
  fix xs ys
haftmann@40820
    19
  show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
haftmann@40820
    20
    by (induct xs ys rule: list_induct2') simp_all
haftmann@40820
    21
qed
kaliszyk@35222
    22
haftmann@40820
    23
lemma list_reflp:
haftmann@40820
    24
  assumes "reflp R"
haftmann@40820
    25
  shows "reflp (list_all2 R)"
haftmann@40820
    26
proof (rule reflpI)
haftmann@40820
    27
  from assms have *: "\<And>xs. R xs xs" by (rule reflpE)
haftmann@40820
    28
  fix xs
haftmann@40820
    29
  show "list_all2 R xs xs"
haftmann@40820
    30
    by (induct xs) (simp_all add: *)
haftmann@40820
    31
qed
kaliszyk@35222
    32
haftmann@40820
    33
lemma list_symp:
haftmann@40820
    34
  assumes "symp R"
haftmann@40820
    35
  shows "symp (list_all2 R)"
haftmann@40820
    36
proof (rule sympI)
haftmann@40820
    37
  from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
haftmann@40820
    38
  fix xs ys
haftmann@40820
    39
  assume "list_all2 R xs ys"
haftmann@40820
    40
  then show "list_all2 R ys xs"
haftmann@40820
    41
    by (induct xs ys rule: list_induct2') (simp_all add: *)
haftmann@40820
    42
qed
kaliszyk@35222
    43
haftmann@40820
    44
lemma list_transp:
haftmann@40820
    45
  assumes "transp R"
haftmann@40820
    46
  shows "transp (list_all2 R)"
haftmann@40820
    47
proof (rule transpI)
haftmann@40820
    48
  from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
haftmann@40820
    49
  fix xs ys zs
huffman@45803
    50
  assume "list_all2 R xs ys" and "list_all2 R ys zs"
huffman@45803
    51
  then show "list_all2 R xs zs"
huffman@45803
    52
    by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
haftmann@40820
    53
qed
kaliszyk@35222
    54
haftmann@40820
    55
lemma list_equivp [quot_equiv]:
haftmann@40820
    56
  "equivp R \<Longrightarrow> equivp (list_all2 R)"
haftmann@40820
    57
  by (blast intro: equivpI list_reflp list_symp list_transp elim: equivpE)
kaliszyk@35222
    58
kuncar@47308
    59
lemma list_quotient3 [quot_thm]:
kuncar@47308
    60
  assumes "Quotient3 R Abs Rep"
kuncar@47308
    61
  shows "Quotient3 (list_all2 R) (map Abs) (map Rep)"
kuncar@47308
    62
proof (rule Quotient3I)
kuncar@47308
    63
  from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
haftmann@40820
    64
  then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
haftmann@40820
    65
next
kuncar@47308
    66
  from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient3_rel_rep)
haftmann@40820
    67
  then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
haftmann@40820
    68
    by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
haftmann@40820
    69
next
haftmann@40820
    70
  fix xs ys
kuncar@47308
    71
  from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient3_rel)
haftmann@40820
    72
  then show "list_all2 R xs ys \<longleftrightarrow> list_all2 R xs xs \<and> list_all2 R ys ys \<and> map Abs xs = map Abs ys"
haftmann@40820
    73
    by (induct xs ys rule: list_induct2') auto
haftmann@40820
    74
qed
kaliszyk@35222
    75
kuncar@47308
    76
declare [[mapQ3 list = (list_all2, list_quotient3)]]
kuncar@47094
    77
haftmann@40820
    78
lemma cons_prs [quot_preserve]:
kuncar@47308
    79
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222
    80
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
kuncar@47308
    81
  by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
kaliszyk@35222
    82
haftmann@40820
    83
lemma cons_rsp [quot_respect]:
kuncar@47308
    84
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492
    85
  shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
haftmann@40463
    86
  by auto
kaliszyk@35222
    87
haftmann@40820
    88
lemma nil_prs [quot_preserve]:
kuncar@47308
    89
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222
    90
  shows "map Abs [] = []"
kaliszyk@35222
    91
  by simp
kaliszyk@35222
    92
haftmann@40820
    93
lemma nil_rsp [quot_respect]:
kuncar@47308
    94
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492
    95
  shows "list_all2 R [] []"
kaliszyk@35222
    96
  by simp
kaliszyk@35222
    97
kaliszyk@35222
    98
lemma map_prs_aux:
kuncar@47308
    99
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   100
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   101
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
   102
  by (induct l)
kuncar@47308
   103
     (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   104
haftmann@40820
   105
lemma map_prs [quot_preserve]:
kuncar@47308
   106
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   107
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   108
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216
   109
  and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
haftmann@40463
   110
  by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
kuncar@47308
   111
    (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
haftmann@40463
   112
haftmann@40820
   113
lemma map_rsp [quot_respect]:
kuncar@47308
   114
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   115
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   116
  shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
kaliszyk@37492
   117
  and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
haftmann@40463
   118
  apply (simp_all add: fun_rel_def)
kaliszyk@36216
   119
  apply(rule_tac [!] allI)+
kaliszyk@36216
   120
  apply(rule_tac [!] impI)
kaliszyk@36216
   121
  apply(rule_tac [!] allI)+
kaliszyk@36216
   122
  apply (induct_tac [!] xa ya rule: list_induct2')
kaliszyk@35222
   123
  apply simp_all
kaliszyk@35222
   124
  done
kaliszyk@35222
   125
kaliszyk@35222
   126
lemma foldr_prs_aux:
kuncar@47308
   127
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   128
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   129
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kuncar@47308
   130
  by (induct l) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   131
haftmann@40820
   132
lemma foldr_prs [quot_preserve]:
kuncar@47308
   133
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   134
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   135
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
haftmann@40463
   136
  apply (simp add: fun_eq_iff)
haftmann@40463
   137
  by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
kaliszyk@35222
   138
     (simp)
kaliszyk@35222
   139
kaliszyk@35222
   140
lemma foldl_prs_aux:
kuncar@47308
   141
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   142
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   143
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kuncar@47308
   144
  by (induct l arbitrary:e) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   145
haftmann@40820
   146
lemma foldl_prs [quot_preserve]:
kuncar@47308
   147
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   148
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   149
  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
haftmann@40463
   150
  by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
kaliszyk@35222
   151
kaliszyk@35222
   152
(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222
   153
lemma foldl_rsp[quot_respect]:
kuncar@47308
   154
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   155
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   156
  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
haftmann@40463
   157
  apply(auto simp add: fun_rel_def)
huffman@45803
   158
  apply (erule_tac P="R1 xa ya" in rev_mp)
kaliszyk@35222
   159
  apply (rule_tac x="xa" in spec)
kaliszyk@35222
   160
  apply (rule_tac x="ya" in spec)
huffman@45803
   161
  apply (erule list_all2_induct, simp_all)
kaliszyk@35222
   162
  done
kaliszyk@35222
   163
kaliszyk@35222
   164
lemma foldr_rsp[quot_respect]:
kuncar@47308
   165
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   166
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   167
  shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
haftmann@40463
   168
  apply (auto simp add: fun_rel_def)
huffman@45803
   169
  apply (erule list_all2_induct, simp_all)
kaliszyk@35222
   170
  done
kaliszyk@35222
   171
kaliszyk@37492
   172
lemma list_all2_rsp:
kaliszyk@36154
   173
  assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
kaliszyk@37492
   174
  and l1: "list_all2 R x y"
kaliszyk@37492
   175
  and l2: "list_all2 R a b"
kaliszyk@37492
   176
  shows "list_all2 S x a = list_all2 T y b"
huffman@45803
   177
  using l1 l2
huffman@45803
   178
  by (induct arbitrary: a b rule: list_all2_induct,
huffman@45803
   179
    auto simp: list_all2_Cons1 list_all2_Cons2 r)
kaliszyk@36154
   180
haftmann@40820
   181
lemma [quot_respect]:
kaliszyk@37492
   182
  "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
haftmann@40463
   183
  by (simp add: list_all2_rsp fun_rel_def)
kaliszyk@36154
   184
haftmann@40820
   185
lemma [quot_preserve]:
kuncar@47308
   186
  assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492
   187
  shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
nipkow@39302
   188
  apply (simp add: fun_eq_iff)
kaliszyk@36154
   189
  apply clarify
kaliszyk@36154
   190
  apply (induct_tac xa xb rule: list_induct2')
kuncar@47308
   191
  apply (simp_all add: Quotient3_abs_rep[OF a])
kaliszyk@36154
   192
  done
kaliszyk@36154
   193
haftmann@40820
   194
lemma [quot_preserve]:
kuncar@47308
   195
  assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492
   196
  shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kuncar@47308
   197
  by (induct l m rule: list_induct2') (simp_all add: Quotient3_rel_rep[OF a])
kaliszyk@36154
   198
kaliszyk@37492
   199
lemma list_all2_find_element:
kaliszyk@36276
   200
  assumes a: "x \<in> set a"
kaliszyk@37492
   201
  and b: "list_all2 R a b"
kaliszyk@36276
   202
  shows "\<exists>y. (y \<in> set b \<and> R x y)"
huffman@45803
   203
  using b a by induct auto
kaliszyk@36276
   204
kaliszyk@37492
   205
lemma list_all2_refl:
kaliszyk@35222
   206
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@37492
   207
  shows "list_all2 R x x"
kaliszyk@35222
   208
  by (induct x) (auto simp add: a)
kaliszyk@35222
   209
kuncar@47308
   210
lemma list_quotient:
kuncar@47308
   211
  assumes "Quotient R Abs Rep T"
kuncar@47308
   212
  shows "Quotient (list_all2 R) (List.map Abs) (List.map Rep) (list_all2 T)"
kuncar@47308
   213
proof (rule QuotientI)
kuncar@47308
   214
  from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient_abs_rep)
kuncar@47308
   215
  then show "\<And>xs. List.map Abs (List.map Rep xs) = xs" by (simp add: comp_def)
kuncar@47308
   216
next
kuncar@47308
   217
  from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient_rel_rep)
kuncar@47308
   218
  then show "\<And>xs. list_all2 R (List.map Rep xs) (List.map Rep xs)"
kuncar@47308
   219
    by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
kuncar@47308
   220
next
kuncar@47308
   221
  fix xs ys
kuncar@47308
   222
  from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient_rel)
kuncar@47308
   223
  then show "list_all2 R xs ys \<longleftrightarrow> list_all2 R xs xs \<and> list_all2 R ys ys \<and> List.map Abs xs = List.map Abs ys"
kuncar@47308
   224
    by (induct xs ys rule: list_induct2') auto
kuncar@47308
   225
next
kuncar@47308
   226
  {
kuncar@47308
   227
    fix l1 l2
kuncar@47308
   228
    have "List.length l1 = List.length l2 \<Longrightarrow>
kuncar@47308
   229
         (\<forall>(x, y)\<in>set (zip l1 l1). R x y) = (\<forall>(x, y)\<in>set (zip l1 l2). R x x)"
kuncar@47308
   230
     by (induction rule: list_induct2) auto
kuncar@47308
   231
  } note x = this
kuncar@47308
   232
  {
kuncar@47308
   233
    fix f g
kuncar@47308
   234
    have "list_all2 (\<lambda>x y. f x y \<and> g x y) = (\<lambda> x y. list_all2 f x y \<and> list_all2 g x y)"
kuncar@47308
   235
      by (intro ext) (auto simp add: list_all2_def)
kuncar@47308
   236
  } note list_all2_conj = this
kuncar@47308
   237
  from assms have t: "T = (\<lambda>x y. R x x \<and> Abs x = y)" by (rule Quotient_cr_rel)
kuncar@47308
   238
  show "list_all2 T = (\<lambda>x y. list_all2 R x x \<and> List.map Abs x = y)" 
kuncar@47308
   239
    apply (simp add: t list_all2_conj[symmetric])
kuncar@47308
   240
    apply (rule sym) 
kuncar@47308
   241
    apply (simp add: list_all2_conj) 
kuncar@47308
   242
    apply(intro ext) 
kuncar@47308
   243
    apply (intro rev_conj_cong)
kuncar@47308
   244
      unfolding list_all2_def apply (metis List.list_all2_eq list_all2_def list_all2_map1)
kuncar@47308
   245
    apply (drule conjunct1) 
kuncar@47308
   246
    apply (intro conj_cong) 
kuncar@47308
   247
      apply simp 
kuncar@47308
   248
    apply(simp add: x)
kuncar@47308
   249
  done
kuncar@47308
   250
qed
kuncar@47308
   251
kuncar@47308
   252
declare [[map list = (list_all2, list_quotient)]]
kuncar@47308
   253
kaliszyk@35222
   254
end