src/HOL/Library/RBT_Mapping.thy
author wenzelm
Sun Nov 02 17:20:45 2014 +0100 (2014-11-02)
changeset 58881 b9556a055632
parent 56019 682bba24e474
child 60373 68eb60fd22a6
permissions -rw-r--r--
modernized header;
     1 (*  Title:      HOL/Library/RBT_Mapping.thy
     2     Author:     Florian Haftmann and Ondrej Kuncar
     3 *)
     4 
     5 section {* Implementation of mappings with Red-Black Trees *}
     6 
     7 (*<*)
     8 theory RBT_Mapping
     9 imports RBT Mapping
    10 begin
    11 
    12 subsection {* Implementation of mappings *}
    13 
    14 context includes rbt.lifting begin
    15 lift_definition Mapping :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) mapping" is RBT.lookup .
    16 end
    17 
    18 code_datatype Mapping
    19 
    20 context includes rbt.lifting begin
    21 
    22 lemma lookup_Mapping [simp, code]:
    23   "Mapping.lookup (Mapping t) = RBT.lookup t"
    24    by (transfer fixing: t) rule
    25 
    26 lemma empty_Mapping [code]: "Mapping.empty = Mapping RBT.empty"
    27 proof -
    28   note RBT.empty.transfer[transfer_rule del]
    29   show ?thesis by transfer simp
    30 qed
    31 
    32 lemma is_empty_Mapping [code]:
    33   "Mapping.is_empty (Mapping t) \<longleftrightarrow> RBT.is_empty t"
    34   unfolding is_empty_def by (transfer fixing: t) simp
    35 
    36 lemma insert_Mapping [code]:
    37   "Mapping.update k v (Mapping t) = Mapping (RBT.insert k v t)"
    38   by (transfer fixing: t) simp
    39 
    40 lemma delete_Mapping [code]:
    41   "Mapping.delete k (Mapping t) = Mapping (RBT.delete k t)"
    42   by (transfer fixing: t) simp
    43 
    44 lemma map_entry_Mapping [code]:
    45   "Mapping.map_entry k f (Mapping t) = Mapping (RBT.map_entry k f t)"
    46   apply (transfer fixing: t) by (case_tac "RBT.lookup t k") auto
    47 
    48 lemma keys_Mapping [code]:
    49   "Mapping.keys (Mapping t) = set (RBT.keys t)"
    50 by (transfer fixing: t) (simp add: lookup_keys)
    51 
    52 lemma ordered_keys_Mapping [code]:
    53   "Mapping.ordered_keys (Mapping t) = RBT.keys t"
    54 unfolding ordered_keys_def 
    55 by (transfer fixing: t) (auto simp add: lookup_keys intro: sorted_distinct_set_unique)
    56 
    57 lemma Mapping_size_card_keys: (*FIXME*)
    58   "Mapping.size m = card (Mapping.keys m)"
    59 unfolding size_def by transfer simp
    60 
    61 lemma size_Mapping [code]:
    62   "Mapping.size (Mapping t) = length (RBT.keys t)"
    63 unfolding size_def
    64 by (transfer fixing: t) (simp add: lookup_keys distinct_card)
    65 
    66 context
    67   notes RBT.bulkload.transfer[transfer_rule del]
    68 begin
    69   lemma tabulate_Mapping [code]:
    70     "Mapping.tabulate ks f = Mapping (RBT.bulkload (List.map (\<lambda>k. (k, f k)) ks))"
    71   by transfer (simp add: map_of_map_restrict)
    72   
    73   lemma bulkload_Mapping [code]:
    74     "Mapping.bulkload vs = Mapping (RBT.bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
    75   by transfer (simp add: map_of_map_restrict fun_eq_iff)
    76 end
    77 
    78 lemma equal_Mapping [code]:
    79   "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> RBT.entries t1 = RBT.entries t2"
    80   by (transfer fixing: t1 t2) (simp add: entries_lookup)
    81 
    82 lemma [code nbe]:
    83   "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
    84   by (fact equal_refl)
    85 
    86 end
    87 
    88 (*>*)
    89 
    90 text {* 
    91   This theory defines abstract red-black trees as an efficient
    92   representation of finite maps, backed by the implementation
    93   in @{theory RBT_Impl}.
    94 *}
    95 
    96 subsection {* Data type and invariant *}
    97 
    98 text {*
    99   The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with
   100   keys of type @{typ "'k"} and values of type @{typ "'v"}. To function
   101   properly, the key type musorted belong to the @{text "linorder"}
   102   class.
   103 
   104   A value @{term t} of this type is a valid red-black tree if it
   105   satisfies the invariant @{text "is_rbt t"}.  The abstract type @{typ
   106   "('k, 'v) rbt"} always obeys this invariant, and for this reason you
   107   should only use this in our application.  Going back to @{typ "('k,
   108   'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven
   109   properties about the operations must be established.
   110 
   111   The interpretation function @{const "RBT.lookup"} returns the partial
   112   map represented by a red-black tree:
   113   @{term_type[display] "RBT.lookup"}
   114 
   115   This function should be used for reasoning about the semantics of the RBT
   116   operations. Furthermore, it implements the lookup functionality for
   117   the data structure: It is executable and the lookup is performed in
   118   $O(\log n)$.  
   119 *}
   120 
   121 subsection {* Operations *}
   122 
   123 text {*
   124   Currently, the following operations are supported:
   125 
   126   @{term_type [display] "RBT.empty"}
   127   Returns the empty tree. $O(1)$
   128 
   129   @{term_type [display] "RBT.insert"}
   130   Updates the map at a given position. $O(\log n)$
   131 
   132   @{term_type [display] "RBT.delete"}
   133   Deletes a map entry at a given position. $O(\log n)$
   134 
   135   @{term_type [display] "RBT.entries"}
   136   Return a corresponding key-value list for a tree.
   137 
   138   @{term_type [display] "RBT.bulkload"}
   139   Builds a tree from a key-value list.
   140 
   141   @{term_type [display] "RBT.map_entry"}
   142   Maps a single entry in a tree.
   143 
   144   @{term_type [display] "RBT.map"}
   145   Maps all values in a tree. $O(n)$
   146 
   147   @{term_type [display] "RBT.fold"}
   148   Folds over all entries in a tree. $O(n)$
   149 *}
   150 
   151 
   152 subsection {* Invariant preservation *}
   153 
   154 text {*
   155   \noindent
   156   @{thm Empty_is_rbt}\hfill(@{text "Empty_is_rbt"})
   157 
   158   \noindent
   159   @{thm rbt_insert_is_rbt}\hfill(@{text "rbt_insert_is_rbt"})
   160 
   161   \noindent
   162   @{thm rbt_delete_is_rbt}\hfill(@{text "delete_is_rbt"})
   163 
   164   \noindent
   165   @{thm rbt_bulkload_is_rbt}\hfill(@{text "bulkload_is_rbt"})
   166 
   167   \noindent
   168   @{thm rbt_map_entry_is_rbt}\hfill(@{text "map_entry_is_rbt"})
   169 
   170   \noindent
   171   @{thm map_is_rbt}\hfill(@{text "map_is_rbt"})
   172 
   173   \noindent
   174   @{thm rbt_union_is_rbt}\hfill(@{text "union_is_rbt"})
   175 *}
   176 
   177 
   178 subsection {* Map Semantics *}
   179 
   180 text {*
   181   \noindent
   182   \underline{@{text "lookup_empty"}}
   183   @{thm [display] lookup_empty}
   184   \vspace{1ex}
   185 
   186   \noindent
   187   \underline{@{text "lookup_insert"}}
   188   @{thm [display] lookup_insert}
   189   \vspace{1ex}
   190 
   191   \noindent
   192   \underline{@{text "lookup_delete"}}
   193   @{thm [display] lookup_delete}
   194   \vspace{1ex}
   195 
   196   \noindent
   197   \underline{@{text "lookup_bulkload"}}
   198   @{thm [display] lookup_bulkload}
   199   \vspace{1ex}
   200 
   201   \noindent
   202   \underline{@{text "lookup_map"}}
   203   @{thm [display] lookup_map}
   204   \vspace{1ex}
   205 *}
   206 
   207 end