• No results found

A STUDY OF SEMANTICS, TYPES AND LANGUAGES FOR DATABASES AND OBJECT-ORIENTED PROGRAMMING ATSUSHI OHORI. Computer and Information Science

N/A
N/A
Protected

Academic year: 2021

Share "A STUDY OF SEMANTICS, TYPES AND LANGUAGES FOR DATABASES AND OBJECT-ORIENTED PROGRAMMING ATSUSHI OHORI. Computer and Information Science"

Copied!
213
0
0

Loading.... (view fulltext now)

Full text

(1)

A STUDY OF SEMANTICS , TYPES AND LANGUAGES

FOR DATABASES AND

OBJECT-ORIENTED PROGRAMMING

ATSUSHI OHORI

A DISSERTATION in

Computer and Information Science

Presented to the Faculties of the University of Pennsylvania in Partial Fulfillment of the Requirements for the Degree of Doctor of Philosophy.

1989

Peter Buneman Val Breazu-Tannen

Co-supervisor of Dissertation Co-supervisor of Dissertation

Jean Gallier

(2)

c

°

COPYRIGHT

ATSUSHI OHORI

1989

(3)

ABSTRACT

A STUDY OF SEMANTICS, TYPES AND LANGUAGES FOR DATABASES AND OBJECT-ORIENTED PROGRAMMING

ATSUSHI OHORI

CO-SUPERVISORS : PETER BUNEMAN AND VAL BREAZU-TANNEN

The purpose of this thesis is to investigate a type system for databases and object-oriented programming and to design a statically typed programming language for these applications. Such a language should ideally have a static type system that supports:

polymorphism and static type inference,

rich data structures and operations to represent various data models for databases including the relational model and more recent complex object models,

central features of object-oriented programming including user definable class hierarchies, multiple inheritance, and data abstraction,

the notion of extents and object-identities for object-oriented databases.

Without a proper formalism, it is not obvious that the construction of such a type system is possible. This thesis attempts to construct one such formalism and proposes a programming language that uniformly integrate all of the above features.

The specific contributions of this thesis include:

A simple semantics for ML polymorphism and axiomatization of the equational theory of ML. A uniform generalization of the relational model to arbitrary complex database objects that

are constructed by labeled records, labeled variants, finite sets and recursive definition. A framework for semantics of types for complex database objects.

The notion ofconditional typing schemes that extends Milner’s typing scheme for ML to a wide range of complex structures and operations.

(4)

The notion of views that enable us to represent object-oriented databases in an ML style static type system.

A proposal of a polymorphic programming language, Machiavelli, for databases and object-oriented programming with static type inference.

In addition to the above technical contributions, a prototype implementation of Machiavelli that embodies most of the features presented in this thesis has been done in Standard ML of New Jersey.

(5)

ACKNOWLEDGEMENTS

I am indebted to my co-adviser Peter Buneman. He originally introduced me the area of types and semantics for database programming languages and recommended me to undertake the doctoral study. Without his enthusiastic encouragement, this dissertation would have never existed. Special thanks are also to my co-adviser Val Breazu-Tannen for his guiding me the are of syntax and semantics of types for programming languages and for his encouragements. Moreover, their constructive criticism and our continuous discussion directly contribute to the work.

I am grateful to my external commitee members Tomasz Imielinski and John Mitchell for very useful discussion at several opportune moments and many helpful comments. I am as grateful to the other members of my thesis committee, Jean Gallier, Aravind Joshi, and Dale Miller for their interest in my work and their invaluable comments.

Many thanks are also to Luca Cardelli, Carl Gunter, Richard Hull, David MacQueen, Mitchell Wand and Aaron Watters for very helpful discussions and suggestions. Moreover Mitchell Wand made several constructive criticism on the draft paper on which a part of chapter 5 is based.

I am grateful to OKI Electric Industry Co., Japan. They granted my leave of absence for the study at University of Pennsylvania and provided financial support. Among them, special thanks are to Kenji Tateoka, Eiji Nohara, and Toshiyuki Matsumoto who gave me assistance and support at critical moments.

Finally my special gratitude goes to my wife Hiroko. Without her, the endevour of graduate study would have been arid and trying experience.

(6)

Contents

Abstract iii

Acknowledgements v

1 Introduction 1

1.1 Motivations and Purposes . . . . 1

1.2 Organization of Thesis . . . . 4

2 Mathematical Preliminaries 8 2.1 Basic Notions and Notations . . . . 8

2.2 Labeled Trees . . . . 10

2.2.1 Definitions and Basic Properties . . . . 10

2.2.2 Substitutions and Unifications on Trees . . . . 15

2.2.3 Regular Trees . . . . 16

3 Analysis of ML : its Syntax and Semantics 22 3.1 Introduction. . . . 22

3.1.1 Milner’s original semantics . . . . 24

3.1.2 Damas-Milner type inference system and Mitchell-Harper’s analysis . . . . . 25

3.1.3 A simple framework for ML polymorphism . . . . 29

(7)

3.2.1 Raw terms, types and type-schemes . . . . 30

3.2.2 Typings, typing schemes and terms of Core-ML . . . . 33

3.2.3 Type Inference Problem . . . . 35

3.2.4 Relation to Damas-Milner System . . . . 38

3.2.5 Equational theories of Core-ML. . . . 43

3.3 Semantics of Core-ML . . . . 45

3.3.1 Explicitly-typed language TΛ and its semantics . . . . 45

3.3.2 Relationship betweenTΛ and Core-ML . . . . 47

3.3.3 Semantics of Core-ML . . . . 50

3.4 Soundness and Completeness of Core-ML Theories . . . . 51

3.5 Extensions of Core-ML. . . . 52

3.6 Full Abstraction of ML. . . . 57

4 Database Domains 61 4.1 Introduction. . . . 61

4.1.1 Data Structures for Database Objects . . . . 62

4.1.2 Operations on Complex Objects . . . . 63

4.1.3 A Strategy to Generalize Join and Projection . . . . 64

4.2 Analysis of the Relational Model . . . . 65

4.3 Database Domains . . . . 75

4.4 A Type System for Complex Database Objects . . . . 79

4.4.1 Set of Description Types. . . . 79

4.4.2 Universe of Descriptions . . . . 84

4.4.3 Typing Relation . . . . 86

4.4.4 Description Domains . . . . 88

(8)

5 A Polymorphic Language for Databases and Object-Oriented Programming 99

5.1 Introduction. . . . 99

5.1.1 Records and Variants for Object-oriented Programming . . . . 101

5.1.2 Integrating Database Objects . . . . 108

5.2 Definition of Machiavelli . . . . 110

5.2.1 Types and Description Types . . . . 110

5.2.2 Raw Terms . . . . 110

5.2.3 The Proof System for Typings . . . . 115

5.3 Alternative Presentation of Raw Terms and Typings . . . . 115

5.4 Type Inference Problem . . . . 117

5.4.1 Conditional Typing schemes. . . . 118

5.4.2 Satisfiability of Conditions. . . . 126

5.5 Semantics of Machiavelli . . . . 134

5.5.1 Denotational Semantics of Machiavelli . . . . 134

5.5.2 Operational Semantics . . . . 136

5.6 Syntactic Shorthands. . . . 138

5.6.1 Recursive Function . . . . 138

5.6.2 Value Bindings and Function Definitions. . . . 139

5.6.3 Database Operations . . . . 139

5.7 Programming Examples . . . . 141

6 Parametric Classes for Object-Oriented Programming 147 6.1 Introduction. . . . 147

6.2 Raw Terms, Types, and Type-schemes . . . . 152

6.3 Syntax of Class Definitions . . . . 153

6.4 Proof System for Class Definitions and Typings . . . . 156

(9)

6.6 Type Inference for the Extended Language . . . . 160

6.7 Further Examples. . . . 166

6.8 Limitations and Implementation . . . . 168

7 Object-identities and Views for Object-oriented Databases 170 7.1 Introduction. . . . 170

7.2 Reference Types . . . . 173

7.3 Views for Representing Extents . . . . 175

8 Conclusion and Topics for Further Research 180 8.1 Semantics . . . . 181

8.1.1 A Concrete Models for Machiavelli . . . . 181

8.1.2 Call-by-Value Semantics for ML Polymorphism . . . . 181

8.1.3 Semantics of Class Declarations. . . . 182

8.1.4 Semantics of Object Identities. . . . 182

8.2 Extensions of the Language . . . . 182

8.2.1 General Set Operation . . . . 183

8.2.2 Heterogeneous Sets . . . . 184

8.3 Communicating to Existing Databases . . . . 186

8.4 Implementation Issues . . . . 187

A Abstract Syntax of Machiavelli 188

Bibliography 190

(10)

List of Figures

2.1 An example of a finite labeled tree . . . . 11

2.2 An example of an infinite labeled tree . . . . 12

3.1 Examples of Type Inference with Recursive Types . . . . 56

4.1 A Simple Relation and its Representation as a Table . . . . 66

4.2 Join of Relations Containing Null Values. . . . 69

4.3 Examples of Description Types . . . . 80

4.4 Examples of Ordering on Description Types . . . . 81

4.5 Examples of Descriptions . . . . 85

4.6 Examples of Typing Relation . . . . 87

5.1 A Simple Session in Machiavelli . . . . 142

5.2 A Simple Example Involvingjoinandproject . . . . 143

5.3 A Part-Supplier Database in Generalized Relational Model . . . . 143

5.4 Some Simple Queries . . . . 144

5.5 A Simple Implementation of Polymorphic Transitive Closure . . . . 144

5.6 Query Processing Using Polymorphic Functions . . . . 145

6.1 A Simple Interactive Session with Classes . . . . 167

(11)

Chapter 1

Introduction

1.1

Motivations and Purposes

The term “impedance mismatch” has been coined [75, 13] to describe the phenomenon that the structures and operations available in a programming language do not usually match those needed for database systems. This problem is painfully familiar to anyone who has used a high-level programming language to communicate with a database. This mismatch is particularly unfortunate when database programming cannot share the benefits of recent developments in the theory of types in programming languages. Among them most important ones are polymorphism [94, 78] andstatic type inference [78, 34], which should have had apparent practical advantages for many database applications. A similar situation exists in the field of object-oriented programming. In the development of object-oriented languages [44, 33], various practical ideas such asclasses and

multiple inheritancehave been proposed and implemented. The advantages of these features seem orthogonal to those of conventional notion of types and the integration of them into a type system of a programming language is highly desirable. However, a proper formalism that enables us to integrate these features in a type system with polymorphism and static type inference has not yet been well established.

The motivation of this thesis is to attempt to solve these mismatch problems and to develop a programming language that unifies databases and object-oriented programming in a modern type system. Such a language should provide a programming environment where the programmer can enjoy both the capability of database management and the desirable features of object-oriented

(12)

programming with all the benefits of a modern type system. Recent studies in the area of object-oriented databases [13, 17] and “semantic” data models [58] suggest that such integration is highly desirable. Such a language should be also suitable for many other applications whose main interest is manipulation of highly structured data such as knowledge representation [18] and natural language processing [97]. It is therefore hoped that the integration should also contribute to solve the “high-level” impedance mismatches between database systems and other applications.

One desirable feature of a programming language for those data intensive application is static type-checking. The main objective of static type-checking is to detect inconsistency in applications of operations to data before program execution. This eliminates certain programming errors at early stages of programming development. This should be particularly important for data intensive applications. Data structures such as schemes in database systems and class definitions in object-oriented programming are large and complex structures. Much of programming errors in these applications would show up astype errors were those data structures a part of the type structure of the program. Therefore a type system in which such errors can be anticipated by a static analysis of the program is, I believe, a prerequisite for a good programming language for data intensive applications. As argued in [106], static type-checking may also contributes to efficient execution of programs by eliminating run-time type-checking.

Until recently, statically typed languages were justly criticized for being too cumbersome. First, they did not allow certain types of generic code; and second the type declarations, while admittedly useful as documentation, were often tedious and obvious. With the inventions of polymorphism [94, 78] and static type inference [78, 34], however, I believe that a static type system can overcome these disadvantages. The ML family of languages – including Standard ML [48] and Miranda [107] – are successful examples. In those languages, the programmer is not required to specify types of programs. The type systeminfers a most general polymorphic type for any type correct programs. By this mechanism, those languages achieve much of the flexibility of dynamically typed languages like Lisp without sacrificing the advantages of the static type-checking. Moreover, I believe that an ML style type system can achieve a proper integration of databases and object-oriented systems in a statically typed programming language if it is extended with the following features:

1. records and variants to represent various data structures,

2. the structures and operations rich enough to represent various data models for databases including the relational model [29], nested relations [36, 62, 95] and complex object models [59, 14, 4],

(13)

3. user definable classes, data abstraction and multiple inheritance for object-oriented program-ming,

4. object-identities and extents for object-oriented data models [13, 17].

The last two years have seen considerable research into the integration of records and variants in an ML style type system to support an aspect of object-oriented programming [111, 105, 85, 63, 38, 93] – including a contribution of a part of this study ([85]) – which shows that the integration of the first is now possible. However, there seems no existing approach that integrates the other three features in an ML style type system. For 2, the problem is seen by simply noting that no existing polymorphic type system can represent even the relational model – perhaps the simplest form of a data model for databases. As pointed out in [10], no existing type system can type-check the polymorphicnatural joinoperation. For 3, there have been some efforts [7] and suggestions [63] toward the integration of user definable classes, data abstraction and inheritance in a static type system. However, to my knowledge, there is no formal system that integrates these features in an ML style type inference system. Moreover, there appears to be no static type system of any kind that successfully deals with 4. The purpose of this thesis is to develop an ML style type system that uniformly integrates all the above features and to propose a programming language having that type system.

It is also desirable for the language to have a clean mathematics semantics. Such a semantics should provide a better understanding of the interaction of various features of the language and should be useful for further extensions of the language. This thesis also attempts to construct a semantic framework for the polymorphic core of the proposed language.

The following example illustrates the flavor of the language proposed in this thesis. Consider a function which takes a set of records (i.e. a relation) with Name and Salary information and returns the set of all Name values that correspond to Salary values over 100K. For example, applied to the relation (set of records)

{[N ame="Joe", Salary= 23456], [N ame="Fred", Salary= 123456], [N ame="Helen", Salary= 132000]}

this function should yield the set{"Fred","Helen"}. Such a function is written in the language as follows:

(14)

wherex S

withx.Salary >100000;

The select. . .where. . .with form is simple syntactic sugar for more basic program structure. Although no data types are mentioned in the code, the type system infers the following type information

wealthy:{[(s1)Salary:int, N ame:s2]} → {s2}

which means that wealthy is a function that takes a homogeneous set of records, each of type [(s1)Salary:int, N ame:s2] and returns a homogeneous set of values of types2, wheres2is a type

variable ranging over all types on which equality is defined and [(s1)Salary: int, N ame:s2] is a

type variable ranging over arbitrary record types that containsSalary:intand N ame:s2 fields.

Consequently, the type system will allowwealthyto be applied, for example, to relations of type {[N ame:string, Age:int, Salary:int]}

and also to relations of type

{[N ame: [F irst:string, Last:string], W eight:int, Salary:int]}.

Moreover, the type system statically checks the type correctness of each application and computes the result type by instantiating the type variables.

1.2

Organization of Thesis

One of the purposes of this thesis is to design a typed programming language that is rich enough to represent various data structures used in a wide rage of data intensive applications. For such a language, recursively defined types and objects are essential. In order to treat recursive structures uniformly, we useregular trees as a mathematical tool to represent them. In order to make this thesis self-contained, I have gathered in chapter 2 standard results about regular trees as well as some standard mathematical notations and definitions. Everything there is standard knowledge. My recommendation to the reader is to go over this chapter quickly to familialize himself notations and then to use this chapter as references when needed.

The rest of the thesis consists of the following five investigation to achieve the purpose stated in the previous section:

(15)

1. an analysis of ML and a construction of a semantic framework for ML polymorphism, 2. a type system for complex database objects and its semantics,

3. an extension of ML type inference method to records, variants and complex database objects, 4. a formulation of classes for object-oriented programming,

5. a method to represent object-oriented databases in an ML style type system.

All of these five investigations lead to the proposal of Machiavelli – a polymorphic language for databases and object-oriented programming. The first of them gives a framework to understand ML’s syntactic properties and to define a denotational semantics. This will serve as a starting point of the development of Machiavelli. The second investigation formulates the structures of domains for databases and constructs a type system for complex database objects. Then, by combining the above two, the third investigation develops the polymorphic core of Machiavelli as an extension of ML. The rest of the two extend the core language to represent oriented systems and object-oriented databases. I think that this thesis is best read by reading from cover to cover going through all of the above five topics in that order. However, in order to reconcile diverse interests of the readers, I organize each of these five investigations as a relatively self-contained chapter (chapter 3 to chapter 7). For this reason, I do not make a independent chapter for backgrounds of this entire thesis but include an introductory section in each chapter giving enough background for each topic. In particular the analysis of ML in chapter 3 and the construction of a framework for database domains in chapter 4 can be read independently. The subsequent developments are of course based on the above two investigations. However, they can be read using the above two chapters as references. I will try to provide appropriate reference keys to major results in chapter 3 and 4. The readers whose main interest is type systems of programming languages and their denotational semantics would start with chapter 3 and then go to chapter 5 and 6. The readers whose main interest is data models and database programming may start with chapter 4 continue on chapter 5 and then go to chapter 7. The readers whose main interest is object-oriented programming and programming language design may go directly to chapter 5 and continue on chapter 6 and 7. In the rest of this section, I outline these five investigations.

In chapter 3, I begin our investigation with ML type system. After analyzing the existing approachs to ML’s type inference system and its semantics, an alternative proof system for ML type inference is given. This proof system only requires simple types but is shown to be equivalent (in a very natural sense) to the proof system given by Damas and Milner [34]. A semantic framework for ML polymorphism is then proposed and an axiomatization of the equational theory of ML terms

(16)

is given. The proposed framework requires no more semantic material than what is needed for modeling the simple type discipline, yet it provides a better account for ML’s implicit type system. The axiomatization of the equational theory corresponds exactly to the proposed semantics. The analogs of the completeness theorems that Friedman proved [37] for the simply typed lambda calculus are proved with respect to the proposed semantics. The framework is then extended to languages with various type constructors, constants and recursive types (via regular trees). At the end of the chapter, it is also shown that certain full abstraction result for typed languages can be transferred to the corresponding ML like languages.

Chapter 4 investigates type systems for databases and their semantics and proposes a concrete type system for complex database objects. It starts the investigation with the analysis of the relational model. The relational model is characterized independently of the underlying tuple structure. By generalizing this abstract analysis, the notions ofdatabase type systemsanddatabase domains are defined as characterizations of the structures of type systems for databases and their semantics domains. Based on these abstract characterizations, a concrete database type system for complex database objects and its database domain are constructed. The proposed type system allows arbitrary complex structures constructed by labeled records, labeled variants, finite sets and recursive definition. Moreover, it is a proper generalization of the relational model to those complex structures. In addition to standard operations for records, variants and sets, join and projection

are available as polymorphically typed computable functions on those complex structures (even on recursively defined structures).

By combining the results of the previous two chapters, chapter 5 defines a programming language as an extension of ML. This language is the polymorphic core of Machiavelli – the language I propose in this thesis. In order to develop a type inference algorithm for the core language, a new paradigm for type inference – the notion ofconditional typing schemes– is proposed. By allowing typing schemes to include conditions on substitutions of type variables, it extends Milner’s method uniformly to a wide range of structures and operations. Using this mechanism, an algorithm to compute a principal conditional typing scheme is developed. At the end of the chapter, extended examples of database programming are given.

Chapter 6 extends the core language with parameterized class declarations, which allows the programmer to build a hierarchy of classes connected by multiple inheritance declarations. This extension achieves a proper integration of multiple inheritance in object-oriented programming and ML style abstract data types with type parameterization. A formal system to type-check class definitions and to infer typings of raw terms containing method names are defined. It is then shown that the extended language is sound with respect to the type system of the core language

(17)

by showing the property that if a typing is provable in the extended type system then the typing obtained by unfolding all class names and method names by their implementations is provable in the type system of the core language. The type inference algorithm for the core language defined in the previous chapter is then extended to include classes. It is shown that it still computes a principal conditional typing scheme for any typable raw terms.

Chapter 7 presents a method to represents object-oriented databases. Major properties of objects in object-oriented databases can be captured by the combinations of references and variants. I then introduce the notion of views (sets of structures with “identities”) and show that various operations in object-oriented databases are naturally represented by the combination of viewing functions and the operation join and projection generalized in chapter 4.

Chapter 8 concludes the thesis and discuss some possible topics for further investigation. Appendix contains the abstract syntax of Machiavelli (including classes definitions).

(18)

Chapter 2

Mathematical Preliminaries

2.1

Basic Notions and Notations

Thedomain and the range of a function f are denoted by dom(f) and ran(f) respectively. We write f : A B for a function f such that dom(f) =A and ran(f) = B. If f is a function and A dom(f) then f(A) is the set {f(a)|a A}. The restriction of a function f to a set A⊆dom(f), denoted byfA, is the functionf0 such thatdom(f0) =A and f0(a) =f(a) for all

a∈A. We write{x1:=v1, . . . , xn:=vn}for the function f such thatdom(f) ={x1, . . . , xn} and

f(xi) =vi(1≤i≤n) (assuming that allxi are distinct). For a function f : A→B, f{x:=v} is

the functionf0 such thatdom(f0) =A∪ {x},f(x) =v andf0(a) =f(a) for allasuch thata6=x.

Iff, gare functions such thatran(g)⊆dom(f) thenf◦gis thecompositionoff andgdefined as dom(f◦g) =dom(g) andf ◦g(x) =f(g(x)) for allx∈dom(g).

For a setA, P(A) is the set of all finite subset of A. IfA is a finite set then we denote by|A| the cardinality ofA. LetA, B be a sets. The product of A andB, denoted by A×B, is the set {(x, y)|x∈A, y∈B}. IfAis the singleton set{x} then we writex×B for{x} ×B. Similarly for B×x.

Arelation on a setAis a subset ofA×A. Ifris a relation onAandx, y∈A, we usually write x r yto denote (x, y)∈r. Letrbe a relation on a setA,x, y, zbe any elements inA. risreflexive

iffx r x. r issymmetric iffx r yimpliesy r x. r isantisymmetric iffx r y andy r ximpliesx=y. ristransitiveiffx r yandy r z impliesx r z.

(19)

an antisymmetric preorder. A partial orderronAis alinear order iff for anyx, y∈Aeither x r y ory r x.

Definition 2.1 (Preordered Sets and Partially Ordered Sets) A preordered set (P,¹) is a setP endowed with a preorder¹onP. A partially ordered set(or poset) (P,v)is a setP endowed with a partial ordervon P.

When the preorder (partial order)¹(v) is understood, we sometimes writeP for (P,¹) ((P,v)) and say a preordered set (a poset)P.

Let (P,¹) be a preordered set and A⊆P. An elementp∈P is anupper bound ofAiffa¹p for alla∈A. An element pis aleast upper bound ofA iff for any element ofx∈P,p¹xiffxis an upper bound ofA. An elementp∈P is alower bound of Aiffp¹afor alla∈A. An element pis agreatest lower bound ofAiff for any element ofx∈P,x¹piffxis a lower bound ofA. The following property is an immediate consequence of the definition:

Proposition 2.1 In a poset, least upper bounds and greatest lower bounds are unique.

Let (P,v) be a poset and A ⊆P. We writetA for the least upper bound of A anduA for the greatest lower bound ofA (if they exists). Forx, y∈P, we write xty and xuy respectively for t{x, y}and u{x, y}.

Definition 2.2 (Pairwise Bounded Join Property) A preordered set (P,¹) has the pairwise bounded join property iff for anyp1, p2∈P if{p1, p2}has an upper bound then it has a least upper

bound.

Let (P,¹) be a preordered set. For an elementp∈P, we denote by [p] theequivalence class

containingpdefined as [p] ={x|x≤p, p≤x}.

Definition 2.3 (Quotient Poset) The quotient poset [(P,¹)] of a preordered set (P,≤) is the poset(P/≡,¹/≡)whereP/≡={[p]|p∈P}and[p] ¹/≡[q]iffp¹q.

It is easily verified that the relation¹/≡ in the above definition is well defined in the sense that it

does not depend on the representatives of equivalence classes. For [(P,≤)], the following results is an immediately consequence of the definition:

(20)

2.2

Labeled Trees

We shall uselabeled treesto represent types and data structures. In particular, a class of trees called

regular tree play a central role. The main reference on this subject is a work by Courcelle [32]. In this section I have gathered from [32] definitions and standard results (with some adaptation of their presentations) that are useful in the subsequent investigation. I include proofs only for those results whose presentations differ from those found in [32].

2.2.1

Definitions and Basic Properties

LetX be a set of symbols. A string over A is a finite sequence of symbols in A. We denote by X∗ the set of all string overX. The empty string is denoted by ². We identify an element xX

and the string consisting of the symbolx(the string of length one). Theconcatenation of a string aand a stringb is denoted bya·b. A string ais aprefix of a stringbif there is another stringc such thatb=a·c. A prefixa of a stringb isproper ifa6=b. The length of a string ais denoted by|a|. (Note that the notation|x|is overloaded.) For a natural numbernandx∈X, we writexn

for the string ofn x’s. For convenience, we definex0=². If AX andaX, then we denote

bya·Athe set{a·b|b∈A}and byA/athe set{b|a·b∈A}.

Throughout this thesis, we assume that there is a given countably infinite set L of symbols (ranged over byl, l1, . . .), calledlabels, equipped with a linear order¿. For a technical convenience

we assume thatLis closed under products, i.e. there is an injective functionprodcode: (L×L)→ L. We use the injection prodcodeimplicitly and treat L as if it satisfies (L × L)⊂ L. In particular, (L × L)⊆ L. On (L × L) we define the mappingsfirst,secondinductively as follows:

first∗(²) = ²

first∗(a·(l1, l2)) = first∗(a)·l1

second∗(²) = ²

second∗(a·(l1, l2)) = second∗(a)·l2

On{(a, b)|a∈ L∗, b∈ L,|a|=|b|}, we define pair as follows: pair∗(², ²) = ²

pair∗(a·l

1, b·l2) = pair∗(a, b)·(l1, l2)

Fora∈(L × L), the following equation always holds:

(21)

f h k f l2 l1 l2 @ @ @ R ¡ ¡ ¡ ª @ @ @ R

Figure 2.1: An example of a finite labeled tree

The following definition of labeled trees is due to A¨ıt-Kaci[6]. LetF be a (not necessarily finite) set of symbols.

Definition 2.4 (Labeled Trees) A labeled F-tree is a functionα : A→F such that

1. Ais a prefix-closed subset of L∗, i.e. for anya, b∈ L, ifa·bA thenaA, and

2. Ais finitely branching, i.e. ifa∈Athen the set{l|a·l∈A} is finite.

Ifdom(α) is finite thenαisfinite otherwise it isinfinite. The set of all labeledF-trees and the set of all finite labeledF-trees are denoted respectively by T∞(F) andT(F).

As an example, letαbe the function such thatdom(α) ={², l1, l2, l1·l2}and

α(²) = f α(l1) = k

α(l2) = h

α(l1·l2) = f

Thenα is the tree shown in figure 2.1. As an example of infinite labeled trees, letlistint be the

following function:

dom(listint) = {(r·tl)n|n0} ∪

{(r·tl)n·r|n0} ∪

{(r·tl)n·l|n≥0} ∪ {(r·tl)n·r·hd|n0}

(22)

sum © © © © ¼ l HHHHrj nil ©©prod © © ¼ hd HHHHtlj int sum © © © © ¼ l HHHHrj nil ©©prod © © ¼ hd · · ·· · int

Figure 2.2: An example of an infinite labeled tree and

listint((r·tl)n) = sum

listint((r·tl)n·r) = prod

listint((r·tl)n·l) = nil listint((r·tl)n·r·hd) = int

Thenlistint is the tree depicted in figure 2.2. This tree can be regarded as a represetation of the

type of integer lists.

Ifα∈T∞(F) andadom(α) thenα/ais the treeα0 such thatdom(α0) =dom(α)/a, and for

allb∈dom(α0),α0(b) =α(a·b).

Definition 2.5 The set of subtrees of a tree α, denoted by Subtrees(α), is the set {α/a|a dom(α)}.

For any element f ∈F, we also denote by f the one node tree such that dom(f) = {²} and f(²) =f. Let α1, . . . , αn ∈T∞(F), l1, . . . , ln ∈ L and f ∈F. We write f(l1 =α1, . . . , ln =αn)

to denote the tree α such that dom(α) = {²} ∪(l1·dom(α1))∪ · · · ∪(ln·dom(αn)), α(²) = f,

α(li·a) =αi(a) for alla∈dom(αi) (1≤i≤n). For example, an equation that holds for the tree

listint in the above example can be written using this notation as follows:

listint = sum(l=nil, r=prod(hd=int, tl=listint)).

A labeled tree can be regarded as a notational variant of a tree defined in [32] based on atree domain [45]. Let N be the set of natural numbers and N+ be the set of positive ones. A tree

(23)

domainA is a subset of N∗

+ such that (1) for any a, b∈N+, ifa·b∈ Athen a∈A, (2) for any

a∈N∗

+ andn∈N+, if a·n∈A then for any 1≤i ≤n, a·i∈A. A ranked alphabet is a set of

symbolsF associated with a mappingr : F→N called aranking function.

Definition 2.6 Let F be a ranked alphabet with the ranking functionr. AnF-treeαis a function

α : A F such that A is a tree domain and for any a A if r(α(a)) = n then a·i A iff

1≤i≤n.

The set of allF-trees is denoted by t∞(F). For a set F of function symbols, F

F is the ranked alphabet F ×P(L) with the ranking function r defined as r((f, L)) = |L|. The set of labeled trees T∞(F) has one-to-one correspondence to the set of trees t∞(F

F). Let α T∞(F) with dom(α) =A. Define a functionθα : A→N+ by induction on the length|a| ofa∈Aas follows:

θα(²) = ²,

θα(a·l) = θ(a)·i whereiis the natural number such thatlis thei-th smallest label

under¿in{l|a·l∈A}. Also define a functionηα : A→FF as follows:

ηα(a) = (α(a),{l|a·l∈A}).

Now define a functionφ : T∞(F)→t∞(F

F) asdom(φ(α)) =θα(A) andφ(α)(a) =ηαα−1(a)).

Proposition 2.2 The functionφ : T∞(F)→t∞(F

F)is a bijection.

Proof We first show that θα is injective and therefore φ is well defined. By simple induction,

|θα(a)|=|a|. Supposeθα(a) =θα(b). We need to show that a=b. The proof is by induction on

the length of θα(a). Suppose θα(a) = ². Then|a| = |b| =|²| and thereforea =b = ². Suppose

θα(a) = c·i. |a| =|b| = |c|+ 1. Let a =a0 ·la and b = b0 ·lb. Then since θα(a) = θα(b) and

by the definition ofθ, θα(a0)·i=θα(b0)·i. This impliesθα(a0) =θα(b0). Then by the induction

hypothesis,a0 =b0. By the definition ofθ

α,la=lb and thereforea=b.

By the definition ofθα it is clear that θα(A) is a tree domain. For the arity restriction, suppose

r((φ(α))(a)) = n. Then (φ(α))(a) = (f,{l1, . . . , ln}) such that {l1, . . . , ln} = {l|(θα−1(a))·l

dom(α)}. Then by the definition ofθα,a·i∈dom(φ(α)) iff i≤n. Thereforeφ(α)∈t∞(FF).

Forβ ∈t∞(F

F), define a functionµβ ondom(β) by induction on the length of strings as follows:

µβ(²) = ²,

µβ(a·i) = µβ(a)·l wherelis thei-th smallest label under¿in L

(24)

and a functionνβ as νβ(a) =f iffβ(a) = (f, L) for someL. Now define a functionψont∞(FF)

such that dom(ψ(β)) = µβ(dom(β)) andψ(β)(a) = νββ1(a)). Similar to the proof that θα is

injective, it is shown thatµβ is injective. By the definition of µβ it is also clear thatµβ(dom(β))

is prefix closed. Thereforeψis well defined andψ(β)∈T∞(F).

Letα : A→F be any element in T∞(F). We showψ(φ(α)) =αby showing the properties that (1) for anya,a∈dom(α) iffa∈dom(ψ(φ(α))) and that (2) for anya∈A,α(a) = (ψ(φ(α)))(a). Since dom(ψ(φ(α))) = µφ(α)(θα(dom(α))), (1) is shown by showing that for all a dom(α),

µφ(α)(θα(a)) =a. Proof is by induction on the length ofa. Basis is trivila. Leta·l∈dom(α) and

ibe the natural number such thatlis theithsmallest label in{l|a·ldom(α)}. Then we have:

µφ(α)(θα(a·l))

= µφ(α)(θα(a)·i)

= (µφ(α)(θα(a)))·l0 wherel0 is theithsmallest label inLsuch that

φ(α)(θα(a)) = (f, L) for somef .

But by the definition ofφ,φ(α)(θα(a)) = (α(a),{l|a·l∈dom(α)}). Therefore l=l0 and hence by

the induction hypothesis,µφ(α)(θα(a·l)) =a·l. For (2), supposeψ(φ(α))(a) =f. By the definition

ofψ, φ(α)(µ−φ(1α)(a)) = (f, L) for someL. By the definition of φ, α(θ−1

α−φ(1α)(a))) =f. But we

have shown thatµφ(α)(θα(a)) =a. This implies thatθα−1(µ−φ(1α)(a)) =a. Thereforeα(a) =f.

The propertyφ(ψ(α)) =αis shown by similar reasoning. Thereforeφ−1=ψ.

Because of this connection, we can regard labeled trees as trees and vice versa. In particular, all properties on trees shown in [32] can be applied to labeled trees. In what follows, we will use the term trees for labeled trees.

Lemma 2.2 Let α, α0 be trees of the forms α=f(l

1=α1, . . . , ln =αn),α0=f(l01=α01, . . . , l0n=

α0

n). They are equal iff f =g,{l1, . . . , ln}={l01, . . . , l0n} andα/li=α0/li for all1≤i≤n.

The following two lemmas hold only for finite trees:

Lemma 2.3 (Definition by Structural Induction) There exists one and only one mappingξ: T(F)→A such that

1. ξ(f) =base(f)for allf ∈F,

(25)

wherebaseandstep are given mappings of the following types:

base : F →A, step : [

{l1,...,ln}∈P(L)

F ×(l1×A)× · · · ×(ln×A)→A.

Lemma 2.4 (Proof by Structural Induction) In order to prove a propertyP on the setT(F), it suffices to prove that:

1. for allf ∈F,P(f),

2. for all f F, {l1, . . . , ln} ∈ P(L), and α1, . . . , αn T(F), if P(α1), . . . , P(αn) then

P(f(l1=α1, . . . , ln=αn)).

2.2.2

Substitutions and Unifications on Trees

We first introduce trees containingvariables. LetV be a set ofvariablesdisjoint fromF. We denote byT∞(F, V) the set of trees generated by the set of function symbols F ∪V such that there are no outgoing edge from variable nodes, i.e.α∈T∞(F, V) iffα∈T∞(F ∪V) and ifα(a)∈V then there is nol∈ L such thata·l∈dom(α). T(F, V) is the set of finite trees inT∞(F, V).

Definition 2.7 (Substitutions) A first-order substitution (or simply substitution)θis a function fromV toT∞(F, V)such that θ(v)6=v for only finitely many vV. Let αT(F, V) be any tree. The result of simultaneous substitution ofθ(v) forv∈V in α, denoted by θ∗(α), is the tree

α0 defined as:

dom(α0) =dom(α)[{a·dom(θ(v))|adom(α), α(a) =v} and

α0(a) = α(a)ifadom(α)andα(a)6∈V,

α0(a) = θ(α(b))(c)if a=b·c, α(b)V.

Sinceαandθ(v) (for anyv) are trees,θ∗(α) is a well defined tree.

When restricted to finite trees, the above definiton of application of substitution is equivalent to the following inductive definition:

θ∗(v) = θ(v) ifvV,

θ∗(f(l1=α1, . . . , ln =αn)) = f(l1=θ∗(α1), . . . , ln=θ∗n))

(26)

which is also characterized by the unique homomorphic extension ofθ toT(F, V). The function θ∗ on T(F, V) can be also defined by a unique extension of the above inductive definition [32].

Since various syntactic structures such as pairs and sequences can be regarded as trees, we apply θ∗ directly to those syntactic structures containing trees.

Ifθ is a substitution, then we denote bydom(θ) the set{v|v∈V, θ(v)6=v}. LetV be a set of variables. Arestriction of a substitutionθ to V, denoted byθV, is the substitution θ0 defined as

follows: θ0(v) =    θ(v) ifv∈V v otherwise.

Note that notationsdom(θ) and θV are overloaded with the corresponding notions of functions.

Distinction of them should be clear from our usage of meta notations and the context. Ifdom(θ) = {l1, . . . , ln} and θ(vi) = αi(1 ≤i n) then we shall use the notation [v1 := α1, . . . , vn := αn]

and α[v1 := α1, . . . , vn := αn] for θ and θ∗(α) respectively. If θ, η are substitutions then their

composition is the substitution defined asθ∗η. In what follows, we will identify the mappingθ

withθ. In particular, we writeθ◦η for the composition ofθ andη

A substitution θ isground for a tree αifθ(α)∈T∞(F). A tree α0 is a substitution instance

(or simplyinstance) of αif there is some substitutionθ such that α0 =θ(α). If θ is gound forα

thenα0 is a ground instance ofα.

Definition 2.8 (Unifier) A substitution θ is a unifier of trees α, β∈T∞(F, V) if θ(α) = θ(β). A unifierθ is more general thanθ0 if there is another substitutionη such that θ0=ηθ.

Substitutions induce the following preorder on trees containing variables:

Definition 2.9 Let α, β T∞(F, V). αis more general than β, denoted by β -α, iff there is some substitutionθ such that β=θ(α).

2.2.3

Regular Trees

An important class of trees inT∞(F) is the set of regular trees. Since “‘all properties’ of regular trees are decidable”[32], they provide rich yet computationally feasible data structures for databases and other information systems.

Definition 2.10 (Regular Trees) A treeα∈T∞(F)is regular iff the setSubtrees(α)is finite.

(27)

On regular trees, the following properties hold. Proposition 2.3 1. T(F)⊂R(F)⊂T∞(F).

2. Any subtree of a regular tree is regular.

3. The set of symbols occurring in a regular tree is finite.

4. R(F)is closed under substitution. We mean by this that θ(α)is regular ifθ(v)is regular for allv∈V.

Intuitive way of understanding the definition is that regular trees are trees that contain only finite amount of information. This intuition corresponds to the property that a regular tree has a finite representation. There are several equivalent representations of regular trees. Following [6], we use Moore machines to represent them.

Definition 2.11 (Moore Machine) A Moore machine is a 5-tuple (Q, s,F, δ, o), where Q is a finite set of states, s is a distinguished element in Q called the start state, F is the set of output symbols,δ is a partial function from Q× L toQcalled the state transition function such that for anyq∈Q,{l∈ L|δ(q, l)is defined} is finite and ois the output function from QtoF.

In the above definition, the input alphabet is implicitly assumed to be the fixed setL of labels. Because of the restriction onδ, a Moore machine under the above definition behaves like a Moore machine under a standard definition (such as in [52]) where the input alphabetLis finite and δis defined as a total function onQ× L.

As is done in standard finite state automata [52], we extend δ to the partial function δ∗ on

Q× L∗ as follows:

δ∗(q, ²) = q,

δ∗(q, l) = q0 for alll∈ L such thatδ(q, l) =q0,

δ∗(q, a·l) = q00for alla∈ L∗, l∈ Lsuch thatδ∗(q, a) =q0, δ(q0, l) =q00.

A stateq∈Qisreachableif there is somea∈ L∗such thatδ(s, a) =q. ais called apath fromsto

q. Each stateq∈Qin a Moore machineM = (Q, s,F, δ, o) represents a function form a subset ofL∗

toF. DefineM(q) as the function such thatdom(M(q)) ={a∈ L∗(q, a) =q0 for some q0Q}

andM(q)(a) =o(δ∗(q, a)) for alladom(M).

The following theorem establishes the relationship between Moore machines and regular trees, which corresponds to the equivalence between regular trees andregular systems shown in [32].

(28)

Theorem 2.1 For any Moore machine M = (Q, s,F, δ, o), M(q) R(F) for any q Q. Con-versely, for any regular tree α R(F) there is a Moore machine M = (Q, s,F, δ, o) such that

α=M(s).

Proof LetM = (Q, s,F, δ, o) be a Moore machine. For anya, b∈ L∗, andqQ, ifδ(q, a·b) =q0

for someq0, then by the definition of δ,δ(q, a) =q00for some q00. Thereforedom(M(q)) is prefix

closed. By the restriction ofδ, {l|a·l ∈dom(M(q))} is finite. ThereforeM(q) ∈T∞(F). Since M(q)/a = M(q, a)) for all a dom(M(q)), |Subtrees(M(q))| ≤ |Q| and hence finite. This

establishesM(q)∈R(F).

Letα0∈R(F) be any regular tree with the set of subtrees Subtrees(α0) =0, . . . , αn}. Define

the Moore machine0= ({q0, . . . , qn}, q0,F, δ, o) as follows:

1. δis the function such thatδ(pi, l) is defined and equal toqj iffl∈dom(αi) andαi/l=αj,

2. ois defined aso(qi) =αi(²).

Then for anya∈ L∗, it is shwon by simple induction on|a|thatadom(M

α0(q0)) iffa∈dom(α0)

and0(q)(a) =f iffα0(a) =f. Therefore0(q) =α.

We say that a regular treeαis represented by a Moore machineM ifM(s) =α.

The following construction on Moore machines will be often useful to determine various relations on regular trees via Moore machines.

Definition 2.12 (Product Machine) Let 'be an equivalence relation on L. Given two Moore machinesM1= (Q1, s1,F1, δ1, o1)andM2= (Q2, s2F2, δ2, o2), a product machine of M1 and M2

modulo', denoted by(M1×M2)/', is the Moore machine(Q, s,F, δ, o)such that:

1. Q= (Q1∪ {$})×(Q2∪ {$})where $ is a new distinguished symbol that does not appear in

M1 or M2,

2. s= (s1, s2),

3. F = (F1∪ {$})×(F2∪ {$})

4. δ((x, y), l)is defined and equal to(x0, y0)iff one of the following holds:

(a) l= (l1, l2),l16= $, l26= $,l1'l2,x∈Q1, y∈Q2, andδ1(x, l1) =x0, δ2(y, l2) =y0,

(b) l= (l1, l2),l1 6= $, l2 = $,x∈Q1, δ1(x, l1) =x0,y0 = $and either y= $ or there is no

l0

(29)

(c) l= (l1, l2),l1 = $, l2 6= $, y∈Q22(y, l2) =y0,x0 = $and either x= $ or there is no

l0

1 such that l10 'l2 andδ1(x, l10)is defined,

5. o((x1, x2)) = (O1, O2)such that Oi=oi(xi)ifxi∈Qi otherwise Oi= $ (i∈ {1,2}).

If'is the identity relation = onLthen we writeM1×M2 for (M1×M2)/=. The construction of

a product machine is clearly effective.

For a given equivalence relation', '∗ is the equivalence relation onL∗defined as follows:

² '∗ ² a·l1 ' b·l2ifa ' bandl1'l2.

For product machines, the following are immediate consequences of the definition:

Lemma 2.5 Let M1 = (Q1, s1,F1, δ1, o1), M2 = (Q2, s2,F2, δ2, o2) and (Q, s,F, δ, o) = (M1×

M2)/'.

1. If δ∗(s, a) = (q

1, q2), q1 ∈Q1, q2∈Q2 then first∗(a)

' second∗(a) andδ∗

1(s1,first∗(a)) =q1,

δ∗

2(s2,second∗(a)) = q2. Conversely, if there are a, b such that a

' b, δ∗

1(s1, a) = q1 and

δ∗

2(s2, b) =q2 thenδ∗(s,pair∗(a, b)) = (q1, q2).

2. Ifδ∗(s, a) = (q, x), qQ

1 thenδ∗1(s1,first∗(a)) =qand first∗(o((q, x))) =o1(q). Ifδ∗(s, a) =

(x, q), q∈Q2 thenδ∗2(s2,second∗(a)) =qand second∗(o(x, q)) =o2(q).

3. If δ∗

1(s1, a) = q then there is some b such that first∗(b) = a, δ∗(s, b) = (q, x) and o1(q) =

first∗(o((q, x))). Ifδ∗

2(s2, a) =qthen there is someb such that second∗(b) =aandδ∗(s, b) =

(x, q)ando2(q) =second∗(o((q, x))).

On Moore machines defined on the same set of output symbols F, we define an equivalence relationasM1≈M2iffM1(s1) =M2(s2) wheres1, s2are the start states ofM1, M2respectively.

Theorem 2.2 The relationM1≈M2 is decidable.

Proof The idea of the following proof is due to Hopcroft and Karp [53], who defined an algorithm to decide whether two finite state automata accept a same regular set or not.

LetM1 = (Q1, s1,F1, δ1, o1), M2 = (Q2, s2,F2, δ2, o2). We will show that M1 ≈M2 iff for any

reachable stateqinM1×M2,qis of the form (q1, q2) such thatq1∈Q1, q2∈Q2 ando(q) = (f, f)

(30)

o1(δ1(s1, a)) = o2(δ2(s2, a)). Therefore, for any a ∈ L∗, there is some reachable q1 Q1 such

that δ∗

1(s1, a) =q1 and o1(q1) =f for some f F iff there is some reachable q2 ∈Q2 such that

δ∗

2(s2, a) =q2 ando2(q2) =f. Then by lemma 2.5, for any reachable stateq in M1×M2, qis of

the form (q1, q2) ando(q) = (f, f) for somef ∈F.

Conversely, suppose M1×M2 satisfies the condition. Then {pair∗(a, a)|a dom(M1(s1))} =

{pair∗(a, a)|adom(M

2(s2))} and for anya∈dom(M1(s1)),o(δ∗((s1, s2), pair(a, a)) =

(o1(δ1(s1, a)), o2(δ2(s2, a))) = (f, f) for somef ∈F. This impliesM1(s1) =M2(s2).

By the relationship between Moore machines and regular trees (theorem 2.1) the above theorem implies:

Corollary 2.1 Equality on regular trees is decidable.

This property was first shown by Ginali [41]. Alternative proofs can be found in [32].

Another important property on regular trees is the decidability of the unification problem shown by Huet [55]:

Theorem 2.3 (Huet) There is an algorithmU which, given a pair of regular treesα, β∈R(F, V), returns either a substitution or failure such that if it returns a substitution then it is a most general unifier ofαandβ otherwise they have no unifier.

Finally we define term representations of Moore machines (and therefore regular trees). Lete denote terms given by the following syntax:

e::=v |f |f(l=e, . . . , l=e)|(rec v. e)

wherev stands for auxiliary variables,f stands for a given setF of output symbols, andl stands for the setL of input symbols. A variable occurrence v is bound occurrence if it is in (rec v . . .) otherwise it isfree. A term eis proper if it does not contain free variables and if e≡(rec v. e0)

thene0 is a term of the formf(l

1=e1, . . . , ln =en). We denote the set of proper terms generated

byF by the following syntax:

e::=f |f(l=e, . . . , l=e)|(rec v. e(v)).

A proper termedenotes the Moore machineMe= (Q, q0,F, δ, o) defined as follows:

(31)

2. q0=qf wheref is the outmost occurrence of an output symbol ine,

3. o(qf) =f, and

4. δis the following function:

δ(qf, l) = qg iff, gare occurrences in a subterm of the forms f(. . . , l=g, . . .),

f(. . . , l=g(. . .), . . .), orf(. . . , l= (rec v. g . . .), . . .),

δ(qf, l) = qg iff is the occurrence in a subterm of the formf(. . . , l=v, . . .) and

gis the occurrence in its innermost surrounding subterm of the form (rec v. g(. . .)).

Conversely, for any Moore machineM = (Q, s,F, δ, o) there is a termeM that representsM. Define

a partial orderingon reachable states inQas follows: q≤q0 iff the shortest path fromsto qis

a prefix of the shortest path formstoq0. We define two mappingsR

1,R2 respectively onQ×Q andQas follows: R1(p, q) =    q ifq≤p R2(q) otherwise and R2(p) = (rec p. o(p)(l1=R1(p, δ(p, l1)), . . . , ln =R1(p, δ(p, ln)))) where{l1, . . . , ln}={l|δ(p, l) is defined}.

Sinceis a well founded partial ordering, the above definition is well defined. Then the termR2(s)

representsM.

As an example, letM = ({q1, q2, q3}, q1,{f, g, h, . . .}, δ, o) such that

δ(q1, l1) = q2, δ(q1, l2) = q3, δ(q2, l3) = q1, and o(q1) = f, o(q2) = g, o(q3) = h.

ThenR2(M) = (rec q1.f(l1= (rec q2.g(l3=q1)), l2= (rec q3.h))). Note that a term representation

of a Moore machine is not unique. The above machine (modulo renaming of state names) also has the following simpler term representation: (rec q. f(l1=g(l3=q), l2=h)).

(32)

Chapter 3

Analysis of ML : its Syntax and

Semantics

This chapter analyzes the syntactic properties of the polymorphic core of the programming language ML and proposes a framework for denotational semantics for ML polymorphism. These analyses provide bases for the subsequent development of a type system and a language for databases and object-oriented programming. Most of the results of this chapter were presented in [84].

3.1

Introduction

ML is a strongly typed programming language sharing with other typed languages the property that the type correctness of a program is completely checked by static analysis of the program – usually done at compile time. Among other strongly typed languages, one feature that distinguishes ML is itsimplicit type system. Unlike explicitly-typed languages such as Algol [109], Pascal [64] and Ada [60], ML does not require type specifications of bound variables (formal parameters). The type of a program is automaticallyinferred by ML type system. Through this type inference mechanism, ML achieves much of the convenience of dynamically typed languages without sacrificing the desired feature of complete static type-checking. As an example, consider the following definition of the factorial function in ML:

fun fact n = if n = 1 then 1

(33)

Besides the notational differences, the above definition has the identical structure to the following definition in Lisp:

(defun fact (n)

(cond ((equal n 1) 1)

(t (mul n (fact (sub n 1))))))

In particular, both of them have no mention of types. However, in ML, the compiler statically

infers the typeint -> int offact. By this mechanism, the type correctness of ML programs is completely checked at compile time. This contrast with Lisp (and any other dynamically typed languages), where type errors such as the one in(defun foo ... (fact ’("a" "b" "c"))..)

are not caught until something goes wrong at run-time, often with a disastrous consequence. Another important feature of ML is that it supportspolymorphismin a static type system. This is achieved by inferring amost general (orprincipal)type-scheme of any type correct program. A principal type-scheme of a program represents the set of all possible types of the program, capturing the polymorphic nature of the untyped program code. By this mechanism, ML also achieves much of the flexibility of dynamically typed languages in a static type system. For example, from the following definition of identity function:

fun id x = x;

ML type system infers the following type-scheme:

’a -> ’a

where’ais a type variable representing arbitrary types. As a consequence,idcan be used as an identity function of any type of the formτ →τ. The type correctness of each application ofidis statically checked. Moreover, the result type of the application is also statically determined. For example,id("a")yields an expression of typestringandid(3)yields an expression of typeint. There are two major existing approaches to denotational semantics for ML polymorphism; the one by Milner [78] (extended by MacQueen, Plotkin and Sethi [72]) based on an untyped language and the other by Mitchell and Harper [79] based on an explicitly-typed language using Damas and Milner’s type inference system [34]. As I shall suggest in this chapter, however, neither of them properly explains the behavior of ML programs. Because of the implicit type system, ML behaves differently from both untyped languages and explicitly-typed languages. In order to understand ML, we need to develop a framework for denotational semantics and equational theories that give

(34)

precise account for ML’s implicit type system. The goal of this chapter is to propose such a framework, which will provide a basis to extend safely its type system to include various structures and operations for databases and object-oriented programming. In the rest of this section, we review the two existing approaches in subsection 3.1.1, 3.1.2 and outline our approach in subsection 3.1.3.

3.1.1

Milner’s original semantics

In [78], Milner proposed a semantic framework for ML based on a semantics of anuntypedlanguage. He defined the following two classes of types:

τ ::= b|τ →τ, ρ ::= t|b |ρ→ρ

whereb stands for base types and t stands for type variables. Here we call them types and type-schemes respectively. Type-schemes containing type variables represent all their substitution in-stances and correspond to polymorphic types. He defined the preorder of generalness on type-schemes as the preorder on trees induced by substitution (definition 2.9), i.e.ρis more general than ρ0 iff ρ0 is an substitution instance of ρ. He then gave the algorithm W that infers most general

type-schemes for the following raw terms:

e::=x|(λx. e)|(e e)|ifetheneelsee|fixx e|letx=eineend.

He interpreted an ML typinge : ρas the semantion assertion [[e]][[ρ]], i.e. the denotation ofe is an element of the denotation ofρand showed that the type inference algorithmWis sound under this interpretation. The denotation of a raw term is defined as an element of a domain satisfying the following domain equation:

V =B1+· · ·+Bn+ [V →V] +{wrong}

where B1, . . . , Bn are domains corresponding to base types and wrong represents run-time error.

The denotation of a type is defined as a subset of V not containing wrong. The denotation of a type-scheme is defined as the intersection of the denotations of all its instance types. This semantics was extended to recursive types by MacQueen, Plotkin and Sethi [72]. (See also [51, 30] for related studies.)

This semantics explains the polymorphic nature of ML programs and verifies that ML typing discipline prevents all run-time type errors. However, this semantics does not completely fit the operational behavior of ML programs. As an example, consider the following two raw termse1and

References

Related documents

 Emergency core cooling system via the steam generators – cools down the reactor core upon loss of power and loss of coolant in the primary or secondary circuit. In the event

Abiotic (drought, heat, and cold stress) and biotic (pod borers – Helicoverpa armigera and Spodoptera exigua, aphids – Aphis crac- civora, leaf miner – Liriomyza cicerina, and

In this study the focus is on six general groups of services for visually impaired people: orientation and mobility, domestic life, communication, reading, leisure and

When a nonpublic entity chooses to measure a nonemployee share-based payment award by estimating its expected term and applies the intrinsic value practical expedient, it must

The two data sets have been accepted by the Danish Maritime Authority and the Danish Geodata Agency and used for giving Notice to Mariners and to update Navigational Charts for

An introduction to computer programming design and development with a primary focus on data structures and abstraction using the C++ object-oriented programming language..

– CSE 12: Basic Data Structures and Object-Oriented Design using C++ and/or Java... Introduction to

1.2 Scripting languages 1.3 Data-oriented languages 1.4 Object-oriented languages 1.5 Event-driven Programming 1.6 Language Standardisation 2