Loyola University Chicago Loyola eCommons
Computer Science: Faculty Publications and Other Works Faculty Publications
1-1996
Putting Type Annotations to Work
Martin Odersky Universität Karlsruhe
Konstantin Laufer Loyola University Chicago, [email protected]
Follow this and additional works at: https://ecommons.luc.edu/cs_facpubs
Part of the Computer Sciences Commons
Recommended Citation M. Odersky and K. Läufer, Putting type annotations to work, in Proceedings of the 23rd ACM SIGPLAN- SIGACT symposium on Principles of programming languages, ser. POPL '96. New York, NY, USA: ACM, 1996, pp. 54-67. [Online]. Available: http://webpages.cs.luc.edu/~laufer/papers/popl96.pdf
This Conference Proceeding is brought to you for free and open access by the Faculty Publications at Loyola eCommons. It has been accepted for inclusion in Computer Science: Faculty Publications and Other Works by an authorized administrator of Loyola eCommons. For more information, please contact [email protected].
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License. Copyright © 1996 Martin Odersky and Konstantin Läufer
Putting Typ e Annotations to Work
Martin Odersky Konstantin Läufer
Department of Computer Science Department of Mathematical Sciences
University of Karlsruhe Loyola University Chicago
76128 Karlsruhe, Germany Chicago, Illinois 60626, USA
o [email protected] [email protected]
Abstract egories. Curry-style reconstruction lls in p olymorphic
abstractions and applications together with typ e an-
We study an extension of the Hindley/Milner system
notations. This style of reconstruction is complicated
with explicit typ e scheme annotations and typ e decla-
by the lack of principal typ es in F . The prop osed
2
rations. The system can express p olymorphic function
schemes all have rather complex inference rules with
arguments, user-dened data typ es with abstract com-
cumb ersome conversions b etween declared and inferred
p onents, and structure typ es with p olymorphic elds.
typ es [McC84, OG89]. By contrast, Church-style re-
More generally, all programs of the p olymorphic lambda
construction requires the p osition of typ e abstractions
calculus can b e enco ded by a translation b etween typ-
and applications to b e indicated in the original source.
ing derivations. We show that typ e reconstruction in
This style of reconstruction also called partial typ e
this system can b e reduced to the decidable problem of
reconstruction [Bo e89] was shown to b e reducible to
rst-order unication under a mixed prex.
higher-order unication [Pfe88]. Even though Church-
style reconstruction is thus undecidable in general, this
result op ens up the p ossibility for semi-decision pro ce-
1 Intro duction
dures that work well in practice. On the other hand,
the p osition of a p olymorphic application has to b e in-
Two of the most imp ortant cornerstones of typ e theory
dicated explicitly in the source, which leads to a rather
for programming languages are the Hindley/Milner sys-
unfamiliar co ding style, at least for programmers used
tem and the second-order p olymorphic -calculus. This
to the Hindley/Milner system.
pap er tries to explore some of the design space b etween
Recently there havebeenseveral approaches towards
them.
extending the Hindley/Milner system with some form
The Hindley/Milner system [Mil78] extends the
of emb edded quantiers without going all the wayto
simply-typ ed -calculus with p olymorphic let-b ound
the p olymorphic -calculus. For instance, Launch-
identiers. It thus adds considerable expressivepower
bury and Peyton Jones have presented an elegant
yet retains the prop erty thatnotyp e annotations in
typ e system for syntactic control of interference [LPar]
programs are needed, since most general typ es can b e
that uses second-order universal quantication. Perry
inferred [DM82]. This prop erty has made the Hind-
[Per90] and Läufer and Odersky [LO94]have studied
ley/Milner system very app ealing as a basis of typ e
existential quantication in algebraic datatyp es, which
systems for programming languages.
yields a Hindley/Milner style version of Mitchell and
By contrast, the second-order p olymorphic -
Plotkin's abstract typ es [MP88]. This style of existen-
calculus F [Gir71, Rey74] allows p olymorphic typ es
2
tial quantication has b een implemented in compilers
everywhere, but requires explicit annotations of b oth
for Hop e [Per90], Haskell [Aug94] and CAML [MP93].
argumenttyp es and typ e instantiations. The general
Rémy [Rém94] has extended Läufer and Odersky's sys-
problem of typ echecking without typ e annotations is
tem with universal quantication in datatyp es, so that
undecidable [Wel94], but there havebeenseveral ap-
ob jects with p olymorphic metho ds can b e expressed.
proaches towards typ e reconstruction where some typ e
Jones [Jon95] has investigated record typ es with p oly-
information is given. These generally fall into two cat-
morphic elements as a way to capture essential asp ects
of mo dule systems. A prop osal along these lines has
b een accepted for inclusion in Haskell 1.3.
It seems that a combination of all of the ab ove sys-
tems, while feasible, would b e rather unwieldy. Fortu-
nately, it turns out that it is go o d enough to consider as
a generalization a far simpler typ e system that captures only to typ es, never to typ e schemes. We get back
the extensions' commonalities and expresses their dif- the full p ower of F in an indirect way,by allowing
2
ferences via enco dings. The extensions all have in com- typ e schemes as comp onents of explicitly declared
mon that some form of explicit typ e information is re- data typ es. We show that we can enco de all of F
2
quired. For instance, Läufer and Odersky's and Rémy's by providing typ e declarations for all p olymorphic
systems restrict existential quantication to the com- typ es in a given F program. This shows that our
2
p onents of explicitly declared datatyp es, while Jones typing discipline provides essentially the same ca-
restricts universal quantication to elds of explicitly pabilities as F ,even though the enco ding in F
2 2
declared record typ es. do es not supp ort a formal comparison of expres-
Here we study a typ e system that allows but do es sivepower in the sense of Felleisen [Fel90] since it
not require explicit typ e scheme annotations for func- fails to b e comp ositional.
tion arguments. The idea is that a formal function pa-
Our typing discipline is a conservative extension of
rameter is p olymorphic only if annotated with a typ e
the Hindley/Milner system. Every typable program in
scheme; otherwise the parameter is monomorphic, i.e. it
that system continues to b e typable. This holds also if
has a typ e, not a typ e scheme. As an imp ortant sp ecial
typ e annotations in the style of ML or Haskell are added
case we admit a rudimentary form of user-dened data
to Hindley/Milner. Wewere able to show principal
typ e declaration that intro duces a value constructor
typ e prop erties and soundness and completeness of typ e
with a single, p ossibly p olymorphic argument. Finally,
inference fully analogous to the results stated by Damas
we also allowtyp e scheme annotations for expressions.
and Milner [DM82]. Since the engineering issues of
Note that this is roughly the kind of typ e annota-
ML-like programming languages and typ e checkers are
tions that most programming languages oer or require.
bynowwell understo o d, we b elieve that this makes
The crucial extension of this pap er is that annotations
our system promising as a practical kernel language on
and declarations can refer to p olymorphic typ e schemes
whichtyp e-systematic extensions of ML or Haskell can
instead of just typ es. The ramications of this simple
b e based.
idea are quite substantial.
The rest of this pap er is organized as follows. Sec-
tion 2 presents our typ e system. Section 3 shows how
We can express p olymorphic function arguments
previous p olymorphic extensions of ML can b e em-
by annotating the argument with a typ e scheme.
b edded in it. Section 4 discusses an enco ding of the
We can express data typ es and record typ es by
p olymorphic -calculus. Section 5 states the most gen-
their usual Church enco dings in a typ e-correct
eral instantiation problem and presents an algorithm to
way.
solve it. Section 6 presents a typ e inference algorithm.
Section 7 concludes.
By slightly mo difying these Church enco dings, we
can also express existentially or universally quan-
tied comp onenttyp es of records and data typ es,
2 The Typ e System
thereby subsuming the typ e systems of Perry,
Figure 1 presents the abstract syntax of our kernel lan-
Läufer and Odersky,Rémy, and Jones. The en-
guage, Exp: . As in the Hindley/Milner system, we
co dings give us principal typ e prop erties and typ e
distinguish b etween typ es, which cannot contain quan-
inference algorithms for these systems for free .
tication over typ e variables, and typ e schemes, which
Unlike the situation in the simply typ ed -calculus
can. Compared to Milner's language Exp there are two
[Mor68] or ML [Mil78], it is no longer p ossible to
extensions that can b e considered indep endently, but
reduce typ e inference to a simple Herbrand uni-
that are most useful in combination. One extension
cation problem. We need to consider instead
considers typ e annotations for formal arguments and
the problem of nding a most general substitution
expressions; the other considers typ e declarations.
that makes one typ e scheme an instance of an-
other. Weshow here that this problem is reducible
Typ e Scheme Annotations
to the problem of rst-order unication under a
mixed prex [Mil92], which is decidable. Decid-
Typ e scheme annotations can b e applied to formal ar-
ability holds b ecause we still admit only typ es
guments in -abstractions x: :e and to expressions
and not typ e schemes in the range of substitutions
e: . Annotations with typ es are common in program-
otherwise the problem would b e equivalentto
ming languages that build on the Hindley/Milner sys-
semi-unication, which is undecidable [KTU89].
tem. For instance,
Unlike the situation in F ,we still maintain a
map = f: a ! b. xs: [a]. case xs of ...
2
stratication b etween typ es and typ e schemes. A
declares the argumenttyp es of function map in terms of
universally quantied variable can b e instantiated
twotyp e variables a and b. By generalizing over these
Variables x; y ; z
Typ e Constructors T
0 0
Expressions e = x j x:e j ee j let x = e in e Exp terms
j x: :e j e : annotated terms
j T typ e injection