src/HOL/Library/Mapping.thy
author haftmann
Thu, 20 May 2010 17:29:43 +0200
changeset 37026 7e8979a155ae
parent 36176 3fe7e97ccca8
child 37052 80dd92673fca
permissions -rw-r--r--
operations default, map_entry, map_default; more lemmas

(* Author: Florian Haftmann, TU Muenchen *)

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

theory Mapping
imports Main
begin

subsection {* Type definition and primitive operations *}

datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b"

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

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

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

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


subsection {* Derived operations *}

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

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

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

definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
  "size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)"

definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
  "replace k v m = (if lookup m k = None then m else update k v m)"

definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
  "default k v m = (if lookup m k = None then update k v m else m)"

definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
    | Some v \<Rightarrow> update k (f v) m)" 

definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
  "map_default k v f m = map_entry k f (default k v m)" 

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

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


subsection {* Properties *}

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

lemma mapping_eqI:
  assumes "lookup m = lookup n"
  shows "m = n"
  using assms by simp

lemma lookup_empty [simp]:
  "lookup empty = Map.empty"
  by (simp add: empty_def)

lemma lookup_update [simp]:
  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
  by (cases m) simp

lemma lookup_delete [simp]:
  "lookup (delete k m) = (lookup m) (k := None)"
  by (cases m) simp

lemma lookup_map_entry [simp]:
  "lookup (map_entry k f m) = (lookup m) (k := Option.map f (lookup m k))"
  by (cases "lookup m k") (simp_all add: map_entry_def expand_fun_eq)

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

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

lemma update_update:
  "update k v (update k w m) = update k v m"
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
  by (rule mapping_eqI, simp add: fun_upd_twist)+

lemma update_delete [simp]:
  "update k v (delete k m) = update k v m"
  by (rule mapping_eqI) simp

lemma delete_update:
  "delete k (update k v m) = delete k m"
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
  by (rule mapping_eqI, simp add: fun_upd_twist)+

lemma delete_empty [simp]:
  "delete k empty = empty"
  by (rule mapping_eqI) simp

lemma replace_update:
  "k \<notin> dom (lookup m) \<Longrightarrow> replace k v m = m"
  "k \<in> dom (lookup m) \<Longrightarrow> replace k v m = update k v m"
  by (rule mapping_eqI, auto simp add: replace_def fun_upd_twist)+

lemma size_empty [simp]:
  "size empty = 0"
  by (simp add: size_def)

lemma size_update:
  "finite (dom (lookup m)) \<Longrightarrow> size (update k v m) =
    (if k \<in> dom (lookup m) then size m else Suc (size m))"
  by (auto simp add: size_def insert_dom)

lemma size_delete:
  "size (delete k m) = (if k \<in> dom (lookup m) then size m - 1 else size m)"
  by (simp add: size_def)

lemma size_tabulate:
  "size (tabulate ks f) = length (remdups ks)"
  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def)

lemma bulkload_tabulate:
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
  by (rule mapping_eqI) (simp add: expand_fun_eq)

lemma keys_tabulate:
  "keys (tabulate ks f) = set ks"
  by (simp add: tabulate_def keys_def map_of_map_restrict o_def)

lemma keys_bulkload:
  "keys (bulkload xs) = {0..<length xs}"
  by (simp add: keys_tabulate bulkload_tabulate)

lemma is_empty_empty:
  "is_empty m \<longleftrightarrow> m = Mapping Map.empty"
  by (cases m) (simp add: is_empty_def)


subsection {* Some technical code lemmas *}

lemma [code]:
  "mapping_case f m = f (Mapping.lookup m)"
  by (cases m) simp

lemma [code]:
  "mapping_rec f m = f (Mapping.lookup m)"
  by (cases m) simp

lemma [code]:
  "Nat.size (m :: (_, _) mapping) = 0"
  by (cases m) simp

lemma [code]:
  "mapping_size f g m = 0"
  by (cases m) simp


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

end