src/HOL/Library/Mapping.thy
author haftmann
Tue Feb 19 19:44:10 2013 +0100 (2013-02-19)
changeset 51188 9b5bf1a9a710
parent 51161 6ed12ae3b3e1
child 51375 d9e62d9c98de
permissions -rw-r--r--
dropped spurious left-over from 0a2371e7ced3
     1 (*  Title:      HOL/Library/Mapping.thy
     2     Author:     Florian Haftmann and Ondrej Kuncar
     3 *)
     4 
     5 header {* An abstract view on maps for code generation. *}
     6 
     7 theory Mapping
     8 imports Main Quotient_Option
     9 begin
    10 
    11 subsection {* Type definition and primitive operations *}
    12 
    13 typedef ('a, 'b) mapping = "UNIV :: ('a \<rightharpoonup> 'b) set"
    14   morphisms rep Mapping ..
    15 
    16 setup_lifting(no_code) type_definition_mapping
    17 
    18 lift_definition empty :: "('a, 'b) mapping" is "(\<lambda>_. None)" .
    19 
    20 lift_definition lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<Rightarrow> 'b option" is "\<lambda>m k. m k" .
    21 
    22 lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is "\<lambda>k v m. m(k \<mapsto> v)" .
    23 
    24 lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is "\<lambda>k m. m(k := None)" .
    25 
    26 lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" is dom .
    27 
    28 lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" is
    29   "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" .
    30 
    31 lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" is
    32   "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" .
    33 
    34 lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping" is
    35   "\<lambda>f g m. (Option.map g \<circ> m \<circ> f)" .
    36 
    37 
    38 subsection {* Functorial structure *}
    39 
    40 enriched_type map: map
    41   by (transfer, auto simp add: fun_eq_iff Option.map.compositionality Option.map.id)+
    42 
    43 
    44 subsection {* Derived operations *}
    45 
    46 definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
    47   "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
    48 
    49 definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
    50   "is_empty m \<longleftrightarrow> keys m = {}"
    51 
    52 definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
    53   "size m = (if finite (keys m) then card (keys m) else 0)"
    54 
    55 definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    56   "replace k v m = (if k \<in> keys m then update k v m else m)"
    57 
    58 definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    59   "default k v m = (if k \<in> keys m then m else update k v m)"
    60 
    61 lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
    62   "\<lambda>k f m. (case m k of None \<Rightarrow> m
    63     | Some v \<Rightarrow> m (k \<mapsto> (f v)))" .
    64 
    65 lemma map_entry_code [code]: "map_entry k f m = (case lookup m k of None \<Rightarrow> m
    66     | Some v \<Rightarrow> update k (f v) m)"
    67   by transfer rule
    68 
    69 definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    70   "map_default k v f m = map_entry k f (default k v m)" 
    71 
    72 instantiation mapping :: (type, type) equal
    73 begin
    74 
    75 definition
    76   "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. lookup m1 k = lookup m2 k)"
    77 
    78 instance proof
    79 qed (unfold equal_mapping_def, transfer, auto)
    80 
    81 end
    82 
    83 lemma [transfer_rule]:
    84   "fun_rel cr_mapping (fun_rel cr_mapping HOL.iff) HOL.eq HOL.equal"
    85   by (unfold equal) transfer_prover
    86 
    87 
    88 subsection {* Properties *}
    89 
    90 lemma lookup_update: "lookup (update k v m) k = Some v" 
    91   by transfer simp
    92 
    93 lemma lookup_update_neq: "k \<noteq> k' \<Longrightarrow> lookup (update k v m) k' = lookup m k'" 
    94   by transfer simp
    95 
    96 lemma lookup_empty: "lookup empty k = None" 
    97   by transfer simp
    98 
    99 lemma keys_is_none_rep [code_unfold]:
   100   "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
   101   by transfer (auto simp add: is_none_def)
   102 
   103 lemma tabulate_alt_def:
   104   "map_of (List.map (\<lambda>k. (k, f k)) ks) = (Some o f) |` set ks"
   105   by (induct ks) (auto simp add: tabulate_def restrict_map_def)
   106 
   107 lemma update_update:
   108   "update k v (update k w m) = update k v m"
   109   "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
   110   by (transfer, simp add: fun_upd_twist)+
   111 
   112 lemma update_delete [simp]:
   113   "update k v (delete k m) = update k v m"
   114   by transfer simp
   115 
   116 lemma delete_update:
   117   "delete k (update k v m) = delete k m"
   118   "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
   119   by (transfer, simp add: fun_upd_twist)+
   120 
   121 lemma delete_empty [simp]:
   122   "delete k empty = empty"
   123   by transfer simp
   124 
   125 lemma replace_update:
   126   "k \<notin> keys m \<Longrightarrow> replace k v m = m"
   127   "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
   128   by (transfer, auto simp add: replace_def fun_upd_twist)+
   129 
   130 lemma size_empty [simp]:
   131   "size empty = 0"
   132   unfolding size_def by transfer simp
   133 
   134 lemma size_update:
   135   "finite (keys m) \<Longrightarrow> size (update k v m) =
   136     (if k \<in> keys m then size m else Suc (size m))"
   137   unfolding size_def by transfer (auto simp add: insert_dom)
   138 
   139 lemma size_delete:
   140   "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
   141   unfolding size_def by transfer simp
   142 
   143 lemma size_tabulate [simp]:
   144   "size (tabulate ks f) = length (remdups ks)"
   145   unfolding size_def by transfer (auto simp add: tabulate_alt_def card_set comp_def)
   146 
   147 lemma bulkload_tabulate:
   148   "bulkload xs = tabulate [0..<length xs] (nth xs)"
   149   by transfer (auto simp add: tabulate_alt_def)
   150 
   151 lemma is_empty_empty [simp]:
   152   "is_empty empty"
   153   unfolding is_empty_def by transfer simp 
   154 
   155 lemma is_empty_update [simp]:
   156   "\<not> is_empty (update k v m)"
   157   unfolding is_empty_def by transfer simp
   158 
   159 lemma is_empty_delete:
   160   "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
   161   unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
   162 
   163 lemma is_empty_replace [simp]:
   164   "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
   165   unfolding is_empty_def replace_def by transfer auto
   166 
   167 lemma is_empty_default [simp]:
   168   "\<not> is_empty (default k v m)"
   169   unfolding is_empty_def default_def by transfer auto
   170 
   171 lemma is_empty_map_entry [simp]:
   172   "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
   173   unfolding is_empty_def 
   174   apply transfer by (case_tac "m k") auto
   175 
   176 lemma is_empty_map_default [simp]:
   177   "\<not> is_empty (map_default k v f m)"
   178   by (simp add: map_default_def)
   179 
   180 lemma keys_empty [simp]:
   181   "keys empty = {}"
   182   by transfer simp
   183 
   184 lemma keys_update [simp]:
   185   "keys (update k v m) = insert k (keys m)"
   186   by transfer simp
   187 
   188 lemma keys_delete [simp]:
   189   "keys (delete k m) = keys m - {k}"
   190   by transfer simp
   191 
   192 lemma keys_replace [simp]:
   193   "keys (replace k v m) = keys m"
   194   unfolding replace_def by transfer (simp add: insert_absorb)
   195 
   196 lemma keys_default [simp]:
   197   "keys (default k v m) = insert k (keys m)"
   198   unfolding default_def by transfer (simp add: insert_absorb)
   199 
   200 lemma keys_map_entry [simp]:
   201   "keys (map_entry k f m) = keys m"
   202   apply transfer by (case_tac "m k") auto
   203 
   204 lemma keys_map_default [simp]:
   205   "keys (map_default k v f m) = insert k (keys m)"
   206   by (simp add: map_default_def)
   207 
   208 lemma keys_tabulate [simp]:
   209   "keys (tabulate ks f) = set ks"
   210   by transfer (simp add: map_of_map_restrict o_def)
   211 
   212 lemma keys_bulkload [simp]:
   213   "keys (bulkload xs) = {0..<length xs}"
   214   by (simp add: keys_tabulate bulkload_tabulate)
   215 
   216 lemma distinct_ordered_keys [simp]:
   217   "distinct (ordered_keys m)"
   218   by (simp add: ordered_keys_def)
   219 
   220 lemma ordered_keys_infinite [simp]:
   221   "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
   222   by (simp add: ordered_keys_def)
   223 
   224 lemma ordered_keys_empty [simp]:
   225   "ordered_keys empty = []"
   226   by (simp add: ordered_keys_def)
   227 
   228 lemma ordered_keys_update [simp]:
   229   "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
   230   "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
   231   by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
   232 
   233 lemma ordered_keys_delete [simp]:
   234   "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
   235 proof (cases "finite (keys m)")
   236   case False then show ?thesis by simp
   237 next
   238   case True note fin = True
   239   show ?thesis
   240   proof (cases "k \<in> keys m")
   241     case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
   242     with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
   243   next
   244     case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
   245   qed
   246 qed
   247 
   248 lemma ordered_keys_replace [simp]:
   249   "ordered_keys (replace k v m) = ordered_keys m"
   250   by (simp add: replace_def)
   251 
   252 lemma ordered_keys_default [simp]:
   253   "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
   254   "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
   255   by (simp_all add: default_def)
   256 
   257 lemma ordered_keys_map_entry [simp]:
   258   "ordered_keys (map_entry k f m) = ordered_keys m"
   259   by (simp add: ordered_keys_def)
   260 
   261 lemma ordered_keys_map_default [simp]:
   262   "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
   263   "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
   264   by (simp_all add: map_default_def)
   265 
   266 lemma ordered_keys_tabulate [simp]:
   267   "ordered_keys (tabulate ks f) = sort (remdups ks)"
   268   by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
   269 
   270 lemma ordered_keys_bulkload [simp]:
   271   "ordered_keys (bulkload ks) = [0..<length ks]"
   272   by (simp add: ordered_keys_def)
   273 
   274 
   275 subsection {* Code generator setup *}
   276 
   277 code_datatype empty update
   278 
   279 hide_const (open) empty is_empty rep lookup update delete ordered_keys keys size
   280   replace default map_entry map_default tabulate bulkload map
   281 
   282 end
   283