| author | wenzelm | 
| Mon, 04 Nov 2024 21:25:26 +0100 | |
| changeset 81344 | 1b9ea66810ff | 
| parent 81125 | ec121999a9cb | 
| child 81595 | ed264056f5dc | 
| 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  | 
|
| 
80932
 
261cd8722677
standardize mixfix annotations via "isabelle update -u mixfix_cartouches -l Pure HOL" --- to simplify systematic editing;
 
wenzelm 
parents: 
80760 
diff
changeset
 | 
15  | 
type_synonym ('a, 'b) "map" = "'a \<Rightarrow> 'b option" (infixr \<open>\<rightharpoonup>\<close> 0)
 | 
| 
19656
 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 
wenzelm 
parents: 
19378 
diff
changeset
 | 
16  | 
|
| 
77356
 
1f5428d66591
Map.empty no longer output abbreviation; %_. None is shorter and requires no explanation
 
nipkow 
parents: 
77307 
diff
changeset
 | 
17  | 
abbreviation (input)  | 
| 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  | 
| 
80932
 
261cd8722677
standardize mixfix annotations via "isabelle update -u mixfix_cartouches -l Pure HOL" --- to simplify systematic editing;
 
wenzelm 
parents: 
80760 
diff
changeset
 | 
22  | 
  map_comp :: "('b \<rightharpoonup> 'c) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'c)"  (infixl \<open>\<circ>\<^sub>m\<close> 55) where
 | 
| 61069 | 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  | 
| 
80932
 
261cd8722677
standardize mixfix annotations via "isabelle update -u mixfix_cartouches -l Pure HOL" --- to simplify systematic editing;
 
wenzelm 
parents: 
80760 
diff
changeset
 | 
26  | 
  map_add :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)"  (infixl \<open>++\<close> 100) where
 | 
| 60839 | 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  | 
| 
80932
 
261cd8722677
standardize mixfix annotations via "isabelle update -u mixfix_cartouches -l Pure HOL" --- to simplify systematic editing;
 
wenzelm 
parents: 
80760 
diff
changeset
 | 
30  | 
  restrict_map :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set \<Rightarrow> ('a \<rightharpoonup> 'b)"  (infixl \<open>|`\<close>  110) where
 | 
| 60839 | 31  | 
"m|`A = (\<lambda>x. if x \<in> A then m x else None)"  | 
| 13910 | 32  | 
|
| 21210 | 33  | 
notation (latex output)  | 
| 
80932
 
261cd8722677
standardize mixfix annotations via "isabelle update -u mixfix_cartouches -l Pure HOL" --- to simplify systematic editing;
 
wenzelm 
parents: 
80760 
diff
changeset
 | 
34  | 
restrict_map (\<open>_\<restriction>\<^bsub>_\<^esub>\<close> [111,110] 110)  | 
| 
19656
 
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  | 
| 73832 | 45  | 
  graph :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<times> 'b) set" where
 | 
46  | 
  "graph m = {(a, b) | a b. m a = Some b}"
 | 
|
47  | 
||
48  | 
definition  | 
|
| 
80932
 
261cd8722677
standardize mixfix annotations via "isabelle update -u mixfix_cartouches -l Pure HOL" --- to simplify systematic editing;
 
wenzelm 
parents: 
80760 
diff
changeset
 | 
49  | 
  map_le :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> bool"  (infix \<open>\<subseteq>\<^sub>m\<close> 50) where
 | 
| 60839 | 50  | 
"(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 | 51  | 
|
| 77671 | 52  | 
text \<open>Function update syntax \<open>f(x := y, \<dots>)\<close> is extended with \<open>x \<mapsto> y\<close>, which is short for  | 
53  | 
\<open>x := Some y\<close>. \<open>:=\<close> and \<open>\<mapsto>\<close> can be mixed freely.  | 
|
54  | 
The syntax \<open>[x \<mapsto> y, \<dots>]\<close> is short for \<open>Map.empty(x \<mapsto> y, \<dots>)\<close>  | 
|
55  | 
but must only contain \<open>\<mapsto>\<close>, not \<open>:=\<close>, because \<open>[x:=y]\<close> clashes with the list update syntax \<open>xs[i:=x]\<close>.\<close>  | 
|
56  | 
||
57  | 
nonterminal maplet and maplets  | 
|
| 14180 | 58  | 
|
| 5300 | 59  | 
syntax  | 
| 81125 | 60  | 
"_maplet" :: "['a, 'a] \<Rightarrow> maplet" (\<open>(\<open>open_block notation=\<open>mixfix maplet\<close>\<close>_ /\<mapsto>/ _)\<close>)  | 
61  | 
"" :: "maplet \<Rightarrow> updbind" (\<open>_\<close>)  | 
|
62  | 
"" :: "maplet \<Rightarrow> maplets" (\<open>_\<close>)  | 
|
63  | 
"_Maplets" :: "[maplet, maplets] \<Rightarrow> maplets" (\<open>_,/ _\<close>)  | 
|
64  | 
"_Map" :: "maplets \<Rightarrow> 'a \<rightharpoonup> 'b" (\<open>(\<open>indent=1 notation=\<open>mixfix map\<close>\<close>[_])\<close>)  | 
|
| 77671 | 65  | 
(* Syntax forbids \<open>[\<dots>, x := y, \<dots>]\<close> by introducing \<open>maplets\<close> in addition to \<open>updbinds\<close> *)  | 
| 3981 | 66  | 
|
| 
61955
 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 
wenzelm 
parents: 
61799 
diff
changeset
 | 
67  | 
syntax (ASCII)  | 
| 81125 | 68  | 
"_maplet" :: "['a, 'a] \<Rightarrow> maplet" (\<open>(\<open>open_block notation=\<open>mixfix maplet\<close>\<close>_ /|->/ _)\<close>)  | 
| 14180 | 69  | 
|
| 80760 | 70  | 
syntax_consts  | 
71  | 
"_maplet" "_Maplets" "_Map" \<rightleftharpoons> fun_upd  | 
|
72  | 
||
| 5300 | 73  | 
translations  | 
| 77671 | 74  | 
"_Update f (_maplet x y)" \<rightleftharpoons> "f(x := CONST Some y)"  | 
75  | 
"_Maplets m ms" \<rightharpoonup> "_updbinds m ms"  | 
|
76  | 
"_Map ms" \<rightharpoonup> "_Update (CONST empty) ms"  | 
|
| 14180 | 77  | 
|
| 77671 | 78  | 
(* Printing must create \<open>_Map\<close> only for \<open>_maplet\<close> *)  | 
79  | 
"_Map (_maplet x y)" \<leftharpoondown> "_Update (\<lambda>u. CONST None) (_maplet x y)"  | 
|
80  | 
"_Map (_updbinds m (_maplet x y))" \<leftharpoondown> "_Update (_Map m) (_maplet x y)"  | 
|
81  | 
||
| 80760 | 82  | 
|
| 77671 | 83  | 
text \<open>Updating with lists:\<close>  | 
84  | 
||
85  | 
primrec map_of :: "('a \<times> 'b) list \<Rightarrow> 'a \<rightharpoonup> 'b" where
 | 
|
| 77307 | 86  | 
"map_of [] = empty"  | 
87  | 
| "map_of (p # ps) = (map_of ps)(fst p \<mapsto> snd p)"  | 
|
88  | 
||
89  | 
lemma map_of_Cons_code [code]:  | 
|
90  | 
"map_of [] k = None"  | 
|
91  | 
"map_of ((l, v) # ps) k = (if l = k then Some v else map_of ps k)"  | 
|
92  | 
by simp_all  | 
|
93  | 
||
| 77671 | 94  | 
definition map_upds :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'a \<rightharpoonup> 'b" where
 | 
95  | 
"map_upds m xs ys = m ++ map_of (rev (zip xs ys))"  | 
|
96  | 
||
97  | 
text \<open>There is also the more specialized update syntax \<open>xs [\<mapsto>] ys\<close> for lists \<open>xs\<close> and \<open>ys\<close>.\<close>  | 
|
98  | 
||
99  | 
syntax  | 
|
| 81125 | 100  | 
"_maplets" :: "['a, 'a] \<Rightarrow> maplet" (\<open>(\<open>open_block notation=\<open>mixfix maplet\<close>\<close>_ /[\<mapsto>]/ _)\<close>)  | 
| 77671 | 101  | 
|
102  | 
syntax (ASCII)  | 
|
| 81125 | 103  | 
"_maplets" :: "['a, 'a] \<Rightarrow> maplet" (\<open>(\<open>open_block notation=\<open>mixfix maplet\<close>\<close>_ /[|->]/ _)\<close>)  | 
| 77671 | 104  | 
|
| 80760 | 105  | 
syntax_consts  | 
106  | 
"_maplets" \<rightleftharpoons> map_upds  | 
|
107  | 
||
| 77671 | 108  | 
translations  | 
109  | 
"_Update m (_maplets xs ys)" \<rightleftharpoons> "CONST map_upds m xs ys"  | 
|
110  | 
||
111  | 
"_Map (_maplets xs ys)" \<leftharpoondown> "_Update (\<lambda>u. CONST None) (_maplets xs ys)"  | 
|
112  | 
"_Map (_updbinds m (_maplets xs ys))" \<leftharpoondown> "_Update (_Map m) (_maplets xs ys)"  | 
|
113  | 
||
| 20800 | 114  | 
|
| 60758 | 115  | 
subsection \<open>@{term [source] empty}\<close>
 | 
| 13908 | 116  | 
|
| 20800 | 117  | 
lemma empty_upd_none [simp]: "empty(x := None) = empty"  | 
| 60839 | 118  | 
by (rule ext) simp  | 
| 13908 | 119  | 
|
120  | 
||
| 60758 | 121  | 
subsection \<open>@{term [source] map_upd}\<close>
 | 
| 13908 | 122  | 
|
| 60839 | 123  | 
lemma map_upd_triv: "t k = Some x \<Longrightarrow> t(k\<mapsto>x) = t"  | 
124  | 
by (rule ext) simp  | 
|
| 13908 | 125  | 
|
| 60839 | 126  | 
lemma map_upd_nonempty [simp]: "t(k\<mapsto>x) \<noteq> empty"  | 
| 20800 | 127  | 
proof  | 
128  | 
assume "t(k \<mapsto> x) = empty"  | 
|
129  | 
then have "(t(k \<mapsto> x)) k = None" by simp  | 
|
130  | 
then show False by simp  | 
|
131  | 
qed  | 
|
| 13908 | 132  | 
|
| 20800 | 133  | 
lemma map_upd_eqD1:  | 
134  | 
assumes "m(a\<mapsto>x) = n(a\<mapsto>y)"  | 
|
135  | 
shows "x = y"  | 
|
136  | 
proof -  | 
|
| 41550 | 137  | 
from assms have "(m(a\<mapsto>x)) a = (n(a\<mapsto>y)) a" by simp  | 
| 20800 | 138  | 
then show ?thesis by simp  | 
139  | 
qed  | 
|
| 14100 | 140  | 
|
| 20800 | 141  | 
lemma map_upd_Some_unfold:  | 
| 60838 | 142  | 
"((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
 | 
143  | 
by auto  | 
| 14100 | 144  | 
|
| 20800 | 145  | 
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
 | 
146  | 
by auto  | 
| 15303 | 147  | 
|
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
148  | 
lemma finite_range_updI:  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
149  | 
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
 | 
150  | 
proof -  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
151  | 
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
 | 
152  | 
by auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
153  | 
then show ?thesis  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
154  | 
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
 | 
155  | 
qed  | 
| 13908 | 156  | 
|
157  | 
||
| 60758 | 158  | 
subsection \<open>@{term [source] map_of}\<close>
 | 
| 13908 | 159  | 
|
| 68454 | 160  | 
lemma map_of_eq_empty_iff [simp]:  | 
161  | 
"map_of xys = empty \<longleftrightarrow> xys = []"  | 
|
162  | 
proof  | 
|
163  | 
show "map_of xys = empty \<Longrightarrow> xys = []"  | 
|
164  | 
by (induction xys) simp_all  | 
|
165  | 
qed simp  | 
|
166  | 
||
| 68460 | 167  | 
lemma empty_eq_map_of_iff [simp]:  | 
168  | 
"empty = map_of xys \<longleftrightarrow> xys = []"  | 
|
169  | 
by(subst eq_commute) simp  | 
|
170  | 
||
| 77307 | 171  | 
lemma map_of_eq_None_iff:  | 
172  | 
"(map_of xys x = None) = (x \<notin> fst ` (set xys))"  | 
|
173  | 
by (induct xys) simp_all  | 
|
174  | 
||
175  | 
lemma map_of_eq_Some_iff [simp]:  | 
|
176  | 
"distinct(map fst xys) \<Longrightarrow> (map_of xys x = Some y) = ((x,y) \<in> set xys)"  | 
|
177  | 
proof (induct xys)  | 
|
178  | 
case (Cons xy xys)  | 
|
179  | 
then show ?case  | 
|
180  | 
by (cases xy) (auto simp flip: map_of_eq_None_iff)  | 
|
181  | 
qed auto  | 
|
182  | 
||
183  | 
lemma Some_eq_map_of_iff [simp]:  | 
|
184  | 
"distinct(map fst xys) \<Longrightarrow> (Some y = map_of xys x) = ((x,y) \<in> set xys)"  | 
|
185  | 
by (auto simp del: map_of_eq_Some_iff simp: map_of_eq_Some_iff [symmetric])  | 
|
186  | 
||
187  | 
lemma map_of_is_SomeI [simp]:  | 
|
188  | 
"\<lbrakk>distinct(map fst xys); (x,y) \<in> set xys\<rbrakk> \<Longrightarrow> map_of xys x = Some y"  | 
|
189  | 
by simp  | 
|
190  | 
||
191  | 
lemma map_of_zip_is_None [simp]:  | 
|
192  | 
"length xs = length ys \<Longrightarrow> (map_of (zip xs ys) x = None) = (x \<notin> set xs)"  | 
|
193  | 
by (induct rule: list_induct2) simp_all  | 
|
194  | 
||
195  | 
lemma map_of_zip_is_Some:  | 
|
196  | 
assumes "length xs = length ys"  | 
|
197  | 
shows "x \<in> set xs \<longleftrightarrow> (\<exists>y. map_of (zip xs ys) x = Some y)"  | 
|
198  | 
using assms by (induct rule: list_induct2) simp_all  | 
|
199  | 
||
200  | 
lemma map_of_zip_upd:  | 
|
201  | 
fixes x :: 'a and xs :: "'a list" and ys zs :: "'b list"  | 
|
202  | 
assumes "length ys = length xs"  | 
|
203  | 
and "length zs = length xs"  | 
|
204  | 
and "x \<notin> set xs"  | 
|
| 
77644
 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 
nipkow 
parents: 
77361 
diff
changeset
 | 
205  | 
and "(map_of (zip xs ys))(x \<mapsto> y) = (map_of (zip xs zs))(x \<mapsto> z)"  | 
| 77307 | 206  | 
shows "map_of (zip xs ys) = map_of (zip xs zs)"  | 
207  | 
proof  | 
|
208  | 
fix x' :: 'a  | 
|
209  | 
show "map_of (zip xs ys) x' = map_of (zip xs zs) x'"  | 
|
210  | 
proof (cases "x = x'")  | 
|
211  | 
case True  | 
|
212  | 
from assms True map_of_zip_is_None [of xs ys x']  | 
|
213  | 
have "map_of (zip xs ys) x' = None" by simp  | 
|
214  | 
moreover from assms True map_of_zip_is_None [of xs zs x']  | 
|
215  | 
have "map_of (zip xs zs) x' = None" by simp  | 
|
216  | 
ultimately show ?thesis by simp  | 
|
217  | 
next  | 
|
218  | 
case False from assms  | 
|
| 
77644
 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 
nipkow 
parents: 
77361 
diff
changeset
 | 
219  | 
have "((map_of (zip xs ys))(x \<mapsto> y)) x' = ((map_of (zip xs zs))(x \<mapsto> z)) x'" by auto  | 
| 77307 | 220  | 
with False show ?thesis by simp  | 
221  | 
qed  | 
|
222  | 
qed  | 
|
223  | 
||
224  | 
lemma map_of_zip_inject:  | 
|
225  | 
assumes "length ys = length xs"  | 
|
226  | 
and "length zs = length xs"  | 
|
227  | 
and dist: "distinct xs"  | 
|
228  | 
and map_of: "map_of (zip xs ys) = map_of (zip xs zs)"  | 
|
229  | 
shows "ys = zs"  | 
|
230  | 
using assms(1) assms(2)[symmetric]  | 
|
231  | 
using dist map_of  | 
|
232  | 
proof (induct ys xs zs rule: list_induct3)  | 
|
233  | 
case Nil show ?case by simp  | 
|
234  | 
next  | 
|
235  | 
case (Cons y ys x xs z zs)  | 
|
236  | 
from \<open>map_of (zip (x#xs) (y#ys)) = map_of (zip (x#xs) (z#zs))\<close>  | 
|
| 
77644
 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 
nipkow 
parents: 
77361 
diff
changeset
 | 
237  | 
have map_of: "(map_of (zip xs ys))(x \<mapsto> y) = (map_of (zip xs zs))(x \<mapsto> z)" by simp  | 
| 77307 | 238  | 
from Cons have "length ys = length xs" and "length zs = length xs"  | 
239  | 
and "x \<notin> set xs" by simp_all  | 
|
240  | 
then have "map_of (zip xs ys) = map_of (zip xs zs)" using map_of by (rule map_of_zip_upd)  | 
|
241  | 
with Cons.hyps \<open>distinct (x # xs)\<close> have "ys = zs" by simp  | 
|
242  | 
moreover from map_of have "y = z" by (rule map_upd_eqD1)  | 
|
243  | 
ultimately show ?case by simp  | 
|
244  | 
qed  | 
|
245  | 
||
246  | 
lemma map_of_zip_nth:  | 
|
247  | 
assumes "length xs = length ys"  | 
|
248  | 
assumes "distinct xs"  | 
|
249  | 
assumes "i < length ys"  | 
|
250  | 
shows "map_of (zip xs ys) (xs ! i) = Some (ys ! i)"  | 
|
251  | 
using assms proof (induct arbitrary: i rule: list_induct2)  | 
|
252  | 
case Nil  | 
|
253  | 
then show ?case by simp  | 
|
254  | 
next  | 
|
255  | 
case (Cons x xs y ys)  | 
|
256  | 
then show ?case  | 
|
257  | 
using less_Suc_eq_0_disj by auto  | 
|
258  | 
qed  | 
|
259  | 
||
260  | 
lemma map_of_zip_map:  | 
|
261  | 
"map_of (zip xs (map f xs)) = (\<lambda>x. if x \<in> set xs then Some (f x) else None)"  | 
|
262  | 
by (induct xs) (simp_all add: fun_eq_iff)  | 
|
263  | 
||
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
14739 
diff
changeset
 | 
264  | 
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
 | 
265  | 
proof (induct xys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
266  | 
case (Cons a xys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
267  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
268  | 
using finite_range_updI by fastforce  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
269  | 
qed auto  | 
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
14739 
diff
changeset
 | 
270  | 
|
| 77307 | 271  | 
lemma map_of_SomeD: "map_of xs k = Some y \<Longrightarrow> (k, y) \<in> set xs"  | 
272  | 
by (induct xs) (auto split: if_splits)  | 
|
273  | 
||
274  | 
lemma map_of_mapk_SomeI:  | 
|
275  | 
"inj f \<Longrightarrow> map_of t k = Some x \<Longrightarrow>  | 
|
276  | 
map_of (map (case_prod (\<lambda>k. Pair (f k))) t) (f k) = Some x"  | 
|
277  | 
by (induct t) (auto simp: inj_eq)  | 
|
278  | 
||
279  | 
lemma weak_map_of_SomeI: "(k, x) \<in> set l \<Longrightarrow> \<exists>x. map_of l k = Some x"  | 
|
280  | 
by (induct l) auto  | 
|
281  | 
||
282  | 
lemma map_of_filter_in:  | 
|
283  | 
"map_of xs k = Some z \<Longrightarrow> P k z \<Longrightarrow> map_of (filter (case_prod P) xs) k = Some z"  | 
|
284  | 
by (induct xs) auto  | 
|
285  | 
||
286  | 
lemma map_of_map:  | 
|
287  | 
"map_of (map (\<lambda>(k, v). (k, f v)) xs) = map_option f \<circ> map_of xs"  | 
|
288  | 
by (induct xs) (auto simp: fun_eq_iff)  | 
|
289  | 
||
| 55466 | 290  | 
lemma dom_map_option:  | 
291  | 
"dom (\<lambda>k. map_option (f k) (m k)) = dom m"  | 
|
| 35607 | 292  | 
by (simp add: dom_def)  | 
| 13908 | 293  | 
|
| 56545 | 294  | 
lemma dom_map_option_comp [simp]:  | 
295  | 
"dom (map_option g \<circ> m) = dom m"  | 
|
296  | 
using dom_map_option [of "\<lambda>_. g" m] by (simp add: comp_def)  | 
|
297  | 
||
| 13908 | 298  | 
|
| 69593 | 299  | 
subsection \<open>\<^const>\<open>map_option\<close> related\<close>  | 
| 13908 | 300  | 
|
| 67091 | 301  | 
lemma map_option_o_empty [simp]: "map_option f \<circ> empty = empty"  | 
| 24331 | 302  | 
by (rule ext) simp  | 
| 13908 | 303  | 
|
| 55466 | 304  | 
lemma map_option_o_map_upd [simp]:  | 
| 67091 | 305  | 
"map_option f \<circ> m(a\<mapsto>b) = (map_option f \<circ> m)(a\<mapsto>f b)"  | 
| 24331 | 306  | 
by (rule ext) simp  | 
| 20800 | 307  | 
|
| 13908 | 308  | 
|
| 60758 | 309  | 
subsection \<open>@{term [source] map_comp} related\<close>
 | 
| 17391 | 310  | 
|
| 20800 | 311  | 
lemma map_comp_empty [simp]:  | 
| 24331 | 312  | 
"m \<circ>\<^sub>m empty = empty"  | 
313  | 
"empty \<circ>\<^sub>m m = empty"  | 
|
| 60839 | 314  | 
by (auto simp: map_comp_def split: option.splits)  | 
| 17391 | 315  | 
|
| 20800 | 316  | 
lemma map_comp_simps [simp]:  | 
| 24331 | 317  | 
"m2 k = None \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = None"  | 
318  | 
"m2 k = Some k' \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = m1 k'"  | 
|
| 60839 | 319  | 
by (auto simp: map_comp_def)  | 
| 17391 | 320  | 
|
321  | 
lemma map_comp_Some_iff:  | 
|
| 24331 | 322  | 
"((m1 \<circ>\<^sub>m m2) k = Some v) = (\<exists>k'. m2 k = Some k' \<and> m1 k' = Some v)"  | 
| 60839 | 323  | 
by (auto simp: map_comp_def split: option.splits)  | 
| 17391 | 324  | 
|
325  | 
lemma map_comp_None_iff:  | 
|
| 24331 | 326  | 
"((m1 \<circ>\<^sub>m m2) k = None) = (m2 k = None \<or> (\<exists>k'. m2 k = Some k' \<and> m1 k' = None)) "  | 
| 60839 | 327  | 
by (auto simp: map_comp_def split: option.splits)  | 
| 13908 | 328  | 
|
| 20800 | 329  | 
|
| 61799 | 330  | 
subsection \<open>\<open>++\<close>\<close>  | 
| 13908 | 331  | 
|
| 14025 | 332  | 
lemma map_add_empty[simp]: "m ++ empty = m"  | 
| 24331 | 333  | 
by(simp add: map_add_def)  | 
| 13908 | 334  | 
|
| 14025 | 335  | 
lemma empty_map_add[simp]: "empty ++ m = m"  | 
| 24331 | 336  | 
by (rule ext) (simp add: map_add_def split: option.split)  | 
| 13908 | 337  | 
|
| 14025 | 338  | 
lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3"  | 
| 24331 | 339  | 
by (rule ext) (simp add: map_add_def split: option.split)  | 
| 20800 | 340  | 
|
341  | 
lemma map_add_Some_iff:  | 
|
| 67091 | 342  | 
"((m ++ n) k = Some x) = (n k = Some x \<or> n k = None \<and> m k = Some x)"  | 
| 24331 | 343  | 
by (simp add: map_add_def split: option.split)  | 
| 14025 | 344  | 
|
| 20800 | 345  | 
lemma map_add_SomeD [dest!]:  | 
| 24331 | 346  | 
"(m ++ n) k = Some x \<Longrightarrow> n k = Some x \<or> n k = None \<and> m k = Some x"  | 
347  | 
by (rule map_add_Some_iff [THEN iffD1])  | 
|
| 13908 | 348  | 
|
| 60839 | 349  | 
lemma map_add_find_right [simp]: "n k = Some xx \<Longrightarrow> (m ++ n) k = Some xx"  | 
| 24331 | 350  | 
by (subst map_add_Some_iff) fast  | 
| 13908 | 351  | 
|
| 67091 | 352  | 
lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None \<and> m k = None)"  | 
| 24331 | 353  | 
by (simp add: map_add_def split: option.split)  | 
| 13908 | 354  | 
|
| 60838 | 355  | 
lemma map_add_upd[simp]: "f ++ g(x\<mapsto>y) = (f ++ g)(x\<mapsto>y)"  | 
| 24331 | 356  | 
by (rule ext) (simp add: map_add_def)  | 
| 13908 | 357  | 
|
| 14186 | 358  | 
lemma map_add_upds[simp]: "m1 ++ (m2(xs[\<mapsto>]ys)) = (m1++m2)(xs[\<mapsto>]ys)"  | 
| 24331 | 359  | 
by (simp add: map_upds_def)  | 
| 14186 | 360  | 
|
| 
32236
 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 
krauss 
parents: 
31380 
diff
changeset
 | 
361  | 
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
 | 
362  | 
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
 | 
363  | 
|
| 20800 | 364  | 
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
 | 
365  | 
unfolding map_add_def  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
366  | 
proof (induct xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
367  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
368  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
369  | 
by (force split: option.split)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
370  | 
qed auto  | 
| 13908 | 371  | 
|
| 14025 | 372  | 
lemma finite_range_map_of_map_add:  | 
| 60839 | 373  | 
"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
 | 
374  | 
proof (induct l)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
375  | 
case (Cons a l)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
376  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
377  | 
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
 | 
378  | 
qed auto  | 
| 13908 | 379  | 
|
| 20800 | 380  | 
lemma inj_on_map_add_dom [iff]:  | 
| 24331 | 381  | 
"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
 | 
382  | 
by (fastforce simp: map_add_def dom_def inj_on_def split: option.splits)  | 
| 20800 | 383  | 
|
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
384  | 
lemma map_upds_fold_map_upd:  | 
| 35552 | 385  | 
"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
 | 
386  | 
unfolding map_upds_def proof (rule sym, rule zip_obtain_same_length)  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
387  | 
fix ks :: "'a list" and vs :: "'b list"  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
388  | 
assume "length ks = length vs"  | 
| 35552 | 389  | 
then show "foldl (\<lambda>m (k, v). m(k\<mapsto>v)) m (zip ks vs) = m ++ map_of (rev (zip ks vs))"  | 
390  | 
by(induct arbitrary: m rule: list_induct2) simp_all  | 
|
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
391  | 
qed  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
392  | 
|
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
393  | 
lemma map_add_map_of_foldr:  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
394  | 
"m ++ map_of ps = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m"  | 
| 60839 | 395  | 
by (induct ps) (auto simp: fun_eq_iff map_add_def)  | 
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
396  | 
|
| 15304 | 397  | 
|
| 60758 | 398  | 
subsection \<open>@{term [source] restrict_map}\<close>
 | 
| 14100 | 399  | 
|
| 20800 | 400  | 
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
 | 
401  | 
by (simp add: restrict_map_def)  | 
| 14186 | 402  | 
|
| 31380 | 403  | 
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
 | 
404  | 
by (auto simp: restrict_map_def)  | 
| 31380 | 405  | 
|
| 20800 | 406  | 
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
 | 
407  | 
by (simp add: restrict_map_def)  | 
| 14186 | 408  | 
|
| 15693 | 409  | 
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
 | 
410  | 
by (simp add: restrict_map_def)  | 
| 14100 | 411  | 
|
| 15693 | 412  | 
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
 | 
413  | 
by (simp add: restrict_map_def)  | 
| 14100 | 414  | 
|
| 15693 | 415  | 
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
 | 
416  | 
by (auto simp: restrict_map_def ran_def split: if_split_asm)  | 
| 14100 | 417  | 
|
| 15693 | 418  | 
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
 | 
419  | 
by (auto simp: restrict_map_def dom_def split: if_split_asm)  | 
| 14100 | 420  | 
|
| 15693 | 421  | 
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
 | 
422  | 
by (rule ext) (auto simp: restrict_map_def)  | 
| 14100 | 423  | 
|
| 15693 | 424  | 
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
 | 
425  | 
by (rule ext) (auto simp: restrict_map_def)  | 
| 14100 | 426  | 
|
| 20800 | 427  | 
lemma restrict_fun_upd [simp]:  | 
| 24331 | 428  | 
  "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
 | 
429  | 
by (simp add: restrict_map_def fun_eq_iff)  | 
| 14186 | 430  | 
|
| 20800 | 431  | 
lemma fun_upd_None_restrict [simp]:  | 
| 60839 | 432  | 
  "(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
 | 
433  | 
by (simp add: restrict_map_def fun_eq_iff)  | 
| 14186 | 434  | 
|
| 20800 | 435  | 
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
 | 
436  | 
by (simp add: restrict_map_def fun_eq_iff)  | 
| 14186 | 437  | 
|
| 20800 | 438  | 
lemma fun_upd_restrict_conv [simp]:  | 
| 24331 | 439  | 
  "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
 | 
440  | 
by (rule fun_upd_restrict)  | 
| 14186 | 441  | 
|
| 
35159
 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 
haftmann 
parents: 
35115 
diff
changeset
 | 
442  | 
lemma map_of_map_restrict:  | 
| 
 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 
haftmann 
parents: 
35115 
diff
changeset
 | 
443  | 
"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
 | 
444  | 
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
 | 
445  | 
|
| 35619 | 446  | 
lemma restrict_complement_singleton_eq:  | 
447  | 
  "f |` (- {x}) = f(x := None)"
 | 
|
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
448  | 
by auto  | 
| 35619 | 449  | 
|
| 14100 | 450  | 
|
| 60758 | 451  | 
subsection \<open>@{term [source] map_upds}\<close>
 | 
| 14025 | 452  | 
|
| 60838 | 453  | 
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
 | 
454  | 
by (simp add: map_upds_def)  | 
| 14025 | 455  | 
|
| 60838 | 456  | 
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
 | 
457  | 
by (simp add:map_upds_def)  | 
| 20800 | 458  | 
|
| 60838 | 459  | 
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
 | 
460  | 
by (simp add:map_upds_def)  | 
| 14025 | 461  | 
|
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
462  | 
lemma map_upds_append1 [simp]:  | 
| 
77644
 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 
nipkow 
parents: 
77361 
diff
changeset
 | 
463  | 
"size xs < size ys \<Longrightarrow> m(xs@[x] [\<mapsto>] ys) = m(xs [\<mapsto>] ys, x \<mapsto> ys!size xs)"  | 
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
464  | 
proof (induct xs arbitrary: ys m)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
465  | 
case Nil  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
466  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
467  | 
by (auto simp: neq_Nil_conv)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
468  | 
next  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
469  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
470  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
471  | 
by (cases ys) auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
472  | 
qed  | 
| 14187 | 473  | 
|
| 20800 | 474  | 
lemma map_upds_list_update2_drop [simp]:  | 
| 
46588
 
4895d7f1be42
removing some unnecessary premises from Map theory
 
bulwahn 
parents: 
44921 
diff
changeset
 | 
475  | 
"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
 | 
476  | 
proof (induct xs arbitrary: m ys i)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
477  | 
case Nil  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
478  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
479  | 
by auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
480  | 
next  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
481  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
482  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
483  | 
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
 | 
484  | 
qed  | 
| 14025 | 485  | 
|
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
486  | 
text \<open>Something weirdly sensitive about this proof, which needs only four lines in apply style\<close>  | 
| 20800 | 487  | 
lemma map_upd_upds_conv_if:  | 
| 60838 | 488  | 
"(f(x\<mapsto>y))(xs [\<mapsto>] ys) =  | 
| 60839 | 489  | 
(if x \<in> set(take (length ys) xs) then f(xs [\<mapsto>] ys)  | 
| 60838 | 490  | 
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
 | 
491  | 
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
 | 
492  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
493  | 
show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
494  | 
proof (cases ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
495  | 
case (Cons z zs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
496  | 
then show ?thesis  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
497  | 
using Cons.hyps  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
498  | 
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
 | 
499  | 
using Cons.hyps apply fastforce+  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
500  | 
done  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
501  | 
qed auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
502  | 
qed auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
503  | 
|
| 14025 | 504  | 
|
505  | 
lemma map_upds_twist [simp]:  | 
|
| 
77644
 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 
nipkow 
parents: 
77361 
diff
changeset
 | 
506  | 
"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
 | 
507  | 
using set_take_subset by (fastforce simp add: map_upd_upds_conv_if)  | 
| 14025 | 508  | 
|
| 20800 | 509  | 
lemma map_upds_apply_nontin [simp]:  | 
| 60839 | 510  | 
"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
 | 
511  | 
proof (induct xs arbitrary: ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
512  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
513  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
514  | 
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
 | 
515  | 
qed auto  | 
| 14025 | 516  | 
|
| 20800 | 517  | 
lemma fun_upds_append_drop [simp]:  | 
| 24331 | 518  | 
"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
 | 
519  | 
proof (induct xs arbitrary: ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
520  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
521  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
522  | 
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
 | 
523  | 
qed auto  | 
| 14300 | 524  | 
|
| 20800 | 525  | 
lemma fun_upds_append2_drop [simp]:  | 
| 24331 | 526  | 
"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
 | 
527  | 
proof (induct xs arbitrary: ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
528  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
529  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
530  | 
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
 | 
531  | 
qed auto  | 
| 14300 | 532  | 
|
| 20800 | 533  | 
lemma restrict_map_upds[simp]:  | 
534  | 
"\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk>  | 
|
535  | 
\<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
 | 
536  | 
proof (induct xs arbitrary: m ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
537  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
538  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
539  | 
proof (cases ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
540  | 
case (Cons z zs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
541  | 
with Cons.hyps Cons.prems show ?thesis  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
542  | 
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
 | 
543  | 
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
 | 
544  | 
done  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
545  | 
qed auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
546  | 
qed auto  | 
| 14186 | 547  | 
|
548  | 
||
| 60758 | 549  | 
subsection \<open>@{term [source] dom}\<close>
 | 
| 13908 | 550  | 
|
| 31080 | 551  | 
lemma dom_eq_empty_conv [simp]: "dom f = {} \<longleftrightarrow> f = empty"
 | 
| 44921 | 552  | 
by (auto simp: dom_def)  | 
| 31080 | 553  | 
|
| 60839 | 554  | 
lemma domI: "m a = Some b \<Longrightarrow> a \<in> dom m"  | 
555  | 
by (simp add: dom_def)  | 
|
| 14100 | 556  | 
(* declare domI [intro]? *)  | 
| 13908 | 557  | 
|
| 60839 | 558  | 
lemma domD: "a \<in> dom m \<Longrightarrow> \<exists>b. m a = Some b"  | 
559  | 
by (cases "m a") (auto simp add: dom_def)  | 
|
| 13908 | 560  | 
|
| 66010 | 561  | 
lemma domIff [iff, simp del, code_unfold]: "a \<in> dom m \<longleftrightarrow> m a \<noteq> None"  | 
| 60839 | 562  | 
by (simp add: dom_def)  | 
| 13908 | 563  | 
|
| 20800 | 564  | 
lemma dom_empty [simp]: "dom empty = {}"
 | 
| 60839 | 565  | 
by (simp add: dom_def)  | 
| 13908 | 566  | 
|
| 20800 | 567  | 
lemma dom_fun_upd [simp]:  | 
| 60839 | 568  | 
  "dom(f(x := y)) = (if y = None then dom f - {x} else insert x (dom f))"
 | 
569  | 
by (auto simp: dom_def)  | 
|
| 13908 | 570  | 
|
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
571  | 
lemma dom_if:  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
572  | 
  "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
 | 
573  | 
by (auto split: if_splits)  | 
| 13937 | 574  | 
|
| 15304 | 575  | 
lemma dom_map_of_conv_image_fst:  | 
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
576  | 
"dom (map_of xys) = fst ` set xys"  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
577  | 
by (induct xys) (auto simp add: dom_if)  | 
| 15304 | 578  | 
|
| 60839 | 579  | 
lemma dom_map_of_zip [simp]: "length xs = length ys \<Longrightarrow> dom (map_of (zip xs ys)) = set xs"  | 
580  | 
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
 | 
581  | 
|
| 13908 | 582  | 
lemma finite_dom_map_of: "finite (dom (map_of l))"  | 
| 60839 | 583  | 
by (induct l) (auto simp: dom_def insert_Collect [symmetric])  | 
| 13908 | 584  | 
|
| 20800 | 585  | 
lemma dom_map_upds [simp]:  | 
| 60839 | 586  | 
"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
 | 
587  | 
proof (induct xs arbitrary: ys)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
588  | 
case (Cons a xs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
589  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
590  | 
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
 | 
591  | 
qed auto  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
592  | 
|
| 13910 | 593  | 
|
| 60839 | 594  | 
lemma dom_map_add [simp]: "dom (m ++ n) = dom n \<union> dom m"  | 
595  | 
by (auto simp: dom_def)  | 
|
| 13910 | 596  | 
|
| 20800 | 597  | 
lemma dom_override_on [simp]:  | 
| 60839 | 598  | 
"dom (override_on f g A) =  | 
599  | 
    (dom f  - {a. a \<in> A - dom g}) \<union> {a. a \<in> A \<inter> dom g}"
 | 
|
600  | 
by (auto simp: dom_def override_on_def)  | 
|
| 13908 | 601  | 
|
| 60839 | 602  | 
lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1 ++ m2 = m2 ++ m1"
 | 
603  | 
by (rule ext) (force simp: map_add_def dom_def split: option.split)  | 
|
| 20800 | 604  | 
|
| 
32236
 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 
krauss 
parents: 
31380 
diff
changeset
 | 
605  | 
lemma map_add_dom_app_simps:  | 
| 60839 | 606  | 
"m \<in> dom l2 \<Longrightarrow> (l1 ++ l2) m = l2 m"  | 
607  | 
"m \<notin> dom l1 \<Longrightarrow> (l1 ++ l2) m = l2 m"  | 
|
608  | 
"m \<notin> dom l2 \<Longrightarrow> (l1 ++ l2) m = l1 m"  | 
|
609  | 
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
 | 
610  | 
|
| 29622 | 611  | 
lemma dom_const [simp]:  | 
| 
35159
 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 
haftmann 
parents: 
35115 
diff
changeset
 | 
612  | 
"dom (\<lambda>x. Some (f x)) = UNIV"  | 
| 29622 | 613  | 
by auto  | 
614  | 
||
| 22230 | 615  | 
(* Due to John Matthews - could be rephrased with dom *)  | 
616  | 
lemma finite_map_freshness:  | 
|
617  | 
"finite (dom (f :: 'a \<rightharpoonup> 'b)) \<Longrightarrow> \<not> finite (UNIV :: 'a set) \<Longrightarrow>  | 
|
618  | 
\<exists>x. f x = None"  | 
|
| 60839 | 619  | 
by (bestsimp dest: ex_new_if_finite)  | 
| 14027 | 620  | 
|
| 28790 | 621  | 
lemma dom_minus:  | 
622  | 
"f x = None \<Longrightarrow> dom f - insert x A = dom f - A"  | 
|
623  | 
unfolding dom_def by simp  | 
|
624  | 
||
625  | 
lemma insert_dom:  | 
|
626  | 
"f x = Some y \<Longrightarrow> insert x (dom f) = dom f"  | 
|
627  | 
unfolding dom_def by auto  | 
|
628  | 
||
| 35607 | 629  | 
lemma map_of_map_keys:  | 
630  | 
"set xs = dom m \<Longrightarrow> map_of (map (\<lambda>k. (k, the (m k))) xs) = m"  | 
|
631  | 
by (rule ext) (auto simp add: map_of_map_restrict restrict_map_def)  | 
|
632  | 
||
| 
39379
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
633  | 
lemma map_of_eqI:  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
634  | 
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
 | 
635  | 
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
 | 
636  | 
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
 | 
637  | 
proof (rule ext)  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
638  | 
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
 | 
639  | 
proof (cases "map_of xs k")  | 
| 60839 | 640  | 
case None  | 
641  | 
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
 | 
642  | 
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
 | 
643  | 
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
 | 
644  | 
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
 | 
645  | 
next  | 
| 60839 | 646  | 
case (Some v)  | 
647  | 
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
 | 
648  | 
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
 | 
649  | 
qed  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
650  | 
qed  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
651  | 
|
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
652  | 
lemma map_of_eq_dom:  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
653  | 
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
 | 
654  | 
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
 | 
655  | 
proof -  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
656  | 
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
 | 
657  | 
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
 | 
658  | 
qed  | 
| 
 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 
haftmann 
parents: 
39302 
diff
changeset
 | 
659  | 
|
| 53820 | 660  | 
lemma finite_set_of_finite_maps:  | 
| 60839 | 661  | 
assumes "finite A" "finite B"  | 
662  | 
  shows "finite {m. dom m = A \<and> ran m \<subseteq> B}" (is "finite ?S")
 | 
|
| 53820 | 663  | 
proof -  | 
664  | 
  let ?S' = "{m. \<forall>x. (x \<in> A \<longrightarrow> m x \<in> Some ` B) \<and> (x \<notin> A \<longrightarrow> m x = None)}"
 | 
|
665  | 
have "?S = ?S'"  | 
|
666  | 
proof  | 
|
| 60839 | 667  | 
show "?S \<subseteq> ?S'" by (auto simp: dom_def ran_def image_def)  | 
| 53820 | 668  | 
show "?S' \<subseteq> ?S"  | 
669  | 
proof  | 
|
670  | 
fix m assume "m \<in> ?S'"  | 
|
671  | 
hence 1: "dom m = A" by force  | 
|
| 60839 | 672  | 
hence 2: "ran m \<subseteq> B" using \<open>m \<in> ?S'\<close> by (auto simp: dom_def ran_def)  | 
| 53820 | 673  | 
from 1 2 show "m \<in> ?S" by blast  | 
674  | 
qed  | 
|
675  | 
qed  | 
|
676  | 
with assms show ?thesis by(simp add: finite_set_of_finite_funs)  | 
|
677  | 
qed  | 
|
| 28790 | 678  | 
|
| 60839 | 679  | 
|
| 60758 | 680  | 
subsection \<open>@{term [source] ran}\<close>
 | 
| 14100 | 681  | 
|
| 60839 | 682  | 
lemma ranI: "m a = Some b \<Longrightarrow> b \<in> ran m"  | 
683  | 
by (auto simp: ran_def)  | 
|
| 14100 | 684  | 
(* declare ranI [intro]? *)  | 
| 13908 | 685  | 
|
| 20800 | 686  | 
lemma ran_empty [simp]: "ran empty = {}"
 | 
| 60839 | 687  | 
by (auto simp: ran_def)  | 
| 13908 | 688  | 
|
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
689  | 
lemma ran_map_upd [simp]: "m a = None \<Longrightarrow> ran(m(a\<mapsto>b)) = insert b (ran m)"  | 
| 60839 | 690  | 
unfolding ran_def  | 
| 
71616
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
691  | 
by force  | 
| 20800 | 692  | 
|
| 73832 | 693  | 
lemma fun_upd_None_if_notin_dom[simp]: "k \<notin> dom m \<Longrightarrow> m(k := None) = m"  | 
694  | 
by auto  | 
|
695  | 
||
| 74802 | 696  | 
lemma ran_map_upd_Some:  | 
697  | 
  "\<lbrakk> m x = Some y; inj_on m (dom m); z \<notin> ran m \<rbrakk> \<Longrightarrow> ran(m(x := Some z)) = ran m - {y} \<union> {z}"
 | 
|
698  | 
by(force simp add: ran_def domI inj_onD)  | 
|
699  | 
||
| 66583 | 700  | 
lemma ran_map_add:  | 
701  | 
  assumes "dom m1 \<inter> dom m2 = {}"
 | 
|
702  | 
shows "ran (m1 ++ m2) = ran m1 \<union> ran m2"  | 
|
703  | 
proof  | 
|
704  | 
show "ran (m1 ++ m2) \<subseteq> ran m1 \<union> ran m2"  | 
|
705  | 
unfolding ran_def by auto  | 
|
706  | 
next  | 
|
707  | 
show "ran m1 \<union> ran m2 \<subseteq> ran (m1 ++ m2)"  | 
|
708  | 
proof -  | 
|
709  | 
have "(m1 ++ m2) x = Some y" if "m1 x = Some y" for x y  | 
|
710  | 
using assms map_add_comm that by fastforce  | 
|
711  | 
moreover have "(m1 ++ m2) x = Some y" if "m2 x = Some y" for x y  | 
|
712  | 
using assms that by auto  | 
|
713  | 
ultimately show ?thesis  | 
|
714  | 
unfolding ran_def by blast  | 
|
715  | 
qed  | 
|
716  | 
qed  | 
|
717  | 
||
718  | 
lemma finite_ran:  | 
|
719  | 
assumes "finite (dom p)"  | 
|
720  | 
shows "finite (ran p)"  | 
|
721  | 
proof -  | 
|
722  | 
have "ran p = (\<lambda>x. the (p x)) ` dom p"  | 
|
723  | 
unfolding ran_def by force  | 
|
724  | 
from this \<open>finite (dom p)\<close> show ?thesis by auto  | 
|
725  | 
qed  | 
|
726  | 
||
| 60839 | 727  | 
lemma ran_distinct:  | 
728  | 
assumes dist: "distinct (map fst al)"  | 
|
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
729  | 
shows "ran (map_of al) = snd ` set al"  | 
| 60839 | 730  | 
using assms  | 
731  | 
proof (induct al)  | 
|
732  | 
case Nil  | 
|
733  | 
then show ?case by simp  | 
|
| 
34979
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
734  | 
next  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
735  | 
case (Cons kv al)  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
736  | 
then have "ran (map_of al) = snd ` set al" by simp  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
737  | 
moreover from Cons.prems have "map_of al (fst kv) = None"  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
738  | 
by (simp add: map_of_eq_None_iff)  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
739  | 
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
 | 
740  | 
qed  | 
| 
 
8cb6e7a42e9c
more correspondence lemmas between related operations
 
haftmann 
parents: 
34941 
diff
changeset
 | 
741  | 
|
| 66584 | 742  | 
lemma ran_map_of_zip:  | 
743  | 
assumes "length xs = length ys" "distinct xs"  | 
|
744  | 
shows "ran (map_of (zip xs ys)) = set ys"  | 
|
745  | 
using assms by (simp add: ran_distinct set_map[symmetric])  | 
|
746  | 
||
| 60057 | 747  | 
lemma ran_map_option: "ran (\<lambda>x. map_option f (m x)) = f ` ran m"  | 
| 60839 | 748  | 
by (auto simp add: ran_def)  | 
749  | 
||
| 73832 | 750  | 
subsection \<open>@{term [source] graph}\<close>
 | 
751  | 
||
752  | 
lemma graph_empty[simp]: "graph empty = {}"
 | 
|
753  | 
unfolding graph_def by simp  | 
|
754  | 
||
755  | 
lemma in_graphI: "m k = Some v \<Longrightarrow> (k, v) \<in> graph m"  | 
|
756  | 
unfolding graph_def by blast  | 
|
757  | 
||
758  | 
lemma in_graphD: "(k, v) \<in> graph m \<Longrightarrow> m k = Some v"  | 
|
759  | 
unfolding graph_def by blast  | 
|
760  | 
||
761  | 
lemma graph_map_upd[simp]: "graph (m(k \<mapsto> v)) = insert (k, v) (graph (m(k := None)))"  | 
|
762  | 
unfolding graph_def by (auto split: if_splits)  | 
|
763  | 
||
764  | 
lemma graph_fun_upd_None: "graph (m(k := None)) = {e \<in> graph m. fst e \<noteq> k}"
 | 
|
765  | 
unfolding graph_def by (auto split: if_splits)  | 
|
766  | 
||
767  | 
lemma graph_restrictD:  | 
|
768  | 
assumes "(k, v) \<in> graph (m |` A)"  | 
|
769  | 
shows "k \<in> A" and "m k = Some v"  | 
|
770  | 
using assms unfolding graph_def  | 
|
771  | 
by (auto simp: restrict_map_def split: if_splits)  | 
|
772  | 
||
773  | 
lemma graph_map_comp[simp]: "graph (m1 \<circ>\<^sub>m m2) = graph m2 O graph m1"  | 
|
774  | 
unfolding graph_def by (auto simp: map_comp_Some_iff relcomp_unfold)  | 
|
775  | 
||
776  | 
lemma graph_map_add: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> graph (m1 ++ m2) = graph m1 \<union> graph m2"
 | 
|
777  | 
unfolding graph_def using map_add_comm by force  | 
|
778  | 
||
779  | 
lemma graph_eq_to_snd_dom: "graph m = (\<lambda>x. (x, the (m x))) ` dom m"  | 
|
780  | 
unfolding graph_def dom_def by force  | 
|
781  | 
||
782  | 
lemma fst_graph_eq_dom: "fst ` graph m = dom m"  | 
|
783  | 
unfolding graph_eq_to_snd_dom by force  | 
|
784  | 
||
785  | 
lemma graph_domD: "x \<in> graph m \<Longrightarrow> fst x \<in> dom m"  | 
|
786  | 
using fst_graph_eq_dom by (metis imageI)  | 
|
787  | 
||
788  | 
lemma snd_graph_ran: "snd ` graph m = ran m"  | 
|
789  | 
unfolding graph_def ran_def by force  | 
|
790  | 
||
791  | 
lemma graph_ranD: "x \<in> graph m \<Longrightarrow> snd x \<in> ran m"  | 
|
792  | 
using snd_graph_ran by (metis imageI)  | 
|
793  | 
||
794  | 
lemma finite_graph_map_of: "finite (graph (map_of al))"  | 
|
795  | 
unfolding graph_eq_to_snd_dom finite_dom_map_of  | 
|
796  | 
using finite_dom_map_of by blast  | 
|
797  | 
||
| 
74157
 
8e2355ddce1b
add/rename some theorems about Map(pings)
 
Lukas Stevens <mail@lukas-stevens.de> 
parents: 
73832 
diff
changeset
 | 
798  | 
lemma graph_map_of_if_distinct_dom: "distinct (map fst al) \<Longrightarrow> graph (map_of al) = set al"  | 
| 73832 | 799  | 
unfolding graph_def by auto  | 
800  | 
||
801  | 
lemma finite_graph_iff_finite_dom[simp]: "finite (graph m) = finite (dom m)"  | 
|
802  | 
by (metis graph_eq_to_snd_dom finite_imageI fst_graph_eq_dom)  | 
|
803  | 
||
804  | 
lemma inj_on_fst_graph: "inj_on fst (graph m)"  | 
|
805  | 
unfolding graph_def inj_on_def by force  | 
|
| 13910 | 806  | 
|
| 61799 | 807  | 
subsection \<open>\<open>map_le\<close>\<close>  | 
| 13910 | 808  | 
|
| 13912 | 809  | 
lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g"  | 
| 60839 | 810  | 
by (simp add: map_le_def)  | 
| 13910 | 811  | 
|
| 17724 | 812  | 
lemma upd_None_map_le [simp]: "f(x := None) \<subseteq>\<^sub>m f"  | 
| 60839 | 813  | 
by (force simp add: map_le_def)  | 
| 14187 | 814  | 
|
| 13910 | 815  | 
lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)"  | 
| 60839 | 816  | 
by (fastforce simp add: map_le_def)  | 
| 13910 | 817  | 
|
| 17724 | 818  | 
lemma map_le_imp_upd_le [simp]: "m1 \<subseteq>\<^sub>m m2 \<Longrightarrow> m1(x := None) \<subseteq>\<^sub>m m2(x \<mapsto> y)"  | 
| 60839 | 819  | 
by (force simp add: map_le_def)  | 
| 14187 | 820  | 
|
| 20800 | 821  | 
lemma map_le_upds [simp]:  | 
| 60839 | 822  | 
"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
 | 
823  | 
proof (induct as arbitrary: f g bs)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
824  | 
case (Cons a as)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
825  | 
then show ?case  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
826  | 
by (cases bs) (use Cons in auto)  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
827  | 
qed auto  | 
| 13908 | 828  | 
|
| 14033 | 829  | 
lemma map_le_implies_dom_le: "(f \<subseteq>\<^sub>m g) \<Longrightarrow> (dom f \<subseteq> dom g)"  | 
| 60839 | 830  | 
by (fastforce simp add: map_le_def dom_def)  | 
| 14033 | 831  | 
|
832  | 
lemma map_le_refl [simp]: "f \<subseteq>\<^sub>m f"  | 
|
| 60839 | 833  | 
by (simp add: map_le_def)  | 
| 14033 | 834  | 
|
| 14187 | 835  | 
lemma map_le_trans[trans]: "\<lbrakk> m1 \<subseteq>\<^sub>m m2; m2 \<subseteq>\<^sub>m m3\<rbrakk> \<Longrightarrow> m1 \<subseteq>\<^sub>m m3"  | 
| 60839 | 836  | 
by (auto simp add: map_le_def dom_def)  | 
| 14033 | 837  | 
|
838  | 
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
 | 
839  | 
unfolding map_le_def  | 
| 
 
a9de39608b1a
more tidying up of old apply-proofs
 
paulson <lp15@cam.ac.uk> 
parents: 
69593 
diff
changeset
 | 
840  | 
by (metis ext domIff)  | 
| 14033 | 841  | 
|
| 60839 | 842  | 
lemma map_le_map_add [simp]: "f \<subseteq>\<^sub>m g ++ f"  | 
843  | 
by (fastforce simp: map_le_def)  | 
|
| 14033 | 844  | 
|
| 60839 | 845  | 
lemma map_le_iff_map_add_commute: "f \<subseteq>\<^sub>m f ++ g \<longleftrightarrow> f ++ g = g ++ f"  | 
846  | 
by (fastforce simp: map_add_def map_le_def fun_eq_iff split: option.splits)  | 
|
| 15304 | 847  | 
|
| 60839 | 848  | 
lemma map_add_le_mapE: "f ++ g \<subseteq>\<^sub>m h \<Longrightarrow> g \<subseteq>\<^sub>m h"  | 
849  | 
by (fastforce simp: map_le_def map_add_def dom_def)  | 
|
| 15303 | 850  | 
|
| 60839 | 851  | 
lemma map_add_le_mapI: "\<lbrakk> f \<subseteq>\<^sub>m h; g \<subseteq>\<^sub>m h \<rbrakk> \<Longrightarrow> f ++ g \<subseteq>\<^sub>m h"  | 
852  | 
by (auto simp: map_le_def map_add_def dom_def split: option.splits)  | 
|
| 15303 | 853  | 
|
| 63828 | 854  | 
lemma map_add_subsumed1: "f \<subseteq>\<^sub>m g \<Longrightarrow> f++g = g"  | 
855  | 
by (simp add: map_add_le_mapI map_le_antisym)  | 
|
856  | 
||
857  | 
lemma map_add_subsumed2: "f \<subseteq>\<^sub>m g \<Longrightarrow> g++f = g"  | 
|
858  | 
by (metis map_add_subsumed1 map_le_iff_map_add_commute)  | 
|
859  | 
||
| 31080 | 860  | 
lemma dom_eq_singleton_conv: "dom f = {x} \<longleftrightarrow> (\<exists>v. f = [x \<mapsto> v])"
 | 
| 63834 | 861  | 
(is "?lhs \<longleftrightarrow> ?rhs")  | 
862  | 
proof  | 
|
863  | 
assume ?rhs  | 
|
864  | 
then show ?lhs by (auto split: if_split_asm)  | 
|
| 31080 | 865  | 
next  | 
| 63834 | 866  | 
assume ?lhs  | 
867  | 
then obtain v where v: "f x = Some v" by auto  | 
|
868  | 
show ?rhs  | 
|
869  | 
proof  | 
|
870  | 
show "f = [x \<mapsto> v]"  | 
|
871  | 
proof (rule map_le_antisym)  | 
|
872  | 
show "[x \<mapsto> v] \<subseteq>\<^sub>m f"  | 
|
873  | 
using v by (auto simp add: map_le_def)  | 
|
874  | 
show "f \<subseteq>\<^sub>m [x \<mapsto> v]"  | 
|
875  | 
        using \<open>dom f = {x}\<close> \<open>f x = Some v\<close> by (auto simp add: map_le_def)
 | 
|
876  | 
qed  | 
|
877  | 
qed  | 
|
| 31080 | 878  | 
qed  | 
879  | 
||
| 68454 | 880  | 
lemma map_add_eq_empty_iff[simp]:  | 
881  | 
"(f++g = empty) \<longleftrightarrow> f = empty \<and> g = empty"  | 
|
882  | 
by (metis map_add_None)  | 
|
883  | 
||
884  | 
lemma empty_eq_map_add_iff[simp]:  | 
|
885  | 
"(empty = f++g) \<longleftrightarrow> f = empty \<and> g = empty"  | 
|
886  | 
by(subst map_add_eq_empty_iff[symmetric])(rule eq_commute)  | 
|
887  | 
||
| 35565 | 888  | 
|
| 60758 | 889  | 
subsection \<open>Various\<close>  | 
| 35565 | 890  | 
|
891  | 
lemma set_map_of_compr:  | 
|
892  | 
assumes distinct: "distinct (map fst xs)"  | 
|
893  | 
  shows "set xs = {(k, v). map_of xs k = Some v}"
 | 
|
| 60839 | 894  | 
using assms  | 
895  | 
proof (induct xs)  | 
|
896  | 
case Nil  | 
|
897  | 
then show ?case by simp  | 
|
| 35565 | 898  | 
next  | 
899  | 
case (Cons x xs)  | 
|
900  | 
obtain k v where "x = (k, v)" by (cases x) blast  | 
|
901  | 
with Cons.prems have "k \<notin> dom (map_of xs)"  | 
|
902  | 
by (simp add: dom_map_of_conv_image_fst)  | 
|
903  | 
  then have *: "insert (k, v) {(k, v). map_of xs k = Some v} =
 | 
|
| 
77644
 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 
nipkow 
parents: 
77361 
diff
changeset
 | 
904  | 
    {(k', v'). ((map_of xs)(k \<mapsto> v)) k' = Some v'}"
 | 
| 35565 | 905  | 
by (auto split: if_splits)  | 
906  | 
  from Cons have "set xs = {(k, v). map_of xs k = Some v}" by simp
 | 
|
| 60758 | 907  | 
with * \<open>x = (k, v)\<close> show ?case by simp  | 
| 35565 | 908  | 
qed  | 
909  | 
||
| 67051 | 910  | 
lemma eq_key_imp_eq_value:  | 
911  | 
"v1 = v2"  | 
|
912  | 
if "distinct (map fst xs)" "(k, v1) \<in> set xs" "(k, v2) \<in> set xs"  | 
|
913  | 
proof -  | 
|
914  | 
from that have "inj_on fst (set xs)"  | 
|
915  | 
by (simp add: distinct_map)  | 
|
916  | 
moreover have "fst (k, v1) = fst (k, v2)"  | 
|
917  | 
by simp  | 
|
918  | 
ultimately have "(k, v1) = (k, v2)"  | 
|
919  | 
by (rule inj_onD) (fact that)+  | 
|
920  | 
then show ?thesis  | 
|
921  | 
by simp  | 
|
922  | 
qed  | 
|
923  | 
||
| 35565 | 924  | 
lemma map_of_inject_set:  | 
925  | 
assumes distinct: "distinct (map fst xs)" "distinct (map fst ys)"  | 
|
926  | 
shows "map_of xs = map_of ys \<longleftrightarrow> set xs = set ys" (is "?lhs \<longleftrightarrow> ?rhs")  | 
|
927  | 
proof  | 
|
928  | 
assume ?lhs  | 
|
| 60758 | 929  | 
  moreover from \<open>distinct (map fst xs)\<close> have "set xs = {(k, v). map_of xs k = Some v}"
 | 
| 35565 | 930  | 
by (rule set_map_of_compr)  | 
| 60758 | 931  | 
  moreover from \<open>distinct (map fst ys)\<close> have "set ys = {(k, v). map_of ys k = Some v}"
 | 
| 35565 | 932  | 
by (rule set_map_of_compr)  | 
933  | 
ultimately show ?rhs by simp  | 
|
934  | 
next  | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53015 
diff
changeset
 | 
935  | 
assume ?rhs show ?lhs  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53015 
diff
changeset
 | 
936  | 
proof  | 
| 35565 | 937  | 
fix k  | 
| 60839 | 938  | 
show "map_of xs k = map_of ys k"  | 
939  | 
proof (cases "map_of xs k")  | 
|
| 35565 | 940  | 
case None  | 
| 60758 | 941  | 
with \<open>?rhs\<close> have "map_of ys k = None"  | 
| 35565 | 942  | 
by (simp add: map_of_eq_None_iff)  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53015 
diff
changeset
 | 
943  | 
with None show ?thesis by simp  | 
| 35565 | 944  | 
next  | 
945  | 
case (Some v)  | 
|
| 60758 | 946  | 
with distinct \<open>?rhs\<close> have "map_of ys k = Some v"  | 
| 35565 | 947  | 
by simp  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53015 
diff
changeset
 | 
948  | 
with Some show ?thesis by simp  | 
| 35565 | 949  | 
qed  | 
950  | 
qed  | 
|
951  | 
qed  | 
|
952  | 
||
| 73832 | 953  | 
lemma finite_Map_induct[consumes 1, case_names empty update]:  | 
954  | 
assumes "finite (dom m)"  | 
|
955  | 
assumes "P Map.empty"  | 
|
956  | 
assumes "\<And>k v m. finite (dom m) \<Longrightarrow> k \<notin> dom m \<Longrightarrow> P m \<Longrightarrow> P (m(k \<mapsto> v))"  | 
|
957  | 
shows "P m"  | 
|
958  | 
using assms(1)  | 
|
959  | 
proof(induction "dom m" arbitrary: m rule: finite_induct)  | 
|
960  | 
case empty  | 
|
961  | 
then show ?case using assms(2) unfolding dom_def by simp  | 
|
962  | 
next  | 
|
963  | 
case (insert x F)  | 
|
964  | 
then have "finite (dom (m(x:=None)))" "x \<notin> dom (m(x:=None))" "P (m(x:=None))"  | 
|
965  | 
by (metis Diff_insert_absorb dom_fun_upd)+  | 
|
966  | 
with assms(3)[OF this] show ?case  | 
|
967  | 
by (metis fun_upd_triv fun_upd_upd option.exhaust)  | 
|
968  | 
qed  | 
|
969  | 
||
970  | 
hide_const (open) Map.empty Map.graph  | 
|
| 68450 | 971  | 
|
| 3981 | 972  | 
end  |