src/HOL/Library/Quotient_List.thy
author haftmann
Fri Nov 01 18:51:14 2013 +0100 (2013-11-01)
changeset 54230 b1d955791529
parent 53012 cb82606b8215
child 55564 e81ee43ab290
permissions -rw-r--r--
more simplification rules on unary and binary minus
wenzelm@47455
     1
(*  Title:      HOL/Library/Quotient_List.thy
kuncar@53012
     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
huffman@47929
     8
imports Main Quotient_Set Quotient_Product Quotient_Option
kaliszyk@35222
     9
begin
kaliszyk@35222
    10
kuncar@53012
    11
subsection {* Rules for the Quotient package *}
huffman@47641
    12
haftmann@40820
    13
lemma map_id [id_simps]:
haftmann@40820
    14
  "map id = id"
haftmann@46663
    15
  by (fact List.map.id)
kaliszyk@35222
    16
kuncar@53012
    17
lemma list_all2_eq [id_simps]:
haftmann@40820
    18
  "list_all2 (op =) = (op =)"
haftmann@40820
    19
proof (rule ext)+
haftmann@40820
    20
  fix xs ys
haftmann@40820
    21
  show "list_all2 (op =) xs ys \<longleftrightarrow> xs = ys"
haftmann@40820
    22
    by (induct xs ys rule: list_induct2') simp_all
haftmann@40820
    23
qed
kaliszyk@35222
    24
haftmann@40820
    25
lemma list_symp:
haftmann@40820
    26
  assumes "symp R"
haftmann@40820
    27
  shows "symp (list_all2 R)"
haftmann@40820
    28
proof (rule sympI)
haftmann@40820
    29
  from assms have *: "\<And>xs ys. R xs ys \<Longrightarrow> R ys xs" by (rule sympE)
haftmann@40820
    30
  fix xs ys
haftmann@40820
    31
  assume "list_all2 R xs ys"
haftmann@40820
    32
  then show "list_all2 R ys xs"
haftmann@40820
    33
    by (induct xs ys rule: list_induct2') (simp_all add: *)
haftmann@40820
    34
qed
kaliszyk@35222
    35
haftmann@40820
    36
lemma list_transp:
haftmann@40820
    37
  assumes "transp R"
haftmann@40820
    38
  shows "transp (list_all2 R)"
haftmann@40820
    39
proof (rule transpI)
haftmann@40820
    40
  from assms have *: "\<And>xs ys zs. R xs ys \<Longrightarrow> R ys zs \<Longrightarrow> R xs zs" by (rule transpE)
haftmann@40820
    41
  fix xs ys zs
huffman@45803
    42
  assume "list_all2 R xs ys" and "list_all2 R ys zs"
huffman@45803
    43
  then show "list_all2 R xs zs"
huffman@45803
    44
    by (induct arbitrary: zs) (auto simp: list_all2_Cons1 intro: *)
haftmann@40820
    45
qed
kaliszyk@35222
    46
haftmann@40820
    47
lemma list_equivp [quot_equiv]:
haftmann@40820
    48
  "equivp R \<Longrightarrow> equivp (list_all2 R)"
kuncar@51994
    49
  by (blast intro: equivpI reflp_list_all2 list_symp list_transp elim: equivpE)
kaliszyk@35222
    50
kuncar@47308
    51
lemma list_quotient3 [quot_thm]:
kuncar@47308
    52
  assumes "Quotient3 R Abs Rep"
kuncar@47308
    53
  shows "Quotient3 (list_all2 R) (map Abs) (map Rep)"
kuncar@47308
    54
proof (rule Quotient3I)
kuncar@47308
    55
  from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
haftmann@40820
    56
  then show "\<And>xs. map Abs (map Rep xs) = xs" by (simp add: comp_def)
haftmann@40820
    57
next
kuncar@47308
    58
  from assms have "\<And>x y. R (Rep x) (Rep y) \<longleftrightarrow> x = y" by (rule Quotient3_rel_rep)
haftmann@40820
    59
  then show "\<And>xs. list_all2 R (map Rep xs) (map Rep xs)"
haftmann@40820
    60
    by (simp add: list_all2_map1 list_all2_map2 list_all2_eq)
haftmann@40820
    61
next
haftmann@40820
    62
  fix xs ys
kuncar@47308
    63
  from assms have "\<And>x y. R x x \<and> R y y \<and> Abs x = Abs y \<longleftrightarrow> R x y" by (rule Quotient3_rel)
haftmann@40820
    64
  then show "list_all2 R xs ys \<longleftrightarrow> list_all2 R xs xs \<and> list_all2 R ys ys \<and> map Abs xs = map Abs ys"
haftmann@40820
    65
    by (induct xs ys rule: list_induct2') auto
haftmann@40820
    66
qed
kaliszyk@35222
    67
kuncar@47308
    68
declare [[mapQ3 list = (list_all2, list_quotient3)]]
kuncar@47094
    69
haftmann@40820
    70
lemma cons_prs [quot_preserve]:
kuncar@47308
    71
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222
    72
  shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
kuncar@47308
    73
  by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
kaliszyk@35222
    74
haftmann@40820
    75
lemma cons_rsp [quot_respect]:
kuncar@47308
    76
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492
    77
  shows "(R ===> list_all2 R ===> list_all2 R) (op #) (op #)"
haftmann@40463
    78
  by auto
kaliszyk@35222
    79
haftmann@40820
    80
lemma nil_prs [quot_preserve]:
kuncar@47308
    81
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@35222
    82
  shows "map Abs [] = []"
kaliszyk@35222
    83
  by simp
kaliszyk@35222
    84
haftmann@40820
    85
lemma nil_rsp [quot_respect]:
kuncar@47308
    86
  assumes q: "Quotient3 R Abs Rep"
kaliszyk@37492
    87
  shows "list_all2 R [] []"
kaliszyk@35222
    88
  by simp
kaliszyk@35222
    89
kaliszyk@35222
    90
lemma map_prs_aux:
kuncar@47308
    91
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
    92
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
    93
  shows "(map abs2) (map ((abs1 ---> rep2) f) (map rep1 l)) = map f l"
kaliszyk@35222
    94
  by (induct l)
kuncar@47308
    95
     (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
    96
haftmann@40820
    97
lemma map_prs [quot_preserve]:
kuncar@47308
    98
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
    99
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   100
  shows "((abs1 ---> rep2) ---> (map rep1) ---> (map abs2)) map = map"
kaliszyk@36216
   101
  and   "((abs1 ---> id) ---> map rep1 ---> id) map = map"
haftmann@40463
   102
  by (simp_all only: fun_eq_iff map_prs_aux[OF a b] comp_def)
kuncar@47308
   103
    (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
haftmann@40463
   104
haftmann@40820
   105
lemma map_rsp [quot_respect]:
kuncar@47308
   106
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   107
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   108
  shows "((R1 ===> R2) ===> (list_all2 R1) ===> list_all2 R2) map map"
kaliszyk@37492
   109
  and   "((R1 ===> op =) ===> (list_all2 R1) ===> op =) map map"
huffman@47641
   110
  unfolding list_all2_eq [symmetric] by (rule map_transfer)+
kaliszyk@35222
   111
kaliszyk@35222
   112
lemma foldr_prs_aux:
kuncar@47308
   113
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   114
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   115
  shows "abs2 (foldr ((abs1 ---> abs2 ---> rep2) f) (map rep1 l) (rep2 e)) = foldr f l e"
kuncar@47308
   116
  by (induct l) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   117
haftmann@40820
   118
lemma foldr_prs [quot_preserve]:
kuncar@47308
   119
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   120
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   121
  shows "((abs1 ---> abs2 ---> rep2) ---> (map rep1) ---> rep2 ---> abs2) foldr = foldr"
haftmann@40463
   122
  apply (simp add: fun_eq_iff)
haftmann@40463
   123
  by (simp only: fun_eq_iff foldr_prs_aux[OF a b])
kaliszyk@35222
   124
     (simp)
kaliszyk@35222
   125
kaliszyk@35222
   126
lemma foldl_prs_aux:
kuncar@47308
   127
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   128
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   129
  shows "abs1 (foldl ((abs1 ---> abs2 ---> rep1) f) (rep1 e) (map rep2 l)) = foldl f e l"
kuncar@47308
   130
  by (induct l arbitrary:e) (simp_all add: Quotient3_abs_rep[OF a] Quotient3_abs_rep[OF b])
kaliszyk@35222
   131
haftmann@40820
   132
lemma foldl_prs [quot_preserve]:
kuncar@47308
   133
  assumes a: "Quotient3 R1 abs1 rep1"
kuncar@47308
   134
  and     b: "Quotient3 R2 abs2 rep2"
kaliszyk@35222
   135
  shows "((abs1 ---> abs2 ---> rep1) ---> rep1 ---> (map rep2) ---> abs1) foldl = foldl"
haftmann@40463
   136
  by (simp add: fun_eq_iff foldl_prs_aux [OF a b])
kaliszyk@35222
   137
kaliszyk@35222
   138
(* induct_tac doesn't accept 'arbitrary', so we manually 'spec' *)
kaliszyk@35222
   139
lemma foldl_rsp[quot_respect]:
kuncar@47308
   140
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   141
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   142
  shows "((R1 ===> R2 ===> R1) ===> R1 ===> list_all2 R2 ===> R1) foldl foldl"
huffman@47641
   143
  by (rule foldl_transfer)
kaliszyk@35222
   144
kaliszyk@35222
   145
lemma foldr_rsp[quot_respect]:
kuncar@47308
   146
  assumes q1: "Quotient3 R1 Abs1 Rep1"
kuncar@47308
   147
  and     q2: "Quotient3 R2 Abs2 Rep2"
kaliszyk@37492
   148
  shows "((R1 ===> R2 ===> R2) ===> list_all2 R1 ===> R2 ===> R2) foldr foldr"
huffman@47641
   149
  by (rule foldr_transfer)
kaliszyk@35222
   150
kaliszyk@37492
   151
lemma list_all2_rsp:
kaliszyk@36154
   152
  assumes r: "\<forall>x y. R x y \<longrightarrow> (\<forall>a b. R a b \<longrightarrow> S x a = T y b)"
kaliszyk@37492
   153
  and l1: "list_all2 R x y"
kaliszyk@37492
   154
  and l2: "list_all2 R a b"
kaliszyk@37492
   155
  shows "list_all2 S x a = list_all2 T y b"
huffman@45803
   156
  using l1 l2
huffman@45803
   157
  by (induct arbitrary: a b rule: list_all2_induct,
huffman@45803
   158
    auto simp: list_all2_Cons1 list_all2_Cons2 r)
kaliszyk@36154
   159
haftmann@40820
   160
lemma [quot_respect]:
kaliszyk@37492
   161
  "((R ===> R ===> op =) ===> list_all2 R ===> list_all2 R ===> op =) list_all2 list_all2"
huffman@47641
   162
  by (rule list_all2_transfer)
kaliszyk@36154
   163
haftmann@40820
   164
lemma [quot_preserve]:
kuncar@47308
   165
  assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492
   166
  shows "((abs1 ---> abs1 ---> id) ---> map rep1 ---> map rep1 ---> id) list_all2 = list_all2"
nipkow@39302
   167
  apply (simp add: fun_eq_iff)
kaliszyk@36154
   168
  apply clarify
kaliszyk@36154
   169
  apply (induct_tac xa xb rule: list_induct2')
kuncar@47308
   170
  apply (simp_all add: Quotient3_abs_rep[OF a])
kaliszyk@36154
   171
  done
kaliszyk@36154
   172
haftmann@40820
   173
lemma [quot_preserve]:
kuncar@47308
   174
  assumes a: "Quotient3 R abs1 rep1"
kaliszyk@37492
   175
  shows "(list_all2 ((rep1 ---> rep1 ---> id) R) l m) = (l = m)"
kuncar@47308
   176
  by (induct l m rule: list_induct2') (simp_all add: Quotient3_rel_rep[OF a])
kaliszyk@36154
   177
kaliszyk@37492
   178
lemma list_all2_find_element:
kaliszyk@36276
   179
  assumes a: "x \<in> set a"
kaliszyk@37492
   180
  and b: "list_all2 R a b"
kaliszyk@36276
   181
  shows "\<exists>y. (y \<in> set b \<and> R x y)"
huffman@45803
   182
  using b a by induct auto
kaliszyk@36276
   183
kaliszyk@37492
   184
lemma list_all2_refl:
kaliszyk@35222
   185
  assumes a: "\<And>x y. R x y = (R x = R y)"
kaliszyk@37492
   186
  shows "list_all2 R x x"
kaliszyk@35222
   187
  by (induct x) (auto simp add: a)
kaliszyk@35222
   188
kaliszyk@35222
   189
end