Chapter 6 Pattern Matching for Dependent Types
6.2 interactive pattern matching in O LEG
6.2.1 computational aspects of elimination
Suppose a function can be given in terms of anotherÈ as follows:
Y ^ D ^ ¥ JRÈ ^
What can we infer about the computational behaviour of from that of È ?
This is a very common situation. If È is an elimination rule and we construct by
eliminating some of its arguments withÈ , this is exactly the structure which will take.
If È has a reduction behaviour given by Y-reductions or a pattern matching function in
the Coquand style, we may be able to infer the corresponding behaviour for . For example, we have already seen how to construct 88(9@;A=?> from 88(§fg¨ in this way:
how does88(9@;4=?> reduce when it is fed constructor-headed numbers? It is not hard to
check that it inherits the appropriate behaviour from88:§pfg¨ :
88(9@;A=?> C {ø4{ù S ÿ {ø 88(9@;A=?> C {ø4{ù = G ÿ {ù G
Similarly, if we want to implement the pattern matching version of¤
f¥y= by means of 88:§pfg¨ , we need to be sure that the defining equations are intensionally recoverable.
In particular, we need to show that any recursive calls to88:§pfg¨ in the implementation
can be replaced by recursive calls to ¤
f¥y= convertible to them. We can achieve this
by a process of unfold/fold transformation on functional programs which respects the computational equality of OLEG.
Let us consider unfolding first.
SupposeÈ is given by a pattern matching program
È DsE^ w D ^ J ù È ^ d
Ãd (over pattern variables
^ w D ^ d)
From the definition of , we can infer the lengthened equation ^ ÿ È ^ (any ^ D ^ ¥ ) For each^
d, there are two possibilities
Á ^ is at least as long as^ d Á ^ is shorter than^ d
In the former case, we may split ^
as ^ x ^ C , so that ^ ^ d D ^ . If ª is a substitution from ^ yx ^ w to terms over ^ u D ^ ¸ which unifies ^ d and ^ , then we have ª ^ ÿ Èúª ^ cx ^ C Èúª ^ d x ª ^ C ÿ ªÃd ª ^ C (over^ u D ^ ¸ )
In the latter case, it is ^
d which we split as ^ x ^ C so that^ ^ D ^ û , where ^ û is a prefix of ^ . Ifª is a unifying substitution, then we have
ª ^ ÿ Èúª ^ Èúª ^ and therefore ª ^ yx ª ^ C ÿ Èúª ^ cx ^ C Èvª ^ d ÿ ªjÃd
Note that we may not, in general, pad out the application of before the unification, because ^
may not have functional type until the ^
have been instantiated.
Folding is more straightforward. If we know that
^ ÿ where ^
is the free pattern, and
]^ ÿ Ã ª 7 (over ^ w D ^ ) then ^ ÿ Ã ª ^ p (over^ w D ^ )
I have not explained where these substitutions ª come from,
6 but I do not have to:
unfolding and folding are a pair of techniques by which we can derive new intensional equations from old ones. I do not propose to use them to construct pattern matching programs, but rather to confirm their intensional status. For example, the program¤
f¥y=
may be written in pattern matching notation
¤ f¥y= S w w ¤ f¥y= = w = ¤ f¥y= ÙwR
This quite clearly falls within Coquand’s class of definable functions. We have already seen¤
f¥¦= defined somewhat less perspicuously in OLEG:
¤ f¥y= 88(§fg¨ Y D 88ªJM88&«88 Y w D 88ªJ wR Y D 88ªJFY ¤ f¥y= ³ D 88&«88ªJ*Y w D 88ªJV= ¤ f¥¦= ³ wRB
We can check that the pattern matching equations hold intensionally for the OLEG
definition. First unfolding with respect to each Y-reduction of88(§pfg¨ :
¤ f¥y= S Y w D 88ªJ w ¤ f¥y= = Y w D 88ªJM= 88(§pfg¨ J7JMJ ÙwR
Folding with respect to the OLEG definition:
¤ f¥y= S Y w D 88ªJ w ¤ f¥y= = Y w D 88ªJM= ¤ f¥y= ÙwR Lengthening: ¤ f¥y= S w w ¤ f¥y= = w = ¤ f¥y= ÙwR
We have checked our implementation of the pattern matching program!
In fact, we can use lengthening, unfolding and folding to check all the dervived com- putation laws in this thesis, and we shall use them in particular to ensure the intensional validity of the pattern matching programs we shall shortly construct.
Of particular interest is the computational effect of case analysis followed by unifica- tion.
Suppose we face the goal
X íDE ^ D ^ J ù where d is some ZF[A\ ^ z , with the ^ z
in constructor form. Let
ZF[A\
^ u D ¸ 9@ ^ u D ZF[4\ ^ z Eliminating d by ZF[A\[4? yields, in general: C Y ^ w D ZF[A\ E^ D ^ E^ D^ w ^ zjx1 d ù .. . X DeE ^ u D ^ ¸ E^ D ^ Ey^ DH^ z xF 9@ ^ u~ ^ zyx1 d ù .. . Y ^ D ^ Z][4\([AB C J7JMJ tJ7JMJ ^ zyx1 d > ^ zRxF > d
Now let us apply the unification algorithm to . Either there is no unifier, in which
case we have no need of a computational explanation, or there is a most general unifier
ª~ . In this case, the new subgoal looks like
X C DVE^ w D ^ ª~ ù
Furthermore, having found ª~ , we may also unfold the definition of with respect to Z][4\([AB
, discovering that for all ^
w ª~ ^ ÿ jª~ ^ u·x ª~ ^ yxF >üª~ ^ zyx d B ÿ C ^ w
The latter conversion holds by the computational properties of the proof term generated by the unification algorithm established in the previous chapter.
This shows us that case analysis with constructor form unification really does corre- spond intensionally to Coquand’s case-splitting step. We are now in a position to prove a crucial metatheorem.