src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Mon Nov 16 13:08:52 2015 +0100 (2015-11-16)
changeset 61686 e6784939d645
parent 61651 415e816d3f37
child 61790 0494964bb226
permissions -rw-r--r--
tuned names
nipkow@61640
     1
(* Author: Tobias Nipkow *)
nipkow@61640
     2
nipkow@61640
     3
section {* Unbalanced Tree as Map *}
nipkow@61640
     4
nipkow@61640
     5
theory Tree_Map
nipkow@61640
     6
imports
nipkow@61640
     7
  Tree_Set
nipkow@61640
     8
  Map_by_Ordered
nipkow@61640
     9
begin
nipkow@61640
    10
nipkow@61640
    11
fun lookup :: "('a::cmp*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
nipkow@61640
    12
"lookup Leaf x = None" |
nipkow@61640
    13
"lookup (Node l (a,b) r) x =
nipkow@61640
    14
  (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
nipkow@61640
    15
nipkow@61640
    16
fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
nipkow@61640
    17
"update x y Leaf = Node Leaf (x,y) Leaf" |
nipkow@61640
    18
"update x y (Node l (a,b) r) = (case cmp x a of
nipkow@61640
    19
   LT \<Rightarrow> Node (update x y l) (a,b) r |
nipkow@61640
    20
   EQ \<Rightarrow> Node l (x,y) r |
nipkow@61640
    21
   GT \<Rightarrow> Node l (a,b) (update x y r))"
nipkow@61640
    22
nipkow@61640
    23
fun delete :: "'a::cmp \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
nipkow@61640
    24
"delete x Leaf = Leaf" |
nipkow@61640
    25
"delete x (Node l (a,b) r) = (case cmp x a of
nipkow@61640
    26
  LT \<Rightarrow> Node (delete x l) (a,b) r |
nipkow@61640
    27
  GT \<Rightarrow> Node l (a,b) (delete x r) |
nipkow@61640
    28
  EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
nipkow@61640
    29
nipkow@61640
    30
nipkow@61640
    31
subsection "Functional Correctness Proofs"
nipkow@61640
    32
nipkow@61640
    33
lemma lookup_eq:
nipkow@61640
    34
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
nipkow@61640
    35
by (induction t) (auto simp: map_of_simps split: option.split)
nipkow@61640
    36
nipkow@61640
    37
lemma inorder_update:
nipkow@61640
    38
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
nipkow@61640
    39
by(induction t) (auto simp: upd_list_simps)
nipkow@61640
    40
nipkow@61640
    41
lemma inorder_delete:
nipkow@61640
    42
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61640
    43
by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
nipkow@61640
    44
nipkow@61640
    45
interpretation Map_by_Ordered
nipkow@61640
    46
where empty = Leaf and lookup = lookup and update = update and delete = delete
nipkow@61686
    47
and inorder = inorder and inv = "\<lambda>_. True"
nipkow@61640
    48
proof (standard, goal_cases)
nipkow@61640
    49
  case 1 show ?case by simp
nipkow@61640
    50
next
nipkow@61640
    51
  case 2 thus ?case by(simp add: lookup_eq)
nipkow@61640
    52
next
nipkow@61640
    53
  case 3 thus ?case by(simp add: inorder_update)
nipkow@61640
    54
next
nipkow@61640
    55
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61686
    56
qed auto
nipkow@61640
    57
nipkow@61640
    58
end