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