• Nenhum resultado encontrado

Theorem 4 (Subject reduction). IfΓ⊢t1 :τ and t1 −→β t2 then Γ⊢t2:τ. Theorem 5 (Termination). IfΓ⊢t1:τ then t1 is strongly normalizing.

2.3 Polymorphism

Simple types lead to a lot of code duplication because the same untyped λ-term can usually be given several types. For example, the identity functionλx. xcan be assigned all the types of the formτ → τ and the composition of functionsλf. λg. λx. f (g x) can be assigned all the types of the form(τ2→τ3)→(τ1 →τ2)→τ1→τ3. When theλ-calculus is used as a basis for a programming language, this duplication is not acceptable. We do not want to duplicate the code of the programs in order to get the right to use them on dierent types.

This problem is solved by enriching the type system by polymorphism. Polymorphism is the ability to dene functions acting on several types; two kinds of polymorphism can be distinguished: ad-hoc polymorphism and parametric polymorphism.

An ad-hoc polymorphic function can act dierently depending on the type of its ar- gument. Ad-hoc polymorphism is usually dened by overloading a function symbol with several types and denitions. A typical example of ad-hoc polymorphism in programming is the printing function. Many programming languages provide a function print which prints its argument and is dened dierently depending on the type of the argument: for example, numbers are converted in decimal notation before printing.

Parametric polymorphic functions on the other side are functions whose denitions are generic in one or more types; they do not inspect the type of their arguments. The identity function and the composition of functions are examples of parametric polymorphic functions.

The most common polymorphic type system implemented in functional programming languages such as OCaml and Haskell is the Damas-Hindley-Milner type system [59]. It extends the simply-typedλ-calculus with parametric polymorphism. This type system is also used to dene higher-order logic.

We present Damas-Hindley-Milner type system in Section 2.3.1 and higher-order logic

2.3. POLYMORPHISM

in Section 2.3.2.

2.3.1 Damas-Hindley-Milner Type System

To provide a powerful type system for the functional programming language ML, Damas, and Milner [59] have proposed a polymorphic type system which had already been discovered by Hindley [96] in the context of combinatory logic. Damas-Hindley-Milner type system is also known as ML-like polymorphism, prenex polymorphism, let-polymorphism, and rank-1 polymorphism. There are a few equivalent presentations of this type system, one of the simplest is the syntax-directed presentation [53] that we adopt here.

In order to write the types of the identity function and of the composition of functions, we add type variables denoted by α. The termλx. x still accepts multiple types such as ι→ιand (ι→ι)→ (ι→ι) but it has a principle type α→α; all the types ofλx. x are obtained from α → α by substituting the type variable α. Similarly, λf. λg. λx. f (g x) will have the principle type (α2 →α3) →(α1 →α2) → α1 →α3. All λ-terms which are well-typed with respect to the simply-typed λ-calculus have principle types and principle types are eciently computable.

In order to bind the variables occurring in types, we introduce the notion of type schemes. Type schemes have the form Πα1. . . .Παn. τ1, they appear instead of types in typing contexts.

Polymorphic terms are introduced by named local denitions with the syntax letx := t1int2

which is semantically equivalent to (λx. t2) t1 but has a more liberal typing rule: in the case of the local denition, t2 is checked in a typing context in whichx is assigned a type scheme whereas in the case of the abstractionλx. t2, we are only allowed to assign a type tox while checkingt2. The syntax and typing rules of Damas-Hindley-Milner type system are given in Figure 2.2.

Contrary to other extensions of simply-typed λ-calculus to polymorphism such as Sys- tem F [82], type inference is decidable in Damas-Hindley-Milner type system.

Theorem 6 (Type inference). Given a typing context Γ and a term t, we can decide

1We use the notationΠfor prenex quantication instead of the more common notationin order to avoid confusion with the logical universal quantier.

2.3. POLYMORPHISM

SyntaxTypes τ ::= α Type variable

ι Atomic type

τ1 →τ2 Arrow type

Type schemes σ ::= τ Type

Πα. σ Universal scheme

Terms t ::= x Variable

t1 t2 Application

λx. t Abstraction

letx := t1 int2 Local denition

Typing contexts Γ ::= ∅ Empty context

Γ, x:σ Extended context

Typing

(when (x: Πα1. . . .Παn. τ)∈Γ)

(Var)

Γ⊢x:τ{α11, . . . αnn}

Γ⊢t11→τ2 Γ⊢t21 (App)

Γ⊢t1 t22 Γ, x:τ1 ⊢t:τ2

(Abs)

Γ⊢λx. t:τ1 →τ2 Γ⊢t11

Γ, x: Πα1. . . .Παn. τ1⊢t22 (when {α1, . . . , αn}=FV(τ1)\FV(Γ))

(Let)

Γ⊢letx := t1 int22

Figure 2.2: Damas-Hindley-Milner type system

2.3. POLYMORPHISM

whether or not there exists a type τ such that Γ⊢t:τ.

As in the simply-typed case, this result is constructive in the sense that when such a type τ exists, it can actually be computed fromΓ andt.

2.3.2 HOL

Prenex polymorphism is also the type discipline adopted in Church simple theory of types, also known as Higher-Order Logic (HOL for short). HOL has been implemented in various proof assistants: HOL Light, HOL4, HOL Zero, ProofPower-HOL and Is- abelle/HOL. These proof assistants are commonly referred to as the HOL family.

As its name suggests, HOL is a logic in which quantication is allowed at all orders:

in HOL we can quantify over logical propositions, predicates and arbitrary λ-terms. The type system of HOL is simply obtained by extending Damas-Hindley-Milner type system by a new atomic type o. Logical propositions are terms of type o and predicates over a type τ are terms of type τ →o.

As a logical system, HOL is axiomatized by a typing context called the signature and a set of rules for deriving new theorems. All theorem statements should be well-typed terms of typeo in the signature.

The usual axiomatization of HOL which is implemented in proof assistants of the HOL family is named Q0, it has been proposed by Andrews [7]. The signature of Q0 is = :˙ Πα. α →α →o, ϵ: Πα.(α→ o)→α. We write t1 =t2 for =˙ t1 t2. The deduction rules of Q0 are as follows:

Deduction rules

(Assume)

p⊢p ⊢t=t (Re) ⊢t1=t2

(AbsThm)

⊢λx. t1=λx. t2

⊢t1=t2 ⊢t3=t4

(AppThm)

⊢t1 t3=t2 t4

⊢p=q ⊢p

(EqMP)

⊢q q⊢p p⊢q

(DeductAntiSym)

⊢p=q

(η-equality)

⊢λx. t x=t p t⊢p (ϵ p) (Choice)

2.3. POLYMORPHISM

In Q0, functional extensionality f x = g x ⊢ f = g is provable from AbsThm and η-equality. From extensionality and Choice, the Law of Excluded Middle can be derived by Diaconescu Theorem [65] henceQ0 denes a classical logic.

If we want to work in an intuitionistic higher-order logic, we can take the universal quantier and the implication as primitives instead of equality and the choice operator. The signature of this alternative axiomatization is Σ := ˙∀: Πα. (α →o) →o,⇒˙ :o→ o→o. We write∀x. p instead of ∀(λx. p)˙ and p⇒ q instead of ⇒˙ p q. The derivation rules are as follows: