• Nenhum resultado encontrado

1.4. ZENON MODULO

Chapter 2

λ -Calculus and Type Theory

Type theory was invented by Russell [153] in 1908 to solve the paradoxes of naive set theory that had just been discovered a few years before.

The typing discipline corresponds to the mathematical habit of not interchanging ob- jects of dierent natures. In planar geometry for example, the parallelism relation applies only to straight lines; statements such as "2 is parallel to the empty set" are not rejected because they are false statements, they must be ruled-out because they carry no meaning.

Types also have a wide range of applications in programming languages. Most lan- guages assign types to data at least to indicate how much size they have in memory. This assignation happens either dynamically during the program evaluation or statically during the compilation of the program. In the case of static typing, types are used to ensure that some dynamic errors such as trying to apply a number as a function will not appear. Types are also of a great help for reporting errors, for guiding compiler optimizations [114], and for ensuring various properties.

In the particular case of the λ-calculus at the heart of the functional languages, many type systems have been studied. We present λ-calculus in Section 2.1, the simplest type system for λ-calculus in Section 2.2, and we extend this system to polymorphism in Sec- tion 2.3 to obtain the type system used in real functional programming languages. By adding another feature called dependent typing, type systems can be used to encode log- ics. We present dependent type systems in Section 2.4 and a particular class of type systems called logical frameworks specialized in the encoding of logical systems in Section 2.5.

2.1. λ-CALCULUS

2.1 λ -Calculus

The λ-calculus is the core calculus of functional programming languages. It is dened by a syntax describing which terms belong to the language and a semantics describing how programs are evaluated.

The syntax of the pure λ-calculus has only one category: the category of λ-terms.

λ-terms are built from variables, binary application, and the original operation of λ- abstraction:

λ-terms t ::= x Variable

t1 t2 Application λx. t λ-abstraction

Application roughly corresponds to the usual operation consisting of applying a function to its argument and is written t1 t2 instead of the more common mathematical notation t1(t2) in order to save parentheses. To save more parentheses, we take the convention that application associates to the left: t1 t2 t3 should be read as (t1 t2)t3, not t1 (t2 t3).

λ-abstraction is the way functions are dened inλ-calculus: the termλx. tcorresponds roughly to the function returning t when applied to x. In the term λx. t, the variable x is bound in the term t (the notion of binding has been introduced in Section 1.1.1.2). In the context of theλ-calculus, the renaming operation is called α-renaming, α-conversion, orα-equivalence. For example, the termsλx. λy. xandλz. λx. z areα-equivalent. We are usually interested inλ-terms moduloα-equivalence only.

The notions of free variables and substitutions are the same as for rst-order formulae.

The set of free variables FV(t) of a termtis dened as follows:

FV(x) := x

FV(t1 t2) := FV(t1)∪FV(t2) FV(λx. t) := FV(t)\ {x}

It is always possible toα-rename aλ-term in such a way that free and bound variables form disjoint sets.

The substitution of the variablexby the termt1 in the termt2 is writtent2{x\t1}and dened as follows:

2.1. λ-CALCULUS

x{x\t1} := t1

y{x\t1} := y wheny ̸=x

(t2 t3){x\t1} := (t2{x\t1}) (t3{x\t1}) (λx. t){x\t1} := t

(λy. t){x\t1} := λy.(t{x\t1}) wheny ̸∈FV(t1) λ-calculus is given a computational meaning by dening theβ rewriting rule:

ˆ (λx. t2) t1−→t2{x\t1}

This rule is not a rst-order rewrite rule in the sense discussed in Section 1.2 because of the presence of theλbinder in the left-hand side and the substitution in the right-hand side so we need to generalize the denitions of Section 1.2.

β-reduction is the smallest relation −→β such that:

ˆ (λx. t2) t1−→β t2{x\t1}, and

ˆ −→β is a closed under context:

if t1−→β t2 thent1 t3−→β t2 t3 and t3 t1 −→β t3 t2, and if t1−→β t2 thenλx. t1 −→β λx. t2.

For example, the termλx.(λy. x)x β-reduces toλx. xand the term(λx. x x) (λy. y y) β-reduces to itself.

We denote by−→β the reexive and transitive closure of−→β dened as the smallest relation such that:

ˆ if t1−→β t2 thent1 −→β t2,

ˆ −→β is reexive: t−→β t, and

ˆ −→β is transitive: ift1 −→β t2 and t2 −→β t3 thent1 −→β t3.

Finally, we denote by≡β the reexive, symmetric and transitive closure of−→β dened as the smallest relation such that:

ˆ if t1−→β t2 thent1β t2,

2.1. λ-CALCULUS

ˆ ≡β is reexive: t≡β t,

ˆ ≡β is symmetric: ift1β t2, thent2β t1,

ˆ ≡β is transitive: ift1β t2 and t2β t3 thent1β t3.

A λ-termt1 for which noλ-termt2 exists such thatt1 −→β t2 is called a normal term.

For example, the identity λx. x is normal. If t1 −→β t2 and t2 is normal then we say thatt1 normalizes to t2 and that t2 is the normal form oft1. Not all terms normalize, for example the self-reducing term(λx. x x) (λy. y y)does not normalize but the normal form of a normalizing term is unique. This is a consequence of the Church-Rosser theorem [48]

stating that β-reduction is a conuent relation:

Theorem 1 (Church-Rosser). Let t1, t2, and t3 be λ-terms such that t1 −→β t2 and t1 −→β t3, there exists a λ-term t4 such that t2−→β t4 andt3 −→β t4.

We can distinguish three kinds of λ-terms:

ˆ diverging terms having no normal form such as(λx. x x) (λy. y y)

ˆ weakly normalizing terms which have normal forms but also innite reduction se- quences such as(λx. λy. y) ((λx. x x) (λy. y y))which reduces both to itself and to the normal termλy. y

ˆ strongly normalizing terms for which all reduction sequences are nite.

Despite its simplicity, λ-calculus is a Turing-complete model of computation. In par- ticular, natural numbers can be encoded in various ways, the simplest of which is probably Church numerals. In this encoding of numerals, the natural number n is represented as the operation consisting of iterating functions ntimes:

n:=λf. λx.

ntimes

  

f (f (. . .(f x). . .)) Some arithmetic functions are easy to dene in this setting:

0 :=λf. λx. x

succ:=λn. λf. λx. f (n f x) + :=λm. λn. λf. λx. m f (n f x)

×:=λm. λn. λf. λx. m(n f)x