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