<<

Middlesex University Research Repository

An open access repository of Middlesex University research

http://eprints.mdx.ac.uk

Clark, Tony (1997) Metaclasses and reflection in . Technical Report. University of Bradford.

Available from Middlesex University’s Research Repository at http://eprints.mdx.ac.uk/6181/

Copyright:

Middlesex University Research Repository makes the University’s research available electronically.

Copyright and moral rights to this thesis/research project are retained by the author and/or other copyright owners. The work is supplied on the understanding that any use for commercial gain is strictly forbidden. A copy may be downloaded for personal, non-commercial, research or study without prior permission and without charge. Any use of the thesis/research project for private study or research must be properly acknowledged with reference to the work’s full bibliographic details.

This thesis/research project may not be reproduced in any format or medium, or extensive quotations taken from it, or its content changed in any way, without first obtaining permission in writing from the copyright holder(s).

If you believe that any material held in the repository infringes copyright law, please contact the Repository Team at Middlesex University via the following email address: [email protected]

The item will be removed from the repository while any claim is being investigated.

Metaclasses and Reection in Smalltalk

A N Clark Department of Computing University of Bradford

Bradford West Yorkshire BD DP UK

email ANClarkcompbradacuk tel

Septemb er

Keywords ob jectoriented metaob jects reection inheritance message passing

Abstract

Many Ob jectOriented Programming Languages provide reective features which may b e used

to control the interpretive mechanism of the language Often these features are dened with

resp ect to a golden braid consisting of ob jects classes and metaclasses This pap er describ es

the Smalltalk golden braid and generalize it for multiple inheritance Multiple inheritance

leads to choices b etween many dierent inheritance strategies The reective features of

Smalltalk cannot aect the basic mechanisms of inheritance and so an arbitrary choice must

b e made for multiple inheritance A language is describ ed in which the reective features of

Smalltalk are extended so as to allow programmer dened inheritance strategies

Intro duction

The evaluation of a expression e in a given context c may b e describ ed

by the evaluation of a program p which takes a representation of e and c as input e is termed

an ob jectlevel construct whilst p and the representations of e and c are termed metalevel

constructs For illustration we use an op erator M which maps ob jectlevel constructs to

metalevel constructs If the languages which are used for b oth the ob ject and metalevels

are the same and causally connected then the language is reective

Ob jectOriented Programming Languages OOPLs have interpretive mechanisms which

are based up on classes ob ject creation message passing and inheritance Classes typically

dene the lo cal state and op erations for ob jects which are their instances When a message

is passed to an ob ject the op eration with the message name is invoked with resp ect to

the lo cal state A class inherits from another class by including all the inherited storage

and op eration denitions along with its own The metalevel of an OOPL describ es how to

p erform inheritance message passing instance creation etc If the OOPL is reective then

these mechanisms are describ ed in terms of messages which are sent to ob jects at the meta

level Consider the ob jects at some base level B the ob jects and messages which describ e how

to p erform creation of ob jects at B message passing at B etc are dened at level MB these

ob jects are called classes A class at level MB is characterized by controlling the creation

and subsequent b ehaviour of a collection of ob jects at level B The ob jects which are classes

2

at level MB are created and controlled by ob jects at level M B these ob jects are called

2

metaclasses A metaclass at level M B is characterized by controlling the creation and

subsequent b ehaviour of classes at level MB

coined the term golden braid to describ e the relationship b etween ob jects classes and

metaclasses Of course since classes are ob jects then we can view the golden braid starting at

3 2

MB and ending at M B which reinterprets the metaclasses at level M B as classes

n

A feature of an OOPL is builtin at all levels when its b ehaviour is the same for M n

Such features cannot b e extended and may b e said to b e intransigent An OOPL can b e

reective whilst still having intransigent features an OOPL which has no intransigent features

may b e said to b e ful ly reective

Reective OOPLs include Smalltalk Lo ops CLOS KRS Ob jVLisp

These languages dier in terms of the ways in which the golden braid is implemented

and the extent to which it may b e used to aect the basic interpretive mechanisms of the

resp ective languages

This pap er describ es the reective p ower of Smalltalk identies an intransigent language

feature send and prop oses a language extension which increases the reective p ower by

including the feature into the metalevel x describ es a simple functional language which will

b e used to implement three reective ob jectoriented systems which dier with resp ect to how

expressive their reective features are x describ es a system called Abstract Smalltalk AS

which is an implementation of the relevant features of the language Smalltalk AS exhibits

single inheritance which is generalised to multiple inheritance in the system AS with Multiple

Inheritance ASMI describ ed in x Both AS and ASMI have intransigent language features

including ob ject representation and message passing which means that it is dicult to have

multiple typ es of inheritance mechanism coexistsing in the same system This is particularly

a drawback when the system exhibits multiple inheritance b ecause there are many orthogonal

multiple inheritance schemes ASMI with Reective Send ASMIRS describ ed in x makes

b oth ob ject representation and message passing a reective feature of the system This is

shown to supp ort dierent typ es of multiple inheritance strategy within the same system

Functional Representation

The systems AS ASMI and ASMIRS are constructed using a simple functional language

which following has b een enriched with data values op erators and evaluation rules

which are characteristic of ob jectoriented programming languages The language has a call

byvalue semantics and enriches the evaluation rules for the calculus with pattern

matching currying rst class environments and up dateable lo cations The syntax of the

language is divided into two the kernel syntax which is given a semantics using a state

transition system based on the SECD machine and the sugared syntax which is given

a semantics by translating into the kernel For more information ab out functional languages

see and The sugared syntax is given b elow

T let D

D I E j F

+ +

F I P E j meth I P E j F jF

j P P j KP j N j S P I j

+

E I j N j S j P E j EE j EOE j if E then E else E j E E j E j E E

+ +

E E j E where D j let D in E j case E of A end j op en E in E

A P E j AA

A program is a sequence of top level recursive denitions t T and expressions e E D

is the syntax of declarations which may b e a simple value such as i or a functional

declaration f F such as add x y x y Functional declarations may b e overloaded and

may include metho ds b oth of which are describ ed in app endix A Patterns p P are used

in binding p ositions to limit the domain of a function and to decomp ose the value which is

supplied as an argument to the function by extracting subcomp onents and binding them to

identiers A pattern may b e an identier i in which case the supplied value is b ound to i

a wildcard in which case the supplied value is ignored a tuple p p in which case

1 n

the supplied value must b e a tuple of the same length and the corresp onding subcomp onents

must match against the subpatterns a constructor k K applied to a pattern p in which

case the supplied value must b e constructed using k eg k v and v must match p or a

constant numb er n N or string s S in which case the supplied value must b e the constant

An expression e is an identier numb er or string a function p p p e which may

1 2 n

b e curried and have patterns in the binding p ositions a prex application e e an inx

1 2

application e e where O denes a collection of inx op erators a conditional expression a

1 2

tuple a parenthesized expression a sequenced expression e e which is used to control side

1 2

eects a list expression e e where e constructs singleton lists and is the empty

1 n

list a where or let expression b oth of which may have a sequence of declarations which

are established in parallel a case expression case e in p e p e end where e

1 1 n n

is evaluated and tested against the patterns in turn the rst pattern which matches will

deconstruct the value of e p ossibly bind some identiers and evaluate the corresp onding

expression an op en expression op en e in e where e pro duces an environment binding

1 2 1

identiers to values which is added to the current environment for the scop e of the evaluation

of expression e

2

Environments are collections of asso ciations bindings b etween keys and values The

op erational semantics of programming languages often uses environments which bind names

to values in order to describ e the identiers which may b e legally referenced at any p oint in

the program execution and their resp ective values Environments are a convenient represen

tation for ob jects and the functional language provides builtin op erators for constructing and

manipulating environments as data values The empty environment which binds no keys to

values is the value fg a singleton environment binding the key k to the value v is constructed

using an inx op erator k v a pair of environments are concatenated using an inx op

erator e e The value asso ciated with a key in an environment is lo oked up using an

1 2

inx op erator e k where the environment e binds k more than once the rightmost value is

returned When e do es not bind k the distinguished value is returned

Functions are asso ciated with environments which dene the values for the identiers

which are freely referenced in the function b o dies The environment which is asso ciated with a

function or closure is returned using the reication op erator R The environment asso ciated

with a function is up dated pro ducing a new function using the instal lation op erator I which

is is applied to a pair e f Using environment primitives R and I the inx op erator

dened which extends the environment of a function on the right

let e f I R f e f

The systems AS ASMI and ASMIRS implement ob jects using side eects The builtin

evaluates its left hand op erand to pro duce an up dateable lo cation ie the op erator

address of the value in the state machine which is shared with many other values in the

program state The contents of the lo cation are up dated to by the value of the right hand

op erand All values in environments are up dateable lo cations

Cyclic data values are constructed using the paradoxical op erator Y which nds the xed

p oint of a function

Yf f Yf

and list homomorphisms are constructed Lists are built using the constructors

using the op erators n and n which are dened as follows

r

l

n f v v v v f v f v f v v

r 1 2 n 1 2 n

n f v v v v v f v f v f v

1 2 n 1 2 n

l

Environments are built using the constructors fg and environment homomor

phisms are constructed using using the op erator for example

v k v fg k v k v v k v

1 1 2 2 1 1 2 2

g Sets are used to implement ob jects in b oth ASMI and ASMIRS The empty is f

constructs singleton sets is the set union op erator and is the set dierence op erator

The functional language supp orts set comprehensions it is b eyond the scop e of this pap er

to describ e how such comprehensions are implemented in functional languages see for

more details

The op erator splitlistl will map a pair v l to a pair l v l such that l l v l

1 2 1 2

and v o ccurs only once in l The op erator splitlistr is similiar except the result is l v l

1 2

The op erator nd is applied to a predicate p a value v and a list l and will return the rst

value in l which satises the predicate p or v otherwise

The semantics of the functional language is describ ed by a translation to the kernel lan

guage in app endix A

Abstract Smalltalk

Smalltalk implements a golden braid with b oth classes and metaclasses treated as prop er

ob jects The metaclasses are used in a slightly restricted way such that a metaclass has

only one instance Smalltalk is not fully reective since the representation of ob jects and the

semantics of message passing are intransigent language features This section describ es the

Smalltalk golden braid in terms of a system called Abstract Smalltalk AS x describ es

the basic classes necessary to supp ort the Smalltalk interpretive mechanism x describ es

how AS is represented using the language of x x describ es the main AS op erations of

ob ject creation and message passing and x gives a metacircular denition of AS

Basic Classes

An AS object is a data value which contains two environments an instance variable environ

ment and a method environment The metho d environment binds names to functions each of

which can b e invoked by sending the ob ject a message containing the name referred to as

the message selector and the actual parameter The instance variable environment provides

bindings for identiers which may b e referenced in the b o dy of the metho ds but otherwise

cannot b e accessed

Each ob ject is created by instantiating a class which denes the metho d environment and

the names which will b e b ound in the instance variable environment The instantiation pro cess

supplies values for each of the instance variable names A class is created as a subclass of

another class which is referred to as its superclass the new class will contain all of the instance

variable names and metho d denitions from its sup erclass this is termed inheritance

In AS all classes are also ob jects A class is distinuished from an ordinary ob ject b ecause

its metho d environment contains a function which can create new ob jects and its instance

variable environment contains the metho ds and instance variable names which all instances

of the class will contain

Classes are instances of classes which are termed metaclasses A metaclass is distinguished

from an ordinary class b ecause its instance variable environment contains denitions of the

instance variables and metho ds which are necessary to b e a class As is shown in x it

is not necessary to dene the term metametaclass b ecause metaclasses can b e conveniently

dened in terms of themselves

AS is dened as a collection of basic classes each of which has an instance variable envi

ronment and a metho d environment

ienv ivars x y menv init pointinit

menv new cdnew init cdinit

which is the instance variable and metho d environments for a class which creates two dimen

sional p oints Each instance of this class will have two instance variables namely x and y

which are initialised using the metho d named init implemented as the function pointinit An

instance is created by sending the class a new message which is implemented by the function

cdnew The class itself was initialised using the metho d named init which is implemented

using the function cdinit An instance of this class is the following

ienv x y

menv init pointinit

The basic classes which are necessary to supp ort the AS system are shown in gure they

are object which all other classes inherit from describ ed in x cd which is a metaclass

describing the minimum amount of information necessary to b e a class describ ed in x

mc which is a metaclass describing the minimum amound of information necessary to b e

a metaclass describ ed in x nally class which is a metaclass describing the minimum

amount of information necessary to b e a nonmetaclass class describ ed in x

Figure can b e interpreted by chasing links In order to nd out what instance variables

and metho ds an ob ject has rst follow the instance link to the class which was used to create

the ob ject This class will dene a collection of instance variable names which are b ound to

values in the ienv comp onent of the ob ject and a collection of metho ds which form part of

the menv comp onent of the ob ject Next follow the sup erclass link to a class which denes

further variable names and metho ds which are found in the ienv and menv comp onents of

the ob ject resp ectively Continue this pro cess until object is reached All sup erclass links

eventually lead to object which is referred to as the ro ot of the AS ob ject inheritance

All classes form the ro ot of an inheritance tree which identies a collection of ob jects which

contain the variable names and metho ds dened by the class object superclass

instance cd

mc class

cc oc cdc

mcc

Figure Initial AS class conguration

Ob ject

ienv super nul lclass menv new cdnew

ivars class init cdinit

menv init objinit subclass classsub

dnu objdnu dnu objdnu

class oc

The class object is the ro ot of the AS inheritance tree and as such denes the minimum

instance variable names and metho ds in order to b e an ob ject The sup erclass of object is

the pseudo class nul lclass which is necessary to allow all classes to uniformly inherit from

somewhere object denes a single instance variable named class which is inherited by all AS

classes and therefore will o ccur in the ienv comp onent of all AS ob jects The value of this

variable in each ob ject will b e the class which was used to create the ob ject object denes

two metho ds which will b e inherited by all AS classes and therefore will o ccur in the menv

comp onent of all AS ob jects unless shadowed by a sub class denition The metho d named

init is implemented using the function objinit and is used to initialise the ob ject after it is

created The single purp ose of obnjinit is to set the class variable The metho d named dnu

is implemented using the function objdnu and is invoked when a message is sent to an ob ject

for which there is no menv comp onent

The menv comp onent of object contains the metho ds which dene the b ehaviour of object

This is the standard class b ehaviour new is used to create an instance of the class init is used

to initialise the class after it is created subclass is used to create a class which is a sub class of

object and dnu is invoked if object is ever sent a message whose name is not b ound in menv

Class Description

ienv super object menv new cdnew

ivars super ivars menv init cdinit

menv new cdnew subclass classsub

init cdinit dnu objdnu

class cdc

The class cd is the ro ot of the AS class inheritance tree and as such denes the minimum

instance variable names and metho ds in order to b e a class The sup erclass of cd is object

so cd inherits the instance variable names ie class and the metho ds ie init and dnu

from object The minimum instance variables which are necessary to b e a class are super

whose value must b e a class whose instance variable names and metho ds are inherited ivars

whose value must b e a list of instance variable names and menv whose value must b e an

environment binding metho d names to functions Notice that all the classes describ ed in

this section have ienv comp onents which bind variables super ivars menv and class which

originate from cd class b eing inherited from object

Metaclass

ienv super cd menv new cdnew

ivars init cdinit

menv subclass metasub subclass classsub

class mcc dnu objdnu

The class mc is the ro ot of the AS metaclass inheritance tree and as such it denes

the minimum instance variable names and metho ds in order to b e a metaclass Each time

a class is constructed an instance of mc is also constructed as its unique metaclass The

metaclass will dene the metho ds for its sole instance which are extensions to the standard

class b ehaviour as dened by cd Furthermore when a class is sent a message telling it to

create a sub class of itself its metaclass is also sent a sub class message so the metaclass

inheritance tree corresp onds exactly to the class inheritance tree This is seen in gure

where the classes object mc cd and class are instances of the metaclasses oc mcc cdc and cc

where the inheritance b etween the metaclasses follows exactly that b etween the corresp onding

classes

The class mc denes a metho d subclass which is used to create a sub class of a given

metaclass The metho d is implemented using the function metasub which will create an

instance of mc and initialise this to have the receiver of the message as its sup erclass

Class

ienv super cd menv new cdnew

ivars init cdinit

menv subclass classsub subclass classsub

class cc dnu objdnu

The class class is the ro ot of the AS class nonmetaclass inheritance tree and as such

dened the minimum instance variable names and metho ds in order to b e a nonmetaclass

class class denes a metho d subclass which is used to create a sub class of the receiver This

metho d deals with constructing a new metaclass by sending a subclass message to the class

of the receiver and then creating an instance of this metaclass subclass is implemented by

the function classsub

A subclass message is sent to a receiver c as follows

1

send c subclass e l e

1 1 2

where e is an environment of metho ds which are termed class metho ds and which will

1

form part of the menv comp onent of the result l and e are a list of names and a metho d

2

environment which are the instance variables and metho ds dened by the new sub class

Assuming that c is a nonmetaclass then the send expression is equivalent to the following

1

sendsend c subclass e new c l e

2 1 1 2

where c is the class of c Since c is a metaclass it will handle the subclass message dierently

2 1 2

from c Up on receiving a subclass message c will create an instance of mc supplying the

1 2

value of the sup erclass instance variable names and metho d environment as c and e

2 1

resp ectively

sendsend mc new c e new c l e

2 1 1 2

The result of this expression is a class

ienv super c menv e

1 1

ivars l new cdnew

menv e init cdinit

2

class c subclass classsub

3

dnu objdnu

where the class c is

3

ienv super c menv new cdnew

2

ivars init cdinit

menv e subclass metasub

1

class mc dnu objdnu

Representation

An ob ject is either null which corresp onds to an instance of the sup erclass of object or a

basic ob ject which corresp onds to an instance of anything else Classes are layered like onions

where the outermost layer corresp onds to the most recent sub class the next inner layer to its

sup erclass the next inner layer to its sup ersup erclass etc The heart of the onion is the value

nul lclass which is the pseudo sup erclass of object An ob ject mirrors the onionlike structure

of its class with each layer containing the instance variables and metho ds declared by the

corresp onding layer of the class Each layer of an ob ject has an additional value which is

the entire ob ject and is referred to as self The heart of an ob jectonion o is the null ob ject

nul lobjo which is a pseudo instance of nul lclass

An ob ject is represented as obje e o o where e is an environment asso ciating in

1 2 1 2 1

stance variable names with lo cations containing their values ienv e is an environment

2

asso ciating metho d names with metho ds menv o is an ob ject which is the next innermost

1

layer of the ob jectonion referred to as super and o is the whole ob jectonion referred to as

2

self

Basic Op erations

The AS system is dened as a collection of basic classes whose metho ds are implemented

using functions objdnu cdnew etc and a message passing op erator send The functions

which implement the class metho ds are dened in terms of a single op erator whose job is

to construct ob jects in a particular format This op erator mkobj is dened in x The

op erator send implements the message delivery service which searches an ob ject for a named

metho d and then invokes it send is dened in x

Both ob ject creation and message passing are dened in terms of the concatenation of

the instance variable environments which are contained in an ob ject The op erator getal lenv

dened b elow is used to construct this environment

fg j let getal lenvnul lobj

o e getal lenv o getal lenvobje

Ob ject Creation

The op erator mkobj dened b elow

let mkobjnul lclass o nul lobjo j

mkobjco op en getal lenvc in objn fgivars menv mkobjsuper o o

r

This op erator is the primitive AS ob ject creation op erator The argument c is a class which

is to b e instantiated and o is an ob ject which will b e the self comp onent of the resulting

instance A class will bind the variables super ivars and menv in its ienv comp onent A

0

new instance is created layer by layer Each layer has the format obje e o o where e

1 2 1

binds the instance variable names from the corresp onding layer of the class c to the null value

0

e is the metho d environment from the corresp onding layer of c and o is the result of

2

instantiating the sup erclass of c

Message Passing

The op erator send dened b elow

let send nul lobjo n v send o dnu n v j

send obje e o o n v

1 2 1 2

if n dom e

2

then e no o e getal lenvo v

2 2 1 1 1

else send o n v

1

p erforms AS message passing The op erator has three parameters which corresp ond to the

receiver of the message the selector of the message and the actual parameter of the message

The op eration of send can b e describ ed in terms of the ob jectonion mo del describ ed in

x When a message is sent to an ob ject the metho ds dened by the outermost layer are

searched for a metho d whose selector matches If such a metho d is found then it is activated

by applying it to ob jects o and o an environment e getal lenv o and value v o is the

2 1 1 1 2

whole ob jectonion no matter which particular layer contains the matching metho d o is the

1

ob jectonion constructed by stripping away the layer which contains the matching metho d if

a message is sent to o it will continue the search where send left o e getal lenvo is the

1 1 1

concatenation of all instance variable environments in all layers from that which contained the

matching metho d to the heart of the ob jectonion v is the actual parameter of the message

If no metho d matches then the pro cess continues with the next innermost onionlayer If the

pro cess encounters the heart of the onion then no metho d was dened for the selector and

dnu is sent to o

2

Denition

AS is dened as an initial collection of classes and a delivery service send The classes dene

metho ds which are implemented using the functions which are given b elow

let meth objinitc class c self

let meth objdnu m error message str m not understood

let meth cdinit c l e v super c ivars l menv e send next init v

let meth cdnew v send Ymkobj self init v self

let meth metasub e send mc new self e

let meth classsub e l e send sendclass subclass e new self l e

1 2 1 2

1

Collections of metho ds and instance variable names are predened for convenience

let om init objinit dnu objdnu

let cdm new basicnew init classinit

let cdv super ivars menv

let mm subclass metasub

let cm subclass classsub

The denitions of the AS classes are given b elow

let object send oc new nul lclass class om

oc sendmc new class fg

cd sendobject subclass fg cdv cdm

mc send cd subclass fg mm

class sendcd subclass fg cm

These denitions are metacircular ie the classes are assumed to exist for the pro cess of

their own denition It is necessary to b o otstrap AS by providing initial values for the

classes which can b e used to construct themselves Using the description of chasing links

given in x it is p ossible to determine the outcome of the metacircular denitions For

example it is p ossible to replace all messages with the selector subclass with a selector

new by observing that all the receivers in question will inherit the metho d from class where

it is implemented using classsub The denitions have b een partially expanded by chasing

1

We should b e careful ab out sharing and side eects here since the lo cations which are created in the

predened environments will o ccur more than once in the nal system A complete discussion of the issues

of sharing is outside the scop e of this pap er and we dene that no lo cation is ever implicitly copied ie all

o ccurrences of bindings will share and b e aected by a single side eect

links in the following denitions

let object objfg fg o object

1

where o objfg subclass classsub o object

1 2

where o obje cdm o object

2 3

where e super nul lclass ivars class menv om

o objclass oc om nul lobj object object

3

oc obj fg mcm o oc

1

where o obje cdm o oc

1 2

where e super cdc ivars cdv menv cdm

o objclass mc om nul lobjoc oc

2

cd send object subclass fg cdv cdm

mc send cd subclass fg mcm

class sendcd subclass fg cm

AS with Multiple Inheritance

The AS system describ ed in x provides single inheritance which means that each class is

allowed only one sup erclass whose state variable declarations and metho d denitions are

inherited This leads to a tree structure of classes with object as the ro ot of the tree and

new classes b eing added as leaves This section will generalize AS to pro duce ASMI which is

Abstract Smalltalk with Multiple Inheritance Multiple inheritance allows each class to have

more than one sup erclass from which it will inherit state variable declarations and metho d

denitions This leads to a graph structure of classes with the constraint that the graph must

not contain cycles ie a class cannot inherit from itself

When classes are tree structured as in AS inheritance of variable declarations and meth

o ds is straightforward b ecause there is no choice as to the order in which information from

sup erclasses will b e inherited When classes are graph structured as in ASMI inheritance

b ecomes more complex b ecause there may b e more than one route from one class to another

For example since all classes inherit from object any class which inherits from more than one

sup erclass will construct at least two dierent inheritance paths from itself though resp ective

sup erclasses leading to object Will the information from object b e inherited twice What

happ ens when an ob ject sends a message to next Dierent programming languages oer

dierent solutions to this problem these include

Information is inherited as many times as it is reachable from the inheritor This will

lead to multiple copies of instance variable lo cations but has the advantage of b eing

mo dular

Information is inherited only once The graph is traversed from an inheritor such that

each no de is encountered exactly once This will cause no problems provided that the

names of the metho ds are distinct If they are not then a question arises with resp ect to

which metho d will shadow the other and therefore which metho d will b e executed when

its selector is part of a message to super One strategy is a left to right traversal of the

inheritance graph omitting a sup erclass if it has already b een visited Another strategy

is a left to right traversal of the inheritance graph omitting a sup erclass if it will b e

visited later Both strategies have the disadvantage that the shap e of the inheritance

graph is distorted with resp ect to inherited classes

Language facilities are provided so that all ambiguities which should arise are eliminated

under programmer control

It is not the purp ose of this pap er to discuss in depth the merits of various MI strategies

for more information see and for ASMI we will arbitrarily cho ose a left to right

graph traversal which defers a sup erclass to its nal o ccurence ASMI and AS are compared

in x the ASMI basic classes are describ ed in x the representation of ASMI ob jects is

dened in x the ASMI basic op erations are dened in x and nally ASMI is dened in

x

Comparison with AS

ASMI is a version of AS with multiple inheritance The inheritance strategy which is adopted

is that of a left to right graph traversal which defers shared sup erclasses to their nal o c

currence Where each AS class has a single class as the value of its state variable super

each ASMI class has a corresp onding state variable called supers whose value is a list of

classes The empty list is used for the sup erclass of object which delivers us from the irksome

nul lclass

Each class forms the ro ot of an acyclic directed graph where the no des are classes and

the edges represent inheritance links In AS when a class is instantiated the onion structure

of the classes is translated to an identical onion structure for the instance in which all the

instance variables are b ound to initial values In ASMI when a class is instantiated the

graph structure of the classes is translated to an identical graph structure for the instance in

which all the instance variables have b een allo cated new storage lo cations

When an AS message is sent to an ob ject the search will progress through successive

ob ject layers until a metho d with the required selector is found A subsequent message sent

to next will continue the search until the heart of the onion structure is found When an

ASMI message is sent to an ob ject the search will p erform a left to right depth rst graph

traversal until a metho d with the required selector is found A subsequent message sent to

next will continue the search until the nal no de in the graph is encountered

In AS a class is constructed by sending a subclass message to a class c which will b ecome

the sup erclass of the new class The class is created by sending a subclass message to the

class of c which creates a metaclass which is instantiated by sending it a new message In

ASMI classes have multiple sup erclasses so it is not p ossible to send a single subclass message

without giving one of the sup erclasses an articial status as the receiver of the message and

all other sup erclasses lesser merit by supplying them as the argument In ASMI a class is

created by sending a metaclass a message with the selector new the sup erclasses of the

new class are supplied as a list in the message argument If a new metaclass is required for

example to dene the class metho ds then it must b e created explicitly

Basic Classes

Figure shows the initial ASMI conguration of classes Following Ob jVLisp it is not

necessary for each class to have a single unique metaclass the AS mo del whereby each class

has a single unique metaclass used to dene the class metho ds can b e built from the general

mo del up on which ASMI is based In ASMI there are only two initial classes object and

class all classes will have object as their ultimate sup erclass and all metaclasses will have

class as their p enultimate sup erclass object describ es the state variables and metho ds which object instance

subclass

class

Figure Initial ASMI class conguration

are common to all classes and class describ es the state variables and metho ds which are

common to all metaclasses class is an instance of itself and therefore represents the xed

p oint of the M op erator discussed in x ASMI classes object and class are describ ed in x

and x resp ectively

Ob ject

ienv supers menv new classnew

ivars class init classinit

menv init objinit dnu objdnu

gc objgc

dnu objdnu

class class

object denes no sup erclasses a single instance variable class and three metho ds which

initialise an ob ject by setting the class instance variable get the class of an ob ject and handle

the case when an ob ject do es not understand a message

Class

ienv supers object menv new classnew

ivars supers ivars menv init classinit

menv init classinit dnu objdnu

new classnew

class class

class denes the instance variables supers ivars and menv These corresp ond to those of cd

in AS except that for a given class supers is a list of classes rather than a single class class

denes metho ds new and init which will instantiate and initialize the receiver rep ectively

Representation

An ASMI ob ject is a graph whose no des contain instance variable and metho d environments

and whose links represent inheritance The representation of a graph is as follows

ge S e e S

1 1 2 3 2

where e is an environment mapping no de addresses to no des S is a set of edges e is an

1 1 2

environment mapping edges to their source no de address e is an environment mapping edges

3

to their target no de address and S is a partial ordering on the no des A no de is represented

2

as

ne e

1 2

where e is an instance variable environment and e is a metho d environment The functions

1 2

getenv and getmenv extract the instance variable and metho d environments from a no de

Basic Op erations

Like AS ASMI is dened in terms of a collection of basic classes the functions which im

plement their metho ds and a message delivery service In addition ob jects are implemented

as graphs and op erations are provided which can b e used to traverse graphs in various ways

x describ es the op erations for graph traversal x describ es basic ob ject creation and

x describ es the ASMI message delivery service

Graph Traversal

The basic op erations which are used to traverse ob ject graphs are dened as follows

let nul lgraph gfg fg fg

let gm f ge S e e S g I f fge S e e S

1 1 2 3 2 1 1 2 3 2

let gmerge ge S e e S g e S e e S ge e S S e e e e S S

1 1 2 3 2 4 3 5 6 4 1 4 1 3 2 5 3 6 2 4

0 0

fe e e j e S fe j e e e e g g let root ge S e e

1 2 3 2 1 2 3

let targetnodes nge S e e fe e e j e S e e e ng

1 2 3 1 3 1 2

let walk ng n n walk sort targetnodes ng ord g

r

let traverse g walk ng where fng root g

The op erator gm is used to construct graph morphisms where f is a function to b e applied

to all the no des in the graph The op erator gmerge will merge two ob ject graphs to pro duce

a new ob ject graph targetnodes ng pro duces the set of all no des which are reachable from

the no de n by traversing one edge in g root g is the set of all no des which have no edges

incident up on them such sets will b e singletons for prop erly formed ob jects traverse g

is the sequence of no des which are visited stating with the ro ot of g and visiting each no de

reachable from the ro ot in an order which is consistent with b oth the ordering imp osed by

the edges of g and the partial ordering comp onent of g sort S S is the sequence of S

1 2 1

elements whose ordering is consistent with the partial ordering S

2

The op erator traverse will visit a no de more than once if it is reachable using two or

more paths from the ro ot of a graph ASMI will visit each no de once when searching for a

metho d in an ob ject graph The question arises as to which of the many paths to a no de

will b e chosen The two op erators dened b elow onr and onl will order the no des of an

ob ject graph so that no des which can b e encountered more than once are visited last and rst

resp ectively in a manner which is consistent with the ordering imp osed by the graph edges

and partial ordering The op erator removeifmarked will delete any no des which have b een

marked in a sp ecial way and is explained in x

let l n if n l then l else n l j

n l if n l then l else l n

let onr removeifmarked n I traverse

r

let onl removeifmarked n I traverse

l

The instance variable environment of an ob ject is extracted using the op erator getal lenv

let getal lenv o n getenvfgonr o

r

Ob ject Creation

A class may b e viewed as two dierent graphs The rst is as an ob ject graph since all classes

are ob jects where the no des contain instance variable and metho d environments and the edges

represent the inheritance of variables and metho ds The second is as a class graph where the

no des contain instance variable names and metho d denitions relating to the instances of the

class and edges represent inheritance of names and metho ds from the sup erclasses Given a

class graph and some initialisation values for the instance variables instantiation is a simple

graph morphism which retains the class graph structure and asso ciates the values with the

variable names in each no de The function cg dened b elow

let cgg

let e getal lenvg in

let n n e ivars e menv in

let g n gmerge cgnul lgraph e supers in

r

in addnode n g

translates a class represented as an ob ject graph to a class graph addnode n g will add

the no de n to the graph g by allo cating a new address for n and will link n to each no de in

root g by allo cating a new edge The function addnode is not dened in this pap er

Message Passing

ASMI message passing is similar to AS message passing since the ob ject is traversed to nd a

metho d with the given selector but diers b ecause the ob ject representation is a graph and

not a tree ASMI metho ds have the same representation as AS metho ds ie they have three

hidden parameters for self next and the instance variable environment The value of the rst

parameter is the entire ob ject graph The value of the second parameter is the entire ob ject

graph but the no des which were traversed in order to nd the metho d have b een marked so

that if a message is ever sent to next the marked no des are ignored Marking and unmarking

of ob ject graphs is p erformed by the following two functions

0 0 0

let mark gn e s t on gn fn g fmark n g e s t o

let unmark gm f where f ne e ne e j f markn n

1 2 1 2

ASMI message delivery is p erformed by the function send which is dened b elow

let sendo n v

let x nd ponr o where pnode n dom getmeths node

in if x then send unmark o dnu n v

else let o n mark I ost splitlistl x onr o

1

l

o unmark o

2

e n getenvfgnd splitlistr x onr o

r

in getmeths x no o ev

2 1

The b ehaviour of send is as follows onr is used to order the no des in the ob ject graph o

and x is a no de with a metho d environment containing the selector n If no such x is in o

then the message dnu is sent to o after it is unmarked Otherwise o is an ob ject which will

1

continue searching from no de x when it is sent a message In order to continue from x o is

1

pro duced by marking all of the no des which have b een traversed from the ro ot no de up to

and including x o is an ob ject which start searching from the original ro ot no de of o so

2

all marks are removed e is the environment constructed by concatenating all of the instance

variable environments in the no des which are traversed starting with x and continuing on

through the rest of o Finally the metho d is applied to the hidden parameters o o and e

2 1

and the actual parameter v of the message

Denition

ASMI is dened as an initial collection of classes and a delivery service send The classes

dene metho ds which are implemented using the functions given b elow

let meth objgc class

let instantiate c gmn l e nn fgl ecg c

r

let meth classnew v sendinstantiate self init v self

The collections of ob ject metho ds and class metho ds are predened for convenience

let om init objinit dnu objdnu gc objgc

let cm init classinit new classnew

The metacircular denition of the ASMI classes is given b elow

let object send class new class om

class sendclass new object cdv cm

The classes are b o otstrapp ed using the same reasoning as is discussed in x except ASMI

is signicantly simpler since it consists of two classes and not eight Even though there are

fewer classes ASMI do es not represent a reduction in expressive p ower since AS can b e

implemented by dening a metho d and a pair of classes

let asnew e l l e send send class new n f l e new l l e

1 1 2 2 r 1 1 1 2 2

where f c sendc gc

let asm sendclass new class new asnew

let asc send asm new asm fg

ASMI with Reective Send

x denes an OOPL called ASMI with multiple inheritance this is a generalisation of AS which

only supp orts single inheritance It is not p ossible to dene ASMI using the reective facilities

of AS since the inheritance strategy of AS dep ends up on send which is an intransigent language

feature This section denes the language Abstract Smalltalk with Multiple Inheritance and

Reective Send ASMIRS which extends the reective p ower of ASMI by making send a

metho d which is implemented at the metalevel Since send is a metho d it may b e extended

or redened this allows for programmer control of the inheritance strategy at the metalevel

Innite regress is prevented b ecause the language ASMIRS is metacircular and the xed

p oint of M causes message passing to b ottom out

x compares ASMIRS with ASMI and AS x describ es the ASMIRS basic classes x

extends the ob ject representation from ASMI so that it b ecomes a reective language feature

x describ es message passing and x gives the metacircular denition of ASMIRS

Comparison with ASMI

ASMI represents ob jects as graphs where the no des are instance variable and metho d envi

ronments and the multiple edges leading from a no de arise due to multiple inheritance The

ob ject creation and message delivery service require intimate knowledge of an ob ject graph

in order to construct them and extract metho ds with a given selector There is no scop e for

representing ob jects in any other way or for making the message delivery service dep endent

up on the typ e of an ob ject Ob ject representation and message delivery are intransigent lan

guage features in ASMI which means that there is only one p ossible ob jectgraph traversal

scheme when delivering a message

ASMIRS allows the representation and message delivery service of an ob ject to dep end

up on its typ e Ob ject creation is p erformed by sending a new message to a class Message

delivery is p erformed by sending a send message to the class of the receiver Multiple ob ject

representations and message delivery services can coexist within ASMIRS ASMI is easily

implemented in ASMIRS which will also allow dierent variations of the ASMI ob jectgraph

traversal scheme

Basic Classes

ASMIRS is dened as a basic collection of classes whose initial conguration is shown in gure

x describ es object and x describ es class

Ob ject

ienv supers menv new classnew

ivars class init classinit

menv init objinit on classon

dnu objdnu send classsend

class class dnu objdnu

object denes a single instance variable class whose value in any ob ject is the class which

was instantiated to pro duce the ob ject and two metho ds for initialisation and handling un

known messages object is an instance of class

Class

ienv supers object menv new classnew

ivars supers ivars menv init classinit

menv init classinit on classon

new classnew send classsend

on classon dnu objdnu

send classsend

class class

class denes three instance variables supers ivars and menv It denes four metho ds new

which is used to create ob jects and is implemented using classnew which represents ob jects

as ASMI graphs init which is used to initialise classes and is implemented by classinit on

which is used to order the no des in an ob jectgraph and is implemented by classon and send

which is the message delivery service for ob jects which are implemented as ASMI graphs and

is implemented by classsend

Representation

An ASMIRS ob ject is represented as a value

objc v

where c is the class which was instantiated to pro duce the ob ject and v is a value which

is the ob jects internal representation The functions classof and repof extract the class and

representation from an ob ject resp ectively

An ob ject is usually created by sending a class c a new message The class will resp ond

to this message by constructing a representation for the ob ject which is appropriate to the

way that the class implements its message handling service ie the metho d send In this

way all ob jects have a uniform representation containing a class and a value but the value is

manipulated at the metalevel by metho ds which are implemented by the class

Message Passing

A message is sent to an ob ject using the ASMIRS send primitive

send o n v

The message is delivered to the ob ject and executed by the delivery service which is imple

mented by the class of o A delivery service is implemented by a metho d send and p erformed

by sending the message

send classof o send o n v

Since classof o is itself an ob ject the send message is p erformed by the delivery service which

is implemented by its class

send classof classof o send classof c send o n v

When do es this regression terminate ASMIRS has an initial class conguration which is the

same as that for ASMI shown in gure Any direct instance of class will have a delivery

service which is implemented by classsend This knowledge is built into the basic message

sending primitive For any wellformed conguration of ASMIRS ob jects a chain of classof

calls will eventually lead to an instance of class whose delivery service is known The message

delivery primitive is dened b elow

let sendo n v

if classof o class

then case n of

on classon v

send classsend v

send classof o send o n v

end

else sendclassof o send o n v

It knows ab out very little of the ASMIRS system other than the minimal knowledge of how

class is implemented Other than this all messages are delivered by the particular service

dep ending up on the typ e of the receiver

Denition

ASMIRS is dened as an initial collection of classes and a delivery service send The classes

dene metho ds which are implemented using the functions given b elow

let meth classon o onr repof o

let meth classnew c v sendobj c instantiate repof c init c v

let meth classsend o n v

let x ndpsend classof o on o where px n dom getmeths x

in if x

then send objclassof o unmark repof o dnu n v

else let o n mark Ist splitlistl x send classof o on o

1

l

o objclassof o unmark repof o

2

e n getmeths fgnd splitlistr x send classof o on o

r

in getmeths x no o ev

2 1

The metho ds dened by class are dened to b e cm

let cm on classon new classnew

send classsend init classinit

The metacircular denition of ASMIRS classes is given b elow

let object send class new class om

class sendclass new object supers ivars menv cm

The following class c is an example of the reective p ower of ASMIRS at work c implements

ob jects as ASMI graphs but uses onl to order the no des rather than onr

let meth con o onl repof o

let c sendclass new class on con

Conclusion Further and Related Work

This pap er has describ ed three ob jectoriented systems of increasing reective p ower AS

ASMI and ASMIRS The initial system is based on the initial conguration of Smalltalk

classes AS implements single inheritance which is generalised to multiple inheritance in

ASMI There are a wide variety of multiple inheritance implementation strategies which can

not coexist within ASMI since the ob ject representation and message delivery service are

intransigent language features These restrictions are lifted in ASMIRS which allows multiple

ob ject implementations and message delivery services to coexist within the same language

by lifting them to the metalevel The semantics of AS ASMI amd ASMIRS is made precise

by implementing them in a functional language which has b een sp ecically designed with

primitive features for ob jectoriented languages

This work is closely related to the ideas of the Ob jVLisp community which

greatly simplied the representation of reective ob jectoriented programming features See

and for more recent work on particlar asp ects of metaclasses in ob jectoriented

programming languages The use of rst class identier binding environments as a basis for

ob jects and related programming language features is describ ed in

and There is a collection of literature describing elegant record environment calculi

used as the basis of ob ject oriented programming language semantics including and

The issues of reection in ob jectoriented programming languages has b een develop ed from

an abstract although executable p oint of view ASMIRS is a simple but precise sp ecication

of an ob jectoriented programming language which has a very expressive reective capability

ASMIRS can b e used as the basis of exp erimentation for new reective mechanisms Further

work is necessary to nd ecient implementation techniques for the features which have

b een describ ed in this pap er The reective features which have b een describ ed are strongly

related to a particular typ e of programming language namely class based ob jectoriented

languages A functional programming language whose semantics is describ ed in terms of a

state transition system such as the SECD machine is particularly amenable to the reication

of computational entities such as environments which are necessary to describ e reective

features and allow a program to control its own execution It would b e interesting to see other

reective mechanisms could b e designed using simple extensions to the underlying language

References

Agha G The Structure and Semantics of Actor Languages In Foundations of Ob ject

Oriented Languages LNCS

Bird R Wadler P Introduction to Functional Programming Prentice Hall Interna

tional Series in Computer Science

Bobrow D et al Object System Specication Lisp and Symb olic Compu

tation Jan

Bobrow D Stek M The Loops Manual Intelligent Systems Lab oratory Xerox PARC

Briot JP Cointe P The ObjVLisp Model Denition of a Uniform Reexive and

Extensible object Oriented Language ECAI

Briot JP Cointe P A Uniform Model for object Oriented Languages Using the class

Abstraction IJCAI

Cardelli L The Semantics of Multiple Inheritance Pro ceedings of the Conference on the

Semantics of Data Typ es SpringerVerlag LNCS June

Cardelli L A Semantics of Multiple Inheritance LNCS The Semantics of Data Typ es

Cardelli L Mitchell J Operations in Records Math Struct In Comp Science

Cardelli L Wegner P On Understanding Types Data Abstraction and Polymorphism

Computing Surveys

Clark A N Semantic primitives for OOPLS Forthcoming PhD Thesis QMW College

London University

Clark A N A Layered ObjectOriented Programming Language To app ear early

in The GEC Journal of Research Also submitted in a greatly extended form to ACM

TOPLAS Oct

Cointe P Metaclasses are First Class The ObjVLisp Model OOPSLA

Ducournau R et al Monotonic Conict Resoloution Mechanisms for Inheritance OOP

SLA

Ferb er J Computational Reection in Class based ObjectOriented Languages OOPSLA

Field A J Harrison P G Functional Programming Addison Wesley International

Computer Science Series

Fo ote B Johnson R E Reective Facilities in Smal ltalk OOPSLA

Gelernter D Environments as First Class Objects POPL

Goldb erg A Robson D Smal ltalk The Language and its Implementation Addison

Wesley

Graub e N Metaclass Compatibility OOPSLA

Harp er R et al A Record Calculus Based on Symmetric Concatenation POPL

Jagannathan S Metalevel Building Blo cks for Mo dular Systems ACM TOPLAS

Jagannathan S Agha G A Reective Model of Inheritance ECOOP

Kiczales G et al The Art of the Protocol MIT Press

Landin P The Mechanical Evaluation of Expressions Computer J

Landin P The Next Programming Languages Comm ACM

Maes P Reection in an object Oriented Language Vrije Universiteit Brussel AI Memo

Meyer B Eiel The Language Prentice Hall ob ject Oriented Series

Miller J Rozas G Free Variables and FirstClass Environments Lisp and Symb olic

Computation

Ossher H Harrison W Combination of Inheritance Hierarchies OOPSLA

Peyton Jones S The Implementation of Functional Programming Languages Prentice

Hall International

Plotkin G Cal lbyname cal lbyvalue and the calculus Theoretical Computer Science

Snyder A Encapsulation and Inheritance in ObjectOriented Programming OOPSLA

Steels L The KRS Concept System Vrije Universiteit Brussel AI Lab Technical Rep ort

th

Wadler P Comprehending Monads In Pro c Symp on Lisp and Functional Pro

gramming Nice ACM

A Functional Language Semantics

The kernel language syntax allows top level recursive denitions T and expressions E

0 0

T let I E

0 0

E I j N j S j I E j E E j if E then E else E j E e j E E

0 0 0 0 0 0 0 0 0 0 0

The semantics of the kernel language is that of a simple callbyvalue functional language

It is b eyond the scop e of this pap er to fully describ e the kenel semantics see for

a complete description which is based up on Landins SECD machine extended with

primitive features for ob jectoriented programming The semantics of the sugared language

is given as a translation to the kernel as follows Let b e a rewrite rule which removes

the outer layer of sugar from an expression is dened by case analysis b elow and by

rep eated application to a sugared expression will pro duce a kernel expression

A curried function is translated by making all of the functions explicit

p p p e p p p e

1 2 n 1 2 n

A function with a pattern parameter is translated to a function with a new parameter i which

returns a distinguished value if the supplied value do es not match the pattern A tuple

pattern uses isntuple to test whether the supplied vaue is a tuple of the correct length and

to extract the required comp onents from the tuple uses

p p e i if isntuple i then let p i p i n in e else

1 n 1 n

A constructor pattern uses isk and stripk to test whether the supplied value is constructed

using the constructor k and to strip the constructor to reveal the value underneath

k pe i if iski then let p stripk i in e else

A constant pattern tests whether or not the supplied value is the required constant

ce i if i c then e else

An overloaded function denition is translated to a single function denition where the alter

natives are comp osed using the inx op erator which is dened as follows

f v let x f v in if x then f x else x f

2 1 2 1

Function denitions are translated using the op erators name and body

f name f body f

The name of a collection of function denitions is only dened when the names of the functions

are all the same note that p denotes a sequence of patterns

name ip e i

name meth ip e i

name f j f name f name f

1 2 1 2

where combines identiers returning one of the identiers if they are b oth the same and

is undened otherwise The op erator body translates a named function or metho d to an

anonymous function or metho d

body ip e pe

body meth ip e meth pe

body f body f j f body f

2 1 2 1

A metho d is a function which has some hidden parameters

meth pe self next i p op en i in e

The identiers named self and next are scop ed over the b o dy of the metho d e and are named

self and super in Smalltalk The identier i is b ound to an instance variable environment

which is op ened for the scop e of the metho d b o dy It is imp ortant that this identier do es

not capture any free references to identiers in e In the presence of environment reication

and installation this is not achieved simply through a static analysis of the co de

All inx op erators are curried

e oe oe e

1 2 1 2

Sequences of declarations are translated to a single declaration

i e i e i e i i i e e e

1 1 2 2 n n 1 2 n 1 2 n

Let and where expressions are translated to function applications

e where p e pe e

1 2 1 2

let p e in e pe e

1 2 2 1

A case expression is translated to a function application

case e of a end ae

A case arm is translated to a partial function

p e pe

a a a a

2 1 2 1

An op en expression is translated to use the builtin primitives for environment manipulation

op en e in e e e

1 2 1 2 Metaclasses and reflection in smalltalk.

CLARK, Anthony

Available from the Sheffield Hallam University Research Archive (SHURA) at:

http://shura.shu.ac.uk/11924/

Copyright and re-use policy

Please visit http://shura.shu.ac.uk/11924/ and http://shura.shu.ac.uk/information.html for further details about copyright and re-use permissions.