src/HOL/Library/Quotient_List.thy
author haftmann
Tue Nov 09 14:02:12 2010 +0100 (2010-11-09)
changeset 40463 75e544159549
parent 40032 5f78dfb2fa7d
child 40820 fd9c98ead9a9
permissions -rw-r--r--
fun_rel_def is no simp rule by default
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[quot_preserve]:
kaliszyk@35222
    86
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    87
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
haftmann@40463
    88
  by (auto simp add: fun_eq_iff comp_def Quotient_abs_rep [OF q])
kaliszyk@35222
    89
kaliszyk@35222
    90
lemma cons_rsp[quot_respect]:
kaliszyk@35222
    91
  assumes q: "Quotient R Abs Rep"
kaliszyk@37492
    92
  shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
haftmann@40463
    93
  by auto
kaliszyk@35222
    94
kaliszyk@35222
    95
lemma nil_prs[quot_preserve]:
kaliszyk@35222
    96
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    97
  shows "map Abs [] = []"
kaliszyk@35222
    98
  by simp
kaliszyk@35222
    99
kaliszyk@35222
   100
lemma nil_rsp[quot_respect]:
kaliszyk@35222
   101
  assumes q: "Quotient R Abs Rep"
kaliszyk@37492
   102
  shows "list_all2 R [] []"
kaliszyk@35222
   103
  by simp
kaliszyk@35222
   104
kaliszyk@35222
   105
lemma map_prs_aux:
kaliszyk@35222
   106
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   107
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   108
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
   109
  by (induct l)
kaliszyk@35222
   110
     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   111
kaliszyk@35222
   112
lemma map_prs[quot_preserve]:
kaliszyk@35222
   113
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   114
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   115
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216
   116
  and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
haftmann@40463
   117
  by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
haftmann@40463
   118
    (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
haftmann@40463
   119
kaliszyk@35222
   120
kaliszyk@35222
   121
lemma map_rsp[quot_respect]:
kaliszyk@35222
   122
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   123
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@37492
   124
  shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
kaliszyk@37492
   125
  and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
haftmann@40463
   126
  apply (simp_all add: fun_rel_def)
kaliszyk@36216
   127
  apply(rule_tac [!] allI)+
kaliszyk@36216
   128
  apply(rule_tac [!] impI)
kaliszyk@36216
   129
  apply(rule_tac [!] allI)+
kaliszyk@36216
   130
  apply (induct_tac [!] xa ya rule: list_induct2')
kaliszyk@35222
   131
  apply simp_all
kaliszyk@35222
   132
  done
kaliszyk@35222
   133
kaliszyk@35222
   134
lemma foldr_prs_aux:
kaliszyk@35222
   135
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   136
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   137
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kaliszyk@35222
   138
  by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   139
kaliszyk@35222
   140
lemma foldr_prs[quot_preserve]:
kaliszyk@35222
   141
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   142
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   143
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
haftmann@40463
   144
  apply (simp add: fun_eq_iff)
haftmann@40463
   145
  by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
kaliszyk@35222
   146
     (simp)
kaliszyk@35222
   147
kaliszyk@35222
   148
lemma foldl_prs_aux:
kaliszyk@35222
   149
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   150
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   151
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kaliszyk@35222
   152
  by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   153
kaliszyk@35222
   154
kaliszyk@35222
   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
kaliszyk@36154
   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
kaliszyk@36154
   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
kaliszyk@36154
   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_eq[id_simps]:
kaliszyk@37492
   239
  shows "(list_all2 (op =)) = (op =)"
nipkow@39302
   240
  unfolding fun_eq_iff
kaliszyk@35222
   241
  apply(rule allI)+
kaliszyk@35222
   242
  apply(induct_tac x xa rule: list_induct2')
kaliszyk@35222
   243
  apply(simp_all)
kaliszyk@35222
   244
  done
kaliszyk@35222
   245
kaliszyk@37492
   246
lemma list_all2_find_element:
kaliszyk@36276
   247
  assumes a: "x \<in> set a"
kaliszyk@37492
   248
  and b: "list_all2 R a b"
kaliszyk@36276
   249
  shows "\<exists>y. (y \<in> set b \<and> R x y)"
kaliszyk@36276
   250
proof -
kaliszyk@37492
   251
  have "length a = length b" using b by (rule list_all2_lengthD)
kaliszyk@36276
   252
  then show ?thesis using a b by (induct a b rule: list_induct2) auto
kaliszyk@36276
   253
qed
kaliszyk@36276
   254
kaliszyk@37492
   255
lemma list_all2_refl:
kaliszyk@35222
   256
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@37492
   257
  shows "list_all2 R x x"
kaliszyk@35222
   258
  by (induct x) (auto simp add: a)
kaliszyk@35222
   259
kaliszyk@35222
   260
end