Metaclasses and Re Ection in Smalltalk
Total Page:16
File Type:pdf, Size:1020Kb
Metaclasses and Reection in Smalltalk A N Clark Department of Computing University of Bradford Bradford West Yorkshire BD DP UK email ANClarkcompbradacuk tel September 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 Introduction The evaluation of a programming language 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 types 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 types 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 number n N or string s S in which case the supplied value must b e the constant An expression e is an identier number 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