A simple applicative language: mini-ML

1. I n t r o d u c t i o n ML is a programming language with very interesting characteristics from the standpoint of static and dynamic semantics. • ML is a strongly typed language but there is no type declaration: expressions axe typed implicitly. • ML exhibits polymorphism: it is possible to define functions that work uniformly on arguments of many types. • ML allows the definition of higher-order functions: the value of an ML expression may be a closure. ML typechecking is the object of numerous discussions in the literature, e.g. [1], [4], [6], 113], and the use of an inference s~sttm to describe ML typing is now widely accepted. On the other hand recent work of Curien and Cousineau [31 has shown how to compile ML into code for an abstract machine, the Categorical Abstract Machine {CAM). Hence we are in a position to describe formally and completely three aspects of ML: typechecking, dynamic semantics, and translation into CAM code. Without loss This work is part ial ly supported under ESPRIT, p. 348 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 the publication and its 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 specfic permission. © 1986 A C M 0-89791-200-4/86/0800-0013 75¢ 13 of generality, we restrict ML to a central part christened Mini-ML, a simple typed ~-calculus with constants, products, conditionals, and recursive function definitions. 1.1. S a m p l e p r o g r a m s To illustrate Mini-ML, we introduce several examples in concrete syntax. Firs t of course is how to write and use the factorial function: l e t r e c / a c t = ~z. if z = 0 t h e n 1 else z */act (z 1) in fact 4 The next example shows the definition of a higher order function twice: le t succ ffi Az.z + 1 in le t twice ffi Af .Az . ( f ( f z)) in ucc) 0) This expression uses block structure: l e t i =5 in le t i = i + 1 in i Here we have both simultaneous definitions and block structure: let (z,y) = (2,3) in le t (z ,y) = (y, z) in z This example has simultaneous recursive definitions: le t rec (even, odd)= (Az. if z ffi 0 t h e n true else odd(z 1), Az. i f z --0 t h e n false else eve n (z 1) in even(3) Finally here is a typical example of polymorphism: le t f = ~z.z in f f The examples above, which we use throughout this paper, axe believed to reflect the main intricacies in static and dynamic semantics of ML. The remainder of this paper is divided into four parts. In Section 2, we discuss the static semantics of Mini-ML. First , we present the Damas-Milner type inference system for the simple A-calculus par t of Mini-ML. Then we show how this system, which is non.deterministic, can be turned into • deterministic inference system. The latter system can be read as an 41gorithm to type-check Mini-ML expressions. Finally we give complementary rules to deal with products and lztre~. In Section 3, the dynamic semantics of Mini-ML is also given as an inference system. The nicer points of this system are recur•lye function specific•tion and handling of products. To compile Mini-ML, we use CAM as the target machine. The CAM is • very simple abstract machine. The formal system in Section 4 specifies the transitions of the machine. Finally Section 5 cont•ins translation rules from Mini-ML to CAM, again in the same style. 1.2. A b s t r a c t S y n t a x o f Min i -NIL The abstract syntax given below describes A-calculns extended with le t , l e t re¢ , if, and products. Furthermore, in an expression AP.e, P may be either an identifier or • (tree-like) pattern. For example A(z,~).e is ~ valid expres• ion and so is ~ ( z , ( ( p , z ) , t ) ) . e ' . The constructor mlpoir builds products of expressions, while the pairpat constructot serves in building pat terns of identifiers. The nullpat constructor is used for the unit object 0 , which is both a pattern and an expression. s o r t s EXP, IDENT, PAT, NULLPAT sub•o rS• EXPD NULLPAT, IDENT PATD NULLPAT, IDENT c o n s t r u c t o r s 'Patterns' pairpat : PATxPAT ---* PAT nullpat : --* NULLPAT 'Ezpre#Mona ' ident : --¢ IDENT number : ~ EXP false : --* EXP true : --* EXP apply : EXPx EXP --* EXP mlpsir : EXP xEXP --, EXP lambda : PATx EXP --, EXP let : PATxEXPxEXP --, EXP letrec : PATxEXPxEXP --* EXP if : EXPXEXPXEXP -..¢ EXP Fi#urc 1. Abstract Syntax of Mini-ML 2. S t a t i c S e m a n t i c s Before introducing an inference system that assigns types in Mini-ML we must define the type language. In typed A-calculus every object has • type. Thus the type langu•ge must be •ble to express b~ic t ~ e s as well as /unctions/ types. For example, the type of the successor function Az.z + I is in~ --* inL In the same way the ident i ty function Az.x for integers has t ype ins -~ inS, but for booleans it has type boo/--* boo/. I t is clear that the ident i ty function rosy be defined without taking into account the type of its present parameter. To express this obstrac. • ion on the type of the parameter, the tppe •inviable a is bound by • quantifier: the polymorphic identity function has type Va.a --, a . 2.1. T h e T y p e L a n g u a g e The type language contains two syntactic categories, types and type schemes. T y p e s : a type • is either i. a basic type in•, boo/, i i . • type variable ~ , i l l • functional type T --. r S, where • and ~' are types, iv. a product type r × r r where • and r t are types. T y p e schemes : • type-scheme ¢ is either i. • type ~', ii. • type-scheme Va.o, where ¢ is a type-scheme. Remark: quantiflers may occur only s t the top level of type-schemes, they do not occur within type-schemes. A type expression in this language may have bo th / r e • and bound v • r i •b les . Let us write F V ( o ) and BV(o) for the sets of free and bound variables of • type expression ¢. Following [4], we now define two tel••ions between type expressions that contain type variables. De f in i t i on 2.1. A type scheme o ~ is called an i n • a n t s d • type scheme ~r/t" there exists • substitution S o f types/or free type variables such that :

[1]  Joëlle Despeyroux,et al.  Proof of Translation in Natural Semantics , 1986, LICS.

[2]  David H. D. Warren Logic programming and compiler writing , 1980, Softw. Pract. Exp..

[3]  Pierre-Louis Curien,et al.  The Categorical Abstract Machine , 1987, Sci. Comput. Program..

[4]  Nancy McCracken,et al.  The Typechecking of Programs with Implicit Type Structure , 1984, Semantics of Data Types.

[5]  Alan Mycroft,et al.  Polymorphic Type Schemes and Recursive Definitions , 1984, Symposium on Programming.

[6]  Patrick J. Hayes,et al.  Computation and Deduction , 1973, MFCS.

[7]  Robin Milner,et al.  A Theory of Type Polymorphism in Programming , 1978, J. Comput. Syst. Sci..

[8]  Thierry Despeyroux,et al.  Executable Specification of Static Semantics , 1984, Semantics of Data Types.

[9]  David B. MacQueen Modules for standard ML , 1984, LFP '84.

[10]  Robin Milner,et al.  Principal type-schemes for functional programs , 1982, POPL '82.

[11]  John C. Reynolds,et al.  Three Approaches to Type Structure , 1985, TAPSOFT, Vol.1.