
bulwahn@43124: (* Author: Florian Haftmann, TU Muenchen *)
bulwahn@43124: 
bulwahn@43124: header {* Implementation of mappings with Red-Black Trees *}
bulwahn@43124: 
bulwahn@43124: (*<*)
bulwahn@43124: theory RBT_Mapping
bulwahn@43124: imports RBT Mapping
bulwahn@43124: begin
bulwahn@43124: 
bulwahn@43124: subsection {* Implementation of mappings *}
bulwahn@43124: 
bulwahn@43124: definition Mapping :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) mapping" where
bulwahn@43124:   "Mapping t = Mapping.Mapping (lookup t)"
bulwahn@43124: 
bulwahn@43124: code_datatype Mapping
bulwahn@43124: 
bulwahn@43124: lemma lookup_Mapping [simp, code]:
bulwahn@43124:   "Mapping.lookup (Mapping t) = lookup t"
bulwahn@43124:   by (simp add: Mapping_def)
bulwahn@43124: 
bulwahn@43124: lemma empty_Mapping [code]:
bulwahn@43124:   "Mapping.empty = Mapping empty"
bulwahn@43124:   by (rule mapping_eqI) simp
bulwahn@43124: 
bulwahn@43124: lemma is_empty_Mapping [code]:
bulwahn@43124:   "Mapping.is_empty (Mapping t) \<longleftrightarrow> is_empty t"
bulwahn@43124:   by (simp add: rbt_eq_iff Mapping.is_empty_empty Mapping_def)
bulwahn@43124: 
bulwahn@43124: lemma insert_Mapping [code]:
bulwahn@43124:   "Mapping.update k v (Mapping t) = Mapping (insert k v t)"
bulwahn@43124:   by (rule mapping_eqI) simp
bulwahn@43124: 
bulwahn@43124: lemma delete_Mapping [code]:
bulwahn@43124:   "Mapping.delete k (Mapping t) = Mapping (delete k t)"
bulwahn@43124:   by (rule mapping_eqI) simp
bulwahn@43124: 
bulwahn@43124: lemma map_entry_Mapping [code]:
bulwahn@43124:   "Mapping.map_entry k f (Mapping t) = Mapping (map_entry k f t)"
bulwahn@43124:   by (rule mapping_eqI) simp
bulwahn@43124: 
bulwahn@43124: lemma keys_Mapping [code]:
bulwahn@43124:   "Mapping.keys (Mapping t) = set (keys t)"
Andreas@47450:   by (simp add: RBT.keys_def Mapping_def Mapping.keys_def lookup_def rbt_lookup_keys)
bulwahn@43124: 
bulwahn@43124: lemma ordered_keys_Mapping [code]:
bulwahn@43124:   "Mapping.ordered_keys (Mapping t) = keys t"
bulwahn@43124:   by (rule sorted_distinct_set_unique) (simp_all add: ordered_keys_def keys_Mapping)
bulwahn@43124: 
bulwahn@43124: lemma Mapping_size_card_keys: (*FIXME*)
bulwahn@43124:   "Mapping.size m = card (Mapping.keys m)"
bulwahn@43124:   by (simp add: Mapping.size_def Mapping.keys_def)
bulwahn@43124: 
bulwahn@43124: lemma size_Mapping [code]:
bulwahn@43124:   "Mapping.size (Mapping t) = length (keys t)"
bulwahn@43124:   by (simp add: Mapping_size_card_keys keys_Mapping distinct_card)
bulwahn@43124: 
bulwahn@43124: lemma tabulate_Mapping [code]:
bulwahn@43124:   "Mapping.tabulate ks f = Mapping (bulkload (List.map (\<lambda>k. (k, f k)) ks))"
bulwahn@43124:   by (rule mapping_eqI) (simp add: map_of_map_restrict)
bulwahn@43124: 
bulwahn@43124: lemma bulkload_Mapping [code]:
bulwahn@43124:   "Mapping.bulkload vs = Mapping (bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
bulwahn@43124:   by (rule mapping_eqI) (simp add: map_of_map_restrict fun_eq_iff)
bulwahn@43124: 
bulwahn@43124: lemma equal_Mapping [code]:
bulwahn@43124:   "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> entries t1 = entries t2"
bulwahn@43124:   by (simp add: equal Mapping_def entries_lookup)
bulwahn@43124: 
bulwahn@43124: lemma [code nbe]:
bulwahn@43124:   "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
bulwahn@43124:   by (fact equal_refl)
bulwahn@43124: 
bulwahn@43124: 
bulwahn@43124: hide_const (open) impl_of lookup empty insert delete
bulwahn@43124:   entries keys bulkload map_entry map fold
bulwahn@43124: (*>*)
bulwahn@43124: 
bulwahn@43124: text {* 
bulwahn@43124:   This theory defines abstract red-black trees as an efficient
bulwahn@43124:   representation of finite maps, backed by the implementation
bulwahn@43124:   in @{theory RBT_Impl}.
bulwahn@43124: *}
bulwahn@43124: 
bulwahn@43124: subsection {* Data type and invariant *}
bulwahn@43124: 
bulwahn@43124: text {*
bulwahn@43124:   The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with
bulwahn@43124:   keys of type @{typ "'k"} and values of type @{typ "'v"}. To function
bulwahn@43124:   properly, the key type musorted belong to the @{text "linorder"}
bulwahn@43124:   class.
bulwahn@43124: 
bulwahn@43124:   A value @{term t} of this type is a valid red-black tree if it
bulwahn@43124:   satisfies the invariant @{text "is_rbt t"}.  The abstract type @{typ
bulwahn@43124:   "('k, 'v) rbt"} always obeys this invariant, and for this reason you
bulwahn@43124:   should only use this in our application.  Going back to @{typ "('k,
bulwahn@43124:   'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven
bulwahn@43124:   properties about the operations must be established.
bulwahn@43124: 
bulwahn@43124:   The interpretation function @{const "RBT.lookup"} returns the partial
bulwahn@43124:   map represented by a red-black tree:
bulwahn@43124:   @{term_type[display] "RBT.lookup"}
bulwahn@43124: 
bulwahn@43124:   This function should be used for reasoning about the semantics of the RBT
bulwahn@43124:   operations. Furthermore, it implements the lookup functionality for
bulwahn@43124:   the data structure: It is executable and the lookup is performed in
bulwahn@43124:   $O(\log n)$.  
bulwahn@43124: *}
bulwahn@43124: 
bulwahn@43124: subsection {* Operations *}
bulwahn@43124: 
bulwahn@43124: text {*
bulwahn@43124:   Currently, the following operations are supported:
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.empty"}
bulwahn@43124:   Returns the empty tree. $O(1)$
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.insert"}
bulwahn@43124:   Updates the map at a given position. $O(\log n)$
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.delete"}
bulwahn@43124:   Deletes a map entry at a given position. $O(\log n)$
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.entries"}
bulwahn@43124:   Return a corresponding key-value list for a tree.
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.bulkload"}
bulwahn@43124:   Builds a tree from a key-value list.
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.map_entry"}
bulwahn@43124:   Maps a single entry in a tree.
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.map"}
bulwahn@43124:   Maps all values in a tree. $O(n)$
bulwahn@43124: 
bulwahn@43124:   @{term_type [display] "RBT.fold"}
bulwahn@43124:   Folds over all entries in a tree. $O(n)$
bulwahn@43124: *}
bulwahn@43124: 
bulwahn@43124: 
bulwahn@43124: subsection {* Invariant preservation *}
bulwahn@43124: 
bulwahn@43124: text {*
bulwahn@43124:   \noindent
bulwahn@43124:   @{thm Empty_is_rbt}\hfill(@{text "Empty_is_rbt"})
bulwahn@43124: 
bulwahn@43124:   \noindent
Andreas@47450:   @{thm rbt_insert_is_rbt}\hfill(@{text "rbt_insert_is_rbt"})
bulwahn@43124: 
bulwahn@43124:   \noindent
Andreas@47450:   @{thm rbt_delete_is_rbt}\hfill(@{text "delete_is_rbt"})
bulwahn@43124: 
bulwahn@43124:   \noindent
Andreas@47450:   @{thm rbt_bulkload_is_rbt}\hfill(@{text "bulkload_is_rbt"})
bulwahn@43124: 
bulwahn@43124:   \noindent
Andreas@47450:   @{thm rbt_map_entry_is_rbt}\hfill(@{text "map_entry_is_rbt"})
bulwahn@43124: 
bulwahn@43124:   \noindent
bulwahn@43124:   @{thm map_is_rbt}\hfill(@{text "map_is_rbt"})
bulwahn@43124: 
bulwahn@43124:   \noindent
Andreas@47450:   @{thm rbt_union_is_rbt}\hfill(@{text "union_is_rbt"})
bulwahn@43124: *}
bulwahn@43124: 
bulwahn@43124: 
bulwahn@43124: subsection {* Map Semantics *}
bulwahn@43124: 
bulwahn@43124: text {*
bulwahn@43124:   \noindent
bulwahn@43124:   \underline{@{text "lookup_empty"}}
bulwahn@43124:   @{thm [display] lookup_empty}
bulwahn@43124:   \vspace{1ex}
bulwahn@43124: 
bulwahn@43124:   \noindent
bulwahn@43124:   \underline{@{text "lookup_insert"}}
bulwahn@43124:   @{thm [display] lookup_insert}
bulwahn@43124:   \vspace{1ex}
bulwahn@43124: 
bulwahn@43124:   \noindent
bulwahn@43124:   \underline{@{text "lookup_delete"}}
bulwahn@43124:   @{thm [display] lookup_delete}
bulwahn@43124:   \vspace{1ex}
bulwahn@43124: 
bulwahn@43124:   \noindent
bulwahn@43124:   \underline{@{text "lookup_bulkload"}}
bulwahn@43124:   @{thm [display] lookup_bulkload}
bulwahn@43124:   \vspace{1ex}
bulwahn@43124: 
bulwahn@43124:   \noindent
bulwahn@43124:   \underline{@{text "lookup_map"}}
bulwahn@43124:   @{thm [display] lookup_map}
bulwahn@43124:   \vspace{1ex}
bulwahn@43124: *}
bulwahn@43124: 
bulwahn@43124: end



