src/HOL/Library/RBT.thy
author bulwahn
Fri Apr 08 16:31:14 2011 +0200 (2011-04-08)
changeset 42316 12635bb655fd
parent 40612 7ae5b89d8913
child 43124 fdb7e1d5f762
permissions -rw-r--r--
deactivating other compilations in quickcheck_exhaustive momentarily that only interesting for my benchmarks and experiments
haftmann@35617
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@35617
     2
haftmann@36147
     3
header {* Abstract type of Red-Black Trees *}
haftmann@35617
     4
haftmann@36147
     5
(*<*)
haftmann@36147
     6
theory RBT
haftmann@36147
     7
imports Main RBT_Impl Mapping
haftmann@35617
     8
begin
haftmann@35617
     9
haftmann@35617
    10
subsection {* Type definition *}
haftmann@35617
    11
haftmann@36147
    12
typedef (open) ('a, 'b) rbt = "{t :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt. is_rbt t}"
haftmann@36147
    13
  morphisms impl_of RBT
haftmann@35617
    14
proof -
haftmann@36147
    15
  have "RBT_Impl.Empty \<in> ?rbt" by simp
haftmann@35617
    16
  then show ?thesis ..
haftmann@35617
    17
qed
haftmann@35617
    18
haftmann@39380
    19
lemma rbt_eq_iff:
haftmann@39380
    20
  "t1 = t2 \<longleftrightarrow> impl_of t1 = impl_of t2"
haftmann@39380
    21
  by (simp add: impl_of_inject)
haftmann@39380
    22
haftmann@39380
    23
lemma rbt_eqI:
haftmann@39380
    24
  "impl_of t1 = impl_of t2 \<Longrightarrow> t1 = t2"
haftmann@39380
    25
  by (simp add: rbt_eq_iff)
haftmann@39380
    26
haftmann@36147
    27
lemma is_rbt_impl_of [simp, intro]:
haftmann@36147
    28
  "is_rbt (impl_of t)"
haftmann@36147
    29
  using impl_of [of t] by simp
haftmann@35617
    30
haftmann@39380
    31
lemma RBT_impl_of [simp, code abstype]:
haftmann@36147
    32
  "RBT (impl_of t) = t"
haftmann@36147
    33
  by (simp add: impl_of_inverse)
haftmann@35617
    34
haftmann@35617
    35
haftmann@35617
    36
subsection {* Primitive operations *}
haftmann@35617
    37
haftmann@36147
    38
definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" where
haftmann@36147
    39
  [code]: "lookup t = RBT_Impl.lookup (impl_of t)"
haftmann@35617
    40
haftmann@36147
    41
definition empty :: "('a\<Colon>linorder, 'b) rbt" where
haftmann@36147
    42
  "empty = RBT RBT_Impl.Empty"
haftmann@35617
    43
haftmann@36147
    44
lemma impl_of_empty [code abstract]:
haftmann@36147
    45
  "impl_of empty = RBT_Impl.Empty"
haftmann@36147
    46
  by (simp add: empty_def RBT_inverse)
haftmann@35617
    47
haftmann@36147
    48
definition insert :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
haftmann@36147
    49
  "insert k v t = RBT (RBT_Impl.insert k v (impl_of t))"
haftmann@35617
    50
haftmann@36147
    51
lemma impl_of_insert [code abstract]:
haftmann@36147
    52
  "impl_of (insert k v t) = RBT_Impl.insert k v (impl_of t)"
haftmann@36147
    53
  by (simp add: insert_def RBT_inverse)
haftmann@35617
    54
haftmann@36147
    55
definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
haftmann@36147
    56
  "delete k t = RBT (RBT_Impl.delete k (impl_of t))"
haftmann@35617
    57
haftmann@36147
    58
lemma impl_of_delete [code abstract]:
haftmann@36147
    59
  "impl_of (delete k t) = RBT_Impl.delete k (impl_of t)"
haftmann@36147
    60
  by (simp add: delete_def RBT_inverse)
haftmann@35617
    61
haftmann@36147
    62
definition entries :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" where
haftmann@36147
    63
  [code]: "entries t = RBT_Impl.entries (impl_of t)"
haftmann@35617
    64
haftmann@36147
    65
definition keys :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list" where
haftmann@36147
    66
  [code]: "keys t = RBT_Impl.keys (impl_of t)"
haftmann@36111
    67
haftmann@36147
    68
definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" where
haftmann@36147
    69
  "bulkload xs = RBT (RBT_Impl.bulkload xs)"
haftmann@35617
    70
haftmann@36147
    71
lemma impl_of_bulkload [code abstract]:
haftmann@36147
    72
  "impl_of (bulkload xs) = RBT_Impl.bulkload xs"
haftmann@36147
    73
  by (simp add: bulkload_def RBT_inverse)
haftmann@35617
    74
haftmann@36147
    75
definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
haftmann@36147
    76
  "map_entry k f t = RBT (RBT_Impl.map_entry k f (impl_of t))"
haftmann@35617
    77
haftmann@36147
    78
lemma impl_of_map_entry [code abstract]:
haftmann@36147
    79
  "impl_of (map_entry k f t) = RBT_Impl.map_entry k f (impl_of t)"
haftmann@36147
    80
  by (simp add: map_entry_def RBT_inverse)
haftmann@35617
    81
haftmann@36147
    82
definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
haftmann@36147
    83
  "map f t = RBT (RBT_Impl.map f (impl_of t))"
haftmann@35617
    84
haftmann@36147
    85
lemma impl_of_map [code abstract]:
haftmann@36147
    86
  "impl_of (map f t) = RBT_Impl.map f (impl_of t)"
haftmann@36147
    87
  by (simp add: map_def RBT_inverse)
haftmann@35617
    88
haftmann@36147
    89
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
haftmann@36147
    90
  [code]: "fold f t = RBT_Impl.fold f (impl_of t)"
haftmann@35617
    91
haftmann@35617
    92
haftmann@35617
    93
subsection {* Derived operations *}
haftmann@35617
    94
haftmann@36147
    95
definition is_empty :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> bool" where
haftmann@36147
    96
  [code]: "is_empty t = (case impl_of t of RBT_Impl.Empty \<Rightarrow> True | _ \<Rightarrow> False)"
haftmann@35617
    97
haftmann@35617
    98
haftmann@35617
    99
subsection {* Abstract lookup properties *}
haftmann@35617
   100
haftmann@36147
   101
lemma lookup_RBT:
haftmann@36147
   102
  "is_rbt t \<Longrightarrow> lookup (RBT t) = RBT_Impl.lookup t"
haftmann@36147
   103
  by (simp add: lookup_def RBT_inverse)
haftmann@35617
   104
haftmann@36147
   105
lemma lookup_impl_of:
haftmann@36147
   106
  "RBT_Impl.lookup (impl_of t) = lookup t"
haftmann@35617
   107
  by (simp add: lookup_def)
haftmann@35617
   108
haftmann@36147
   109
lemma entries_impl_of:
haftmann@36147
   110
  "RBT_Impl.entries (impl_of t) = entries t"
haftmann@35617
   111
  by (simp add: entries_def)
haftmann@35617
   112
haftmann@36147
   113
lemma keys_impl_of:
haftmann@36147
   114
  "RBT_Impl.keys (impl_of t) = keys t"
haftmann@36111
   115
  by (simp add: keys_def)
haftmann@36111
   116
haftmann@35617
   117
lemma lookup_empty [simp]:
haftmann@35617
   118
  "lookup empty = Map.empty"
nipkow@39302
   119
  by (simp add: empty_def lookup_RBT fun_eq_iff)
haftmann@35617
   120
haftmann@36147
   121
lemma lookup_insert [simp]:
haftmann@36147
   122
  "lookup (insert k v t) = (lookup t)(k \<mapsto> v)"
haftmann@36147
   123
  by (simp add: insert_def lookup_RBT lookup_insert lookup_impl_of)
haftmann@35617
   124
haftmann@35617
   125
lemma lookup_delete [simp]:
haftmann@35617
   126
  "lookup (delete k t) = (lookup t)(k := None)"
haftmann@36147
   127
  by (simp add: delete_def lookup_RBT RBT_Impl.lookup_delete lookup_impl_of restrict_complement_singleton_eq)
haftmann@35617
   128
haftmann@35617
   129
lemma map_of_entries [simp]:
haftmann@35617
   130
  "map_of (entries t) = lookup t"
haftmann@36147
   131
  by (simp add: entries_def map_of_entries lookup_impl_of)
haftmann@35617
   132
haftmann@36111
   133
lemma entries_lookup:
haftmann@36111
   134
  "entries t1 = entries t2 \<longleftrightarrow> lookup t1 = lookup t2"
haftmann@36111
   135
  by (simp add: entries_def lookup_def entries_lookup)
haftmann@36111
   136
haftmann@35617
   137
lemma lookup_bulkload [simp]:
haftmann@35617
   138
  "lookup (bulkload xs) = map_of xs"
haftmann@36147
   139
  by (simp add: bulkload_def lookup_RBT RBT_Impl.lookup_bulkload)
haftmann@35617
   140
haftmann@35617
   141
lemma lookup_map_entry [simp]:
haftmann@35617
   142
  "lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))"
haftmann@37027
   143
  by (simp add: map_entry_def lookup_RBT RBT_Impl.lookup_map_entry lookup_impl_of)
haftmann@35617
   144
haftmann@35617
   145
lemma lookup_map [simp]:
haftmann@35617
   146
  "lookup (map f t) k = Option.map (f k) (lookup t k)"
haftmann@40612
   147
  by (simp add: map_def lookup_RBT RBT_Impl.lookup_map lookup_impl_of)
haftmann@35617
   148
haftmann@35617
   149
lemma fold_fold:
haftmann@37462
   150
  "fold f t = More_List.fold (prod_case f) (entries t)"
nipkow@39302
   151
  by (simp add: fold_def fun_eq_iff RBT_Impl.fold_def entries_impl_of)
haftmann@35617
   152
haftmann@36111
   153
lemma is_empty_empty [simp]:
haftmann@36111
   154
  "is_empty t \<longleftrightarrow> t = empty"
haftmann@39380
   155
  by (simp add: rbt_eq_iff is_empty_def impl_of_empty split: rbt.split)
haftmann@36111
   156
haftmann@36111
   157
lemma RBT_lookup_empty [simp]: (*FIXME*)
haftmann@36147
   158
  "RBT_Impl.lookup t = Map.empty \<longleftrightarrow> t = RBT_Impl.Empty"
nipkow@39302
   159
  by (cases t) (auto simp add: fun_eq_iff)
haftmann@36111
   160
haftmann@36111
   161
lemma lookup_empty_empty [simp]:
haftmann@36111
   162
  "lookup t = Map.empty \<longleftrightarrow> t = empty"
haftmann@36147
   163
  by (cases t) (simp add: empty_def lookup_def RBT_inject RBT_inverse)
haftmann@36111
   164
haftmann@36111
   165
lemma sorted_keys [iff]:
haftmann@36111
   166
  "sorted (keys t)"
haftmann@36147
   167
  by (simp add: keys_def RBT_Impl.keys_def sorted_entries)
haftmann@36111
   168
haftmann@36111
   169
lemma distinct_keys [iff]:
haftmann@36111
   170
  "distinct (keys t)"
haftmann@36147
   171
  by (simp add: keys_def RBT_Impl.keys_def distinct_entries)
haftmann@36111
   172
haftmann@36111
   173
haftmann@36111
   174
subsection {* Implementation of mappings *}
haftmann@36111
   175
haftmann@36147
   176
definition Mapping :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) mapping" where
haftmann@36111
   177
  "Mapping t = Mapping.Mapping (lookup t)"
haftmann@36111
   178
haftmann@36111
   179
code_datatype Mapping
haftmann@36111
   180
haftmann@36111
   181
lemma lookup_Mapping [simp, code]:
haftmann@36111
   182
  "Mapping.lookup (Mapping t) = lookup t"
haftmann@36111
   183
  by (simp add: Mapping_def)
haftmann@36111
   184
haftmann@36111
   185
lemma empty_Mapping [code]:
haftmann@36111
   186
  "Mapping.empty = Mapping empty"
haftmann@36111
   187
  by (rule mapping_eqI) simp
haftmann@36111
   188
haftmann@36111
   189
lemma is_empty_Mapping [code]:
haftmann@36111
   190
  "Mapping.is_empty (Mapping t) \<longleftrightarrow> is_empty t"
haftmann@39380
   191
  by (simp add: rbt_eq_iff Mapping.is_empty_empty Mapping_def)
haftmann@36111
   192
haftmann@36147
   193
lemma insert_Mapping [code]:
haftmann@36147
   194
  "Mapping.update k v (Mapping t) = Mapping (insert k v t)"
haftmann@36111
   195
  by (rule mapping_eqI) simp
haftmann@36111
   196
haftmann@36111
   197
lemma delete_Mapping [code]:
haftmann@37027
   198
  "Mapping.delete k (Mapping t) = Mapping (delete k t)"
haftmann@37027
   199
  by (rule mapping_eqI) simp
haftmann@37027
   200
haftmann@37027
   201
lemma map_entry_Mapping [code]:
haftmann@37027
   202
  "Mapping.map_entry k f (Mapping t) = Mapping (map_entry k f t)"
haftmann@36111
   203
  by (rule mapping_eqI) simp
haftmann@36111
   204
haftmann@36111
   205
lemma keys_Mapping [code]:
haftmann@36111
   206
  "Mapping.keys (Mapping t) = set (keys t)"
haftmann@36111
   207
  by (simp add: keys_def Mapping_def Mapping.keys_def lookup_def lookup_keys)
haftmann@36111
   208
haftmann@36111
   209
lemma ordered_keys_Mapping [code]:
haftmann@36111
   210
  "Mapping.ordered_keys (Mapping t) = keys t"
haftmann@36111
   211
  by (rule sorted_distinct_set_unique) (simp_all add: ordered_keys_def keys_Mapping)
haftmann@36111
   212
haftmann@36111
   213
lemma Mapping_size_card_keys: (*FIXME*)
haftmann@36111
   214
  "Mapping.size m = card (Mapping.keys m)"
haftmann@36111
   215
  by (simp add: Mapping.size_def Mapping.keys_def)
haftmann@36111
   216
haftmann@36111
   217
lemma size_Mapping [code]:
haftmann@36111
   218
  "Mapping.size (Mapping t) = length (keys t)"
haftmann@36111
   219
  by (simp add: Mapping_size_card_keys keys_Mapping distinct_card)
haftmann@36111
   220
haftmann@36111
   221
lemma tabulate_Mapping [code]:
haftmann@36111
   222
  "Mapping.tabulate ks f = Mapping (bulkload (List.map (\<lambda>k. (k, f k)) ks))"
haftmann@36111
   223
  by (rule mapping_eqI) (simp add: map_of_map_restrict)
haftmann@36111
   224
haftmann@36111
   225
lemma bulkload_Mapping [code]:
haftmann@36111
   226
  "Mapping.bulkload vs = Mapping (bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
nipkow@39302
   227
  by (rule mapping_eqI) (simp add: map_of_map_restrict fun_eq_iff)
haftmann@36111
   228
haftmann@38857
   229
lemma equal_Mapping [code]:
haftmann@38857
   230
  "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> entries t1 = entries t2"
haftmann@38857
   231
  by (simp add: equal Mapping_def entries_lookup)
haftmann@36111
   232
haftmann@38857
   233
lemma [code nbe]:
haftmann@38857
   234
  "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
haftmann@38857
   235
  by (fact equal_refl)
haftmann@38857
   236
haftmann@36111
   237
wenzelm@36176
   238
hide_const (open) impl_of lookup empty insert delete
haftmann@36111
   239
  entries keys bulkload map_entry map fold
haftmann@36147
   240
(*>*)
haftmann@36147
   241
haftmann@36147
   242
text {* 
haftmann@36147
   243
  This theory defines abstract red-black trees as an efficient
haftmann@36147
   244
  representation of finite maps, backed by the implementation
haftmann@36147
   245
  in @{theory RBT_Impl}.
haftmann@36147
   246
*}
haftmann@36147
   247
haftmann@36147
   248
subsection {* Data type and invariant *}
haftmann@36147
   249
haftmann@36147
   250
text {*
haftmann@36147
   251
  The type @{typ "('k, 'v) RBT_Impl.rbt"} denotes red-black trees with
haftmann@36147
   252
  keys of type @{typ "'k"} and values of type @{typ "'v"}. To function
haftmann@36147
   253
  properly, the key type musorted belong to the @{text "linorder"}
haftmann@36147
   254
  class.
haftmann@36147
   255
haftmann@36147
   256
  A value @{term t} of this type is a valid red-black tree if it
haftmann@36147
   257
  satisfies the invariant @{text "is_rbt t"}.  The abstract type @{typ
haftmann@36147
   258
  "('k, 'v) rbt"} always obeys this invariant, and for this reason you
haftmann@36147
   259
  should only use this in our application.  Going back to @{typ "('k,
haftmann@36147
   260
  'v) RBT_Impl.rbt"} may be necessary in proofs if not yet proven
haftmann@36147
   261
  properties about the operations must be established.
haftmann@36147
   262
haftmann@36147
   263
  The interpretation function @{const "RBT.lookup"} returns the partial
haftmann@36147
   264
  map represented by a red-black tree:
haftmann@36147
   265
  @{term_type[display] "RBT.lookup"}
haftmann@36147
   266
haftmann@36147
   267
  This function should be used for reasoning about the semantics of the RBT
haftmann@36147
   268
  operations. Furthermore, it implements the lookup functionality for
haftmann@36147
   269
  the data structure: It is executable and the lookup is performed in
haftmann@36147
   270
  $O(\log n)$.  
haftmann@36147
   271
*}
haftmann@36147
   272
haftmann@36147
   273
subsection {* Operations *}
haftmann@36147
   274
haftmann@36147
   275
text {*
haftmann@36147
   276
  Currently, the following operations are supported:
haftmann@36147
   277
haftmann@36147
   278
  @{term_type [display] "RBT.empty"}
haftmann@36147
   279
  Returns the empty tree. $O(1)$
haftmann@36147
   280
haftmann@36147
   281
  @{term_type [display] "RBT.insert"}
haftmann@36147
   282
  Updates the map at a given position. $O(\log n)$
haftmann@36147
   283
haftmann@36147
   284
  @{term_type [display] "RBT.delete"}
haftmann@36147
   285
  Deletes a map entry at a given position. $O(\log n)$
haftmann@36147
   286
haftmann@36147
   287
  @{term_type [display] "RBT.entries"}
haftmann@36147
   288
  Return a corresponding key-value list for a tree.
haftmann@36147
   289
haftmann@36147
   290
  @{term_type [display] "RBT.bulkload"}
haftmann@36147
   291
  Builds a tree from a key-value list.
haftmann@36147
   292
haftmann@36147
   293
  @{term_type [display] "RBT.map_entry"}
haftmann@36147
   294
  Maps a single entry in a tree.
haftmann@36147
   295
haftmann@36147
   296
  @{term_type [display] "RBT.map"}
haftmann@36147
   297
  Maps all values in a tree. $O(n)$
haftmann@36147
   298
haftmann@36147
   299
  @{term_type [display] "RBT.fold"}
haftmann@36147
   300
  Folds over all entries in a tree. $O(n)$
haftmann@36147
   301
*}
haftmann@36147
   302
haftmann@36147
   303
haftmann@36147
   304
subsection {* Invariant preservation *}
haftmann@36147
   305
haftmann@36147
   306
text {*
haftmann@36147
   307
  \noindent
haftmann@36147
   308
  @{thm Empty_is_rbt}\hfill(@{text "Empty_is_rbt"})
haftmann@36147
   309
haftmann@36147
   310
  \noindent
haftmann@36147
   311
  @{thm insert_is_rbt}\hfill(@{text "insert_is_rbt"})
haftmann@36147
   312
haftmann@36147
   313
  \noindent
haftmann@36147
   314
  @{thm delete_is_rbt}\hfill(@{text "delete_is_rbt"})
haftmann@36147
   315
haftmann@36147
   316
  \noindent
haftmann@36147
   317
  @{thm bulkload_is_rbt}\hfill(@{text "bulkload_is_rbt"})
haftmann@36147
   318
haftmann@36147
   319
  \noindent
haftmann@36147
   320
  @{thm map_entry_is_rbt}\hfill(@{text "map_entry_is_rbt"})
haftmann@36147
   321
haftmann@36147
   322
  \noindent
haftmann@36147
   323
  @{thm map_is_rbt}\hfill(@{text "map_is_rbt"})
haftmann@36147
   324
haftmann@36147
   325
  \noindent
haftmann@36147
   326
  @{thm union_is_rbt}\hfill(@{text "union_is_rbt"})
haftmann@36147
   327
*}
haftmann@36147
   328
haftmann@36147
   329
haftmann@36147
   330
subsection {* Map Semantics *}
haftmann@36147
   331
haftmann@36147
   332
text {*
haftmann@36147
   333
  \noindent
haftmann@36147
   334
  \underline{@{text "lookup_empty"}}
haftmann@36147
   335
  @{thm [display] lookup_empty}
haftmann@36147
   336
  \vspace{1ex}
haftmann@36147
   337
haftmann@36147
   338
  \noindent
haftmann@36147
   339
  \underline{@{text "lookup_insert"}}
haftmann@36147
   340
  @{thm [display] lookup_insert}
haftmann@36147
   341
  \vspace{1ex}
haftmann@36147
   342
haftmann@36147
   343
  \noindent
haftmann@36147
   344
  \underline{@{text "lookup_delete"}}
haftmann@36147
   345
  @{thm [display] lookup_delete}
haftmann@36147
   346
  \vspace{1ex}
haftmann@36147
   347
haftmann@36147
   348
  \noindent
haftmann@36147
   349
  \underline{@{text "lookup_bulkload"}}
haftmann@36147
   350
  @{thm [display] lookup_bulkload}
haftmann@36147
   351
  \vspace{1ex}
haftmann@36147
   352
haftmann@36147
   353
  \noindent
haftmann@36147
   354
  \underline{@{text "lookup_map"}}
haftmann@36147
   355
  @{thm [display] lookup_map}
haftmann@36147
   356
  \vspace{1ex}
haftmann@36147
   357
*}
haftmann@35617
   358
haftmann@35617
   359
end