author | paulson <lp15@cam.ac.uk> |
Thu, 03 Jun 2021 10:47:20 +0100 | |
changeset 73795 | 8893e0ed263a |
parent 71616 | a9de39608b1a |
child 73832 | 9db620f007fa |
permissions | -rw-r--r-- |
3981 | 1 |
(* Title: HOL/Map.thy |
2 |
Author: Tobias Nipkow, based on a theory by David von Oheimb |
|
13908 | 3 |
Copyright 1997-2003 TU Muenchen |
3981 | 4 |
|
60838 | 5 |
The datatype of "maps"; strongly resembles maps in VDM. |
3981 | 6 |
*) |
7 |
||
60758 | 8 |
section \<open>Maps\<close> |
13914 | 9 |
|
15131 | 10 |
theory Map |
67780
7655e6369c9f
more abbrevs -- this makes "(=" ambiguous and thus simplifies input of "(=)" (within the context of Main HOL);
wenzelm
parents:
67091
diff
changeset
|
11 |
imports List |
7655e6369c9f
more abbrevs -- this makes "(=" ambiguous and thus simplifies input of "(=)" (within the context of Main HOL);
wenzelm
parents:
67091
diff
changeset
|
12 |
abbrevs "(=" = "\<subseteq>\<^sub>m" |
15131 | 13 |
begin |
3981 | 14 |
|
61069 | 15 |
type_synonym ('a, 'b) "map" = "'a \<Rightarrow> 'b option" (infixr "\<rightharpoonup>" 0) |
19656
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
wenzelm
parents:
19378
diff
changeset
|
16 |
|
19378 | 17 |
abbreviation |
60838 | 18 |
empty :: "'a \<rightharpoonup> 'b" where |
60839 | 19 |
"empty \<equiv> \<lambda>x. None" |
19378 | 20 |
|
19656
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
wenzelm
parents:
19378
diff
changeset
|
21 |
definition |
61069 | 22 |
map_comp :: "('b \<rightharpoonup> 'c) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'c)" (infixl "\<circ>\<^sub>m" 55) where |
23 |
"f \<circ>\<^sub>m g = (\<lambda>k. case g k of None \<Rightarrow> None | Some v \<Rightarrow> f v)" |
|
19656
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
wenzelm
parents:
19378
diff
changeset
|
24 |
|
20800 | 25 |
definition |
60839 | 26 |
map_add :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)" (infixl "++" 100) where |
27 |
"m1 ++ m2 = (\<lambda>x. case m2 x of None \<Rightarrow> m1 x | Some y \<Rightarrow> Some y)" |
|
20800 | 28 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21210
diff
changeset
|
29 |
definition |
60839 | 30 |
restrict_map :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set \<Rightarrow> ('a \<rightharpoonup> 'b)" (infixl "|`" 110) where |
31 |
"m|`A = (\<lambda>x. if x \<in> A then m x else None)" |
|
13910 | 32 |
|
21210 | 33 |
notation (latex output) |
19656
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
wenzelm
parents:
19378
diff
changeset
|
34 |
restrict_map ("_\<restriction>\<^bsub>_\<^esub>" [111,110] 110) |
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
wenzelm
parents:
19378
diff
changeset
|
35 |
|
20800 | 36 |
definition |
60839 | 37 |
dom :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set" where |
38 |
"dom m = {a. m a \<noteq> None}" |
|
20800 | 39 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21210
diff
changeset
|
40 |
definition |
60839 | 41 |
ran :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'b set" where |
42 |
"ran m = {b. \<exists>a. m a = Some b}" |
|
20800 | 43 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21210
diff
changeset
|
44 |
definition |
60839 | 45 |
map_le :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> bool" (infix "\<subseteq>\<^sub>m" 50) where |
46 |
"(m\<^sub>1 \<subseteq>\<^sub>m m\<^sub>2) \<longleftrightarrow> (\<forall>a \<in> dom m\<^sub>1. m\<^sub>1 a = m\<^sub>2 a)" |
|
20800 | 47 |
|
41229
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
wenzelm
parents:
39992
diff
changeset
|
48 |
nonterminal maplets and maplet |
14180 | 49 |
|
5300 | 50 |
syntax |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
51 |
"_maplet" :: "['a, 'a] \<Rightarrow> maplet" ("_ /\<mapsto>/ _") |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
52 |
"_maplets" :: "['a, 'a] \<Rightarrow> maplet" ("_ /[\<mapsto>]/ _") |
60839 | 53 |
"" :: "maplet \<Rightarrow> maplets" ("_") |
54 |
"_Maplets" :: "[maplet, maplets] \<Rightarrow> maplets" ("_,/ _") |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
55 |
"_MapUpd" :: "['a \<rightharpoonup> 'b, maplets] \<Rightarrow> 'a \<rightharpoonup> 'b" ("_/'(_')" [900, 0] 900) |
60839 | 56 |
"_Map" :: "maplets \<Rightarrow> 'a \<rightharpoonup> 'b" ("(1[_])") |
3981 | 57 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
58 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
59 |
"_maplet" :: "['a, 'a] \<Rightarrow> maplet" ("_ /|->/ _") |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
60 |
"_maplets" :: "['a, 'a] \<Rightarrow> maplet" ("_ /[|->]/ _") |
14180 | 61 |
|
5300 | 62 |
translations |
60839 | 63 |
"_MapUpd m (_Maplets xy ms)" \<rightleftharpoons> "_MapUpd (_MapUpd m xy) ms" |
64 |
"_MapUpd m (_maplet x y)" \<rightleftharpoons> "m(x := CONST Some y)" |
|
65 |
"_Map ms" \<rightleftharpoons> "_MapUpd (CONST empty) ms" |
|
66 |
"_Map (_Maplets ms1 ms2)" \<leftharpoondown> "_MapUpd (_Map ms1) ms2" |
|
67 |
"_Maplets ms1 (_Maplets ms2 ms3)" \<leftharpoondown> "_Maplets (_Maplets ms1 ms2) ms3" |
|
14180 | 68 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
69 |
primrec map_of :: "('a \<times> 'b) list \<Rightarrow> 'a \<rightharpoonup> 'b" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
70 |
where |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
71 |
"map_of [] = empty" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
72 |
| "map_of (p # ps) = (map_of ps)(fst p \<mapsto> snd p)" |
5300 | 73 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
74 |
definition map_upds :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'a \<rightharpoonup> 'b" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
75 |
where "map_upds m xs ys = m ++ map_of (rev (zip xs ys))" |
34941 | 76 |
translations |
60839 | 77 |
"_MapUpd m (_maplets x y)" \<rightleftharpoons> "CONST map_upds m x y" |
25965 | 78 |
|
60839 | 79 |
lemma map_of_Cons_code [code]: |
25965 | 80 |
"map_of [] k = None" |
81 |
"map_of ((l, v) # ps) k = (if l = k then Some v else map_of ps k)" |
|
82 |
by simp_all |
|
83 |
||
20800 | 84 |
|
60758 | 85 |
subsection \<open>@{term [source] empty}\<close> |
13908 | 86 |
|
20800 | 87 |
lemma empty_upd_none [simp]: "empty(x := None) = empty" |
60839 | 88 |
by (rule ext) simp |
13908 | 89 |
|
90 |
||
60758 | 91 |
subsection \<open>@{term [source] map_upd}\<close> |
13908 | 92 |
|
60839 | 93 |
lemma map_upd_triv: "t k = Some x \<Longrightarrow> t(k\<mapsto>x) = t" |
94 |
by (rule ext) simp |
|
13908 | 95 |
|
60839 | 96 |
lemma map_upd_nonempty [simp]: "t(k\<mapsto>x) \<noteq> empty" |
20800 | 97 |
proof |
98 |
assume "t(k \<mapsto> x) = empty" |
|
99 |
then have "(t(k \<mapsto> x)) k = None" by simp |
|
100 |
then show False by simp |
|
101 |
qed |
|
13908 | 102 |
|
20800 | 103 |
lemma map_upd_eqD1: |
104 |
assumes "m(a\<mapsto>x) = n(a\<mapsto>y)" |
|
105 |
shows "x = y" |
|
106 |
proof - |
|
41550 | 107 |
from assms have "(m(a\<mapsto>x)) a = (n(a\<mapsto>y)) a" by simp |
20800 | 108 |
then show ?thesis by simp |
109 |
qed |
|
14100 | 110 |
|
20800 | 111 |
lemma map_upd_Some_unfold: |
60838 | 112 |
"((m(a\<mapsto>b)) x = Some y) = (x = a \<and> b = y \<or> x \<noteq> a \<and> m x = Some y)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
113 |
by auto |
14100 | 114 |
|
20800 | 115 |
lemma image_map_upd [simp]: "x \<notin> A \<Longrightarrow> m(x \<mapsto> y) ` A = m ` A" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
116 |
by auto |
15303 | 117 |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
118 |
lemma finite_range_updI: |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
119 |
assumes "finite (range f)" shows "finite (range (f(a\<mapsto>b)))" |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
120 |
proof - |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
121 |
have "range (f(a\<mapsto>b)) \<subseteq> insert (Some b) (range f)" |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
122 |
by auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
123 |
then show ?thesis |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
124 |
by (rule finite_subset) (use assms in auto) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
125 |
qed |
13908 | 126 |
|
127 |
||
60758 | 128 |
subsection \<open>@{term [source] map_of}\<close> |
13908 | 129 |
|
68454 | 130 |
lemma map_of_eq_empty_iff [simp]: |
131 |
"map_of xys = empty \<longleftrightarrow> xys = []" |
|
132 |
proof |
|
133 |
show "map_of xys = empty \<Longrightarrow> xys = []" |
|
134 |
by (induction xys) simp_all |
|
135 |
qed simp |
|
136 |
||
68460 | 137 |
lemma empty_eq_map_of_iff [simp]: |
138 |
"empty = map_of xys \<longleftrightarrow> xys = []" |
|
139 |
by(subst eq_commute) simp |
|
140 |
||
15304 | 141 |
lemma map_of_eq_None_iff: |
24331 | 142 |
"(map_of xys x = None) = (x \<notin> fst ` (set xys))" |
143 |
by (induct xys) simp_all |
|
15304 | 144 |
|
20800 | 145 |
lemma map_of_eq_Some_iff [simp]: |
24331 | 146 |
"distinct(map fst xys) \<Longrightarrow> (map_of xys x = Some y) = ((x,y) \<in> set xys)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
147 |
proof (induct xys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
148 |
case (Cons xy xys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
149 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
150 |
by (cases xy) (auto simp flip: map_of_eq_None_iff) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
151 |
qed auto |
15304 | 152 |
|
20800 | 153 |
lemma Some_eq_map_of_iff [simp]: |
24331 | 154 |
"distinct(map fst xys) \<Longrightarrow> (Some y = map_of xys x) = ((x,y) \<in> set xys)" |
60839 | 155 |
by (auto simp del: map_of_eq_Some_iff simp: map_of_eq_Some_iff [symmetric]) |
15304 | 156 |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
157 |
lemma map_of_is_SomeI [simp]: |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
158 |
"\<lbrakk>distinct(map fst xys); (x,y) \<in> set xys\<rbrakk> \<Longrightarrow> map_of xys x = Some y" |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
159 |
by simp |
15304 | 160 |
|
20800 | 161 |
lemma map_of_zip_is_None [simp]: |
24331 | 162 |
"length xs = length ys \<Longrightarrow> (map_of (zip xs ys) x = None) = (x \<notin> set xs)" |
163 |
by (induct rule: list_induct2) simp_all |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
14739
diff
changeset
|
164 |
|
26443 | 165 |
lemma map_of_zip_is_Some: |
166 |
assumes "length xs = length ys" |
|
167 |
shows "x \<in> set xs \<longleftrightarrow> (\<exists>y. map_of (zip xs ys) x = Some y)" |
|
168 |
using assms by (induct rule: list_induct2) simp_all |
|
169 |
||
170 |
lemma map_of_zip_upd: |
|
171 |
fixes x :: 'a and xs :: "'a list" and ys zs :: "'b list" |
|
172 |
assumes "length ys = length xs" |
|
173 |
and "length zs = length xs" |
|
174 |
and "x \<notin> set xs" |
|
175 |
and "map_of (zip xs ys)(x \<mapsto> y) = map_of (zip xs zs)(x \<mapsto> z)" |
|
176 |
shows "map_of (zip xs ys) = map_of (zip xs zs)" |
|
177 |
proof |
|
178 |
fix x' :: 'a |
|
179 |
show "map_of (zip xs ys) x' = map_of (zip xs zs) x'" |
|
180 |
proof (cases "x = x'") |
|
181 |
case True |
|
182 |
from assms True map_of_zip_is_None [of xs ys x'] |
|
183 |
have "map_of (zip xs ys) x' = None" by simp |
|
184 |
moreover from assms True map_of_zip_is_None [of xs zs x'] |
|
185 |
have "map_of (zip xs zs) x' = None" by simp |
|
186 |
ultimately show ?thesis by simp |
|
187 |
next |
|
188 |
case False from assms |
|
189 |
have "(map_of (zip xs ys)(x \<mapsto> y)) x' = (map_of (zip xs zs)(x \<mapsto> z)) x'" by auto |
|
190 |
with False show ?thesis by simp |
|
191 |
qed |
|
192 |
qed |
|
193 |
||
194 |
lemma map_of_zip_inject: |
|
195 |
assumes "length ys = length xs" |
|
196 |
and "length zs = length xs" |
|
197 |
and dist: "distinct xs" |
|
198 |
and map_of: "map_of (zip xs ys) = map_of (zip xs zs)" |
|
199 |
shows "ys = zs" |
|
60839 | 200 |
using assms(1) assms(2)[symmetric] |
201 |
using dist map_of |
|
202 |
proof (induct ys xs zs rule: list_induct3) |
|
26443 | 203 |
case Nil show ?case by simp |
204 |
next |
|
205 |
case (Cons y ys x xs z zs) |
|
60758 | 206 |
from \<open>map_of (zip (x#xs) (y#ys)) = map_of (zip (x#xs) (z#zs))\<close> |
26443 | 207 |
have map_of: "map_of (zip xs ys)(x \<mapsto> y) = map_of (zip xs zs)(x \<mapsto> z)" by simp |
208 |
from Cons have "length ys = length xs" and "length zs = length xs" |
|
209 |
and "x \<notin> set xs" by simp_all |
|
210 |
then have "map_of (zip xs ys) = map_of (zip xs zs)" using map_of by (rule map_of_zip_upd) |
|
60758 | 211 |
with Cons.hyps \<open>distinct (x # xs)\<close> have "ys = zs" by simp |
26443 | 212 |
moreover from map_of have "y = z" by (rule map_upd_eqD1) |
213 |
ultimately show ?case by simp |
|
214 |
qed |
|
215 |
||
66584 | 216 |
lemma map_of_zip_nth: |
217 |
assumes "length xs = length ys" |
|
218 |
assumes "distinct xs" |
|
219 |
assumes "i < length ys" |
|
220 |
shows "map_of (zip xs ys) (xs ! i) = Some (ys ! i)" |
|
221 |
using assms proof (induct arbitrary: i rule: list_induct2) |
|
222 |
case Nil |
|
223 |
then show ?case by simp |
|
224 |
next |
|
225 |
case (Cons x xs y ys) |
|
226 |
then show ?case |
|
227 |
using less_Suc_eq_0_disj by auto |
|
228 |
qed |
|
229 |
||
33635 | 230 |
lemma map_of_zip_map: |
231 |
"map_of (zip xs (map f xs)) = (\<lambda>x. if x \<in> set xs then Some (f x) else None)" |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
232 |
by (induct xs) (simp_all add: fun_eq_iff) |
33635 | 233 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
14739
diff
changeset
|
234 |
lemma finite_range_map_of: "finite (range (map_of xys))" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
235 |
proof (induct xys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
236 |
case (Cons a xys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
237 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
238 |
using finite_range_updI by fastforce |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
239 |
qed auto |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
14739
diff
changeset
|
240 |
|
20800 | 241 |
lemma map_of_SomeD: "map_of xs k = Some y \<Longrightarrow> (k, y) \<in> set xs" |
60841 | 242 |
by (induct xs) (auto split: if_splits) |
13908 | 243 |
|
20800 | 244 |
lemma map_of_mapk_SomeI: |
60839 | 245 |
"inj f \<Longrightarrow> map_of t k = Some x \<Longrightarrow> |
61032
b57df8eecad6
standardized some occurences of ancient "split" alias
haftmann
parents:
60841
diff
changeset
|
246 |
map_of (map (case_prod (\<lambda>k. Pair (f k))) t) (f k) = Some x" |
60839 | 247 |
by (induct t) (auto simp: inj_eq) |
13908 | 248 |
|
60839 | 249 |
lemma weak_map_of_SomeI: "(k, x) \<in> set l \<Longrightarrow> \<exists>x. map_of l k = Some x" |
24331 | 250 |
by (induct l) auto |
13908 | 251 |
|
20800 | 252 |
lemma map_of_filter_in: |
61032
b57df8eecad6
standardized some occurences of ancient "split" alias
haftmann
parents:
60841
diff
changeset
|
253 |
"map_of xs k = Some z \<Longrightarrow> P k z \<Longrightarrow> map_of (filter (case_prod P) xs) k = Some z" |
24331 | 254 |
by (induct xs) auto |
13908 | 255 |
|
35607 | 256 |
lemma map_of_map: |
55466 | 257 |
"map_of (map (\<lambda>(k, v). (k, f v)) xs) = map_option f \<circ> map_of xs" |
60839 | 258 |
by (induct xs) (auto simp: fun_eq_iff) |
35607 | 259 |
|
55466 | 260 |
lemma dom_map_option: |
261 |
"dom (\<lambda>k. map_option (f k) (m k)) = dom m" |
|
35607 | 262 |
by (simp add: dom_def) |
13908 | 263 |
|
56545 | 264 |
lemma dom_map_option_comp [simp]: |
265 |
"dom (map_option g \<circ> m) = dom m" |
|
266 |
using dom_map_option [of "\<lambda>_. g" m] by (simp add: comp_def) |
|
267 |
||
13908 | 268 |
|
69593 | 269 |
subsection \<open>\<^const>\<open>map_option\<close> related\<close> |
13908 | 270 |
|
67091 | 271 |
lemma map_option_o_empty [simp]: "map_option f \<circ> empty = empty" |
24331 | 272 |
by (rule ext) simp |
13908 | 273 |
|
55466 | 274 |
lemma map_option_o_map_upd [simp]: |
67091 | 275 |
"map_option f \<circ> m(a\<mapsto>b) = (map_option f \<circ> m)(a\<mapsto>f b)" |
24331 | 276 |
by (rule ext) simp |
20800 | 277 |
|
13908 | 278 |
|
60758 | 279 |
subsection \<open>@{term [source] map_comp} related\<close> |
17391 | 280 |
|
20800 | 281 |
lemma map_comp_empty [simp]: |
24331 | 282 |
"m \<circ>\<^sub>m empty = empty" |
283 |
"empty \<circ>\<^sub>m m = empty" |
|
60839 | 284 |
by (auto simp: map_comp_def split: option.splits) |
17391 | 285 |
|
20800 | 286 |
lemma map_comp_simps [simp]: |
24331 | 287 |
"m2 k = None \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = None" |
288 |
"m2 k = Some k' \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = m1 k'" |
|
60839 | 289 |
by (auto simp: map_comp_def) |
17391 | 290 |
|
291 |
lemma map_comp_Some_iff: |
|
24331 | 292 |
"((m1 \<circ>\<^sub>m m2) k = Some v) = (\<exists>k'. m2 k = Some k' \<and> m1 k' = Some v)" |
60839 | 293 |
by (auto simp: map_comp_def split: option.splits) |
17391 | 294 |
|
295 |
lemma map_comp_None_iff: |
|
24331 | 296 |
"((m1 \<circ>\<^sub>m m2) k = None) = (m2 k = None \<or> (\<exists>k'. m2 k = Some k' \<and> m1 k' = None)) " |
60839 | 297 |
by (auto simp: map_comp_def split: option.splits) |
13908 | 298 |
|
20800 | 299 |
|
61799 | 300 |
subsection \<open>\<open>++\<close>\<close> |
13908 | 301 |
|
14025 | 302 |
lemma map_add_empty[simp]: "m ++ empty = m" |
24331 | 303 |
by(simp add: map_add_def) |
13908 | 304 |
|
14025 | 305 |
lemma empty_map_add[simp]: "empty ++ m = m" |
24331 | 306 |
by (rule ext) (simp add: map_add_def split: option.split) |
13908 | 307 |
|
14025 | 308 |
lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3" |
24331 | 309 |
by (rule ext) (simp add: map_add_def split: option.split) |
20800 | 310 |
|
311 |
lemma map_add_Some_iff: |
|
67091 | 312 |
"((m ++ n) k = Some x) = (n k = Some x \<or> n k = None \<and> m k = Some x)" |
24331 | 313 |
by (simp add: map_add_def split: option.split) |
14025 | 314 |
|
20800 | 315 |
lemma map_add_SomeD [dest!]: |
24331 | 316 |
"(m ++ n) k = Some x \<Longrightarrow> n k = Some x \<or> n k = None \<and> m k = Some x" |
317 |
by (rule map_add_Some_iff [THEN iffD1]) |
|
13908 | 318 |
|
60839 | 319 |
lemma map_add_find_right [simp]: "n k = Some xx \<Longrightarrow> (m ++ n) k = Some xx" |
24331 | 320 |
by (subst map_add_Some_iff) fast |
13908 | 321 |
|
67091 | 322 |
lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None \<and> m k = None)" |
24331 | 323 |
by (simp add: map_add_def split: option.split) |
13908 | 324 |
|
60838 | 325 |
lemma map_add_upd[simp]: "f ++ g(x\<mapsto>y) = (f ++ g)(x\<mapsto>y)" |
24331 | 326 |
by (rule ext) (simp add: map_add_def) |
13908 | 327 |
|
14186 | 328 |
lemma map_add_upds[simp]: "m1 ++ (m2(xs[\<mapsto>]ys)) = (m1++m2)(xs[\<mapsto>]ys)" |
24331 | 329 |
by (simp add: map_upds_def) |
14186 | 330 |
|
32236
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
krauss
parents:
31380
diff
changeset
|
331 |
lemma map_add_upd_left: "m\<notin>dom e2 \<Longrightarrow> e1(m \<mapsto> u1) ++ e2 = (e1 ++ e2)(m \<mapsto> u1)" |
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
krauss
parents:
31380
diff
changeset
|
332 |
by (rule ext) (auto simp: map_add_def dom_def split: option.split) |
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
krauss
parents:
31380
diff
changeset
|
333 |
|
20800 | 334 |
lemma map_of_append[simp]: "map_of (xs @ ys) = map_of ys ++ map_of xs" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
335 |
unfolding map_add_def |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
336 |
proof (induct xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
337 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
338 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
339 |
by (force split: option.split) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
340 |
qed auto |
13908 | 341 |
|
14025 | 342 |
lemma finite_range_map_of_map_add: |
60839 | 343 |
"finite (range f) \<Longrightarrow> finite (range (f ++ map_of l))" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
344 |
proof (induct l) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
345 |
case (Cons a l) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
346 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
347 |
by (metis finite_range_updI map_add_upd map_of.simps(2)) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
348 |
qed auto |
13908 | 349 |
|
20800 | 350 |
lemma inj_on_map_add_dom [iff]: |
24331 | 351 |
"inj_on (m ++ m') (dom m') = inj_on m' (dom m')" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
352 |
by (fastforce simp: map_add_def dom_def inj_on_def split: option.splits) |
20800 | 353 |
|
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
354 |
lemma map_upds_fold_map_upd: |
35552 | 355 |
"m(ks[\<mapsto>]vs) = foldl (\<lambda>m (k, v). m(k \<mapsto> v)) m (zip ks vs)" |
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
356 |
unfolding map_upds_def proof (rule sym, rule zip_obtain_same_length) |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
357 |
fix ks :: "'a list" and vs :: "'b list" |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
358 |
assume "length ks = length vs" |
35552 | 359 |
then show "foldl (\<lambda>m (k, v). m(k\<mapsto>v)) m (zip ks vs) = m ++ map_of (rev (zip ks vs))" |
360 |
by(induct arbitrary: m rule: list_induct2) simp_all |
|
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
361 |
qed |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
362 |
|
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
363 |
lemma map_add_map_of_foldr: |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
364 |
"m ++ map_of ps = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m" |
60839 | 365 |
by (induct ps) (auto simp: fun_eq_iff map_add_def) |
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
366 |
|
15304 | 367 |
|
60758 | 368 |
subsection \<open>@{term [source] restrict_map}\<close> |
14100 | 369 |
|
20800 | 370 |
lemma restrict_map_to_empty [simp]: "m|`{} = empty" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
371 |
by (simp add: restrict_map_def) |
14186 | 372 |
|
31380 | 373 |
lemma restrict_map_insert: "f |` (insert a A) = (f |` A)(a := f a)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
374 |
by (auto simp: restrict_map_def) |
31380 | 375 |
|
20800 | 376 |
lemma restrict_map_empty [simp]: "empty|`D = empty" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
377 |
by (simp add: restrict_map_def) |
14186 | 378 |
|
15693 | 379 |
lemma restrict_in [simp]: "x \<in> A \<Longrightarrow> (m|`A) x = m x" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
380 |
by (simp add: restrict_map_def) |
14100 | 381 |
|
15693 | 382 |
lemma restrict_out [simp]: "x \<notin> A \<Longrightarrow> (m|`A) x = None" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
383 |
by (simp add: restrict_map_def) |
14100 | 384 |
|
15693 | 385 |
lemma ran_restrictD: "y \<in> ran (m|`A) \<Longrightarrow> \<exists>x\<in>A. m x = Some y" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
386 |
by (auto simp: restrict_map_def ran_def split: if_split_asm) |
14100 | 387 |
|
15693 | 388 |
lemma dom_restrict [simp]: "dom (m|`A) = dom m \<inter> A" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
389 |
by (auto simp: restrict_map_def dom_def split: if_split_asm) |
14100 | 390 |
|
15693 | 391 |
lemma restrict_upd_same [simp]: "m(x\<mapsto>y)|`(-{x}) = m|`(-{x})" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
392 |
by (rule ext) (auto simp: restrict_map_def) |
14100 | 393 |
|
15693 | 394 |
lemma restrict_restrict [simp]: "m|`A|`B = m|`(A\<inter>B)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
395 |
by (rule ext) (auto simp: restrict_map_def) |
14100 | 396 |
|
20800 | 397 |
lemma restrict_fun_upd [simp]: |
24331 | 398 |
"m(x := y)|`D = (if x \<in> D then (m|`(D-{x}))(x := y) else m|`D)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
399 |
by (simp add: restrict_map_def fun_eq_iff) |
14186 | 400 |
|
20800 | 401 |
lemma fun_upd_None_restrict [simp]: |
60839 | 402 |
"(m|`D)(x := None) = (if x \<in> D then m|`(D - {x}) else m|`D)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
403 |
by (simp add: restrict_map_def fun_eq_iff) |
14186 | 404 |
|
20800 | 405 |
lemma fun_upd_restrict: "(m|`D)(x := y) = (m|`(D-{x}))(x := y)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
406 |
by (simp add: restrict_map_def fun_eq_iff) |
14186 | 407 |
|
20800 | 408 |
lemma fun_upd_restrict_conv [simp]: |
24331 | 409 |
"x \<in> D \<Longrightarrow> (m|`D)(x := y) = (m|`(D-{x}))(x := y)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
410 |
by (rule fun_upd_restrict) |
14186 | 411 |
|
35159
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
haftmann
parents:
35115
diff
changeset
|
412 |
lemma map_of_map_restrict: |
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
haftmann
parents:
35115
diff
changeset
|
413 |
"map_of (map (\<lambda>k. (k, f k)) ks) = (Some \<circ> f) |` set ks" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
414 |
by (induct ks) (simp_all add: fun_eq_iff restrict_map_insert) |
35159
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
haftmann
parents:
35115
diff
changeset
|
415 |
|
35619 | 416 |
lemma restrict_complement_singleton_eq: |
417 |
"f |` (- {x}) = f(x := None)" |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
418 |
by auto |
35619 | 419 |
|
14100 | 420 |
|
60758 | 421 |
subsection \<open>@{term [source] map_upds}\<close> |
14025 | 422 |
|
60838 | 423 |
lemma map_upds_Nil1 [simp]: "m([] [\<mapsto>] bs) = m" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
424 |
by (simp add: map_upds_def) |
14025 | 425 |
|
60838 | 426 |
lemma map_upds_Nil2 [simp]: "m(as [\<mapsto>] []) = m" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
427 |
by (simp add:map_upds_def) |
20800 | 428 |
|
60838 | 429 |
lemma map_upds_Cons [simp]: "m(a#as [\<mapsto>] b#bs) = (m(a\<mapsto>b))(as[\<mapsto>]bs)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
430 |
by (simp add:map_upds_def) |
14025 | 431 |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
432 |
lemma map_upds_append1 [simp]: |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
433 |
"size xs < size ys \<Longrightarrow> m(xs@[x] [\<mapsto>] ys) = m(xs [\<mapsto>] ys)(x \<mapsto> ys!size xs)" |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
434 |
proof (induct xs arbitrary: ys m) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
435 |
case Nil |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
436 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
437 |
by (auto simp: neq_Nil_conv) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
438 |
next |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
439 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
440 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
441 |
by (cases ys) auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
442 |
qed |
14187 | 443 |
|
20800 | 444 |
lemma map_upds_list_update2_drop [simp]: |
46588
4895d7f1be42
removing some unnecessary premises from Map theory
bulwahn
parents:
44921
diff
changeset
|
445 |
"size xs \<le> i \<Longrightarrow> m(xs[\<mapsto>]ys[i:=y]) = m(xs[\<mapsto>]ys)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
446 |
proof (induct xs arbitrary: m ys i) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
447 |
case Nil |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
448 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
449 |
by auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
450 |
next |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
451 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
452 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
453 |
by (cases ys) (use Cons in \<open>auto split: nat.split\<close>) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
454 |
qed |
14025 | 455 |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
456 |
text \<open>Something weirdly sensitive about this proof, which needs only four lines in apply style\<close> |
20800 | 457 |
lemma map_upd_upds_conv_if: |
60838 | 458 |
"(f(x\<mapsto>y))(xs [\<mapsto>] ys) = |
60839 | 459 |
(if x \<in> set(take (length ys) xs) then f(xs [\<mapsto>] ys) |
60838 | 460 |
else (f(xs [\<mapsto>] ys))(x\<mapsto>y))" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
461 |
proof (induct xs arbitrary: x y ys f) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
462 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
463 |
show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
464 |
proof (cases ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
465 |
case (Cons z zs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
466 |
then show ?thesis |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
467 |
using Cons.hyps |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
468 |
apply (auto split: if_split simp: fun_upd_twist) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
469 |
using Cons.hyps apply fastforce+ |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
470 |
done |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
471 |
qed auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
472 |
qed auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
473 |
|
14025 | 474 |
|
475 |
lemma map_upds_twist [simp]: |
|
60839 | 476 |
"a \<notin> set as \<Longrightarrow> m(a\<mapsto>b)(as[\<mapsto>]bs) = m(as[\<mapsto>]bs)(a\<mapsto>b)" |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
42163
diff
changeset
|
477 |
using set_take_subset by (fastforce simp add: map_upd_upds_conv_if) |
14025 | 478 |
|
20800 | 479 |
lemma map_upds_apply_nontin [simp]: |
60839 | 480 |
"x \<notin> set xs \<Longrightarrow> (f(xs[\<mapsto>]ys)) x = f x" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
481 |
proof (induct xs arbitrary: ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
482 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
483 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
484 |
by (cases ys) (auto simp: map_upd_upds_conv_if) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
485 |
qed auto |
14025 | 486 |
|
20800 | 487 |
lemma fun_upds_append_drop [simp]: |
24331 | 488 |
"size xs = size ys \<Longrightarrow> m(xs@zs[\<mapsto>]ys) = m(xs[\<mapsto>]ys)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
489 |
proof (induct xs arbitrary: ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
490 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
491 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
492 |
by (cases ys) (auto simp: map_upd_upds_conv_if) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
493 |
qed auto |
14300 | 494 |
|
20800 | 495 |
lemma fun_upds_append2_drop [simp]: |
24331 | 496 |
"size xs = size ys \<Longrightarrow> m(xs[\<mapsto>]ys@zs) = m(xs[\<mapsto>]ys)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
497 |
proof (induct xs arbitrary: ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
498 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
499 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
500 |
by (cases ys) (auto simp: map_upd_upds_conv_if) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
501 |
qed auto |
14300 | 502 |
|
20800 | 503 |
lemma restrict_map_upds[simp]: |
504 |
"\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk> |
|
505 |
\<Longrightarrow> m(xs [\<mapsto>] ys)|`D = (m|`(D - set xs))(xs [\<mapsto>] ys)" |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
506 |
proof (induct xs arbitrary: m ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
507 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
508 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
509 |
proof (cases ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
510 |
case (Cons z zs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
511 |
with Cons.hyps Cons.prems show ?thesis |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
512 |
apply (simp add: insert_absorb flip: Diff_insert) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
513 |
apply (auto simp add: map_upd_upds_conv_if) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
514 |
done |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
515 |
qed auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
516 |
qed auto |
14186 | 517 |
|
518 |
||
60758 | 519 |
subsection \<open>@{term [source] dom}\<close> |
13908 | 520 |
|
31080 | 521 |
lemma dom_eq_empty_conv [simp]: "dom f = {} \<longleftrightarrow> f = empty" |
44921 | 522 |
by (auto simp: dom_def) |
31080 | 523 |
|
60839 | 524 |
lemma domI: "m a = Some b \<Longrightarrow> a \<in> dom m" |
525 |
by (simp add: dom_def) |
|
14100 | 526 |
(* declare domI [intro]? *) |
13908 | 527 |
|
60839 | 528 |
lemma domD: "a \<in> dom m \<Longrightarrow> \<exists>b. m a = Some b" |
529 |
by (cases "m a") (auto simp add: dom_def) |
|
13908 | 530 |
|
66010 | 531 |
lemma domIff [iff, simp del, code_unfold]: "a \<in> dom m \<longleftrightarrow> m a \<noteq> None" |
60839 | 532 |
by (simp add: dom_def) |
13908 | 533 |
|
20800 | 534 |
lemma dom_empty [simp]: "dom empty = {}" |
60839 | 535 |
by (simp add: dom_def) |
13908 | 536 |
|
20800 | 537 |
lemma dom_fun_upd [simp]: |
60839 | 538 |
"dom(f(x := y)) = (if y = None then dom f - {x} else insert x (dom f))" |
539 |
by (auto simp: dom_def) |
|
13908 | 540 |
|
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
541 |
lemma dom_if: |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
542 |
"dom (\<lambda>x. if P x then f x else g x) = dom f \<inter> {x. P x} \<union> dom g \<inter> {x. \<not> P x}" |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
543 |
by (auto split: if_splits) |
13937 | 544 |
|
15304 | 545 |
lemma dom_map_of_conv_image_fst: |
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
546 |
"dom (map_of xys) = fst ` set xys" |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
547 |
by (induct xys) (auto simp add: dom_if) |
15304 | 548 |
|
60839 | 549 |
lemma dom_map_of_zip [simp]: "length xs = length ys \<Longrightarrow> dom (map_of (zip xs ys)) = set xs" |
550 |
by (induct rule: list_induct2) (auto simp: dom_if) |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
14739
diff
changeset
|
551 |
|
13908 | 552 |
lemma finite_dom_map_of: "finite (dom (map_of l))" |
60839 | 553 |
by (induct l) (auto simp: dom_def insert_Collect [symmetric]) |
13908 | 554 |
|
20800 | 555 |
lemma dom_map_upds [simp]: |
60839 | 556 |
"dom(m(xs[\<mapsto>]ys)) = set(take (length ys) xs) \<union> dom m" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
557 |
proof (induct xs arbitrary: ys) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
558 |
case (Cons a xs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
559 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
560 |
by (cases ys) (auto simp: map_upd_upds_conv_if) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
561 |
qed auto |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
562 |
|
13910 | 563 |
|
60839 | 564 |
lemma dom_map_add [simp]: "dom (m ++ n) = dom n \<union> dom m" |
565 |
by (auto simp: dom_def) |
|
13910 | 566 |
|
20800 | 567 |
lemma dom_override_on [simp]: |
60839 | 568 |
"dom (override_on f g A) = |
569 |
(dom f - {a. a \<in> A - dom g}) \<union> {a. a \<in> A \<inter> dom g}" |
|
570 |
by (auto simp: dom_def override_on_def) |
|
13908 | 571 |
|
60839 | 572 |
lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1 ++ m2 = m2 ++ m1" |
573 |
by (rule ext) (force simp: map_add_def dom_def split: option.split) |
|
20800 | 574 |
|
32236
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
krauss
parents:
31380
diff
changeset
|
575 |
lemma map_add_dom_app_simps: |
60839 | 576 |
"m \<in> dom l2 \<Longrightarrow> (l1 ++ l2) m = l2 m" |
577 |
"m \<notin> dom l1 \<Longrightarrow> (l1 ++ l2) m = l2 m" |
|
578 |
"m \<notin> dom l2 \<Longrightarrow> (l1 ++ l2) m = l1 m" |
|
579 |
by (auto simp add: map_add_def split: option.split_asm) |
|
32236
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
krauss
parents:
31380
diff
changeset
|
580 |
|
29622 | 581 |
lemma dom_const [simp]: |
35159
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
haftmann
parents:
35115
diff
changeset
|
582 |
"dom (\<lambda>x. Some (f x)) = UNIV" |
29622 | 583 |
by auto |
584 |
||
22230 | 585 |
(* Due to John Matthews - could be rephrased with dom *) |
586 |
lemma finite_map_freshness: |
|
587 |
"finite (dom (f :: 'a \<rightharpoonup> 'b)) \<Longrightarrow> \<not> finite (UNIV :: 'a set) \<Longrightarrow> |
|
588 |
\<exists>x. f x = None" |
|
60839 | 589 |
by (bestsimp dest: ex_new_if_finite) |
14027 | 590 |
|
28790 | 591 |
lemma dom_minus: |
592 |
"f x = None \<Longrightarrow> dom f - insert x A = dom f - A" |
|
593 |
unfolding dom_def by simp |
|
594 |
||
595 |
lemma insert_dom: |
|
596 |
"f x = Some y \<Longrightarrow> insert x (dom f) = dom f" |
|
597 |
unfolding dom_def by auto |
|
598 |
||
35607 | 599 |
lemma map_of_map_keys: |
600 |
"set xs = dom m \<Longrightarrow> map_of (map (\<lambda>k. (k, the (m k))) xs) = m" |
|
601 |
by (rule ext) (auto simp add: map_of_map_restrict restrict_map_def) |
|
602 |
||
39379
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
603 |
lemma map_of_eqI: |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
604 |
assumes set_eq: "set (map fst xs) = set (map fst ys)" |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
605 |
assumes map_eq: "\<forall>k\<in>set (map fst xs). map_of xs k = map_of ys k" |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
606 |
shows "map_of xs = map_of ys" |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
607 |
proof (rule ext) |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
608 |
fix k show "map_of xs k = map_of ys k" |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
609 |
proof (cases "map_of xs k") |
60839 | 610 |
case None |
611 |
then have "k \<notin> set (map fst xs)" by (simp add: map_of_eq_None_iff) |
|
39379
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
612 |
with set_eq have "k \<notin> set (map fst ys)" by simp |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
613 |
then have "map_of ys k = None" by (simp add: map_of_eq_None_iff) |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
614 |
with None show ?thesis by simp |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
615 |
next |
60839 | 616 |
case (Some v) |
617 |
then have "k \<in> set (map fst xs)" by (auto simp add: dom_map_of_conv_image_fst [symmetric]) |
|
39379
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
618 |
with map_eq show ?thesis by auto |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
619 |
qed |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
620 |
qed |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
621 |
|
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
622 |
lemma map_of_eq_dom: |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
623 |
assumes "map_of xs = map_of ys" |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
624 |
shows "fst ` set xs = fst ` set ys" |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
625 |
proof - |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
626 |
from assms have "dom (map_of xs) = dom (map_of ys)" by simp |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
627 |
then show ?thesis by (simp add: dom_map_of_conv_image_fst) |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
628 |
qed |
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
haftmann
parents:
39302
diff
changeset
|
629 |
|
53820 | 630 |
lemma finite_set_of_finite_maps: |
60839 | 631 |
assumes "finite A" "finite B" |
632 |
shows "finite {m. dom m = A \<and> ran m \<subseteq> B}" (is "finite ?S") |
|
53820 | 633 |
proof - |
634 |
let ?S' = "{m. \<forall>x. (x \<in> A \<longrightarrow> m x \<in> Some ` B) \<and> (x \<notin> A \<longrightarrow> m x = None)}" |
|
635 |
have "?S = ?S'" |
|
636 |
proof |
|
60839 | 637 |
show "?S \<subseteq> ?S'" by (auto simp: dom_def ran_def image_def) |
53820 | 638 |
show "?S' \<subseteq> ?S" |
639 |
proof |
|
640 |
fix m assume "m \<in> ?S'" |
|
641 |
hence 1: "dom m = A" by force |
|
60839 | 642 |
hence 2: "ran m \<subseteq> B" using \<open>m \<in> ?S'\<close> by (auto simp: dom_def ran_def) |
53820 | 643 |
from 1 2 show "m \<in> ?S" by blast |
644 |
qed |
|
645 |
qed |
|
646 |
with assms show ?thesis by(simp add: finite_set_of_finite_funs) |
|
647 |
qed |
|
28790 | 648 |
|
60839 | 649 |
|
60758 | 650 |
subsection \<open>@{term [source] ran}\<close> |
14100 | 651 |
|
60839 | 652 |
lemma ranI: "m a = Some b \<Longrightarrow> b \<in> ran m" |
653 |
by (auto simp: ran_def) |
|
14100 | 654 |
(* declare ranI [intro]? *) |
13908 | 655 |
|
20800 | 656 |
lemma ran_empty [simp]: "ran empty = {}" |
60839 | 657 |
by (auto simp: ran_def) |
13908 | 658 |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
659 |
lemma ran_map_upd [simp]: "m a = None \<Longrightarrow> ran(m(a\<mapsto>b)) = insert b (ran m)" |
60839 | 660 |
unfolding ran_def |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
661 |
by force |
20800 | 662 |
|
66583 | 663 |
lemma ran_map_add: |
664 |
assumes "dom m1 \<inter> dom m2 = {}" |
|
665 |
shows "ran (m1 ++ m2) = ran m1 \<union> ran m2" |
|
666 |
proof |
|
667 |
show "ran (m1 ++ m2) \<subseteq> ran m1 \<union> ran m2" |
|
668 |
unfolding ran_def by auto |
|
669 |
next |
|
670 |
show "ran m1 \<union> ran m2 \<subseteq> ran (m1 ++ m2)" |
|
671 |
proof - |
|
672 |
have "(m1 ++ m2) x = Some y" if "m1 x = Some y" for x y |
|
673 |
using assms map_add_comm that by fastforce |
|
674 |
moreover have "(m1 ++ m2) x = Some y" if "m2 x = Some y" for x y |
|
675 |
using assms that by auto |
|
676 |
ultimately show ?thesis |
|
677 |
unfolding ran_def by blast |
|
678 |
qed |
|
679 |
qed |
|
680 |
||
681 |
lemma finite_ran: |
|
682 |
assumes "finite (dom p)" |
|
683 |
shows "finite (ran p)" |
|
684 |
proof - |
|
685 |
have "ran p = (\<lambda>x. the (p x)) ` dom p" |
|
686 |
unfolding ran_def by force |
|
687 |
from this \<open>finite (dom p)\<close> show ?thesis by auto |
|
688 |
qed |
|
689 |
||
60839 | 690 |
lemma ran_distinct: |
691 |
assumes dist: "distinct (map fst al)" |
|
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
692 |
shows "ran (map_of al) = snd ` set al" |
60839 | 693 |
using assms |
694 |
proof (induct al) |
|
695 |
case Nil |
|
696 |
then show ?case by simp |
|
34979
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
697 |
next |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
698 |
case (Cons kv al) |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
699 |
then have "ran (map_of al) = snd ` set al" by simp |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
700 |
moreover from Cons.prems have "map_of al (fst kv) = None" |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
701 |
by (simp add: map_of_eq_None_iff) |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
702 |
ultimately show ?case by (simp only: map_of.simps ran_map_upd) simp |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
703 |
qed |
8cb6e7a42e9c
more correspondence lemmas between related operations
haftmann
parents:
34941
diff
changeset
|
704 |
|
66584 | 705 |
lemma ran_map_of_zip: |
706 |
assumes "length xs = length ys" "distinct xs" |
|
707 |
shows "ran (map_of (zip xs ys)) = set ys" |
|
708 |
using assms by (simp add: ran_distinct set_map[symmetric]) |
|
709 |
||
60057 | 710 |
lemma ran_map_option: "ran (\<lambda>x. map_option f (m x)) = f ` ran m" |
60839 | 711 |
by (auto simp add: ran_def) |
712 |
||
13910 | 713 |
|
61799 | 714 |
subsection \<open>\<open>map_le\<close>\<close> |
13910 | 715 |
|
13912 | 716 |
lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g" |
60839 | 717 |
by (simp add: map_le_def) |
13910 | 718 |
|
17724 | 719 |
lemma upd_None_map_le [simp]: "f(x := None) \<subseteq>\<^sub>m f" |
60839 | 720 |
by (force simp add: map_le_def) |
14187 | 721 |
|
13910 | 722 |
lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)" |
60839 | 723 |
by (fastforce simp add: map_le_def) |
13910 | 724 |
|
17724 | 725 |
lemma map_le_imp_upd_le [simp]: "m1 \<subseteq>\<^sub>m m2 \<Longrightarrow> m1(x := None) \<subseteq>\<^sub>m m2(x \<mapsto> y)" |
60839 | 726 |
by (force simp add: map_le_def) |
14187 | 727 |
|
20800 | 728 |
lemma map_le_upds [simp]: |
60839 | 729 |
"f \<subseteq>\<^sub>m g \<Longrightarrow> f(as [\<mapsto>] bs) \<subseteq>\<^sub>m g(as [\<mapsto>] bs)" |
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
730 |
proof (induct as arbitrary: f g bs) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
731 |
case (Cons a as) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
732 |
then show ?case |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
733 |
by (cases bs) (use Cons in auto) |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
734 |
qed auto |
13908 | 735 |
|
14033 | 736 |
lemma map_le_implies_dom_le: "(f \<subseteq>\<^sub>m g) \<Longrightarrow> (dom f \<subseteq> dom g)" |
60839 | 737 |
by (fastforce simp add: map_le_def dom_def) |
14033 | 738 |
|
739 |
lemma map_le_refl [simp]: "f \<subseteq>\<^sub>m f" |
|
60839 | 740 |
by (simp add: map_le_def) |
14033 | 741 |
|
14187 | 742 |
lemma map_le_trans[trans]: "\<lbrakk> m1 \<subseteq>\<^sub>m m2; m2 \<subseteq>\<^sub>m m3\<rbrakk> \<Longrightarrow> m1 \<subseteq>\<^sub>m m3" |
60839 | 743 |
by (auto simp add: map_le_def dom_def) |
14033 | 744 |
|
745 |
lemma map_le_antisym: "\<lbrakk> f \<subseteq>\<^sub>m g; g \<subseteq>\<^sub>m f \<rbrakk> \<Longrightarrow> f = g" |
|
71616
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
746 |
unfolding map_le_def |
a9de39608b1a
more tidying up of old apply-proofs
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
747 |
by (metis ext domIff) |
14033 | 748 |
|
60839 | 749 |
lemma map_le_map_add [simp]: "f \<subseteq>\<^sub>m g ++ f" |
750 |
by (fastforce simp: map_le_def) |
|
14033 | 751 |
|
60839 | 752 |
lemma map_le_iff_map_add_commute: "f \<subseteq>\<^sub>m f ++ g \<longleftrightarrow> f ++ g = g ++ f" |
753 |
by (fastforce simp: map_add_def map_le_def fun_eq_iff split: option.splits) |
|
15304 | 754 |
|
60839 | 755 |
lemma map_add_le_mapE: "f ++ g \<subseteq>\<^sub>m h \<Longrightarrow> g \<subseteq>\<^sub>m h" |
756 |
by (fastforce simp: map_le_def map_add_def dom_def) |
|
15303 | 757 |
|
60839 | 758 |
lemma map_add_le_mapI: "\<lbrakk> f \<subseteq>\<^sub>m h; g \<subseteq>\<^sub>m h \<rbrakk> \<Longrightarrow> f ++ g \<subseteq>\<^sub>m h" |
759 |
by (auto simp: map_le_def map_add_def dom_def split: option.splits) |
|
15303 | 760 |
|
63828 | 761 |
lemma map_add_subsumed1: "f \<subseteq>\<^sub>m g \<Longrightarrow> f++g = g" |
762 |
by (simp add: map_add_le_mapI map_le_antisym) |
|
763 |
||
764 |
lemma map_add_subsumed2: "f \<subseteq>\<^sub>m g \<Longrightarrow> g++f = g" |
|
765 |
by (metis map_add_subsumed1 map_le_iff_map_add_commute) |
|
766 |
||
31080 | 767 |
lemma dom_eq_singleton_conv: "dom f = {x} \<longleftrightarrow> (\<exists>v. f = [x \<mapsto> v])" |
63834 | 768 |
(is "?lhs \<longleftrightarrow> ?rhs") |
769 |
proof |
|
770 |
assume ?rhs |
|
771 |
then show ?lhs by (auto split: if_split_asm) |
|
31080 | 772 |
next |
63834 | 773 |
assume ?lhs |
774 |
then obtain v where v: "f x = Some v" by auto |
|
775 |
show ?rhs |
|
776 |
proof |
|
777 |
show "f = [x \<mapsto> v]" |
|
778 |
proof (rule map_le_antisym) |
|
779 |
show "[x \<mapsto> v] \<subseteq>\<^sub>m f" |
|
780 |
using v by (auto simp add: map_le_def) |
|
781 |
show "f \<subseteq>\<^sub>m [x \<mapsto> v]" |
|
782 |
using \<open>dom f = {x}\<close> \<open>f x = Some v\<close> by (auto simp add: map_le_def) |
|
783 |
qed |
|
784 |
qed |
|
31080 | 785 |
qed |
786 |
||
68454 | 787 |
lemma map_add_eq_empty_iff[simp]: |
788 |
"(f++g = empty) \<longleftrightarrow> f = empty \<and> g = empty" |
|
789 |
by (metis map_add_None) |
|
790 |
||
791 |
lemma empty_eq_map_add_iff[simp]: |
|
792 |
"(empty = f++g) \<longleftrightarrow> f = empty \<and> g = empty" |
|
793 |
by(subst map_add_eq_empty_iff[symmetric])(rule eq_commute) |
|
794 |
||
35565 | 795 |
|
60758 | 796 |
subsection \<open>Various\<close> |
35565 | 797 |
|
798 |
lemma set_map_of_compr: |
|
799 |
assumes distinct: "distinct (map fst xs)" |
|
800 |
shows "set xs = {(k, v). map_of xs k = Some v}" |
|
60839 | 801 |
using assms |
802 |
proof (induct xs) |
|
803 |
case Nil |
|
804 |
then show ?case by simp |
|
35565 | 805 |
next |
806 |
case (Cons x xs) |
|
807 |
obtain k v where "x = (k, v)" by (cases x) blast |
|
808 |
with Cons.prems have "k \<notin> dom (map_of xs)" |
|
809 |
by (simp add: dom_map_of_conv_image_fst) |
|
810 |
then have *: "insert (k, v) {(k, v). map_of xs k = Some v} = |
|
811 |
{(k', v'). (map_of xs(k \<mapsto> v)) k' = Some v'}" |
|
812 |
by (auto split: if_splits) |
|
813 |
from Cons have "set xs = {(k, v). map_of xs k = Some v}" by simp |
|
60758 | 814 |
with * \<open>x = (k, v)\<close> show ?case by simp |
35565 | 815 |
qed |
816 |
||
67051 | 817 |
lemma eq_key_imp_eq_value: |
818 |
"v1 = v2" |
|
819 |
if "distinct (map fst xs)" "(k, v1) \<in> set xs" "(k, v2) \<in> set xs" |
|
820 |
proof - |
|
821 |
from that have "inj_on fst (set xs)" |
|
822 |
by (simp add: distinct_map) |
|
823 |
moreover have "fst (k, v1) = fst (k, v2)" |
|
824 |
by simp |
|
825 |
ultimately have "(k, v1) = (k, v2)" |
|
826 |
by (rule inj_onD) (fact that)+ |
|
827 |
then show ?thesis |
|
828 |
by simp |
|
829 |
qed |
|
830 |
||
35565 | 831 |
lemma map_of_inject_set: |
832 |
assumes distinct: "distinct (map fst xs)" "distinct (map fst ys)" |
|
833 |
shows "map_of xs = map_of ys \<longleftrightarrow> set xs = set ys" (is "?lhs \<longleftrightarrow> ?rhs") |
|
834 |
proof |
|
835 |
assume ?lhs |
|
60758 | 836 |
moreover from \<open>distinct (map fst xs)\<close> have "set xs = {(k, v). map_of xs k = Some v}" |
35565 | 837 |
by (rule set_map_of_compr) |
60758 | 838 |
moreover from \<open>distinct (map fst ys)\<close> have "set ys = {(k, v). map_of ys k = Some v}" |
35565 | 839 |
by (rule set_map_of_compr) |
840 |
ultimately show ?rhs by simp |
|
841 |
next |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53015
diff
changeset
|
842 |
assume ?rhs show ?lhs |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53015
diff
changeset
|
843 |
proof |
35565 | 844 |
fix k |
60839 | 845 |
show "map_of xs k = map_of ys k" |
846 |
proof (cases "map_of xs k") |
|
35565 | 847 |
case None |
60758 | 848 |
with \<open>?rhs\<close> have "map_of ys k = None" |
35565 | 849 |
by (simp add: map_of_eq_None_iff) |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53015
diff
changeset
|
850 |
with None show ?thesis by simp |
35565 | 851 |
next |
852 |
case (Some v) |
|
60758 | 853 |
with distinct \<open>?rhs\<close> have "map_of ys k = Some v" |
35565 | 854 |
by simp |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53015
diff
changeset
|
855 |
with Some show ?thesis by simp |
35565 | 856 |
qed |
857 |
qed |
|
858 |
qed |
|
859 |
||
68450 | 860 |
hide_const (open) Map.empty |
861 |
||
3981 | 862 |
end |