1 / 22

Chai: Traits for Java-like Languages

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.

johnestelle
Download Presentation

Chai: Traits for Java-like Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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(){ … } }

  4. 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();

  5. 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();

  6. Syntax of Chai1

  7. Operational Semantics of Chai1 • Not mentioned traits explicitly • Null pointer exception • Omitting other

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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();

  14. 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(); // ??

  15. Semantics of Chai3 • New rule: mutate • Changed: new,method-call,super-call

  16. implement? Type System of Chai3 Suitable for trait replacement Subtyping rule Additional Typing rule

  17. 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

  18. trait T1 trait T2 class C trait T3 Implementation (cont.) • trait T3 uses T1, T2 • class C uses T3

  19. class C T3_impl T2_impl T1_impl Implementation (cont.) T1_interface T2_interface T1_user T2_user T3_interface T3_user

  20. Demo

  21. 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

  22. 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

More Related