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