equal
deleted
inserted
replaced
1 (*<*)theory Partial = While_Combinator:(*>*) |
1 (*<*)theory Partial = While_Combinator:(*>*) |
2 |
2 |
3 text{*\noindent Throughout the tutorial we have emphasized the fact |
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 |
4 that all functions in HOL are total. Hence we cannot hope to define |
5 truly partial functions but must totalize them. A straightforward |
5 truly partial functions, but must make them total. A straightforward |
6 totalization is to lift the result type of the function from $\tau$ to |
6 method is to lift the result type of the function from $\tau$ to |
7 $\tau$~@{text option} (see \ref{sec:option}), where @{term None} is |
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 |
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. |
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 |
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 |
11 how it works. Its main drawback is that the result of such a lifted |
163 fst(while (\<lambda>(x,x'). x' \<noteq> x) (\<lambda>(x,x'). (x',f x')) (x,f x))" |
163 fst(while (\<lambda>(x,x'). x' \<noteq> x) (\<lambda>(x,x'). (x',f x')) (x,f x))" |
164 |
164 |
165 text{*\noindent |
165 text{*\noindent |
166 The loop operates on two ``local variables'' @{term x} and @{term x'} |
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}. |
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 |
168 They are initialized with the global @{term x} and @{term"f x"}. At the |
169 end @{term fst} selects the local @{term x}. |
169 end @{term fst} selects the local @{term x}. |
170 |
170 |
171 Although the definition of tail recursive functions via @{term while} avoids |
171 Although the definition of tail recursive functions via @{term while} avoids |
172 termination proofs, there is no free lunch. When proving properties of |
172 termination proofs, there is no free lunch. When proving properties of |
173 functions defined by @{term while}, termination rears its ugly head |
173 functions defined by @{term while}, termination rears its ugly head |
204 done |
204 done |
205 |
205 |
206 text{* Let us conclude this section on partial functions by a |
206 text{* Let us conclude this section on partial functions by a |
207 discussion of the merits of the @{term while} combinator. We have |
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 |
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 |
209 provide a termination argument when defining a function via @{term |
210 while} merely puts off the evil hour. On top of that, tail recursive |
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 |
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 |
212 @{term while} at all? The only reason is executability: the recursion |
213 equation for @{term while} is a directly executable functional |
213 equation for @{term while} is a directly executable functional |
214 program. This is in stark contrast to guarded recursion as introduced |
214 program. This is in stark contrast to guarded recursion as introduced |