src/HOL/Library/Quotient_List.thy
author Cezary Kaliszyk <kaliszyk@in.tum.de>
Fri Feb 19 13:54:19 2010 +0100 (2010-02-19)
changeset 35222 4f1fba00f66d
child 35788 f1deaca15ca3
permissions -rw-r--r--
Initial version of HOL quotient package.
kaliszyk@35222
     1
(*  Title:      Quotient_List.thy
kaliszyk@35222
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222
     3
*)
kaliszyk@35222
     4
theory Quotient_List
kaliszyk@35222
     5
imports Main Quotient_Syntax
kaliszyk@35222
     6
begin
kaliszyk@35222
     7
kaliszyk@35222
     8
section {* Quotient infrastructure for the list type. *}
kaliszyk@35222
     9
kaliszyk@35222
    10
fun
kaliszyk@35222
    11
  list_rel
kaliszyk@35222
    12
where
kaliszyk@35222
    13
  "list_rel R [] [] = True"
kaliszyk@35222
    14
| "list_rel R (x#xs) [] = False"
kaliszyk@35222
    15
| "list_rel R [] (x#xs) = False"
kaliszyk@35222
    16
| "list_rel R (x#xs) (y#ys) = (R x y \<and> list_rel R xs ys)"
kaliszyk@35222
    17
kaliszyk@35222
    18
declare [[map list = (map, list_rel)]]
kaliszyk@35222
    19
kaliszyk@35222
    20
lemma split_list_all:
kaliszyk@35222
    21
  shows "(\<forall>x. P x) \<longleftrightarrow> P [] \<and> (\<forall>x xs. P (x#xs))"
kaliszyk@35222
    22
  apply(auto)
kaliszyk@35222
    23
  apply(case_tac x)
kaliszyk@35222
    24
  apply(simp_all)
kaliszyk@35222
    25
  done
kaliszyk@35222
    26
kaliszyk@35222
    27
lemma map_id[id_simps]:
kaliszyk@35222
    28
  shows "map id = id"
kaliszyk@35222
    29
  apply(simp add: expand_fun_eq)
kaliszyk@35222
    30
  apply(rule allI)
kaliszyk@35222
    31
  apply(induct_tac x)
kaliszyk@35222
    32
  apply(simp_all)
kaliszyk@35222
    33
  done
kaliszyk@35222
    34
kaliszyk@35222
    35
kaliszyk@35222
    36
lemma list_rel_reflp:
kaliszyk@35222
    37
  shows "equivp R \<Longrightarrow> list_rel R xs xs"
kaliszyk@35222
    38
  apply(induct xs)
kaliszyk@35222
    39
  apply(simp_all add: equivp_reflp)
kaliszyk@35222
    40
  done
kaliszyk@35222
    41
kaliszyk@35222
    42
lemma list_rel_symp:
kaliszyk@35222
    43
  assumes a: "equivp R"
kaliszyk@35222
    44
  shows "list_rel R xs ys \<Longrightarrow> list_rel R ys xs"
kaliszyk@35222
    45
  apply(induct xs ys rule: list_induct2')
kaliszyk@35222
    46
  apply(simp_all)
kaliszyk@35222
    47
  apply(rule equivp_symp[OF a])
kaliszyk@35222
    48
  apply(simp)
kaliszyk@35222
    49
  done
kaliszyk@35222
    50
kaliszyk@35222
    51
lemma list_rel_transp:
kaliszyk@35222
    52
  assumes a: "equivp R"
kaliszyk@35222
    53
  shows "list_rel R xs1 xs2 \<Longrightarrow> list_rel R xs2 xs3 \<Longrightarrow> list_rel R xs1 xs3"
kaliszyk@35222
    54
  apply(induct xs1 xs2 arbitrary: xs3 rule: list_induct2')
kaliszyk@35222
    55
  apply(simp_all)
kaliszyk@35222
    56
  apply(case_tac xs3)
kaliszyk@35222
    57
  apply(simp_all)
kaliszyk@35222
    58
  apply(rule equivp_transp[OF a])
kaliszyk@35222
    59
  apply(auto)
kaliszyk@35222
    60
  done
kaliszyk@35222
    61
kaliszyk@35222
    62
lemma list_equivp[quot_equiv]:
kaliszyk@35222
    63
  assumes a: "equivp R"
kaliszyk@35222
    64
  shows "equivp (list_rel R)"
kaliszyk@35222
    65
  apply(rule equivpI)
kaliszyk@35222
    66
  unfolding reflp_def symp_def transp_def
kaliszyk@35222
    67
  apply(subst split_list_all)
kaliszyk@35222
    68
  apply(simp add: equivp_reflp[OF a] list_rel_reflp[OF a])
kaliszyk@35222
    69
  apply(blast intro: list_rel_symp[OF a])
kaliszyk@35222
    70
  apply(blast intro: list_rel_transp[OF a])
kaliszyk@35222
    71
  done
kaliszyk@35222
    72
kaliszyk@35222
    73
lemma list_rel_rel:
kaliszyk@35222
    74
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    75
  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
    76
  apply(induct r s rule: list_induct2')
kaliszyk@35222
    77
  apply(simp_all)
kaliszyk@35222
    78
  using Quotient_rel[OF q]
kaliszyk@35222
    79
  apply(metis)
kaliszyk@35222
    80
  done
kaliszyk@35222
    81
kaliszyk@35222
    82
lemma list_quotient[quot_thm]:
kaliszyk@35222
    83
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
    84
  shows "Quotient (list_rel R) (map Abs) (map Rep)"
kaliszyk@35222
    85
  unfolding Quotient_def
kaliszyk@35222
    86
  apply(subst split_list_all)
kaliszyk@35222
    87
  apply(simp add: Quotient_abs_rep[OF q] abs_o_rep[OF q] map_id)
kaliszyk@35222
    88
  apply(rule conjI)
kaliszyk@35222
    89
  apply(rule allI)
kaliszyk@35222
    90
  apply(induct_tac a)
kaliszyk@35222
    91
  apply(simp)
kaliszyk@35222
    92
  apply(simp)
kaliszyk@35222
    93
  apply(simp add: Quotient_rep_reflp[OF q])
kaliszyk@35222
    94
  apply(rule allI)+
kaliszyk@35222
    95
  apply(rule list_rel_rel[OF q])
kaliszyk@35222
    96
  done
kaliszyk@35222
    97
kaliszyk@35222
    98
kaliszyk@35222
    99
lemma cons_prs_aux:
kaliszyk@35222
   100
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   101
  shows "(map Abs) ((Rep h) # (map Rep t)) = h # t"
kaliszyk@35222
   102
  by (induct t) (simp_all add: Quotient_abs_rep[OF q])
kaliszyk@35222
   103
kaliszyk@35222
   104
lemma cons_prs[quot_preserve]:
kaliszyk@35222
   105
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   106
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
kaliszyk@35222
   107
  by (simp only: expand_fun_eq fun_map_def cons_prs_aux[OF q])
kaliszyk@35222
   108
     (simp)
kaliszyk@35222
   109
kaliszyk@35222
   110
lemma cons_rsp[quot_respect]:
kaliszyk@35222
   111
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   112
  shows "(R ===> list_rel R ===> list_rel R) (op #) (op #)"
kaliszyk@35222
   113
  by (auto)
kaliszyk@35222
   114
kaliszyk@35222
   115
lemma nil_prs[quot_preserve]:
kaliszyk@35222
   116
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   117
  shows "map Abs [] = []"
kaliszyk@35222
   118
  by simp
kaliszyk@35222
   119
kaliszyk@35222
   120
lemma nil_rsp[quot_respect]:
kaliszyk@35222
   121
  assumes q: "Quotient R Abs Rep"
kaliszyk@35222
   122
  shows "list_rel R [] []"
kaliszyk@35222
   123
  by simp
kaliszyk@35222
   124
kaliszyk@35222
   125
lemma map_prs_aux:
kaliszyk@35222
   126
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   127
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   128
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
   129
  by (induct l)
kaliszyk@35222
   130
     (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   131
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@35222
   137
  by (simp only: expand_fun_eq fun_map_def map_prs_aux[OF a b])
kaliszyk@35222
   138
     (simp)
kaliszyk@35222
   139
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@35222
   145
  apply(simp)
kaliszyk@35222
   146
  apply(rule allI)+
kaliszyk@35222
   147
  apply(rule impI)
kaliszyk@35222
   148
  apply(rule allI)+
kaliszyk@35222
   149
  apply (induct_tac xa ya rule: list_induct2')
kaliszyk@35222
   150
  apply simp_all
kaliszyk@35222
   151
  done
kaliszyk@35222
   152
kaliszyk@35222
   153
lemma foldr_prs_aux:
kaliszyk@35222
   154
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   155
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   156
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kaliszyk@35222
   157
  by (induct l) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   158
kaliszyk@35222
   159
lemma foldr_prs[quot_preserve]:
kaliszyk@35222
   160
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   161
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   162
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
kaliszyk@35222
   163
  by (simp only: expand_fun_eq fun_map_def foldr_prs_aux[OF a b])
kaliszyk@35222
   164
     (simp)
kaliszyk@35222
   165
kaliszyk@35222
   166
lemma foldl_prs_aux:
kaliszyk@35222
   167
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   168
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   169
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kaliszyk@35222
   170
  by (induct l arbitrary:e) (simp_all add: Quotient_abs_rep[OF a] Quotient_abs_rep[OF b])
kaliszyk@35222
   171
kaliszyk@35222
   172
kaliszyk@35222
   173
lemma foldl_prs[quot_preserve]:
kaliszyk@35222
   174
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   175
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   176
  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
kaliszyk@35222
   177
  by (simp only: expand_fun_eq fun_map_def foldl_prs_aux[OF a b])
kaliszyk@35222
   178
     (simp)
kaliszyk@35222
   179
kaliszyk@35222
   180
lemma list_rel_empty:
kaliszyk@35222
   181
  shows "list_rel R [] b \<Longrightarrow> length b = 0"
kaliszyk@35222
   182
  by (induct b) (simp_all)
kaliszyk@35222
   183
kaliszyk@35222
   184
lemma list_rel_len:
kaliszyk@35222
   185
  shows "list_rel R a b \<Longrightarrow> length a = length b"
kaliszyk@35222
   186
  apply (induct a arbitrary: b)
kaliszyk@35222
   187
  apply (simp add: list_rel_empty)
kaliszyk@35222
   188
  apply (case_tac b)
kaliszyk@35222
   189
  apply simp_all
kaliszyk@35222
   190
  done
kaliszyk@35222
   191
kaliszyk@35222
   192
(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222
   193
lemma foldl_rsp[quot_respect]:
kaliszyk@35222
   194
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   195
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   196
  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_rel R2 ===> R1) foldl foldl"
kaliszyk@35222
   197
  apply(auto)
kaliszyk@35222
   198
  apply (subgoal_tac "R1 xa ya \<longrightarrow> list_rel R2 xb yb \<longrightarrow> R1 (foldl x xa xb) (foldl y ya yb)")
kaliszyk@35222
   199
  apply simp
kaliszyk@35222
   200
  apply (rule_tac x="xa" in spec)
kaliszyk@35222
   201
  apply (rule_tac x="ya" in spec)
kaliszyk@35222
   202
  apply (rule_tac xs="xb" and ys="yb" in list_induct2)
kaliszyk@35222
   203
  apply (rule list_rel_len)
kaliszyk@35222
   204
  apply (simp_all)
kaliszyk@35222
   205
  done
kaliszyk@35222
   206
kaliszyk@35222
   207
lemma foldr_rsp[quot_respect]:
kaliszyk@35222
   208
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   209
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   210
  shows "((R1 ===> R2 ===> R2) ===> list_rel R1 ===> R2 ===> R2) foldr foldr"
kaliszyk@35222
   211
  apply auto
kaliszyk@35222
   212
  apply(subgoal_tac "R2 xb yb \<longrightarrow> list_rel R1 xa ya \<longrightarrow> R2 (foldr x xa xb) (foldr y ya yb)")
kaliszyk@35222
   213
  apply simp
kaliszyk@35222
   214
  apply (rule_tac xs="xa" and ys="ya" in list_induct2)
kaliszyk@35222
   215
  apply (rule list_rel_len)
kaliszyk@35222
   216
  apply (simp_all)
kaliszyk@35222
   217
  done
kaliszyk@35222
   218
kaliszyk@35222
   219
lemma list_rel_eq[id_simps]:
kaliszyk@35222
   220
  shows "(list_rel (op =)) = (op =)"
kaliszyk@35222
   221
  unfolding expand_fun_eq
kaliszyk@35222
   222
  apply(rule allI)+
kaliszyk@35222
   223
  apply(induct_tac x xa rule: list_induct2')
kaliszyk@35222
   224
  apply(simp_all)
kaliszyk@35222
   225
  done
kaliszyk@35222
   226
kaliszyk@35222
   227
lemma list_rel_refl:
kaliszyk@35222
   228
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@35222
   229
  shows "list_rel R x x"
kaliszyk@35222
   230
  by (induct x) (auto simp add: a)
kaliszyk@35222
   231
kaliszyk@35222
   232
end