src/HOL/Library/Quotient_List.thy
author Christian Urban <urbanc@in.tum.de>
Tue May 11 07:45:47 2010 +0100 (2010-05-11)
changeset 36812 e090bdb4e1c5
parent 36276 92011cc923f5
child 37492 ab36b1a50ca8
permissions -rw-r--r--
tuned proof so that no simplifier warning is printed
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@35222
    11
fun
kaliszyk@35222
    12
  list_rel
kaliszyk@35222
    13
where
kaliszyk@35222
    14
  "list_rel R [] [] = True"
kaliszyk@35222
    15
| "list_rel R (x#xs) [] = False"
kaliszyk@35222
    16
| "list_rel R [] (x#xs) = False"
kaliszyk@35222
    17
| "list_rel R (x#xs) (y#ys) = (R x y \<and> list_rel R xs ys)"
kaliszyk@35222
    18
kaliszyk@35222
    19
declare [[map list = (map, list_rel)]]
kaliszyk@35222
    20
kaliszyk@35222
    21
lemma split_list_all:
kaliszyk@35222
    22
  shows "(\<forall>x. P x) \<longleftrightarrow> P [] \<and> (\<forall>x xs. P (x#xs))"
kaliszyk@35222
    23
  apply(auto)
kaliszyk@35222
    24
  apply(case_tac x)
kaliszyk@35222
    25
  apply(simp_all)
kaliszyk@35222
    26
  done
kaliszyk@35222
    27
kaliszyk@35222
    28
lemma map_id[id_simps]:
kaliszyk@35222
    29
  shows "map id = id"
kaliszyk@35222
    30
  apply(simp add: expand_fun_eq)
kaliszyk@35222
    31
  apply(rule allI)
kaliszyk@35222
    32
  apply(induct_tac x)
kaliszyk@35222
    33
  apply(simp_all)
kaliszyk@35222
    34
  done
kaliszyk@35222
    35
kaliszyk@35222
    36
kaliszyk@35222
    37
lemma list_rel_reflp:
kaliszyk@35222
    38
  shows "equivp R \<Longrightarrow> list_rel R xs xs"
kaliszyk@35222
    39
  apply(induct xs)
kaliszyk@35222
    40
  apply(simp_all add: equivp_reflp)
kaliszyk@35222
    41
  done
kaliszyk@35222
    42
kaliszyk@35222
    43
lemma list_rel_symp:
kaliszyk@35222
    44
  assumes a: "equivp R"
kaliszyk@35222
    45
  shows "list_rel R xs ys \<Longrightarrow> list_rel R ys xs"
kaliszyk@35222
    46
  apply(induct xs ys rule: list_induct2')
kaliszyk@35222
    47
  apply(simp_all)
kaliszyk@35222
    48
  apply(rule equivp_symp[OF a])
kaliszyk@35222
    49
  apply(simp)
kaliszyk@35222
    50
  done
kaliszyk@35222
    51
kaliszyk@35222
    52
lemma list_rel_transp:
kaliszyk@35222
    53
  assumes a: "equivp R"
kaliszyk@35222
    54
  shows "list_rel R xs1 xs2 \<Longrightarrow> list_rel R xs2 xs3 \<Longrightarrow> list_rel R xs1 xs3"
urbanc@36812
    55
  using a
urbanc@36812
    56
  apply(induct R xs1 xs2 arbitrary: xs3 rule: list_rel.induct)
urbanc@36812
    57
  apply(simp)
urbanc@36812
    58
  apply(simp)
urbanc@36812
    59
  apply(simp)
kaliszyk@35222
    60
  apply(case_tac xs3)
urbanc@36812
    61
  apply(clarify)
urbanc@36812
    62
  apply(simp (no_asm_use))
urbanc@36812
    63
  apply(clarify)
urbanc@36812
    64
  apply(simp (no_asm_use))
urbanc@36812
    65
  apply(auto intro: equivp_transp)
kaliszyk@35222
    66
  done
kaliszyk@35222
    67
kaliszyk@35222
    68
lemma list_equivp[quot_equiv]:
kaliszyk@35222
    69
  assumes a: "equivp R"
kaliszyk@35222
    70
  shows "equivp (list_rel R)"
kaliszyk@35222
    71
  apply(rule equivpI)
kaliszyk@35222
    72
  unfolding reflp_def symp_def transp_def
kaliszyk@35222
    73
  apply(subst split_list_all)
kaliszyk@35222
    74
  apply(simp add: equivp_reflp[OF a] list_rel_reflp[OF a])
kaliszyk@35222
    75
  apply(blast intro: list_rel_symp[OF a])
kaliszyk@35222
    76
  apply(blast intro: list_rel_transp[OF a])
kaliszyk@35222
    77
  done
kaliszyk@35222
    78
kaliszyk@35222
    79
lemma list_rel_rel:
kaliszyk@35222
    80
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    81
  shows "list_rel R r s = (list_rel R r r \<and> list_rel R s s \<and> (map Abs r = map Abs s))"
kaliszyk@35222
    82
  apply(induct r s rule: list_induct2')
kaliszyk@35222
    83
  apply(simp_all)
kaliszyk@35222
    84
  using Quotient_rel[OF q]
kaliszyk@35222
    85
  apply(metis)
kaliszyk@35222
    86
  done
kaliszyk@35222
    87
kaliszyk@35222
    88
lemma list_quotient[quot_thm]:
kaliszyk@35222
    89
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    90
  shows "Quotient (list_rel R) (map Abs) (map Rep)"
kaliszyk@35222
    91
  unfolding Quotient_def
kaliszyk@35222
    92
  apply(subst split_list_all)
kaliszyk@35222
    93
  apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
kaliszyk@35222
    94
  apply(rule conjI)
kaliszyk@35222
    95
  apply(rule allI)
kaliszyk@35222
    96
  apply(induct_tac a)
kaliszyk@35222
    97
  apply(simp)
kaliszyk@35222
    98
  apply(simp)
kaliszyk@35222
    99
  apply(simp add: Quotient_rep_reflp[OF q])
kaliszyk@35222
   100
  apply(rule allI)+
kaliszyk@35222
   101
  apply(rule list_rel_rel[OF q])
kaliszyk@35222
   102
  done
kaliszyk@35222
   103
kaliszyk@35222
   104
kaliszyk@35222
   105
lemma cons_prs_aux:
kaliszyk@35222
   106
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   107
  shows "(map Abs) ((Rep h) # (map Rep t)) = h # t"
kaliszyk@35222
   108
  by (induct t) (simp_all add: Quotient_abs_rep[OF q])
kaliszyk@35222
   109
kaliszyk@35222
   110
lemma cons_prs[quot_preserve]:
kaliszyk@35222
   111
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   112
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
kaliszyk@35222
   113
  by (simp only: expand_fun_eq fun_map_def cons_prs_aux[OF q])
kaliszyk@35222
   114
     (simp)
kaliszyk@35222
   115
kaliszyk@35222
   116
lemma cons_rsp[quot_respect]:
kaliszyk@35222
   117
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   118
  shows "(R ===> list_rel R ===> list_rel R) (op #) (op #)"
kaliszyk@35222
   119
  by (auto)
kaliszyk@35222
   120
kaliszyk@35222
   121
lemma nil_prs[quot_preserve]:
kaliszyk@35222
   122
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   123
  shows "map Abs [] = []"
kaliszyk@35222
   124
  by simp
kaliszyk@35222
   125
kaliszyk@35222
   126
lemma nil_rsp[quot_respect]:
kaliszyk@35222
   127
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   128
  shows "list_rel R [] []"
kaliszyk@35222
   129
  by simp
kaliszyk@35222
   130
kaliszyk@35222
   131
lemma map_prs_aux:
kaliszyk@35222
   132
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   133
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   134
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
   135
  by (induct l)
kaliszyk@35222
   136
     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   137
kaliszyk@35222
   138
lemma map_prs[quot_preserve]:
kaliszyk@35222
   139
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   140
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   141
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216
   142
  and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
kaliszyk@36216
   143
  by (simp_all only: expand_fun_eq fun_map_def map_prs_aux[OF a b])
kaliszyk@36216
   144
     (simp_all add: Quotient_abs_rep[OF a])
kaliszyk@35222
   145
kaliszyk@35222
   146
lemma map_rsp[quot_respect]:
kaliszyk@35222
   147
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   148
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   149
  shows "((R1 ===> R2) ===> (list_rel R1) ===> list_rel R2) map map"
kaliszyk@36216
   150
  and   "((R1 ===> op =) ===> (list_rel R1) ===> op =) map map"
kaliszyk@36216
   151
  apply simp_all
kaliszyk@36216
   152
  apply(rule_tac [!] allI)+
kaliszyk@36216
   153
  apply(rule_tac [!] impI)
kaliszyk@36216
   154
  apply(rule_tac [!] allI)+
kaliszyk@36216
   155
  apply (induct_tac [!] xa ya rule: list_induct2')
kaliszyk@35222
   156
  apply simp_all
kaliszyk@35222
   157
  done
kaliszyk@35222
   158
kaliszyk@35222
   159
lemma foldr_prs_aux:
kaliszyk@35222
   160
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   161
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   162
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kaliszyk@35222
   163
  by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   164
kaliszyk@35222
   165
lemma foldr_prs[quot_preserve]:
kaliszyk@35222
   166
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   167
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   168
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
kaliszyk@35222
   169
  by (simp only: expand_fun_eq fun_map_def foldr_prs_aux[OF a b])
kaliszyk@35222
   170
     (simp)
kaliszyk@35222
   171
kaliszyk@35222
   172
lemma foldl_prs_aux:
kaliszyk@35222
   173
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   174
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   175
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kaliszyk@35222
   176
  by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   177
kaliszyk@35222
   178
kaliszyk@35222
   179
lemma foldl_prs[quot_preserve]:
kaliszyk@35222
   180
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   181
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   182
  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
kaliszyk@35222
   183
  by (simp only: expand_fun_eq fun_map_def foldl_prs_aux[OF a b])
kaliszyk@35222
   184
     (simp)
kaliszyk@35222
   185
kaliszyk@35222
   186
lemma list_rel_empty:
kaliszyk@35222
   187
  shows "list_rel R [] b \<Longrightarrow> length b = 0"
kaliszyk@35222
   188
  by (induct b) (simp_all)
kaliszyk@35222
   189
kaliszyk@35222
   190
lemma list_rel_len:
kaliszyk@35222
   191
  shows "list_rel R a b \<Longrightarrow> length a = length b"
kaliszyk@35222
   192
  apply (induct a arbitrary: b)
kaliszyk@35222
   193
  apply (simp add: list_rel_empty)
kaliszyk@35222
   194
  apply (case_tac b)
kaliszyk@35222
   195
  apply simp_all
kaliszyk@35222
   196
  done
kaliszyk@35222
   197
kaliszyk@35222
   198
(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222
   199
lemma foldl_rsp[quot_respect]:
kaliszyk@35222
   200
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   201
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   202
  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_rel R2 ===> R1) foldl foldl"
kaliszyk@35222
   203
  apply(auto)
kaliszyk@35222
   204
  apply (subgoal_tac "R1 xa ya \<longrightarrow> list_rel R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
kaliszyk@35222
   205
  apply simp
kaliszyk@35222
   206
  apply (rule_tac x="xa" in spec)
kaliszyk@35222
   207
  apply (rule_tac x="ya" in spec)
kaliszyk@35222
   208
  apply (rule_tac xs="xb" and ys="yb" in list_induct2)
kaliszyk@35222
   209
  apply (rule list_rel_len)
kaliszyk@35222
   210
  apply (simp_all)
kaliszyk@35222
   211
  done
kaliszyk@35222
   212
kaliszyk@35222
   213
lemma foldr_rsp[quot_respect]:
kaliszyk@35222
   214
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   215
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   216
  shows "((R1 ===> R2 ===> R2) ===> list_rel R1 ===> R2 ===> R2) foldr foldr"
kaliszyk@35222
   217
  apply auto
kaliszyk@35222
   218
  apply(subgoal_tac "R2 xb yb \<longrightarrow> list_rel R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
kaliszyk@35222
   219
  apply simp
kaliszyk@35222
   220
  apply (rule_tac xs="xa" and ys="ya" in list_induct2)
kaliszyk@35222
   221
  apply (rule list_rel_len)
kaliszyk@35222
   222
  apply (simp_all)
kaliszyk@35222
   223
  done
kaliszyk@35222
   224
kaliszyk@36154
   225
lemma list_rel_rsp:
kaliszyk@36154
   226
  assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
kaliszyk@36154
   227
  and l1: "list_rel R x y"
kaliszyk@36154
   228
  and l2: "list_rel R a b"
kaliszyk@36154
   229
  shows "list_rel S x a = list_rel T y b"
kaliszyk@36154
   230
  proof -
kaliszyk@36154
   231
    have a: "length y = length x" by (rule list_rel_len[OF l1, symmetric])
kaliszyk@36154
   232
    have c: "length a = length b" by (rule list_rel_len[OF l2])
kaliszyk@36154
   233
    show ?thesis proof (cases "length x = length a")
kaliszyk@36154
   234
      case True
kaliszyk@36154
   235
      have b: "length x = length a" by fact
kaliszyk@36154
   236
      show ?thesis using a b c r l1 l2 proof (induct rule: list_induct4)
kaliszyk@36154
   237
        case Nil
kaliszyk@36154
   238
        show ?case using assms by simp
kaliszyk@36154
   239
      next
kaliszyk@36154
   240
        case (Cons h t)
kaliszyk@36154
   241
        then show ?case by auto
kaliszyk@36154
   242
      qed
kaliszyk@36154
   243
    next
kaliszyk@36154
   244
      case False
kaliszyk@36154
   245
      have d: "length x \<noteq> length a" by fact
kaliszyk@36154
   246
      then have e: "\<not>list_rel S x a" using list_rel_len by auto
kaliszyk@36154
   247
      have "length y \<noteq> length b" using d a c by simp
kaliszyk@36154
   248
      then have "\<not>list_rel T y b" using list_rel_len by auto
kaliszyk@36154
   249
      then show ?thesis using e by simp
kaliszyk@36154
   250
    qed
kaliszyk@36154
   251
  qed
kaliszyk@36154
   252
kaliszyk@36154
   253
lemma[quot_respect]:
kaliszyk@36154
   254
  "((R ===> R ===> op =) ===> list_rel R ===> list_rel R ===> op =) list_rel list_rel"
kaliszyk@36154
   255
  by (simp add: list_rel_rsp)
kaliszyk@36154
   256
kaliszyk@36154
   257
lemma[quot_preserve]:
kaliszyk@36154
   258
  assumes a: "Quotient R abs1 rep1"
kaliszyk@36154
   259
  shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_rel = list_rel"
kaliszyk@36154
   260
  apply (simp add: expand_fun_eq)
kaliszyk@36154
   261
  apply clarify
kaliszyk@36154
   262
  apply (induct_tac xa xb rule: list_induct2')
kaliszyk@36154
   263
  apply (simp_all add: Quotient_abs_rep[OF a])
kaliszyk@36154
   264
  done
kaliszyk@36154
   265
kaliszyk@36154
   266
lemma[quot_preserve]:
kaliszyk@36154
   267
  assumes a: "Quotient R abs1 rep1"
kaliszyk@36154
   268
  shows "(list_rel ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kaliszyk@36154
   269
  by (induct l m rule: list_induct2') (simp_all add: Quotient_rel_rep[OF a])
kaliszyk@36154
   270
kaliszyk@35222
   271
lemma list_rel_eq[id_simps]:
kaliszyk@35222
   272
  shows "(list_rel (op =)) = (op =)"
kaliszyk@35222
   273
  unfolding expand_fun_eq
kaliszyk@35222
   274
  apply(rule allI)+
kaliszyk@35222
   275
  apply(induct_tac x xa rule: list_induct2')
kaliszyk@35222
   276
  apply(simp_all)
kaliszyk@35222
   277
  done
kaliszyk@35222
   278
kaliszyk@36276
   279
lemma list_rel_find_element:
kaliszyk@36276
   280
  assumes a: "x \<in> set a"
kaliszyk@36276
   281
  and b: "list_rel R a b"
kaliszyk@36276
   282
  shows "\<exists>y. (y \<in> set b \<and> R x y)"
kaliszyk@36276
   283
proof -
kaliszyk@36276
   284
  have "length a = length b" using b by (rule list_rel_len)
kaliszyk@36276
   285
  then show ?thesis using a b by (induct a b rule: list_induct2) auto
kaliszyk@36276
   286
qed
kaliszyk@36276
   287
kaliszyk@35222
   288
lemma list_rel_refl:
kaliszyk@35222
   289
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@35222
   290
  shows "list_rel R x x"
kaliszyk@35222
   291
  by (induct x) (auto simp add: a)
kaliszyk@35222
   292
kaliszyk@35222
   293
end