src/HOL/Data_Structures/Tree_Map.thy
author nipkow
Wed Jun 13 15:24:20 2018 +0200 (10 months ago)
changeset 68440 6826718f732d
parent 68431 b294e095f64c
permissions -rw-r--r--
qualify interpretations to avoid clashes
nipkow@61640
     1
(* Author: Tobias Nipkow *)
nipkow@61640
     2
nipkow@63411
     3
section \<open>Unbalanced Tree Implementation of Map\<close>
nipkow@61640
     4
nipkow@61640
     5
theory Tree_Map
nipkow@61640
     6
imports
nipkow@61640
     7
  Tree_Set
nipkow@67965
     8
  Map_Specs
nipkow@61640
     9
begin
nipkow@61640
    10
nipkow@63411
    11
fun lookup :: "('a::linorder*'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@63411
    16
fun update :: "'a::linorder \<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@63411
    23
fun delete :: "'a::linorder \<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@68020
    28
  EQ \<Rightarrow> if r = Leaf then l else let (ab',r') = split_min r in Node l ab' r')"
nipkow@61640
    29
nipkow@61640
    30
nipkow@61640
    31
subsection "Functional Correctness Proofs"
nipkow@61640
    32
nipkow@61790
    33
lemma lookup_map_of:
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@68440
    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@68440
    41
lemma inorder_delete:
nipkow@61640
    42
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@68020
    43
by(induction t) (auto simp: del_list_simps split_minD split: prod.splits)
nipkow@61640
    44
nipkow@68440
    45
interpretation M: Map_by_Ordered
nipkow@68431
    46
where empty = empty 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@68431
    49
  case 1 show ?case by (simp add: empty_def)
nipkow@61640
    50
next
nipkow@61790
    51
  case 2 thus ?case by(simp add: lookup_map_of)
nipkow@61640
    52
next
nipkow@68440
    53
  case 3 thus ?case by(simp add: inorder_update)
nipkow@61640
    54
next
nipkow@68440
    55
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61686
    56
qed auto
nipkow@61640
    57
nipkow@61640
    58
end