src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Wed, 23 Sep 2015 09:14:22 +0200
changeset 61231 cc6969542f8d
parent 61224 759b5299a9f2
child 61534 a88e07c8d0d5
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     2
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     3
section {* Unbalanced Tree as Map *}
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     4
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     5
theory Tree_Map
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     6
imports
61231
nipkow
parents: 61224
diff changeset
     7
  Tree_Set
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     8
  Map_by_Ordered
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     9
begin
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    10
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    11
fun lookup :: "('a::linorder*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    12
"lookup Leaf x = None" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    13
"lookup (Node l (a,b) r) x = (if x < a then lookup l x else
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    14
  if x > a then lookup r x else Some b)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    15
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    16
fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    17
"update a b Leaf = Node Leaf (a,b) Leaf" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    18
"update a b (Node l (x,y) r) =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    19
   (if a < x then Node (update a b l) (x,y) r
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    20
    else if a=x then Node l (a,b) r
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    21
    else Node l (x,y) (update a b r))"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    22
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    23
fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    24
"delete k Leaf = Leaf" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    25
"delete k (Node l (a,b) r) = (if k<a then Node (delete k l) (a,b) r else
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    26
  if k > a then Node l (a,b) (delete k r) else
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    27
  if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    28
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    29
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    30
subsection "Functional Correctness Proofs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    31
61224
759b5299a9f2 added red black trees
nipkow
parents: 61203
diff changeset
    32
lemma lookup_eq:
759b5299a9f2 added red black trees
nipkow
parents: 61203
diff changeset
    33
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
61231
nipkow
parents: 61224
diff changeset
    34
by (induction t) (auto simp: map_of_simps split: option.split)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    35
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    36
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    37
lemma inorder_update:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    38
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
61224
759b5299a9f2 added red black trees
nipkow
parents: 61203
diff changeset
    39
by(induction t) (auto simp: upd_list_simps)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    40
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    41
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    42
lemma del_minD:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    43
  "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    44
   x # inorder t' = inorder t"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    45
by(induction t arbitrary: t' rule: del_min.induct)
61231
nipkow
parents: 61224
diff changeset
    46
  (auto simp: del_list_simps split: prod.splits)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    47
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    48
lemma inorder_delete:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    49
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
61231
nipkow
parents: 61224
diff changeset
    50
by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    51
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    52
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    53
interpretation Map_by_Ordered
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    54
where empty = Leaf and lookup = lookup and update = update and delete = delete
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    55
and inorder = inorder and wf = "\<lambda>_. True"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    56
proof (standard, goal_cases)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    57
  case 1 show ?case by simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    58
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    59
  case 2 thus ?case by(simp add: lookup_eq)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    60
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    61
  case 3 thus ?case by(simp add: inorder_update)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    62
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    63
  case 4 thus ?case by(simp add: inorder_delete)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    64
qed (rule TrueI)+
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    65
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    66
end