src/HOL/Library/Table.thy
author haftmann
Sat, 06 Mar 2010 15:31:30 +0100
changeset 35617 a6528fb99641
child 36111 5844017e31f8
permissions -rw-r--r--
added Table.thy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35617
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     2
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     3
header {* Tables: finite mappings implemented by red-black trees *}
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     4
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     5
theory Table
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     6
imports Main RBT
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     7
begin
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     8
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
     9
subsection {* Type definition *}
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    10
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    11
typedef (open) ('a, 'b) table = "{t :: ('a\<Colon>linorder, 'b) rbt. is_rbt t}"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    12
  morphisms tree_of Table
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    13
proof -
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    14
  have "RBT.Empty \<in> ?table" by simp
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    15
  then show ?thesis ..
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    16
qed
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    17
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    18
lemma is_rbt_tree_of [simp, intro]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    19
  "is_rbt (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    20
  using tree_of [of t] by simp
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    21
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    22
lemma table_eq:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    23
  "t1 = t2 \<longleftrightarrow> tree_of t1 = tree_of t2"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    24
  by (simp add: tree_of_inject)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    25
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    26
code_abstype Table tree_of
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    27
  by (simp add: tree_of_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    28
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    29
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    30
subsection {* Primitive operations *}
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    31
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    32
definition lookup :: "('a\<Colon>linorder, 'b) table \<Rightarrow> 'a \<rightharpoonup> 'b" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    33
  [code]: "lookup t = RBT.lookup (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    34
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    35
definition empty :: "('a\<Colon>linorder, 'b) table" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    36
  "empty = Table RBT.Empty"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    37
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    38
lemma tree_of_empty [code abstract]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    39
  "tree_of empty = RBT.Empty"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    40
  by (simp add: empty_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    41
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    42
definition update :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) table \<Rightarrow> ('a, 'b) table" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    43
  "update k v t = Table (RBT.insert k v (tree_of t))"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    44
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    45
lemma tree_of_update [code abstract]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    46
  "tree_of (update k v t) = RBT.insert k v (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    47
  by (simp add: update_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    48
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    49
definition delete :: "'a\<Colon>linorder \<Rightarrow> ('a, 'b) table \<Rightarrow> ('a, 'b) table" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    50
  "delete k t = Table (RBT.delete k (tree_of t))"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    51
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    52
lemma tree_of_delete [code abstract]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    53
  "tree_of (delete k t) = RBT.delete k (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    54
  by (simp add: delete_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    55
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    56
definition entries :: "('a\<Colon>linorder, 'b) table \<Rightarrow> ('a \<times> 'b) list" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    57
  [code]: "entries t = RBT.entries (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    58
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    59
definition bulkload :: "('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) table" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    60
  "bulkload xs = Table (RBT.bulkload xs)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    61
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    62
lemma tree_of_bulkload [code abstract]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    63
  "tree_of (bulkload xs) = RBT.bulkload xs"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    64
  by (simp add: bulkload_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    65
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    66
definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) table \<Rightarrow> ('a, 'b) table" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    67
  "map_entry k f t = Table (RBT.map_entry k f (tree_of t))"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    68
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    69
lemma tree_of_map_entry [code abstract]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    70
  "tree_of (map_entry k f t) = RBT.map_entry k f (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    71
  by (simp add: map_entry_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    72
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    73
definition map :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) table \<Rightarrow> ('a, 'b) table" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    74
  "map f t = Table (RBT.map f (tree_of t))"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    75
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    76
lemma tree_of_map [code abstract]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    77
  "tree_of (map f t) = RBT.map f (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    78
  by (simp add: map_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    79
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    80
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) table \<Rightarrow> 'c \<Rightarrow> 'c" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    81
  [code]: "fold f t = RBT.fold f (tree_of t)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    82
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    83
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    84
subsection {* Derived operations *}
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    85
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    86
definition is_empty :: "('a\<Colon>linorder, 'b) table \<Rightarrow> bool" where
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    87
  [code]: "is_empty t = (case tree_of t of RBT.Empty \<Rightarrow> True | _ \<Rightarrow> False)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    88
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    89
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    90
subsection {* Abstract lookup properties *}
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    91
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    92
lemma lookup_Table:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    93
  "is_rbt t \<Longrightarrow> lookup (Table t) = RBT.lookup t"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    94
  by (simp add: lookup_def Table_inverse)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    95
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    96
lemma lookup_tree_of:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    97
  "RBT.lookup (tree_of t) = lookup t"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    98
  by (simp add: lookup_def)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
    99
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   100
lemma entries_tree_of:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   101
  "RBT.entries (tree_of t) = entries t"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   102
  by (simp add: entries_def)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   103
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   104
lemma lookup_empty [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   105
  "lookup empty = Map.empty"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   106
  by (simp add: empty_def lookup_Table expand_fun_eq)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   107
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   108
lemma lookup_update [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   109
  "lookup (update k v t) = (lookup t)(k \<mapsto> v)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   110
  by (simp add: update_def lookup_Table lookup_insert lookup_tree_of)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   111
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   112
lemma lookup_delete [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   113
  "lookup (delete k t) = (lookup t)(k := None)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   114
  by (simp add: delete_def lookup_Table lookup_delete lookup_tree_of restrict_complement_singleton_eq)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   115
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   116
lemma map_of_entries [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   117
  "map_of (entries t) = lookup t"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   118
  by (simp add: entries_def map_of_entries lookup_tree_of)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   119
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   120
lemma lookup_bulkload [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   121
  "lookup (bulkload xs) = map_of xs"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   122
  by (simp add: bulkload_def lookup_Table lookup_bulkload)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   123
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   124
lemma lookup_map_entry [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   125
  "lookup (map_entry k f t) = (lookup t)(k := Option.map f (lookup t k))"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   126
  by (simp add: map_entry_def lookup_Table lookup_map_entry lookup_tree_of)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   127
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   128
lemma lookup_map [simp]:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   129
  "lookup (map f t) k = Option.map (f k) (lookup t k)"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   130
  by (simp add: map_def lookup_Table lookup_map lookup_tree_of)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   131
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   132
lemma fold_fold:
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   133
  "fold f t = (\<lambda>s. foldl (\<lambda>s (k, v). f k v s) s (entries t))"
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   134
  by (simp add: fold_def expand_fun_eq RBT.fold_def entries_tree_of)
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   135
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   136
hide (open) const tree_of lookup empty update delete
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   137
  entries bulkload map_entry map fold
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   138
a6528fb99641 added Table.thy
haftmann
parents:
diff changeset
   139
end