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