| 61640 |      1 | (* Author: Tobias Nipkow *)
 | 
|  |      2 | 
 | 
|  |      3 | section {* Tree Implementation of Sets *}
 | 
|  |      4 | 
 | 
|  |      5 | theory Tree_Set
 | 
|  |      6 | imports
 | 
|  |      7 |   "~~/src/HOL/Library/Tree"
 | 
|  |      8 |   Cmp
 | 
|  |      9 |   Set_by_Ordered
 | 
|  |     10 | begin
 | 
|  |     11 | 
 | 
|  |     12 | fun isin :: "'a::cmp tree \<Rightarrow> 'a \<Rightarrow> bool" where
 | 
|  |     13 | "isin Leaf x = False" |
 | 
|  |     14 | "isin (Node l a r) x =
 | 
| 61678 |     15 |   (case cmp x a of
 | 
|  |     16 |      LT \<Rightarrow> isin l x |
 | 
|  |     17 |      EQ \<Rightarrow> True |
 | 
|  |     18 |      GT \<Rightarrow> isin r x)"
 | 
| 61640 |     19 | 
 | 
|  |     20 | hide_const (open) insert
 | 
|  |     21 | 
 | 
|  |     22 | fun insert :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
 | 
|  |     23 | "insert x Leaf = Node Leaf x Leaf" |
 | 
| 61678 |     24 | "insert x (Node l a r) =
 | 
|  |     25 |   (case cmp x a of
 | 
|  |     26 |      LT \<Rightarrow> Node (insert x l) a r |
 | 
|  |     27 |      EQ \<Rightarrow> Node l a r |
 | 
|  |     28 |      GT \<Rightarrow> Node l a (insert x r))"
 | 
| 61640 |     29 | 
 | 
|  |     30 | fun del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
 | 
| 61678 |     31 | "del_min (Node l a r) =
 | 
|  |     32 |   (if l = Leaf then (a,r) else let (x,l') = del_min l in (x, Node l' a r))"
 | 
| 61640 |     33 | 
 | 
|  |     34 | fun delete :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
 | 
|  |     35 | "delete x Leaf = Leaf" |
 | 
| 61678 |     36 | "delete x (Node l a r) =
 | 
|  |     37 |   (case cmp x a of
 | 
|  |     38 |      LT \<Rightarrow>  Node (delete x l) a r |
 | 
|  |     39 |      GT \<Rightarrow>  Node l a (delete x r) |
 | 
|  |     40 |      EQ \<Rightarrow> if r = Leaf then l else let (a',r') = del_min r in Node l a' r')"
 | 
| 61640 |     41 | 
 | 
|  |     42 | 
 | 
|  |     43 | subsection "Functional Correctness Proofs"
 | 
|  |     44 | 
 | 
|  |     45 | lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
 | 
|  |     46 | by (induction t) (auto simp: elems_simps1)
 | 
|  |     47 | 
 | 
|  |     48 | lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
 | 
|  |     49 | by (induction t) (auto simp: elems_simps2)
 | 
|  |     50 | 
 | 
|  |     51 | 
 | 
|  |     52 | lemma inorder_insert:
 | 
|  |     53 |   "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
 | 
|  |     54 | by(induction t) (auto simp: ins_list_simps)
 | 
|  |     55 | 
 | 
|  |     56 | 
 | 
|  |     57 | lemma del_minD:
 | 
| 61651 |     58 |   "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> x # inorder t' = inorder t"
 | 
| 61640 |     59 | by(induction t arbitrary: t' rule: del_min.induct)
 | 
| 61647 |     60 |   (auto simp: sorted_lems split: prod.splits if_splits)
 | 
| 61640 |     61 | 
 | 
|  |     62 | lemma inorder_delete:
 | 
|  |     63 |   "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
 | 
|  |     64 | by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
 | 
|  |     65 | 
 | 
|  |     66 | interpretation Set_by_Ordered
 | 
|  |     67 | where empty = Leaf and isin = isin and insert = insert and delete = delete
 | 
|  |     68 | and inorder = inorder and inv = "\<lambda>_. True"
 | 
|  |     69 | proof (standard, goal_cases)
 | 
|  |     70 |   case 1 show ?case by simp
 | 
|  |     71 | next
 | 
|  |     72 |   case 2 thus ?case by(simp add: isin_set)
 | 
|  |     73 | next
 | 
|  |     74 |   case 3 thus ?case by(simp add: inorder_insert)
 | 
|  |     75 | next
 | 
|  |     76 |   case 4 thus ?case by(simp add: inorder_delete)
 | 
|  |     77 | qed (rule TrueI)+
 | 
|  |     78 | 
 | 
|  |     79 | end
 |