src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Thu, 07 Jul 2016 18:08:02 +0200
changeset 63411 e051eea34990
parent 61790 0494964bb226
child 67965 aaa31cd0caef
permissions -rw-r--r--
got rid of class cmp; added height-size proofs by Daniel Stuewe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     2
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 61790
diff changeset
     3
section \<open>Unbalanced Tree Implementation of Map\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     5
theory Tree_Map
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     6
imports
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     7
  Tree_Set
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     8
  Map_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     9
begin
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    10
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 61790
diff changeset
    11
fun lookup :: "('a::linorder*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    12
"lookup Leaf x = None" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    13
"lookup (Node l (a,b) r) x =
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    14
  (case cmp x a of LT \<Rightarrow> lookup l x | GT \<Rightarrow> lookup r x | EQ \<Rightarrow> Some b)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    15
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 61790
diff changeset
    16
fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    17
"update x y Leaf = Node Leaf (x,y) Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    18
"update x y (Node l (a,b) r) = (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    19
   LT \<Rightarrow> Node (update x y l) (a,b) r |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    20
   EQ \<Rightarrow> Node l (x,y) r |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    21
   GT \<Rightarrow> Node l (a,b) (update x y r))"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    22
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 61790
diff changeset
    23
fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    24
"delete x Leaf = Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    25
"delete x (Node l (a,b) r) = (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    26
  LT \<Rightarrow> Node (delete x l) (a,b) r |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    27
  GT \<Rightarrow> Node l (a,b) (delete x r) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    28
  EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    29
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    30
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    31
subsection "Functional Correctness Proofs"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    32
61790
0494964bb226 avoid name clashes
nipkow
parents: 61686
diff changeset
    33
lemma lookup_map_of:
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    34
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    35
by (induction t) (auto simp: map_of_simps split: option.split)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    36
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    37
lemma inorder_update:
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    38
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    39
by(induction t) (auto simp: upd_list_simps)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    40
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    41
lemma inorder_delete:
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    42
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    43
by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    44
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    45
interpretation Map_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    46
where empty = Leaf and lookup = lookup and update = update and delete = delete
61686
e6784939d645 tuned names
nipkow
parents: 61651
diff changeset
    47
and inorder = inorder and inv = "\<lambda>_. True"
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    48
proof (standard, goal_cases)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    49
  case 1 show ?case by simp
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    50
next
61790
0494964bb226 avoid name clashes
nipkow
parents: 61686
diff changeset
    51
  case 2 thus ?case by(simp add: lookup_map_of)
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    52
next
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    53
  case 3 thus ?case by(simp add: inorder_update)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    54
next
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    55
  case 4 thus ?case by(simp add: inorder_delete)
61686
e6784939d645 tuned names
nipkow
parents: 61651
diff changeset
    56
qed auto
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    57
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    58
end