1 (* Title: HOL/Statespace/DistinctTreeProver.thy |
1 (* Title: HOL/Statespace/DistinctTreeProver.thy |
2 Author: Norbert Schirmer, TU Muenchen |
2 Author: Norbert Schirmer, TU Muenchen |
3 *) |
3 *) |
4 |
4 |
5 section {* Distinctness of Names in a Binary Tree \label{sec:DistinctTreeProver}*} |
5 section \<open>Distinctness of Names in a Binary Tree \label{sec:DistinctTreeProver}\<close> |
6 |
6 |
7 theory DistinctTreeProver |
7 theory DistinctTreeProver |
8 imports Main |
8 imports Main |
9 begin |
9 begin |
10 |
10 |
11 text {* A state space manages a set of (abstract) names and assumes |
11 text \<open>A state space manages a set of (abstract) names and assumes |
12 that the names are distinct. The names are stored as parameters of a |
12 that the names are distinct. The names are stored as parameters of a |
13 locale and distinctness as an assumption. The most common request is |
13 locale and distinctness as an assumption. The most common request is |
14 to proof distinctness of two given names. We maintain the names in a |
14 to proof distinctness of two given names. We maintain the names in a |
15 balanced binary tree and formulate a predicate that all nodes in the |
15 balanced binary tree and formulate a predicate that all nodes in the |
16 tree have distinct names. This setup leads to logarithmic certificates. |
16 tree have distinct names. This setup leads to logarithmic certificates. |
17 *} |
17 \<close> |
18 |
18 |
19 subsection {* The Binary Tree *} |
19 subsection \<open>The Binary Tree\<close> |
20 |
20 |
21 datatype 'a tree = Node "'a tree" 'a bool "'a tree" | Tip |
21 datatype 'a tree = Node "'a tree" 'a bool "'a tree" | Tip |
22 |
22 |
23 |
23 |
24 text {* The boolean flag in the node marks the content of the node as |
24 text \<open>The boolean flag in the node marks the content of the node as |
25 deleted, without having to build a new tree. We prefer the boolean |
25 deleted, without having to build a new tree. We prefer the boolean |
26 flag to an option type, so that the ML-layer can still use the node |
26 flag to an option type, so that the ML-layer can still use the node |
27 content to facilitate binary search in the tree. The ML code keeps the |
27 content to facilitate binary search in the tree. The ML code keeps the |
28 nodes sorted using the term order. We do not have to push ordering to |
28 nodes sorted using the term order. We do not have to push ordering to |
29 the HOL level. *} |
29 the HOL level.\<close> |
30 |
30 |
31 subsection {* Distinctness of Nodes *} |
31 subsection \<open>Distinctness of Nodes\<close> |
32 |
32 |
33 |
33 |
34 primrec set_of :: "'a tree \<Rightarrow> 'a set" |
34 primrec set_of :: "'a tree \<Rightarrow> 'a set" |
35 where |
35 where |
36 "set_of Tip = {}" |
36 "set_of Tip = {}" |
42 | "all_distinct (Node l x d r) = |
42 | "all_distinct (Node l x d r) = |
43 ((d \<or> (x \<notin> set_of l \<and> x \<notin> set_of r)) \<and> |
43 ((d \<or> (x \<notin> set_of l \<and> x \<notin> set_of r)) \<and> |
44 set_of l \<inter> set_of r = {} \<and> |
44 set_of l \<inter> set_of r = {} \<and> |
45 all_distinct l \<and> all_distinct r)" |
45 all_distinct l \<and> all_distinct r)" |
46 |
46 |
47 text {* Given a binary tree @{term "t"} for which |
47 text \<open>Given a binary tree @{term "t"} for which |
48 @{const all_distinct} holds, given two different nodes contained in the tree, |
48 @{const all_distinct} holds, given two different nodes contained in the tree, |
49 we want to write a ML function that generates a logarithmic |
49 we want to write a ML function that generates a logarithmic |
50 certificate that the content of the nodes is distinct. We use the |
50 certificate that the content of the nodes is distinct. We use the |
51 following lemmas to achieve this. *} |
51 following lemmas to achieve this.\<close> |
52 |
52 |
53 lemma all_distinct_left: "all_distinct (Node l x b r) \<Longrightarrow> all_distinct l" |
53 lemma all_distinct_left: "all_distinct (Node l x b r) \<Longrightarrow> all_distinct l" |
54 by simp |
54 by simp |
55 |
55 |
56 lemma all_distinct_right: "all_distinct (Node l x b r) \<Longrightarrow> all_distinct r" |
56 lemma all_distinct_right: "all_distinct (Node l x b r) \<Longrightarrow> all_distinct r" |
79 by blast |
79 by blast |
80 |
80 |
81 lemma neq_to_eq_False: "x\<noteq>y \<Longrightarrow> (x=y)\<equiv>False" |
81 lemma neq_to_eq_False: "x\<noteq>y \<Longrightarrow> (x=y)\<equiv>False" |
82 by simp |
82 by simp |
83 |
83 |
84 subsection {* Containment of Trees *} |
84 subsection \<open>Containment of Trees\<close> |
85 |
85 |
86 text {* When deriving a state space from other ones, we create a new |
86 text \<open>When deriving a state space from other ones, we create a new |
87 name tree which contains all the names of the parent state spaces and |
87 name tree which contains all the names of the parent state spaces and |
88 assume the predicate @{const all_distinct}. We then prove that the new |
88 assume the predicate @{const all_distinct}. We then prove that the new |
89 locale interprets all parent locales. Hence we have to show that the |
89 locale interprets all parent locales. Hence we have to show that the |
90 new distinctness assumption on all names implies the distinctness |
90 new distinctness assumption on all names implies the distinctness |
91 assumptions of the parent locales. This proof is implemented in ML. We |
91 assumptions of the parent locales. This proof is implemented in ML. We |
93 by ``subtraction''. We subtract the parent tree from the new tree. If |
93 by ``subtraction''. We subtract the parent tree from the new tree. If |
94 this succeeds we know that @{const all_distinct} of the new tree |
94 this succeeds we know that @{const all_distinct} of the new tree |
95 implies @{const all_distinct} of the parent tree. The resulting |
95 implies @{const all_distinct} of the parent tree. The resulting |
96 certificate is of the order @{term "n * log(m)"} where @{term "n"} is |
96 certificate is of the order @{term "n * log(m)"} where @{term "n"} is |
97 the size of the (smaller) parent tree and @{term "m"} the size of the |
97 the size of the (smaller) parent tree and @{term "m"} the size of the |
98 (bigger) new tree. *} |
98 (bigger) new tree.\<close> |
99 |
99 |
100 |
100 |
101 primrec delete :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree option" |
101 primrec delete :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree option" |
102 where |
102 where |
103 "delete x Tip = None" |
103 "delete x Tip = None" |