| author | wenzelm | 
| Tue, 26 Jan 2021 23:34:40 +0100 | |
| changeset 73194 | c0d6d57a9a31 | 
| parent 71829 | 6f2663df8374 | 
| permissions | -rw-r--r-- | 
| 61640 | 1 | (* Author: Tobias Nipkow *) | 
| 2 | ||
| 63411 
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
 nipkow parents: 
61678diff
changeset | 3 | section \<open>Unbalanced Tree Implementation of Set\<close> | 
| 61640 | 4 | |
| 5 | theory Tree_Set | |
| 6 | imports | |
| 66453 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
63411diff
changeset | 7 | "HOL-Library.Tree" | 
| 61640 | 8 | Cmp | 
| 67965 | 9 | Set_Specs | 
| 61640 | 10 | begin | 
| 11 | ||
| 68431 | 12 | definition empty :: "'a tree" where | 
| 71829 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 13 | "empty = Leaf" | 
| 68431 | 14 | |
| 63411 
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
 nipkow parents: 
61678diff
changeset | 15 | fun isin :: "'a::linorder tree \<Rightarrow> 'a \<Rightarrow> bool" where | 
| 61640 | 16 | "isin Leaf x = False" | | 
| 17 | "isin (Node l a r) x = | |
| 61678 | 18 | (case cmp x a of | 
| 19 | LT \<Rightarrow> isin l x | | |
| 20 | EQ \<Rightarrow> True | | |
| 21 | GT \<Rightarrow> isin r x)" | |
| 61640 | 22 | |
| 23 | hide_const (open) insert | |
| 24 | ||
| 63411 
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
 nipkow parents: 
61678diff
changeset | 25 | fun insert :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where | 
| 61640 | 26 | "insert x Leaf = Node Leaf x Leaf" | | 
| 61678 | 27 | "insert x (Node l a r) = | 
| 28 | (case cmp x a of | |
| 29 | LT \<Rightarrow> Node (insert x l) a r | | |
| 30 | EQ \<Rightarrow> Node l a r | | |
| 31 | GT \<Rightarrow> Node l a (insert x r))" | |
| 61640 | 32 | |
| 71463 | 33 | text \<open>Deletion by replacing:\<close> | 
| 34 | ||
| 68020 | 35 | fun split_min :: "'a tree \<Rightarrow> 'a * 'a tree" where | 
| 36 | "split_min (Node l a r) = | |
| 37 | (if l = Leaf then (a,r) else let (x,l') = split_min l in (x, Node l' a r))" | |
| 61640 | 38 | |
| 63411 
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
 nipkow parents: 
61678diff
changeset | 39 | fun delete :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where | 
| 61640 | 40 | "delete x Leaf = Leaf" | | 
| 61678 | 41 | "delete x (Node l a r) = | 
| 42 | (case cmp x a of | |
| 43 | LT \<Rightarrow> Node (delete x l) a r | | |
| 44 | GT \<Rightarrow> Node l a (delete x r) | | |
| 68020 | 45 | EQ \<Rightarrow> if r = Leaf then l else let (a',r') = split_min r in Node l a' r')" | 
| 61640 | 46 | |
| 71829 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 47 | text \<open>Deletion by joining:\<close> | 
| 71463 | 48 | |
| 71829 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 49 | fun join :: "('a::linorder)tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
 | 
| 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 50 | "join t Leaf = t" | | 
| 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 51 | "join Leaf t = t" | | 
| 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 52 | "join (Node t1 a t2) (Node t3 b t4) = | 
| 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 53 | (case join t2 t3 of | 
| 71463 | 54 | Leaf \<Rightarrow> Node t1 a (Node Leaf b t4) | | 
| 55 | Node u2 x u3 \<Rightarrow> Node (Node t1 a u2) x (Node u3 b t4))" | |
| 56 | ||
| 57 | fun delete2 :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where | |
| 58 | "delete2 x Leaf = Leaf" | | |
| 59 | "delete2 x (Node l a r) = | |
| 60 | (case cmp x a of | |
| 61 | LT \<Rightarrow> Node (delete2 x l) a r | | |
| 62 | GT \<Rightarrow> Node l a (delete2 x r) | | |
| 71829 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 63 | EQ \<Rightarrow> join l r)" | 
| 71463 | 64 | |
| 61640 | 65 | |
| 66 | subsection "Functional Correctness Proofs" | |
| 67 | ||
| 67929 | 68 | lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> set (inorder t))" | 
| 69 | by (induction t) (auto simp: isin_simps) | |
| 61640 | 70 | |
| 71 | lemma inorder_insert: | |
| 72 | "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)" | |
| 73 | by(induction t) (auto simp: ins_list_simps) | |
| 74 | ||
| 75 | ||
| 68020 | 76 | lemma split_minD: | 
| 77 | "split_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> x # inorder t' = inorder t" | |
| 78 | by(induction t arbitrary: t' rule: split_min.induct) | |
| 61647 | 79 | (auto simp: sorted_lems split: prod.splits if_splits) | 
| 61640 | 80 | |
| 81 | lemma inorder_delete: | |
| 82 | "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)" | |
| 68020 | 83 | by(induction t) (auto simp: del_list_simps split_minD split: prod.splits) | 
| 61640 | 84 | |
| 68440 | 85 | interpretation S: Set_by_Ordered | 
| 68431 | 86 | where empty = empty and isin = isin and insert = insert and delete = delete | 
| 61640 | 87 | and inorder = inorder and inv = "\<lambda>_. True" | 
| 88 | proof (standard, goal_cases) | |
| 68431 | 89 | case 1 show ?case by (simp add: empty_def) | 
| 61640 | 90 | next | 
| 91 | case 2 thus ?case by(simp add: isin_set) | |
| 92 | next | |
| 93 | case 3 thus ?case by(simp add: inorder_insert) | |
| 94 | next | |
| 95 | case 4 thus ?case by(simp add: inorder_delete) | |
| 96 | qed (rule TrueI)+ | |
| 97 | ||
| 71829 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 98 | lemma inorder_join: | 
| 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 99 | "inorder(join l r) = inorder l @ inorder r" | 
| 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 100 | by(induction l r rule: join.induct) (auto split: tree.split) | 
| 71463 | 101 | |
| 102 | lemma inorder_delete2: | |
| 103 | "sorted(inorder t) \<Longrightarrow> inorder(delete2 x t) = del_list x (inorder t)" | |
| 71829 
6f2663df8374
"app" -> "join" for uniformity with Join theory; tuned defs
 nipkow parents: 
71463diff
changeset | 104 | by(induction t) (auto simp: inorder_join del_list_simps) | 
| 71463 | 105 | |
| 106 | interpretation S2: Set_by_Ordered | |
| 107 | where empty = empty and isin = isin and insert = insert and delete = delete2 | |
| 108 | and inorder = inorder and inv = "\<lambda>_. True" | |
| 109 | proof (standard, goal_cases) | |
| 110 | case 1 show ?case by (simp add: empty_def) | |
| 111 | next | |
| 112 | case 2 thus ?case by(simp add: isin_set) | |
| 113 | next | |
| 114 | case 3 thus ?case by(simp add: inorder_insert) | |
| 115 | next | |
| 116 | case 4 thus ?case by(simp add: inorder_delete2) | |
| 117 | qed (rule TrueI)+ | |
| 118 | ||
| 61640 | 119 | end |