• No results found

5.2 Formal Data Types

5.2.3 Examples of Data Types

In this section we show some examples of useful data types in the intended model M. Through the whole section data type will mean data type inM.

The Unit Predicate

The first example of a data type is the unit predicate defined as :=λy.?=y.

It is obvious that M|=∀z. z↔?=z. Therefore to prove that is a data type it suffices to show that

M|=y.yr ?y=? ?.

This will hold if we interpret?as the (equivalence class of the) identity,?M :=

λzz.

Take an arbitrary valuation ν and a term r, setν0 :=ν[r/y]. We proveν0 |= yr ?↔y=?∧ ?. First assumeν0|=y=?∧ ?. Asν0|=y=?it suffices to showν0 |=?r ?. We have ?r ?≡ ?r(?=?) ≡ ?r∀X.X?→X? ≡ ∀X+.u.urX??urX?. But as?M λzz it suffices to show ν0 |=∀X+.u.u rX?→urX?, which is trivial.

Next assume that ν0|=yr ?, that is ν0|=∀X+.u.u

rX?→yurX?

in particular ifX+:=λu

1, u2.u2=u1u∧ u1we have that

ν0|=∀u.u=?u∧ ?→yu=?u∧ ?.

It is clear that the antecedent holds, therefore from the succedent we get in particular ν0 |= ∀u.yu = ?u which by the interpretation of ? leads to ν0 |= ∀u.yu =u, this implies that rs =βη s for all terms s which leads to ν0(y) = r=βη λzz. Henceν0 |=y=?and the proof is finished.

The above proof also shows that the predicate r:=λy, z.zr y only holds

fory, z:=?, ?. The Booleans The Predicate

:=µX h ,truegi,h ,falsegi

representing booleans is a data type if we interprettrueM

g := 21,false

M g := 22. We will show

M|=xy.yr xy=x x

Take r, s arbitrary terms and a valuation ν, set ν0 := ν[r, s/x, y]. Assume ν0 |=y =x x. We will show ν0 |=x r x. By hypothesis we haveν0 |= x

which by definition is the same as

5.2. FORMAL DATA TYPES 147

which, as holds only for?, simplifies to

ν0 |=∀X.X(trueg?), X(falseg?)→Xx,

in particular ifX :=λz.zr zwe have

ν0 |=trueg?r (trueg?),falseg?r (falseg?)→xr x.

Therefore it suffices to showν0|=true

g?r (trueg?) andν0|=falseg?r (falseg?).

ν0 |= trueg?r (trueg?)⇔

ν0 |= µX+(h r,true, 2

1i,h r,false, 22i)(trueg?)(trueg?)⇔

ν0 |= X+.X+(true

g?)( 21?), X+(falseg?)( 22?)→X+(trueg?)(trueg?)

But astrueM

g := 21 it suffices to show

ν0|=∀X+.X+(true

g?)( 21?), X+(falseg?)( 22?)→X+(trueg?)( 21?)

which obviously holds. Similarly we concludeν0 |=falseg?r (falseg?).

Now assumeν0 |=yr x. We will prove ν0 |=y =x∧ x. The assumption is equivalent to

ν0|=∀X+.X+(true

g?)( 21?), X+(falseg?)( 22?)→X+xy

which in particular withX+:=λu

1, u2.u2=u1∧ u1implies

ν0|= 21?=trueg?∧ (trueg?), 22?=falseg?∧ (falseg?)→y=x∧ x.

Next observe that (trueg?), (falseg?) are trivially satisfied and by the interpre-

tations of trueg,falseg alsoν0 |= 21?= (trueg?) and ν0 |= 22?= (falseg?) hold.

Therefore the antecedents of the implication are satisfied and we can conclude

ν0|=y=x∧ x.

The Natural Numbers If 0M

g := 21, sM := 22then

:=µX h ,0gi,hX, si

is a data type representing natural numbers.

Taker, t∈ |M|and a valuationν. Set ν0 := [x, y/r, t].

Assume ν0|=yr

x. Our goal is to showν0|=y=x

x. We have ν0 |= yr x⇔ ν0 |= µX+ h r,0 g, 21i,hX+, s, 22i xy⇔ ν0 |= ∀X+. rX+0g, 21, X+ X+s, 22 X+xy

which as r only holds for?, ?simplifies to ν0|=∀X+.X+(0

g?)( 21?), X+ ⊆X+

s, 2

2 X+xy

In particular settingX+:=λu

1, u2.u2=u1∧ u1, we have ν0|= 2 1?= 0g?∧ (0g?),(λu1, u2.u2=u1∧ u1)⊆(λu1, u2.u2=u1∧ u1)s, 2 2 →y =x∧ x That is, ν0|= 2 1?= 0g?∧ (0g?), ∀uv.v=u∧ u→ 2 2v=su∧ su →y=x∧ x (5.32) ν0|=

(0g?) holds trivially andν0 |= 21?= 0g?holds, because 0Mg = 21. Takep, q∈ |M|and setν00:=ν0[u, v/p, q] and assumeν00|=v=u∧

u. As |= ⊆ s andν00 |= uwe get ν00 |= su. Moreover as ν00 |=v =u then ν00 |= 2 2v = 22u, which as s M := 2 2 yields ν00 |= 22v =su. Therefore ν00 |= 2 2v=su∧

su, the anteccedents of (5.32) hold and we getν0|=y=x∧

x. Now assume ν0 |= y = x

x. The goal is to show ν0 |= y r

x. As ν0 |=y=xit suffices to showν0|=xr x. ν0|= x⇔ ν0|= ∀X. ⊆X0g, XXsXx ν0|= ∀X.X(0g?),(∀z.Xz→Xsz)→Xx

This implies in particular forX :=λz.zr

z ν0|= 0g?r (0g?),(∀z.zr z→szr sz)→xr x

We prove the anteccedents of this implication ◦ ν0|= 0 g?r 0g?. We have ν0|= 0g?r 0g?⇔ ν0|= µX+ h r,0 g, 21i,hX+, s, 22i (0g?)(0g?)⇔ ν0|= X+. rX+0g, 2 1, X+X+s, 22X+(0 g?)(0g?)⇔ ν0|= X+.X+(0 g?)( 21?), X+ ⊆X+ s, 2 2 X+(0 g?)(0g?) ⇔ 0M g ≡ 21 ν0|= ∀X+.X+(0 g?)(0g?), X+⊆X+ s, 2 2 X+(0 g?)(0g?)

and the last claim is trivial. Therefore we are done. ◦ ν0 |=z.zr

z →sz r

sz. Set ν00:=ν[z/t] with t∈ |M|, and assume ν00|=zr z i.e. ν00|=∀X+.X+(0 g?)( 21?), X+ ⊆X+ s, 2 2 X+zz (5.33)

5.2. FORMAL DATA TYPES 149

The goal is to showν00|=szr

sz, i.e.

ν00|=∀X+.X+(0

g?)( 21?), X+⊆X+

s, 2

2 X+(sz)(sz)

Take ν∗ :=ν00[X+/R] with R ⊆ |M|2 and assumeν|=X+(0

g?)( 21?) and ν∗ |=X+ X+s, 22. These assumptions together with (5.33) yield

ν∗|=X+zzwhich, by the second assumption, impliesν?|=X+(sz)( 2 2z). Finally assM 2

2 we getν∗|=X+(sz)(sz) and we are done. We leave to the reader the verification of the remaining examples. Sum of Data Types

IfA,Bare data types then their sum (disjoint union) A+B:=µX hA,inli,h ,inri is a data type if we setinlM:= 2

1,inrM := 22. Product of Data Types

IfA,Bare data types then their product

A × B:=νX hA, 1i,h , 2i

is a data type if we set 1M := 21, 2M := 22. The proof relies on the follo- wing consequence of the extensionality property (M ηInv) of M: If ν |= 1v =

1u, ν|= 2v= 2uthenν |=v=u.

For another concept of product data type which do not need this extensional property see page 170

Function Space of Data Types

IfA,Bare data types then their function space A → B:=λf.∀z.Az→ Bf z

is a data type. Observe that this predicate is not (co)inductive. Lists

Given a data type Awe set

LA:=µX h ,nilgi,hA×X,consi

.

LAdefines the set of lists of elements of the data typeA, which is again a data

type if nilMg := 2 1,cons

M

:= 2 2.

Streams

Given a data typeAwe would like the predicate ofA-streams SA:=νX hA,headi,hX,taili

to be again a data type if we interprethead:= 2

1,tail:= 22. However even if Ais a data type we cannot prove thatSAis a data type. When trying to prove

M|=xy.yrSA[x]y=x∧ SA[x]

in the direction from left to right we cannot gety=x∧ SA[x] but only

Ahead tailkx for everyk∈

and Leibniz’ equality is to weak to concludeSA[x] from this fact.

A solution to this problem will be given in section 6.5.1.