• No results found

Equalities and Identities

In document Type Theory & Functional Programming (Page 176-181)

for the monomorphic. Salvesen gives a negative answer to this question, showing by a number of counterexamples the differences between the poly- morphic and monomorphic theories. For instance, in [Sal89b] it is shown that the derivations

0 :N λyN⇒N.0 : (N ⇒N)⇒N (⇒I) [x:N] λxN. x : N ⇒N (⇒I) (λyN⇒N.0) (λxN. x) : N (⇒E) 0 :N λyB⇒B.0 : (B⇒B)⇒N (⇒I) [x:B] λxB. x : B⇒B (⇒I) (λyB⇒B.0) (λxB. x) : N (⇒E) both give rise to derivations of identical conclusions

(λy .0) (λx . x) : N

and it is simply impossible to derive a single monomorphic type for the variablesxand y in this derivation. Building on top of this simple exam- ple, there is a derivation in the polymorphic theory which, it is argued, cannot arise from a monomorphic derivation by suppressing type informa- tion. More complicated examples show how a Milner-style ([Mil78]) system ofprincipal types is not possible.

Finally, we should note that the polymorphism mentioned here is im- plicit polymorphism; we can give definitions which areexplicitly polymor- phic, even in the monomorphic system, by introducing (type) variables which range over the universesUn.

5.7

Equalities and Identities

In the discussion thus far can be found four different notions of equality or identity. We survey their differing roles in this section, and after a discussion of the purpose of these various notions, propose the definition of an extensional equality relation.

5.7.1

Definitional equality

Our first relation is in the meta-language, that is the language in which we discuss the various systems. We say that two termseandf are identical

if they are identical up to change of bound variable after all the defined terms, introduced by means of the definitional equality ‘≡df ’, have been

expanded out. We simply treat identical expressions as identical — there are no contexts in which we wish to distinguish between two identical ex- pressions.

As an aside, it is worth noting that although it is obvious what this relation is, we have to do some work in a computer implementation to ensure that we can decide exactly when two expressions are identical.

5.7.2

Convertibility

Two expressions are convertible if the computation steps embodied in the computation rules for the system are sufficient to bring them together. Formally we build the relation ‘↔↔’ by taking the reflexive, symmetric transitive and substitutive closure of the relation ‘→’. In other words, we ask that, for all expressionsa, b, c, . . .and variablesx,

Computation Ifa → bthena ↔↔ b.

Reflexivity a ↔↔ a.

Symmetry Ifa ↔↔ bthenb ↔↔ a.

Transitivity Ifa ↔↔ b andb ↔↔ cthena ↔↔ c.

Substitutivity Ifa ↔↔ b andc ↔↔ dthena[c/x] ↔↔ b[d/x].

We saw in the last section that two terms were convertible if and only if they have the same normal form, this means that the relation of convertibility is decidable.

The definition of convertibility is external to the system — a ↔↔ b is intended to embody the fact that the two expressions a andb denote the same object. In the light of the characterisation above, we can identify this object as the normal form of the expression, if we wish.

In section 4.11 we introduced the rules of substitution which allow in- terconvertible expressions to be substituted for each other in derivations of judgements. This emphasizes the fact that judgements are intended to be about the objects denoted by the expressions, rather than the expressions themselves. We shall come back to this important distinction below.

Because ‘a ↔↔ b’ is not a proposition of the system, we are unable to build more complex assertions on the basis of it. To do this we turn to our third relation, the identity predicate.

5.7. EQUALITIES AND IDENTITIES 165

5.7.3

Identity; the

I

type

As a primitive proposition (or type) forming operator we have the Ioper- ation, forming a type thus:

A is a type a:A b:A I(A, a, b)is a type (IF)

The type is also writtena=Abor evena=bwhen no confusion can result.

I(A, a, b) is provable, by the object r(a), whena ↔↔ b, so we can see the type as an internalisation of convertibility. On top of the I type we can build more complex assertions, such as

(∀x, y:A).((x=Ay)⇒((f x) =B(g y)))

wheref and g are functions of typeA⇒B. Proof thatIis the internali- sation of ‘↔↔’ is given by the result

Theorem 5.22 For closedaandb, the judgementI(A, a, b) is derivable if and only ifa ↔↔ b.

Proof: Clearly the ‘if’ part is valid. Suppose thatp:I(A, a, b) is derivable; taking normal forms`a la theorem 5.14 we have

p0:I(A0, a0, b0)

but for this to be derivable, it must be the case thata0≡b0, which means

thata ↔↔ b 2

The expressionx=Ay denotes a proposition or type of the system. In

order to test for identity in a computation we require a function or operation which returns not a type but rather a valueT rueorF alseof boolean type.

5.7.4

Equality functions

An equality function is a boolean valued function which can be used in a computation to test for the equality of two objects.

Definition 5.23 An equality function (or equality operation) over the typeAis a termequalAof typeequalA : A⇒A⇒bool such that the

following propositions are valid

(∀a, b:A).(a=Ab ⇒ equalAa b=boolT rue)

Note that one consequence of the definition is that for closeda,bifa↔↔b

then

equalAa b ↔↔ T rue

but on the other hand the non-derivability ofa ↔↔ bdoesnot imply that

equalAa b ↔↔ F alse

Over which types do we have an equality operation? We start our discussion with two definitions.

Definition 5.24 A predicateP(x1, . . . , xk) isformally decidableif and

only if the following proposition is derivable

(∀x1:A1). . . .(∀xk:Ak).(P(x1, . . . , xk)∨ ¬P(x1, . . . , xk)) (5.1)

Definition 5.25 A predicate P(x1, . . . , xk) is representable if and only

if for some termrthe following propositions are derivable (∀x1:A1). . . .(∀xk:Ak). (r x1. . . xk =boolT rue⇒P(x1, . . . , xk)) (5.2) (∀x1:A1). . . .(∀xk:Ak). (r x1. . . xk =boolF alse⇒ ¬P(x1, . . . , xk)) (5.3)

Theorem 5.26 A predicate is representable if and only if it is formally decidable.

Proof: To prove that a representable predicate is decidable, note first that using the axiom ofbool elimination we can derive

(∀b:bool).(b=boolT rue∨b=boolF alse)

(a proof of this appears in section 4.10.1). By means of the propositions 5.2, 5.3, we can derive the formula 5.1, as required.

To prove the converse, we need to take the derivation given by 5.1,

d : (∀x1:A1).(∀xk:Ak).(P(x1, . . . , xk)∨ ¬P(x1, . . . , xk))

The termdis a function, which we compose with the function defined over a disjunction which returnsT rueover the first disjunct andF alseover the second. This function is given by the term

λx .(cases x(λx . T rue) (λx . F alse))

5.7. EQUALITIES AND IDENTITIES 167

Corollary 5.27 A type A carries an equality function if and only if the equality over that type is formally decidable.

Proof: The equality function is a representation of equality over the type. The theorem therefore applies the result immediately. 2

Theorem 5.28 A ground typeA carries an equality function.

Proof: By the previous corollary it is sufficient to show that equality over the type is formally decidable. We can prove by induction over the construction of ground types that equality is decidable for them. Indeed, we have given direct definitions of equality functions in the exercises in the

previous chapter. 2

Will equality over any other types be decidable? It seems highly unlikely that this is so. Two closed terms of typeN ⇒N can be proved equal if and only if they have the same normal form, but there is no way,internally to type theory to compare normal forms. An extensional equality, to which we turn in the following section, has other drawbacks. From an extensional decidability predicate over a functional type we are able to prove a result like

((∀x:N). f x=N 0)∨ ¬((∀x:N). f x=N 0)

which is not in general acceptable to the constructivist, breaking as it does the constraint that properties be finitary.

5.7.5

Characterising equality

The elimination rules for the various type constructors allow us to derive characterisations of equality for types in terms of their component parts. In section 4.10.1 we saw that

(∀b:bool).(b=boolT rue∨b=boolF alse)

and we argued that other similar results could also be proved. In particular, we gave as an exercise the proof of

(∀x:N).(x=N 0∨(∃y:N).(x=N succ y))

We would also mention the other characterisations (∀x:A∧B).(∃y:A).(∃z:B).(x= (y, z)) and

and for the finite types

(∀x:Nn).(x= 1n∨. . .∨x=nn)

with the special case that (∀x:>). x=T riv

These results are all proved in similar ways, using the axiom of elimination for the type in question.

Exercises

5.22. Complete the proof of theorem 5.26 by exhibiting the termrexplicitly. 5.23. Give a definition of the equality function over thetree type.

5.24. Prove the characterisations of equality for conjunction (product) and disjunction (sum) types given above.

In document Type Theory & Functional Programming (Page 176-181)