foo a minimal modern oo calculus
play

Foo A Minimal Modern OO Calculus Prodromos Gerakios George - PowerPoint PPT Presentation

Motivation Semantics Formal properties Future directions Foo A Minimal Modern OO Calculus Prodromos Gerakios George Fourtounis Yannis Smaragdakis Department of Informatics University of Athens { pgerakios,gfour,smaragd } @di.uoa.gr 1 / 24


  1. Motivation Semantics Formal properties Future directions Foo A Minimal Modern OO Calculus Prodromos Gerakios George Fourtounis Yannis Smaragdakis Department of Informatics University of Athens { pgerakios,gfour,smaragd } @di.uoa.gr 1 / 24

  2. Motivation Semantics Formal properties Future directions What A core OO calculus with nominal and (width) structural subtyping 2 / 24

  3. Motivation Semantics Formal properties Future directions Overview Motivation Semantics Formal properties Future directions 3 / 24

  4. Motivation Semantics Formal properties Future directions Why • Well-known OO calculi (e.g., FJ) are non-minimal or only express one kind of subtyping • We need a simple core calculus with flexibility - (painfully) minimal - study both nominal and structural subtyping • Foo motivated by our own language modeling work - morphing [Huang and Smaragdakis, 2011, Gerakios et al., 2013] 4 / 24

  5. Motivation Semantics Formal properties Future directions Fundamentals • Basic idea: hybrid types unify nominal and structural subtyping • Very compact, tiny syntax, 15 rules for everything, non-essential features removed • Mimics (modulo minor syntactic conventions) a tiny subset of Scala • our examples are executable code 5 / 24

  6. Motivation Semantics Formal properties Future directions Example: Extending a class (new Employee { def extra() = println("add-on") } ).extra(); 6 / 24

  7. Motivation Semantics Formal properties Future directions Example: Inheritance Overriding a method: class EnhancedEmployee extends Employee { def extra() = println("more") } 7 / 24

  8. Motivation Semantics Formal properties Future directions Example: Methods and formals • Methods only accept one formal argument (plus the implicit this ) • But anonymous classes can see formals from their environment class C { def f(x : Integer) = new Object { def g(y : Integer) = x + y } } 8 / 24

  9. Motivation Semantics Formal properties Future directions Example: Fields • Fields are represented by dummy-argument methods that return the field value • To set a field, we override its method class C { def field(d : Object) = 1 } ... new C { def field(d : Object) = 42 } • Informally, we use obj.field instead of obj.field(new Object { } ) 9 / 24

  10. Motivation Semantics Formal properties Future directions Example: Emulating multiple arguments class Add { def apply(x : Integer, y : Integer) = x + y } (new Add).apply(5, 10) becomes (Scala): class Add { def x(): Integer def apply(y : Integer) = x() + y } (new Add { def x() = 5 } ).apply(10) 10 / 24

  11. Motivation Semantics Formal properties Future directions Example: Structural subtyping def fun(e : { def extra() } ) = e.extra ... fun(new Object { def extra() = println("subtyping") } ) 11 / 24

  12. Motivation Semantics Formal properties Future directions Syntax Member type ::= m : N − → N Ψ Hybrid Type ::= C & Ψ N Member ::= m ( x ) e M Program Value ::= new N { M } | x v Expression e ::= v | v . m(e) Top-level classes ::= class C = N { M } P 12 / 24

  13. Motivation Semantics Formal properties Future directions Hybrid types Purely structural type: ⊢ H Ψ ( W-O) ⊢ H Object & Ψ Class extended by (optional) structural part: ⊢ H ( C & Ψ ) ⇒ Ψ ′ ; . . . ( W-C) ⊢ H C & Ψ Method signatures (elements of Ψ ): ⊢ H N , N ′ ( W-M) → N ′ ⊢ H m : N − 13 / 24

  14. Motivation Semantics Formal properties Future directions Reduction Formal argument can be reduced: → P e ′ e − ( R-C) → P new N { M } . m(e ′ ) new N { M } . m(e) − Formal argument is in normal form, call method: v ′ = new . . . mbody ( P , N { M } , m ) = m ( x ) e ( R-I) new N { M } . m(v ′ ) − → P e [( new N { M } ) / this , v ′ / x ] 14 / 24

  15. Motivation Semantics Formal properties Future directions Subtyping Based on the hierarchy computation: Ψ ′ ⊆ Ψ ⊢ H N ′ ⇒ Ψ ′ ; N ′ ⊢ H N ⇒ Ψ ; N , N ′ ( S-N) ⊢ H N < : N ′ Width subtyping ( ⊆ relation) 15 / 24

  16. Motivation Semantics Formal properties Future directions Formal properties of Foo • Correctness proof, being formalized in Coq • No subsumption axiom • Substitution lemma is special 16 / 24

  17. Motivation Semantics Formal properties Future directions Proof Subject reduction, with narrowing. → P e ’ and e : N , then ∃ N ′ , e ’ : N ’ ∧ N ’ < : N If e − Foo does not admit the standard subject reduction theorem, like DOT [Amin et al., 2012] Progress. If e : N , then ∃ M , e = new N M or ∃ e ′ , e − → P e ’ 17 / 24

  18. Motivation Semantics Formal properties Future directions No subsumption • Subsumption property: if Γ ⊢ H x : N and N < : N ′ , then Γ ⊢ H x : N ′ • Usually added as an axiom in the type system • In Foo , expressions have a single type • Substitutivity-of-subtypes-for-supertypes still captured by rules: “you can use a subtype for formal arguments” T-I T-M “you can use a subtype for method bodies” 18 / 24

  19. Motivation Semantics Formal properties Future directions Substitution lemma (I) • Without subsumption, the familiar substitution lemma plays different role in the type safety proof • Example, identity method, with N ′ < : N : o = new Object { id(N o) : N = o } t = new N t’ = new N’ − → P t o.id(t) : N → P t’ : N ′ o.id(t’) − • We cannot say that t’ : N , so the substitution lemma does not hold for formals! • A lemma still holds for substitution of this 19 / 24

  20. Motivation Semantics Formal properties Future directions Substitution lemma (II) • Intuitively, lack of a substitution lemma for formals is not a problem • Values are passed/returned by rules T-I / T-M , which accept subtypes • Formally, our proof just uses the fact above directly, instead of going through a separate substitution lemma for formals 20 / 24

  21. Motivation Semantics Formal properties Future directions Other core calculi • DOT - combines nominal and structural subtypes - more features (path-dependent types), bigger calculus • Unity [Malayeri and Aldrich, 2008] - structural subtyping with branding - similarity: internal vs. external methods - intersection types, depth subtyping, abstract - bigger calculus, e.g. 13 subtyping rules • Tinygrace - almost as minimal as Foo , extra feature (casts) - structural subtyping, supports nominal subtyping if further extended with branding [Jones et al. 2015] 21 / 24

  22. Motivation Semantics Formal properties Future directions Future directions and applications • We already have an extension of Foo with generics, to match FJ • To be used in formalizing universal morphing (see our jUCM paper at MASPEGHI) • Finish Coq proof (the usual culprit: binding representation) 22 / 24

  23. Motivation Semantics Formal properties Future directions Thank You! 23 / 24

  24. Motivation Semantics Formal properties Future directions References N. Amin, A. Moors, and M. Odersky. Dependent Object Types: Towards a foundation for Scala’s type system. FOOL ’12 . P. Gerakios, A. Biboudis, and Y. Smaragdakis. Forsaking inheritance: Supercharged delegation in DelphJ. OOPSLA ’13 . S. S. Huang and Y. Smaragdakis. Morphing: Structurally shaping a class by reflecting on others. ACM Transactions on Programming Languages and Systems , 33(2):1–44, 2011. T. Jones, M. Homer, and J. Noble. Brand Objects for Nominal Typing. ECOOP ’15 . D. Malayeri and J. Aldrich. Integrating Nominal and Structural Subtyping. ECOOP ’08 . 24 / 24

  25. Extra slides Expression and method typing Variables, new objects, method invocations: x �→ N ∈ Γ ⊢ H N ( T-V) Γ ⊢ H x : N N = C & Ψ ⊢ H N ( Γ \ this ) , this �→ N ⊢ H Ψ M ( T-N) Γ ⊢ H new N { M } : N Γ ⊢ H v 1 : N 1 Γ ⊢ H e 2 : N 2 ⊢ H N 2 < : N 3 ⊢ H N 1 ⇒ Ψ ′ ; . . . Ψ ′ ( m ) = N 3 − → N 4 ( T-I) Γ ⊢ H v 1 . m(e 2 ) : N 4 Method definitions: ⊢ H N ′′ < : N ′ Γ , x �→ N ⊢ H e : N ′′ ( T-M) → N ′ m ( x ) e Γ ⊢ H m : N − 25 / 24

  26. Extra slides Hierarchy computation • Given a hybrid type N , extracts the pair Ψ;N : Ψ : signatures for all methods that can be called on N N : the “path” of parent classes towards Object • Purely structural case: ⊢ H Ψ ( H-O) ⊢ H Object & Ψ ⇒ Ψ ; [ Object & Ψ ] • Involving classes: ⊢ H N ⇒ Ψ ′ ; N H ( C ) = N ⊢ H Ψ for all m ∈ dom( Ψ ) ∩ dom( Ψ ′ ) Ψ ( m ) = Ψ ′ ( m ) ( H-C) ⊢ H C & Ψ ⇒ Ψ ∪ Ψ ′ ; C & Ψ , N 26 / 24

  27. Extra slides Method lookup Look up method in structural part of object: m ∈ dom( M ) ( M-O) mbody ( P , N { M } , m ) = M ( m ) Look up method in the parent class: ′ } ) , m ) = M mbody ( P , ( N { M ′ } m / ∈ dom( M ) P ( C ) = N { M ( M-C) mbody ( P , ( C & Ψ ) { M } , m ) = M 27 / 24

  28. Extra slides Class definitions [ this �→ C & • ] ⊢ H Ψ M N = C ′ & Ψ H ( C ) = N ⊢ H C & • ( T-C) ⊢ H class C = N { M } 28 / 24

Recommend


More recommend