22
Chai: Traits for Chai: Traits for Java-like Java-like Languages Languages by Charles Smith, Sophia by Charles Smith, Sophia Drossopoulou Drossopoulou

Chai: Traits for Java-like Languages by Charles Smith, Sophia Drossopoulou

Embed Size (px)

Citation preview

Chai: Traits for Java-like Chai: Traits for Java-like LanguagesLanguages

by Charles Smith, Sophia by Charles Smith, Sophia DrossopoulouDrossopoulou

OutlineOutline

Example of ChaiExample of Chai11, Chai, Chai22 and Chai and Chai33

Semantics and type system of ChaiSemantics and type system of Chai11

Alter of ChaiAlter of Chai11 for Chai for Chai22

Semantics and type system of ChaiSemantics and type system of Chai33

Implementation & DemoImplementation & Demo Problems in the paper and the Problems in the paper and the

implementationimplementation

Example of ChaiExample of Chai11class Circle{ int radius; int getRadius(){ … }}

trait TEmptyCircle{ requires{ void drawPoint(int x, int y); int getRadius(); } void draw(){ … }}

Trait TFilledCircle{ require{ void drawPoint(int x, int y); int getRadius(); } void draw(){ … }}

trait TScreenShape{ void drawPoint(int x, int y){ … }}

trait TPrintedShape{ void drawPoint(int , int y){ … }}

class ScreenEmptyCircle extends Circle uses TEmptyCircle, TScreenShape{}

Class PrintedFilledCircle extends Circle uses TFilledCircle, TPrintedShape{}

Example of ChaiExample of Chai22

Class ScreenShapeStack{ void push(TScreenShape shape){ … } TScreenShape pop(){ … }}

ScreenShapSTack stack = new ScreenShapeStack();stack.push(new ScreenEmptyCircle());stack.push(new ScreenFilledCircle());TScreenShape shape = stack.pop();

Example of ChaiExample of Chai33

class CircleShape extends Circle uses TEmptyCircle, TScreenShape{}

CircleShape circle = new CircleShape();circle.draw();

Circle<TEmptyCircle -> TFilledCircle>;circle.draw();

Circle<TScreenShape -> TPrintedShape>;circle.draw();

Syntax of ChaiSyntax of Chai11

Operational Semantics of ChaiOperational Semantics of Chai11

Not mentioned traits explicitlyNot mentioned traits explicitly Null pointer exceptionNull pointer exception Omitting otherOmitting other

Lookup Rules (Non-recursive)Lookup Rules (Non-recursive) PPsupsup((cl): direct super class): direct super class PPfldfld((cl,, f): type of field ): type of field f in in cl PPmthmth((cl, , m), P), Pmthmth((tr, , m): methods with identifier ): methods with identifier m

in in cl or or tr PPuseuse((cl), P), Puseuse((tr): traits directly used by ): traits directly used by cl or or tr PPexclexcl((tr): pair of trait and identifier excluded from ): pair of trait and identifier excluded from

tr PPaliasalias((tr, , m): pair of trait and identifier aliased as ): pair of trait and identifier aliased as m

in in tr PPreqreq((tr): method signatures required in ): method signatures required in tr PPreq_supreq_sup((tr): method signatures required for ): method signatures required for

superclass in superclass in tr

Lookup Rules (Recursive)Lookup Rules (Recursive)

FF((P, , cl, , f): type of ): type of cl.f FFss((P, , cl): { ): { f | | FF((P, , cl, , f) ) ≠≠⊥ }⊥ }

MM((P,,tr,,m):P):Pmthmth((tr,,m),),MsAliasMsAlias,,MsUsedMsUsed

MM((P, , cl, , m): P): Pmthmth((cl, , m), ), MsUsedMsUsed, , MsSuperMsSuper

MSigMSig11((P, , cl | | tr, , m): signatures): signatures MMorigorig((P, , cl, , m): origin of ): origin of cl.m

Type SystemType System

Classes and only classes are typesClasses and only classes are types Subtyping: inheritanceSubtyping: inheritance Well-formed: Well-formed: (*)(*)

• No field overridingNo field overriding• No method conflictNo method conflict• Method invariantMethod invariant

Methods checkedMethods checkedin all used classesin all used classes

Not require acyclicNot require acyclic

Alter of ChaiAlter of Chai11 for Chai for Chai22

Traits play the role of interfacesTraits play the role of interfaces Type check traits in isolationType check traits in isolation Type check calls to required methodsType check calls to required methods

typetype ::= cl | tr ::= cl | tr Operational semantics unchangedOperational semantics unchanged

Type SystemType System

Complete class: fulfilling all the Complete class: fulfilling all the requirements of used traits requirements of used traits (*)(*)

Traits are typesTraits are types Use-clause forms subtypingUse-clause forms subtyping P├├22 t’ ≤ ≤ t implies that implies that MSigMSig22((P, , t, , m) is a ) is a

subset of subset of MSigMSig22((P, , t’, , m)) Well formed: no field overriding (class Well formed: no field overriding (class

only), invariant, methods and fields (class only), invariant, methods and fields (class only) defined directly are well-typedonly) defined directly are well-typed

ChaiChai33 Dynamic trait substitutionDynamic trait substitution Use-clause becomes a placeholderUse-clause becomes a placeholder Window w; w.display();Window w; w.display();

w<TOpened -> TIconified>; w<TOpened -> TIconified>; w.display();w.display();w<TOpened -> TOpened>;w<TOpened -> TOpened>;// wrong:// wrong:// w<TIconified -> TOpened>// w<TIconified -> TOpened>w.display();w.display();

Syntax and Method Calls Syntax and Method Calls

expexp ::= ::= expexp< tr -> tr>< tr -> tr> Only methods provided by the Only methods provided by the

original trait are replaced by the original trait are replaced by the ones provided by the replacing traitones provided by the replacing trait

trait TrtB{ requires{ int m1(); } int m2(){ this.m1(); }}trait TrtB2{ int m2(){ this.m1(); } int m1(){ 5 }}

trait TrtA{ int m1(){ 3 } }class C uses TrtA, TrtB{}

C c = new c;c.m2();c<TrtB -> TrtB2>;c.m2(); // ??

Semantics of ChaiSemantics of Chai33

New rule: mutateNew rule: mutate Changed: new,method-call,super-callChanged: new,method-call,super-call

Type System of ChaiType System of Chai33Suitable for trait replacement

Subtyping rule

Additional Typing rule implement?

ImplementationImplementation

Translator to JavaTranslator to Java Support ChaiSupport Chai11 (But not completed) (But not completed) Every class map to a class in JavaEvery class map to a class in Java Every trait map toEvery trait map to

• A trait interfaceA trait interface• A trait-user interfaceA trait-user interface• A trait implementation classA trait implementation class

Using proxy object to make ChaiUsing proxy object to make Chai33 possiblepossible

Implementation (cont.)Implementation (cont.)

trait T3 uses T1, T2trait T3 uses T1, T2 class C uses T3class C uses T3

class C trait T3

trait T2trait T1

Implementation (cont.)Implementation (cont.)

class C

T3_impl

T2_implT1_impl T3_interface

T2_user

T1_interface T2_interface

T1_user

T3_user

DemoDemo

Problems in The PaperProblems in The Paper

Well-typeness of classes and traits Well-typeness of classes and traits does not check superclasses and does not check superclasses and used traitsused traits

Completeness of classes in ChaiCompleteness of classes in Chai22 does not check super-requirementsdoes not check super-requirements

Replaceability of traits in Chai3Replaceability of traits in Chai3

Problems in The ImplementationProblems in The Implementation

Requirement and super-requirement Requirement and super-requirement in deeply used traits lack stubsin deeply used traits lack stubs

Implement only ChaiImplement only Chai11 but type check but type check in Chaiin Chai22 way way

Not knowing if it work for ChaiNot knowing if it work for Chai33

Incompatible with JRE 1.5 (JRE’s fault Incompatible with JRE 1.5 (JRE’s fault this time)this time)

Wrong function dispatching ruleWrong function dispatching rule Wrong “assignable” checkWrong “assignable” check