src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Thu, 05 Nov 2015 08:27:14 +0100
changeset 61581 00d9682e8dd7
parent 61534 a88e07c8d0d5
child 61640 44c9198f210c
permissions -rw-r--r--
Convertd to 3-way comparisons
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
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    11
fun lookup :: "('a::cmp*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    12
"lookup Leaf x = None" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    13
"lookup (Node l (a,b) r) x =
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    14
  (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    15
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    16
fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    17
"update x y Leaf = Node Leaf (x,y) Leaf" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    18
"update x y (Node l (a,b) r) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    19
   LT \<Rightarrow> Node (update x y l) (a,b) r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    20
   EQ \<Rightarrow> Node l (x,y) r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    21
   GT \<Rightarrow> Node l (a,b) (update x y r))"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    22
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    23
fun delete :: "'a::cmp \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    24
"delete x Leaf = Leaf" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    25
"delete x (Node l (a,b) r) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    26
  LT \<Rightarrow> Node (delete x l) (a,b) r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    27
  GT \<Rightarrow> Node l (a,b) (delete x r) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61534
diff changeset
    28
  EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    29
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    30
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    31
subsection "Functional Correctness Proofs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    32
61224
759b5299a9f2 added red black trees
nipkow
parents: 61203
diff changeset
    33
lemma lookup_eq:
759b5299a9f2 added red black trees
nipkow
parents: 61203
diff changeset
    34
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
61231
nipkow
parents: 61224
diff changeset
    35
by (induction t) (auto simp: map_of_simps split: option.split)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    36
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    37
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    38
lemma inorder_update:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    39
  "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
    40
by(induction t) (auto simp: upd_list_simps)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    41
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    42
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    43
lemma del_minD:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    44
  "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
    45
   x # inorder t' = inorder t"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    46
by(induction t arbitrary: t' rule: del_min.induct)
61231
nipkow
parents: 61224
diff changeset
    47
  (auto simp: del_list_simps split: prod.splits)
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    48
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    49
lemma inorder_delete:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    50
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
61231
nipkow
parents: 61224
diff changeset
    51
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
    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