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