Challenge the future
Delft
University of
Technology
Course IN4303 2013/14
Compiler Construction
Guido Wachsmuth
Declarativ...
today’s lecture
Term Rewriting
Overview
manual implementation
• static analysis & error checking
• code generation
• seman...
Term Rewriting
overview
I
3
architecture
Term Rewriting
Modern Compilers in IDEs
4
Source
Code
architecture
Term Rewriting
Modern Compilers in IDEs
4
Source
Code
parse
architecture
Term Rewriting
Modern Compilers in IDEs
4
Source
Code
parse
check
architecture
Term Rewriting
Modern Compilers in IDEs
4
Source
Code
parse
check
architecture
Term Rewriting
Modern Compilers in IDEs
4
Source
Code
parse generate
Machine
Code
check
manual implementation
Term Rewriting
Traditional Compiler Compilers
5
Language
Implementation
compile
Compiler
compilation + compilation
Term Rewriting
Traditional Compiler Compilers
6
Language
Implementation
compile
Compiler
Languag...
compilation + interpretation
Term Rewriting
Traditional Compiler Compilers
7
interpret
Interpreter
Language
Definition
com...
compilation + interpretation
Term Rewriting
Spoofax Language Workbench
8
interpret
Generic
Parser
Syntax
Definition
compil...
manual implementation
Term Rewriting
Spoofax Language Workbench
Stratego
• declarative DSL
• domain: program transformatio...
concepts
Term Rewriting
Stratego
signatures
rewrite rules
• transform term to term
• left-hand side
• pattern matching & v...
example
Term Rewriting
Stratego
module desugar
imports
include/Tiger
operators
strategies
	
desugar-all = innermost(desuga...
Term Rewriting
signatures
II
12
context-free syntax
"-" Exp -> Exp {cons("Uminus")}
Exp "+" Exp -> Exp {cons("Plus")}
Exp "-" Exp -> Exp {cons("Minus")}
E...
signature
Term Rewriting
signature
constructors
Constructor : Cons -> Term
Application : Cons * List(Term) -> Term
List : ...
Term Rewriting
rewrite rules
III
15
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printint(x)
else
()
IfThen(
Var("x")
, Call(
"printint"
, [Va...
desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printi...
desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printi...
desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printi...
desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printi...
desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal())
Term Rewriting
Desugaring
if x then
printint(x)
16
if x then
printi...
desugar: Uminus(e) 		 -> Bop(MINUS(), Int("0"), e)
	
desugar: Plus(e1, e2) -> Bop(PLUS(), e1, e2)
desugar: Minus(e1, e2) -...
Term Rewriting
Constant Folding
x := 21 + 21 + x
18
x := 42 + x
Term Rewriting
Constant Folding
x := 21 + 21 + x
18
x := 42 + x
Assign(
Var("x")
, Plus(
Plus(
Int("21")
, Int("21")
)
, V...
eval: Bop(PLUS(), Int(i1), Int(i2)) -> Int(i3)
where <addS> (i1, i2) => i3
Term Rewriting
Constant Folding
x := 21 + 21 + ...
eval: Bop(PLUS(), Int(i1), Int(i2)) -> Int(<addS> (i1, i2))
eval: Bop(MINUS(), Int(i1), Int(i2)) -> Int(<subtS> (i1, i2))
...
parameters
Term Rewriting
Rewrite Rules
f(x,y|a,b): lhs -> rhs
• strategy or rule parameters x,y
• term parameters a,b
• n...
example: map
Term Rewriting
Rules with Parameters
21
[ 1
, 2
, 3
]
[ 2
, 3
, 4
]
map(inc)
example: map
Term Rewriting
Rules with Parameters
21
[ 1
, 2
, 3
]
[ 2
, 3
, 4
]
map(inc)
inc
1
2
3
2
3
4inc
inc
example: map
Term Rewriting
Rules with Parameters
map(s): [] -> []	
map(s): [x|xs] -> [<s> x | <map(s)> xs]
21
[ 1
, 2
, 3...
example: zip
Term Rewriting
Rules with Parameters
22
[ 1
, 2
, 3
]
[ (1,4)
, (2,5)
, (3,6)
]
[ 4
, 5
, 6
]zip
example: zip
Term Rewriting
Rules with Parameters
22
[ 1
, 2
, 3
]
[ (1,4)
, (2,5)
, (3,6)
]
[ 4
, 5
, 6
]zip
[ 1
, 2
, 3
...
example: zip
Term Rewriting
Rules with Parameters
22
[ 1
, 2
, 3
]
[ (1,4)
, (2,5)
, (3,6)
]
[ 4
, 5
, 6
]zip
[ 1
, 2
, 3
...
example: zip
Term Rewriting
Rules with Parameters
zip(s): ([],[]) -> []	
zip(s): ([x|xs],[y|ys]) -> [<s> (x,y) | <zip(s)> ...
example: fold
Term Rewriting
Rules with Parameters
23
[1,2,3] foldr(!0,add) 6
example: fold
Term Rewriting
Rules with Parameters
23
[1,2,3] foldr(!0,add) 6
[]
0
[3]
3
[2,3]
56
[1,2,3]
example: fold
Term Rewriting
Rules with Parameters
foldr(s1,s2): [] -> <s1>	
foldr(s1,s2): [x|xs] -> <s2> (x,<foldr(s1,s2)...
example: inverse
Term Rewriting
Rules with Parameters
24
[1,2,3] inverse(|[]) [3,2,1]
example: inverse
Term Rewriting
Rules with Parameters
24
[1,2,3] inverse(|[]) [3,2,1]
[2,3]
[1][]
[1,2,3] [3]
[2,1] [3,2,1...
example: inverse
Term Rewriting
Rules with Parameters
inverse(|is): [] -> is	
inverse(|is): [x|xs] -> <inverse(|[x|is])> x...
Term Rewriting 25
coffee break
Term Rewriting
rewrite strategies
IV
26
rules and strategies
Term Rewriting
Transformation Definitions
rewrite rules
• term to term
• left-hand side matching
• ri...
example
Term Rewriting
Stratego
module desugar
imports
include/Tiger
operators
strategies
	
desugar-all = innermost(desuga...
example
Term Rewriting
Stratego
module eval
imports
include/Tiger
operators
desugar
strategies
eval-all = innermost(desuga...
Term Rewriting
Strategy Combinators
identity
id
30
Term Rewriting
Strategy Combinators
identity
id
failure
fail
30
Term Rewriting
Strategy Combinators
identity
id
failure
fail
sequential composition
s1 ; s2
30
Term Rewriting
Strategy Combinators
identity
id
failure
fail
sequential composition
s1 ; s2
deterministic choice
s1 <+ s2
...
Term Rewriting
Strategy Combinators
identity
id
failure
fail
sequential composition
s1 ; s2
deterministic choice
s1 <+ s2
...
Term Rewriting
Strategy Combinators
identity
id
failure
fail
sequential composition
s1 ; s2
deterministic choice
s1 <+ s2
...
variables
Term Rewriting
Strategy Combinators
pattern matching
?t
31
variables
Term Rewriting
Strategy Combinators
pattern matching
?t
pattern instantiation
!t
31
variables
Term Rewriting
Strategy Combinators
pattern matching
?t
pattern instantiation
!t
strategy application
<s> t ≣ !t...
variables
Term Rewriting
Strategy Combinators
pattern matching
?t
pattern instantiation
!t
strategy application
<s> t ≣ !t...
variables
Term Rewriting
Strategy Combinators
pattern matching
?t
pattern instantiation
!t
strategy application
<s> t ≣ !t...
rules and strategies
Term Rewriting
Strategy Combinators
named rewrite rule
l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2
32
rules and strategies
Term Rewriting
Strategy Combinators
named rewrite rule
l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2
unscop...
rules and strategies
Term Rewriting
Strategy Combinators
named rewrite rule
l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2
unscop...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
33
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
examples
Term Rewriting
Strategy Combinators
try(s) = s <+ id
repeat(s) = try(s ; repeat(s))
topdown(s) = s ; all(topdown(...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
topdown(s) = s ; all...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(...
example
Term Rewriting
Stratego
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(alltd(s))
alltd(switch)
42
Const
Mu...
example
Term Rewriting
Stratego
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(alltd(s))
alltd(switch)
42
Const
Mu...
example
Term Rewriting
Stratego
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(alltd(s))
alltd(switch)
42
Const
Mu...
example
Term Rewriting
Stratego
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(alltd(s))
alltd(switch)
42
Const
Mu...
example
Term Rewriting
Stratego
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(alltd(s))
alltd(switch)
42
Const
Mu...
example
Term Rewriting
Stratego
switch: Mul(e1, e2) -> Mul(e2, e1)
alltd(s) = s <+ all(alltd(s))
alltd(switch)
43
Const
Mu...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
bottomup(s) = all(bo...
Mul
Const
3
Const
5 4
Const
Add
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> ...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
example
Term Rewriting
Stratego
switch: Add(e1, e2) -> Add(e2, e1)
switch: Mul(e1, e2) -> Mul(e2, e1)
innermost(s) = botto...
Term Rewriting
summary
V
55
lessons learned
Term Rewriting
Summary
56
lessons learned
Term Rewriting
Summary
How do you define AST transformations in Stratego?
• signatures
• rewrite rules
• r...
lessons learned
Term Rewriting
Summary
How do you define AST transformations in Stratego?
• signatures
• rewrite rules
• r...
learn more
Term Rewriting
Literature
57
learn more
Term Rewriting
Literature
Spoofax
Lennart C. L. Kats, Eelco Visser: The Spoofax Language Workbench.
Rules for D...
learn more
Term Rewriting
Literature
Spoofax
Lennart C. L. Kats, Eelco Visser: The Spoofax Language Workbench.
Rules for D...
coming next
Term Rewriting
Outlook
declarative semantics definition
• Lecture 5: Static Analysis and Error Checking
• Lect...
Term Rewriting 59
attribution & copyrights
Term Rewriting
Pictures
Slides 1, 9, 10, 20, 27: Stratego by Kendrick Arnett, some rights reserve...
Upcoming SlideShare
Loading in...5
×

Declarative Semantics Definition - Term Rewriting

2,000

Published on

Presentation slides for lecture 6 of course IN4303 on Compiler Construction at TU Delft.

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,000
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • feedback loop\n
  • feedback loop\n
  • feedback loop\n
  • feedback loop\n
  • feedback loop\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n
  • Declarative Semantics Definition - Term Rewriting

    1. 1. Challenge the future Delft University of Technology Course IN4303 2013/14 Compiler Construction Guido Wachsmuth Declarative Semantics Definition Term Rewriting
    2. 2. today’s lecture Term Rewriting Overview manual implementation • static analysis & error checking • code generation • semantic editor services domain-specific language • Stratego • transform ASTs • working on ATerms • declarative style 2
    3. 3. Term Rewriting overview I 3
    4. 4. architecture Term Rewriting Modern Compilers in IDEs 4 Source Code
    5. 5. architecture Term Rewriting Modern Compilers in IDEs 4 Source Code parse
    6. 6. architecture Term Rewriting Modern Compilers in IDEs 4 Source Code parse check
    7. 7. architecture Term Rewriting Modern Compilers in IDEs 4 Source Code parse check
    8. 8. architecture Term Rewriting Modern Compilers in IDEs 4 Source Code parse generate Machine Code check
    9. 9. manual implementation Term Rewriting Traditional Compiler Compilers 5 Language Implementation compile Compiler
    10. 10. compilation + compilation Term Rewriting Traditional Compiler Compilers 6 Language Implementation compile Compiler Language Definition compile
    11. 11. compilation + interpretation Term Rewriting Traditional Compiler Compilers 7 interpret Interpreter Language Definition compile Language Implementation
    12. 12. compilation + interpretation Term Rewriting Spoofax Language Workbench 8 interpret Generic Parser Syntax Definition compile Parse Table
    13. 13. manual implementation Term Rewriting Spoofax Language Workbench Stratego • declarative DSL • domain: program transformation • means to manipulate ASTs • working on ATerms • compiles to C or Java Stratego in Spoofax • static analysis & error checking • code generation • semantic editor services 9
    14. 14. concepts Term Rewriting Stratego signatures rewrite rules • transform term to term • left-hand side • pattern matching & variable binding • right-hand side • pattern instantiation & variable substitution rewrite strategies • algorithm for applying rewrite rules 10
    15. 15. example Term Rewriting Stratego module desugar imports include/Tiger operators strategies desugar-all = innermost(desugar) rules desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) 11
    16. 16. Term Rewriting signatures II 12
    17. 17. context-free syntax "-" Exp -> Exp {cons("Uminus")} Exp "+" Exp -> Exp {cons("Plus")} Exp "-" Exp -> Exp {cons("Minus")} Exp "*" Exp -> Exp {cons("Times")} Exp "/" Exp -> Exp {cons("Divide")} Exp "=" Exp -> Exp {cons("Eq")} Exp "<>" Exp -> Exp {cons("Neq")} Exp ">" Exp -> Exp {cons("Gt")} Exp "<" Exp -> Exp {cons("Lt")} Exp ">=" Exp -> Exp {cons("Geq")} Exp "<=" Exp -> Exp {cons("Leq")} Exp "&" Exp -> Exp {cons("And")} Exp "|" Exp -> Exp {cons("Or")} Term Rewriting Signatures signature constructors Uminus : Exp -> Exp Plus : Exp * Exp -> Exp Minus : Exp * Exp -> Exp Times : Exp * Exp -> Exp Divide : Exp * Exp -> Exp Eq : Exp * Exp -> Exp Neq : Exp * Exp -> Exp Gt : Exp * Exp -> Exp Lt : Exp * Exp -> Exp Geq : Exp * Exp -> Exp Leq : Exp * Exp -> Exp And : Exp * Exp -> Exp Or : Exp * Exp -> Exp 13
    18. 18. signature Term Rewriting signature constructors Constructor : Cons -> Term Application : Cons * List(Term) -> Term List : List(Term) -> Term Tuple : List(Term) -> Term signature constructors Nil : -> List(a) Cons : a * List(a) -> List(a) Conc : List(a) * List(a) -> List(a) 14 ATerms in Stratego
    19. 19. Term Rewriting rewrite rules III 15
    20. 20. Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else ()
    21. 21. Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else () IfThen( Var("x") , Call( "printint" , [Var("x")] ) ) IfThenElse( Var("x") , Call( "printint" , [Var("x")] ) , NoVal() )
    22. 22. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else () IfThen( Var("x") , Call( "printint" , [Var("x")] ) ) IfThenElse( Var("x") , Call( "printint" , [Var("x")] ) , NoVal() )
    23. 23. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else () IfThen( Var("x") , Call( "printint" , [Var("x")] ) ) IfThenElse( Var("x") , Call( "printint" , [Var("x")] ) , NoVal() ) pattern matching
    24. 24. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else () IfThen( Var("x") , Call( "printint" , [Var("x")] ) ) IfThenElse( Var("x") , Call( "printint" , [Var("x")] ) , NoVal() ) pattern matching e1 e2
    25. 25. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else () IfThen( Var("x") , Call( "printint" , [Var("x")] ) ) IfThenElse( Var("x") , Call( "printint" , [Var("x")] ) , NoVal() ) pattern matching pattern instantiation e1 e2
    26. 26. desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) Term Rewriting Desugaring if x then printint(x) 16 if x then printint(x) else () IfThen( Var("x") , Call( "printint" , [Var("x")] ) ) IfThenElse( Var("x") , Call( "printint" , [Var("x")] ) , NoVal() ) pattern matching pattern instantiation e1 e2
    27. 27. desugar: Uminus(e) -> Bop(MINUS(), Int("0"), e) desugar: Plus(e1, e2) -> Bop(PLUS(), e1, e2) desugar: Minus(e1, e2) -> Bop(MINUS(), e1, e2) desugar: Times(e1, e2) -> Bop(MUL(), e1, e2) desugar: Divide(e1, e2) -> Bop(DIV(), e1, e2) desugar: Eq(e1, e2) -> Rop(EQ(), e1, e2) desugar: Neq(e1, e2) -> Rop(NE(), e1, e2) desugar: Leq(e1, e2) -> Rop(LE(), e1, e2) desugar: Lt(e1, e2) -> Rop(LT(), e1, e2) desugar: Geq(e1, e2) -> Rop(LE(), e2, e1) desugar: Gt(e1, e2) -> Rop(LT(), e2, e1) desugar: And(e1, e2) -> IfThenElse(e1, e2, Int("0")) desugar: Or(e1, e2) -> IfThenElse(e1, Int("1"), e2) Term Rewriting More Desugaring signature constructors PLUS: BinOp MINUS: BinOp MUL: BinOp DIV: BinOp EQ: RelOp NE: RelOp LE: RelOp LT: RelOp Bop: BinOp * Expr * Expr -> Expr Rop: RelOp * Expr * Expr -> Expr 17
    28. 28. Term Rewriting Constant Folding x := 21 + 21 + x 18 x := 42 + x
    29. 29. Term Rewriting Constant Folding x := 21 + 21 + x 18 x := 42 + x Assign( Var("x") , Plus( Plus( Int("21") , Int("21") ) , Var("x") ) ) Assign( Var("x") , Plus( Int("42") , Var("x") ) )
    30. 30. eval: Bop(PLUS(), Int(i1), Int(i2)) -> Int(i3) where <addS> (i1, i2) => i3 Term Rewriting Constant Folding x := 21 + 21 + x 18 x := 42 + x Assign( Var("x") , Plus( Plus( Int("21") , Int("21") ) , Var("x") ) ) Assign( Var("x") , Plus( Int("42") , Var("x") ) )
    31. 31. eval: Bop(PLUS(), Int(i1), Int(i2)) -> Int(<addS> (i1, i2)) eval: Bop(MINUS(), Int(i1), Int(i2)) -> Int(<subtS> (i1, i2)) eval: Bop(MUL(), Int(i1), Int(i2)) -> Int(<mulS> (i1, i2)) eval: Bop(DIV(), Int(i1), Int(i2)) -> Int(<divS> (i1, i2)) eval: Rop(EQ(), Int(i), Int(i)) -> Int("1") eval: Rop(EQ(), Int(i1), Int(i2)) -> Int("0") where not( <eq> (i1, i2) ) eval: Rop(NE(), Int(i), Int(i)) -> Int("0") eval: Rop(NE(), Int(i1), Int(i2)) -> Int("1") where not( <eq> (i1, i2) ) eval: Rop(LT(), Int(i1), Int(i2)) -> Int("1") where <ltS> (i1, i2) eval: Rop(LT(), Int(i1), Int(i2)) -> Int("0") where not( <ltS> (i1, i2) ) eval: Rop(LE(), Int(i1), Int(i2)) -> Int("1") where <leqS> (i1, i2) eval: Rop(LE(), Int(i1), Int(i2)) -> Int("0") where not( <leqS> (i1, i2)) Term Rewriting More Constant Folding 19
    32. 32. parameters Term Rewriting Rewrite Rules f(x,y|a,b): lhs -> rhs • strategy or rule parameters x,y • term parameters a,b • no matching f(|a,b): lhs -> rhs • optional strategy parameters f(x,y): lhs -> rhs • optional term parameters f: lhs -> rhs 20
    33. 33. example: map Term Rewriting Rules with Parameters 21 [ 1 , 2 , 3 ] [ 2 , 3 , 4 ] map(inc)
    34. 34. example: map Term Rewriting Rules with Parameters 21 [ 1 , 2 , 3 ] [ 2 , 3 , 4 ] map(inc) inc 1 2 3 2 3 4inc inc
    35. 35. example: map Term Rewriting Rules with Parameters map(s): [] -> [] map(s): [x|xs] -> [<s> x | <map(s)> xs] 21 [ 1 , 2 , 3 ] [ 2 , 3 , 4 ] map(inc) inc 1 2 3 2 3 4inc inc
    36. 36. example: zip Term Rewriting Rules with Parameters 22 [ 1 , 2 , 3 ] [ (1,4) , (2,5) , (3,6) ] [ 4 , 5 , 6 ]zip
    37. 37. example: zip Term Rewriting Rules with Parameters 22 [ 1 , 2 , 3 ] [ (1,4) , (2,5) , (3,6) ] [ 4 , 5 , 6 ]zip [ 1 , 2 , 3 ] [ 5 , 7 , 9 ] [ 4 , 5 , 6 ]zip(add)
    38. 38. example: zip Term Rewriting Rules with Parameters 22 [ 1 , 2 , 3 ] [ (1,4) , (2,5) , (3,6) ] [ 4 , 5 , 6 ]zip [ 1 , 2 , 3 ] [ 5 , 7 , 9 ] [ 4 , 5 , 6 ]zip(add) map(add)
    39. 39. example: zip Term Rewriting Rules with Parameters zip(s): ([],[]) -> [] zip(s): ([x|xs],[y|ys]) -> [<s> (x,y) | <zip(s)> (xs,ys)] zip = zip(id) 22 [ 1 , 2 , 3 ] [ (1,4) , (2,5) , (3,6) ] [ 4 , 5 , 6 ]zip [ 1 , 2 , 3 ] [ 5 , 7 , 9 ] [ 4 , 5 , 6 ]zip(add) map(add)
    40. 40. example: fold Term Rewriting Rules with Parameters 23 [1,2,3] foldr(!0,add) 6
    41. 41. example: fold Term Rewriting Rules with Parameters 23 [1,2,3] foldr(!0,add) 6 [] 0 [3] 3 [2,3] 56 [1,2,3]
    42. 42. example: fold Term Rewriting Rules with Parameters foldr(s1,s2): [] -> <s1> foldr(s1,s2): [x|xs] -> <s2> (x,<foldr(s1,s2)> xs) 23 [1,2,3] foldr(!0,add) 6 [] 0 [3] 3 [2,3] 56 [1,2,3]
    43. 43. example: inverse Term Rewriting Rules with Parameters 24 [1,2,3] inverse(|[]) [3,2,1]
    44. 44. example: inverse Term Rewriting Rules with Parameters 24 [1,2,3] inverse(|[]) [3,2,1] [2,3] [1][] [1,2,3] [3] [2,1] [3,2,1] []
    45. 45. example: inverse Term Rewriting Rules with Parameters inverse(|is): [] -> is inverse(|is): [x|xs] -> <inverse(|[x|is])> xs 24 [1,2,3] inverse(|[]) [3,2,1] [2,3] [1][] [1,2,3] [3] [2,1] [3,2,1] []
    46. 46. Term Rewriting 25 coffee break
    47. 47. Term Rewriting rewrite strategies IV 26
    48. 48. rules and strategies Term Rewriting Transformation Definitions rewrite rules • term to term • left-hand side matching • right-hand side instantiation • conditional • partial transformation rewrite strategies • rule selection • algorithm • composition of transformations 27
    49. 49. example Term Rewriting Stratego module desugar imports include/Tiger operators strategies desugar-all = innermost(desugar) rules desugar: IfThen(e1, e2) -> IfThenElse(e1, e2, NoVal()) 28
    50. 50. example Term Rewriting Stratego module eval imports include/Tiger operators desugar strategies eval-all = innermost(desugar + eval) rules eval: Bop(PLUS(),Int(i1),Int(i2)) -> Int(i3) where <addS> (i1, i2) => i3 29
    51. 51. Term Rewriting Strategy Combinators identity id 30
    52. 52. Term Rewriting Strategy Combinators identity id failure fail 30
    53. 53. Term Rewriting Strategy Combinators identity id failure fail sequential composition s1 ; s2 30
    54. 54. Term Rewriting Strategy Combinators identity id failure fail sequential composition s1 ; s2 deterministic choice s1 <+ s2 30
    55. 55. Term Rewriting Strategy Combinators identity id failure fail sequential composition s1 ; s2 deterministic choice s1 <+ s2 non-deterministic choice s1 + s2 30
    56. 56. Term Rewriting Strategy Combinators identity id failure fail sequential composition s1 ; s2 deterministic choice s1 <+ s2 non-deterministic choice s1 + s2 guarded choice s1 < s2 + s3 30
    57. 57. variables Term Rewriting Strategy Combinators pattern matching ?t 31
    58. 58. variables Term Rewriting Strategy Combinators pattern matching ?t pattern instantiation !t 31
    59. 59. variables Term Rewriting Strategy Combinators pattern matching ?t pattern instantiation !t strategy application <s> t ≣ !t ; s 31
    60. 60. variables Term Rewriting Strategy Combinators pattern matching ?t pattern instantiation !t strategy application <s> t ≣ !t ; s result matching s => t ≣ s ; ?t <s> t1 => t2 t2 := <s> t1 31
    61. 61. variables Term Rewriting Strategy Combinators pattern matching ?t pattern instantiation !t strategy application <s> t ≣ !t ; s result matching s => t ≣ s ; ?t <s> t1 => t2 t2 := <s> t1 variable scope {x,y: s} 31
    62. 62. rules and strategies Term Rewriting Strategy Combinators named rewrite rule l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2 32
    63. 63. rules and strategies Term Rewriting Strategy Combinators named rewrite rule l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2 unscoped rewrite rule (t1 -> t2 where s) ≣ ?t1 ; s ; !t2 32
    64. 64. rules and strategies Term Rewriting Strategy Combinators named rewrite rule l: t1 -> t2 where s ≣ l = ?t1 ; s ; !t2 unscoped rewrite rule (t1 -> t2 where s) ≣ ?t1 ; s ; !t2 strategy definition f(x,y|a,b) = s 32
    65. 65. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) 33
    66. 66. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) 33
    67. 67. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) alltd(s) = s <+ all(alltd(s)) 33
    68. 68. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) alltd(s) = s <+ all(alltd(s)) bottomup(s) = all(bottomup(s)) ; s innermost(s) = bottomup(try(s ; innermost(s))) oncetd(s) = s <+ one(oncetd(s)) contains(|t) = oncetd(?t) 33
    69. 69. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) alltd(s) = s <+ all(alltd(s)) bottomup(s) = all(bottomup(s)) ; s innermost(s) = bottomup(try(s ; innermost(s))) oncetd(s) = s <+ one(oncetd(s)) contains(|t) = oncetd(?t) 33
    70. 70. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) alltd(s) = s <+ all(alltd(s)) bottomup(s) = all(bottomup(s)) ; s innermost(s) = bottomup(try(s ; innermost(s))) oncetd(s) = s <+ one(oncetd(s)) contains(|t) = oncetd(?t) 33
    71. 71. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) alltd(s) = s <+ all(alltd(s)) bottomup(s) = all(bottomup(s)) ; s innermost(s) = bottomup(try(s ; innermost(s))) oncetd(s) = s <+ one(oncetd(s)) contains(|t) = oncetd(?t) 33
    72. 72. examples Term Rewriting Strategy Combinators try(s) = s <+ id repeat(s) = try(s ; repeat(s)) topdown(s) = s ; all(topdown(s)) alltd(s) = s <+ all(alltd(s)) bottomup(s) = all(bottomup(s)) ; s innermost(s) = bottomup(try(s ; innermost(s))) oncetd(s) = s <+ one(oncetd(s)) contains(|t) = oncetd(?t) 33
    73. 73. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(switch) 34 Const Mul Const 3 4 5 Const Add
    74. 74. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(switch) 34 Const Mul Const 3 4 5 Const Add1
    75. 75. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(switch) 35 Mul Const 3 Const 4 5 Const Add1
    76. 76. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(switch) 35 Mul Const 3 Const 4 5 Const Add1 2
    77. 77. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(switch) 36 Mul Const 3 Const 5 4 Const Add1 2
    78. 78. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(switch) 36 Mul Const 3 Const 5 4 Const Add1 2 3
    79. 79. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 37 Const Mul Const 3 4 5 Const Add
    80. 80. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 37 Const Mul Const 3 4 5 Const Add1
    81. 81. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 38 Mul Const 3 Const 4 5 Const Add1
    82. 82. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 38 Mul Const 3 Const 4 5 Const Add1 2
    83. 83. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2
    84. 84. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2 3
    85. 85. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2 3 4
    86. 86. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2 3 4 5
    87. 87. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2 3 4 5 6
    88. 88. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2 3 4 5 6 7
    89. 89. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) topdown(s) = s ; all(topdown(s)) topdown(try(switch)) 39 Mul Const 3 Const 5 4 Const Add1 2 3 4 5 6 7 8
    90. 90. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 40 Const Mul Const 3 4 5 Const Add
    91. 91. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 40 Const Mul Const 3 4 5 Const Add1
    92. 92. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 41 Mul Const 3 Const 4 5 Const Add1
    93. 93. example Term Rewriting Stratego switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 42 Const Mul Const 3 4 5 Const Add
    94. 94. example Term Rewriting Stratego switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 42 Const Mul Const 3 4 5 Const Add1
    95. 95. example Term Rewriting Stratego switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 42 Const Mul Const 3 4 5 Const Add1 2
    96. 96. example Term Rewriting Stratego switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 42 Const Mul Const 3 4 5 Const Add1 2 3
    97. 97. example Term Rewriting Stratego switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 42 Const Mul Const 3 4 5 Const Add1 2 3 4
    98. 98. example Term Rewriting Stratego switch: Mul(e1, e2) -> Mul(e2, e1) alltd(s) = s <+ all(alltd(s)) alltd(switch) 43 Const Mul Const 3 5 4 Const Add1 2 3 4
    99. 99. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(switch) 44 Const Mul Const 3 4 5 Const Add
    100. 100. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(switch) 44 Const Mul Const 3 4 5 Const Add1
    101. 101. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(switch) 44 Const Mul Const 3 4 5 Const Add1 2
    102. 102. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(switch) 44 Const Mul Const 3 4 5 Const Add1 2 3
    103. 103. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add
    104. 104. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1
    105. 105. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2
    106. 106. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2 3
    107. 107. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2 3 4
    108. 108. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2 3 4 5
    109. 109. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2 3 4 5 6
    110. 110. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2 3 4 5 6 7
    111. 111. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 45 Const Mul Const 3 4 5 Const Add1 2 3 4 5 6 7 8
    112. 112. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 46 Const Mul Const 3 4 5 Const Add1 2 3 4
    113. 113. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 47 Const Mul Const 3 5 4 Const Add1 2 3 4
    114. 114. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 48 Const Mul Const 3 5 4 Const Add1
    115. 115. Mul Const 3 Const 5 4 Const Add example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) bottomup(s) = all(bottomup(s)) ; s bottomup(try(switch)) 49 1
    116. 116. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add
    117. 117. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1
    118. 118. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2
    119. 119. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2 3
    120. 120. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2 3 4
    121. 121. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2 3 4 5
    122. 122. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2 3 4 5 6
    123. 123. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2 3 4 5 6 7
    124. 124. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 50 Const Mul Const 3 4 5 Const Add1 2 3 4 5 6 7 8
    125. 125. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 51 Const Mul Const 3 4 5 Const Add1 2 3 4
    126. 126. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 52 Const Mul Const 3 5 4 Const Add1 2 3 4
    127. 127. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 52 Const Mul Const 3 5 4 Const Add1 2 3 4 9
    128. 128. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 52 Const Mul Const 3 5 4 Const Add1 2 3 4 9 10
    129. 129. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 52 Const Mul Const 3 5 4 Const Add1 2 3 4 9 10 11
    130. 130. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 52 Const Mul Const 3 5 4 Const Add1 2 3 4 9 10 11 12
    131. 131. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 53 Const Mul Const 3 5 4 Const Add1 2 3 4
    132. 132. example Term Rewriting Stratego switch: Add(e1, e2) -> Add(e2, e1) switch: Mul(e1, e2) -> Mul(e2, e1) innermost(s) = bottomup(try(s ; innermost(s))) innermost(switch) 54 Const Mul Const 3 4 5 Const Add1 2 3 4
    133. 133. Term Rewriting summary V 55
    134. 134. lessons learned Term Rewriting Summary 56
    135. 135. lessons learned Term Rewriting Summary How do you define AST transformations in Stratego? • signatures • rewrite rules • rewrite strategies • strategy combinators 56
    136. 136. lessons learned Term Rewriting Summary How do you define AST transformations in Stratego? • signatures • rewrite rules • rewrite strategies • strategy combinators What kind of strategies can you find in the Stratego library? • arithmetics • map, zip, foldr • generic traversals 56
    137. 137. learn more Term Rewriting Literature 57
    138. 138. learn more Term Rewriting Literature Spoofax Lennart C. L. Kats, Eelco Visser: The Spoofax Language Workbench. Rules for Declarative Specification of Languages and IDEs. OOPSLA 2010 http://www.spoofax.org 57
    139. 139. learn more Term Rewriting Literature Spoofax Lennart C. L. Kats, Eelco Visser: The Spoofax Language Workbench. Rules for Declarative Specification of Languages and IDEs. OOPSLA 2010 http://www.spoofax.org Stratego Martin Bravenboer, Karl Trygve Kalleberg, Rob Vermaas, Eelco Visser: Stratego/XT 0.17. A language and toolset for program transformation. Science of Computer Programming, 72(1-2), 2008. http://www.strategoxt.org 57
    140. 140. coming next Term Rewriting Outlook declarative semantics definition • Lecture 5: Static Analysis and Error Checking • Lecture 6: Code Generation Assignment Oct 2: MiniJava grammar in SDF • submit on Blackboard Lab Oct 03 • syntactic editor services • pretty-printer for MiniJava 58
    141. 141. Term Rewriting 59
    142. 142. attribution & copyrights Term Rewriting Pictures Slides 1, 9, 10, 20, 27: Stratego by Kendrick Arnett, some rights reserved Slides 23-26, 28, 31, 37-39, 41-43: PICOL icons by Melih Bilgil, some rights reserved Slide 14: Thesaurus by Enoch Lau, some rights reserved Slide 21: Google Maps Street View Camera by Ian Britton, some rights reserved Slide 22: Zipper by Rabensteiner, some rights reserved Slide 23: Fold by Kim Piper Werker, some rights reserved Slide 24: Inverse by lanchongzi, some rights reserved Slide 25: Maxwell's by Dominica Williamson, some rights reserved Slide 58: Reichstag by Wolfgang Staudt, some rights reserved 60
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×