Backstage Java Making a Difference in Metaprogramming Zachary - - PowerPoint PPT Presentation

backstage java
SMART_READER_LITE
LIVE PREVIEW

Backstage Java Making a Difference in Metaprogramming Zachary - - PowerPoint PPT Presentation

Backstage Java Making a Difference in Metaprogramming Zachary Palmer and Scott F. Smith The Johns Hopkins University October 27, 2011 Difference-Based Metaprogramming Introduction to Metaprogramming Difference-Based Metaprogramming


slide-1
SLIDE 1

Backstage Java

Making a Difference in Metaprogramming Zachary Palmer and Scott F. Smith

The Johns Hopkins University

October 27, 2011

slide-2
SLIDE 2

Difference-Based Metaprogramming

  • Introduction to Metaprogramming
slide-3
SLIDE 3

Difference-Based Metaprogramming

  • Introduction to Metaprogramming
  • Compile-Time Metaprogramming in Java
slide-4
SLIDE 4

Difference-Based Metaprogramming

  • Introduction to Metaprogramming
  • Compile-Time Metaprogramming in Java
  • Traditional Metaprogramming Model
slide-5
SLIDE 5

Difference-Based Metaprogramming

  • Introduction to Metaprogramming
  • Compile-Time Metaprogramming in Java
  • Traditional Metaprogramming Model
  • Difference-Based Metaprogramming Model
slide-6
SLIDE 6

What is metaprogramming?

slide-7
SLIDE 7

Metaprogramming

  • Programs input data and output data.

Input ρ Output

slide-8
SLIDE 8

Metaprogramming

  • Programs input data and output data.

Input ρ Output

  • Metaprograms input programs (or program

fragments) and output the same.

ρ φ ρ′

slide-9
SLIDE 9

Examples of Metaprogramming

  • C Macros
  • C++ Templates
  • LISP Macros
  • Template Haskell
  • MetaOCaml
  • Stratego
  • Groovy
  • etc. etc.
slide-10
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
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
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
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

  • x * x * . . . * x * x
slide-14
SLIDE 14

Template Haskell

  • Programmatic code generation
slide-15
SLIDE 15

Template Haskell

  • Programmatic code generation
  • Literal syntax for AST construction
slide-16
SLIDE 16

Template Haskell

  • Programmatic code generation
  • Literal syntax for AST construction
  • Functional programming style
slide-17
SLIDE 17

Template Haskell

  • Programmatic code generation
  • Literal syntax for AST construction
  • Functional programming style
  • Very limited ability to inspect environment
slide-18
SLIDE 18

Why not Template Java?

slide-19
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
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
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
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
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
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
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
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
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
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
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
SLIDE 30

What Do We Want?

  • Object-oriented, declarative metaprogramming

style

slide-31
SLIDE 31

What Do We Want?

  • Object-oriented, declarative metaprogramming

style

  • Awareness of surrounding code
slide-32
SLIDE 32

What Do We Want?

  • Object-oriented, declarative metaprogramming

style

  • Awareness of surrounding code
  • Modular, independent metaprograms
slide-33
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
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
SLIDE 35

Traditional Metaprogramming

slide-36
SLIDE 36

Traditional Metaprogramming

  • Metaprograms are a series of program

transformations

slide-37
SLIDE 37

Traditional Metaprogramming

  • Metaprograms are a series of program

transformations

  • Each available transformation occurs exactly
  • nce
slide-38
SLIDE 38

Traditional Metaprogramming

  • Metaprograms are a series of program

transformations

  • Each available transformation occurs exactly
  • nce
  • True even for embedded syntax
slide-39
SLIDE 39

Embedded Metaprogram Semantics

slide-40
SLIDE 40

Embedded Metaprogram Semantics

slide-41
SLIDE 41

Embedded Metaprogram Semantics

φ1 φ2

slide-42
SLIDE 42

Embedded Metaprogram Semantics

φ1

  • φ2
slide-43
SLIDE 43

Embedded Metaprogram Semantics

φ2

  • φ1
slide-44
SLIDE 44

Embedded Metaprogram Semantics

φ2

  • φ1
  • How do we pick?
slide-45
SLIDE 45

Ambiguity in Metaprogramming

Assuming three transformations φ1, φ2, φ3... ρ ? ? ? ? ? ?

slide-46
SLIDE 46

Ambiguity in Metaprogramming

Assuming three transformations φ1, φ2, φ3... ρ ? ? ? ? ? ? OpenJava, Groovy

slide-47
SLIDE 47

Total Ordering Solution

Declaring φ2 before φ1, which is before φ3. ρ ρ′

slide-48
SLIDE 48

Total Ordering Solution

Declaring φ2 before φ1, which is before φ3. ρ ρ′ ROSE

slide-49
SLIDE 49

Necessary Commutation Solution

Requiring that φi ◦ φj = φj ◦ φi for all i and j ρ ρ′

slide-50
SLIDE 50

Necessary Commutation Solution

Requiring that φi ◦ φj = φj ◦ φi for all i and j ρ ρ′ Template Haskell, MetaOCaml, LISP, ...

slide-51
SLIDE 51

Hybrid Solution

ρ

slide-52
SLIDE 52

Hybrid Solution

Suppose that φ1 and φ2 commute. ρ

slide-53
SLIDE 53

Hybrid Solution

Suppose that φ1 and φ2 commute. ρ

slide-54
SLIDE 54

Hybrid Solution

Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ

slide-55
SLIDE 55

Hybrid Solution

Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ

slide-56
SLIDE 56

Hybrid Solution

Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ

slide-57
SLIDE 57

Hybrid Solution

Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ

slide-58
SLIDE 58

Hybrid Solution

Suppose that φ1 and φ2 commute. Suppose that φ3 must occur after them. ρ

  • Backstage Java*
slide-59
SLIDE 59

Commuting Transformations

How do we tell if φ1 and φ2 commute?

slide-60
SLIDE 60

Commuting Transformations

How do we tell if φ1 and φ2 commute?

  • ?
slide-61
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
SLIDE 62

Difference-Based Metaprogramming

slide-63
SLIDE 63

Difference-Based Metaprogramming

Treat metaprograms as transformation generators:

slide-64
SLIDE 64

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ

slide-65
SLIDE 65

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′

slide-66
SLIDE 66

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′

  • Language of ¯

δ is not Turing-complete

slide-67
SLIDE 67

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′

  • Language of ¯

δ is not Turing-complete

  • Each ¯

δ is generated on a case-by-case basis

slide-68
SLIDE 68

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′

  • Language of ¯

δ is not Turing-complete

  • Each ¯

δ is generated on a case-by-case basis

  • No practically significant loss of expressiveness
slide-69
SLIDE 69

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ ¯ δ can express:

  • Creation of a node
slide-70
SLIDE 70

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ ¯ δ can express:

  • Creation of a node
  • Assignment to a node property
slide-71
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
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
SLIDE 73

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ Now, prove commutation over pairs of ¯ δ.

slide-74
SLIDE 74

Difference-Based Metaprogramming

Treat metaprograms as transformation generators: φ(ρ) = ¯ δ ¯ δ(ρ) = ρ′ Now, prove commutation over pairs of ¯ δ.

  • ¯

δ1 ¯ δ2 ¯ δ2 ¯ δ1

slide-75
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
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
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
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
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(ρ) = ¯

δ2 (“How are you?” last)

slide-80
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

slide-81
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-82
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-83
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-84
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-85
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-86
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-87
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(ρ) = ¯

δ2 (“How are you?” last)

  • Prove that ¯

δ1 and ¯ δ2 commute.

  • Execute ¯

δ1 and ¯ δ2 in some order.

slide-88
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
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(ρ) = ¯

δ2 (“How are you?” first)

slide-90
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-91
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-92
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-93
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-94
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-95
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-96
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

slide-97
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(ρ) = ¯

δ2 (“How are you?” first)

  • Now, ¯

δ1 and ¯ δ2 do not commute!

  • But we can detect this!
slide-98
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 = Σ( △

η3, M, L) L = [◦ η′, ◦ η3, ◦ η′′] L′ = [◦ η′, (η2, M, ∅), ◦ η3, ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : η2 △ η3) ρ ⇒ ρ′ List Add After Rule

η3 = ⊳ η1 → L ∈ ρ

  • η3 = Σ( △

η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
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 = Σ( △

η3, M, L) L = [◦ η′, ◦ η3, ◦ η′′] L′ = [◦ η′, (η2, M, ∅), ◦ η3, ◦ η′′] ρη1 → L′ ⇒ ρ′ (M η1 : η2 △ η3) ρ ⇒ ρ′ List Add After Rule

η3 = ⊳ η1 → L ∈ ρ

  • η3 = Σ( △

η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
SLIDE 100

Huzzah!

  • Metaprogram conflicts are detected at compile

time

slide-101
SLIDE 101

Huzzah!

  • Metaprogram conflicts are detected at compile

time

  • Metaprograms are still aware of their

surroundings

slide-102
SLIDE 102

Huzzah!

  • Metaprogram conflicts are detected at compile

time

  • Metaprograms are still aware of their

surroundings

slide-103
SLIDE 103

Dependencies

So how do we resolve the conflict?

slide-104
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
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
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
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
SLIDE 108

Dependency Graph

  • One node per metaprogram
slide-109
SLIDE 109

Dependency Graph

M1 M2

  • One node per metaprogram
slide-110
SLIDE 110

Dependency Graph

M1 M2

  • One node per metaprogram
  • M2 is a member of target “foo”
slide-111
SLIDE 111

Dependency Graph

M1 M2

foo

  • One node per metaprogram
  • M2 is a member of target “foo”
slide-112
SLIDE 112

Dependency Graph

M1 M2

foo

  • One node per metaprogram
  • M2 is a member of target “foo”
slide-113
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
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
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
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
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
SLIDE 118

Dependency Graph

M1

a

M4

c

M6 M2 M3

b

M5 M7

  • M6 depends on M2 - no obligation to commute
slide-119
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
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
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
SLIDE 122

A more practical example...

slide-123
SLIDE 123

A more practical example... ...but first, a new feature

slide-124
SLIDE 124

Meta-Annotations

❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;

  • Declarative metaprogramming abstraction
slide-125
SLIDE 125

Meta-Annotations

❅❅Pr♦♣❡rt② ♣r✐✈❛t❡ ✐♥t x;

  • Declarative metaprogramming abstraction
  • Specifies metaprogram code and dependencies
slide-126
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
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
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
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
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
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
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
SLIDE 133

BSJ Dependency Graph

  • One node per metaprogram
slide-134
SLIDE 134

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties

  • One node per metaprogram
slide-135
SLIDE 135

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties

  • One node per metaprogram
  • @@Property participates in “property” target
slide-136
SLIDE 136

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties property

  • One node per metaprogram
  • @@Property participates in “property” target
slide-137
SLIDE 137

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties property

  • One node per metaprogram
  • @@Property participates in “property” target
slide-138
SLIDE 138

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties property

  • One node per metaprogram
  • @@Property participates in “property” target
  • @@GenerateConstructorFromProperties

depends on “property”

slide-139
SLIDE 139

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties property

  • One node per metaprogram
  • @@Property participates in “property” target
  • @@GenerateConstructorFromProperties

depends on “property”

slide-140
SLIDE 140

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property

slide-141
SLIDE 141

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property

  • φ3 depends on φ1
slide-142
SLIDE 142

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property

  • φ3 depends on φ1
  • φ3 depends on φ2
slide-143
SLIDE 143

BSJ Dependency Graph

@@Property @@Property @@GenerateConstructorFromProperties φ1 φ3 φ2 property

  • φ3 depends on φ1
  • φ3 depends on φ2

ρ

slide-144
SLIDE 144

Execution Example

ρ

slide-145
SLIDE 145

Execution Example

ρ

  • Execute φ1 obtaining ¯

δ1.

slide-146
SLIDE 146

Execution Example

ρ ¯ δ1 φ1 +getX +setX

  • Execute φ1 obtaining ¯

δ1.

slide-147
SLIDE 147

Execution Example

ρ ¯ δ1 φ1

  • Execute φ1 obtaining ¯

δ1.

  • Execute φ2 obtaining ¯

δ2.

slide-148
SLIDE 148

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 +getY +setY

  • Execute φ1 obtaining ¯

δ1.

  • Execute φ2 obtaining ¯

δ2.

slide-149
SLIDE 149

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2

  • Execute φ1 obtaining ¯

δ1.

  • Execute φ2 obtaining ¯

δ2.

  • Prove ¯

δ1 and ¯ δ2 commute.

slide-150
SLIDE 150

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 +getX +setX +getY +setY

  • Execute φ1 obtaining ¯

δ1.

  • Execute φ2 obtaining ¯

δ2.

  • Prove ¯

δ1 and ¯ δ2 commute.

slide-151
SLIDE 151

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 +getX +setX +getY +setY +getY +setY +getX +setX

  • Execute φ1 obtaining ¯

δ1.

  • Execute φ2 obtaining ¯

δ2.

  • Prove ¯

δ1 and ¯ δ2 commute.

slide-152
SLIDE 152

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 +getX +setX +getY +setY +getY +setY +getX +setX ∼ =

  • Execute φ1 obtaining ¯

δ1.

  • Execute φ2 obtaining ¯

δ2.

  • Prove ¯

δ1 and ¯ δ2 commute.

slide-153
SLIDE 153

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2

  • Apply ¯

δ1 and ¯ δ2 to ρ.

slide-154
SLIDE 154

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2

  • Apply ¯

δ1 and ¯ δ2 to ρ.

slide-155
SLIDE 155

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • Apply ¯

δ1 and ¯ δ2 to ρ.

slide-156
SLIDE 156

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • Apply ¯

δ1 and ¯ δ2 to ρ.

slide-157
SLIDE 157

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • Apply ¯

δ1 and ¯ δ2 to ρ.

  • Execute φ3 on the result to get ¯

δ3.

slide-158
SLIDE 158

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • ¯

δ3 φ3

  • Apply ¯

δ1 and ¯ δ2 to ρ.

  • Execute φ3 on the result to get ¯

δ3.

slide-159
SLIDE 159

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • ¯

δ3 φ3

  • Apply ¯

δ1 and ¯ δ2 to ρ.

  • Execute φ3 on the result to get ¯

δ3.

  • Apply ¯

δ3 to get the final object program.

slide-160
SLIDE 160

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • ¯

δ3 φ3 ρ′ ¯ δ3

  • Apply ¯

δ1 and ¯ δ2 to ρ.

  • Execute φ3 on the result to get ¯

δ3.

  • Apply ¯

δ3 to get the final object program.

slide-161
SLIDE 161

Execution Example

ρ ¯ δ1 φ1 ¯ δ2 φ2 ¯ δ1 ¯ δ2 ¯ δ2 ¯ δ1

  • ¯

δ3 φ3 ρ′ ¯ δ3

  • Apply ¯

δ1 and ¯ δ2 to ρ.

  • Execute φ3 on the result to get ¯

δ3.

  • Apply ¯

δ3 to get the final object program.

slide-162
SLIDE 162

Difference-Based Metaprogramming Summary

  • Ambiguities detected at compile-time
slide-163
SLIDE 163

Difference-Based Metaprogramming Summary

  • Ambiguities detected at compile-time
  • Metaprograms can inspect their environments
slide-164
SLIDE 164

Difference-Based Metaprogramming Summary

  • Ambiguities detected at compile-time
  • Metaprograms can inspect their environments
  • Modular, declarative metaprogramming style
slide-165
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
SLIDE 166

Backstage Java Implementation

  • Working reference implementation available
slide-167
SLIDE 167

Backstage Java Implementation

  • Working reference implementation available
  • Includes source (∼50k SLOC)
slide-168
SLIDE 168

Backstage Java Implementation

  • Working reference implementation available
  • Includes source (∼50k SLOC)
  • Full superset of Java 1.6
slide-169
SLIDE 169

BSJ Standard Library – @@Memoized @@Memoized

slide-170
SLIDE 170

BSJ Standard Library – @@Memoized @@Memoized

  • Class for generating images
slide-171
SLIDE 171

BSJ Standard Library – @@Memoized @@Memoized

  • Class for generating images
  • Each image is generated from pair of Colors
slide-172
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
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
SLIDE 174

BSJ Standard Library – @@Memoized

♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ❅❅▼❡♠♦✐③❡❞ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } }

slide-175
SLIDE 175

BSJ Standard Library – @@Memoized

♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ❅❅▼❡♠♦✐③❡❞ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } }

slide-176
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
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
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
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
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
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
SLIDE 182

BSJ Standard Library – @@Memoized

♣✉❜❧✐❝ ❝❧❛ss ImageGenerator { ❅❅▼❡♠♦✐③❡❞ ♣✉❜❧✐❝ Image gen(Color a, Color b) {· · · } }

slide-183
SLIDE 183

Difference-Based Metaprogramming

slide-184
SLIDE 184

Difference-Based Metaprogramming Questions?

slide-185
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
SLIDE 186

Injection Conflicts

M1 M2 M3

t

slide-187
SLIDE 187

Injection Conflicts

M1 M2 M3 M4

t