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