src/HOL/Library/RBT.thy
author haftmann
Fri Nov 01 18:51:14 2013 +0100 (2013-11-01)
changeset 54230 b1d955791529
parent 53013 3fbcfa911863
child 55414 eab03e9cee8a
permissions -rw-r--r--
more simplification rules on unary and binary minus
kuncar@48622
     1
(*  Title:      HOL/Library/RBT.thy
kuncar@48622
     2
    Author:     Lukas Bulwahn and Ondrej Kuncar
kuncar@48622
     3
*)
haftmann@35617
     4
kuncar@48622
     5
header {* Abstract type of RBT trees *}
haftmann@35617
     6
kuncar@48622
     7
theory RBT 
kuncar@53013
     8
imports Main RBT_Impl
haftmann@35617
     9
begin
haftmann@35617
    10
haftmann@35617
    11
subsection {* Type definition *}
haftmann@35617
    12
wenzelm@49834
    13
typedef ('a, 'b) rbt = "{t :: ('a\<Colon>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
haftmann@35617
    36
subsection {* Primitive operations *}
haftmann@35617
    37
kuncar@48622
    38
setup_lifting type_definition_rbt
kuncar@51375
    39
print_theorems
haftmann@35617
    40
kuncar@48622
    41
lift_definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "rbt_lookup" 
kuncar@48622
    42
by simp
haftmann@35617
    43
kuncar@48622
    44
lift_definition empty :: "('a\<Colon>linorder, 'b) rbt" is RBT_Impl.Empty 
kuncar@48622
    45
by (simp add: empty_def)
haftmann@35617
    46
kuncar@48622
    47
lift_definition insert :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_insert" 
kuncar@48622
    48
by simp
haftmann@35617
    49
kuncar@48622
    50
lift_definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_delete" 
kuncar@48622
    51
by simp
haftmann@35617
    52
kuncar@48622
    53
lift_definition entries :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" is RBT_Impl.entries
kuncar@48622
    54
by simp
haftmann@35617
    55
kuncar@48622
    56
lift_definition keys :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list" is RBT_Impl.keys 
kuncar@48622
    57
by simp
haftmann@35617
    58
kuncar@48622
    59
lift_definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" is "rbt_bulkload" 
kuncar@48622
    60
by simp
haftmann@35617
    61
kuncar@48622
    62
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is rbt_map_entry 
kuncar@48622
    63
by simp
haftmann@35617
    64
kuncar@51375
    65
lift_definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'c) rbt" is RBT_Impl.map
kuncar@48622
    66
by simp
haftmann@35617
    67
kuncar@48622
    68
lift_definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"  is RBT_Impl.fold 
kuncar@48622
    69
by simp
haftmann@35617
    70
kuncar@48622
    71
lift_definition union :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_union"
kuncar@48622
    72
by (simp add: rbt_union_is_rbt)
haftmann@35617
    73
kuncar@48622
    74
lift_definition foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"
kuncar@48622
    75
  is RBT_Impl.foldi by simp
haftmann@35617
    76
haftmann@35617
    77
subsection {* Derived operations *}
haftmann@35617
    78
haftmann@36147
    79
definition is_empty :: "('a\<Colon>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
haftmann@35617
    82
haftmann@35617
    83
subsection {* Abstract lookup properties *}
haftmann@35617
    84
haftmann@36147
    85
lemma lookup_RBT:
Andreas@47450
    86
  "is_rbt t \<Longrightarrow> lookup (RBT t) = rbt_lookup t"
haftmann@36147
    87
  by (simp add: lookup_def RBT_inverse)
haftmann@35617
    88
haftmann@36147
    89
lemma lookup_impl_of:
Andreas@47450
    90
  "rbt_lookup (impl_of t) = lookup t"
kuncar@48622
    91
  by transfer (rule refl)
haftmann@35617
    92
haftmann@36147
    93
lemma entries_impl_of:
haftmann@36147
    94
  "RBT_Impl.entries (impl_of t) = entries t"
kuncar@48622
    95
  by transfer (rule refl)
haftmann@35617
    96
haftmann@36147
    97
lemma keys_impl_of:
haftmann@36147
    98
  "RBT_Impl.keys (impl_of t) = keys t"
kuncar@48622
    99
  by transfer (rule refl)
haftmann@36111
   100
kuncar@49927
   101
lemma lookup_keys: 
kuncar@49927
   102
  "dom (lookup t) = set (keys t)" 
kuncar@49927
   103
  by transfer (simp add: rbt_lookup_keys)
kuncar@49927
   104
haftmann@35617
   105
lemma lookup_empty [simp]:
haftmann@35617
   106
  "lookup empty = Map.empty"
nipkow@39302
   107
  by (simp add: empty_def lookup_RBT fun_eq_iff)
haftmann@35617
   108
haftmann@36147
   109
lemma lookup_insert [simp]:
haftmann@36147
   110
  "lookup (insert k v t) = (lookup t)(k \<mapsto> v)"
kuncar@48622
   111
  by transfer (rule rbt_lookup_rbt_insert)
haftmann@35617
   112
haftmann@35617
   113
lemma lookup_delete [simp]:
haftmann@35617
   114
  "lookup (delete k t) = (lookup t)(k := None)"
kuncar@48622
   115
  by transfer (simp add: rbt_lookup_rbt_delete restrict_complement_singleton_eq)
haftmann@35617
   116
haftmann@35617
   117
lemma map_of_entries [simp]:
haftmann@35617
   118
  "map_of (entries t) = lookup t"
kuncar@48622
   119
  by transfer (simp add: map_of_entries)
haftmann@35617
   120
haftmann@36111
   121
lemma entries_lookup:
haftmann@36111
   122
  "entries t1 = entries t2 \<longleftrightarrow> lookup t1 = lookup t2"
kuncar@48622
   123
  by transfer (simp add: entries_rbt_lookup)
haftmann@36111
   124
haftmann@35617
   125
lemma lookup_bulkload [simp]:
haftmann@35617
   126
  "lookup (bulkload xs) = map_of xs"
kuncar@48622
   127
  by transfer (rule rbt_lookup_rbt_bulkload)
haftmann@35617
   128
haftmann@35617
   129
lemma lookup_map_entry [simp]:
haftmann@35617
   130
  "lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))"
kuncar@48622
   131
  by transfer (rule rbt_lookup_rbt_map_entry)
haftmann@35617
   132
haftmann@35617
   133
lemma lookup_map [simp]:
haftmann@35617
   134
  "lookup (map f t) k = Option.map (f k) (lookup t k)"
kuncar@48622
   135
  by transfer (rule rbt_lookup_map)
haftmann@35617
   136
haftmann@35617
   137
lemma fold_fold:
haftmann@46133
   138
  "fold f t = List.fold (prod_case f) (entries t)"
kuncar@48622
   139
  by transfer (rule RBT_Impl.fold_def)
kuncar@48622
   140
kuncar@48622
   141
lemma impl_of_empty:
kuncar@48622
   142
  "impl_of empty = RBT_Impl.Empty"
kuncar@48622
   143
  by transfer (rule refl)
haftmann@35617
   144
haftmann@36111
   145
lemma is_empty_empty [simp]:
haftmann@36111
   146
  "is_empty t \<longleftrightarrow> t = empty"
kuncar@48622
   147
  unfolding is_empty_def by transfer (simp split: rbt.split)
haftmann@36111
   148
haftmann@36111
   149
lemma RBT_lookup_empty [simp]: (*FIXME*)
Andreas@47450
   150
  "rbt_lookup t = Map.empty \<longleftrightarrow> t = RBT_Impl.Empty"
nipkow@39302
   151
  by (cases t) (auto simp add: fun_eq_iff)
haftmann@36111
   152
haftmann@36111
   153
lemma lookup_empty_empty [simp]:
haftmann@36111
   154
  "lookup t = Map.empty \<longleftrightarrow> t = empty"
kuncar@48622
   155
  by transfer (rule RBT_lookup_empty)
haftmann@36111
   156
haftmann@36111
   157
lemma sorted_keys [iff]:
haftmann@36111
   158
  "sorted (keys t)"
kuncar@48622
   159
  by transfer (simp add: RBT_Impl.keys_def rbt_sorted_entries)
haftmann@36111
   160
haftmann@36111
   161
lemma distinct_keys [iff]:
haftmann@36111
   162
  "distinct (keys t)"
kuncar@48622
   163
  by transfer (simp add: RBT_Impl.keys_def distinct_entries)
kuncar@48622
   164
kuncar@48622
   165
lemma finite_dom_lookup [simp, intro!]: "finite (dom (lookup t))"
kuncar@48622
   166
  by transfer simp
kuncar@48622
   167
kuncar@48622
   168
lemma lookup_union: "lookup (union s t) = lookup s ++ lookup t"
kuncar@48622
   169
  by transfer (simp add: rbt_lookup_rbt_union)
kuncar@48622
   170
kuncar@48622
   171
lemma lookup_in_tree: "(lookup t k = Some v) = ((k, v) \<in> set (entries t))"
kuncar@48622
   172
  by transfer (simp add: rbt_lookup_in_tree)
kuncar@48622
   173
kuncar@48622
   174
lemma keys_entries: "(k \<in> set (keys t)) = (\<exists>v. (k, v) \<in> set (entries t))"
kuncar@48622
   175
  by transfer (simp add: keys_entries)
kuncar@48622
   176
kuncar@48622
   177
lemma fold_def_alt:
kuncar@48622
   178
  "fold f t = List.fold (prod_case f) (entries t)"
kuncar@48622
   179
  by transfer (auto simp: RBT_Impl.fold_def)
kuncar@48622
   180
kuncar@48622
   181
lemma distinct_entries: "distinct (List.map fst (entries t))"
kuncar@48622
   182
  by transfer (simp add: distinct_entries)
kuncar@48622
   183
kuncar@48622
   184
lemma non_empty_keys: "t \<noteq> empty \<Longrightarrow> keys t \<noteq> []"
kuncar@48622
   185
  by transfer (simp add: non_empty_rbt_keys)
kuncar@48622
   186
kuncar@48622
   187
lemma keys_def_alt:
kuncar@48622
   188
  "keys t = List.map fst (entries t)"
kuncar@48622
   189
  by transfer (simp add: RBT_Impl.keys_def)
haftmann@36111
   190
bulwahn@45928
   191
subsection {* Quickcheck generators *}
bulwahn@45928
   192
bulwahn@46565
   193
quickcheck_generator rbt predicate: is_rbt constructors: empty, insert
haftmann@36111
   194
haftmann@35617
   195
end