src/HOL/Data_Structures/Tree_Map.thy
 changeset 61203 a8a8eca85801 child 61224 759b5299a9f2
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Data_Structures/Tree_Map.thy	Mon Sep 21 14:44:32 2015 +0200
1.3 @@ -0,0 +1,72 @@
1.4 +(* Author: Tobias Nipkow *)
1.5 +
1.6 +section {* Unbalanced Tree as Map *}
1.7 +
1.8 +theory Tree_Map
1.9 +imports
1.10 +  "~~/src/HOL/Library/Tree"
1.11 +  Map_by_Ordered
1.12 +begin
1.13 +
1.14 +fun lookup :: "('a::linorder*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
1.15 +"lookup Leaf x = None" |
1.16 +"lookup (Node l (a,b) r) x = (if x < a then lookup l x else
1.17 +  if x > a then lookup r x else Some b)"
1.18 +
1.19 +fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
1.20 +"update a b Leaf = Node Leaf (a,b) Leaf" |
1.21 +"update a b (Node l (x,y) r) =
1.22 +   (if a < x then Node (update a b l) (x,y) r
1.23 +    else if a=x then Node l (a,b) r
1.24 +    else Node l (x,y) (update a b r))"
1.25 +
1.26 +fun del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
1.27 +"del_min (Node Leaf a r) = (a, r)" |
1.28 +"del_min (Node l a r) = (let (x,l') = del_min l in (x, Node l' a r))"
1.29 +
1.30 +fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
1.31 +"delete k Leaf = Leaf" |
1.32 +"delete k (Node l (a,b) r) = (if k<a then Node (delete k l) (a,b) r else
1.33 +  if k > a then Node l (a,b) (delete k r) else
1.34 +  if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
1.35 +
1.36 +
1.37 +subsection "Functional Correctness Proofs"
1.38 +
1.39 +lemma lookup_eq: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
1.40 +apply (induction t)
1.41 +apply (auto simp: sorted_lems map_of_append map_of_sorteds split: option.split)
1.42 +done
1.43 +
1.44 +
1.45 +lemma inorder_update:
1.46 +  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
1.47 +by(induction t) (auto simp: upd_list_sorteds sorted_lems)
1.48 +
1.49 +
1.50 +lemma del_minD:
1.51 +  "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
1.52 +   x # inorder t' = inorder t"
1.53 +by(induction t arbitrary: t' rule: del_min.induct)
1.54 +  (auto simp: sorted_lems split: prod.splits)
1.55 +
1.56 +lemma inorder_delete:
1.57 +  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
1.58 +by(induction t)
1.59 +  (auto simp: del_list_sorted sorted_lems dest!: del_minD split: prod.splits)
1.60 +
1.61 +
1.62 +interpretation Map_by_Ordered
1.63 +where empty = Leaf and lookup = lookup and update = update and delete = delete
1.64 +and inorder = inorder and wf = "\<lambda>_. True"
1.65 +proof (standard, goal_cases)
1.66 +  case 1 show ?case by simp
1.67 +next
1.68 +  case 2 thus ?case by(simp add: lookup_eq)
1.69 +next
1.70 +  case 3 thus ?case by(simp add: inorder_update)
1.71 +next
1.72 +  case 4 thus ?case by(simp add: inorder_delete)
1.73 +qed (rule TrueI)+
1.74 +
1.75 +end
```