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