src/HOL/Library/RBT.thy
author bulwahn
Wed Jun 01 09:10:13 2011 +0200 (2011-06-01)
changeset 43124 fdb7e1d5f762
parent 40612 7ae5b89d8913
child 45694 4a8743618257
permissions -rw-r--r--
splitting RBT theory into RBT and RBT_Mapping
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
bulwahn@43124
     7
imports Main RBT_Impl
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@35617
   174
end