src/HOL/Library/RBT_Mapping.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 61585 a9599d3d7610
child 62390 842917225d56
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
     1 (*  Title:      HOL/Library/RBT_Mapping.thy
     2     Author:     Florian Haftmann and Ondrej Kuncar
     3 *)
     4 
     5 section \<open>Implementation of mappings with Red-Black Trees\<close>
     6 
     7 (*<*)
     8 theory RBT_Mapping
     9 imports RBT Mapping
    10 begin
    11 
    12 subsection \<open>Implementation of mappings\<close>
    13 
    14 context includes rbt.lifting begin
    15 lift_definition Mapping :: "('a::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 
    70 lemma tabulate_Mapping [code]:
    71   "Mapping.tabulate ks f = Mapping (RBT.bulkload (List.map (\<lambda>k. (k, f k)) ks))"
    72 by transfer (simp add: map_of_map_restrict)
    73 
    74 lemma bulkload_Mapping [code]:
    75   "Mapping.bulkload vs = Mapping (RBT.bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
    76 by transfer (simp add: map_of_map_restrict fun_eq_iff)
    77 
    78 end
    79 
    80 lemma equal_Mapping [code]:
    81   "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> RBT.entries t1 = RBT.entries t2"
    82   by (transfer fixing: t1 t2) (simp add: entries_lookup)
    83 
    84 lemma [code nbe]:
    85   "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
    86   by (fact equal_refl)
    87 
    88 end
    89 
    90 (*>*)
    91 
    92 text \<open>
    93   This theory defines abstract red-black trees as an efficient
    94   representation of finite maps, backed by the implementation
    95   in @{theory RBT_Impl}.
    96 \<close>
    97 
    98 subsection \<open>Data type and invariant\<close>
    99 
   100 text \<open>
   101   The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with
   102   keys of type @{typ "'k"} and values of type @{typ "'v"}. To function
   103   properly, the key type musorted belong to the \<open>linorder\<close>
   104   class.
   105 
   106   A value @{term t} of this type is a valid red-black tree if it
   107   satisfies the invariant \<open>is_rbt t\<close>.  The abstract type @{typ
   108   "('k, 'v) rbt"} always obeys this invariant, and for this reason you
   109   should only use this in our application.  Going back to @{typ "('k,
   110   'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven
   111   properties about the operations must be established.
   112 
   113   The interpretation function @{const "RBT.lookup"} returns the partial
   114   map represented by a red-black tree:
   115   @{term_type[display] "RBT.lookup"}
   116 
   117   This function should be used for reasoning about the semantics of the RBT
   118   operations. Furthermore, it implements the lookup functionality for
   119   the data structure: It is executable and the lookup is performed in
   120   $O(\log n)$.  
   121 \<close>
   122 
   123 subsection \<open>Operations\<close>
   124 
   125 text \<open>
   126   Currently, the following operations are supported:
   127 
   128   @{term_type [display] "RBT.empty"}
   129   Returns the empty tree. $O(1)$
   130 
   131   @{term_type [display] "RBT.insert"}
   132   Updates the map at a given position. $O(\log n)$
   133 
   134   @{term_type [display] "RBT.delete"}
   135   Deletes a map entry at a given position. $O(\log n)$
   136 
   137   @{term_type [display] "RBT.entries"}
   138   Return a corresponding key-value list for a tree.
   139 
   140   @{term_type [display] "RBT.bulkload"}
   141   Builds a tree from a key-value list.
   142 
   143   @{term_type [display] "RBT.map_entry"}
   144   Maps a single entry in a tree.
   145 
   146   @{term_type [display] "RBT.map"}
   147   Maps all values in a tree. $O(n)$
   148 
   149   @{term_type [display] "RBT.fold"}
   150   Folds over all entries in a tree. $O(n)$
   151 \<close>
   152 
   153 
   154 subsection \<open>Invariant preservation\<close>
   155 
   156 text \<open>
   157   \noindent
   158   @{thm Empty_is_rbt}\hfill(\<open>Empty_is_rbt\<close>)
   159 
   160   \noindent
   161   @{thm rbt_insert_is_rbt}\hfill(\<open>rbt_insert_is_rbt\<close>)
   162 
   163   \noindent
   164   @{thm rbt_delete_is_rbt}\hfill(\<open>delete_is_rbt\<close>)
   165 
   166   \noindent
   167   @{thm rbt_bulkload_is_rbt}\hfill(\<open>bulkload_is_rbt\<close>)
   168 
   169   \noindent
   170   @{thm rbt_map_entry_is_rbt}\hfill(\<open>map_entry_is_rbt\<close>)
   171 
   172   \noindent
   173   @{thm map_is_rbt}\hfill(\<open>map_is_rbt\<close>)
   174 
   175   \noindent
   176   @{thm rbt_union_is_rbt}\hfill(\<open>union_is_rbt\<close>)
   177 \<close>
   178 
   179 
   180 subsection \<open>Map Semantics\<close>
   181 
   182 text \<open>
   183   \noindent
   184   \underline{\<open>lookup_empty\<close>}
   185   @{thm [display] lookup_empty}
   186   \vspace{1ex}
   187 
   188   \noindent
   189   \underline{\<open>lookup_insert\<close>}
   190   @{thm [display] lookup_insert}
   191   \vspace{1ex}
   192 
   193   \noindent
   194   \underline{\<open>lookup_delete\<close>}
   195   @{thm [display] lookup_delete}
   196   \vspace{1ex}
   197 
   198   \noindent
   199   \underline{\<open>lookup_bulkload\<close>}
   200   @{thm [display] lookup_bulkload}
   201   \vspace{1ex}
   202 
   203   \noindent
   204   \underline{\<open>lookup_map\<close>}
   205   @{thm [display] lookup_map}
   206   \vspace{1ex}
   207 \<close>
   208 
   209 end