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