38 "size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)" |
38 "size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)" |
39 |
39 |
40 definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where |
40 definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where |
41 "replace k v m = (if lookup m k = None then m else update k v m)" |
41 "replace k v m = (if lookup m k = None then m else update k v m)" |
42 |
42 |
|
43 definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where |
|
44 "default k v m = (if lookup m k = None then update k v m else m)" |
|
45 |
|
46 definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where |
|
47 "map_entry k f m = (case lookup m k of None \<Rightarrow> m |
|
48 | Some v \<Rightarrow> update k (f v) m)" |
|
49 |
|
50 definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where |
|
51 "map_default k v f m = map_entry k f (default k v m)" |
|
52 |
43 definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where |
53 definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where |
44 "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))" |
54 "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))" |
45 |
55 |
46 definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where |
56 definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where |
47 "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)" |
57 "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)" |
67 by (cases m) simp |
77 by (cases m) simp |
68 |
78 |
69 lemma lookup_delete [simp]: |
79 lemma lookup_delete [simp]: |
70 "lookup (delete k m) = (lookup m) (k := None)" |
80 "lookup (delete k m) = (lookup m) (k := None)" |
71 by (cases m) simp |
81 by (cases m) simp |
|
82 |
|
83 lemma lookup_map_entry [simp]: |
|
84 "lookup (map_entry k f m) = (lookup m) (k := Option.map f (lookup m k))" |
|
85 by (cases "lookup m k") (simp_all add: map_entry_def expand_fun_eq) |
72 |
86 |
73 lemma lookup_tabulate [simp]: |
87 lemma lookup_tabulate [simp]: |
74 "lookup (tabulate ks f) = (Some o f) |` set ks" |
88 "lookup (tabulate ks f) = (Some o f) |` set ks" |
75 by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq) |
89 by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq) |
76 |
90 |
120 |
134 |
121 lemma bulkload_tabulate: |
135 lemma bulkload_tabulate: |
122 "bulkload xs = tabulate [0..<length xs] (nth xs)" |
136 "bulkload xs = tabulate [0..<length xs] (nth xs)" |
123 by (rule mapping_eqI) (simp add: expand_fun_eq) |
137 by (rule mapping_eqI) (simp add: expand_fun_eq) |
124 |
138 |
|
139 lemma keys_tabulate: |
|
140 "keys (tabulate ks f) = set ks" |
|
141 by (simp add: tabulate_def keys_def map_of_map_restrict o_def) |
|
142 |
|
143 lemma keys_bulkload: |
|
144 "keys (bulkload xs) = {0..<length xs}" |
|
145 by (simp add: keys_tabulate bulkload_tabulate) |
|
146 |
125 lemma is_empty_empty: |
147 lemma is_empty_empty: |
126 "is_empty m \<longleftrightarrow> m = Mapping Map.empty" |
148 "is_empty m \<longleftrightarrow> m = Mapping Map.empty" |
127 by (cases m) (simp add: is_empty_def) |
149 by (cases m) (simp add: is_empty_def) |
128 |
150 |
129 |
151 |