# HG changeset patch # User haftmann # Date 1267885890 -3600 # Node ID a6528fb9964165b3c160246cae29fc37540b2e34 # Parent 0f2c634c8ab7395c1854810282a6116ecca660f0 added Table.thy diff -r 0f2c634c8ab7 -r a6528fb99641 src/HOL/IsaMakefile --- a/src/HOL/IsaMakefile Sat Mar 06 11:21:09 2010 +0100 +++ b/src/HOL/IsaMakefile Sat Mar 06 15:31:30 2010 +0100 @@ -401,7 +401,7 @@ Library/Ramsey.thy Library/Zorn.thy Library/Library/ROOT.ML \ Library/Library/document/root.tex Library/Library/document/root.bib \ Library/Transitive_Closure_Table.thy Library/While_Combinator.thy \ - Library/Product_ord.thy Library/Char_nat.thy \ + Library/Product_ord.thy Library/Char_nat.thy Library/Table.thy \ Library/Sublist_Order.thy Library/List_lexord.thy \ Library/Coinductive_List.thy Library/AssocList.thy \ Library/Formal_Power_Series.thy Library/Binomial.thy \ diff -r 0f2c634c8ab7 -r a6528fb99641 src/HOL/Library/Library.thy --- a/src/HOL/Library/Library.thy Sat Mar 06 11:21:09 2010 +0100 +++ b/src/HOL/Library/Library.thy Sat Mar 06 15:31:30 2010 +0100 @@ -58,6 +58,7 @@ SML_Quickcheck State_Monad Sum_Of_Squares + Table Transitive_Closure_Table Univ_Poly While_Combinator diff -r 0f2c634c8ab7 -r a6528fb99641 src/HOL/Library/Table.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Library/Table.thy Sat Mar 06 15:31:30 2010 +0100 @@ -0,0 +1,139 @@ +(* Author: Florian Haftmann, TU Muenchen *) + +header {* Tables: finite mappings implemented by red-black trees *} + +theory Table +imports Main RBT +begin + +subsection {* Type definition *} + +typedef (open) ('a, 'b) table = "{t :: ('a\linorder, 'b) rbt. is_rbt t}" + morphisms tree_of Table +proof - + have "RBT.Empty \ ?table" by simp + then show ?thesis .. +qed + +lemma is_rbt_tree_of [simp, intro]: + "is_rbt (tree_of t)" + using tree_of [of t] by simp + +lemma table_eq: + "t1 = t2 \ tree_of t1 = tree_of t2" + by (simp add: tree_of_inject) + +code_abstype Table tree_of + by (simp add: tree_of_inverse) + + +subsection {* Primitive operations *} + +definition lookup :: "('a\linorder, 'b) table \ 'a \ 'b" where + [code]: "lookup t = RBT.lookup (tree_of t)" + +definition empty :: "('a\linorder, 'b) table" where + "empty = Table RBT.Empty" + +lemma tree_of_empty [code abstract]: + "tree_of empty = RBT.Empty" + by (simp add: empty_def Table_inverse) + +definition update :: "'a\linorder \ 'b \ ('a, 'b) table \ ('a, 'b) table" where + "update k v t = Table (RBT.insert k v (tree_of t))" + +lemma tree_of_update [code abstract]: + "tree_of (update k v t) = RBT.insert k v (tree_of t)" + by (simp add: update_def Table_inverse) + +definition delete :: "'a\linorder \ ('a, 'b) table \ ('a, 'b) table" where + "delete k t = Table (RBT.delete k (tree_of t))" + +lemma tree_of_delete [code abstract]: + "tree_of (delete k t) = RBT.delete k (tree_of t)" + by (simp add: delete_def Table_inverse) + +definition entries :: "('a\linorder, 'b) table \ ('a \ 'b) list" where + [code]: "entries t = RBT.entries (tree_of t)" + +definition bulkload :: "('a\linorder \ 'b) list \ ('a, 'b) table" where + "bulkload xs = Table (RBT.bulkload xs)" + +lemma tree_of_bulkload [code abstract]: + "tree_of (bulkload xs) = RBT.bulkload xs" + by (simp add: bulkload_def Table_inverse) + +definition map_entry :: "'a \ ('b \ 'b) \ ('a\linorder, 'b) table \ ('a, 'b) table" where + "map_entry k f t = Table (RBT.map_entry k f (tree_of t))" + +lemma tree_of_map_entry [code abstract]: + "tree_of (map_entry k f t) = RBT.map_entry k f (tree_of t)" + by (simp add: map_entry_def Table_inverse) + +definition map :: "('a \ 'b \ 'b) \ ('a\linorder, 'b) table \ ('a, 'b) table" where + "map f t = Table (RBT.map f (tree_of t))" + +lemma tree_of_map [code abstract]: + "tree_of (map f t) = RBT.map f (tree_of t)" + by (simp add: map_def Table_inverse) + +definition fold :: "('a \ 'b \ 'c \ 'c) \ ('a\linorder, 'b) table \ 'c \ 'c" where + [code]: "fold f t = RBT.fold f (tree_of t)" + + +subsection {* Derived operations *} + +definition is_empty :: "('a\linorder, 'b) table \ bool" where + [code]: "is_empty t = (case tree_of t of RBT.Empty \ True | _ \ False)" + + +subsection {* Abstract lookup properties *} + +lemma lookup_Table: + "is_rbt t \ lookup (Table t) = RBT.lookup t" + by (simp add: lookup_def Table_inverse) + +lemma lookup_tree_of: + "RBT.lookup (tree_of t) = lookup t" + by (simp add: lookup_def) + +lemma entries_tree_of: + "RBT.entries (tree_of t) = entries t" + by (simp add: entries_def) + +lemma lookup_empty [simp]: + "lookup empty = Map.empty" + by (simp add: empty_def lookup_Table expand_fun_eq) + +lemma lookup_update [simp]: + "lookup (update k v t) = (lookup t)(k \ v)" + by (simp add: update_def lookup_Table lookup_insert lookup_tree_of) + +lemma lookup_delete [simp]: + "lookup (delete k t) = (lookup t)(k := None)" + by (simp add: delete_def lookup_Table lookup_delete lookup_tree_of restrict_complement_singleton_eq) + +lemma map_of_entries [simp]: + "map_of (entries t) = lookup t" + by (simp add: entries_def map_of_entries lookup_tree_of) + +lemma lookup_bulkload [simp]: + "lookup (bulkload xs) = map_of xs" + by (simp add: bulkload_def lookup_Table lookup_bulkload) + +lemma lookup_map_entry [simp]: + "lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))" + by (simp add: map_entry_def lookup_Table lookup_map_entry lookup_tree_of) + +lemma lookup_map [simp]: + "lookup (map f t) k = Option.map (f k) (lookup t k)" + by (simp add: map_def lookup_Table lookup_map lookup_tree_of) + +lemma fold_fold: + "fold f t = (\s. foldl (\s (k, v). f k v s) s (entries t))" + by (simp add: fold_def expand_fun_eq RBT.fold_def entries_tree_of) + +hide (open) const tree_of lookup empty update delete + entries bulkload map_entry map fold + +end diff -r 0f2c634c8ab7 -r a6528fb99641 src/HOL/ex/Codegenerator_Candidates.thy --- a/src/HOL/ex/Codegenerator_Candidates.thy Sat Mar 06 11:21:09 2010 +0100 +++ b/src/HOL/ex/Codegenerator_Candidates.thy Sat Mar 06 15:31:30 2010 +0100 @@ -21,6 +21,7 @@ Product_ord "~~/src/HOL/ex/Records" SetsAndFunctions + Table Tree While_Combinator Word