src/HOL/Library/RBT.thy
author kuncar
Fri Mar 08 13:21:06 2013 +0100 (2013-03-08)
changeset 51375 d9e62d9c98de
parent 49939 eb8b434158c8
child 53013 3fbcfa911863
permissions -rw-r--r--
patch Isabelle ditribution to conform to changes regarding the parametricity
     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 Quotient_List
     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 print_theorems
    40 
    41 lift_definition lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "rbt_lookup" 
    42 by simp
    43 
    44 lift_definition empty :: "('a\<Colon>linorder, 'b) rbt" is RBT_Impl.Empty 
    45 by (simp add: empty_def)
    46 
    47 lift_definition insert :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_insert" 
    48 by simp
    49 
    50 lift_definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_delete" 
    51 by simp
    52 
    53 lift_definition entries :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list" is RBT_Impl.entries
    54 by simp
    55 
    56 lift_definition keys :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list" is RBT_Impl.keys 
    57 by simp
    58 
    59 lift_definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" is "rbt_bulkload" 
    60 by simp
    61 
    62 lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is rbt_map_entry 
    63 by simp
    64 
    65 lift_definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'c) rbt" is RBT_Impl.map
    66 by simp
    67 
    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 
    69 by simp
    70 
    71 lift_definition union :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" is "rbt_union"
    72 by (simp add: rbt_union_is_rbt)
    73 
    74 lift_definition foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"
    75   is RBT_Impl.foldi by simp
    76 
    77 subsection {* Derived operations *}
    78 
    79 definition is_empty :: "('a\<Colon>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 
    83 subsection {* Abstract lookup properties *}
    84 
    85 lemma lookup_RBT:
    86   "is_rbt t \<Longrightarrow> lookup (RBT t) = rbt_lookup t"
    87   by (simp add: lookup_def RBT_inverse)
    88 
    89 lemma lookup_impl_of:
    90   "rbt_lookup (impl_of t) = lookup t"
    91   by transfer (rule refl)
    92 
    93 lemma entries_impl_of:
    94   "RBT_Impl.entries (impl_of t) = entries t"
    95   by transfer (rule refl)
    96 
    97 lemma keys_impl_of:
    98   "RBT_Impl.keys (impl_of t) = keys t"
    99   by transfer (rule refl)
   100 
   101 lemma lookup_keys: 
   102   "dom (lookup t) = set (keys t)" 
   103   by transfer (simp add: rbt_lookup_keys)
   104 
   105 lemma lookup_empty [simp]:
   106   "lookup empty = Map.empty"
   107   by (simp add: empty_def lookup_RBT fun_eq_iff)
   108 
   109 lemma lookup_insert [simp]:
   110   "lookup (insert k v t) = (lookup t)(k \<mapsto> v)"
   111   by transfer (rule rbt_lookup_rbt_insert)
   112 
   113 lemma lookup_delete [simp]:
   114   "lookup (delete k t) = (lookup t)(k := None)"
   115   by transfer (simp add: rbt_lookup_rbt_delete restrict_complement_singleton_eq)
   116 
   117 lemma map_of_entries [simp]:
   118   "map_of (entries t) = lookup t"
   119   by transfer (simp add: map_of_entries)
   120 
   121 lemma entries_lookup:
   122   "entries t1 = entries t2 \<longleftrightarrow> lookup t1 = lookup t2"
   123   by transfer (simp add: entries_rbt_lookup)
   124 
   125 lemma lookup_bulkload [simp]:
   126   "lookup (bulkload xs) = map_of xs"
   127   by transfer (rule rbt_lookup_rbt_bulkload)
   128 
   129 lemma lookup_map_entry [simp]:
   130   "lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))"
   131   by transfer (rule rbt_lookup_rbt_map_entry)
   132 
   133 lemma lookup_map [simp]:
   134   "lookup (map f t) k = Option.map (f k) (lookup t k)"
   135   by transfer (rule rbt_lookup_map)
   136 
   137 lemma fold_fold:
   138   "fold f t = List.fold (prod_case f) (entries t)"
   139   by transfer (rule RBT_Impl.fold_def)
   140 
   141 lemma impl_of_empty:
   142   "impl_of empty = RBT_Impl.Empty"
   143   by transfer (rule refl)
   144 
   145 lemma is_empty_empty [simp]:
   146   "is_empty t \<longleftrightarrow> t = empty"
   147   unfolding is_empty_def by transfer (simp split: rbt.split)
   148 
   149 lemma RBT_lookup_empty [simp]: (*FIXME*)
   150   "rbt_lookup t = Map.empty \<longleftrightarrow> t = RBT_Impl.Empty"
   151   by (cases t) (auto simp add: fun_eq_iff)
   152 
   153 lemma lookup_empty_empty [simp]:
   154   "lookup t = Map.empty \<longleftrightarrow> t = empty"
   155   by transfer (rule RBT_lookup_empty)
   156 
   157 lemma sorted_keys [iff]:
   158   "sorted (keys t)"
   159   by transfer (simp add: RBT_Impl.keys_def rbt_sorted_entries)
   160 
   161 lemma distinct_keys [iff]:
   162   "distinct (keys t)"
   163   by transfer (simp add: RBT_Impl.keys_def distinct_entries)
   164 
   165 lemma finite_dom_lookup [simp, intro!]: "finite (dom (lookup t))"
   166   by transfer simp
   167 
   168 lemma lookup_union: "lookup (union s t) = lookup s ++ lookup t"
   169   by transfer (simp add: rbt_lookup_rbt_union)
   170 
   171 lemma lookup_in_tree: "(lookup t k = Some v) = ((k, v) \<in> set (entries t))"
   172   by transfer (simp add: rbt_lookup_in_tree)
   173 
   174 lemma keys_entries: "(k \<in> set (keys t)) = (\<exists>v. (k, v) \<in> set (entries t))"
   175   by transfer (simp add: keys_entries)
   176 
   177 lemma fold_def_alt:
   178   "fold f t = List.fold (prod_case f) (entries t)"
   179   by transfer (auto simp: RBT_Impl.fold_def)
   180 
   181 lemma distinct_entries: "distinct (List.map fst (entries t))"
   182   by transfer (simp add: distinct_entries)
   183 
   184 lemma non_empty_keys: "t \<noteq> empty \<Longrightarrow> keys t \<noteq> []"
   185   by transfer (simp add: non_empty_rbt_keys)
   186 
   187 lemma keys_def_alt:
   188   "keys t = List.map fst (entries t)"
   189   by transfer (simp add: RBT_Impl.keys_def)
   190 
   191 subsection {* Quickcheck generators *}
   192 
   193 quickcheck_generator rbt predicate: is_rbt constructors: empty, insert
   194 
   195 end