src/HOL/Library/Quotient_List.thy
author wenzelm
Sun Mar 14 14:31:24 2010 +0100 (2010-03-14)
changeset 35788 f1deaca15ca3
parent 35222 4f1fba00f66d
child 36154 11c6106d7787
permissions -rw-r--r--
observe standard header format;
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
kaliszyk@35222
   134
lemma map_prs[quot_preserve]:
kaliszyk@35222
   135
  assumes a: "Quotient R1 abs1 rep1"
kaliszyk@35222
   136
  and     b: "Quotient R2 abs2 rep2"
kaliszyk@35222
   137
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@35222
   138
  by (simp only: expand_fun_eq fun_map_def map_prs_aux[OF a b])
kaliszyk@35222
   139
     (simp)
kaliszyk@35222
   140
kaliszyk@35222
   141
kaliszyk@35222
   142
lemma map_rsp[quot_respect]:
kaliszyk@35222
   143
  assumes q1: "Quotient R1 Abs1 Rep1"
kaliszyk@35222
   144
  and     q2: "Quotient R2 Abs2 Rep2"
kaliszyk@35222
   145
  shows "((R1 ===> R2) ===> (list_rel R1) ===> list_rel R2) map map"
kaliszyk@35222
   146
  apply(simp)
kaliszyk@35222
   147
  apply(rule allI)+
kaliszyk@35222
   148
  apply(rule impI)
kaliszyk@35222
   149
  apply(rule allI)+
kaliszyk@35222
   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@35222
   220
lemma list_rel_eq[id_simps]:
kaliszyk@35222
   221
  shows "(list_rel (op =)) = (op =)"
kaliszyk@35222
   222
  unfolding expand_fun_eq
kaliszyk@35222
   223
  apply(rule allI)+
kaliszyk@35222
   224
  apply(induct_tac x xa rule: list_induct2')
kaliszyk@35222
   225
  apply(simp_all)
kaliszyk@35222
   226
  done
kaliszyk@35222
   227
kaliszyk@35222
   228
lemma list_rel_refl:
kaliszyk@35222
   229
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@35222
   230
  shows "list_rel R x x"
kaliszyk@35222
   231
  by (induct x) (auto simp add: a)
kaliszyk@35222
   232
kaliszyk@35222
   233
end