src/HOL/Library/RBT.thy
author kuncar
Fri Dec 09 18:07:04 2011 +0100 (2011-12-09)
changeset 45802 b16f976db515
parent 45694 4a8743618257
child 45928 874845660119
permissions -rw-r--r--
Quotient_Info stores only relation maps
     1 (* Author: Florian Haftmann, TU Muenchen *)
     2 
     3 header {* Abstract type of Red-Black Trees *}
     4 
     5 (*<*)
     6 theory RBT
     7 imports Main RBT_Impl
     8 begin
     9 
    10 subsection {* Type definition *}
    11 
    12 typedef (open) ('a, 'b) rbt = "{t :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt. is_rbt t}"
    13   morphisms impl_of RBT
    14 proof
    15   show "RBT_Impl.Empty \<in> {t. is_rbt t}" by simp
    16 qed
    17 
    18 lemma rbt_eq_iff:
    19   "t1 = t2 \<longleftrightarrow> impl_of t1 = impl_of t2"
    20   by (simp add: impl_of_inject)
    21 
    22 lemma rbt_eqI:
    23   "impl_of t1 = impl_of t2 \<Longrightarrow> t1 = t2"
    24   by (simp add: rbt_eq_iff)
    25 
    26 lemma is_rbt_impl_of [simp, intro]:
    27   "is_rbt (impl_of t)"
    28   using impl_of [of t] by simp
    29 
    30 lemma RBT_impl_of [simp, code abstype]:
    31   "RBT (impl_of t) = t"
    32   by (simp add: impl_of_inverse)
    33 
    34 
    35 subsection {* Primitive operations *}
    36 
    37 definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" where
    38   [code]: "lookup t = RBT_Impl.lookup (impl_of t)"
    39 
    40 definition empty :: "('a\<Colon>linorder, 'b) rbt" where
    41   "empty = RBT RBT_Impl.Empty"
    42 
    43 lemma impl_of_empty [code abstract]:
    44   "impl_of empty = RBT_Impl.Empty"
    45   by (simp add: empty_def RBT_inverse)
    46 
    47 definition insert :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
    48   "insert k v t = RBT (RBT_Impl.insert k v (impl_of t))"
    49 
    50 lemma impl_of_insert [code abstract]:
    51   "impl_of (insert k v t) = RBT_Impl.insert k v (impl_of t)"
    52   by (simp add: insert_def RBT_inverse)
    53 
    54 definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
    55   "delete k t = RBT (RBT_Impl.delete k (impl_of t))"
    56 
    57 lemma impl_of_delete [code abstract]:
    58   "impl_of (delete k t) = RBT_Impl.delete k (impl_of t)"
    59   by (simp add: delete_def RBT_inverse)
    60 
    61 definition entries :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" where
    62   [code]: "entries t = RBT_Impl.entries (impl_of t)"
    63 
    64 definition keys :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list" where
    65   [code]: "keys t = RBT_Impl.keys (impl_of t)"
    66 
    67 definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" where
    68   "bulkload xs = RBT (RBT_Impl.bulkload xs)"
    69 
    70 lemma impl_of_bulkload [code abstract]:
    71   "impl_of (bulkload xs) = RBT_Impl.bulkload xs"
    72   by (simp add: bulkload_def RBT_inverse)
    73 
    74 definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
    75   "map_entry k f t = RBT (RBT_Impl.map_entry k f (impl_of t))"
    76 
    77 lemma impl_of_map_entry [code abstract]:
    78   "impl_of (map_entry k f t) = RBT_Impl.map_entry k f (impl_of t)"
    79   by (simp add: map_entry_def RBT_inverse)
    80 
    81 definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
    82   "map f t = RBT (RBT_Impl.map f (impl_of t))"
    83 
    84 lemma impl_of_map [code abstract]:
    85   "impl_of (map f t) = RBT_Impl.map f (impl_of t)"
    86   by (simp add: map_def RBT_inverse)
    87 
    88 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
    89   [code]: "fold f t = RBT_Impl.fold f (impl_of t)"
    90 
    91 
    92 subsection {* Derived operations *}
    93 
    94 definition is_empty :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> bool" where
    95   [code]: "is_empty t = (case impl_of t of RBT_Impl.Empty \<Rightarrow> True | _ \<Rightarrow> False)"
    96 
    97 
    98 subsection {* Abstract lookup properties *}
    99 
   100 lemma lookup_RBT:
   101   "is_rbt t \<Longrightarrow> lookup (RBT t) = RBT_Impl.lookup t"
   102   by (simp add: lookup_def RBT_inverse)
   103 
   104 lemma lookup_impl_of:
   105   "RBT_Impl.lookup (impl_of t) = lookup t"
   106   by (simp add: lookup_def)
   107 
   108 lemma entries_impl_of:
   109   "RBT_Impl.entries (impl_of t) = entries t"
   110   by (simp add: entries_def)
   111 
   112 lemma keys_impl_of:
   113   "RBT_Impl.keys (impl_of t) = keys t"
   114   by (simp add: keys_def)
   115 
   116 lemma lookup_empty [simp]:
   117   "lookup empty = Map.empty"
   118   by (simp add: empty_def lookup_RBT fun_eq_iff)
   119 
   120 lemma lookup_insert [simp]:
   121   "lookup (insert k v t) = (lookup t)(k \<mapsto> v)"
   122   by (simp add: insert_def lookup_RBT lookup_insert lookup_impl_of)
   123 
   124 lemma lookup_delete [simp]:
   125   "lookup (delete k t) = (lookup t)(k := None)"
   126   by (simp add: delete_def lookup_RBT RBT_Impl.lookup_delete lookup_impl_of restrict_complement_singleton_eq)
   127 
   128 lemma map_of_entries [simp]:
   129   "map_of (entries t) = lookup t"
   130   by (simp add: entries_def map_of_entries lookup_impl_of)
   131 
   132 lemma entries_lookup:
   133   "entries t1 = entries t2 \<longleftrightarrow> lookup t1 = lookup t2"
   134   by (simp add: entries_def lookup_def entries_lookup)
   135 
   136 lemma lookup_bulkload [simp]:
   137   "lookup (bulkload xs) = map_of xs"
   138   by (simp add: bulkload_def lookup_RBT RBT_Impl.lookup_bulkload)
   139 
   140 lemma lookup_map_entry [simp]:
   141   "lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))"
   142   by (simp add: map_entry_def lookup_RBT RBT_Impl.lookup_map_entry lookup_impl_of)
   143 
   144 lemma lookup_map [simp]:
   145   "lookup (map f t) k = Option.map (f k) (lookup t k)"
   146   by (simp add: map_def lookup_RBT RBT_Impl.lookup_map lookup_impl_of)
   147 
   148 lemma fold_fold:
   149   "fold f t = More_List.fold (prod_case f) (entries t)"
   150   by (simp add: fold_def fun_eq_iff RBT_Impl.fold_def entries_impl_of)
   151 
   152 lemma is_empty_empty [simp]:
   153   "is_empty t \<longleftrightarrow> t = empty"
   154   by (simp add: rbt_eq_iff is_empty_def impl_of_empty split: rbt.split)
   155 
   156 lemma RBT_lookup_empty [simp]: (*FIXME*)
   157   "RBT_Impl.lookup t = Map.empty \<longleftrightarrow> t = RBT_Impl.Empty"
   158   by (cases t) (auto simp add: fun_eq_iff)
   159 
   160 lemma lookup_empty_empty [simp]:
   161   "lookup t = Map.empty \<longleftrightarrow> t = empty"
   162   by (cases t) (simp add: empty_def lookup_def RBT_inject RBT_inverse)
   163 
   164 lemma sorted_keys [iff]:
   165   "sorted (keys t)"
   166   by (simp add: keys_def RBT_Impl.keys_def sorted_entries)
   167 
   168 lemma distinct_keys [iff]:
   169   "distinct (keys t)"
   170   by (simp add: keys_def RBT_Impl.keys_def distinct_entries)
   171 
   172 
   173 end