author  haftmann 
Thu, 20 May 2010 17:29:43 +0200  
changeset 37026  7e8979a155ae 
parent 36176  3fe7e97ccca8 
child 37052  80dd92673fca 
permissions  rwrr 
31459  1 
(* Author: Florian Haftmann, TU Muenchen *) 
29708  2 

3 
header {* An abstract view on maps for code generation. *} 

4 

5 
theory Mapping 

35157  6 
imports Main 
29708  7 
begin 
8 

9 
subsection {* Type definition and primitive operations *} 

10 

35157  11 
datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b" 
29708  12 

35157  13 
definition empty :: "('a, 'b) mapping" where 
14 
"empty = Mapping (\<lambda>_. None)" 

29708  15 

35157  16 
primrec lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<rightharpoonup> 'b" where 
17 
"lookup (Mapping f) = f" 

29708  18 

35157  19 
primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where 
20 
"update k v (Mapping f) = Mapping (f (k \<mapsto> v))" 

29708  21 

35157  22 
primrec delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where 
23 
"delete k (Mapping f) = Mapping (f (k := None))" 

29708  24 

25 

26 
subsection {* Derived operations *} 

27 

35157  28 
definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where 
29 
"keys m = dom (lookup m)" 

29708  30 

35194  31 
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where 
32 
"ordered_keys m = sorted_list_of_set (keys m)" 

33 

35157  34 
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where 
35 
"is_empty m \<longleftrightarrow> dom (lookup m) = {}" 

36 

37 
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where 

38 
"size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)" 

39 

40 
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where 

29814  41 
"replace k v m = (if lookup m k = None then m else update k v m)" 
42 

37026
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

43 
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

44 
"default k v m = (if lookup m k = None then update k v m else m)" 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

45 

7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

46 
definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

47 
"map_entry k f m = (case lookup m k of None \<Rightarrow> m 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

48 
 Some v \<Rightarrow> update k (f v) m)" 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

49 

7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

50 
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

51 
"map_default k v f m = map_entry k f (default k v m)" 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

52 

35157  53 
definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where 
54 
"tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))" 

29708  55 

35157  56 
definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where 
57 
"bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)" 

29826  58 

29708  59 

60 
subsection {* Properties *} 

61 

35157  62 
lemma lookup_inject [simp]: 
29708  63 
"lookup m = lookup n \<longleftrightarrow> m = n" 
64 
by (cases m, cases n) simp 

65 

35157  66 
lemma mapping_eqI: 
67 
assumes "lookup m = lookup n" 

68 
shows "m = n" 

69 
using assms by simp 

70 

29708  71 
lemma lookup_empty [simp]: 
72 
"lookup empty = Map.empty" 

73 
by (simp add: empty_def) 

74 

75 
lemma lookup_update [simp]: 

76 
"lookup (update k v m) = (lookup m) (k \<mapsto> v)" 

77 
by (cases m) simp 

78 

35157  79 
lemma lookup_delete [simp]: 
80 
"lookup (delete k m) = (lookup m) (k := None)" 

81 
by (cases m) simp 

29708  82 

37026
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

83 
lemma lookup_map_entry [simp]: 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

84 
"lookup (map_entry k f m) = (lookup m) (k := Option.map f (lookup m k))" 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

85 
by (cases "lookup m k") (simp_all add: map_entry_def expand_fun_eq) 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

86 

35157  87 
lemma lookup_tabulate [simp]: 
29708  88 
"lookup (tabulate ks f) = (Some o f) ` set ks" 
89 
by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq) 

90 

35157  91 
lemma lookup_bulkload [simp]: 
29826  92 
"lookup (bulkload xs) = (\<lambda>k. if k < length xs then Some (xs ! k) else None)" 
35157  93 
by (simp add: bulkload_def) 
29826  94 

29708  95 
lemma update_update: 
96 
"update k v (update k w m) = update k v m" 

97 
"k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)" 

35157  98 
by (rule mapping_eqI, simp add: fun_upd_twist)+ 
29708  99 

35157  100 
lemma update_delete [simp]: 
101 
"update k v (delete k m) = update k v m" 

102 
by (rule mapping_eqI) simp 

29708  103 

104 
lemma delete_update: 

105 
"delete k (update k v m) = delete k m" 

106 
"k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)" 

35157  107 
by (rule mapping_eqI, simp add: fun_upd_twist)+ 
29708  108 

35157  109 
lemma delete_empty [simp]: 
110 
"delete k empty = empty" 

111 
by (rule mapping_eqI) simp 

29708  112 

35157  113 
lemma replace_update: 
114 
"k \<notin> dom (lookup m) \<Longrightarrow> replace k v m = m" 

115 
"k \<in> dom (lookup m) \<Longrightarrow> replace k v m = update k v m" 

116 
by (rule mapping_eqI, auto simp add: replace_def fun_upd_twist)+ 

29708  117 

118 
lemma size_empty [simp]: 

119 
"size empty = 0" 

35157  120 
by (simp add: size_def) 
29708  121 

122 
lemma size_update: 

35157  123 
"finite (dom (lookup m)) \<Longrightarrow> size (update k v m) = 
124 
(if k \<in> dom (lookup m) then size m else Suc (size m))" 

125 
by (auto simp add: size_def insert_dom) 

29708  126 

127 
lemma size_delete: 

35157  128 
"size (delete k m) = (if k \<in> dom (lookup m) then size m  1 else size m)" 
129 
by (simp add: size_def) 

29708  130 

131 
lemma size_tabulate: 

132 
"size (tabulate ks f) = length (remdups ks)" 

35157  133 
by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def) 
29708  134 

29831  135 
lemma bulkload_tabulate: 
29826  136 
"bulkload xs = tabulate [0..<length xs] (nth xs)" 
35157  137 
by (rule mapping_eqI) (simp add: expand_fun_eq) 
29826  138 

37026
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

139 
lemma keys_tabulate: 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

140 
"keys (tabulate ks f) = set ks" 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

141 
by (simp add: tabulate_def keys_def map_of_map_restrict o_def) 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

142 

7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

143 
lemma keys_bulkload: 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

144 
"keys (bulkload xs) = {0..<length xs}" 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

145 
by (simp add: keys_tabulate bulkload_tabulate) 
7e8979a155ae
operations default, map_entry, map_default; more lemmas
haftmann
parents:
36176
diff
changeset

146 

36110  147 
lemma is_empty_empty: 
148 
"is_empty m \<longleftrightarrow> m = Mapping Map.empty" 

149 
by (cases m) (simp add: is_empty_def) 

150 

31459  151 

152 
subsection {* Some technical code lemmas *} 

153 

154 
lemma [code]: 

35157  155 
"mapping_case f m = f (Mapping.lookup m)" 
31459  156 
by (cases m) simp 
157 

158 
lemma [code]: 

35157  159 
"mapping_rec f m = f (Mapping.lookup m)" 
31459  160 
by (cases m) simp 
161 

162 
lemma [code]: 

35157  163 
"Nat.size (m :: (_, _) mapping) = 0" 
31459  164 
by (cases m) simp 
165 

166 
lemma [code]: 

35157  167 
"mapping_size f g m = 0" 
31459  168 
by (cases m) simp 
169 

35157  170 

36176
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact'  frees some popular keywords;
wenzelm
parents:
36110
diff
changeset

171 
hide_const (open) empty is_empty lookup update delete ordered_keys keys size replace tabulate bulkload 
35157  172 

29708  173 
end 