• Keine Ergebnisse gefunden

Principal type-schemes for functional programs

N/A
N/A
Protected

Academic year: 2021

Aktie "Principal type-schemes for functional programs"

Copied!
7
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Principal type-schemes for functional programs

Luis Damas

and Robin Milner

First published in POPL ’82: Proceedings of the 9th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, ACM, pp. 207–212

Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of its publication and date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission.

© 1982 ACM 0-89791-065-6/82/001/0207 $00.75

1 Introduction

This paper is concerned with the polymorphic type discipline of ML, which is a gen- eral purpose functional programming language, although it was first introduced as a metalanguage (whence its name) for constructing proofs in the LCF proof system.[4]

The type discipline was studied in[5]where it was shown to be semantically sound, in a sense made precise below, but where one important question was left open:

does the type-checking algorithm — or more precisely the typeassignmentalgorithm (since types are assigned by the compiler, and need not be mentioned by the pro- grammer) — find the most general type possible for every expression and declara- tion? Here we answer the question in the affirmative, for the purely applicative part of ML. It follows immediately that it is decidable whether a program is well-typed, in contrast with the elegant and slightly more permissive type discipline of Coppo.[1]

After several years of successful use of the language, both in LCF and other research, and in teaching to undergraduates, it has become important to answer these ques- tions — particularly because the combination of flexibility (due to polymorphism), robustness (due to semantic soundness) and detection of errors at compile time has proved to be one of the strongest aspects of ML.

The discipline can be well illustrated by a small example. Let us define in ML the functionmap, which maps a given function over a given list — that is

map f [x1; ...; xn] = [f(x1),...,f(xn)]

The required declaration is

Re-keyed 12 October 2010 by Ian Grantiang@pobox.com

The work of this author is supported by the Portuguese Instituto Nacional de Investigacao Cientifica

(2)

letrec map f s = if null s then nil

else cons(f(hd s)) (map f (tl s))

The type checker will deduce a type-scheme formapfrom existing type-schemes for null,nil,cons,hdandtl; the termtype-schemeis appropriate since all these objects are polymorphic. In fact from

null : ∀α(αlist→bool) nil : ∀α(αlist)

cons : ∀α(α→(αlist→αlist)) hd : ∀α(αlist→α)

tl : ∀α(αlist→αlist) will be deduced

map : ∀α∀β((α→β)→(αlist→βlist)).

Types are built from type constants (bool. . .) and type variables (α,β, . . .) using type operators (such as infixed→for functions and postfixedlistfor lists); a type-scheme is a type with (possibly) quantification of type variables at the outermost.

Thus, the main result of this paper is that the type-scheme deduced for such a dec- laration (and more generally, for any ML expression) is aprincipaltype-scheme, i.e.

that any other type-scheme for the declaration is a generic instance of it. This is a generalisation of Hindley’s result for Combinatory Logic[3].

ML may be contrasted with Algol 68, in which there is no polymorphism, and with Russell[2], in which parametric types appear explicitly as arguments to polymorphic functions. The generic types of Ada may be compared with type-schemes. For sim- plicity, our definitions and results here are formulated for a skeletal language, since their extension to ML is a routine matter. For example recursion is omitted since it can be introduced by simply adding the polymorphic fixed-point operator

fix : ∀α((α→α)α) and likewise for conditional expressions.

2 The language

Assuming a setIdof identifiers xthe language Expofexpressions e is given by the syntax

e::=x|e e0|λx.e|letx=eine0

(where parentheses may be used to avoid ambiguity). Only the last clause extends the ń-calculus. Indeed for type checking purposes everyletexpression could be elimi- nated (by replacingxbyeeverywhere ine0), except for the important consideration that in on-line use of ML declarations

letx=e

(3)

are allowed, whose scope (e0) is the remainder of the on-line session. As illustrated in the introduction, it must be possible to assign type-schemes to the identifiers thus declared.

Note that types are absent from the languageExp. Assuming a set oftype variablesα and ofprimitive typesι, the syntax oftypesτand oftype-schemesσis given by

τ::=α|ι|ττ σ::=τ| ∀ασ

A type-scheme∀α1. . .∀αnτ(which we may write∀α1. . .αnτ) hasgenerictype vari- ablesα1. . .αn. Amonotypeµis a type containing no type variables.

3 Type instantiation

IfSis a substitution of types for type variables, often written[τ11, . . . ,τnn]or [τii], andσis a type-scheme, then Sσis the type-scheme obtained by replacing each free occurrence ofαiinσbyτi, renaming the generic variables ofσif necessary.

ThenSσ is called aninstanceofσ; the notions of substitution and instance extend naturally to larger syntactic constructs containing type-schemes.

By contrast a type-schemeσ=∀α1. . .αmτ has ageneric instanceσ0=∀β1. . .βnτ0 ifτ0= [τii]τfor some typesτ1, . . . ,τm and theβjare not free inσ. In this case we shall writeσ > σ0. Note that instantiation acts on free variables, while generic instantiation acts on bound variables. It follows thatσ > σ0impliesSσ >Sσ0.

4 Semantics

The semantic domainV forExpis a complete partial order satisfying the following equations up to isomorphism, whereBi is a cpo corresponding to primitive typeιi:

V =B0+B1+· · ·+F+W (disjoint sum)

F =V→V (function space)

W ={·} (error element)

To each monotypeµcorresponds a subsetV, as detailed in[5]; ifv∈V is in the subset forµwe writev:µ. Further we writev:τifv:µfor every monotype instance µofτ, and we writev:σifv:τfor everyτwhich is a generic instance ofσ.

Now letEnv=Id→V be the domain of environmentsη. The semantic function

":Exp→Env→V is given in[5]. Using it, we wish to attach meaning to assertions of the form

A|=e:σ

wheree∈ExpandAis a set of assumptions of the formx:σ,x∈Id. If the assertion is closed, i.e. ifAandσ contain no free type variables, then the sentence is said to hold iff, for every environmentη, wheneverη[[x]]:σ0for each memberx:σ0ofA, it follows that"[[e]]η:σ. Further, an assertion holds iff all its closed instances hold.

(4)

Thus, to verify the assertion

x:α,f :∀β(β→β)|= (f x):α

it is enough to verify it for every monotypeµin place of α. This example illus- trates that free type variables in an assertion are implicitly quantified over the whole assertion, while explicit quantification in a type scheme has restricted scope.

The remainder of this paper proceeds as follows. First we present an inference system for inferring valid assertions. Next we present an algorithmW for computing a type- scheme for any expression, under assumptionsA. We then show thatW issound, in the sense that any type-scheme it derives is derivable in the inference system. Fi- nally we show thatW iscomplete, in the sense that[any]derivable type-scheme is an instance of that computed byW.

5 Type inference

From now on we shall assume thatAcontains at most one assumption about each identifierx.Axstands for removing any assumption aboutxfromA.

For assumptionsA, expressionseand type-schemeσwe write A`e:σ

if this instance may be derived from the following inference rules:

TAUT: (xA)

A`x:σ

A`e:σ

INST: (σ > σ0)

A`e:σ0 A`e:σ

GEN: not free inA)

A`e:∀ασ

A`e:τ0τ A`e00

COMB:

A`(e e0):τ Ax∪ {x:τ0} `e:τ

ABS: A`(λx.e):τ0τ

A`e:σ Ax∪ {x:σ} `e0

LET: A`(letx=eine0):τ

The following example of a derivation is organised as a tree, in which each node follows from those immediately above it by an inference rule.

TAUT:

x:α`x:α

ABS: `(λx.x):αα

GEN:

`(λx.x):∀α(α→α)

TAUT:

i:∀α(α→α)`i:∀α(α→α)

INST:

i:∀α(α→α)`i:(α→α)→(α→α)

TAUT:

i:∀α(α→α)`i:∀α(α→α)

INST:

i:∀α(α→α)`i:αα

COMB:

i:∀α(α→α)`i i:αα

(5)

`(λx.x):∀α(α→α) i:∀α(α→α)`i i:αα

LET: `(leti= (λx.x)ini i):αα

The following proposition, stating the semantic soundness of inference, can be proved by induction one.

Proposition 1(Soundness of inference). If A`e:σthen A|=e:σ.

We will also require later the following two properties of the inference system.

Proposition 2. If S is a substitution and A`e:σthen SA`e:Sσ. Moreover if there is a derivation of A`e:σof height n then there is also a derivation of SA`e:Sσof height less[than]or equal to n.

Proof. By induction onn.

Lemma 1. Ifσ > σ0and Ax∪ {x:σ0} `e:σ0then also Ax∪ {x:σ} `e:σ0.

Proof. We construct a derivation ofAx∪ {x:σ} `e:σ0from that ofAx∪ {x:σ0} ` e:σ0by substituting each use ofTAUTforx:σ0withx:σ, followed by anINSTstep to derivex:σ0. Note thatGENsteps remain valid since ifαoccurs free inσthen it also occurs free inσ0.

6 The type assignment algorithm W

The type inference system itself does not provide an easy method for finding, given Aand e, a type-schemeσ such thatA`e:σ. We now present an algorithmW for this purpose. In fact,W goes a step further. GivenAand e, ifW succeeds it finds a substitutionSand a typeτ, which are most general in a sense to be made precise below, such that

SA`e:τ.

To defineW we require the unification algorithm of Robinson[6].

Proposition 3(Robinson). There is an algorithm U which, given a pair of types, either returns a substitution V or fails; further

(i) If U(τ,τ0)returns V , then V unifiesτandτ0, i.e. Vτ=τ0.

(ii) If S unifiesτandτ0then U(τ,τ0)returns some V and there is another substitution R such that S=RV .

Moreover, V involves only variables inτandτ0.

We also need to define the closure of a typeτwith respect to assumptionsA;

A(τ) =∀α1, . . . ,αnτ

whereα1, . . . ,αnare the type variables occurring free inτbut not inA.

(6)

AlgorithmW.

W(A,e) = (S,τ)where1

(i) If e is x and there is an assumption x:∀α1, . . . ,αnτ0 in Athen S = Id2 and τ= [βii0where theβis are new.

(ii) Ifeise1e2then letW(A,e2) = (S1,τ2)andW(S1A,e2) = (S22)andU(S2τ12β) =V whereβis new; thenS=V S2S1andτ=Vβ.

(iii) Ifeisλx.e1then letβbe a new type variable andW(Ax∪{x:β},e1) = (S1,τ1);

thenS=S1andτ=S1βτ1.

(iv) Ifeisletx=e1ine2then letW(A,e1) = (S1,τ2)andW(S1Ax∪{x:S1A(τ1)},e2) = (S22); thenS=S2S1andτ=τ2.

NOTE: When any of the conditions above is not metW fails.

The following proposition proves thatW meets our requirements.

Proposition 4(Soundness ofW). If W(A,e)succeeds with(S,τ)then there is a deriva- tion of SA`e:τ.

Proof. By induction oneusing proposition 2.

It follows that there is also a derivation of SA` e:SA(τ). We refer to SA(τ)as a type-scheme computed byW foreunderA.

7 Completeness of W

GivenAande, we will callσPaprincipal type-schemeofeunder assumptionsAiff (i) A`e:σP

(ii) Any otherσfor whichA`e:σis a generic instance ofσP.

Our main result, restricted to the simple case whereAcontains no free type variables, may be stated as follows:

IfA`e:σfor someσ, thenW computes a principal type scheme fore underA.

This is a direct corollary of the following general theorem which is a stronger result suited to inductive proof:

Theorem (Completeness ofW). Given A and e, let A0be an instance of A andσ a type-scheme such that

A0`e:σ

1[There are obvious typographic errors in parts (ii) and (iv) which are in the original publication. I have left the correction of these as an easy exercise for the reader.]

2[Of course this is the identity (empty) substitution, not the setIdof identifiers.]

(7)

then

(i) W(A,e)succeeds.

(ii) If W(A,e) = (S,τ)then, for some substitution R,

A0=RSA and R SA(τ)> σ.

In fact, from the theorem one also derives as corollaries that it is decidable whether ehas any type at all under the assumptionsA, and that, if so, it has a principal type- scheme underA.

The detailed proofs of results in this paper, and related results, will appear in the first author’s forthcoming Ph.D. Thesis.

References

[1] M. Coppo. An extended polymorphic type system for applicative languages. In Lecture Notes in Computer Science, volume 88, pages 192–204. Springer, 1980.

[2] A. Demers and J. Donahue. Report on the programming language russell. Tech- nical Report TR-79-371, Computer Science Department, Cornell University, 1979.

[3] R. Hindley. The principal type-scheme of an object in combinatory logic.Trans- actions of the AMS, 146:29–60, 1969.

[4] R. Milner M. Gordon and C. Wadsworth. Edinburgh LCF. InLecture Notes in Computer Science, volume 78. Springer, 1979.

[5] R. Milner. A theory of type polymorphism in programming. JCSS, 17(3):348–

375, 1978.

[6] J.A. Robinson. A machine-oriented logic based on the resolution principle.Jour- nal of the ACM, 12(1):23–41, 1965.

Referenzen

ÄHNLICHE DOKUMENTE

The algorithm operates as follows: It takes the regular inequality to prove as argument and retrieves the leading simple type names and element names from the left hand side

The research part of this dissertation focuses on the following areas: (i) studies of BPV1 and EBV segregation elements with heterologous replicons; (ii) discus- sion

As solving this problem exactly is time-consuming, a greedy evaluation procedure is used for approximating the quality of intermediate leader solution candidates, which is described

For Chu and Beasley’s GA and CRH-GA, the table contains for each problem in- stance the average percentage gap of the runs’ final solu- tions, the corresponding standard

Throughout this analysis, the following set of controls are used: the neighbourhood-effect, the number of previous arrests of the defendant, the race and gender of the

Cílem č lánku bylo analyzovat závislosti stupn ě materiální deprivace na vy- braných kategoriálních ukazatelích domácností, resp.. vysv ě tlit vztah mezi

In the particular case where K and L are outer parallel bodies of convex bodies at distance r > 0, the result will be deduced from a recent (local) translative integral formula

Gruppenalgebran Gber nicht--zyklischen p-Gruppen~ J.Reine Ang.Math.. The indecozposable representations of the dihedral