SLIDE 1 Backstage Java
Making a Difference in Metaprogramming Zachary Palmer and Scott F. Smith
The Johns Hopkins University
October 27, 2011
SLIDE 2 Difference-Based Metaprogramming
- Introduction to Metaprogramming
SLIDE 3 Difference-Based Metaprogramming
- Introduction to Metaprogramming
- Compile-Time Metaprogramming in Java
SLIDE 4 Difference-Based Metaprogramming
- Introduction to Metaprogramming
- Compile-Time Metaprogramming in Java
- Traditional Metaprogramming Model
SLIDE 5 Difference-Based Metaprogramming
- Introduction to Metaprogramming
- Compile-Time Metaprogramming in Java
- Traditional Metaprogramming Model
- Difference-Based Metaprogramming Model
SLIDE 6
What is metaprogramming?
SLIDE 7 Metaprogramming
- Programs input data and output data.
Input ρ Output
SLIDE 8 Metaprogramming
- Programs input data and output data.
Input ρ Output
- Metaprograms input programs (or program
fragments) and output the same.
ρ φ ρ′
SLIDE 9 Examples of Metaprogramming
- C Macros
- C++ Templates
- LISP Macros
- Template Haskell
- MetaOCaml
- Stratego
- Groovy
- etc. etc.
SLIDE 10 Classifying Metaprogramming
- When is it run?
- Compile-time (static)
- Runtime (dynamic)
- How are programs represented?
- Textually (strings)
- Lexically (tokens)
- Structurally (ASTs)
- Semantically (various structures)
- Which language is used to metaprogram?
- Same language (homogenous)
- Different language (heterogeneous)
from Accomplishments and Research Challenges in Metaprogramming (Tim Sheard)
SLIDE 11 Classifying Metaprogramming
- When is it run?
- Compile-time (static)
- Runtime (dynamic)
- How are programs represented?
- Textually (strings)
- Lexically (tokens)
- Structurally (ASTs)
- Semantically (various structures)
- Which language is used to metaprogram?
- Same language (homogenous)
- Different language (heterogeneous)
from Accomplishments and Research Challenges in Metaprogramming (Tim Sheard)
SLIDE 12 Template Haskell Example
$( ❧❡t mkExp n v = ✐❢ n == 0 t❤❡♥ ❬⑤ ✶ ⑤❪ ❡❧s❡ ❬⑤ ✩✭✈✮ ✯ ✩✭♠❦❊①♣ ✭♥✲✶✮ ✈✮ ⑤❪ ✐♥ ❧❡t f n = ❧❡t funNm = mkName ("exp" ++ (s❤♦✇ n)) ✐♥ ❧❡t params = [varP (mkName "x")] ✐♥ funD funNm $ [clause params (normalB $ mkExp n (varE $ mkName "x")) []] ✐♥ ♠❛♣▼ f [2..50] )
SLIDE 13 Template Haskell Example
exp2 x = x * x exp3 x = x * x * x exp4 x = x * x * x * x exp5 x = x * x * x * x * x exp6 x = x * x * x * x * x * x . . . . . . . . . exp50 x =
50
SLIDE 14 Template Haskell
- Programmatic code generation
SLIDE 15 Template Haskell
- Programmatic code generation
- Literal syntax for AST construction
SLIDE 16 Template Haskell
- Programmatic code generation
- Literal syntax for AST construction
- Functional programming style
SLIDE 17 Template Haskell
- Programmatic code generation
- Literal syntax for AST construction
- Functional programming style
- Very limited ability to inspect environment
SLIDE 18
Why not Template Java?
SLIDE 19 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ♣r✐✈❛t❡ ✐♥t x; ♣✉❜❧✐❝ ✐♥t getX() { r❡t✉r♥ t❤✐s.x; } ♣✉❜❧✐❝ ✈♦✐❞ setX(✐♥t x) { t❤✐s.x = x; } ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ ✐♥t getY() { r❡t✉r♥ t❤✐s.y; } ♣✉❜❧✐❝ ✈♦✐❞ setY(✐♥t y) { t❤✐s.y = y; } ♣✉❜❧✐❝ Location(✐♥t x, ✐♥t y) { t❤✐s.x = x; t❤✐s.y = y; } ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 20 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ♣r✐✈❛t❡ ✐♥t ①❀ ♣✉❜❧✐❝ ✐♥t ❣❡t❳✭✮ ④ r❡t✉r♥ t❤✐s✳①❀ ⑥ ♣✉❜❧✐❝ ✈♦✐❞ s❡t❳✭✐♥t ①✮ ④ t❤✐s✳① ❂ ①❀ ⑥ ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ ✐♥t getY() { r❡t✉r♥ t❤✐s.y; } ♣✉❜❧✐❝ ✈♦✐❞ setY(✐♥t y) { t❤✐s.y = y; } ♣✉❜❧✐❝ Location(✐♥t x, ✐♥t y) { t❤✐s.x = x; t❤✐s.y = y; } ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 21 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ ✐♥t getY() { r❡t✉r♥ t❤✐s.y; } ♣✉❜❧✐❝ ✈♦✐❞ setY(✐♥t y) { t❤✐s.y = y; } ♣✉❜❧✐❝ Location(✐♥t x, ✐♥t y) { t❤✐s.x = x; t❤✐s.y = y; } ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 22 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ♣r✐✈❛t❡ ✐♥t ②❀ ♣✉❜❧✐❝ ✐♥t ❣❡t❨✭✮ ④ r❡t✉r♥ t❤✐s✳②❀ ⑥ ♣✉❜❧✐❝ ✈♦✐❞ s❡t❨✭✐♥t ②✮ ④ t❤✐s✳② ❂ ②❀ ⑥ ♣✉❜❧✐❝ Location(✐♥t x, ✐♥t y) { t❤✐s.x = x; t❤✐s.y = y; } ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 23 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ♣✉❜❧✐❝ Location(✐♥t x, ✐♥t y) { t❤✐s.x = x; t❤✐s.y = y; } ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 24 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ♣✉❜❧✐❝ ▲♦❝❛t✐♦♥✭✐♥t ①✱ ✐♥t ②✮ ④ t❤✐s✳① ❂ ①❀ t❤✐s✳② ❂ ②❀ ⑥ ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 25 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ✩✭ ♠❛❦❡Pr♦♣❡rt②❈♦♥str✉❝t♦r✭ ❬⑤✐♥t ①⑤❪✱ ❬⑤✐♥t ②⑤❪✮ ✮ ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 26 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ✩✭ ♠❛❦❡Pr♦♣❡rt②❈♦♥str✉❝t♦r✭ ❬⑤✐♥t ①⑤❪✱ ❬⑤✐♥t ②⑤❪✮ ✮ ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 27 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ✩✭ ♠❛❦❡Pr♦♣❡rt②❈♦♥str✉❝t♦r✭ ❬⑤✐♥t ①⑤❪✱ ❬⑤✐♥t ②⑤❪✮ ✮ ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
- Metaprograms can’t react to surrounding code
SLIDE 28 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ✩✭ ♠❛❦❡Pr♦♣❡rt②❈♦♥str✉❝t♦r✭ ❬⑤✐♥t ①⑤❪✱ ❬⑤✐♥t ②⑤❪✮ ✮ ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
- Metaprograms can’t react to surrounding code
- Metaprogrammers compensate by duplicating
information
SLIDE 29 Template Java?
♣✉❜❧✐❝ ❝❧❛ss Location { ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ①⑤❪ ✮ ✩✭ ♣r♦♣❡rt②✭ ❬⑤♣r✐✈❛t❡ ✐♥t ②⑤❪ ✮ ✩✭ ♠❛❦❡Pr♦♣❡rt②❈♦♥str✉❝t♦r✭ ❬⑤✐♥t ①⑤❪✱ ❬⑤✐♥t ②⑤❪✮ ✮ ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
- Metaprograms can’t react to surrounding code
- Metaprogrammers compensate by duplicating
information
- Functional metaprogramming in a declarative
- bject-oriented language
SLIDE 30 What Do We Want?
- Object-oriented, declarative metaprogramming
style
SLIDE 31 What Do We Want?
- Object-oriented, declarative metaprogramming
style
- Awareness of surrounding code
SLIDE 32 What Do We Want?
- Object-oriented, declarative metaprogramming
style
- Awareness of surrounding code
- Modular, independent metaprograms
SLIDE 33
Backstage Java
How about some of this? ❅❅●❡♥❡r❛t❡❈♦♥str✉❝t♦r❋r♦♠Pr♦♣❡rt✐❡s ♣✉❜❧✐❝ ❝❧❛ss Location { ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x; ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 34
Backstage Java
How about some of this? ❅❅●❡♥❡r❛t❡❈♦♥str✉❝t♦r❋r♦♠Pr♦♣❡rt✐❡s ♣✉❜❧✐❝ ❝❧❛ss Location { ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x; ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } } Harder than it looks...
SLIDE 35
Traditional Metaprogramming
SLIDE 36 Traditional Metaprogramming
- Metaprograms are a series of program
transformations
SLIDE 37 Traditional Metaprogramming
- Metaprograms are a series of program
transformations
- Each available transformation occurs exactly
- nce
SLIDE 38 Traditional Metaprogramming
- Metaprograms are a series of program
transformations
- Each available transformation occurs exactly
- nce
- True even for embedded syntax
SLIDE 39
Embedded Metaprogram Semantics
SLIDE 40
Embedded Metaprogram Semantics
SLIDE 41
Embedded Metaprogram Semantics
φ1 φ2
SLIDE 42 Embedded Metaprogram Semantics
φ1
SLIDE 43 Embedded Metaprogram Semantics
φ2
SLIDE 44 Embedded Metaprogram Semantics
φ2
SLIDE 45
Ambiguity in Metaprogramming
Assuming three transformations φ1, φ2, φ3... ρ ? ? ? ? ? ?
SLIDE 46
Ambiguity in Metaprogramming
Assuming three transformations φ1, φ2, φ3... ρ ? ? ? ? ? ? OpenJava, Groovy
SLIDE 47
Total Ordering Solution
Declaring φ2 before φ1, which is before φ3. ρ ρ′
SLIDE 48
Total Ordering Solution
Declaring φ2 before φ1, which is before φ3. ρ ρ′ ROSE
SLIDE 49
Necessary Commutation Solution
Requiring that φi ◦ φj = φj ◦ φi for all i and j ρ ρ′
SLIDE 50
Necessary Commutation Solution
Requiring that φi ◦ φj = φj ◦ φi for all i and j ρ ρ′ Template Haskell, MetaOCaml, LISP, ...
SLIDE 51
Hybrid Solution
ρ
SLIDE 52
Hybrid Solution
Suppose that φ1 and φ2 commute. ρ
SLIDE 53
Hybrid Solution
Suppose that φ1 and φ2 commute. ρ
SLIDE 54
Hybrid Solution
Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ
SLIDE 55
Hybrid Solution
Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ
SLIDE 56
Hybrid Solution
Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ
SLIDE 57
Hybrid Solution
Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ
SLIDE 58 Hybrid Solution
Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ
SLIDE 59
Commuting Transformations
How do we tell if φ1 and φ2 commute?
SLIDE 60 Commuting Transformations
How do we tell if φ1 and φ2 commute?
SLIDE 61 Commuting Transformations
How do we tell if φ1 and φ2 commute?
Determining whether or not two arbitrary transformations commute is undecidable!
SLIDE 62
Difference-Based Metaprogramming
SLIDE 63
Difference-Based Metaprogramming
Treat metaprograms as transformation generators:
SLIDE 64
Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ
SLIDE 65
Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′
SLIDE 66 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′
δ is not Turing-complete
SLIDE 67 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′
δ is not Turing-complete
δ is generated on a case-by-case basis
SLIDE 68 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′
δ is not Turing-complete
δ is generated on a case-by-case basis
- No practically significant loss of expressiveness
SLIDE 69 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ ¯ δ can express:
SLIDE 70 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ ¯ δ can express:
- Creation of a node
- Assignment to a node property
SLIDE 71 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ ¯ δ can express:
- Creation of a node
- Assignment to a node property
- Additions before or after an element in a list
SLIDE 72 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ ¯ δ can express:
- Creation of a node
- Assignment to a node property
- Additions before or after an element in a list
- Removal of an element from a list
SLIDE 73
Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ Now, prove commutation over pairs of ¯ δ.
SLIDE 74 Difference-Based Metaprogramming
Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ Now, prove commutation over pairs of ¯ δ.
δ1 ¯ δ2 ¯ δ2 ¯ δ1
SLIDE 75 A Simple BSJ Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❬✿ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀✿❃✮❀ ✿❪ ❬✿ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞▲❛st✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ } }
SLIDE 76 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
SLIDE 77 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
SLIDE 78 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
SLIDE 79 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
SLIDE 80 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
SLIDE 81 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 82 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 83 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 84 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 85 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 86 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 87 A Simple Example
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ; ; ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” last)
δ1 and ¯ δ2 commute.
δ1 and ¯ δ2 in some order.
SLIDE 88 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❬✿ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀✿❃✮❀ ✿❪ ❬✿ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳ ❛❞❞❋✐rst ✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ } }
SLIDE 89 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
SLIDE 90 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 91 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 92 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 93 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 94 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 95 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀
M1 M2
} }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 96 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ; ; } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 97 An Example of Conflict
♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ; ; } }
- Replace metaprograms with anchors
- Run each metaprogram to collect its changes
- φ1(ρ) = ¯
δ1 (“Hello, world!” first)
δ2 (“How are you?” first)
δ1 and ¯ δ2 do not commute!
SLIDE 98 Conflict Detection
Record Node Creation Rule η → ˆ v / ∈ ρ ρη → {l → ˆ v} ⇒ ρ′ (+ R η(l = ˆ v)) ρ ⇒ ρ′ List Node Creation Rule η → ˆ v / ∈ ρ ρη → [(⊲, M, ∅), (⊳, M, ∅)] ⇒ ρ′ (M + Lη) ρ ⇒ ρ′ Record Assignment Rule η → R ∈ ρ ρη → Rl → ˆ v ⇒ ρ′ (η.l ← ˆ v) ρ ⇒ ρ′ List Add Before Rule
△
η3 = ⊲ η1 → L ∈ ρ
η3, M, L) L = [◦ η′, ◦ η3, ◦ η′′] L′ = [◦ η′, (η2, M, ∅), ◦ η3, ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : η2 △ η3) ρ ⇒ ρ′ List Add After Rule
△
η3 = ⊳ η1 → L ∈ ρ
η3, M, L) L = [◦ η′, ◦ η3, ◦ η′′] L′ = [◦ η′, ◦ η3, (η2, M, ∅), ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : △ η3 η2) ρ ⇒ ρ′ List Remove Rule η1 → L ∈ ρ
- η2 = (η2, M′, S) = Σ(η2, M, L)
L = [◦ η′, ◦ η2, ◦ η′′] L′ = [◦ η′, (η2, M′, S ∪ {M}), ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : ↓ η2) ρ ⇒ ρ′ Recursive Application Rule δ′ e ⇒ ρ δ ρ ⇒ ρ′ δ (δ′ e) ⇒ ρ′ Value Rule ˆ v ⇒ ˆ v Record Assignment Conflict Rule ˆ v = ˆ v′ η.l ← ˆ v η.l ← ˆ v′ Add Before Conflict Rule ω(η2) ω(η′
2)
η1 : η2 △ η3 η1 : η′
2 △
η3 Add After Conflict Rule ω(η2) ω(η′
2)
η1 : △ η3 η2 η1 : △ η3 η′
2
Unordered Creation Conflict Rule δ = + R η(l → ˆ v) ∨ δ = + Lη η ∈ δ′ δ δ′
SLIDE 99 Conflict Detection
Record Node Creation Rule η → ˆ v / ∈ ρ ρη → {l → ˆ v} ⇒ ρ′ (+ R η(l = ˆ v)) ρ ⇒ ρ′ List Node Creation Rule η → ˆ v / ∈ ρ ρη → [(⊲, M, ∅), (⊳, M, ∅)] ⇒ ρ′ (M + Lη) ρ ⇒ ρ′ Record Assignment Rule η → R ∈ ρ ρη → Rl → ˆ v ⇒ ρ′ (η.l ← ˆ v) ρ ⇒ ρ′ List Add Before Rule
△
η3 = ⊲ η1 → L ∈ ρ
η3, M, L) L = [◦ η′, ◦ η3, ◦ η′′] L′ = [◦ η′, (η2, M, ∅), ◦ η3, ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : η2 △ η3) ρ ⇒ ρ′ List Add After Rule
△
η3 = ⊳ η1 → L ∈ ρ
η3, M, L) L = [◦ η′, ◦ η3, ◦ η′′] L′ = [◦ η′, ◦ η3, (η2, M, ∅), ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : △ η3 η2) ρ ⇒ ρ′ List Remove Rule η1 → L ∈ ρ
- η2 = (η2, M′, S) = Σ(η2, M, L)
L = [◦ η′, ◦ η2, ◦ η′′] L′ = [◦ η′, (η2, M′, S ∪ {M}), ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : ↓ η2) ρ ⇒ ρ′ Recursive Application Rule δ′ e ⇒ ρ δ ρ ⇒ ρ′ δ (δ′ e) ⇒ ρ′ Value Rule ˆ v ⇒ ˆ v Record Assignment Conflict Rule ˆ v = ˆ v′ η.l ← ˆ v η.l ← ˆ v′ Add Before Conflict Rule ω(η2) ω(η′
2)
η1 : η2 △ η3 η1 : η′
2 △
η3 Add After Conflict Rule ω(η2) ω(η′
2)
η1 : △ η3 η2 η1 : △ η3 η′
2
Unordered Creation Conflict Rule δ = + R η(l → ˆ v) ∨ δ = + Lη η ∈ δ′ δ δ′
SLIDE 100 Huzzah!
- Metaprogram conflicts are detected at compile
time
SLIDE 101 Huzzah!
- Metaprogram conflicts are detected at compile
time
- Metaprograms are still aware of their
surroundings
SLIDE 102 Huzzah!
- Metaprogram conflicts are detected at compile
time
- Metaprograms are still aware of their
surroundings
SLIDE 103
Dependencies
So how do we resolve the conflict?
SLIDE 104 Dependencies
♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❬✿ ★❞❡♣❡♥❞s ❢♦♦❀ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀✿❃✮❀ ✿❪ ❬✿ ★t❛r❣❡t ❢♦♦❀ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ }
SLIDE 105 Dependencies
♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❬✿ ★❞❡♣❡♥❞s ❢♦♦❀ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀✿❃✮❀ ✿❪ ❬✿ ★t❛r❣❡t ❢♦♦❀ ← Declare target membership ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ }
SLIDE 106 Dependencies
♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❬✿ ★❞❡♣❡♥❞s ❢♦♦❀ ← Declare target dependency ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀✿❃✮❀ ✿❪ ❬✿ ★t❛r❣❡t ❢♦♦❀ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ }
SLIDE 107 Dependencies
♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❬✿ ★❞❡♣❡♥❞s ❢♦♦❀ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀✿❃✮❀ ✿❪ ❬✿ ★t❛r❣❡t ❢♦♦❀ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡ ❧✐st ❂ ❝♦♥t❡①t✳❣❡t❆♥❝❤♦r✭✮✳ ❣❡t◆❡❛r❡st❆♥❝❡st♦r❖❢❚②♣❡✭ ❇❧♦❝❦❙t❛t❡♠❡♥t▲✐st◆♦❞❡✳❝❧❛ss✮❀ ❧✐st✳❛❞❞❋✐rst✭ ❁✿❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ }
SLIDE 108 Dependency Graph
SLIDE 109 Dependency Graph
M1 M2
SLIDE 110 Dependency Graph
M1 M2
- One node per metaprogram
- M2 is a member of target “foo”
SLIDE 111 Dependency Graph
M1 M2
foo
- One node per metaprogram
- M2 is a member of target “foo”
SLIDE 112 Dependency Graph
M1 M2
foo
- One node per metaprogram
- M2 is a member of target “foo”
SLIDE 113 Dependency Graph
M1 M2
foo
- One node per metaprogram
- M2 is a member of target “foo”
- M1 depends on the target “foo”
SLIDE 114 Dependency Graph
M1 M2
foo
- One node per metaprogram
- M2 is a member of target “foo”
- M1 depends on the target “foo”
SLIDE 115 Dependency Graph
M1 M2
foo
- One node per metaprogram
- M2 is a member of target “foo”
- M1 depends on the target “foo”
- Therefore, M1 depends on M2 (M1 M2)
SLIDE 116 Dependency Graph
M1 M2
foo
- One node per metaprogram
- M2 is a member of target “foo”
- M1 depends on the target “foo”
- Therefore, M1 depends on M2 (M1 M2)
- No more requirement for them to commute!
SLIDE 117 Dependency Graph
M1
a
M4
c
M6 M2 M3
b
M5 M7
/* M1 */ [: #target a; :] /* M2 */ [: #target a; :] /* M3 */ [: #target a, b; :] /* M4 */ [: #target c; #depends a; :] /* M5 */ [: #target c; #depends b; :] /* M6 */ [: #depends c; :] /* M7 */ [: :]
SLIDE 118 Dependency Graph
M1
a
M4
c
M6 M2 M3
b
M5 M7
- M6 depends on M2 - no obligation to commute
SLIDE 119 Dependency Graph
M1
a
M4
c
M6 M2 M3
b
M5 M7
- M6 depends on M2 - no obligation to commute
- No path between M5 and M4 - must commute
SLIDE 120 Dependency Graph
M1
a
M4
c
M6 M2 M3
b
M5 M7
- M6 depends on M2 - no obligation to commute
- No path between M5 and M4 - must commute
- No path to M7 - must always commute
SLIDE 121 Dependency Graph
M1
a
M4
c
M6 M2 M3
b
M5 M7
- M6 depends on M2 - no obligation to commute
- No path between M5 and M4 - must commute
- No path to M7 - must always commute
- More paths means less obligation to prove
commutativity
SLIDE 122
A more practical example...
SLIDE 123
A more practical example... ...but first, a new feature
SLIDE 124 Meta-Annotations
❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;
- Declarative metaprogramming abstraction
SLIDE 125 Meta-Annotations
❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;
- Declarative metaprogramming abstraction
- Specifies metaprogram code and dependencies
SLIDE 126 Meta-Annotations
❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;
- Declarative metaprogramming abstraction
- Specifies metaprogram code and dependencies
- Can annotate any declaration or block
statement
SLIDE 127 Meta-Annotations
❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;
- Declarative metaprogramming abstraction
- Specifies metaprogram code and dependencies
- Can annotate any declaration or block
statement
- Allows easy reuse of metaprogramming
constructs
SLIDE 128 Meta-Annotations
❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;
- Declarative metaprogramming abstraction
- Specifies metaprogram code and dependencies
- Can annotate any declaration or block
statement
- Allows easy reuse of metaprogramming
constructs
- Here defined by user class named Property
SLIDE 129
Meta-Annotation Dependencies
❅❅●❡♥❡r❛t❡❈♦♥str✉❝t♦r❋r♦♠Pr♦♣❡rt✐❡s ♣✉❜❧✐❝ ❝❧❛ss Location { ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x; ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
SLIDE 130 Meta-Annotation Dependencies
❅❅●❡♥❡r❛t❡❈♦♥str✉❝t♦r❋r♦♠Pr♦♣❡rt✐❡s ♣✉❜❧✐❝ ❝❧❛ss Location { ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x; ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
- Meta-annotation defns. include dependencies
SLIDE 131 Meta-Annotation Dependencies
❅❅●❡♥❡r❛t❡❈♦♥str✉❝t♦r❋r♦♠Pr♦♣❡rt✐❡s ♣✉❜❧✐❝ ❝❧❛ss Location { ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x; ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
- Meta-annotation defns. include dependencies
- @@Property is a member of target “property”
SLIDE 132 Meta-Annotation Dependencies
❅❅●❡♥❡r❛t❡❈♦♥str✉❝t♦r❋r♦♠Pr♦♣❡rt✐❡s ♣✉❜❧✐❝ ❝❧❛ss Location { ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x; ❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t y; ♣✉❜❧✐❝ String toString() { r❡t✉r♥ "("+t❤✐s.x+","+t❤✐s.y+")"; } }
- Meta-annotation defns. include dependencies
- @@Property is a member of target “property”
- @@GenerateConstructorFromProperties
depends on “property”
SLIDE 133 BSJ Dependency Graph
SLIDE 134 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties
SLIDE 135 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties
- One node per metaprogram
- @@Property participates in “property” target
SLIDE 136 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties property
- One node per metaprogram
- @@Property participates in “property” target
SLIDE 137 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties property
- One node per metaprogram
- @@Property participates in “property” target
SLIDE 138 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties property
- One node per metaprogram
- @@Property participates in “property” target
- @@GenerateConstructorFromProperties
depends on “property”
SLIDE 139 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties property
- One node per metaprogram
- @@Property participates in “property” target
- @@GenerateConstructorFromProperties
depends on “property”
SLIDE 140
BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property
SLIDE 141 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property
SLIDE 142 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property
- φ3 depends on φ1
- φ3 depends on φ2
SLIDE 143 BSJ Dependency Graph
@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property
- φ3 depends on φ1
- φ3 depends on φ2
ρ
SLIDE 144
Execution Example
ρ
SLIDE 145 Execution Example
ρ
δ1.
SLIDE 146 Execution Example
ρ ¯ δ1 φ1 +getX +setX
δ1.
SLIDE 147 Execution Example
ρ ¯ δ1 φ1
δ1.
δ2.
SLIDE 148 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 +getY +setY
δ1.
δ2.
SLIDE 149 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2
δ1.
δ2.
δ1 and ¯ δ2 commute.
SLIDE 150 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 +getX +setX +getY +setY
δ1.
δ2.
δ1 and ¯ δ2 commute.
SLIDE 151 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 +getX +setX +getY +setY +getY +setY +getX +setX
δ1.
δ2.
δ1 and ¯ δ2 commute.
SLIDE 152 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 +getX +setX +getY +setY +getY +setY +getX +setX ∼ =
δ1.
δ2.
δ1 and ¯ δ2 commute.
SLIDE 153 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2
δ1 and ¯ δ2 to ρ.
SLIDE 154 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2
δ1 and ¯ δ2 to ρ.
SLIDE 155 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ1 and ¯ δ2 to ρ.
SLIDE 156 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ1 and ¯ δ2 to ρ.
SLIDE 157 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ1 and ¯ δ2 to ρ.
- Execute φ3 on the result to get ¯
δ3.
SLIDE 158 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ3 φ3
δ1 and ¯ δ2 to ρ.
- Execute φ3 on the result to get ¯
δ3.
SLIDE 159 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ3 φ3
δ1 and ¯ δ2 to ρ.
- Execute φ3 on the result to get ¯
δ3.
δ3 to get the final object program.
SLIDE 160 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ3 φ3 ρ′ ¯ δ3
δ1 and ¯ δ2 to ρ.
- Execute φ3 on the result to get ¯
δ3.
δ3 to get the final object program.
SLIDE 161 Execution Example
ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1
δ3 φ3 ρ′ ¯ δ3
δ1 and ¯ δ2 to ρ.
- Execute φ3 on the result to get ¯
δ3.
δ3 to get the final object program.
SLIDE 162 Difference-Based Metaprogramming Summary
- Ambiguities detected at compile-time
SLIDE 163 Difference-Based Metaprogramming Summary
- Ambiguities detected at compile-time
- Metaprograms can inspect their environments
SLIDE 164 Difference-Based Metaprogramming Summary
- Ambiguities detected at compile-time
- Metaprograms can inspect their environments
- Modular, declarative metaprogramming style
SLIDE 165 Difference-Based Metaprogramming Summary
- Ambiguities detected at compile-time
- Metaprograms can inspect their environments
- Modular, declarative metaprogramming style
- Suitable for OO languages like Java
SLIDE 166 Backstage Java Implementation
- Working reference implementation available
SLIDE 167 Backstage Java Implementation
- Working reference implementation available
- Includes source (∼50k SLOC)
SLIDE 168 Backstage Java Implementation
- Working reference implementation available
- Includes source (∼50k SLOC)
- Full superset of Java 1.6
SLIDE 169
BSJ Standard Library – @@Memoized @@Memoized
SLIDE 170 BSJ Standard Library – @@Memoized @@Memoized
- Class for generating images
SLIDE 171 BSJ Standard Library – @@Memoized @@Memoized
- Class for generating images
- Each image is generated from pair of Colors
SLIDE 172 BSJ Standard Library – @@Memoized @@Memoized
- Class for generating images
- Each image is generated from pair of Colors
- Memoizing image generation routine for
performance
SLIDE 173 BSJ Standard Library – @@Memoized @@Memoized
- Class for generating images
- Each image is generated from pair of Colors
- Memoizing image generation routine for
performance
- Store cached images in a private Map keyed by
input to generation method
SLIDE 174
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ❅❅▼❡♠♦✐③❡❞ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } }
SLIDE 175
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ❅❅▼❡♠♦✐③❡❞ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } }
SLIDE 176
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ♣r✐✈❛t❡ st❛t✐❝ ❝❧❛ss ♣r✐✈❛t❡ ♣r✐✈❛t❡ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } ♣✉❜❧✐❝ }
SLIDE 177
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ♣r✐✈❛t❡ st❛t✐❝ ❝❧❛ss ♣r✐✈❛t❡ ♣r✐✈❛t❡ ♣r✐✈❛t❡ Map<❄❄❄,Image> cache = ♥❡✇ · · · ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } ♣✉❜❧✐❝ }
SLIDE 178
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ♣r✐✈❛t❡ st❛t✐❝ ❝❧❛ss Key { ♣r✐✈❛t❡ Color a; ♣r✐✈❛t❡ Color b; · · · } ♣r✐✈❛t❡ Map<❄❄❄,Image> cache = ♥❡✇ · · · ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } ♣✉❜❧✐❝ }
SLIDE 179
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ♣r✐✈❛t❡ st❛t✐❝ ❝❧❛ss Key { ♣r✐✈❛t❡ Color a; ♣r✐✈❛t❡ Color b; · · · } ♣r✐✈❛t❡ Map<Key,Image> cache = ♥❡✇ · · · ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } ♣✉❜❧✐❝ }
SLIDE 180
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ♣r✐✈❛t❡ st❛t✐❝ ❝❧❛ss Key { ♣r✐✈❛t❡ Color a; ♣r✐✈❛t❡ Color b; · · · } ♣r✐✈❛t❡ Map<Key,Image> cache = ♥❡✇ · · · ♣r✐✈❛t❡ Image igen(Color a, Color b) {· · · } ♣✉❜❧✐❝ }
SLIDE 181
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ♣r✐✈❛t❡ st❛t✐❝ ❝❧❛ss Key { ♣r✐✈❛t❡ Color a; ♣r✐✈❛t❡ Color b; · · · } ♣r✐✈❛t❡ Map<Key,Image> cache = ♥❡✇ · · · ♣r✐✈❛t❡ Image igen(Color a, Color b) {· · · } ♣✉❜❧✐❝ Image gen(Color a, Color b) { /* return cache value, igen as needed */ } }
SLIDE 182
BSJ Standard Library – @@Memoized
♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ❅❅▼❡♠♦✐③❡❞ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } }
SLIDE 183
Difference-Based Metaprogramming
SLIDE 184
Difference-Based Metaprogramming Questions?
SLIDE 185
Expressiveness
Difference-based metaprogramming separates analysis from modification. ♣✉❜❧✐❝ ❝❧❛ss Example { ♣r✐✈❛t❡ ✐♥t x = 0; ♣r✐✈❛t❡ ✐♥t y = 0; ❅❅▲♦❣❆♥❞❈♦✉♥t ♣✉❜❧✐❝ ✈♦✐❞ foo() { · · · } ❅❅▲♦❣❆♥❞❈♦✉♥t ♣✉❜❧✐❝ ✈♦✐❞ bar() { · · · } }
SLIDE 186 Injection Conflicts
M1 M2 M3
t
SLIDE 187 Injection Conflicts
M1 M2 M3 M4
t