src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Mon, 21 Sep 2015 14:44:32 +0200
changeset 61203 a8a8eca85801
child 61224 759b5299a9f2
permissions -rw-r--r--
New subdirectory for functional data structures
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
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     7
  "~~/src/HOL/Library/Tree"
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 del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    24
"del_min (Node Leaf a r) = (a, r)" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    25
"del_min (Node l a r) = (let (x,l') = del_min l in (x, Node l' a r))"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    26
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    27
fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    28
"delete k Leaf = Leaf" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    29
"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
    30
  if k > a then Node l (a,b) (delete k r) else
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    31
  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
    32
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    33
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    34
subsection "Functional Correctness Proofs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    35
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    36
lemma lookup_eq: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    37
apply (induction t)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    38
apply (auto simp: sorted_lems map_of_append map_of_sorteds split: option.split)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    39
done
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 inorder_update:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    43
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    44
by(induction t) (auto simp: upd_list_sorteds sorted_lems)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    45
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    46
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    47
lemma del_minD:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    48
  "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
    49
   x # inorder t' = inorder t"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    50
by(induction t arbitrary: t' rule: del_min.induct)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    51
  (auto simp: sorted_lems split: prod.splits)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    52
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    53
lemma inorder_delete:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    54
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    55
by(induction t)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    56
  (auto simp: del_list_sorted sorted_lems dest!: del_minD split: prod.splits)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    57
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    58
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    59
interpretation Map_by_Ordered
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    60
where empty = Leaf and lookup = lookup and update = update and delete = delete
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    61
and inorder = inorder and wf = "\<lambda>_. True"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    62
proof (standard, goal_cases)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    63
  case 1 show ?case by simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    64
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    65
  case 2 thus ?case by(simp add: lookup_eq)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    66
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    67
  case 3 thus ?case by(simp add: inorder_update)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    68
next
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    69
  case 4 thus ?case by(simp add: inorder_delete)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    70
qed (rule TrueI)+
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    71
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    72
end