src/HOL/Library/RBT.thy
author wenzelm
Wed Mar 08 10:50:59 2017 +0100 (2017-03-08)
changeset 65151 a7394aa4d21c
parent 63219 a5697f7a3322
permissions -rw-r--r--
tuned proofs;
kuncar@48622
     1
(*  Title:      HOL/Library/RBT.thy
kuncar@48622
     2
    Author:     Lukas Bulwahn and Ondrej Kuncar
kuncar@48622
     3
*)
haftmann@35617
     4
wenzelm@60500
     5
section \<open>Abstract type of RBT trees\<close>
haftmann@35617
     6
kuncar@48622
     7
theory RBT 
kuncar@53013
     8
imports Main RBT_Impl
haftmann@35617
     9
begin
haftmann@35617
    10
wenzelm@60500
    11
subsection \<open>Type definition\<close>
haftmann@35617
    12
wenzelm@61260
    13
typedef (overloaded) ('a, 'b) rbt = "{t :: ('a::linorder, 'b) RBT_Impl.rbt. is_rbt t}"
haftmann@36147
    14
  morphisms impl_of RBT
kuncar@48622
    15
proof -
kuncar@48622
    16
  have "RBT_Impl.Empty \<in> ?rbt" by simp
kuncar@48622
    17
  then show ?thesis ..
haftmann@35617
    18
qed
haftmann@35617
    19
haftmann@39380
    20
lemma rbt_eq_iff:
haftmann@39380
    21
  "t1 = t2 \<longleftrightarrow> impl_of t1 = impl_of t2"
haftmann@39380
    22
  by (simp add: impl_of_inject)
haftmann@39380
    23
haftmann@39380
    24
lemma rbt_eqI:
haftmann@39380
    25
  "impl_of t1 = impl_of t2 \<Longrightarrow> t1 = t2"
haftmann@39380
    26
  by (simp add: rbt_eq_iff)
haftmann@39380
    27
haftmann@36147
    28
lemma is_rbt_impl_of [simp, intro]:
haftmann@36147
    29
  "is_rbt (impl_of t)"
haftmann@36147
    30
  using impl_of [of t] by simp
haftmann@35617
    31
haftmann@39380
    32
lemma RBT_impl_of [simp, code abstype]:
haftmann@36147
    33
  "RBT (impl_of t) = t"
haftmann@36147
    34
  by (simp add: impl_of_inverse)
haftmann@35617
    35
wenzelm@60500
    36
subsection \<open>Primitive operations\<close>
haftmann@35617
    37
kuncar@48622
    38
setup_lifting type_definition_rbt
haftmann@35617
    39
wenzelm@61076
    40
lift_definition lookup :: "('a::linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "rbt_lookup" .
haftmann@35617
    41
wenzelm@61076
    42
lift_definition empty :: "('a::linorder, 'b) rbt" is RBT_Impl.Empty 
kuncar@48622
    43
by (simp add: empty_def)
haftmann@35617
    44
wenzelm@61076
    45
lift_definition insert :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_insert" 
kuncar@48622
    46
by simp
haftmann@35617
    47
wenzelm@61076
    48
lift_definition delete :: "'a::linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_delete" 
kuncar@48622
    49
by simp
haftmann@35617
    50
wenzelm@61076
    51
lift_definition entries :: "('a::linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" is RBT_Impl.entries .
kuncar@55565
    52
wenzelm@61076
    53
lift_definition keys :: "('a::linorder, 'b) rbt \<Rightarrow> 'a list" is RBT_Impl.keys .
haftmann@35617
    54
wenzelm@61076
    55
lift_definition bulkload :: "('a::linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" is "rbt_bulkload" ..
haftmann@35617
    56
wenzelm@61076
    57
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is rbt_map_entry
kuncar@48622
    58
by simp
haftmann@35617
    59
wenzelm@61076
    60
lift_definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> ('a, 'c) rbt" is RBT_Impl.map
kuncar@48622
    61
by simp
haftmann@35617
    62
wenzelm@61076
    63
lift_definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"  is RBT_Impl.fold .
haftmann@35617
    64
wenzelm@61076
    65
lift_definition union :: "('a::linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_union"
kuncar@48622
    66
by (simp add: rbt_union_is_rbt)
haftmann@35617
    67
kuncar@48622
    68
lift_definition foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"
kuncar@55565
    69
  is RBT_Impl.foldi .
eberlm@63194
    70
  
eberlm@63194
    71
lift_definition combine_with_key :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
eberlm@63194
    72
  is RBT_Impl.rbt_union_with_key by (rule is_rbt_rbt_unionwk)
eberlm@63194
    73
eberlm@63194
    74
lift_definition combine :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
eberlm@63194
    75
  is RBT_Impl.rbt_union_with by (rule rbt_unionw_is_rbt)
haftmann@35617
    76
wenzelm@60500
    77
subsection \<open>Derived operations\<close>
haftmann@35617
    78
wenzelm@61076
    79
definition is_empty :: "('a::linorder, 'b) rbt \<Rightarrow> bool" where
haftmann@36147
    80
  [code]: "is_empty t = (case impl_of t of RBT_Impl.Empty \<Rightarrow> True | _ \<Rightarrow> False)"
haftmann@35617
    81
eberlm@63194
    82
(* TODO: Is deleting more efficient than re-building the tree? 
eberlm@63194
    83
   (Probably more difficult to prove though *)
eberlm@63194
    84
definition filter :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a::linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
eberlm@63194
    85
  [code]: "filter P t = fold (\<lambda>k v t. if P k v then insert k v t else t) t empty" 
haftmann@35617
    86
wenzelm@60500
    87
subsection \<open>Abstract lookup properties\<close>
haftmann@35617
    88
haftmann@36147
    89
lemma lookup_RBT:
Andreas@47450
    90
  "is_rbt t \<Longrightarrow> lookup (RBT t) = rbt_lookup t"
haftmann@36147
    91
  by (simp add: lookup_def RBT_inverse)
haftmann@35617
    92
haftmann@36147
    93
lemma lookup_impl_of:
Andreas@47450
    94
  "rbt_lookup (impl_of t) = lookup t"
kuncar@48622
    95
  by transfer (rule refl)
haftmann@35617
    96
haftmann@36147
    97
lemma entries_impl_of:
haftmann@36147
    98
  "RBT_Impl.entries (impl_of t) = entries t"
kuncar@48622
    99
  by transfer (rule refl)
haftmann@35617
   100
haftmann@36147
   101
lemma keys_impl_of:
haftmann@36147
   102
  "RBT_Impl.keys (impl_of t) = keys t"
kuncar@48622
   103
  by transfer (rule refl)
haftmann@36111
   104
kuncar@49927
   105
lemma lookup_keys: 
kuncar@49927
   106
  "dom (lookup t) = set (keys t)" 
kuncar@49927
   107
  by transfer (simp add: rbt_lookup_keys)
kuncar@49927
   108
haftmann@35617
   109
lemma lookup_empty [simp]:
haftmann@35617
   110
  "lookup empty = Map.empty"
nipkow@39302
   111
  by (simp add: empty_def lookup_RBT fun_eq_iff)
haftmann@35617
   112
haftmann@36147
   113
lemma lookup_insert [simp]:
haftmann@36147
   114
  "lookup (insert k v t) = (lookup t)(k \<mapsto> v)"
kuncar@48622
   115
  by transfer (rule rbt_lookup_rbt_insert)
haftmann@35617
   116
haftmann@35617
   117
lemma lookup_delete [simp]:
haftmann@35617
   118
  "lookup (delete k t) = (lookup t)(k := None)"
kuncar@48622
   119
  by transfer (simp add: rbt_lookup_rbt_delete restrict_complement_singleton_eq)
haftmann@35617
   120
haftmann@35617
   121
lemma map_of_entries [simp]:
haftmann@35617
   122
  "map_of (entries t) = lookup t"
kuncar@48622
   123
  by transfer (simp add: map_of_entries)
haftmann@35617
   124
haftmann@36111
   125
lemma entries_lookup:
haftmann@36111
   126
  "entries t1 = entries t2 \<longleftrightarrow> lookup t1 = lookup t2"
kuncar@48622
   127
  by transfer (simp add: entries_rbt_lookup)
haftmann@36111
   128
haftmann@35617
   129
lemma lookup_bulkload [simp]:
haftmann@35617
   130
  "lookup (bulkload xs) = map_of xs"
kuncar@48622
   131
  by transfer (rule rbt_lookup_rbt_bulkload)
haftmann@35617
   132
haftmann@35617
   133
lemma lookup_map_entry [simp]:
blanchet@55466
   134
  "lookup (map_entry k f t) = (lookup t)(k := map_option f (lookup t k))"
kuncar@48622
   135
  by transfer (rule rbt_lookup_rbt_map_entry)
haftmann@35617
   136
haftmann@35617
   137
lemma lookup_map [simp]:
blanchet@55466
   138
  "lookup (map f t) k = map_option (f k) (lookup t k)"
kuncar@48622
   139
  by transfer (rule rbt_lookup_map)
haftmann@35617
   140
eberlm@63194
   141
lemma lookup_combine_with_key [simp]:
eberlm@63194
   142
  "lookup (combine_with_key f t1 t2) k = combine_options (f k) (lookup t1 k) (lookup t2 k)"
eberlm@63194
   143
  by transfer (simp_all add: combine_options_def rbt_lookup_rbt_unionwk)
eberlm@63194
   144
eberlm@63194
   145
lemma combine_altdef: "combine f t1 t2 = combine_with_key (\<lambda>_. f) t1 t2"
eberlm@63194
   146
  by transfer (simp add: rbt_union_with_def)
eberlm@63194
   147
eberlm@63194
   148
lemma lookup_combine [simp]:
eberlm@63194
   149
  "lookup (combine f t1 t2) k = combine_options f (lookup t1 k) (lookup t2 k)"
eberlm@63194
   150
  by (simp add: combine_altdef)
eberlm@63194
   151
haftmann@35617
   152
lemma fold_fold:
blanchet@55414
   153
  "fold f t = List.fold (case_prod f) (entries t)"
kuncar@48622
   154
  by transfer (rule RBT_Impl.fold_def)
kuncar@48622
   155
kuncar@48622
   156
lemma impl_of_empty:
kuncar@48622
   157
  "impl_of empty = RBT_Impl.Empty"
kuncar@48622
   158
  by transfer (rule refl)
haftmann@35617
   159
haftmann@36111
   160
lemma is_empty_empty [simp]:
haftmann@36111
   161
  "is_empty t \<longleftrightarrow> t = empty"
kuncar@48622
   162
  unfolding is_empty_def by transfer (simp split: rbt.split)
haftmann@36111
   163
haftmann@36111
   164
lemma RBT_lookup_empty [simp]: (*FIXME*)
Andreas@47450
   165
  "rbt_lookup t = Map.empty \<longleftrightarrow> t = RBT_Impl.Empty"
nipkow@39302
   166
  by (cases t) (auto simp add: fun_eq_iff)
haftmann@36111
   167
haftmann@36111
   168
lemma lookup_empty_empty [simp]:
haftmann@36111
   169
  "lookup t = Map.empty \<longleftrightarrow> t = empty"
kuncar@48622
   170
  by transfer (rule RBT_lookup_empty)
haftmann@36111
   171
haftmann@36111
   172
lemma sorted_keys [iff]:
haftmann@36111
   173
  "sorted (keys t)"
kuncar@48622
   174
  by transfer (simp add: RBT_Impl.keys_def rbt_sorted_entries)
haftmann@36111
   175
haftmann@36111
   176
lemma distinct_keys [iff]:
haftmann@36111
   177
  "distinct (keys t)"
kuncar@48622
   178
  by transfer (simp add: RBT_Impl.keys_def distinct_entries)
kuncar@48622
   179
kuncar@48622
   180
lemma finite_dom_lookup [simp, intro!]: "finite (dom (lookup t))"
kuncar@48622
   181
  by transfer simp
kuncar@48622
   182
kuncar@48622
   183
lemma lookup_union: "lookup (union s t) = lookup s ++ lookup t"
kuncar@48622
   184
  by transfer (simp add: rbt_lookup_rbt_union)
kuncar@48622
   185
kuncar@48622
   186
lemma lookup_in_tree: "(lookup t k = Some v) = ((k, v) \<in> set (entries t))"
kuncar@48622
   187
  by transfer (simp add: rbt_lookup_in_tree)
kuncar@48622
   188
kuncar@48622
   189
lemma keys_entries: "(k \<in> set (keys t)) = (\<exists>v. (k, v) \<in> set (entries t))"
kuncar@48622
   190
  by transfer (simp add: keys_entries)
kuncar@48622
   191
kuncar@48622
   192
lemma fold_def_alt:
blanchet@55414
   193
  "fold f t = List.fold (case_prod f) (entries t)"
kuncar@48622
   194
  by transfer (auto simp: RBT_Impl.fold_def)
kuncar@48622
   195
kuncar@48622
   196
lemma distinct_entries: "distinct (List.map fst (entries t))"
kuncar@48622
   197
  by transfer (simp add: distinct_entries)
kuncar@48622
   198
kuncar@48622
   199
lemma non_empty_keys: "t \<noteq> empty \<Longrightarrow> keys t \<noteq> []"
kuncar@48622
   200
  by transfer (simp add: non_empty_rbt_keys)
kuncar@48622
   201
kuncar@48622
   202
lemma keys_def_alt:
kuncar@48622
   203
  "keys t = List.map fst (entries t)"
kuncar@48622
   204
  by transfer (simp add: RBT_Impl.keys_def)
haftmann@36111
   205
eberlm@63194
   206
context
eberlm@63194
   207
begin
eberlm@63194
   208
eberlm@63194
   209
private lemma lookup_filter_aux:
eberlm@63194
   210
  assumes "distinct (List.map fst xs)"
eberlm@63194
   211
  shows   "lookup (List.fold (\<lambda>(k, v) t. if P k v then insert k v t else t) xs t) k =
eberlm@63194
   212
             (case map_of xs k of 
eberlm@63194
   213
                None \<Rightarrow> lookup t k
eberlm@63194
   214
              | Some v \<Rightarrow> if P k v then Some v else lookup t k)"
eberlm@63194
   215
  using assms by (induction xs arbitrary: t) (force split: option.splits)+
eberlm@63194
   216
eberlm@63194
   217
lemma lookup_filter: 
eberlm@63194
   218
  "lookup (filter P t) k = 
eberlm@63194
   219
     (case lookup t k of None \<Rightarrow> None | Some v \<Rightarrow> if P k v then Some v else None)"
eberlm@63194
   220
  unfolding filter_def using lookup_filter_aux[of "entries t" P empty k]
eberlm@63194
   221
  by (simp add: fold_fold distinct_entries split: option.splits)
eberlm@63194
   222
  
eberlm@63194
   223
end
eberlm@63194
   224
eberlm@63194
   225
wenzelm@60500
   226
subsection \<open>Quickcheck generators\<close>
bulwahn@45928
   227
bulwahn@46565
   228
quickcheck_generator rbt predicate: is_rbt constructors: empty, insert
haftmann@36111
   229
wenzelm@60500
   230
subsection \<open>Hide implementation details\<close>
kuncar@56019
   231
kuncar@56019
   232
lifting_update rbt.lifting
kuncar@56019
   233
lifting_forget rbt.lifting
kuncar@56019
   234
kuncar@56019
   235
hide_const (open) impl_of empty lookup keys entries bulkload delete map fold union insert map_entry foldi 
eberlm@63219
   236
  is_empty filter
kuncar@56019
   237
hide_fact (open) empty_def lookup_def keys_def entries_def bulkload_def delete_def map_def fold_def 
eberlm@63219
   238
  union_def insert_def map_entry_def foldi_def is_empty_def filter_def
kuncar@56019
   239
haftmann@35617
   240
end