src/HOL/Library/RBT.thy
author kuncar
Tue Feb 18 23:03:50 2014 +0100 (2014-02-18)
changeset 55565 f663fc1e653b
parent 55466 786edc984c98
child 56019 682bba24e474
permissions -rw-r--r--
simplify proofs because of the stronger reflexivity prover
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@55565
    41
lift_definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "rbt_lookup" .
haftmann@35617
    42
kuncar@48622
    43
lift_definition empty :: "('a\<Colon>linorder, 'b) rbt" is RBT_Impl.Empty 
kuncar@48622
    44
by (simp add: empty_def)
haftmann@35617
    45
kuncar@48622
    46
lift_definition insert :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_insert" 
kuncar@48622
    47
by simp
haftmann@35617
    48
kuncar@48622
    49
lift_definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_delete" 
kuncar@48622
    50
by simp
haftmann@35617
    51
kuncar@55565
    52
lift_definition entries :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" is RBT_Impl.entries .
kuncar@55565
    53
kuncar@55565
    54
lift_definition keys :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list" is RBT_Impl.keys .
haftmann@35617
    55
kuncar@55565
    56
lift_definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" is "rbt_bulkload" ..
haftmann@35617
    57
kuncar@55565
    58
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
    59
by simp
haftmann@35617
    60
kuncar@51375
    61
lift_definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'c) rbt" is RBT_Impl.map
kuncar@48622
    62
by simp
haftmann@35617
    63
kuncar@55565
    64
lift_definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"  is RBT_Impl.fold .
haftmann@35617
    65
kuncar@48622
    66
lift_definition union :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_union"
kuncar@48622
    67
by (simp add: rbt_union_is_rbt)
haftmann@35617
    68
kuncar@48622
    69
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
    70
  is RBT_Impl.foldi .
haftmann@35617
    71
haftmann@35617
    72
subsection {* Derived operations *}
haftmann@35617
    73
haftmann@36147
    74
definition is_empty :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> bool" where
haftmann@36147
    75
  [code]: "is_empty t = (case impl_of t of RBT_Impl.Empty \<Rightarrow> True | _ \<Rightarrow> False)"
haftmann@35617
    76
haftmann@35617
    77
haftmann@35617
    78
subsection {* Abstract lookup properties *}
haftmann@35617
    79
haftmann@36147
    80
lemma lookup_RBT:
Andreas@47450
    81
  "is_rbt t \<Longrightarrow> lookup (RBT t) = rbt_lookup t"
haftmann@36147
    82
  by (simp add: lookup_def RBT_inverse)
haftmann@35617
    83
haftmann@36147
    84
lemma lookup_impl_of:
Andreas@47450
    85
  "rbt_lookup (impl_of t) = lookup t"
kuncar@48622
    86
  by transfer (rule refl)
haftmann@35617
    87
haftmann@36147
    88
lemma entries_impl_of:
haftmann@36147
    89
  "RBT_Impl.entries (impl_of t) = entries t"
kuncar@48622
    90
  by transfer (rule refl)
haftmann@35617
    91
haftmann@36147
    92
lemma keys_impl_of:
haftmann@36147
    93
  "RBT_Impl.keys (impl_of t) = keys t"
kuncar@48622
    94
  by transfer (rule refl)
haftmann@36111
    95
kuncar@49927
    96
lemma lookup_keys: 
kuncar@49927
    97
  "dom (lookup t) = set (keys t)" 
kuncar@49927
    98
  by transfer (simp add: rbt_lookup_keys)
kuncar@49927
    99
haftmann@35617
   100
lemma lookup_empty [simp]:
haftmann@35617
   101
  "lookup empty = Map.empty"
nipkow@39302
   102
  by (simp add: empty_def lookup_RBT fun_eq_iff)
haftmann@35617
   103
haftmann@36147
   104
lemma lookup_insert [simp]:
haftmann@36147
   105
  "lookup (insert k v t) = (lookup t)(k \<mapsto> v)"
kuncar@48622
   106
  by transfer (rule rbt_lookup_rbt_insert)
haftmann@35617
   107
haftmann@35617
   108
lemma lookup_delete [simp]:
haftmann@35617
   109
  "lookup (delete k t) = (lookup t)(k := None)"
kuncar@48622
   110
  by transfer (simp add: rbt_lookup_rbt_delete restrict_complement_singleton_eq)
haftmann@35617
   111
haftmann@35617
   112
lemma map_of_entries [simp]:
haftmann@35617
   113
  "map_of (entries t) = lookup t"
kuncar@48622
   114
  by transfer (simp add: map_of_entries)
haftmann@35617
   115
haftmann@36111
   116
lemma entries_lookup:
haftmann@36111
   117
  "entries t1 = entries t2 \<longleftrightarrow> lookup t1 = lookup t2"
kuncar@48622
   118
  by transfer (simp add: entries_rbt_lookup)
haftmann@36111
   119
haftmann@35617
   120
lemma lookup_bulkload [simp]:
haftmann@35617
   121
  "lookup (bulkload xs) = map_of xs"
kuncar@48622
   122
  by transfer (rule rbt_lookup_rbt_bulkload)
haftmann@35617
   123
haftmann@35617
   124
lemma lookup_map_entry [simp]:
blanchet@55466
   125
  "lookup (map_entry k f t) = (lookup t)(k := map_option f (lookup t k))"
kuncar@48622
   126
  by transfer (rule rbt_lookup_rbt_map_entry)
haftmann@35617
   127
haftmann@35617
   128
lemma lookup_map [simp]:
blanchet@55466
   129
  "lookup (map f t) k = map_option (f k) (lookup t k)"
kuncar@48622
   130
  by transfer (rule rbt_lookup_map)
haftmann@35617
   131
haftmann@35617
   132
lemma fold_fold:
blanchet@55414
   133
  "fold f t = List.fold (case_prod f) (entries t)"
kuncar@48622
   134
  by transfer (rule RBT_Impl.fold_def)
kuncar@48622
   135
kuncar@48622
   136
lemma impl_of_empty:
kuncar@48622
   137
  "impl_of empty = RBT_Impl.Empty"
kuncar@48622
   138
  by transfer (rule refl)
haftmann@35617
   139
haftmann@36111
   140
lemma is_empty_empty [simp]:
haftmann@36111
   141
  "is_empty t \<longleftrightarrow> t = empty"
kuncar@48622
   142
  unfolding is_empty_def by transfer (simp split: rbt.split)
haftmann@36111
   143
haftmann@36111
   144
lemma RBT_lookup_empty [simp]: (*FIXME*)
Andreas@47450
   145
  "rbt_lookup t = Map.empty \<longleftrightarrow> t = RBT_Impl.Empty"
nipkow@39302
   146
  by (cases t) (auto simp add: fun_eq_iff)
haftmann@36111
   147
haftmann@36111
   148
lemma lookup_empty_empty [simp]:
haftmann@36111
   149
  "lookup t = Map.empty \<longleftrightarrow> t = empty"
kuncar@48622
   150
  by transfer (rule RBT_lookup_empty)
haftmann@36111
   151
haftmann@36111
   152
lemma sorted_keys [iff]:
haftmann@36111
   153
  "sorted (keys t)"
kuncar@48622
   154
  by transfer (simp add: RBT_Impl.keys_def rbt_sorted_entries)
haftmann@36111
   155
haftmann@36111
   156
lemma distinct_keys [iff]:
haftmann@36111
   157
  "distinct (keys t)"
kuncar@48622
   158
  by transfer (simp add: RBT_Impl.keys_def distinct_entries)
kuncar@48622
   159
kuncar@48622
   160
lemma finite_dom_lookup [simp, intro!]: "finite (dom (lookup t))"
kuncar@48622
   161
  by transfer simp
kuncar@48622
   162
kuncar@48622
   163
lemma lookup_union: "lookup (union s t) = lookup s ++ lookup t"
kuncar@48622
   164
  by transfer (simp add: rbt_lookup_rbt_union)
kuncar@48622
   165
kuncar@48622
   166
lemma lookup_in_tree: "(lookup t k = Some v) = ((k, v) \<in> set (entries t))"
kuncar@48622
   167
  by transfer (simp add: rbt_lookup_in_tree)
kuncar@48622
   168
kuncar@48622
   169
lemma keys_entries: "(k \<in> set (keys t)) = (\<exists>v. (k, v) \<in> set (entries t))"
kuncar@48622
   170
  by transfer (simp add: keys_entries)
kuncar@48622
   171
kuncar@48622
   172
lemma fold_def_alt:
blanchet@55414
   173
  "fold f t = List.fold (case_prod f) (entries t)"
kuncar@48622
   174
  by transfer (auto simp: RBT_Impl.fold_def)
kuncar@48622
   175
kuncar@48622
   176
lemma distinct_entries: "distinct (List.map fst (entries t))"
kuncar@48622
   177
  by transfer (simp add: distinct_entries)
kuncar@48622
   178
kuncar@48622
   179
lemma non_empty_keys: "t \<noteq> empty \<Longrightarrow> keys t \<noteq> []"
kuncar@48622
   180
  by transfer (simp add: non_empty_rbt_keys)
kuncar@48622
   181
kuncar@48622
   182
lemma keys_def_alt:
kuncar@48622
   183
  "keys t = List.map fst (entries t)"
kuncar@48622
   184
  by transfer (simp add: RBT_Impl.keys_def)
haftmann@36111
   185
bulwahn@45928
   186
subsection {* Quickcheck generators *}
bulwahn@45928
   187
bulwahn@46565
   188
quickcheck_generator rbt predicate: is_rbt constructors: empty, insert
haftmann@36111
   189
haftmann@35617
   190
end