src/HOL/Data_Structures/Tree_Set.thy
 changeset 61203 a8a8eca85801 child 61229 0b9c45c4af29
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Data_Structures/Tree_Set.thy	Mon Sep 21 14:44:32 2015 +0200
1.3 @@ -0,0 +1,75 @@
1.4 +(* Author: Tobias Nipkow *)
1.5 +
1.6 +section {* Tree Implementation of Sets *}
1.7 +
1.8 +theory Tree_Set
1.9 +imports
1.10 +  "~~/src/HOL/Library/Tree"
1.11 +  Set_by_Ordered
1.12 +begin
1.13 +
1.14 +fun isin :: "'a::linorder tree \<Rightarrow> 'a \<Rightarrow> bool" where
1.15 +"isin Leaf x = False" |
1.16 +"isin (Node l a r) x = (x < a \<and> isin l x \<or> x=a \<or> isin r x)"
1.17 +
1.18 +hide_const (open) insert
1.19 +
1.20 +fun insert :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
1.21 +"insert a Leaf = Node Leaf a Leaf" |
1.22 +"insert a (Node l x r) =
1.23 +   (if a < x then Node (insert a l) x r
1.24 +    else if a=x then Node l x r
1.25 +    else Node l x (insert a r))"
1.26 +
1.27 +fun del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
1.28 +"del_min (Node Leaf a r) = (a, r)" |
1.29 +"del_min (Node l a r) = (let (x,l') = del_min l in (x, Node l' a r))"
1.30 +
1.31 +fun delete :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
1.32 +"delete k Leaf = Leaf" |
1.33 +"delete k (Node l a r) = (if k<a then Node (delete k l) a r else
1.34 +  if k > a then Node l a (delete k r) else
1.35 +  if r = Leaf then l else let (a',r') = del_min r in Node l a' r')"
1.36 +
1.37 +
1.38 +subsection "Functional Correctness Proofs"
1.39 +
1.40 +lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
1.41 +by (induction t) (auto simp: elems_simps)
1.42 +
1.43 +lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
1.44 +by (induction t) (auto simp: elems_simps0 dest: sortedD)
1.45 +
1.46 +
1.47 +lemma inorder_insert:
1.48 +  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
1.49 +by(induction t) (auto simp: ins_simps)
1.50 +
1.51 +
1.52 +lemma del_minD:
1.53 +  "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted(inorder t) \<Longrightarrow>
1.54 +   x # inorder t' = inorder t"
1.55 +by(induction t arbitrary: t' rule: del_min.induct)
1.56 +  (auto simp: sorted_lems split: prod.splits)
1.57 +
1.58 +lemma inorder_delete:
1.59 +  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
1.60 +by(induction t) (auto simp: del_simps del_minD split: prod.splits)
1.61 +
1.62 +
1.63 +interpretation Set_by_Ordered
1.64 +where empty = Leaf and isin = isin and insert = insert and delete = delete
1.65 +and inorder = inorder and wf = "\<lambda>_. True"
1.66 +proof (standard, goal_cases)
1.67 +  case 1 show ?case by simp
1.68 +next
1.69 +  case 2 thus ?case by(simp add: isin_set)
1.70 +next
1.71 +  case 3 thus ?case by(simp add: inorder_insert)
1.72 +next
1.73 +  case 4 thus ?case by(simp add: inorder_delete)
1.74 +next
1.75 +  case 5 thus ?case by(simp)
1.76 +qed
1.77 +
1.78 +end
```