10225
|
1 |
(*<*)theory Star = Main:(*>*)
|
|
2 |
|
10898
|
3 |
section{*The Reflexive Transitive Closure*}
|
10225
|
4 |
|
10242
|
5 |
text{*\label{sec:rtc}
|
10898
|
6 |
An inductive definition may accept parameters, so it can express
|
|
7 |
functions that yield sets.
|
|
8 |
Relations too can be defined inductively, since they are just sets of pairs.
|
|
9 |
A perfect example is the function that maps a relation to its
|
|
10 |
reflexive transitive closure. This concept was already
|
10520
|
11 |
introduced in \S\ref{sec:Relations}, where the operator @{text"^*"} was
|
|
12 |
defined as a least fixed point because inductive definitions were not yet
|
|
13 |
available. But now they are:
|
10225
|
14 |
*}
|
|
15 |
|
10242
|
16 |
consts rtc :: "('a \<times> 'a)set \<Rightarrow> ('a \<times> 'a)set" ("_*" [1000] 999)
|
10225
|
17 |
inductive "r*"
|
|
18 |
intros
|
10242
|
19 |
rtc_refl[iff]: "(x,x) \<in> r*"
|
|
20 |
rtc_step: "\<lbrakk> (x,y) \<in> r; (y,z) \<in> r* \<rbrakk> \<Longrightarrow> (x,z) \<in> r*"
|
|
21 |
|
|
22 |
text{*\noindent
|
|
23 |
The function @{term rtc} is annotated with concrete syntax: instead of
|
10520
|
24 |
@{text"rtc r"} we can read and write @{term"r*"}. The actual definition
|
|
25 |
consists of two rules. Reflexivity is obvious and is immediately given the
|
|
26 |
@{text iff} attribute to increase automation. The
|
10363
|
27 |
second rule, @{thm[source]rtc_step}, says that we can always add one more
|
|
28 |
@{term r}-step to the left. Although we could make @{thm[source]rtc_step} an
|
10520
|
29 |
introduction rule, this is dangerous: the recursion in the second premise
|
|
30 |
slows down and may even kill the automatic tactics.
|
10242
|
31 |
|
|
32 |
The above definition of the concept of reflexive transitive closure may
|
|
33 |
be sufficiently intuitive but it is certainly not the only possible one:
|
10898
|
34 |
for a start, it does not even mention transitivity.
|
10242
|
35 |
The rest of this section is devoted to proving that it is equivalent to
|
10898
|
36 |
the standard definition. We start with a simple lemma:
|
10242
|
37 |
*}
|
10225
|
38 |
|
|
39 |
lemma [intro]: "(x,y) : r \<Longrightarrow> (x,y) \<in> r*"
|
10237
|
40 |
by(blast intro: rtc_step);
|
10225
|
41 |
|
10242
|
42 |
text{*\noindent
|
|
43 |
Although the lemma itself is an unremarkable consequence of the basic rules,
|
|
44 |
it has the advantage that it can be declared an introduction rule without the
|
|
45 |
danger of killing the automatic tactics because @{term"r*"} occurs only in
|
|
46 |
the conclusion and not in the premise. Thus some proofs that would otherwise
|
|
47 |
need @{thm[source]rtc_step} can now be found automatically. The proof also
|
10898
|
48 |
shows that @{text blast} is able to handle @{thm[source]rtc_step}. But
|
10242
|
49 |
some of the other automatic tactics are more sensitive, and even @{text
|
|
50 |
blast} can be lead astray in the presence of large numbers of rules.
|
|
51 |
|
10520
|
52 |
To prove transitivity, we need rule induction, i.e.\ theorem
|
|
53 |
@{thm[source]rtc.induct}:
|
|
54 |
@{thm[display]rtc.induct}
|
|
55 |
It says that @{text"?P"} holds for an arbitrary pair @{text"(?xb,?xa) \<in>
|
|
56 |
?r*"} if @{text"?P"} is preserved by all rules of the inductive definition,
|
|
57 |
i.e.\ if @{text"?P"} holds for the conclusion provided it holds for the
|
|
58 |
premises. In general, rule induction for an $n$-ary inductive relation $R$
|
|
59 |
expects a premise of the form $(x@1,\dots,x@n) \in R$.
|
|
60 |
|
|
61 |
Now we turn to the inductive proof of transitivity:
|
10242
|
62 |
*}
|
|
63 |
|
10520
|
64 |
lemma rtc_trans: "\<lbrakk> (x,y) \<in> r*; (y,z) \<in> r* \<rbrakk> \<Longrightarrow> (x,z) \<in> r*"
|
10363
|
65 |
apply(erule rtc.induct)
|
|
66 |
|
|
67 |
txt{*\noindent
|
10520
|
68 |
Unfortunately, even the resulting base case is a problem
|
10363
|
69 |
@{subgoals[display,indent=0,goals_limit=1]}
|
10242
|
70 |
and maybe not what you had expected. We have to abandon this proof attempt.
|
10520
|
71 |
To understand what is going on, let us look again at @{thm[source]rtc.induct}.
|
|
72 |
In the above application of @{text erule}, the first premise of
|
|
73 |
@{thm[source]rtc.induct} is unified with the first suitable assumption, which
|
|
74 |
is @{term"(x,y) \<in> r*"} rather than @{term"(y,z) \<in> r*"}. Although that
|
|
75 |
is what we want, it is merely due to the order in which the assumptions occur
|
|
76 |
in the subgoal, which it is not good practice to rely on. As a result,
|
|
77 |
@{text"?xb"} becomes @{term x}, @{text"?xa"} becomes
|
|
78 |
@{term y} and @{text"?P"} becomes @{term"%u v. (u,z) : r*"}, thus
|
10242
|
79 |
yielding the above subgoal. So what went wrong?
|
|
80 |
|
10520
|
81 |
When looking at the instantiation of @{text"?P"} we see that it does not
|
|
82 |
depend on its second parameter at all. The reason is that in our original
|
|
83 |
goal, of the pair @{term"(x,y)"} only @{term x} appears also in the
|
|
84 |
conclusion, but not @{term y}. Thus our induction statement is too
|
|
85 |
weak. Fortunately, it can easily be strengthened:
|
10363
|
86 |
transfer the additional premise @{prop"(y,z):r*"} into the conclusion:*}
|
|
87 |
(*<*)oops(*>*)
|
10242
|
88 |
lemma rtc_trans[rule_format]:
|
|
89 |
"(x,y) \<in> r* \<Longrightarrow> (y,z) \<in> r* \<longrightarrow> (x,z) \<in> r*"
|
|
90 |
|
|
91 |
txt{*\noindent
|
|
92 |
This is not an obscure trick but a generally applicable heuristic:
|
|
93 |
\begin{quote}\em
|
|
94 |
Whe proving a statement by rule induction on $(x@1,\dots,x@n) \in R$,
|
|
95 |
pull all other premises containing any of the $x@i$ into the conclusion
|
|
96 |
using $\longrightarrow$.
|
|
97 |
\end{quote}
|
|
98 |
A similar heuristic for other kinds of inductions is formulated in
|
|
99 |
\S\ref{sec:ind-var-in-prems}. The @{text rule_format} directive turns
|
|
100 |
@{text"\<longrightarrow>"} back into @{text"\<Longrightarrow>"}. Thus in the end we obtain the original
|
|
101 |
statement of our lemma.
|
|
102 |
*}
|
|
103 |
|
10363
|
104 |
apply(erule rtc.induct)
|
|
105 |
|
|
106 |
txt{*\noindent
|
|
107 |
Now induction produces two subgoals which are both proved automatically:
|
|
108 |
@{subgoals[display,indent=0]}
|
|
109 |
*}
|
|
110 |
|
10225
|
111 |
apply(blast);
|
10237
|
112 |
apply(blast intro: rtc_step);
|
10225
|
113 |
done
|
|
114 |
|
10242
|
115 |
text{*
|
|
116 |
Let us now prove that @{term"r*"} is really the reflexive transitive closure
|
|
117 |
of @{term r}, i.e.\ the least reflexive and transitive
|
|
118 |
relation containing @{term r}. The latter is easily formalized
|
|
119 |
*}
|
10225
|
120 |
|
10237
|
121 |
consts rtc2 :: "('a \<times> 'a)set \<Rightarrow> ('a \<times> 'a)set"
|
|
122 |
inductive "rtc2 r"
|
10225
|
123 |
intros
|
10237
|
124 |
"(x,y) \<in> r \<Longrightarrow> (x,y) \<in> rtc2 r"
|
|
125 |
"(x,x) \<in> rtc2 r"
|
|
126 |
"\<lbrakk> (x,y) \<in> rtc2 r; (y,z) \<in> rtc2 r \<rbrakk> \<Longrightarrow> (x,z) \<in> rtc2 r"
|
10225
|
127 |
|
10242
|
128 |
text{*\noindent
|
|
129 |
and the equivalence of the two definitions is easily shown by the obvious rule
|
10237
|
130 |
inductions:
|
|
131 |
*}
|
10225
|
132 |
|
10237
|
133 |
lemma "(x,y) \<in> rtc2 r \<Longrightarrow> (x,y) \<in> r*"
|
|
134 |
apply(erule rtc2.induct);
|
10225
|
135 |
apply(blast);
|
10237
|
136 |
apply(blast);
|
|
137 |
apply(blast intro: rtc_trans);
|
|
138 |
done
|
|
139 |
|
|
140 |
lemma "(x,y) \<in> r* \<Longrightarrow> (x,y) \<in> rtc2 r"
|
|
141 |
apply(erule rtc.induct);
|
|
142 |
apply(blast intro: rtc2.intros);
|
|
143 |
apply(blast intro: rtc2.intros);
|
10225
|
144 |
done
|
|
145 |
|
10242
|
146 |
text{*
|
|
147 |
So why did we start with the first definition? Because it is simpler. It
|
|
148 |
contains only two rules, and the single step rule is simpler than
|
|
149 |
transitivity. As a consequence, @{thm[source]rtc.induct} is simpler than
|
10898
|
150 |
@{thm[source]rtc2.induct}. Since inductive proofs are hard enough
|
|
151 |
anyway, we should
|
|
152 |
certainly pick the simplest induction schema available.
|
10242
|
153 |
Hence @{term rtc} is the definition of choice.
|
|
154 |
|
10520
|
155 |
\begin{exercise}\label{ex:converse-rtc-step}
|
10242
|
156 |
Show that the converse of @{thm[source]rtc_step} also holds:
|
|
157 |
@{prop[display]"[| (x,y) : r*; (y,z) : r |] ==> (x,z) : r*"}
|
|
158 |
\end{exercise}
|
10520
|
159 |
\begin{exercise}
|
|
160 |
Repeat the development of this section, but starting with a definition of
|
|
161 |
@{term rtc} where @{thm[source]rtc_step} is replaced by its converse as shown
|
|
162 |
in exercise~\ref{ex:converse-rtc-step}.
|
|
163 |
\end{exercise}
|
10242
|
164 |
*}
|
|
165 |
(*<*)
|
|
166 |
lemma rtc_step2[rule_format]: "(x,y) : r* \<Longrightarrow> (y,z) : r --> (x,z) : r*"
|
|
167 |
apply(erule rtc.induct);
|
|
168 |
apply blast;
|
|
169 |
apply(blast intro:rtc_step)
|
|
170 |
done
|
|
171 |
|
|
172 |
end
|
|
173 |
(*>*)
|