src/HOL/Data_Structures/Tree_Set.thy
 author nipkow Sat Apr 21 08:41:42 2018 +0200 (14 months ago) changeset 68020 6aade817bee5 parent 67965 aaa31cd0caef child 68431 b294e095f64c permissions -rw-r--r--
del_min -> split_min
```     1 (* Author: Tobias Nipkow *)
```
```     2
```
```     3 section \<open>Unbalanced Tree Implementation of Set\<close>
```
```     4
```
```     5 theory Tree_Set
```
```     6 imports
```
```     7   "HOL-Library.Tree"
```
```     8   Cmp
```
```     9   Set_Specs
```
```    10 begin
```
```    11
```
```    12 fun isin :: "'a::linorder tree \<Rightarrow> 'a \<Rightarrow> bool" where
```
```    13 "isin Leaf x = False" |
```
```    14 "isin (Node l a r) x =
```
```    15   (case cmp x a of
```
```    16      LT \<Rightarrow> isin l x |
```
```    17      EQ \<Rightarrow> True |
```
```    18      GT \<Rightarrow> isin r x)"
```
```    19
```
```    20 hide_const (open) insert
```
```    21
```
```    22 fun insert :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
```
```    23 "insert x Leaf = Node Leaf x Leaf" |
```
```    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))"
```
```    29
```
```    30 fun split_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
```
```    31 "split_min (Node l a r) =
```
```    32   (if l = Leaf then (a,r) else let (x,l') = split_min l in (x, Node l' a r))"
```
```    33
```
```    34 fun delete :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
```
```    35 "delete x Leaf = Leaf" |
```
```    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') = split_min r in Node l a' r')"
```
```    41
```
```    42
```
```    43 subsection "Functional Correctness Proofs"
```
```    44
```
```    45 lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> set (inorder t))"
```
```    46 by (induction t) (auto simp: isin_simps)
```
```    47
```
```    48 lemma inorder_insert:
```
```    49   "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
```
```    50 by(induction t) (auto simp: ins_list_simps)
```
```    51
```
```    52
```
```    53 lemma split_minD:
```
```    54   "split_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> x # inorder t' = inorder t"
```
```    55 by(induction t arbitrary: t' rule: split_min.induct)
```
```    56   (auto simp: sorted_lems split: prod.splits if_splits)
```
```    57
```
```    58 lemma inorder_delete:
```
```    59   "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
```
```    60 by(induction t) (auto simp: del_list_simps split_minD split: prod.splits)
```
```    61
```
```    62 interpretation Set_by_Ordered
```
```    63 where empty = Leaf and isin = isin and insert = insert and delete = delete
```
```    64 and inorder = inorder and inv = "\<lambda>_. True"
```
```    65 proof (standard, goal_cases)
```
```    66   case 1 show ?case by simp
```
```    67 next
```
```    68   case 2 thus ?case by(simp add: isin_set)
```
```    69 next
```
```    70   case 3 thus ?case by(simp add: inorder_insert)
```
```    71 next
```
```    72   case 4 thus ?case by(simp add: inorder_delete)
```
```    73 qed (rule TrueI)+
```
```    74
```
```    75 end
```