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