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