Download presentation
Presentation is loading. Please wait.
Published byAldous Martin Modified over 9 years ago
1
Chai: Traits for Java-like Languages by Charles Smith, Sophia Drossopoulou
2
Outline Example of Chai 1, Chai 2 and Chai 3 Example of Chai 1, Chai 2 and Chai 3 Semantics and type system of Chai 1 Semantics and type system of Chai 1 Alter of Chai 1 for Chai 2 Alter of Chai 1 for Chai 2 Semantics and type system of Chai 3 Semantics and type system of Chai 3 Implementation & Demo Implementation & Demo Problems in the paper and the implementation Problems in the paper and the implementation
3
Example of Chai 1 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(){ … } } 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{}
4
Example of Chai 2 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 Chai 3 class CircleShape extends Circle uses TEmptyCircle, TScreenShape{} CircleShape circle = new CircleShape(); circle.draw(); Circle TFilledCircle>; circle.draw(); Circle TPrintedShape>; circle.draw();
6
Syntax of Chai 1
7
Operational Semantics of Chai 1 Not mentioned traits explicitly Not mentioned traits explicitly Null pointer exception Null pointer exception Omitting other Omitting other
8
Lookup Rules (Non-recursive) P sup (): direct super class P sup (cl): direct super class P fld (,): type of field in P fld (cl, f): type of field f in cl P mth (, ), P mth (, ): methods with identifier in or P mth (cl, m), P mth (tr, m): methods with identifier m in cl or tr P use (), P use (): traits directly used by or P use (cl), P use (tr): traits directly used by cl or tr P excl (): pair of trait and identifier excluded from P excl (tr): pair of trait and identifier excluded from tr P alias (, ): pair of trait and identifier aliased as in P alias (tr, m): pair of trait and identifier aliased as m in tr P req (): method signatures required in P req (tr): method signatures required in tr P req_sup (): method signatures required for superclass in P req_sup (tr): method signatures required for superclass in tr
9
Lookup Rules (Recursive) F (,, ): type of F (P, cl, f): type of cl.f F s (, ): { | F (,, ) ≠ ⊥ } F s (P, cl): { f | F (P, cl, f) ≠ ⊥ } M (,,):P mth (,),MsAlias,MsUsed M (P,tr,m):P mth (tr,m),MsAlias,MsUsed M (,, ): P mth (, ), MsUsed, MsSuper M (P, cl, m): P mth (cl, m), MsUsed, MsSuper MSig 1 (, |, ): signatures MSig 1 (P, cl | tr, m): signatures M orig (,, ): origin of M orig (P, cl, m): origin of cl.m
10
Type System Classes and only classes are types Classes and only classes are types Subtyping: inheritance Subtyping: inheritance Well-formed: (*) Well-formed: (*) No field overridingNo field overriding No method conflictNo method conflict Method invariantMethod invariant Methods checked in all used classes Methods checked in all used classes Not require acyclic Not require acyclic
11
Alter of Chai 1 for Chai 2 Traits play the role of interfaces Traits play the role of interfaces Type check traits in isolation Type check traits in isolation Type check calls to required methods Type check calls to required methods type ::= cl | tr type ::= cl | tr Operational semantics unchanged Operational semantics unchanged
12
Type System Complete class: fulfilling all the requirements of used traits (*) Complete class: fulfilling all the requirements of used traits (*) Traits are types Traits are types Use-clause forms subtyping Use-clause forms subtyping ├ 2 ≤ implies that MSig 2 (,, ) is a subset of MSig 2 (,, ) P ├ 2 t ’ ≤ t implies that MSig 2 (P, t, m) is a subset of MSig 2 (P, t’, m) Well formed: no field overriding (class only), invariant, methods and fields (class only) defined directly are well-typed Well formed: no field overriding (class only), invariant, methods and fields (class only) defined directly are well-typed
13
Chai 3 Dynamic trait substitution Dynamic trait substitution Use-clause becomes a placeholder Use-clause becomes a placeholder Window w; w.display(); w TIconified>; w.display(); w TOpened>; // wrong: // w TOpened> w.display(); Window w; w.display(); w TIconified>; w.display(); w TOpened>; // wrong: // w TOpened> w.display();
14
Syntax and Method Calls exp ::= exp tr> exp ::= exp tr> Only methods provided by the original trait are replaced by the ones provided by the replacing trait 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 TrtB2>; c.m2(); // ??
15
Semantics of Chai 3 New rule: mutate New rule: mutate Changed: new,method-call,super-call Changed: new,method-call,super-call
16
Type System of Chai 3 Suitable for trait replacement Subtyping rule Additional Typing rule implement?
17
Implementation Translator to Java Translator to Java Support Chai 1 (But not completed) Support Chai 1 (But not completed) Every class map to a class in Java Every class map to a class in Java Every trait map to Every 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 Chai 3 possible Using proxy object to make Chai 3 possible
18
Implementation (cont.) trait T3 uses T1, T2 trait T3 uses T1, T2 class C uses T3 class C uses T3 class Ctrait T3 trait T2trait T1
19
Implementation (cont.) class CT3_implT2_implT1_impl T3_interface T2_user T1_interfaceT2_interface T1_user T3_user
20
Demo
21
Problems in The Paper Well-typeness of classes and traits does not check superclasses and used traits Well-typeness of classes and traits does not check superclasses and used traits Completeness of classes in Chai 2 does not check super-requirements Completeness of classes in Chai 2 does not check super-requirements Replaceability of traits in Chai3 Replaceability of traits in Chai3
22
Problems in The Implementation Requirement and super-requirement in deeply used traits lack stubs Requirement and super-requirement in deeply used traits lack stubs Implement only Chai 1 but type check in Chai 2 way Implement only Chai 1 but type check in Chai 2 way Not knowing if it work for Chai 3 Not knowing if it work for Chai 3 Incompatible with JRE 1.5 (JRE ’ s fault this time) Incompatible with JRE 1.5 (JRE ’ s fault this time) Wrong function dispatching rule Wrong function dispatching rule Wrong “ assignable ” check Wrong “ assignable ” check
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.