• No results found

For each constructor type of algebra, there is a constructor symbol. We adopt each constructor symbol as a special constant called constructors.

Definition 2.2.5 (Constructors). Let ι be an algebra µξ((ρiν(ξ))ν<ni →ξ)i<k. For each

constructor type of ι a constructor Ci is defined as a constant of type (ρiν(ι))ν<ni →ι.

Constructors are means to construct (tree structured) objects from the root to the leaves. On the other hand, we have recursion operators to inspect an object which is built by constructors. A recursion operator reads an object from its leaves to the root. We define for each algebra ι the recursion operator Rτ

ι where τ is an arbitrary type parameter.

We first define map operators which are used in the definition of the conversion rules of recursion operators. Then we define recursion operators. Later we also use map operators to define corecursion operators.

Definition 2.2.6 (Map operator). Let ~α be a list of type variables of lengthl ≥0, ι(α~) be an algebra µξ((ρiν(α, ξ~ ))ν<ni →ξ)i<k and C

~ α

i be the i-th constructor of ι(~α). Assume

a list of function types σi → τi for 0 ≤ i < l, written ~σ → ~τ, where l is the length of ~α.

the occurrences of α~ inρ(α~) andπj(~α) for j < k are strictly positive, we define the map

operator M~σ→~τ

λ~αρ(~α) by induction on the construction ofρ.

M~σλ→~τ ~ αρ(~α):ρ(~σ)→(σi →τi)0≤i<l →ρ(~τ), M~σ→~τ λα~(β→ρ(~α))N β→ρ(~σ)M K~ β 7→ M~σ→~τ λα~ρ(~α)(N K) ρ(~σ)M ,~ M~σ→~τ λα~αiN σiM~ 7→M iN, M~σ→~τ λα~ρ N ρM~ 7→N if FV(ρ)α~ =, M~σ→~τ λα~ι~π(~α)(C ~ π(~σ) i N~) ι~π(~σ)M~ 7→C~π(~τ) i (M ~ π(~σ),ι~π(~σ)→~π(~τ),ι~π(~τ) λβ,γ~ ρiν(β,γ~ ) NνK~(M~πι(~σ)→π~(~τ)·K~))ν<ni,

where M ·M~ stands for λx(Mx ~M) and Kj :=Mλ~σ→~απ~τj(~α)·

~

M for j <|~π|. We define the type and the conversion rule of recursion operators.

Definition 2.2.7 (Recursion operator). Let ιbe an algebraµξ((ρiν(α, ξ~ ))ν<ni →ξ)i<k. For

an arbitrary typeτ, the type of the recursion operatorRτ

ι is defined to beι→~δ →τ where

δi := (ρiν(~α, ι×τ))ν<ni →τ.

The conversion rule of the recursion operators is Rτι(CiN~)M~ 7→Mi(Mιλ→ι×τ

ξρiν(~α,ξ) Nν λzhz,R

τ

ιz ~Mi)ν<ni.

We call eachδiastep type and eachMiastep term. Ifρiν(α, ξ~ ) is justξ, we conventionally

type such a part of the step typeδiby currying ι×τ, namely, with duplicatingιandτ. Then

the conversion rule is given without a map operator, again by duplicating the arguments

Ni and RτιNiM~ in the step term Mi. Recursion operatorRι makes a case distinction on

the outermost constructor of the first argument. Assuming thei-th constructor is the case, then the i-th step term is called with arguments including recursive calls of the recursion operator. An argument consists of two parts, the previous input and the previous output to the recursion operator, which are computed from the argument Nν of the constructor by

means of a map operator. Example 2.2.8 (Type of Rτ

Lα). Consider the type of R

τ

Lα. In the form of constructor

types, n0 = 0, n1 = 2, ρ10 = α and ρ11 = ξ specify the above constructor types. Since

n0 = 0, noρ0ν occurs, thus δ0 =τ. Next,δ1 =α→(Lα×τ)→τ. As mentioned, here we

use α→Lα →τ →τ instead of the above step type with products by duplicating Lα×τ.

Therefore, the type of Rτ

Lα is Lα →τ →(α→Lα →τ →τ)→τ.

Example 2.2.9 (Descending list of natural numbers). The recursion operator on natural numbers, Rτ N, is given as Rτ N :N→τ →(N→τ →τ)→τ, RτN0M0M1 7→M0, RτN(Sn)M0M1 7→M1n(RτNnM0M1).

The following term Desc computes a list of natural numbers descending from the given natural number to 1.

Desc:N→LN

Desc:= λn(RN n []λm,l(Sm::l)).

The computation of Desc is as follows:

Desc07→[], Desc(Sn)7→Sn::Desc n.

Example 2.2.10 (Recursion operator on Nt). The recursion operator on Ntis given as follows. RτNt :Nt→(LNt×τ →τ)→τ Rτ Nt(Bras)M 7→M(MNt →Nt×τ λαLα asλaha,RaMi)

An important variant of recursion operators is case operators. It is to make a case distinction on the outermost constructor.

Definition 2.2.11 (Case operator). Under the same assumptions in Definition 2.2.7, we define Cτ

ι to be a constant of type ι → ~σ → τ, where σi := (ρiν(~α, ι))ν<ni → τ. The

conversion rule of the case operators is

Cιτ(CiN~)M~ 7→MiN .~

Following a common style found in generic programming languages, we adopt to denote a term Cτ

ι tιM~ also in the following way.

Case tι of C0~x0 →L0 .. . Ck−1~xk−1 →Lk−1 or Case tι of λ~x0L0 .. . λ~xk−1Lk−1,

wherexiνis a fresh variable of typeρiν(~α, ι) forν < niandLi is a term such thatMi~xi 7→∗ Li.

We can omit~xi when ni = 0. A line break can be replaced by a semicolon (;).

A well-known programming construction of if-then-else is given by Cτ

B of type B→τ →

τ →τ. We accept to denote Cτ

BM N0N1 as follows. ifMthenN0elseN1 We study simultaneous recursion operators.

Definition 2.2.12(Simultaneous recursion operator). Let~τ andξ~be lists of types of length

recursion operators on ~ι. For each m < l let σm be ιm ×τm and the step types δji be

(ρiν(~σ))ν<ni →τji, then the types of recursion operators are defined for m < l as follows.

R~ι,m~τ :ιm →~δ→τm.

The conversion rules are

R~ι,jτ~ i(CiN~)M~ 7→Mi(M~ιλ→α~ρ~σiν(~α)Nν(λxhx,R

~ τ

~ι,mx ~Mi)m<l)ν<ni.

Example 2.2.13 (Recursion operator on (Ts,T)). Consider simultaneous recursion oper- ators R~τ

(Ts,T),0 and R

(Ts,T),1 which are non-nested. For 0≤i <3, letδi be

δ0 :=τ0, δ1 := T→τ1 →Ts→τ0 →τ0, δ2 := Ts→τ0 →τ1.

The types of R~τ

(Ts,T),0 and R(~τTs,T),1 are

R~τ(Ts,T),0 :Ts→δ0 →δ1 →δ2 →τ0, R~τ(Ts,T),1 :T→δ0 →δ1 →δ2 →τ1. The conversion rules are as follows.

R~τ (Ts,T),0EmptyM~ 7→M0 R~τ (Ts,T),0(Tconsa as)M~ 7→M1a(R~τ(Ts,T),1a ~M)as(R ~ τ (Ts,T),0as ~M) R~τ (Ts,T),1(Branchas)M~ 7→M2as(R~τ(Ts,T),0as ~M)

Example 2.2.14 (Substitution of trees). We define a function of typeT→T→T which replaces each leaves Branch Empty in the first argument by the second one as follows.

λa,b(R(Ts,T),1a b λ,p, ,q(Branch(Tconsp q))λ,pp).

The underscore ( ) is used as a place holder for an abstracted variable which is unused. When only some algebras of simultaneously defined ones are needed to recur, simplified simultaneous recursion operators suffice. Generally, we assume R~τ

~ι is defined and build

a simplified one. We first specify what algebras ιi0, . . . , ιik from~ι are irrelevant, that is,

are not needed to recur. We drop all δi whose final value type is one of τi0, . . . , τik. From

argument types of each remaining δi, we drop all ιi0, . . . , ιik andτi0, . . . , τik. The remaining

algebras are on the other hand called the relevant algebras. For example, we simplify R~τ

Ts,T in two ways.

1. No recursion on T. Among~δ, δ2 and δ3 are dropped because their final value type is

τ1 which is irrelevant. Remaining step types are δ0 and δ1. The typesT and τ1 occur in δ0 no more. From δ1 we remove the type arguments T and τ1. The type and the conversion rules are

Ts :Ts→τ →(Ts→τ →τ)→τ,

2. No recursion on Ts. As we did in 1., we remove irrelevant types. Then the type and the conversion rules are

T :T→τ →τ, R

τ

T(Branchas)M0 7→M0.

Example 2.2.15 (Length of a tree). We define a function Lh to count the length of a tree, namely, the number of branches at the root node.

Lh:T→N, Lh:= λa(Ca λas(RNTsas0λ,n(Sn))).

Due to the inhabitedness of types, for an arbitrary type ρ there is an inhabitant of type ρ, namely, a term of type ρ. We can find from the derivation of Inhab~ζ(ρ) an inhabitant of

type ρ.

Definition 2.2.16 (Inhabitant). Assume τ is a type, a derivation of Inhab~ζ(τ) is given

and an inhabitant cα of each type variable α ∈ FV(τ) is given. We inductively define from the derivation of Inhab~ζ(τ) an inhabitant of type τ. If Inhab~ζ(α) is derived by the

first rule, an inhabitant is cα. If Inhab ~

ζ(ρ→σ) is derived by the second rule, there is an

inhabitantλxρcσ of typeρ→σwherecσ comes by induction hypothesis. If Inhab~

ζ(ι), where

ι = µξ((ρiν)ν<ni → ξ)i<k, is derived by the third rule, there is an inhabitant Ci(c

ρiν)

ν<ni

where Ci is the i-th constructor of ι and each cρiν for all ν < ni comes by induction

hypothesis. An inhabitant cρ iscanonical if it is obtained from the canonical derivation of Inhabζ~(ρ).