30 the HOL level. *} |
30 the HOL level. *} |
31 |
31 |
32 subsection {* Distinctness of Nodes *} |
32 subsection {* Distinctness of Nodes *} |
33 |
33 |
34 |
34 |
35 consts set_of:: "'a tree \<Rightarrow> 'a set" |
35 primrec set_of :: "'a tree \<Rightarrow> 'a set" |
36 primrec |
36 where |
37 "set_of Tip = {}" |
37 "set_of Tip = {}" |
38 "set_of (Node l x d r) = (if d then {} else {x}) \<union> set_of l \<union> set_of r" |
38 | "set_of (Node l x d r) = (if d then {} else {x}) \<union> set_of l \<union> set_of r" |
39 |
39 |
40 consts all_distinct:: "'a tree \<Rightarrow> bool" |
40 primrec all_distinct :: "'a tree \<Rightarrow> bool" |
41 primrec |
41 where |
42 "all_distinct Tip = True" |
42 "all_distinct Tip = True" |
43 "all_distinct (Node l x d r) = ((d \<or> (x \<notin> set_of l \<and> x \<notin> set_of r)) \<and> |
43 | "all_distinct (Node l x d r) = |
44 set_of l \<inter> set_of r = {} \<and> |
44 ((d \<or> (x \<notin> set_of l \<and> x \<notin> set_of r)) \<and> |
45 all_distinct l \<and> all_distinct r)" |
45 set_of l \<inter> set_of r = {} \<and> |
|
46 all_distinct l \<and> all_distinct r)" |
46 |
47 |
47 text {* Given a binary tree @{term "t"} for which |
48 text {* Given a binary tree @{term "t"} for which |
48 @{const all_distinct} holds, given two different nodes contained in the tree, |
49 @{const all_distinct} holds, given two different nodes contained in the tree, |
49 we want to write a ML function that generates a logarithmic |
50 we want to write a ML function that generates a logarithmic |
50 certificate that the content of the nodes is distinct. We use the |
51 certificate that the content of the nodes is distinct. We use the |
97 of the order @{term "n * log(m)"} where @{term "n"} is the size of the |
98 of the order @{term "n * log(m)"} where @{term "n"} is the size of the |
98 (smaller) parent tree and @{term "m"} the size of the (bigger) new tree. |
99 (smaller) parent tree and @{term "m"} the size of the (bigger) new tree. |
99 *} |
100 *} |
100 |
101 |
101 |
102 |
102 consts "delete" :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree option" |
103 primrec delete :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree option" |
103 primrec |
104 where |
104 "delete x Tip = None" |
105 "delete x Tip = None" |
105 "delete x (Node l y d r) = (case delete x l of |
106 | "delete x (Node l y d r) = (case delete x l of |
106 Some l' \<Rightarrow> |
107 Some l' \<Rightarrow> |
107 (case delete x r of |
108 (case delete x r of |
108 Some r' \<Rightarrow> Some (Node l' y (d \<or> (x=y)) r') |
109 Some r' \<Rightarrow> Some (Node l' y (d \<or> (x=y)) r') |
109 | None \<Rightarrow> Some (Node l' y (d \<or> (x=y)) r)) |
110 | None \<Rightarrow> Some (Node l' y (d \<or> (x=y)) r)) |
110 | None \<Rightarrow> |
111 | None \<Rightarrow> |
111 (case (delete x r) of |
112 (case (delete x r) of |
112 Some r' \<Rightarrow> Some (Node l y (d \<or> (x=y)) r') |
113 Some r' \<Rightarrow> Some (Node l y (d \<or> (x=y)) r') |
113 | None \<Rightarrow> if x=y \<and> \<not>d then Some (Node l y True r) |
114 | None \<Rightarrow> if x=y \<and> \<not>d then Some (Node l y True r) |
114 else None))" |
115 else None))" |
115 |
116 |
116 |
117 |
117 lemma delete_Some_set_of: "\<And>t'. delete x t = Some t' \<Longrightarrow> set_of t' \<subseteq> set_of t" |
118 lemma delete_Some_set_of: "\<And>t'. delete x t = Some t' \<Longrightarrow> set_of t' \<subseteq> set_of t" |
118 proof (induct t) |
119 proof (induct t) |
119 case Tip thus ?case by simp |
120 case Tip thus ?case by simp |
291 qed |
292 qed |
292 qed |
293 qed |
293 qed |
294 qed |
294 |
295 |
295 |
296 |
296 consts "subtract" :: "'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree option" |
297 primrec subtract :: "'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree option" |
297 primrec |
298 where |
298 "subtract Tip t = Some t" |
299 "subtract Tip t = Some t" |
299 "subtract (Node l x b r) t = |
300 | "subtract (Node l x b r) t = |
300 (case delete x t of |
301 (case delete x t of |
301 Some t' \<Rightarrow> (case subtract l t' of |
302 Some t' \<Rightarrow> (case subtract l t' of |
302 Some t'' \<Rightarrow> subtract r t'' |
303 Some t'' \<Rightarrow> subtract r t'' |
303 | None \<Rightarrow> None) |
304 | None \<Rightarrow> None) |
304 | None \<Rightarrow> None)" |
305 | None \<Rightarrow> None)" |
305 |
306 |
306 lemma subtract_Some_set_of_res: |
307 lemma subtract_Some_set_of_res: |
307 "\<And>t\<^isub>2 t. subtract t\<^isub>1 t\<^isub>2 = Some t \<Longrightarrow> set_of t \<subseteq> set_of t\<^isub>2" |
308 "\<And>t\<^isub>2 t. subtract t\<^isub>1 t\<^isub>2 = Some t \<Longrightarrow> set_of t \<subseteq> set_of t\<^isub>2" |
308 proof (induct t\<^isub>1) |
309 proof (induct t\<^isub>1) |
309 case Tip thus ?case by simp |
310 case Tip thus ?case by simp |