Event Type Polymorphism Rex D. Fernando Robert Dyer Hridesh Rajan - - PowerPoint PPT Presentation

event type polymorphism
SMART_READER_LITE
LIVE PREVIEW

Event Type Polymorphism Rex D. Fernando Robert Dyer Hridesh Rajan - - PowerPoint PPT Presentation

Event Type Polymorphism Rex D. Fernando Robert Dyer Hridesh Rajan Department of Computer Science Iowa State University { fernanre,rdyer,hridesh } @iastate.edu This work was supported in part by NSF grant CCF-10-17334. Overview Motivation


slide-1
SLIDE 1

Event Type Polymorphism

Rex D. Fernando Robert Dyer Hridesh Rajan

Department of Computer Science Iowa State University {fernanre,rdyer,hridesh}@iastate.edu

This work was supported in part by NSF grant CCF-10-17334.

slide-2
SLIDE 2

Overview Motivation Language Summary

◮ Motivation: Code re-use and specialization for event-based

separation of concerns

◮ Approach: Event Type Polymorphism in Ptolemy ◮ Technical Contributions:

◮ Formal semantics for event type polymorphism ◮ Simpler semantics, when compared to earlier work Rex D. Fernando, Robert Dyer and Hridesh Rajan 1 Event Type Polymorphism

slide-3
SLIDE 3

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp

AST

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-4
SLIDE 4

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp MultVisited

AST Events

C E class C announces event E

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-5
SLIDE 5

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp MultVisited DivVisited

AST Events

C E class C announces event E

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-6
SLIDE 6

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp MultVisited DivVisited NumVisited

AST Events

C E class C announces event E

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-7
SLIDE 7

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp MultVisited DivVisited NumVisited Tracing TypeCheck Evaluation

AST Handlers Events

C E class C announces event E H E event E's announcement invokes handler H

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-8
SLIDE 8

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp MultVisited DivVisited Tracing TypeCheck Evaluation NumVisited

AST Handlers Events

C E class C announces event E H E event E's announcement invokes handler H

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-9
SLIDE 9

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

NumExp NumExp DivExp NumExp MultExp MultVisited DivVisited Tracing TypeCheck Evaluation NumVisited

AST Events Handlers

C E class C announces event E H E event E's announcement invokes handler H

Rex D. Fernando, Robert Dyer and Hridesh Rajan 2 Event Type Polymorphism

slide-10
SLIDE 10

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

MultVisited <event> + node : MultExp + left : Exp + right : Exp DivVisited <event> + node : DivExp + left : Exp + right : Exp PlusVisited <event> + node : PlusExp + left : Exp + right : Exp

Rex D. Fernando, Robert Dyer and Hridesh Rajan 3 Event Type Polymorphism

slide-11
SLIDE 11

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

MultVisited <event> + node : MultExp + left : Exp + right : Exp DivVisited <event> + node : DivExp + left : Exp + right : Exp PlusVisited <event> + node : PlusExp + left : Exp + right : Exp

Rex D. Fernando, Robert Dyer and Hridesh Rajan 3 Event Type Polymorphism

slide-12
SLIDE 12

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

MultVisited <event> + node : MultExp + left : Exp + right : Exp DivVisited <event> + node : DivExp + left : Exp + right : Exp PlusVisited <event> + node : PlusExp + left : Exp + right : Exp

Rex D. Fernando, Robert Dyer and Hridesh Rajan 3 Event Type Polymorphism

slide-13
SLIDE 13

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

class ASTTracer { void printMult(MultVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when MultVisited do printMult; void printDiv(DivVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when DivVisited do printDiv; void printPlus(PlusVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when PlusVisited do printPlus; }

Rex D. Fernando, Robert Dyer and Hridesh Rajan 4 Event Type Polymorphism

slide-14
SLIDE 14

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

class ASTTracer { void printMult(MultVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when MultVisited do printMult; void printDiv(DivVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when DivVisited do printDiv; void printPlus(PlusVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when PlusVisited do printPlus; }

Rex D. Fernando, Robert Dyer and Hridesh Rajan 5 Event Type Polymorphism

slide-15
SLIDE 15

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

◮ Can we re-use code here? ◮ What happens if a new AST type is added? ◮ What happens if an AST type is removed?

Rex D. Fernando, Robert Dyer and Hridesh Rajan 6 Event Type Polymorphism

slide-16
SLIDE 16

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

◮ Can we re-use code here?

◮ No! Passing event closures (next) as argument is illegal.

(to simplify reasoning about invoke/proceed functionality)

◮ What happens if a new AST type is added? ◮ What happens if an AST type is removed?

Rex D. Fernando, Robert Dyer and Hridesh Rajan 6 Event Type Polymorphism

slide-17
SLIDE 17

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

◮ Can we re-use code here?

◮ No! Passing event closures (next) as argument is illegal.

(to simplify reasoning about invoke/proceed functionality)

◮ What happens if a new AST type is added?

◮ Must update all handlers to support that node type!

◮ What happens if an AST type is removed?

Rex D. Fernando, Robert Dyer and Hridesh Rajan 6 Event Type Polymorphism

slide-18
SLIDE 18

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

◮ Can we re-use code here?

◮ No! Passing event closures (next) as argument is illegal.

(to simplify reasoning about invoke/proceed functionality)

◮ What happens if a new AST type is added?

◮ Must update all handlers to support that node type!

◮ What happens if an AST type is removed?

◮ Must update all handlers and remove that node type! Rex D. Fernando, Robert Dyer and Hridesh Rajan 6 Event Type Polymorphism

slide-19
SLIDE 19

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

◮ Can we re-use code here?

◮ No! Passing event closures (next) as argument is illegal.

(to simplify reasoning about invoke/proceed functionality)

◮ What happens if a new AST type is added?

◮ Must update all handlers to support that node type!

◮ What happens if an AST type is removed?

◮ Must update all handlers and remove that node type!

Polymorphism can help us here!

Rex D. Fernando, Robert Dyer and Hridesh Rajan 6 Event Type Polymorphism

slide-20
SLIDE 20

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

MultVisited <event> + node : MultExp DivVisited <event> + node : DivExp PlusVisited <event> + node : PlusExp ExpVisited <event> + node : Exp BinArithVisited <event> + node : BinArith + left : Exp + right : Exp

Rex D. Fernando, Robert Dyer and Hridesh Rajan 7 Event Type Polymorphism

slide-21
SLIDE 21

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

MultVisited <event> + node : MultExp DivVisited <event> + node : DivExp PlusVisited <event> + node : PlusExp ExpVisited <event> + node : Exp BinArithVisited <event> + node : BinArith + left : Exp + right : Exp

Rex D. Fernando, Robert Dyer and Hridesh Rajan 7 Event Type Polymorphism

slide-22
SLIDE 22

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

MultVisited <event> + node : MultExp DivVisited <event> + node : DivExp PlusVisited <event> + node : PlusExp ExpVisited <event> + node : Exp BinArithVisited <event> + node : BinArith + left : Exp + right : Exp

Rex D. Fernando, Robert Dyer and Hridesh Rajan 7 Event Type Polymorphism

slide-23
SLIDE 23

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

class ASTTracer { void printExp(ExpVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when ExpVisited do printExp; }

Rex D. Fernando, Robert Dyer and Hridesh Rajan 8 Event Type Polymorphism

slide-24
SLIDE 24

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

class ASTTracer { void printExp(ExpVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when ExpVisited do printExp; }

◮ Quantifying over entire event hierarchy by only naming super

event!

◮ No need to update when a new AST type added! ◮ No need to update when an AST type removed!

Rex D. Fernando, Robert Dyer and Hridesh Rajan 8 Event Type Polymorphism

slide-25
SLIDE 25

Overview Motivation Language Summary An Example - No Event Type Polymorphism Example Revisited - With Event Type Polymorphism

class ASTTracer { void printExp(ExpVisited next) { logVisitBegin(next.node ().getClass ()); next.invoke (); logVisitEnd(next.node ().getClass ()); } when ExpVisited do printExp; }

◮ Quantifying over entire event hierarchy by only naming super

event!

◮ No need to update when a new AST type added! ◮ No need to update when an AST type removed!

Let’s take a look at the language. . .

Rex D. Fernando, Robert Dyer and Hridesh Rajan 8 Event Type Polymorphism

slide-26
SLIDE 26

Overview Motivation Language Summary Syntax Type-checking Rules Sub-event Relation Summary

decl ::= class c extends d { field* meth* binding* } | c event p extends q { form* } where c ∈ C, a set of class names d ∈ C ∪ {Object}, a set of superclass names p ∈ P, a set of event type names q ∈ P ∪ {Event}, a set of super event type names

Rex D. Fernando, Robert Dyer and Hridesh Rajan 9 Event Type Polymorphism

slide-27
SLIDE 27

Overview Motivation Language Summary Syntax Type-checking Rules Sub-event Relation Summary

binding ::= when p do m e ::= register(e) | unregister(e) | announce p (e*) { e } | e.invoke() where m ∈ M, a set of method names

Rex D. Fernando, Robert Dyer and Hridesh Rajan 10 Event Type Polymorphism

slide-28
SLIDE 28

Overview Motivation Language Summary Syntax Type-checking Rules Sub-event Relation Summary

(check event)

isClass(c) ∀i ∈ [1..n] :: isClass(ti) p ≪: q Π ⊢ c event p extends q {t1 var1, ..., tn varn} : OK

Rex D. Fernando, Robert Dyer and Hridesh Rajan 11 Event Type Polymorphism

slide-29
SLIDE 29

Overview Motivation Language Summary Syntax Type-checking Rules Sub-event Relation Summary

(≪: Top)

isEvent(p) p ≪: Event

(≪: Reflexive)

isEvent(p) p ≪: p

(≪: Transitive)

isEvent(p) isEvent(q) isEvent(q′) p ≪: q′ q′ ≪: q p ≪: q

Rex D. Fernando, Robert Dyer and Hridesh Rajan 12 Event Type Polymorphism

slide-30
SLIDE 30

Overview Motivation Language Summary Syntax Type-checking Rules Sub-event Relation Summary

(≪: Base)

(c event p extends q {t1 var1, ..., tn varn}) ∈ CT isEvent(q) [t′

1 var′ 1, ..., t′ m var′ m] = contextsOf (q)

∀i ∈ [1..n] :: ti vari ∈ [t1 var1, ..., tn varn] ⇒ (∃j ∈ [1..m] :: t′

j vari ∈ [t′ 1 var′ 1, ..., t′ m var′ m] ⇒ ti <: t′ j)

p ≪: q contextsOf recursively computes the list of all context for an event type q, based on its supertypes

Rex D. Fernando, Robert Dyer and Hridesh Rajan 13 Event Type Polymorphism

slide-31
SLIDE 31

Overview Motivation Language Summary Syntax Type-checking Rules Sub-event Relation Summary

◮ New syntax: p extends q ◮ Typing rules use new relation: p ≪: q ◮ Both depth and width subtyping of context information

Rex D. Fernando, Robert Dyer and Hridesh Rajan 14 Event Type Polymorphism

slide-32
SLIDE 32

Overview Motivation Language Summary

Related Work

◮ Implicit Invocation + Implicit Announcement [Steimann 2010]

◮ Implicit announcement allows ambiguity ◮ Harder to reason about what event(s) announced

◮ Escala [Gasiunas 2011]

◮ Does not support width subtyping ◮ Limits the ability to specialize sub-events Rex D. Fernando, Robert Dyer and Hridesh Rajan 15 Event Type Polymorphism

slide-33
SLIDE 33

Overview Motivation Language Summary

Future Work

◮ Finish type-soundness proof (in Coq) ◮ Implement semantics in OpenJDK-based Ptolemy compiler

◮ Non-trivial to implement Rex D. Fernando, Robert Dyer and Hridesh Rajan 16 Event Type Polymorphism

slide-34
SLIDE 34

Overview Motivation Language Summary

◮ Motivation: Code re-use and specialization for event-based

separation of concerns

◮ Ability to quantify over a hierarchy of events ◮ Allows for code re-use in event definitions and handlers ◮ Better maintenance - for both adding and removing events

◮ Approach: Event Type Polymorphism in Ptolemy

◮ Event types have inheritance ◮ Allow width and depth subtyping of context ◮ Handlers also handle sub-events

◮ Technical Contributions:

◮ Formal semantics for event type polymorphism ◮ Simpler semantics, when compared to earlier work Rex D. Fernando, Robert Dyer and Hridesh Rajan 17 Event Type Polymorphism

slide-35
SLIDE 35

Questions?

http://ptolemy.cs.iastate.edu/

slide-36
SLIDE 36
slide-37
SLIDE 37

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

prog ::= decl* e decl ::= class c extends d { field* meth* binding* } | c event p extends q { form* } where c ∈ C, a set of class names d ∈ C ∪ {Object}, a set of superclass names p ∈ P, a set of event type names q ∈ P ∪ {Event}, a set of super event type names

Rex D. Fernando, Robert Dyer and Hridesh Rajan 18 Event Type Polymorphism

slide-38
SLIDE 38

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

t ::= c | thunk p field ::= c f meth ::= c m (form*) { e } form ::= t var, where var=this binding ::= when p do m where f ∈ F, a set of field names m ∈ M, a set of method names var ∈ {this} ∪ V, V is a set of variable names

Rex D. Fernando, Robert Dyer and Hridesh Rajan 19 Event Type Polymorphism

slide-39
SLIDE 39

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

ep ::= n | var | ep.f | ep != null | ep == ep | ep < ep | ! ep | ep && ep e ::= new c() | var | null | e.m(e*) | e.f | e.f = e | cast c e | form = e ; e | e ; e | if (ep) { e } else { e } | while (ep) { e } | register(e) | unregister(e) | announce p (e*) { e } | e.invoke() where n ∈ Z, the set of integers

Rex D. Fernando, Robert Dyer and Hridesh Rajan 20 Event Type Polymorphism

slide-40
SLIDE 40

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(Concrete Type Inh.)

var′

i ∈ {var1, ..., varn}

concreteType(t′

i var′ i , [t1 var1, ..., tn varn]) = t′ i var′ i

(Concrete Type Depth)

∃j ∈ [1..n] :: tj var′

i ∈ [t1 var1, ..., tn varn]

concreteType(t′

i var′ i , [t1 var1, ..., tn varn]) = tj var′ i

Rex D. Fernando, Robert Dyer and Hridesh Rajan 21 Event Type Polymorphism

slide-41
SLIDE 41

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(Top Context Vars)

contextsOf (Event) = •

(Context Vars)

(c event p extends q {t1 var1, ..., tn varn}) ∈ CT [t′

1 var′ 1, ..., t′ m var′ m] = contextsOf (q)

contextsOf (p) = [∀i ∈ [1..m] :: concreteType(t′

i var′ i , [t1 var1, ..., tn varn])]

+ [∀i ∈ [1..n] :: ti vari :: vari ∈ {var′

1, ..., var′ m})]

Rex D. Fernando, Robert Dyer and Hridesh Rajan 22 Event Type Polymorphism

slide-42
SLIDE 42

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

DivVisited <event> + node : DivExp ExpVisited <event> + node : Exp BinArithVisited <event> + node : BinArith + left : Exp + right : Exp contextsOf(ExpVisited) = [node:Exp] contextsOf(BinArithVisited) = [node:BinArith, left:Exp, right:Exp] contextsOf(DivVisited) = [node:DivExp, left:Exp, right:Exp]

Rex D. Fernando, Robert Dyer and Hridesh Rajan 23 Event Type Polymorphism

slide-43
SLIDE 43

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(Is Event)

(c event p extends q {t1 var1, ..., tn varn}) ∈ CT isEvent(p)

Rex D. Fernando, Robert Dyer and Hridesh Rajan 24 Event Type Polymorphism

slide-44
SLIDE 44

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(≪: Top)

isEvent(p) p ≪: Event

(≪: Refl.)

isEvent(p) p ≪: p

(≪: Trans.)

isEvent(p) isEvent(q) isEvent(q′) p ≪: q′ q′ ≪: q p ≪: q

Rex D. Fernando, Robert Dyer and Hridesh Rajan 25 Event Type Polymorphism

slide-45
SLIDE 45

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(≪: Base)

(c event p extends q {t1 var1, ..., tn varn}) ∈ CT isEvent(q) [t′

1 var′ 1, ..., t′ m var′ m] = contextsOf (q)

∀i ∈ [1..n] :: ti vari ∈ [t1 var1, ..., tn varn] ⇒ (∃j ∈ [1..m] :: t′

j vari ∈ [t′ 1 var′ 1, ..., t′ m var′ m] ⇒ ti <: t′ j)

p ≪: q

Rex D. Fernando, Robert Dyer and Hridesh Rajan 26 Event Type Polymorphism

slide-46
SLIDE 46

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

θ ::= “type attributes” OK “program/top-level declaration” | OK in c “method, binding” | var t “var/formal/field” | exp t “expression” τ ::= c | ⊤ | ⊥ “class type expressions” π, Π ::= {I : θI}I∈K, “type environments” where K is finite, K ⊆ (L ∪ {this} ∪ V)

Rex D. Fernando, Robert Dyer and Hridesh Rajan 27 Event Type Polymorphism

slide-47
SLIDE 47

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(check event)

isClass(c) ∀i ∈ [1..n] :: isClass(ti) p ≪: q Π ⊢ c event p extends q {t1 var1, ..., tn varn} : OK

Rex D. Fernando, Robert Dyer and Hridesh Rajan 28 Event Type Polymorphism

slide-48
SLIDE 48

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(check binding)

isClass(c′) (c event p extends q {t1 var1, ..., tn varn}) ∈ CT c′ <: c (c′ m(thunk p var){e}) = methodBody(c, m) Π ⊢ when p do m : OK in c

Rex D. Fernando, Robert Dyer and Hridesh Rajan 29 Event Type Polymorphism

slide-49
SLIDE 49

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

(announce exp type)

(c event p extends q {t1 var1, ..., tn varn}) ∈ CT ∀i ∈ [1..n] :: Π ⊢ ei : exp ti Π ⊢ e : exp c′ c′ <: c Π ⊢ announce p(e1, . . . , en) {e} : exp c

Rex D. Fernando, Robert Dyer and Hridesh Rajan 30 Event Type Polymorphism

slide-50
SLIDE 50

Appendix Full Syntax Auxilliary Methods Type-checking Rules Future Work

Implementation

◮ Static semantics are relatively simple ◮ But implementation is non-trivial

◮ Handling a supertype event requires the entire hierarchy rooted

by that event also be registered

◮ But to maintain separate compilation and type checking, event

types are only aware of their direct supertype

◮ What happens when loading new subtypes and handlers

already registered?

Rex D. Fernando, Robert Dyer and Hridesh Rajan 31 Event Type Polymorphism