src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Thu Nov 05 08:27:14 2015 +0100 (2015-11-05)
changeset 61581 00d9682e8dd7
parent 61534 a88e07c8d0d5
child 61640 44c9198f210c
permissions -rw-r--r--
Convertd to 3-way comparisons
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@61231
     7
  Tree_Set
nipkow@61203
     8
  Map_by_Ordered
nipkow@61203
     9
begin
nipkow@61203
    10
nipkow@61581
    11
fun lookup :: "('a::cmp*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
nipkow@61203
    12
"lookup Leaf x = None" |
nipkow@61581
    13
"lookup (Node l (a,b) r) x =
nipkow@61581
    14
  (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
nipkow@61203
    15
nipkow@61581
    16
fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
nipkow@61534
    17
"update x y Leaf = Node Leaf (x,y) Leaf" |
nipkow@61581
    18
"update x y (Node l (a,b) r) = (case cmp x a of
nipkow@61581
    19
   LT \<Rightarrow> Node (update x y l) (a,b) r |
nipkow@61581
    20
   EQ \<Rightarrow> Node l (x,y) r |
nipkow@61581
    21
   GT \<Rightarrow> Node l (a,b) (update x y r))"
nipkow@61203
    22
nipkow@61581
    23
fun delete :: "'a::cmp \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
nipkow@61534
    24
"delete x Leaf = Leaf" |
nipkow@61581
    25
"delete x (Node l (a,b) r) = (case cmp x a of
nipkow@61581
    26
  LT \<Rightarrow> Node (delete x l) (a,b) r |
nipkow@61581
    27
  GT \<Rightarrow> Node l (a,b) (delete x r) |
nipkow@61581
    28
  EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
nipkow@61203
    29
nipkow@61203
    30
nipkow@61203
    31
subsection "Functional Correctness Proofs"
nipkow@61203
    32
nipkow@61224
    33
lemma lookup_eq:
nipkow@61224
    34
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
nipkow@61231
    35
by (induction t) (auto simp: map_of_simps split: option.split)
nipkow@61203
    36
nipkow@61203
    37
nipkow@61203
    38
lemma inorder_update:
nipkow@61203
    39
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
nipkow@61224
    40
by(induction t) (auto simp: upd_list_simps)
nipkow@61203
    41
nipkow@61203
    42
nipkow@61203
    43
lemma del_minD:
nipkow@61203
    44
  "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
nipkow@61203
    45
   x # inorder t' = inorder t"
nipkow@61203
    46
by(induction t arbitrary: t' rule: del_min.induct)
nipkow@61231
    47
  (auto simp: del_list_simps split: prod.splits)
nipkow@61203
    48
nipkow@61203
    49
lemma inorder_delete:
nipkow@61203
    50
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61231
    51
by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
nipkow@61203
    52
nipkow@61203
    53
interpretation Map_by_Ordered
nipkow@61203
    54
where empty = Leaf and lookup = lookup and update = update and delete = delete
nipkow@61203
    55
and inorder = inorder and wf = "\<lambda>_. True"
nipkow@61203
    56
proof (standard, goal_cases)
nipkow@61203
    57
  case 1 show ?case by simp
nipkow@61203
    58
next
nipkow@61203
    59
  case 2 thus ?case by(simp add: lookup_eq)
nipkow@61203
    60
next
nipkow@61203
    61
  case 3 thus ?case by(simp add: inorder_update)
nipkow@61203
    62
next
nipkow@61203
    63
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61203
    64
qed (rule TrueI)+
nipkow@61203
    65
nipkow@61203
    66
end