fg

A Semantics for  : a Calculus with ×ØÖ Overloading and Late-binding

THOMAS STUDER, Institut fur¨ Informatik und angewandte Mathematik, Universitat¨ Bern, Neubruckstrasse¨ 10, CH–3012 Bern, Switzerland. E-mail: [email protected]

Abstract

Up to now there was no interpretation available for -calculi featuring overloading and late-binding, although these

are two of the main principles of any object-oriented programming language. In this paper we provide a new

fg 

semantics for a stratified version of Castagna’s  ,a -calculus combining overloading with late-binding. The

EEÌÂ ·´ÌÓØ µ·´F Á µ

model-construction is carried out in - Æ , a system of explicit mathematics. We will prove the soundness of our model with respect to subtyping, type-checking and reductions. Furthermore, we show that our semantics yields a solution to the problem of loss of information in the context of type-dependent computations.

Keywords: Explicit mathematics, typed -calculus, overloading, late-binding, loss of information.

1 Introduction Polymorphism is one of the concepts to which the object-oriented paradigm owes its power. The distinction is made between parametric (or universal) and ‘ad hoc’ polymorphism. Using parametric polymorphism a function can be defined which takes arguments of a range of types and works uniformly on them. ‘Ad hoc’ polymorphism allows the writing of functions that can take arguments of several different types which may not exhibit a common structure. These functions may execute a different code depending on the types of the arguments. The proof-theory and the semantics of parametric polymorphism have been investigated by many researchers, while ‘ad hoc’ polymorphism has had little theoretical attention. ‘Ad hoc’ polymorphism denotes the possibility that two objects of different classes can respond differently to the same message. Castagna, Ghelli and Longo [6] illustrate this by the following example: the code executed when sending a message inverse to an object of type matrix will be different from the code executed when the same message is sent to an object representing a real number. Nevertheless the same message behaves uniformly on all objects of a certain class. This behaviour is known as overloading, since we overload an operator (here inverse) by different operations. We say the function consists of several branches and the selection of the actual operation depends on the types of the operands. The real gain of power with overloading occurs only in programming languages which compute with types. They must be computed during the execution of the program and this computation must possibly affect the final result of the computation. Selecting the branch to be executed of an overloaded function at compile-time, does not involve any computation on types. Postponing the resolution of an overloaded function to run-time, would not have any effect if types cannot change during the computation. Only if types can change do we obtain the real power of overloading. Hence we need the concept of subtyping in order to have types that are able to evolve during the execution of a program. In such languages an expression of a certain type can be replaced by another one of a smaller type. Thus the type

J. Logic Computat., Vol. 11 No. 4, pp. 527Ð544 2001 ­c Oxford University Press fg

528 A Semantics for  : a Calculus with Overloading and Late-binding ×ØÖ of an expression may decrease during the computation, which can affect the final result of a computation, if we base the selection of the branch of an overloaded function on the types at a given moment of the execution. We talk of early-binding, if the selection of the branch is based on the types at compile-time. If we use the types of the fully evaluated arguments to decide which branch should be executed, then we call this discipline late-binding.The introduction of overloading with early binding does not significantly influence the underlying language. However overloaded functions combined with subtyping and late-binding show the real benefits of object-oriented programming. Usually higher order lambda calculi are used to model parametric polymorphism. These systems allow abstraction with respect to types and applications of terms to types. However computations in these systems do not truly depend on types, i.e. the semantics of an expres- sion does not depend on the types appearing in it. This fact is nicely exposed in a forgetful interpretation of these calculi. Hence parametricity allows us to define functions that work on many different types, but always in the same way. Overloading on the other hand char- acterizes the possibility of executing different codes for different types. Thus we have two different kinds of polymorphism.

The subject of higher order lambda calculus originates from the work of Girard [15] who F introduced his system F for a consistency proof of analysis. For this reason, system is highly impredicative. Independently, Reynolds [22] rediscovered it later and used it for applications

in programming languages. Feferman [11] gives an interpretation of system F in a theory of explicit mathematics and he discusses in detail the advantages of representing programs in theories of explicit mathematics. Until now there are only a few systems available featuring ‘ad hoc’ polymorphism. Ghelli [14] first defined typed calculi with overloading and late-binding to model object-oriented

programming. This approach was further studied in Castagna, Ghelli and Longo [7]. In our fg paper we will use  presented in Castagna [4, 5]. This calculus is designed for the study of the main properties of programming languages with overloading and late-binding. It is a minimal system in which there is a unique operation of abstraction and a unique form of application. Hence we have only overloaded functions and consider ordinary functions as

overloaded with only one branch defined.

² Castagna, Ghelli and Longo [6] present a category-theoretic semantics for  early which is a calculus with overloading and early binding. In this calculus the types of the arguments of an overloaded function are ‘frozen’; the same goes for compile-time and run-time. Fur-

thermore the is stratified in order to avoid certain problems of impredicativity in fg the definition of the semantics. We will present a semantics for a stratified subsystem of 

which can handle not only overloading but also late-binding. Our model-construction will be

EEÌ ·´ÌÓص·´F Á µ carried out in - Æ , a predicative theory of explicit mathematics. Systems of explicit mathematics have been introduced by Feferman [8, 9] as a framework for Bishop style constructive mathematics. More recently, Feferman’s systems were used to develop a unitary axiomatic framework for representing programs, stating properties of programs and proving properties of programs. Important references for the use of explicit mathematics in this context are Feferman [11, 12, 13] and J¬ager [16, 17]. In systems of ex- plicit mathematics types are represented by names, and those are first-order values. Hence they can be used in computations and, as we have seen above, this allows us to model over- loading and late-binding. Furthermore, we will show that our interpretation yields a solution to the problem of loss of information in the context of type dependent computations. This problem introduced in Cardelli [2] can be described as follows: when we apply for fg

A Semantics for  : a Calculus with Overloading and Late-binding 529

×ØÖ

Ì ! Ì a Ë

example the identity function Ü:Ü of type to an argument of type subtype of

´Ü:ܵa Ì a Ë Ì , then we can only derive that has type . The information that was of type , is lost, although this will not be the case in our model. At this point we have to mention that Castagna [4] is developing a second-order calculus with overloading and late-binding in order to deal with the problem of loss of information and parametric polymorphism. Our work is also a first step towards a better understanding of that system and the integration of

parametric and ‘ad hoc’ polymorphism. fg

2The -calculus fg

In this section we introduce Castagna’s  -calculus. This minimal system, implementing overloading and late-binding, has been first presented in [4, 5]. The goal was to use as few operators as possible. Terms are built up from variables by abstraction and application. Types are generated from a set of basic types by a constructor for overloaded types. Ordinary

functions (-abstraction) are considered as overloaded functions with just one branch. Pretypes: First we define the set of pretypes. Later we will select the types from among

the pretypes, meaning, a pretype will be a type, if it satisfies certain conditions on good type A formation. We start with a set of atomic types i . Now the pretypes are inductively defined by:

1. Every atomic type is a pretype.

Ë ;Ì ;:::;Ë ;Ì fË ! Ì ;:::;Ë ! Ì g

½ Ò Ò ½ ½ Ò Ò 2. If ½ are pretypes, then the finite set is a pretype.

Subtyping: we define a subtyping relation  on the pretypes. This relation will be used to

define the types. We start with a predefined partial order  on the atomic (pre)types and

extend it to a preorder on all pretypes by the following subtyping rule:

8i ¾ Á:9j ¾ Â:´Í  Ë Ì  Î µ

j j i

i and

fË ! Ì g fÍ ! Î g

j j j ¾Â i i i¾Á

If the subtyping relation  is decidable on the atomic types, then it is decidable on all pre-

Í  Î Î  Í

types. Note that  is just a preorder and not an order. For instance, and do

¼ ¼

= Î Ë  Ë fË ! Ì gfË ! Ì g

not imply Í . As an example, assume ,thenwehave ,

¼ ¼

Ë ! Ì gfË ! Ì; Ë ! Ì g fË ! Ì; Ë ! Ì gfË ! Ì g and thus both f and hold. Types: although the selection of the branch is based on run-time types, the static typing must

ensure that no type-errors will occur during a computation. We define the set of types as

Í Ë

follows: we call a pretype Ë minimal element of a set of pretypes, if is an element of

fg

Ì 6= Ë Í Ì  Ë 

Í and if there does not exist a pretype in such that .Thesetof types

fg

 fË ! Ì g

i i¾Á contains all atomic types of as well as all pretypes of the form i that satisfy

the following three consistency conditions concerning good type formation:

Ë ;Ì i; j ¾ Á i

1. i are types for all ,

Ë  Ë Ì  Ì i; j ¾ Á

j i j

2. i implies for all and

i ¾ Á Ë Ë  Ë Þ ¾ Á

3. if there exists and a pretype such that i , then there exists a unique

Ë fË j Ë  Ë ^ j ¾ Á g

j j such that Þ is a minimal element of . The first condition simply states that every overloaded type is built up by making use of other types. The second condition is a consistency condition which ensures that a type may only fg

530 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ

f fÍ ! Î ;Í !

½ ¾

decrease during a computation. If we have an overloaded function of type ½

Î g Í  Í Ò Í

½ ¾ ¾

¾ with andweapplyittoanargument with type at compile-time, then the

f ´Òµ Î Ò Í ½

expression will have type ¾ at compile-time; but if the run-time type of is ,then

f ´Òµ Î Î  Î

½ ¾ the run-time type of will be ½ . Therefore must hold. The third condition

concerns the selection of the correct branch. It assures the existence and uniqueness of a

fË ! Ì g

i i¾Á

branch to be executed. If, for example, we apply a function of type i to a term

Í Þ ¾ Á Ë

of type , then condition (3) states that there exists a unique such that Þ is a minimal

Øh

fË j Í  Ë g Ë Í Þ

i Þ element of the set i ,i.e. best approximates and the branch will be chosen. Hence, this condition deals with the problem of multiple inheritance. It assures that there will be no ambiguity in the selection of the branch.

Terms: terms are built up from variables by an operator of abstraction and one of application:

Å ::= Ü j Ü´Å : Ë µ Ì ;:::;Å : Ë µ Ì µ j Å Å ;

½ ½ ½ Ò Ò Ò ½ ¾

Ò  ½ Ë ;Ì ;:::;Ë ;Ì

½ Ò Ò where and ½ are types. Variables are not indexed by types, because

in a term for an overloaded function such as

Ü´Å : Ë µ Ì ;:::;Å : Ë µ Ì µ;

½ ½ ½ Ò Ò Ò

the variable Ü should be indexed by different types. Thus indexing is avoided and in the

typing rules typing contexts are introduced. A context is a finite set of typing assumptions

Ü : Ì ;:::;Ü : Ì

½ Ò Ò ½ with no variable appearing twice.

Type system: the following rules define the typing-relation between terms and types.

;Ü : Ì ` Ü : Ì

;Ü : Ë ` Å : Í ¡¡¡ ;Ü : Ë ` Å : Í

½ ½ ½ Ò Ò Ò

` Ü´Å : Ë µ Ì µ : fË ! Ì g

i i i i i

i¾f½;:::;Òg i¾f½;:::;Òg

Í  Ì i ¾f½;:::;Òg i

where i holds for all ,and

` Å : fË ! Ì g ` Æ : Ë

i i i¾Á

` ÅÆ : Ì

j

Ë =ÑiÒ fË j Ë  Ë g

i¾Á i i where j holds. Reduction: when we apply an overloaded function to an argument, the argument type selects the branch of the overloaded function which will be executed. This has to be formally ex- pressed by the reduction rules of the system. Since the argument of an application may be an

open term, reduction will depend on a typing context . With induction on the term structure we simultaneously define the notion of reduction and the terms in normal form.

1. We have the following notion of reduction:

´ µ ` Ü´Å : Ë µ Ì µ : fË ! Ì g ` Æ : Ë

i i i i i

¾f½;:::;Òg i¾f½;:::;Òg

Let i and ,

Ë =ÑiÒ fË j Ë  Ë g Æ

i¾Á i i

where j .If is in closed normal form with respect to or

fË j i ¾ Á; Ë  Ë g = fË g

i j j

i ,then

Ü´Å : Ë µ Ì µ ƺ Å [Ü := Æ ];

i i i i¾Á j

Å [Ü := Æ ] Ü Å Æ j

where j denotes the substitution of in by . Then there are rules for the

` Å : fË ! Ì g ` Æ : Ë i ¾ Á

i i¾Á

context closure: let i , and if there exists an with

Ë  Ë

i ,then

¼ ¼

ƺ Æ Åº Å

¼ ¼

ÅÆ º Å Æ ÅÆ º ÅÆ

fg

A Semantics for  : a Calculus with Overloading and Late-binding 531

×ØÖ

` Ü´Å : Ë µ Ì µ : fË ! Ì g

i i i¾Á i i i¾Á

Let i ,then

¼

Å º Å

i ;Ü:Ë

i

i

¼

Ü´¡¡¡ Å : Ë µ Ì ¡¡¡µ º Ü´¡¡¡ Å : Ë µ Ì ¡¡¡µ

i i i i i

i

Æ

2. A term Å is in normal form with respect to , if there does not exist a term such that

ź Æ

.

fg

fg 

In the sequel, we will consider only the stratified subsystem  of . This calculus

×ØÖ fg

emerges from  by restricting the subtype relation on the types. First, we introduce the ÖaÒk

function  on the pretypes by:

ÖaÒk ´A µ=¼ i

1.  ,

ÖaÒk ´fË ! Ì g µ = ÑaÜfÖaÒk ´Ë µ·½; ÖaÒk ´Ì µ j i ¾ Á g

i i i¾Á  i  i

2.  .

With this function we define a new subtyping relation  by adding the condition

ÖaÒk ´fË ! Ì g µ  ÖaÒk ´fÍ ! Î g µ Ë

j j j ¾Â  i i i¾Á

 to the subtyping rule. We call a strict

fg

Ë  Ì   

subtype of Ì if holds. Now, is defined by replacing with in the typing and

×ØÖ fg reduction rules of  . Furthermore, in the consistency conditions for good type formation

we have to add

¼

Ë  Ë Ì  Ì i; j ¾ Á

j i j 2 . i implies for all .

3 Semantics fg

According to Castagna [5] the construction of a model for  poses the following problems:

preorder, type dependent computation, late binding and impredicativity.

fg 

¯ Preorder: as we have seen, the subtyping relation of is a preorder but not an order

¼ ¼

 Ë fË ! Ì g  fË ! Ì; Ë ! Ì g

relation. If Ë holds, then we have as well as

¼

Ë ! Ì; Ë ! Ì g fË ! Ì g f . These two types are completely interchangeable from a semantic point of view. Therefore, both types should have the same interpretation, and the subtyping relation has to be modelled by an order relation on the interpretations of the types.

¯ Type-dependent computation: the types of the terms determine the result of a computa- tion. For this reason the interpretation of an overloaded function must not only take the interpretation of the value of its argument as input but also the interpretation of its argu- ment type. Therefore the semantics of an overloaded function type must take into account the interpretations of the argument types of the functions it consists of. Because we work in a calculus with subtyping, equally all the interpretations of subtypes of the argument types have to be regarded.

¯ Late binding: the choice of the branch to be executed of an overloaded function depends on the run-time types of its arguments and not on the types at . Hence the branch to be executed cannot be chosen at compile time, meaning in the translation of the terms. To determine the value of an overloaded application, first the interpretation of its

argument needs to be evaluated in order to know its run-time type.

fg 

¯ Impredicativity: the type system of is not stratified. This can be seen in the following

Ì ! Ì g ! Ì; Ì ! Ì g fÌ ! Ì g example: we know that ff is a subtype of ,but fg

532 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ

Ì ! Ì g ffÌ ! Ì g! Ì; Ì ! Ì g Å

f is a strict occurrence of . Hence a term of the

Ì ! Ì g ! Ì; Ì ! Ì g

type ff may be applied to any term which is of a subtype of

Ì ! Ì g Å Å fÌ ! Ì g f ; and therefore may be applied to itself, as is of a subtype of . The consequence is that it is not possible to give a semantics for the types by induction on the type-structure, as, in order to give the interpretation of an overloaded type, we

need to know the interpretations of the subtypes of its argument types. Therefore the

Ì ! Ì g! Ì; Ì ! Ì g

interpretation of the type ff refers to itself.

² In Castagna [4] and Castagna, Ghelli and Longo [6] the calculus  early is introduced.

It is a -calculus with overloading and subtyping, but without late-binding. For stratified subsystems of this calculus a category-theoretical semantics is presented which focuses on the problems stemming from the preorder on the types and the type-depended computation. The problem of the preorder on the types is solved by a syntactic construction called type completion. Intuitively, the completion of an overloaded type is formed by adding

all subsumed types. Hence two equivalent types will be transformed by completion to es-

Ë ! Ì g

sentially the same type. For example the completion of f will be something like

fË ! Ì; Ë ! Ì; Ë ! Ì;:::g Ë ;Ë ;::: Ë

¾ ½ ¾ ½ ,where are all (infinitely many) subtypes of .

The problem of type depended computation is handled by interpreting overloaded types

A Ü ¾ A B

as product types. If is a type and for every we know that Ü is a type, then the

¥ B f Ü A

¾A Ü

product type Ü consists of all functions which map an element of to an element

f ´Üµ B

of Ü . Now semantic codes for types are introduced to define the interpretation of an

fË ! Ì ;Ë ! Ì g

½ ¾ ¾

overloaded type as indexed product. The interpretation of the type ½ will

f d Í Ë Ë ¾

be the product type consisting of functions mapping a code for a subtype of ½ or

f ´dµ:Ë ! Ì Í Ò fË ! Ì ;Ë ! Ì g

Ò ½ ½ ¾ ¾

to a function Ò ,if selects the -th branch of . Hence

Ü´Å : Ë µ Ì ;Å : Ë µ Ì µ fË ! Ì ;Ë ! Ì g

½ ½ ¾ ¾ ¾ ½ ½ ¾ ¾

an overloaded function ½ of type

d Í

will be interpreted by a function f which is defined for every code for a type subtype of

Ë Ë ¾

½ or in the following way:



Ü:[[Å ]] Í

½ if selects the first branch,

f ´dµ=

Ü:[[Å ]] Í

¾ if selects the second branch.

² In  early an overloaded application demands an explicit coercion of its arguments. Hence the types of the arguments of overloaded functions are ‘frozen’, and the same goes

for compile-time and run-time. Therefore, the problems of late-binding are avoided. Since

² only stratified subsystems of  early are modelled, there is no problem of impredicativity, either. No type occurs strictly in itself, hence the definition of the semantics of a type is not self-referential; and the interpretation of the types can be defined by induction on the type

structure. fg

In the sequel we will present a model construction for  . Our model is not based on ×ØÖ category-theory; but the construction is performed in a theory of explicit mathematics. Such systems were first presented by Feferman in [8] and [9]. Systems of explicit mathematics deal with functions and classes where functions are given by rules for mechanical computing and classes or types are successively defined or generated from previous ones. To handle late- binding, it is essential that there are first-order values acting for types. It is one of the main features of explicit mathematics that types are represented by names. These are first-order values and hence, we can apply functions to them. In this sense, computing with types is pos- sible in such systems and therefore they are an adequate framework to deal with overloading and late-binding. fg

A Semantics for  : a Calculus with Overloading and Late-binding 533

×ØÖ

fg 

In our model we also define semantic codes for types, i.e. every type Ì of is repre-

×ØÖ

£ £ Ì

sented by a natural number Ì in our theory of types and names. is called symbol for ×Ùb the type Ì . In the language of explicit mathematics, we find a term deciding the subtype relation on the type symbols. Using these constructions we can solve the problems mentioned by Castagna in the following way:

¯ Castagna [4] indicates that the key to model late-binding probably consists of interpreting

terms as pairs (interpretation of the computation, symbol for the type). Then the compu-

[ÅÆ]] tational part of the interpretation [ of an application would be something of the

form

´£X:´Ô [[Å ]]µ[[X ]]µ´Ô [[Æ ]]µ´Ô [[Æ ]]µ:

½ ¼ ¼ (3.1) This remark is the starting point for our construction. We show that interpreting terms as pairs really give a semantics for late-binding. When a term, interpreted as such a pair, is used as an argument in an application, its type is explicitly shown and can be used to compute the final result. Hence this representation enables us to manage late-binding. We investigate how something of the form of (3.1) can be expressed in theories for types and names in order to model overloaded functions. As types are represented by names

in explicit mathematics, we do not need a second-order quantifier as in (3.1) and we can

Ô [[Æ ]]

directly employ the symbol ½ for the type of the argument to select the best matching branch.

¯ In our model, types will be interpreted as classes. Using join types (disjoint unions) we can perform a kind of completion process on the types, so that the subtype relation can be interpreted by the standard subclass relation. Since classes in explicit mathematics are extensional in the usual set theoretic sense, this relation is really an order and not just a preorder.

¯ An overloaded function type is interpreted as the class of functions that map an element of the domain of a branch into the range of that branch, for every branch of the overloaded

function type. As the subtype relation is decidable and since an overloaded function ÝÔaÔ

consists only of finitely many branches, there exists a function Ø such that for two

fg

fË ! Ì g Ë  Ë =ÑiÒ fË j Ë  Ë g

i i¾Á j i¾Á i i

types i and of with we have

×ØÖ

£ £ £ £

i: ;Ì ;Ë µ=hË ØÝÔaÔ´fË ! Ì g

i i

j j i¾Á ÝÔaÔ

This means that Ø yields symbols for the domain and the range of the branch to be

fg 

selected. With this term we define the computational part f of the interpretation of a

×ØÖ

Å := Ü´Å : Ë µ Ì ;Å : Ë µ Ì µ

½ ½ ¾ ¾ ¾

function ½ such that:



£ £

[[Å [Ü := Æ ]]] ØÝÔaÔ´Ô [[Å ]]; Ô [[Æ ]]µ = hË ;Ì i;

½ ½

½ if

½ ½

f ´[[Æ ]]µ =

£ £

[[Å [Ü := Æ ]]] ØÝÔaÔ´Ô [[Å ]]; Ô [[Æ ]]µ = hË ;Ì i:

¾ ½

if ½

¾ ¾

Ô [[Å ]] Ô [[Æ ]] ½

By means of the remark about late-binding, we know that ½ and are symbols Æ for the types of Å , respectively. This demonstrates how types will affect the result of computations.

¯ We are not able to deal with the problem of impredicativity. For that reason we consider

fg

fg 

only the stratified version  of . The stratification of the type systems allows us to ×ØÖ define the semantics of the types by induction on the type structure. fg

534 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ

EEÌ ·´ÌÓص·´F Á µ

4 The theory - Æ

EEÌ ·´ÌÓص·´F Á µ

In this section we present the theory - Æ of explicit mathematics with elementary comprehension and join as basic type existence axioms. Furthermore, in this

system, term application is total and we have full induction on the natural numbers.

EEÌ ·´ÌÓص·´F Á µ

We will not employ Feferman’s original formalization of - Æ ;butwe Ä

will treat it as a theory of types and names as developed in J¬ager [16]. The language Ø is two- A; B ; C ; X ;

sorted with individual variables a; b; c; f ; g ; Ñ; Ü; Ý ; Þ ; : : : and type variables

Y;Z;::: Ä k; ×

. Additionally, Ø includes the following individual constants: (combinators),

Ô; Ô ; Ô ¼ × Ô d

½ Æ Æ Æ

¼ (pairing), (zero), (successor), (predecessor), (definition by cases on

e c

natural numbers), for every natural number a constant e (elementary comprehension) and

j Ä ¡ Ä Ø

(join). Ø has the binary function symbol for term application. In we have the unary

=; ¾ <

relation symbol Æ (natural number) as well as binary relation symbols and (naming).

Ö;×;Ø;::: Ä

The individual terms of Ø are inductively defined by closing individual variables

´×ص ×Ø ´× ¡ ص and constants against application. We will drop ¡ and only write or instead of

and we usually omit parenthesis with the implicit assumption that ¡ associates to the left,

Ö×Ø ´´Ö ¡ ×µ ¡ ص ´Ø ;Ø µ ÔØ Ø Ä

½ ¼ ½ Ø

i.e. stands for .Weuse ¼ for . The atomic formulas of are

´×µ ´× = ص ´× ¾ Aµ ´A = B µ <´×; Aµ Æ ´×µ ×

the formulas Æ , , , and ,where says that is a

´×; Aµ ×

natural number and the formula < is used to express that the individual represents

A A F;G;À;::: Ä the type or is a name of .Theformulas of Ø are generated from the atomic

formulas by closing under negations, disjunctions, conjunctions and quantification in both

F Ä < F sorts. A formula of Ø is called elementary, if the relation symbol does not occur in

and F does not contain bound type variables. The following table contains a useful list of

abbreviations:

Ø ¾ Æ : Æ ´Øµ;

9Ü ¾ A:F : 9Ü:´Ü ¾ A ^ F µ;

8Ü ¾ A:F : 8Ü:´Ü ¾ A ! F µ;

´f : A ! B µ : 8Ü ¾ A:f Ü ¾ B;

A  B : 8Ü ¾ A:Ü ¾ B;

_

× ¾ Ø : 9Y:´<´Ø; Y µ ^ × ¾ Y µ;

_

×  Ø : 9X; Y :´<´×; X µ ^<´Ø; Y µ ^ X  Y µ;

<´×µ : 9X:<´×; X µ:

EEÌ ·´ÌÓص·´F Á µ

Now we are ready to state the axioms of the theory - Æ . The underlying logic is classical first-order logic with equality. Hence, the remaining logical connectives are

defined as usual. The non-logical axioms can be divided into the following four groups.

AÔÔÐicaØiÚe aÜiÓÑ׺ Áº These axioms formalize that the individuals form a combinatory al- gebra, that we have pairing and projection and the usual closure conditions on the natural numbers as well as definition by numerical cases. This first-order part corresponds to the

theory ÌÇÆ of J¬ager and Strahm [19].

Combinatory algebra

ÜÝ = Ü

(1) k .

ÜÝ Þ = ÜÞ ´ÝÞ µ (2) × . fg

A Semantics for  : a Calculus with Overloading and Late-binding 535 ×ØÖ

Pairing and projection

Ô ´Ü; Ý µ=Ü ^ Ô ´Ü; Ý µ=Ý ½ (3) ¼ .

Natural numbers

¼ ¾ Æ ^8Ü ¾ Æ:× Ü ¾ Æ

(4) Æ .

8Ü ¾ Æ:´× Ü 6=¼^ Ô ´× ܵ=ܵ

Æ Æ

(5) Æ .

8Ü ¾ Æ:´Ü 6=¼! Ô ´× ܵ ¾ Æ ^ × ´Ô ܵ=ܵ

Æ Æ Æ (6) Æ .

Definition by cases on natural numbers

Ù ¾ Æ ^ Ú ¾ Æ ^ Ù = Ú ! d ÜÝ ÙÚ = Ü

(7) Æ .

Ù ¾ Æ ^ Ú ¾ Æ ^ Ù 6= Ú ! d ÜÝ ÙÚ = Ý

(8) Æ .

½µ ´¾µ It is standard work in combinatory logic that with the axioms ´ and lambda abstraction can be defined and a recursion theorem can be proven (cf. [1, 8, 18]).

DEFINITION 4.1

We define  abstraction by:

Ü:Ü : ×kk;

: kØ; Ü 6¾ FÎ ´Øµ

Ü:Ø if ,

´Ö×µ : ×´Ü:Ö µ´Ü:×µ; Ü: otherwise.

This definition of -abstraction is compatible with substitution, but the totality of the applica-

tion is needed to make it work. In a partial setting a more complex definition of  abstraction

would be required and it would behave very badly as far as substitution in  expressions is concerned (cf. [23]).

THEOREM 4.2

Öec Ä

(Recursion theorem) There is a closed term of Ø such that:

8f:Öecf = f ´Öecf µ:

EÜÔÐiciØ ÖeÔÖe×eÒØaØiÓÒ aÒd eÜØeÒ×iÓÒaÐiØݺ <

ÁÁº The relation acts as a naming relation be-

´×; Aµ × A tween objects and types, i.e. < says that is a name of the type . While the represen- tation of types by their names is intensional, the types themselves are extensional in the usual set-theoretical sense.

Extensionality

EX̵ 8Ü:´Ü ¾ A ° Ü ¾ B µ ! A = B ´ . The axioms about explicit representation state that every type has a name (E.1) and that there are no homonyms (E.2).

Explicit representation

E:½µ 9Ü:<´Ü; Aµ

´ .

E:¾µ <´a; B µ ^<´a; C µ ! B = C ´ . fg

536 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ ÝÔ e eÜi×ØeÒce aÜiÓÑ׺

Á Á Áº Ba×ic Ø In order to build types, there exists the principle of elemen-

~

F [Ü; ~Ý; Z ] Ä e tary comprehension. Let be an elementary formula of Ø with G¬odelnumber for any fixed G¬odelnumbering, then we have the following axioms:

Elementary comprehension

~

ECA:½µ 9X:8Ü:´Ü ¾ X ° F [Ü; ~a; B ]µ

´ .

~ ~

~ ~

´ECA:¾µ <´b; B µ ^8Ü:´Ü ¾ A ° F [Ü; ~a; B ]µ !<´c ´~a; bµ;Aµ

e .

Besides elementary comprehension, we can also make use of the type building axiom for

=¦´B; f µ

join. Let us write A for the statement

8Ü:´Ü ¾ A ° Ü =´Ô Ü; Ô Üµ ^ Ô Ü ¾ B ^ 9X:´<´f ´Ô ܵ;Xµ ^ Ô Ü ¾ X µµ;

¼ ½ ¼ ¼ ½

Ü ¾ B fÜ i.e. A is the disjoint sum over all of the types named by . Now the (uniform) axiom of join has the form

Join

µ <´a; Aµ ^8Ü ¾ A:9Y:<´fÜ;Y µ ! 9Z:´<´j´a; f µ;Zµ ^ Z =¦´A; f µµ

´ .

FÓÖÑÙÐa iÒdÙcØiÓÒ ÓÒ Øhe ÒaØÙÖaÐ ÒÙÑb eÖ׺ Áκ Our theory enjoys full induction on the natural numbers:

Formula induction on N

´F Á µ F ´¼µ ^8Ü ¾ Æ:´F ´Üµ ! F ´× ܵµ !8Ü ¾ Æ:F ´Üµ Æ

- Æ ,

F Ä

where is an arbitrary formula of Ø .

This induction principle allows us to represent every primitive recursive function and re-

Ä ½ × ¼ < Æ lation as a closed term of Ø . stands for the term and we let denote the usual

‘less than’ relation on the natural numbers. We will need to code finite sequences of natu-

hÜ ;:::;Ü i Ü ;:::;Ü

Ò ½ Ò

ral numbers. Let ½ be the natural number which codes the sequence 

in any fixed coding. hi is the empty sequence. There exists a projection function so

 ihÜ ;:::;Ü ;:::;Ü i = Ü Ü ;:::;Ü ;:::;Ü

i Ò i ½ i Ò

that ½ for all natural numbers . We suppose

¼ ¼

< a ;:::;a i < a ha ;:::;a

i Ò

that our coding satisfies the following property: if ,then ½

i i

ha ;:::;a ;:::;a i

i Ò ½ holds.

Since the totality of the application adds nothing to the proof-theoretic strength of our sys-

EEÌ ·´ÌÓص·´F Á µ

tem, - Æ is proof-theoretically equivalent to Martin-L¬of’s type theory

c

ÅÄ ÁD ½ with one universe and ½ , the theory of non-iterated positive arithmetical inductive

definitions where only the fixed-point property is asserted [10]. There are simple models of

EEÌ ·´ÌÓص·´F Á µ

- Æ . The applicative part can be interpreted in a standard way by a for-

malized total term model of ÌÇÆ [19]. For the basic type existence axioms, codes for classes can be inductively generated and simultaneously a membership relation can be generated to satisfy elementary comprehension and join (actually one generates the element relation and its complement simultaneously). Only the fixed point property of this inductive definition

is needed to establish a model, whereas minimality is not necessary [21]. Formalizing this

c

ÁD EEÌ ·´ÌÓص·´F Á µ Æ procedure in ½ yields a model for - . More on inductive model con- structions for systems of explicit mathematics can be found in [20]. fg

A Semantics for  : a Calculus with Overloading and Late-binding 537

×ØÖ fg

5 Embedding  into explicit mathematics

×ØÖ

fg

 EEÌ ·´ÌÓص·´F Á µ

In this section we carry out the embedding of into the theory - Æ

×ØÖ

fg

£

 Ì

of explicit mathematics. First, we represent each pretype Ì of by a natural number ,

×ØÖ a×Ùb which will be called symbol for the pretype Ì . We presume that there exists a term

deciding the subtype relation on the symbols for atomic pretypes. Using this term we can

ÝÔ ×Ùb ×Ùb ÔØÝÔ

define terms ÔØ , and as such that decides whether a natural number is a

×Ùb   symbol for a pretype and ×Ùb, model the subtype relations and , respectively, on

the pretype symbols.

fg

 

We define the class ÇÌË of all symbols for types of with a well-ordering on it. ×ØÖ

Since we consider only a stratified type system, this can be done to such an extent that if a

fË ! Ì g b Ë Ì

i i¾Á i i

represents the type i and is a symbol for a strict subtype of any or ,then

 a ØÝÔ e

b holds. Therefore, it is possible to define by recursion a term in such a way that

fg Ì

applying this term to the symbol of any  type yields a name for its corresponding type ×ØÖ

in the system of explicit mathematics. This type will contain all the computational aspects of

fg

Ì Ì

the interpretations of  terms with type . Then we can define the semantics for a type

×ØÖ

fg

£

ØÝÔ e´Ë µ Ë Ì

of  as the disjoint union of all classes for strict subtypes of .

×ØÖ

fg

 Å Ä

The interpretation of a term is a pair in Ø consisting of the interpretation of ×ØÖ

the computational aspect of Å and the symbol for its type. Hence, the type information is

explicitly shown and can be used to model overloading and late-binding. To do so, we define

ØÝÔaÔ a; b fË ! Ì g Ë

i i¾Á

aterm which computes out of the symbols for types i and the term

£ £

hË Ë = ÑiÒ fË j Ë  Ë g ØÝÔaÔ ;Ì i

i¾Á i i

such that j holds. In other words can be employed

j j

to select the best matching branch of an overloaded function. Hence, it allows us to give the fg

semantics for overloaded function terms of  using definition by cases on natural numbers. ×ØÖ We prove the soundness of our interpretation with respect to subtyping, type-checking and

reductions.

fg

£

 Ä Ì

First, we introduce a translation from pretypes of to Ø terms. If is a pretype,

×ØÖ

£

Ì A ;A ;::: ¾

then its type symbol is defined as follows: let ½ be any fixed enumeration of all

fg

£

A := h¼;ii

atomic types of  ,thenweset and

×ØÖ

i

£ £ £ £ £

fË ! Ì ;:::;Ë ! Ì g := h½; hË ;Ì i;:::;hË ;Ì ii:

½ ½ Ò Ò

½ ½ Ò Ò

fg

ÖaÒk Ä Ì 

There exists a closed individual term of Ø ,sothatif is a pretype of ,then

×ØÖ

£

EEÌ ·´ÌÓص·´F Á µ ` ÖaÒk´Ì µ=Ò ´µ ÖaÒk ´Ì µ=Ò:  - Æ

We assume that there is a closed individual term a×Ùb available, which adequately represents

the subtype relation on the atomic type symbols, i.e.

EEÌ ·´ÌÓص·´F Á µ `8Ü; Ý ¾ Æ:´a×Ùb´Ü; Ý µ=¼_ a×Ùb´Ü; Ý µ=½µ

1. - Æ .

fg

£ £

Ë; Ì  EEÌ ·´ÌÓص·´F Á µ ` a×Ùb´Ë ;Ì µ=½

2. If are pretypes of ,then - Æ if and only

×ØÖ

 Ì Ë; Ì

if Ë and are atomic.

EEÌ ·´ÌÓص·´F Á µ `8Ü; Ý ; Þ ¾ Æ:´a×Ùb´Ü; Ý µ=½^ a×Ùb´Ý; Þ µ= ½ !

3. - Æ

´Ü; Þ µ=½µ

a×Ùb .

ÝÔ Ò

We find a closed individual term ÔØ which decides, whether a natural number is a symbol

h¼;ii ÔØÝÔ´Òµ a×Ùb´Ò; Òµ

for a pretype. If Ò is of the form ,then is simply .Otherwise

£

ÝÔ´Òµ ÔØ is evaluated using primitive recursion according to the definition of the translation. fg

538 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ

; ÔØÝÔ ÖaÒk

Using the terms a×Ùb and we define by primitive recursion two closed individ-

×Ùb 

ual terms ×Ùb and , so that these terms properly represent the subtype relations and

 , respectively, on the symbols for pretypes.

LEMMA 5.1

fg 

Let Ë; Ì be pretypes of .

×ØÖ

Ü; Ý ¾ Æ:´×Ùb´Ü; Ý µ=¼_ ×Ùb´Ü; Ý µ=½µ

1. 8 ,

Ü; Ý ¾ Æ:´×Ùb ´Ü; Ý µ=¼_ ×Ùb ´Ü; Ý µ=½µ

2. 8 ,

fg

£ £

EEÌ ·´ÌÓص·´F Á µ ` ×Ùb´Ë ;Ì µ=½ Ë  Ì 

3. - Æ if and only if is derivable in ,

×ØÖ

fg

£ £

EEÌ ·´ÌÓص·´F Á µ ` ×Ùb ´Ë ;Ì µ=½ Ë  Ì 

4. - Æ if and only if is derivable in .

×ØÖ

Ä F ´aµ

There is an elementary Ø formula expressing the fact that the type represented by the

symbol a satisfies the consistency conditions on good type formation. Hence, we can define

fg 

aclass ÇÌË consisting of all symbols for types. Since elementary comprehension is

×ØÖ

Ä dÓÑaiÒ ÖaÒge

uniform, there are closed individual Ø terms and satisfying the following

fg

fË ! Ì g  a

i i¾Á

property: assume i is an overloaded function type of and is its symbol.

×ØÖ

´aµ Ü ¾ ÇÌË

Then dÓÑaiÒ is a name for the class containing all symbols for which there

£

¾ Á ×Ùb ´Ü; Ë µ = ½ ÖaÒge´aµ

is an i such that holds. The term represents the class

i

Ü ¾ ÇÌË Ü Î  Ì i ¾ Á

consisting of the symbols where is a symbol of a type i for an .

_ _

Ü ¾ dÓÑaiÒ´aµ Ë Ü ¾ ÖaÒge´aµ

That is denotes a strict subtype of an i and is a symbol for

Ì i ¾ Á ÖaÒk a strict subtype of a i for .Usingthe function, we find a primitive recursive

well-ordering  on the type symbols so that the next lemma holds.

LEMMA 5.2

EEÌ ·´ÌÓص·´F Á µ

- Æ proves:

_

¾ ÇÌË ^ b ¾ dÓÑaiÒ´aµ ! b  a

1. a ,

_

¾ ÇÌË ^ b ¾ ÖaÒge´aµ ! b  a

2. a ,

Ü ¾ ÇÌË:´8Ý ¾ ÇÌË:´Ý  Ü ! F ´Ý µµ ! F ´Üµµ !8Ü ¾ ÇÌË:F ´Üµ;

3. 8 for arbitrary

F Ä

formulas of Ø .

Ä ×Ùb

Since the subtype relation on the type symbols is decidable, i.e. we have the Ø term at

ØÝÔaÔ Ä

our disposal, we find a closed individual term of Ø selecting the best matching branch

fg

fË ! Ì g Ë 

i i¾Á

in an application. Assume i and are types of .Thenwehave

×ØÖ

£ £ £ £

i; ;Ì ;Ë µ=hË ØÝÔaÔ´fË ! Ì g

i i

j j i¾Á

£ £

;Ë µ=¼ Ë = ÑiÒ fË j Ë  Ë g ØÝÔaÔ´fË ! Ì g Ë

i¾Á i i i i j

if j .Weset ,ifsuchan does

i¾Á

fg

£ £ £ £

i ;Ì ØÝÔaÔ´fË ! Ì g ;Ë µ=hË  Å i

not exist. Hence, i means, that if a term of type

×ØÖ

j j

i¾Á

fg

Øh

fË ! Ì g  Æ Ë j Å

i i¾Á

i is applied to a term of type , then the branch of will be applied

×ØÖ

Æ Ë Ë

to . As a result of this is best approximated by j .

fg

Ø 

We assume that there is term G which assigns to each symbol for an atomic type of ×ØÖ

the name of its corresponding type in explicit mathematics. If, for example, we have just one

fg

h¼; ½i

atomic type in  consisting exactly of the natural numbers, then its symbol is and its

×ØÖ

fa j Æ ´aµg Ø Ø := Ü:Ø

assigned type is .If is a name for this type, then we can choose G .If

 ½a =¼  ½b =¼ ×Ùb´a; bµ=½ there are two symbols a; b with and and (e.g. symbols for fg

A Semantics for  : a Calculus with Overloading and Late-binding 539

×ØÖ

fg

_

Ë; Ì  Ë  Ì Ø Ø ´aµ  Ø ´bµ

G G

atomic types of with ), then G has to satisfy .Thismeansthat

×ØÖ

Ø ×Ùb

G has to respect the subtype hierarchy on the type symbols given by . With reference to

ØÝÔ e Ä

the recursion theorem we define a closed individual term of Ø satisfying



Ø Ñ;  ½Ñ =¼;

G if

ØÝÔ e Ñ =

Ø ØÝÔ e Ñ;  ½Ñ =½;

Ç if

Ø ØÝÔ e Ñ

where Ç is a name for

_ _

ff j8a ¾ dÓÑaiҴѵ:8Ü ¾ ØÝÔ e´aµ:

_ _

´Ô ´f ´Ü; aµµ ¾ ÖaÒge´Ñµ ^ Ô ´f ´Ü; aµµ ¾ Ô ´f ´Ü; aµµ ^

½ ¼ ½

×Ùb ´Ô ´f ´Ü; aµµ;  ¾´ØÝÔaÔ´Ñ; aµµµ = ½µg:

½

ÝÔ e Ñ

This type depends on the terms Ø and . Since in explicit mathematics the representation Ø

of types by names is uniform in the parameters of the types, there exists a term Ç such that

Ø ØÝÔ e Ñ

Ç isanamefortheabovetype.

£

ØÝÔ e´A µ

Hence, if A is an atomic type, then is a name for its corresponding type defined

£

Ø fË ! Ì g ØÝÔ e´fË ! Ì g µ

i i i¾Á i i

by G . If we are given an overloaded type ,then contains

i¾Á

fg

Ë  i ¾ Á

all functions f to such an extent that for every type of ,every and every term

×ØÖ

£ £ £

_ _

Ü Ä Ë  Ë Ü ¾ ØÝÔ e´Ë µ Ô ´f ´Ü; Ë µµ ¾ Ô ´f ´Ü; Ë µµ

i ¼ ½

of Ø with and we obtain and

£ £ £ £

Ô ´f ´Ü; Ë µµ Ì Ì =  ¾´ØÝÔaÔ´fË ! Ì g ;Ë µµ

j i i

½ denotes a strict subtype of ,where ,

j

i¾Á

Ë =ÑiÒ fË j Ë  Ë g

i¾Á i i

i.e. j .

ÝÔ e Ñ

In this definition of Ø , there is a kind of type completion built in. Assume is a

_

Ë ! Ì g f ¾ ØÝÔ e´Ñµ f ´Ü; aµ

symbol for an overloaded function type f and .Then is

_ _

¾ dÓÑaiҴѵ Ü ¾ ØÝÔ e´aµ dÓÑaiҴѵ

defined for all a and for all .Since contains all

£ £

;Ë ;::: Ë ØÝÔ e´Ñµ

symbols Ë for strict subtypes of ,theterm represents in a sense the type

½ ¾

fË ! Ì; Ë ! Ì; Ë ! Ì;:::g ¾ ½ . In this way, we make use of a form of type completion to

handle the problem of the preorder on the types.

¾ ÇÌË ØÝÔ e´Ñµ

Using Lemma 5.2 we can prove that for every type symbol Ñ the term

Ñ ¾ ÇÌË:<´ØÝÔ e´Ñµµ represents a type in explicit mathematics, meaning 8 . These types satisfy the following subtype property.

LEMMA 5.3

EEÌ ·´ÌÓص·´F Á µ

- Æ proves:

_

a; b ¾ ÇÌË ^ ×Ùb ´a; bµ=½! ØÝÔ e´aµ  ØÝÔ e´bµ:

ØÝÔ e Ä a ¾ ÇÌË Ø

Now, we define a closed individual term ¾ of , so that for all we have

<´ØÝÔ e ´aµµ

¾ and

_ _

´f; ѵ ¾ ØÝÔ e ´aµ ° Ñ ¾ ÇÌË ^ ×Ùb ´Ñ; aµ=½^ f ¾ ØÝÔ e´Ñµ:

¾ Ä

The following lemma about subtyping is just a corollary of the definition of the Ø term

ØÝÔ e

¾ .

LEMMA 5.4

EEÌ ·´ÌÓص·´F Á µ

- Æ proves:

_

a; b ¾ ÇÌË ^ ×Ùb ´a; bµ=½! ØÝÔ e ´aµ  ØÝÔ e ´bµ:

¾ ¾

fg

Ä ØÝÔ e  ¾

With the Ø term we define the interpretation of types as follows. ×ØÖ fg

540 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ

fg

[Ì ]]  Ì

DEFINITION 5.5 (Interpretation [ of a type )

×ØÖ

fg

£

Ì  [[Ì ]] ØÝÔ e ´Ì µ

If is a type of ,then is the type represented by ¾ . ×ØÖ As an immediate consequence of this definition and the previous lemmas about subtyping, we obtain the soundness of our interpretation with respect to subtyping.

THEOREM 5.6

fg

 Ë  Ì

Let Ë; Ì be types of with ,then

×ØÖ

EEÌ ·´ÌÓص·´F Á µ ` [[Ë ]]  [[Ì ]]:

- Æ fg

Terms of  will be interpreted as ordered pairs, where the first component models the ×ØÖ

computational aspect of the term and the second component is a symbol for the type of

fg ^

the term. To define the semantics for  terms we need an injective translation from the

×ØÖ

fg fg

 Ä 

variables of to the individual variables of Ø . Then the computational part of a

×ØÖ ×ØÖ

Ü:´Å : Ë µ Ì µ f ØÝÔaÔ´fË !

i i i¾Á i

term i can be interpreted by a function as such that if

£ £ £

Ì g ; Ô Ý µ=hË ;Ì i f ½

i holds, then satisfies

j j

i¾Á

f ´Ý µ= ´Ü:^ [[Å ]]µÝ: j

Such a function exists, because an overloaded function is composed only of finitely many

Ä ØÝÔaÔ

branches and we have the Ø term available, which selects the best matching branch.

Ä ÅÆ Ô [[Å ]] ¼

An application of two Ø terms is simply modelled by applying the function to

[Æ ]]

[ .

fg

[Å ]]  Å

DEFINITION 5.7 (Interpretation [ of a term )

×ØÖ

[Å ]]

We define [ by induction on the term structure:

 Ü [[Å ]]:=^Ü

1. Å : .

£

µ Å  Ü:´Å : Ë µ Ì µ [[Å ]] := ´f; fË ! Ì g f

i i i i i

¾f½;:::;Òg

2. i : ,where is

i¾f½;:::;Òg

defined as follows:

8

£ £ £

; Ô Ý µ=hË ´Ü:^ [[Å ]]µÝ; ØÝÔaÔ´fË ! Ì g ;Ì i;

½ ½ i i

>

½ ½

i¾f½;:::;Òg

< .

´Ý µ:=

f .

>

:

£ £ £

i: ;Ì ; Ô Ý µ=hË ´Ü:^ [[Å ]]µÝ; ØÝÔaÔ´fË ! Ì g

½ Ò i i

Ò Ò

i¾f½;:::;Òg

Å  Å Å [[Å ]] Ô [[Å ]][[Å ]]

¾ ¼ ½ ¾ 3. ½ : is defined as .

Employing definition by cases on natural numbers we can combine the interpretations of the

fg

Å 

branches of a  term defined by abstraction to one overloaded function which serves

×ØÖ

ØÝÔaÔ as the interpretation of Å . This definition by cases is evaluated using the function and

the type information which is shown in Å for each branch. Before we can prove two of our main results, soundness of our interpretation with respect to type-checking and with respect to reductions, we have to mention the following preparatory lemma.

LEMMA 5.8

fg

 È [Ü := É] Ü È É

If Å; Æ are terms of and denotes the substitution of in by for both

×ØÖ

fg

 Ä EEÌ ·´ÌÓص·´F Á µ Æ

terms and Ø terms, then - proves:

×ØÖ

[[Å ]][Ü ^ := [[Æ ]]] = [[Å [Ü := Æ ]]]: fg

A Semantics for  : a Calculus with Overloading and Late-binding 541 ×ØÖ

PROOF. The proof proceeds by induction on the term structure of Å . The only critical case  is when Å is defined by abstraction. There, totality in our system of explicit mathematics

is essential since it guarantees that substitution is compatible with  abstraction.

[[ ]] Ü : Ì ;:::;Ü : Ì

½ Ò Ò

We define the interpretation of a context ½ as

[[Ü ]] ¾ [[Ì ]] ^ ::: ^ [[Ü ]] ¾ [[Ì ]]:

½ ½ Ò Ò

Our interpretation is sound with respect to type checking.

THEOREM 5.9

fg

` Å : Ì  EEÌ ·´ÌÓص·´F Á µ

If holds in ,thenin - Æ one can prove:

×ØÖ

[[ ]] ! [[Å ]] ¾ [[Ì ]]:

` Å : Ì

PROOF. The proof proceeds by induction on .

 Ü

1. Å :trivial.

Å  Ü:´Å : Ë µ Ì µ f := Ô [[Å ]] Ì fË ! Ì g

i i i¾Á ¼ i i i¾Á

2. i :let . is of the form .

£ £ £

_ _

´f; Ì µ ¾ ØÝÔ e ´Ì µ f ¾ ØÝÔ e´Ì µ

Therefore, we have to show ¾ .Thatis ,i.e.

£

_ _

8a ¾ dÓÑaiÒ´Ì µ:8Ý ¾ ØÝÔ e´aµ:

£

_ _

´Ô ´f ´Ý; aµµ ¾ ÖaÒge´Ì µ ^ Ô ´f ´Ý; aµµ ¾ Ô ´f ´Ý; aµµ ^

¼ ½

½ (5.1)

£

×Ùb ´Ô ´f ´Ý; aµµ;  ¾´ØÝÔaÔ´Ì ;aµµµ = ½µ:

½

£

_ _

¾ dÓÑaiÒ´Ì µ Ý ¾ ØÝÔ e´aµ j

Choose a , and let the natural number be such that

£ £ £

ØÝÔaÔ´Ì ;aµ = hË f ´Ý; aµ =´Ü:^ [[Å ]]µ´Ý; aµ ;Ì i

, then we obtain j by the definition

j j

of f . With the induction hypothesis we get

[[ ]] ^ [[Ü]] ¾ [[Ë ]] ! [[Å ]] ¾ [[Î ]];

j j j

£ £

_

Î  Ì ØÝÔ e ´Î µ  ØÝÔ e ´Ì µ

j ¾ ¾

for a type j . From Lemma 5.4 we infer .Our

j j

£

_

a Ý j ´Ý; aµ ¾ [[Ë ]] f ´Ý; aµ ¾ ØÝÔ e ´Ì µ ¾

choice of , and yields j and we conclude .

j

_

Ô ´f ´Ý; aµµ ¾ ØÝÔ e´Ô ´f ´Ý; aµµµ Ô ´f ´Ý; aµµ ¾ ÇÌË

½ ½

That is ¼ and as well as

£

×Ùb ´Ô ´f ´Ý; aµµ;Ì µ=½

½ . Therefore, we conclude that (5.1) holds.

j

fg

Å  Å Å fË ! Ì g Ë  j ¾ Á

¾ i i i¾Á

3. ½ : in this case there are types and of and ,

×ØÖ

fg

 ` Å : fË ! Ì g ` Å : Ë

i i i¾Á ¾

such that in one can derive ½ and ,where

×ØÖ

Ë =ÑiÒ fË j Ë  Ë g Ì = Ì

i¾Á i i j

j and . By the induction hypothesis we know

[[ ]] ! [[Å ]] ¾ [[fË ! Ì g ]];

i i i¾Á

½ (5.2)

[[ ]] ! [[Å ]] ¾ [[Ë ]]:

¾ (5.3)

_

Ô [[Å ]] ¾ ØÝÔ e´Ô [[Å ]]µ

¾ ½ ¾

From (5.3) we infer ¼ as well as

£

×Ùb ´Ô [[Å ]];Ë µ=½: ¾

½ (5.4)

£ £ £

k ØÝÔaÔ´fË ! Ì g ; Ô [[Å ]]µ = hË ;Ì i

i ½ ¾

Let be such that i .Using(5.2)weget

i¾Á k k

_

Ô ´Ô [[Å ]][[Å ]]µ ¾ Ô ´Ô [[Å ]][[Å ]]µ

¼ ¼ ½ ¾ ½ ¼ ½ ¾

£

µ = ½ ×Ùb ´Ô ´Ô [[Å ]][[Å ]]µ;Ì

¼ ½ ¾

and ½ . From (5.4) and the consistency conditions on k fg

542 A Semantics for  : a Calculus with Overloading and Late-binding

×ØÖ

£ £

´Ì ;Ì µ

good type formation we obtain ×Ùb . Therefore, we conclude by Lemma 5.4

j

k

Ô [[Å ]][[Å ]] ¾ [[Ì ]]

½ ¾ j that ¼ holds.

In the sequel we will prove the soundness of our model construction with respect to reduc-

² tions. In contrast to the semantics for  early presented in Castagna, Ghelli and Longo

[6], our interpretation of a term does not change, when the term is reduced. We can show that

fg

 Æ Å Æ

if a term Å reduces in to a term , then the interpretations of and are equal. ×ØÖ

THEOREM 5.10

fg

È; É  È º É EEÌÂ ·

If are well-typed terms and , then the following is provable in

×ØÖ

´ÌÓص·´F Á µ

- Æ :

[[ ]] ! [[È ]]=[[É]]: º

PROOF. By induction on . The critical case is:

È : Å ¡ Æ Å  Ü´Å : Ë µ Ì µ ` Æ : Ë

i i i¾Á

, where we have i , as well as

Ë = ÑiÒ fË j Ë  Ë g fË j i ¾ Á; Ë  Ë g = fË g Æ

i¾Á i i i i j j

j .Furthermore, or is in closed

[ ]]

normal form. Assuming that [ holds, then we obtain in both cases

£ £ £

ØÝÔaÔ´fË ! Ì g ; Ô [[Æ ]]µ = hË ;Ì i:

i i ½

i¾Á j j

Therefore, we conclude

[[È ]]=Ô [[Å ]][[Æ ]]=´Ü:^ [[Å ]]µ[[Æ ]]=[[Å ]][Ü ^ := [[Æ ]]]=[[Å [Ü := Æ ]]]=[[É]]:

¼ j j j

6 Loss of information Castagna [4] indicated a relationship between modelling late-binding and the problem of loss

of information. This is a problem in type-theoretic research on object-oriented programming fg

introduced in [2]. It can be described in the following way. We assume we are given a 

×ØÖ

´Ü : Ì µ Ì µ fÌ ! Ì g Ì

function Ü of type , i.e. the identity function on the type .Ifwe

Ë Ë Ì

apply this function to a term Æ of type ,where is a strict subtype of , then we can only

´Ü : Ì µ Ì µÆ Ì Ë

infer that Ü has type (rather than ). Thus, in the application we have Ë lost some information. For this reason, we no longer know that Æ is of type , after having applied the identity function to it. Usually, the solution to this problem is to use a second-order calculus which was originally

proposed in Cardelli and Wegner [3]. The identity function is no longer considered to take an Ì argument of type smaller than or equal to Ì and to return a result of type . Instead, it is a

function which takes any argument smaller than or equal to Ì and returns a result of the same

 Ì type as that of the argument, i.e. it takes an argument of type X and returns a result of

type X . In a second-order calculus we can write the type of this function as

8X  Ì:´X ! X µ:

Recalling Castagna’s proposal how a semantics for late-binding might work, we note the second order quantifier in the expression (3.1). This shows the connection between late- binding and the problem of loss of information. In semantics for late-binding we have to deal with functions which take types as arguments. The same is the case in order to solve the problem of loss of information. fg

A Semantics for  : a Calculus with Overloading and Late-binding 543 ×ØÖ

This interplay of late-binding and the loss of information also appears in our semantics.

fg

 Ü´Ü : Ì µ Ì µ fÌ ! Ì g Æ Ë  Ì

Let Å be the term of type and be a term of type .

×ØÖ

EEÌ ·´ÌÓص·´F Á µ [[ÅÆ]] ¾ [[Ë ]]

Then we still can prove in - Æ that . Thus, there is no loss

fg Å

of information in our interpretation of  . After having applied the identity function to ×ØÖ

Æ , we still can prove that the interpretation of the result is an element of the interpretation of

the type Ë . We have no loss of information in our semantics because the types of the terms are explic- itly shown. Hence, in an application the types of the arguments can be employed to derive

the type of the result. First, the type information of the argument types is used to select the fg

best matching branch. Then, in  , the type of the result is fixed by the type of this branch. ×ØÖ The information of the argument types is lost. Whereas in our model, once the branch to be executed is chosen, the result type depends only on the types of the arguments and the computational aspect of the function. Accordingly, the type of the function will not be used in the computational process except for selecting the branch to be executed. Therefore, all the information is still available. Of course we do not have appropriate types in our model to express that a certain function has no loss of information. Castagna [4] presents a type system for late-bound overloading in which this can be expressed. Our work also is a step towards a better understanding of calculi combining parametric polymorphism and type dependent computations

7Conclusion Overloading, subtyping and late-binding are important features of many object-oriented pro-

gramming languages. Still, there was no interpretation available for  calculi including these fg

principles. In this paper we presented a semantics for  , a calculus combining these three ×ØÖ features. Castagna [4] proposed to interpret terms as pairs (computation, type-symbol) in order to handle late-binding, but he did not give an actual model. Our construction is based on this idea and provides an interpretation for overloading, subtyping and late-binding. The model construction is carried out in a system of explicit mathematics. Types are represented by names in these theories and names are first-order values. Hence, they can be used in computations, which is the key to model overloading and late-binding. This paper provides a first study on how this feature of explicit mathematics can be used to investigate principles of object-oriented programming. Our work shows that theories of types and names are well suited to examine principles that involve computations with types such as overloading and late-binding. We propose to develop a system of explicit mathematics which directly supports overloading. This theory should be proof-theoretically weak, but should also have strong expressive power, [12, 13]. As noticed in the previous section, there is a strong connection between loss of information and parametric polymorphism. Since we obtained a solution to the problem of loss of information for free in our model, we think that explicit mathematics is also an appropriate framework to explore parametric polymorphism in the context of late-bound overloading. fg

544 A Semantics for  : a Calculus with Overloading and Late-binding ×ØÖ Acknowledgements We would like to thank Giorgio Ghelli, Gerhard J¬ager and Thomas Strahm for many helpful comments on earlier versions of this paper. This research was supported by the Schwei- zerische Nationalfonds.

References [1] H. Barendregt. The Lambda Calculus. North-Holland, revised edition, 1985. [2] L. Cardelli. A semantics of multiple inheritance. Information and Computation, 76, 138Ð164, 1988. [3] L. Cardelli and P. Wegner. On understanding types, data abstraction, and polymorphism. Computing Surveys, 17, 471Ð522, 1985.

[4] G. Castagna. Object-Oriented Programming: A Unified Foundation.Birkh¬auser, 1997.

fg  [5] G. Castagna. Unifying overloading and -abstraction: . Theoretical Computer Science, 176, 337Ð345,

1997.

² [6] G. Castagna, G. Ghelli and G. Longo. A semantics for  early: a calculus with overloading and early binding. In Typed Lambda Calculi and Applications, M. Bezem and J. F. Groote, eds, volume 664 of Lecture Notes in Computer Science, pp. 107Ð123. Springer, 1993. [7] G. Castagna, G. Ghelli and G. Longo. A calculus for overloaded functions with subtyping. Information and Computation, 117, 115Ð135, 1995. [8] S. Feferman. A language and axioms for explicit mathematics. In J. N. Crossley, editor, Algebra and Logic, volume 450 of Lecture Notes in Mathematics, pages 87Ð139. Springer, Berlin, 1975. [9] S. Feferman. Constructive theories of functions and classes. In Logic Colloquium ’78,M.Boffa,D.vanDalen, and K. McAloon, eds, pp. 159Ð224. North Holland, 1979. [10] S. Feferman. Iterated inductive fixed-point theories: Application to Hancock’s conjecture. In Patras Logic Symposion, G. Metakides, ed. pp. 171Ð196. North-Holland, 1982. [11] S. Feferman. Polymorphic typed lambda-calculi in a type-free axiomatic framework. In Logic and Computa- tion, volume 106 of Contemporary Mathematics, W. Sieg, ed. pp. 101Ð136. American Mathematical Society, 1990. [12] S. Feferman. Logics for termination and correctness of functional programs. In Logic from Computer Science, volume 21 of MSRI Publications, Y. N. Moschovakis, ed. pp. 95Ð127. Springer, 1991. [13] S. Feferman. Logics for termination and correctness of functional programs II: Logics of strength PRA. In Proof Theory, P. Aczel, H. Simmons and S. S. Wainer, eds. pp. 195Ð225. Cambridge University Press, 1992. [14] G. Ghelli. A static type system for late binding overloading. In Proc. of the Sixth International ACM Conference on Object-Oriented Programming Systems and Applications, A. Paepcke, ed. pp. 129Ð145. Addison-Wesley, 1991. [15] J.-Y. Girard. Interpretation fonctionelle etelimination ´ des coupures de l’arithm´etique d’ordre sup´erieur.Th`ese de doctorad d’«etat, Universit«e de Paris VII, 1972. [16] G. J¬ager. Induction in the elementary theory of types and names. In Computer Science Logic ’87, volume 329 of Lecture Notes in Computer Science,E.B¬orger, H. Kleine B¬uning, and M. M. Richter, eds. pp. 118Ð128. Springer, 1988. [17] G. J¬ager. Type theory and explicit mathematics. In Logic Colloquium ’87, H.-D. Ebbinghaus, J. Fernandez- Prida, M. Garrido, M. Lascar, and M. Rodriguez Artalejo, eds. pp. 117Ð135. North-Holland, 1989. [18] G. J¬ager. Applikative Theorien und explizite Mathematik. Technical Report IAM 97-001, Universit¬at Bern, 1997.

[19] G. J¬ager and T. Strahm. Totality in applicative theories. Annals of Pure and Applied Logic, 74, 105Ð120, 1995. Ì

[20] G. J¬ager and T. Studer. Extending the system ¼ of explicit mathematics: the limit and Mahlo axioms. Sub- mitted. [21] M. Marzetta. Predicative Theories of Types and Names. PhD thesis, Institut f¬ur Informatik und angewandte Mathematik, Universit¬at Bern, 1994. [22] J. C. Reynolds. Towards a theory of type structure. In Programming Symposium: Proc. of Colloque sur la Programmation, volume 19 of Lecture Notes in Computer Science, pp. 408Ð425. Springer, 1974. [23] T. Strahm. Partial applicative theories and explicit substitutions. Journal of Logic and Computation, 6, 55Ð77, 1996. Received 15 November 1999