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