src/HOL/Library/Mapping.thy
author haftmann
Fri Feb 06 09:05:19 2009 +0100 (2009-02-06)
changeset 29814 15344c0899e1
parent 29708 e40b70d38909
child 29826 5132da6ebca3
permissions -rw-r--r--
added replace operation
     1 (*  Title:      HOL/Library/Mapping.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* An abstract view on maps for code generation. *}
     6 
     7 theory Mapping
     8 imports Map
     9 begin
    10 
    11 subsection {* Type definition and primitive operations *}
    12 
    13 datatype ('a, 'b) map = Map "'a \<rightharpoonup> 'b"
    14 
    15 definition empty :: "('a, 'b) map" where
    16   "empty = Map (\<lambda>_. None)"
    17 
    18 primrec lookup :: "('a, 'b) map \<Rightarrow> 'a \<rightharpoonup> 'b" where
    19   "lookup (Map f) = f"
    20 
    21 primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    22   "update k v (Map f) = Map (f (k \<mapsto> v))"
    23 
    24 primrec delete :: "'a \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    25   "delete k (Map f) = Map (f (k := None))"
    26 
    27 primrec keys :: "('a, 'b) map \<Rightarrow> 'a set" where
    28   "keys (Map f) = dom f"
    29 
    30 
    31 subsection {* Derived operations *}
    32 
    33 definition size :: "('a, 'b) map \<Rightarrow> nat" where
    34   "size m = (if finite (keys m) then card (keys m) else 0)"
    35 
    36 definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    37   "replace k v m = (if lookup m k = None then m else update k v m)"
    38 
    39 definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) map" where
    40   "tabulate ks f = Map (map_of (map (\<lambda>k. (k, f k)) ks))"
    41 
    42 
    43 subsection {* Properties *}
    44 
    45 lemma lookup_inject:
    46   "lookup m = lookup n \<longleftrightarrow> m = n"
    47   by (cases m, cases n) simp
    48 
    49 lemma lookup_empty [simp]:
    50   "lookup empty = Map.empty"
    51   by (simp add: empty_def)
    52 
    53 lemma lookup_update [simp]:
    54   "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
    55   by (cases m) simp
    56 
    57 lemma lookup_delete:
    58   "lookup (delete k m) k = None"
    59   "k \<noteq> l \<Longrightarrow> lookup (delete k m) l = lookup m l"
    60   by (cases m, simp)+
    61 
    62 lemma lookup_tabulate:
    63   "lookup (tabulate ks f) = (Some o f) |` set ks"
    64   by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
    65 
    66 lemma update_update:
    67   "update k v (update k w m) = update k v m"
    68   "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
    69   by (cases m, simp add: expand_fun_eq)+
    70 
    71 lemma replace_update:
    72   "lookup m k = None \<Longrightarrow> replace k v m = m"
    73   "lookup m k \<noteq> None \<Longrightarrow> replace k v m = update k v m"
    74   by (auto simp add: replace_def)
    75 
    76 lemma delete_empty [simp]:
    77   "delete k empty = empty"
    78   by (simp add: empty_def)
    79 
    80 lemma delete_update:
    81   "delete k (update k v m) = delete k m"
    82   "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
    83   by (cases m, simp add: expand_fun_eq)+
    84 
    85 lemma update_delete [simp]:
    86   "update k v (delete k m) = update k v m"
    87   by (cases m) simp
    88 
    89 lemma keys_empty [simp]:
    90   "keys empty = {}"
    91   unfolding empty_def by simp
    92 
    93 lemma keys_update [simp]:
    94   "keys (update k v m) = insert k (keys m)"
    95   by (cases m) simp
    96 
    97 lemma keys_delete [simp]:
    98   "keys (delete k m) = keys m - {k}"
    99   by (cases m) simp
   100 
   101 lemma keys_tabulate [simp]:
   102   "keys (tabulate ks f) = set ks"
   103   by (auto simp add: tabulate_def dest: map_of_SomeD intro!: weak_map_of_SomeI)
   104 
   105 lemma size_empty [simp]:
   106   "size empty = 0"
   107   by (simp add: size_def keys_empty)
   108 
   109 lemma size_update:
   110   "finite (keys m) \<Longrightarrow> size (update k v m) =
   111     (if k \<in> keys m then size m else Suc (size m))"
   112   by (simp add: size_def keys_update)
   113     (auto simp only: card_insert card_Suc_Diff1)
   114 
   115 lemma size_delete:
   116   "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
   117   by (simp add: size_def keys_delete)
   118 
   119 lemma size_tabulate:
   120   "size (tabulate ks f) = length (remdups ks)"
   121   by (simp add: size_def keys_tabulate distinct_card [of "remdups ks", symmetric])
   122 
   123 end