src/HOL/Library/RBT_Mapping.thy
 author haftmann Wed Jul 18 20:51:21 2018 +0200 (15 months ago) changeset 68658 16cc1161ad7f parent 68484 59793df7f853 child 69593 3dda49e08b9d permissions -rw-r--r--
tuned equation
     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)

    47   apply (case_tac "RBT.lookup t k")

    48    apply auto

    49   done

    50

    51 lemma keys_Mapping [code]:

    52   "Mapping.keys (Mapping t) = set (RBT.keys t)"

    53 by (transfer fixing: t) (simp add: lookup_keys)

    54

    55 lemma ordered_keys_Mapping [code]:

    56   "Mapping.ordered_keys (Mapping t) = RBT.keys t"

    57 unfolding ordered_keys_def

    58 by (transfer fixing: t) (auto simp add: lookup_keys intro: sorted_distinct_set_unique)

    59

    60 lemma Mapping_size_card_keys: (*FIXME*)

    61   "Mapping.size m = card (Mapping.keys m)"

    62 unfolding size_def by transfer simp

    63

    64 lemma size_Mapping [code]:

    65   "Mapping.size (Mapping t) = length (RBT.keys t)"

    66 unfolding size_def

    67 by (transfer fixing: t) (simp add: lookup_keys distinct_card)

    68

    69 context

    70   notes RBT.bulkload.transfer[transfer_rule del]

    71 begin

    72

    73 lemma tabulate_Mapping [code]:

    74   "Mapping.tabulate ks f = Mapping (RBT.bulkload (List.map (\<lambda>k. (k, f k)) ks))"

    75 by transfer (simp add: map_of_map_restrict)

    76

    77 lemma bulkload_Mapping [code]:

    78   "Mapping.bulkload vs = Mapping (RBT.bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"

    79 by transfer (simp add: map_of_map_restrict fun_eq_iff)

    80

    81 end

    82

    83 lemma map_values_Mapping [code]:

    84   "Mapping.map_values f (Mapping t) = Mapping (RBT.map f t)"

    85   by (transfer fixing: t) (auto simp: fun_eq_iff)

    86

    87 lemma filter_Mapping [code]:

    88   "Mapping.filter P (Mapping t) = Mapping (RBT.filter P t)"

    89   by (transfer' fixing: P t) (simp add: RBT.lookup_filter fun_eq_iff)

    90

    91 lemma combine_with_key_Mapping [code]:

    92   "Mapping.combine_with_key f (Mapping t1) (Mapping t2) =

    93      Mapping (RBT.combine_with_key f t1 t2)"

    94   by (transfer fixing: f t1 t2) (simp_all add: fun_eq_iff)

    95

    96 lemma combine_Mapping [code]:

    97   "Mapping.combine f (Mapping t1) (Mapping t2) =

    98      Mapping (RBT.combine f t1 t2)"

    99   by (transfer fixing: f t1 t2) (simp_all add: fun_eq_iff)

   100

   101 lemma equal_Mapping [code]:

   102   "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> RBT.entries t1 = RBT.entries t2"

   103   by (transfer fixing: t1 t2) (simp add: entries_lookup)

   104

   105 lemma [code nbe]:

   106   "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"

   107   by (fact equal_refl)

   108

   109 end

   110

   111 (*>*)

   112

   113 text \<open>

   114   This theory defines abstract red-black trees as an efficient

   115   representation of finite maps, backed by the implementation

   116   in @{theory "HOL-Library.RBT_Impl"}.

   117 \<close>

   118

   119 subsection \<open>Data type and invariant\<close>

   120

   121 text \<open>

   122   The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with

   123   keys of type @{typ "'k"} and values of type @{typ "'v"}. To function

   124   properly, the key type musorted belong to the \<open>linorder\<close>

   125   class.

   126

   127   A value @{term t} of this type is a valid red-black tree if it

   128   satisfies the invariant \<open>is_rbt t\<close>.  The abstract type @{typ

   129   "('k, 'v) rbt"} always obeys this invariant, and for this reason you

   130   should only use this in our application.  Going back to @{typ "('k,

   131   'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven

   132   properties about the operations must be established.

   133

   134   The interpretation function @{const "RBT.lookup"} returns the partial

   135   map represented by a red-black tree:

   136   @{term_type[display] "RBT.lookup"}

   137

   138   This function should be used for reasoning about the semantics of the RBT

   139   operations. Furthermore, it implements the lookup functionality for

   140   the data structure: It is executable and the lookup is performed in

   141   $O(\log n)$.

   142 \<close>

   143

   144 subsection \<open>Operations\<close>

   145

   146 text \<open>

   147   Currently, the following operations are supported:

   148

   149   @{term_type [display] "RBT.empty"}

   150   Returns the empty tree. $O(1)$

   151

   152   @{term_type [display] "RBT.insert"}

   153   Updates the map at a given position. $O(\log n)$

   154

   155   @{term_type [display] "RBT.delete"}

   156   Deletes a map entry at a given position. $O(\log n)$

   157

   158   @{term_type [display] "RBT.entries"}

   159   Return a corresponding key-value list for a tree.

   160

   161   @{term_type [display] "RBT.bulkload"}

   162   Builds a tree from a key-value list.

   163

   164   @{term_type [display] "RBT.map_entry"}

   165   Maps a single entry in a tree.

   166

   167   @{term_type [display] "RBT.map"}

   168   Maps all values in a tree. $O(n)$

   169

   170   @{term_type [display] "RBT.fold"}

   171   Folds over all entries in a tree. $O(n)$

   172 \<close>

   173

   174

   175 subsection \<open>Invariant preservation\<close>

   176

   177 text \<open>

   178   \noindent

   179   @{thm Empty_is_rbt}\hfill(\<open>Empty_is_rbt\<close>)

   180

   181   \noindent

   182   @{thm rbt_insert_is_rbt}\hfill(\<open>rbt_insert_is_rbt\<close>)

   183

   184   \noindent

   185   @{thm rbt_delete_is_rbt}\hfill(\<open>delete_is_rbt\<close>)

   186

   187   \noindent

   188   @{thm rbt_bulkload_is_rbt}\hfill(\<open>bulkload_is_rbt\<close>)

   189

   190   \noindent

   191   @{thm rbt_map_entry_is_rbt}\hfill(\<open>map_entry_is_rbt\<close>)

   192

   193   \noindent

   194   @{thm map_is_rbt}\hfill(\<open>map_is_rbt\<close>)

   195

   196   \noindent

   197   @{thm rbt_union_is_rbt}\hfill(\<open>union_is_rbt\<close>)

   198 \<close>

   199

   200

   201 subsection \<open>Map Semantics\<close>

   202

   203 text \<open>

   204   \noindent

   205   \underline{\<open>lookup_empty\<close>}

   206   @{thm [display] lookup_empty}

   207   \vspace{1ex}

   208

   209   \noindent

   210   \underline{\<open>lookup_insert\<close>}

   211   @{thm [display] lookup_insert}

   212   \vspace{1ex}

   213

   214   \noindent

   215   \underline{\<open>lookup_delete\<close>}

   216   @{thm [display] lookup_delete}

   217   \vspace{1ex}

   218

   219   \noindent

   220   \underline{\<open>lookup_bulkload\<close>}

   221   @{thm [display] lookup_bulkload}

   222   \vspace{1ex}

   223

   224   \noindent

   225   \underline{\<open>lookup_map\<close>}

   226   @{thm [display] lookup_map}

   227   \vspace{1ex}

   228 \<close>

   229

   230 end