• No results found

Chapter 4 Inductive Datatypes

4.1.1 simple inductive datatypes like

Componentwise

The type former is a constant which inhabits some universe

Ú×Û4Ü ;=u[wyxTz is an example of such an Ú×Û4Ü .

The constructors are function symbols ‰

R67676 ‰ 3Ý

, where for each in

b

‰ Œ ;4q |0;< < 8 Œ 6rq PÌ;< ßNà™áÔ6 The < 8

Œ are called the non-recursive arguments because they may not refer to Ú×Û4Ü

. Neither may they involve any universe as large as that which

ÚpÛ4Ü

inhabits, in order to avoid the paradoxical embedding of a larger universe inside a smaller one—we may usually rely on Harper and Pollack’s universal policeman [HP91] and use the unlabelled u[wyxTz regardless.

We say that ‰

Œ has

â

Œ recursive arguments. Think of elements of Ú×Û4Ü

as tree structures made from nodes of different kinds given by the constructors, a

‰

Œ node having

â

Œ out-edges and a label of telescope <

8

Œ . Actually, there

is no need for the recursive arguments to come after the non-recursive ones, but it makes the presentation simpler if we pretend they always do—since non- recursive arguments cannot have types involving

ÚpÛ4Ü

, they may certainly always be permuted to the front.

We may also think of constructors as introduction rules for

Ú×Û4Ü : < |€; < 8 Œ PSR; Ú×Û4Ü 67676 P àÅá ; Ú×Û4Ü ‰ Œ < | Pš;< Ú×Û4Ü

The derivation trees composed from such rules correspond exactly to the tree notion of inductive data structures mentioned above.

has two constructors:

Fk; J»; &J»;

Observe also that, if

Ú×Û4Ü

is to be inhabited, it will need at least one constructor with no recursive arguments.

Let us examine

ÚpÛ4Ü®ãQä_å_æ

, the constant whose type gives the elimination rule for

Ú×Û4Ü

, in accordance with the general analysis of elimination rules presented earlier.

The pattern which

Ú×Û4Ü®ãKä_å_æ

eliminates is the free pattern on

ÚpÛ4Ü

, which matches any element of

Ú×Û4Ü

. Hence

ÚpÛ4Ü®ãQä_å_æ

has a scheme indexed by

Ú×Û4Ü

, ie%Ï; qsP; Ú×Û4Ü

6u[wyxTz and a rule goal targetting

the element to be eliminated. q Pš; ÚpÛ3Ü

6%GP . The

outline of the rule is as shown.

Ú×Û4Ü®ãKä_å_æ %^;4qsPÌ; ÚpÛ4Ü 6[u[wyxTz „ q P›; Ú×Û4Ü 6%žP

In order to build a proof of %‚P for an arbitraryP , we need a method for each

arguments—more succinctly, that each‰

Œ preserves

% . We may think of%

as a property which must hold wherever its argument is an

Ú×Û4Ü

, hence it must have ‘introduction rules’—the rule subgoals of

Ú×Û4Ü®ãKä_å_æ

—analogous to those of

Ú×Û4Ü

. Thus we manufacture the rule subgoals of

Ú×Û4Ü®ãKä_å_æ

from the introduction rules of

Ú×Û4Ü

by writing%

ý in the former wherever the latter hasý

; ÚpÛ4Ü : < |€; < 8 Œ %žPSR 676[6 %LP àÅá 676[6[676[676[676[676[6[67676r6767676r67676r6767676r6 %Dd ‰ Œ < |<PTf

Note that the recursive argumentsPš;<

Þ

ÚpÛ4Ü

ßà™á have not disappeared entirely. The

types of the recursion hypotheses depend on them, hence we may infer that they are themselves present as case hypotheses, and suppress them from the writ- ten rule accordingly. Functional programmers may be more familiar with ‘fold operators’—the cut down version, where % is a constant and the recursive argu-

ments are supplanted by the recursion hypotheses.

We now have all the pieces we need to complete the

ÚpÛ4Ü®ãQä_å_æ rule: %Ï;4qsPÓ; Ú×Û4Ü 6ru[wyxTz <|€; < 8 R %žPSR 67676 %GP à \ 6767676r67676r67676[6[67676[6[676[676[676[676[6[67676 %td ‰ R |< PTf< 67676 < |Û; < 8 Ý %LPSR 6[676 %GP àç 676[6[67676[6[676[676[6[67676[6[67676r67676r6767676r6 %Dd ‰ 3Ý < | Pnf< q P›; Ú×Û4Ü 6%žP

Or, more inscrutably,

Ú×Û4ܜãQä_å_æ ;4q&%ž; Ú×Û4Ü ¬ u[wyxTz(6 dq |0;< < 8òR{6rq PÌ;< Þ Ú×Û4Ü ßNà \ 6 Þ %LP [ ßNà \ [ ¬ %Dd ‰ R |< PTfõf< ¬ .. . dq<|0; < 8 Ý 6~q><PÌ; Þ Ú×Û4Ü ß à \ 6 Þ %GP [ ß à \ [ ¬ %td ‰ 3Ý < | <PTfBf ¬ q P›; Ú×Û4Ü 6%žP

For the natural numbers, then, we get

, ; q&%G;$qsJÐ; 67u[wyxnzv6 d%‚FTf ¬ dIqsJj; 69d%GJ+f ¬ %‚(J+f ¬ q J.; 6%GJ , %Ï;4qsJj; 6[u[wyxTz %NF %žJ 67676r67676 %‚vJ q J.; 6%GJ

Those of us given to a skeptical disposition would be unlikely to accept the va- lidity of

ÚpÛ4Ü®ãQä_å_æ

if we did not see how to plug the proofs of its rule subgoals together to build an inhabitant of % for any particularP which we might make

from

Ú×Û4Ü

’s constructors. This process is represented in our type theory by the

Ø-reductions associated with Ú×Û4Ü

. By this means we imbue

Ú×Û4Ü®ãKä_å_æ

with a com- putational meaning, allowing us to evaluate recursive functions over

Ú×Û4Ü

.

We add an Ø-reduction for the effect of Ú×Û4Ü®ãKä_å_æ on each constructor: Ú×Û4ܜãQä_å_æ % < 'šd ‰ Œ < | PTfijè0'ªé< |< P< q ÚpÛ3ܜãQä_å_æ % < 'ÒP [ r àÅá [

For the natural numbers, we get two such rules:

,µ%!'0ê>'&ÃF ijèë'ê ,µ%!'0ê>'&Ãn(JÏijèë'&Ã9J‚d ,µ%ì'ê>'¦Ã9Jêf

Given the type former and constructors for a simple inductive datatype, the elimination rule and Ø-reductions can be computed in a straightforward way.