10654
|
1 |
(*<*)theory Partial = While_Combinator:(*>*)
|
|
2 |
|
11277
|
3 |
text{*\noindent Throughout the tutorial we have emphasized the fact
|
|
4 |
that all functions in HOL are total. Hence we cannot hope to define
|
|
5 |
truly partial functions but must totalize them. A straightforward
|
|
6 |
totalization is to lift the result type of the function from $\tau$ to
|
|
7 |
$\tau$~@{text option} (see \ref{sec:option}), where @{term None} is
|
|
8 |
returned if the function is applied to an argument not in its
|
|
9 |
domain. Function @{term assoc} in \S\ref{sec:Trie} is a simple example.
|
|
10 |
We do not pursue this schema further because it should be clear
|
|
11 |
how it works. Its main drawback is that the result of such a lifted
|
|
12 |
function has to be unpacked first before it can be processed
|
|
13 |
further. Its main advantage is that you can distinguish if the
|
|
14 |
function was applied to an argument in its domain or not. If you do
|
|
15 |
not need to make this distinction, for example because the function is
|
|
16 |
never used outside its domain, it is easier to work with
|
|
17 |
\emph{underdefined}\index{underdefined function} functions: for
|
|
18 |
certain arguments we only know that a result exists, but we do not
|
|
19 |
know what it is. When defining functions that are normally considered
|
|
20 |
partial, underdefinedness turns out to be a very reasonable
|
|
21 |
alternative.
|
10654
|
22 |
|
|
23 |
We have already seen an instance of underdefinedness by means of
|
|
24 |
non-exhaustive pattern matching: the definition of @{term last} in
|
|
25 |
\S\ref{sec:recdef-examples}. The same is allowed for \isacommand{primrec}
|
|
26 |
*}
|
|
27 |
|
|
28 |
consts hd :: "'a list \<Rightarrow> 'a"
|
|
29 |
primrec "hd (x#xs) = x"
|
|
30 |
|
|
31 |
text{*\noindent
|
|
32 |
although it generates a warning.
|
|
33 |
Even ordinary definitions allow underdefinedness, this time by means of
|
|
34 |
preconditions:
|
|
35 |
*}
|
|
36 |
|
|
37 |
constdefs minus :: "nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
38 |
"n \<le> m \<Longrightarrow> minus m n \<equiv> m - n"
|
|
39 |
|
|
40 |
text{*
|
|
41 |
The rest of this section is devoted to the question of how to define
|
11256
|
42 |
partial recursive functions by other means than non-exhaustive pattern
|
10654
|
43 |
matching.
|
|
44 |
*}
|
|
45 |
|
10885
|
46 |
subsubsection{*Guarded Recursion*}
|
10654
|
47 |
|
|
48 |
text{* Neither \isacommand{primrec} nor \isacommand{recdef} allow to
|
|
49 |
prefix an equation with a condition in the way ordinary definitions do
|
|
50 |
(see @{term minus} above). Instead we have to move the condition over
|
|
51 |
to the right-hand side of the equation. Given a partial function $f$
|
|
52 |
that should satisfy the recursion equation $f(x) = t$ over its domain
|
|
53 |
$dom(f)$, we turn this into the \isacommand{recdef}
|
|
54 |
@{prop[display]"f(x) = (if x \<in> dom(f) then t else arbitrary)"}
|
|
55 |
where @{term arbitrary} is a predeclared constant of type @{typ 'a}
|
|
56 |
which has no definition. Thus we know nothing about its value,
|
|
57 |
which is ideal for specifying underdefined functions on top of it.
|
|
58 |
|
|
59 |
As a simple example we define division on @{typ nat}:
|
|
60 |
*}
|
|
61 |
|
|
62 |
consts divi :: "nat \<times> nat \<Rightarrow> nat"
|
|
63 |
recdef divi "measure(\<lambda>(m,n). m)"
|
|
64 |
"divi(m,n) = (if n = 0 then arbitrary else
|
|
65 |
if m < n then 0 else divi(m-n,n)+1)"
|
|
66 |
|
|
67 |
text{*\noindent Of course we could also have defined
|
|
68 |
@{term"divi(m,0)"} to be some specific number, for example 0. The
|
|
69 |
latter option is chosen for the predefined @{text div} function, which
|
10885
|
70 |
simplifies proofs at the expense of deviating from the
|
|
71 |
standard mathematical division function.
|
10654
|
72 |
|
|
73 |
As a more substantial example we consider the problem of searching a graph.
|
11277
|
74 |
For simplicity our graph is given by a function @{term f} of
|
10654
|
75 |
type @{typ"'a \<Rightarrow> 'a"} which
|
11196
|
76 |
maps each node to its successor, i.e.\ the graph is really a tree.
|
|
77 |
The task is to find the end of a chain, modelled by a node pointing to
|
|
78 |
itself. Here is a first attempt:
|
10654
|
79 |
@{prop[display]"find(f,x) = (if f x = x then x else find(f, f x))"}
|
|
80 |
This may be viewed as a fixed point finder or as one half of the well known
|
|
81 |
\emph{Union-Find} algorithm.
|
11149
|
82 |
The snag is that it may not terminate if @{term f} has non-trivial cycles.
|
10654
|
83 |
Phrased differently, the relation
|
|
84 |
*}
|
|
85 |
|
|
86 |
constdefs step1 :: "('a \<Rightarrow> 'a) \<Rightarrow> ('a \<times> 'a)set"
|
|
87 |
"step1 f \<equiv> {(y,x). y = f x \<and> y \<noteq> x}"
|
|
88 |
|
|
89 |
text{*\noindent
|
|
90 |
must be well-founded. Thus we make the following definition:
|
|
91 |
*}
|
|
92 |
|
|
93 |
consts find :: "('a \<Rightarrow> 'a) \<times> 'a \<Rightarrow> 'a"
|
|
94 |
recdef find "same_fst (\<lambda>f. wf(step1 f)) step1"
|
|
95 |
"find(f,x) = (if wf(step1 f)
|
|
96 |
then if f x = x then x else find(f, f x)
|
|
97 |
else arbitrary)"
|
11285
|
98 |
(hints recdef_simp: step1_def)
|
10654
|
99 |
|
|
100 |
text{*\noindent
|
|
101 |
The recursion equation itself should be clear enough: it is our aborted
|
|
102 |
first attempt augmented with a check that there are no non-trivial loops.
|
11277
|
103 |
To express the required well-founded relation we employ the
|
11196
|
104 |
predefined combinator @{term same_fst} of type
|
10654
|
105 |
@{text[display]"('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> ('b\<times>'b)set) \<Rightarrow> (('a\<times>'b) \<times> ('a\<times>'b))set"}
|
|
106 |
defined as
|
|
107 |
@{thm[display]same_fst_def[no_vars]}
|
11196
|
108 |
This combinator is designed for
|
|
109 |
recursive functions on pairs where the first component of the argument is
|
|
110 |
passed unchanged to all recursive calls. Given a constraint on the first
|
|
111 |
component and a relation on the second component, @{term same_fst} builds the
|
|
112 |
required relation on pairs. The theorem
|
|
113 |
@{thm[display]wf_same_fst[no_vars]}
|
|
114 |
is known to the well-foundedness prover of \isacommand{recdef}. Thus
|
|
115 |
well-foundedness of the relation given to \isacommand{recdef} is immediate.
|
|
116 |
Furthermore, each recursive call descends along that relation: the first
|
|
117 |
argument stays unchanged and the second one descends along @{term"step1
|
11285
|
118 |
f"}. The proof requires unfolding the definition of @{term step1},
|
|
119 |
as specified in the \isacommand{hints} above.
|
10654
|
120 |
|
|
121 |
Normally you will then derive the following conditional variant of and from
|
|
122 |
the recursion equation
|
|
123 |
*}
|
|
124 |
|
|
125 |
lemma [simp]:
|
|
126 |
"wf(step1 f) \<Longrightarrow> find(f,x) = (if f x = x then x else find(f, f x))"
|
|
127 |
by simp
|
|
128 |
|
|
129 |
text{*\noindent and then disable the original recursion equation:*}
|
|
130 |
|
|
131 |
declare find.simps[simp del]
|
|
132 |
|
|
133 |
text{*
|
|
134 |
We can reason about such underdefined functions just like about any other
|
|
135 |
recursive function. Here is a simple example of recursion induction:
|
|
136 |
*}
|
|
137 |
|
|
138 |
lemma "wf(step1 f) \<longrightarrow> f(find(f,x)) = find(f,x)"
|
|
139 |
apply(induct_tac f x rule:find.induct);
|
|
140 |
apply simp
|
|
141 |
done
|
|
142 |
|
10885
|
143 |
subsubsection{*The {\tt\slshape while} Combinator*}
|
10654
|
144 |
|
|
145 |
text{*If the recursive function happens to be tail recursive, its
|
|
146 |
definition becomes a triviality if based on the predefined \isaindexbold{while}
|
10885
|
147 |
combinator. The latter lives in the Library theory
|
10654
|
148 |
\isa{While_Combinator}, which is not part of @{text Main} but needs to
|
|
149 |
be included explicitly among the ancestor theories.
|
|
150 |
|
|
151 |
Constant @{term while} is of type @{text"('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a"}
|
|
152 |
and satisfies the recursion equation @{thm[display]while_unfold[no_vars]}
|
|
153 |
That is, @{term"while b c s"} is equivalent to the imperative program
|
|
154 |
\begin{verbatim}
|
|
155 |
x := s; while b(x) do x := c(x); return x
|
|
156 |
\end{verbatim}
|
|
157 |
In general, @{term s} will be a tuple (better still: a record). As an example
|
11157
|
158 |
consider the following definition of function @{term find} above:
|
10654
|
159 |
*}
|
|
160 |
|
|
161 |
constdefs find2 :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
|
|
162 |
"find2 f x \<equiv>
|
|
163 |
fst(while (\<lambda>(x,x'). x' \<noteq> x) (\<lambda>(x,x'). (x',f x')) (x,f x))"
|
|
164 |
|
|
165 |
text{*\noindent
|
|
166 |
The loop operates on two ``local variables'' @{term x} and @{term x'}
|
|
167 |
containing the ``current'' and the ``next'' value of function @{term f}.
|
|
168 |
They are initalized with the global @{term x} and @{term"f x"}. At the
|
|
169 |
end @{term fst} selects the local @{term x}.
|
|
170 |
|
11158
|
171 |
Although the definition of tail recursive functions via @{term while} avoids
|
|
172 |
termination proofs, there is no free lunch. When proving properties of
|
|
173 |
functions defined by @{term while}, termination rears its ugly head
|
|
174 |
again. Here is @{thm[source]while_rule}, the well known proof rule for total
|
10654
|
175 |
correctness of loops expressed with @{term while}:
|
11158
|
176 |
@{thm[display,margin=50]while_rule[no_vars]} @{term P} needs to be true of
|
|
177 |
the initial state @{term s} and invariant under @{term c} (premises 1
|
|
178 |
and~2). The post-condition @{term Q} must become true when leaving the loop
|
|
179 |
(premise~3). And each loop iteration must descend along a well-founded
|
|
180 |
relation @{term r} (premises 4 and~5).
|
10654
|
181 |
|
|
182 |
Let us now prove that @{term find2} does indeed find a fixed point. Instead
|
|
183 |
of induction we apply the above while rule, suitably instantiated.
|
|
184 |
Only the final premise of @{thm[source]while_rule} is left unproved
|
|
185 |
by @{text auto} but falls to @{text simp}:
|
|
186 |
*}
|
|
187 |
|
11277
|
188 |
lemma lem: "wf(step1 f) \<Longrightarrow>
|
|
189 |
\<exists>y. while (\<lambda>(x,x'). x' \<noteq> x) (\<lambda>(x,x'). (x',f x')) (x,f x) = (y,y) \<and>
|
10885
|
190 |
f y = y"
|
10654
|
191 |
apply(rule_tac P = "\<lambda>(x,x'). x' = f x" and
|
|
192 |
r = "inv_image (step1 f) fst" in while_rule);
|
|
193 |
apply auto
|
|
194 |
apply(simp add:inv_image_def step1_def)
|
|
195 |
done
|
|
196 |
|
|
197 |
text{*
|
|
198 |
The theorem itself is a simple consequence of this lemma:
|
|
199 |
*}
|
|
200 |
|
|
201 |
theorem "wf(step1 f) \<Longrightarrow> f(find2 f x) = find2 f x"
|
|
202 |
apply(drule_tac x = x in lem)
|
|
203 |
apply(auto simp add:find2_def)
|
|
204 |
done
|
|
205 |
|
|
206 |
text{* Let us conclude this section on partial functions by a
|
|
207 |
discussion of the merits of the @{term while} combinator. We have
|
|
208 |
already seen that the advantage (if it is one) of not having to
|
|
209 |
provide a termintion argument when defining a function via @{term
|
|
210 |
while} merely puts off the evil hour. On top of that, tail recursive
|
|
211 |
functions tend to be more complicated to reason about. So why use
|
|
212 |
@{term while} at all? The only reason is executability: the recursion
|
|
213 |
equation for @{term while} is a directly executable functional
|
|
214 |
program. This is in stark contrast to guarded recursion as introduced
|
|
215 |
above which requires an explicit test @{prop"x \<in> dom f"} in the
|
|
216 |
function body. Unless @{term dom} is trivial, this leads to a
|
11196
|
217 |
definition that is impossible to execute or prohibitively slow.
|
10885
|
218 |
Thus, if you are aiming for an efficiently executable definition
|
10654
|
219 |
of a partial function, you are likely to need @{term while}.
|
|
220 |
*}
|
|
221 |
|
|
222 |
(*<*)end(*>*)
|