220 likes | 241 Views
Explore the semantics, type system, and implementation of Chai traits for Java-like languages as discussed by Charles Smith and Sophia Drossopoulou. Discover example implementations and issues encountered during the development process.
E N D
Chai: Traits for Java-like Languages by Charles Smith, Sophia Drossopoulou
Outline • Example of Chai1, Chai2 and Chai3 • Semantics and type system of Chai1 • Alter of Chai1 for Chai2 • Semantics and type system of Chai3 • Implementation & Demo • Problems in the paper and the implementation
Example of Chai1 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{} class 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(){ … } }
Example of Chai2 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 Chai3 class CircleShape extends Circle uses TEmptyCircle, TScreenShape{} CircleShape circle = new CircleShape(); circle.draw(); Circle<TEmptyCircle -> TFilledCircle>; circle.draw(); Circle<TScreenShape -> TPrintedShape>; circle.draw();
Operational Semantics of Chai1 • Not mentioned traits explicitly • Null pointer exception • Omitting other
Lookup Rules (Non-recursive) • Psup(cl): direct super class • Pfld(cl, f): type of field f in cl • Pmth(cl, m), Pmth(tr, m): methods with identifier m in cl or tr • Puse(cl), Puse(tr): traits directly used by cl or tr • Pexcl(tr): pair of trait and identifier excluded from tr • Palias(tr, m): pair of trait and identifier aliased as m in tr • Preq(tr): method signatures required in tr • Preq_sup(tr): method signatures required for superclass in tr
Lookup Rules (Recursive) • F(P, cl, f): type of cl.f • Fs(P, cl): { f | F(P, cl, f) ≠⊥ } • M(P,tr,m):Pmth(tr,m),MsAlias,MsUsed • M(P, cl, m): Pmth(cl, m), MsUsed, MsSuper • MSig1(P, cl | tr, m): signatures • Morig(P, cl, m): origin of cl.m
Type System • Classes and only classes are types • Subtyping: inheritance • Well-formed: (*) • No field overriding • No method conflict • Method invariant • Methods checkedin all used classes • Not require acyclic
Alter of Chai1 for Chai2 • Traits play the role of interfaces • Type check traits in isolation • Type check calls to required methods • type ::= cl | tr • Operational semantics unchanged
Type System • Complete class: fulfilling all the requirements of used traits (*) • Traits are types • Use-clause forms subtyping • P├2t’ ≤ t implies that MSig2(P, t, m) is a subset of MSig2(P, t’, m) • Well formed: no field overriding (class only), invariant, methods and fields (class only) defined directly are well-typed
Chai3 • Dynamic trait substitution • Use-clause becomes a placeholder • Window w; w.display();w<TOpened -> TIconified>; w.display();w<TOpened -> TOpened>;// wrong:// w<TIconified -> TOpened>w.display();
Syntax and Method Calls • exp ::= exp< tr -> tr> • Only methods provided by the original trait are replaced by the ones 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 Chai3 • New rule: mutate • Changed: new,method-call,super-call
implement? Type System of Chai3 Suitable for trait replacement Subtyping rule Additional Typing rule
Implementation • Translator to Java • Support Chai1 (But not completed) • Every class map to a class in Java • Every trait map to • A trait interface • A trait-user interface • A trait implementation class • Using proxy object to make Chai3 possible
trait T1 trait T2 class C trait T3 Implementation (cont.) • trait T3 uses T1, T2 • class C uses T3
class C T3_impl T2_impl T1_impl Implementation (cont.) T1_interface T2_interface T1_user T2_user T3_interface T3_user
Problems in The Paper • Well-typeness of classes and traits does not check superclasses and used traits • Completeness of classes in Chai2 does not check super-requirements • Replaceability of traits in Chai3
Problems in The Implementation • Requirement and super-requirement in deeply used traits lack stubs • Implement only Chai1 but type check in Chai2 way • Not knowing if it work for Chai3 • Incompatible with JRE 1.5 (JRE’s fault this time) • Wrong function dispatching rule • Wrong “assignable” check