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+")"; } }
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...
Traditional Metaprogramming
Traditional Metaprogramming • Metaprograms are a series of program transformations
Traditional Metaprogramming • Metaprograms are a series of program transformations • Each available transformation occurs exactly once
Traditional Metaprogramming • Metaprograms are a series of program transformations • Each available transformation occurs exactly once • True even for embedded syntax
Embedded Metaprogram Semantics
Embedded Metaprogram Semantics
Embedded Metaprogram Semantics φ 1 φ 2
Embedded Metaprogram Semantics � � �� φ 1 φ 2
Embedded Metaprogram Semantics � � �� φ 2 φ 1
Embedded Metaprogram Semantics � � �� φ 2 φ 1 How do we pick?
Ambiguity in Metaprogramming Assuming three transformations φ 1 , φ 2 , φ 3 ... ? ? ? ? ? ? ρ
Ambiguity in Metaprogramming Assuming three transformations φ 1 , φ 2 , φ 3 ... ? ? ? ? ? ? ρ OpenJava, Groovy
Total Ordering Solution Declaring φ 2 before φ 1 , which is before φ 3 . ρ ′ ρ
Total Ordering Solution Declaring φ 2 before φ 1 , which is before φ 3 . ρ ′ ρ ROSE
Necessary Commutation Solution Requiring that φ i ◦ φ j = φ j ◦ φ i for all i and j ρ ′ ρ
Necessary Commutation Solution Requiring that φ i ◦ φ j = φ j ◦ φ i for all i and j ρ ′ ρ Template Haskell, MetaOCaml, LISP, ...
Hybrid Solution ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. ◦ ◦ ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. Suppose that φ 3 must occur after them. ◦ ◦ ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. Suppose that φ 3 must occur after them. ◦ ◦ ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. Suppose that φ 3 must occur after them. ◦ ◦ ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. Suppose that φ 3 must occur after them. ◦ ◦ ρ
Hybrid Solution Suppose that φ 1 and φ 2 commute. Suppose that φ 3 must occur after them. ◦ ◦ ρ Backstage Java*
Commuting Transformations How do we tell if φ 1 and φ 2 commute?
Commuting Transformations How do we tell if φ 1 and φ 2 commute? ◦ ?
Commuting Transformations How do we tell if φ 1 and φ 2 commute? ◦ ? Determining whether or not two arbitrary transformations commute is undecidable!
Difference-Based Metaprogramming
Difference-Based Metaprogramming Treat metaprograms as transformation generators :
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′ • Language of ¯ δ is not Turing-complete
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′ • Language of ¯ δ is not Turing-complete • Each ¯ δ is generated on a case-by-case basis
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
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′ ¯ δ can express: • Creation of a node
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′ ¯ δ can express: • Creation of a node • Assignment to a node property
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
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
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′ Now, prove commutation over pairs of � ¯ δ � .
Difference-Based Metaprogramming Treat metaprograms as transformation generators : = ¯ φ ( ρ ) δ � ¯ δ � ( ρ ) = ρ ′ Now, prove commutation over pairs of � ¯ δ � . � ¯ � ¯ δ 2 � δ 1 � ◦ � ¯ � ¯ δ 1 � δ 2 �
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❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ } }
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • Replace metaprograms with anchors
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • Replace metaprograms with anchors • Run each metaprogram to collect its changes
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • Replace metaprograms with anchors • Run each metaprogram to collect its changes • φ 1 ( ρ ) = ¯ δ 1 (“Hello, world!” first)
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • Replace metaprograms with anchors • Run each metaprogram to collect its changes • φ 1 ( ρ ) = ¯ δ 1 (“Hello, world!” first) • φ 2 ( ρ ) = ¯ δ 2 (“How are you?” last)
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • 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.
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • 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.
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ M 1 M 2 } } • 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.
A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ M 1 M 2 ❙②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.
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • 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.
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 ❙②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.
A Simple Example ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ M 1 M 2 ❙②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.
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.
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❡ ②♦✉❄✧✮❀✿❃✮❀ ✿❪ } }
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • Replace metaprograms with anchors • Run each metaprogram to collect its changes • φ 1 ( ρ ) = ¯ δ 1 (“Hello, world!” first) • φ 2 ( ρ ) = ¯ δ 2 (“How are you?” first )
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • 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!
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ M 1 M 2 } } • 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!
An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ M 1 M 2 } } • 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!
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { M 1 M 2 } } • 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!
❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ M 1 M 2 } } • 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!
An Example of Conflict ♣✉❜❧✐❝ ❝❧❛ss Example { ♣✉❜❧✐❝ st❛t✐❝ ✈♦✐❞ main(String[] arg) { ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍♦✇ ❛r❡ ②♦✉❄✧✮❀ ❙②st❡♠✳♦✉t✳♣r✐♥t❧♥✭✧❍❡❧❧♦✱ ✇♦r❧❞✦✧✮❀ M 1 M 2 } } • 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!
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!
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!
Conflict Detection Record Node Creation Rule List Remove Rule v } � ⇒ ρ ′ η �→ ˆ v / ∈ ρ ρ � η �→ { l �→ ˆ η 1 �→ L ∈ ρ ◦ R η ( l = ˆ v )) ρ ⇒ ρ ′ η 2 = ( η 2 , M ′ , S ) = Σ( η 2 , M , L ) (+ L = [ ◦ η ′ , ◦ η 2 , ◦ η ′′ ] List Node Creation Rule L ′ = [ ◦ η ′ , ( η 2 , M ′ , S ∪ {M} ) , ◦ η ′′ ] η �→ ˆ v / ∈ ρ ρ � η 1 �→ L ′ � ⇒ ρ ′ ρ � η �→ [( ⊲, M , ∅ ) , ( ⊳, M , ∅ )] � ⇒ ρ ′ ( M � η 1 : ↓ η 2 ) ρ ⇒ ρ ′ L η ) ρ ⇒ ρ ′ ( M � + Recursive Application Rule Record Assignment Rule δ ′ e ⇒ ρ δ ρ ⇒ ρ ′ Value Rule v �� ⇒ ρ ′ η �→ R ∈ ρ ρ � η �→ R � l �→ ˆ δ ( δ ′ e ) ⇒ ρ ′ v ⇒ ˆ v ) ρ ⇒ ρ ′ ˆ v ( η. l ← ˆ Record Assignment Conflict Rule List Add Before Rule v ′ △ ˆ v � = ˆ η 3 � = ⊲ η 1 �→ L ∈ ρ ◦ L = [ ◦ η ′ , ◦ η 3 , ◦ v ′ η 3 = Σ( △ η ′′ ] η. l ← ˆ v � η. l ← ˆ η 3 , M , L ) L ′ = [ ◦ η ′ , ( η 2 , M , ∅ ) , ◦ η 3 , ◦ η ′′ ] Add Before Conflict Rule ω ( η ′ ρ � η 1 �→ L ′ � ⇒ ρ ′ ω ( η 2 ) 2 ) η 1 : η 2 � △ η 3 � η 1 : η ′ 2 � △ ( M � η 1 : η 2 � △ η 3 ) ρ ⇒ ρ ′ η 3 Add After Conflict Rule List Add After Rule ω ( η ′ ω ( η 2 ) 2 ) △ η 3 � = ⊳ η 1 �→ L ∈ ρ η 1 : △ η 3 � η 2 � η 1 : △ η 3 � η ′ ◦ L = [ ◦ η ′ , ◦ η 3 , ◦ η 3 = Σ( △ η ′′ ] 2 η 3 , M , L ) L ′ = [ ◦ η ′ , ◦ η 3 , ( η 2 , M , ∅ ) , ◦ η ′′ ] Unordered Creation Conflict Rule R η ( l �→ ˆ ρ � η 1 �→ L ′ � ⇒ ρ ′ L η η ∈ δ ′ δ = + v ) ∨ δ = + ( M � η 1 : △ η 3 � η 2 ) ρ ⇒ ρ ′ δ � δ ′
Conflict Detection Record Node Creation Rule List Remove Rule v } � ⇒ ρ ′ η �→ ˆ v / ∈ ρ ρ � η �→ { l �→ ˆ η 1 �→ L ∈ ρ ◦ R η ( l = ˆ v )) ρ ⇒ ρ ′ η 2 = ( η 2 , M ′ , S ) = Σ( η 2 , M , L ) (+ L = [ ◦ η ′ , ◦ η 2 , ◦ η ′′ ] List Node Creation Rule L ′ = [ ◦ η ′ , ( η 2 , M ′ , S ∪ {M} ) , ◦ η ′′ ] η �→ ˆ v / ∈ ρ ρ � η 1 �→ L ′ � ⇒ ρ ′ ρ � η �→ [( ⊲, M , ∅ ) , ( ⊳, M , ∅ )] � ⇒ ρ ′ ( M � η 1 : ↓ η 2 ) ρ ⇒ ρ ′ L η ) ρ ⇒ ρ ′ ( M � + Recursive Application Rule Record Assignment Rule δ ′ e ⇒ ρ δ ρ ⇒ ρ ′ Value Rule v �� ⇒ ρ ′ η �→ R ∈ ρ ρ � η �→ R � l �→ ˆ δ ( δ ′ e ) ⇒ ρ ′ v ⇒ ˆ v ) ρ ⇒ ρ ′ ˆ v ( η. l ← ˆ Record Assignment Conflict Rule List Add Before Rule v ′ △ ˆ v � = ˆ η 3 � = ⊲ η 1 �→ L ∈ ρ ◦ L = [ ◦ η ′ , ◦ η 3 , ◦ v ′ η 3 = Σ( △ η ′′ ] η. l ← ˆ v � η. l ← ˆ η 3 , M , L ) L ′ = [ ◦ η ′ , ( η 2 , M , ∅ ) , ◦ η 3 , ◦ η ′′ ] Add Before Conflict Rule ω ( η ′ ρ � η 1 �→ L ′ � ⇒ ρ ′ ω ( η 2 ) 2 ) η 1 : η 2 � △ η 3 � η 1 : η ′ 2 � △ ( M � η 1 : η 2 � △ η 3 ) ρ ⇒ ρ ′ η 3 Add After Conflict Rule List Add After Rule ω ( η ′ ω ( η 2 ) 2 ) △ η 3 � = ⊳ η 1 �→ L ∈ ρ η 1 : △ η 3 � η 2 � η 1 : △ η 3 � η ′ ◦ L = [ ◦ η ′ , ◦ η 3 , ◦ η 3 = Σ( △ η ′′ ] 2 η 3 , M , L ) L ′ = [ ◦ η ′ , ◦ η 3 , ( η 2 , M , ∅ ) , ◦ η ′′ ] Unordered Creation Conflict Rule R η ( l �→ ˆ ρ � η 1 �→ L ′ � ⇒ ρ ′ L η η ∈ δ ′ δ = + v ) ∨ δ = + ( M � η 1 : △ η 3 � η 2 ) ρ ⇒ ρ ′ δ � δ ′
Huzzah! • Metaprogram conflicts are detected at compile time
Recommend
More recommend