SlideShare uma empresa Scribd logo
1 de 36
Baixar para ler offline
Strategies for Online Partial Evaluation
Program Transformation 2004–2005

Eelco Visser
Institute of Information & Computing Sciences
Utrecht University
The Netherlands

March 17, 2005
Online Partial Evaluation
Goal
Inputs: program, inputs for some arguments
Output: program specialized to these inputs, optimized as
much as possible
Simplification
Input: program with constant expressions
Output: program specialized to constant expressions,
optimized as much as possible
Online vs Offline
Online: decision to specialize based on program + inputs
Offline: decision to specialized based on program +
declaration of static inputs

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Outline
Refine constant propagation strategy to online specializer
Specialize 0: constant propagation
Specialize 1: function unfolding
Specialize 2: unfold only static calls
Specialize 3: memoize call unfolding
Specialize 4: specialization of function definitions
Specialize 5: reduction of specialized functions

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 0: Constant Propagation
pe = PropConst <+ pe-assign <+ pe-declare
<+ pe-let <+ pe-if <+ pe-while <+ pe-for
<+ all(pe); try(EvalBinOp <+ EvalRelOp <+ EvalString)
pe-assign =
|[ x := <pe => e> ]|
; if <is-value> e
then rules( PropConst.x : |[ x ]| -> |[ e ]| )
else rules( PropConst.x :- |[ x ]| ) end
pe-declare =
? |[ var x ta ]|
; rules( PropConst+x :- |[ x ]| )
pe-declare =
|[ var x ta := <pe => e> ]|
; if <is-value> e
then rules( PropConst+x : |[ x ]| -> |[ e ]| )
else rules( PropConst+x :- |[ x ]| ) end
pe-let =
|[ let <*id> in <*id> end ]|
; {| PropConst : all(pe) |}
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 0: Constant Propagation (control flow)
pe-if =
|[ if <pe> then <id> ]|
; (EvalIf <+ (|[ if <id> then <pe> ]| /PropConst id))
pe-if =
|[ if <pe> then <id> else <id> ]|
; (EvalIf; pe
<+ (|[ if <id> then <pe> else <id> ]|
/PropConst |[ if <id> then <id> else <pe> ]|))
pe-while =
|[ while <id> do <id> ]|
; (|[ while <pe> do <id> ]|; EvalWhile
<+ /PropConst* |[ while <pe> do <pe> ]|)

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(fact(10))
end

⇓
let function fact(n : int) : int =
if n < 1 then 1 else n * fact(n - 1)
in printint(3628800)
end

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding
Replace call and substitute arguments
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(fact(10))
end

⇓
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(
if 10 < 1 then 1 else (10 * fact(10 - 1))
)
end

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Substitution
pe = PropConst <+ {| PropConst : UnfoldCall; pe |} <+ ...
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ e ]|
where <zip(SubstArg)> (x*, a*) => d*
)
SubstArg =
?(FArg|[ x ta ]|, e)
; rules( PropConst : |[ x ]| -> |[ e ]| )

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Substitution
pe = PropConst <+ {| PropConst : UnfoldCall; pe |} <+ ...
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ e ]|
where <zip(SubstArg)> (x*, a*) => d*
)
SubstArg =
?(FArg|[ x ta ]|, e)
; rules( PropConst : |[ x ]| -> |[ e ]| )

Substitution may lead to duplication of computations and side
effects.
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Let Binding
pe = PropConst <+ UnfoldCall; pe <+ ...
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ let d* in e end ]|
where <zip(BindArg)> (x*, a*) => d*
)
BindArg :
(FArg|[ x ta ]|, e) -> |[ var x ta := e ]|

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Let Binding
pe = PropConst <+ UnfoldCall; pe <+ ...
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ let d* in e end ]|
where <zip(BindArg)> (x*, a*) => d*
)
BindArg :
(FArg|[ x ta ]|, e) -> |[ var x ta := e ]|

Binding expressions to variable and subsequent constant
propagation have same effect as substitution, but is safe.

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Replace call by body
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(fact(10))
end

⇓
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(
let var n := 10
in if n < 1 then 1 else (n * fact(n - 1))
end)
end

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Constant propagation
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(
let var n := 10
in if n < 1 then 1 else (n * fact(n - 1))
end)
end

⇓
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
in printint(
let var n := 10
in 10 * fact(9)
end)
end
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Constant Fold Let
EvalLet :
|[ let d* in i end ]| -> |[ i ]|
pe-let =
|[ let <*id> in <*id> end ]|
; {| PropConst : all(pe) |}
; try(EvalLet)

If let body reduces to a constant value, the bindings are dead.

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 1: Function Unfolding — Cleaning up
pe-let =
|[ let <*id> in <*id> end ]|
; {| PropConst, UnfoldCall : all(pe) |}
; |[ let <*filter(not(DeadBinding))> in <*id> end ]|
; try(EvalLet)
DeadBinding =
?|[ var x ta := x ]|
DeadBinding =
?|[ var x ta := i ]|
EvalLet :
|[ let d* in i end ]| -> |[ i ]|

Remove useless variable bindings
http://www.strategoxt.org

Strategies for Online Partial Evaluation
let ...
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(readint(), 5))
end

⇓
in printint(
let var x : int := readint()
in x * let var x : int :=
let var x : int := x * 1
in x * x end
in x * x end
end)
end

Specialize 1
http://www.strategoxt.org

Strategies for Online Partial Evaluation
let function square(x : int) : int =
x * x
function mod(x : int, y : int) : int =
x - (x / y) * y
function even(x : int) : int =
mod(x, 2) = 0
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(6, readint())); print("n")
end

Problem: Specialize 1 does not terminate for many programs
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 2: Unfold only calls with all static arguments
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ let d* in e end ]|
where <map(is-value)> a*
; <zip(BindArg)> (x*, a*) => d*
)

http://www.strategoxt.org

Strategies for Online Partial Evaluation
let
function fibonacci(n:int):int =
if (n >= 2) then
fibonacci(n-1) + fibonacci(n-2)
else if (n = 1) then
1
else 0
in printint(fibonacci(30))
end

Problem: re-evaluation of same static calls

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize 3: memoize call unfoldings
unfold-call :
|[ f(a*) ]| -> |[ e ]|
where <RetrieveUnfolding> |[ f(a*) ]| => |[ e ]|
<+ <UnfoldCall; pe> |[ f(a*) ]| => |[ e ]|
; rules(
RetrieveUnfolding.f : |[ f(a*) ]| -> |[ e ]|
)
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ let d* in e end ]|
where <map(is-value)> a*
; <zip(BindArg)> (x*, a*) => d*
RetrieveUnfolding+f
)
http://www.strategoxt.org

Strategies for Online Partial Evaluation
memoization works
fib
15
17
20

Specialize2
0m1.656s
0m3.955s
0m14.906s

http://www.strategoxt.org

Specialize3
0m0.219s
0m0.227s
0m0.232s

Strategies for Online Partial Evaluation
let function square(x : int) : int =
x * x
function mod(x : int, y : int) : int =
x - (x / y) * y
function even(x : int) : int =
mod(x, 2) = 0
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(readint(), 5)); print("n")
end

Problem: Specialize 3 does not specialize partially static calls
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize4: specialization of function definitions
pe = ... <+ all(pe); try(... <+ SpecializeCall)
pe-declare =
?|[ function f(x*) ta = e ]|
; rules(
UnfoldCall :
|[ f(a*) ]| -> |[ let d* in e end ]|
where <map(is-value)> a*
; <zip(BindArg)> (x*, a*) => d*
RetrieveUnfolding+f
Specialization+f
SpecializeCall :
|[ f(a1*) ]| -> |[ g(a2*) ]|
where // next slide
)

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize4: specialize function to static arguments
pe-declare = ?|[ function f(x*) ta = e ]|;
rules(
SpecializeCall :
|[ f(a1*) ]| -> |[ g(a2*) ]|
where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*))
; new => g; !e => e2
; rules(
Specialization.f :+
|[ function f(x*) ta = e’ ]| ->
|[ function g(x2*) ta = let d* in e2 end ]|
)
)
split-static-dynamic-args =
zip; partition(BindArgValue); (not([]), unzip)
BindArgValue :
(FArg|[ x ta ]|, e) -> |[ var x ta := e ]| where <is-value> e

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize4: specialize function to static arguments
pe-declare = ?|[ function f(x*) ta = e ]|;
rules(
SpecializeCall :
|[ f(a1*) ]| -> |[ g(a2*) ]|
where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*))
; new => g; !e => e2
; rules(
Specialization.f :+
|[ function f(x*) ta = e’ ]| ->
|[ function g(x2*) ta = let d* in e2 end ]|
)
)
split-static-dynamic-args =
zip; partition(BindArgValue); (not([]), unzip)
BindArgValue :
(FArg|[ x ta ]|, e) -> |[ var x ta := e ]| where <is-value> e

separate static from dynamic arguments

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize4: specialize function to static arguments
pe-declare = ?|[ function f(x*) ta = e ]|;
rules(
SpecializeCall :
|[ f(a1*) ]| -> |[ g(a2*) ]|
where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*))
; new => g; !e => e2
; rules(
Specialization.f :+
|[ function f(x*) ta = e’ ]| ->
|[ function g(x2*) ta = let d* in e2 end ]|
)
)
split-static-dynamic-args =
zip; partition(BindArgValue); (not([]), unzip)
BindArgValue :
(FArg|[ x ta ]|, e) -> |[ var x ta := e ]| where <is-value> e

separate static from dynamic arguments
R :+ l -> r — dynamic rule with multiple right-hand sides
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize4: replace functions with their specialization
pe-let =
|[ let <*id> in <*id> end ]|
; {| PropConst, UnfoldCall, RetrieveUnfolding, Specialization :
all(pe)
; |[ let <*filter(|[ <fd*:mapconcat(bagof-Specialization)> ]|
<+ not(DeadBinding))>
in <*id> end ]|
|}
; try(EvalLet)

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize4: replace functions with their specialization
pe-let =
|[ let <*id> in <*id> end ]|
; {| PropConst, UnfoldCall, RetrieveUnfolding, Specialization :
all(pe)
; |[ let <*filter(|[ <fd*:mapconcat(bagof-Specialization)> ]|
<+ not(DeadBinding))>
in <*id> end ]|
|}
; try(EvalLet)

bagof-R: all rewritings of R

http://www.strategoxt.org

Strategies for Online Partial Evaluation
let function square(x : int) : int =
x * x
function mod(x : int, y : int) : int =
x - (x / y) * y
function even(x : int) : int =
mod(x, 2) = 0
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(readint(), 5))
end

⇓
let function a_0(x : int) : int =
let var n : int := 5
in if n= 0 then 1
else if even(n) then square(power(x, n / 2))
else x * power(x, n - 1)
end
in printint(a_0(readint()))
end

http://www.strategoxt.org

Strategies for Online Partial Evaluation
let function square(x : int) : int =
x * x
function mod(x : int, y : int) : int =
x - (x / y) * y
function even(x : int) : int =
mod(x, 2) = 0
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(readint(), 5))
end

⇓
let function a_0(x : int) : int =
let var n : int := 5
in if n= 0 then 1
else if even(n) then square(power(x, n / 2))
else x * power(x, n - 1)
end
in printint(a_0(readint()))
end

Problem: body of specialized function is not transformed
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize5: reduce body of specialized function
pe-declare = ?|[ function f(x*) ta = e ]|;
rules(
...
SpecializeCall :
|[ f(a1*) ]| -> |[ g(a2*) ]|
where <split-static-dynamic-args>
(x*, a1*) => (d*, (x2*, a2*))
; new => g
; <pe>|[ let d* in e end ]| => e2
; rules(
Specialization.f :+
|[ function f(x*) ta = e’ ]| ->
|[ function g(x2*) ta = e2 ]|
)
)

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Specialize5: reduce body of specialized function
pe-declare = ?|[ function f(x*) ta = e ]|;
rules(
...
SpecializeCall :
|[ f(a1*) ]| -> |[ g(a2*) ]|
where <split-static-dynamic-args>
(x*, a1*) => (d*, (x2*, a2*))
; new => g
; <pe>|[ let d* in e end ]| => e2
; rules(
Specialization.f :+
|[ function f(x*) ta = e’ ]| ->
|[ function g(x2*) ta = e2 ]|
)
)

transform instantiated body before declaring specialization
http://www.strategoxt.org

Strategies for Online Partial Evaluation
let function square(x : int) : int =
x * x
function mod(x : int, y : int) : int =
x - (x / y) * y
function even(x : int) : int =
mod(x, 2) = 0
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(readint(), 5))
end

⇓
let function a_0(x : int) : int
function d_0(x : int) : int
function e_0(x : int) : int
function g_0(x : int) : int
function h_0(x : int) : int
in printint(a_0(readint()));
print("n")
end

=
=
=
=
=

x * d_0(x)
square(e_0(x))
square(g_0(x))
x * h_0(x)
1

http://www.strategoxt.org

Strategies for Online Partial Evaluation
let function square(x : int) : int =
x * x
function mod(x : int, y : int) : int =
x - (x / y) * y
function even(x : int) : int =
mod(x, 2) = 0
function power(x : int, n : int) : int =
if n = 0 then 1
else if even(n) then square(power(x, n/2))
else (x * power(x, n - 1))
in printint(power(readint(), 5))
end

⇓
let function a_0(x : int) : int
function d_0(x : int) : int
function e_0(x : int) : int
function g_0(x : int) : int
function h_0(x : int) : int
in printint(a_0(readint()));
print("n")
end

=
=
=
=
=

x * d_0(x)
square(e_0(x))
square(g_0(x))
x * h_0(x)
1

Problem: lots of ‘trivial’ functions generated
http://www.strategoxt.org

Strategies for Online Partial Evaluation
Cliffhanger ...
Try to unfold as many functions as possible
Doing to much will lead to non-termination
How to control unfolding?

http://www.strategoxt.org

Strategies for Online Partial Evaluation
Cliffhanger ...
Try to unfold as many functions as possible
Doing to much will lead to non-termination
How to control unfolding?
Next week:
Memoization to catch calls to specializations in progress
Offline partial evaluation: use binding time analysis to decide
which calls to unfold and which to specialize

http://www.strategoxt.org

Strategies for Online Partial Evaluation

Mais conteúdo relacionado

Mais procurados

Code Optimization
Code OptimizationCode Optimization
Code Optimization
guest9f8315
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
SFilipp
 

Mais procurados (18)

Parameters
ParametersParameters
Parameters
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
Berlin meetup
Berlin meetupBerlin meetup
Berlin meetup
 
Les nouveautés de C# 6
Les nouveautés de C# 6Les nouveautés de C# 6
Les nouveautés de C# 6
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
Hybrid Multi-level Crossover for Unit Test Case Generation (SSBSE 2021)
Hybrid Multi-level Crossover for Unit Test Case Generation (SSBSE 2021)Hybrid Multi-level Crossover for Unit Test Case Generation (SSBSE 2021)
Hybrid Multi-level Crossover for Unit Test Case Generation (SSBSE 2021)
 
Pure Future
Pure FuturePure Future
Pure Future
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
 
1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional
 
Presentation1 computer shaan
Presentation1 computer shaanPresentation1 computer shaan
Presentation1 computer shaan
 
Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potter
 
C#
C#C#
C#
 
Flying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightFlying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnight
 
.net progrmming part1
.net progrmming part1.net progrmming part1
.net progrmming part1
 
Programação reativa e o actor model
Programação reativa e o actor modelProgramação reativa e o actor model
Programação reativa e o actor model
 

Semelhante a Online partial evaluation

“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1
SethTisue
 
Reasonable Code With Fsharp
Reasonable Code With FsharpReasonable Code With Fsharp
Reasonable Code With Fsharp
Michael Falanga
 

Semelhante a Online partial evaluation (20)

Scoped dynamic rewrite rules
Scoped dynamic rewrite rulesScoped dynamic rewrite rules
Scoped dynamic rewrite rules
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Declare Your Language: Dynamic Semantics
Declare Your Language: Dynamic SemanticsDeclare Your Language: Dynamic Semantics
Declare Your Language: Dynamic Semantics
 
Dafunctor
DafunctorDafunctor
Dafunctor
 
Java parallel programming made simple
Java parallel programming made simpleJava parallel programming made simple
Java parallel programming made simple
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
Being functional in PHP
Being functional in PHPBeing functional in PHP
Being functional in PHP
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For Beginners
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
Dependent dynamic rules
Dependent dynamic rulesDependent dynamic rules
Dependent dynamic rules
 
“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
 
Reasonable Code With Fsharp
Reasonable Code With FsharpReasonable Code With Fsharp
Reasonable Code With Fsharp
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
Write an algorithm for a program that shows the use of all six math fu.docx
Write an algorithm for a program that shows the use of all six math fu.docxWrite an algorithm for a program that shows the use of all six math fu.docx
Write an algorithm for a program that shows the use of all six math fu.docx
 

Mais de Eelco Visser

Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
 

Mais de Eelco Visser (20)

CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Online partial evaluation

  • 1. Strategies for Online Partial Evaluation Program Transformation 2004–2005 Eelco Visser Institute of Information & Computing Sciences Utrecht University The Netherlands March 17, 2005
  • 2. Online Partial Evaluation Goal Inputs: program, inputs for some arguments Output: program specialized to these inputs, optimized as much as possible Simplification Input: program with constant expressions Output: program specialized to constant expressions, optimized as much as possible Online vs Offline Online: decision to specialize based on program + inputs Offline: decision to specialized based on program + declaration of static inputs http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 3. Outline Refine constant propagation strategy to online specializer Specialize 0: constant propagation Specialize 1: function unfolding Specialize 2: unfold only static calls Specialize 3: memoize call unfolding Specialize 4: specialization of function definitions Specialize 5: reduction of specialized functions http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 4. Specialize 0: Constant Propagation pe = PropConst <+ pe-assign <+ pe-declare <+ pe-let <+ pe-if <+ pe-while <+ pe-for <+ all(pe); try(EvalBinOp <+ EvalRelOp <+ EvalString) pe-assign = |[ x := <pe => e> ]| ; if <is-value> e then rules( PropConst.x : |[ x ]| -> |[ e ]| ) else rules( PropConst.x :- |[ x ]| ) end pe-declare = ? |[ var x ta ]| ; rules( PropConst+x :- |[ x ]| ) pe-declare = |[ var x ta := <pe => e> ]| ; if <is-value> e then rules( PropConst+x : |[ x ]| -> |[ e ]| ) else rules( PropConst+x :- |[ x ]| ) end pe-let = |[ let <*id> in <*id> end ]| ; {| PropConst : all(pe) |} http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 5. Specialize 0: Constant Propagation (control flow) pe-if = |[ if <pe> then <id> ]| ; (EvalIf <+ (|[ if <id> then <pe> ]| /PropConst id)) pe-if = |[ if <pe> then <id> else <id> ]| ; (EvalIf; pe <+ (|[ if <id> then <pe> else <id> ]| /PropConst |[ if <id> then <id> else <pe> ]|)) pe-while = |[ while <id> do <id> ]| ; (|[ while <pe> do <id> ]|; EvalWhile <+ /PropConst* |[ while <pe> do <pe> ]|) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 6. Specialize 1: Function Unfolding let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint(fact(10)) end ⇓ let function fact(n : int) : int = if n < 1 then 1 else n * fact(n - 1) in printint(3628800) end http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 7. Specialize 1: Function Unfolding Replace call and substitute arguments let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint(fact(10)) end ⇓ let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint( if 10 < 1 then 1 else (10 * fact(10 - 1)) ) end http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 8. Specialize 1: Function Unfolding — Substitution pe = PropConst <+ {| PropConst : UnfoldCall; pe |} <+ ... pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ e ]| where <zip(SubstArg)> (x*, a*) => d* ) SubstArg = ?(FArg|[ x ta ]|, e) ; rules( PropConst : |[ x ]| -> |[ e ]| ) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 9. Specialize 1: Function Unfolding — Substitution pe = PropConst <+ {| PropConst : UnfoldCall; pe |} <+ ... pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ e ]| where <zip(SubstArg)> (x*, a*) => d* ) SubstArg = ?(FArg|[ x ta ]|, e) ; rules( PropConst : |[ x ]| -> |[ e ]| ) Substitution may lead to duplication of computations and side effects. http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 10. Specialize 1: Function Unfolding — Let Binding pe = PropConst <+ UnfoldCall; pe <+ ... pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ let d* in e end ]| where <zip(BindArg)> (x*, a*) => d* ) BindArg : (FArg|[ x ta ]|, e) -> |[ var x ta := e ]| http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 11. Specialize 1: Function Unfolding — Let Binding pe = PropConst <+ UnfoldCall; pe <+ ... pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ let d* in e end ]| where <zip(BindArg)> (x*, a*) => d* ) BindArg : (FArg|[ x ta ]|, e) -> |[ var x ta := e ]| Binding expressions to variable and subsequent constant propagation have same effect as substitution, but is safe. http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 12. Specialize 1: Function Unfolding — Replace call by body let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint(fact(10)) end ⇓ let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint( let var n := 10 in if n < 1 then 1 else (n * fact(n - 1)) end) end http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 13. Specialize 1: Function Unfolding — Constant propagation let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint( let var n := 10 in if n < 1 then 1 else (n * fact(n - 1)) end) end ⇓ let function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1)) in printint( let var n := 10 in 10 * fact(9) end) end http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 14. Specialize 1: Function Unfolding — Constant Fold Let EvalLet : |[ let d* in i end ]| -> |[ i ]| pe-let = |[ let <*id> in <*id> end ]| ; {| PropConst : all(pe) |} ; try(EvalLet) If let body reduces to a constant value, the bindings are dead. http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 15. Specialize 1: Function Unfolding — Cleaning up pe-let = |[ let <*id> in <*id> end ]| ; {| PropConst, UnfoldCall : all(pe) |} ; |[ let <*filter(not(DeadBinding))> in <*id> end ]| ; try(EvalLet) DeadBinding = ?|[ var x ta := x ]| DeadBinding = ?|[ var x ta := i ]| EvalLet : |[ let d* in i end ]| -> |[ i ]| Remove useless variable bindings http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 16. let ... function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(readint(), 5)) end ⇓ in printint( let var x : int := readint() in x * let var x : int := let var x : int := x * 1 in x * x end in x * x end end) end Specialize 1 http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 17. let function square(x : int) : int = x * x function mod(x : int, y : int) : int = x - (x / y) * y function even(x : int) : int = mod(x, 2) = 0 function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(6, readint())); print("n") end Problem: Specialize 1 does not terminate for many programs http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 18. Specialize 2: Unfold only calls with all static arguments pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ let d* in e end ]| where <map(is-value)> a* ; <zip(BindArg)> (x*, a*) => d* ) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 19. let function fibonacci(n:int):int = if (n >= 2) then fibonacci(n-1) + fibonacci(n-2) else if (n = 1) then 1 else 0 in printint(fibonacci(30)) end Problem: re-evaluation of same static calls http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 20. Specialize 3: memoize call unfoldings unfold-call : |[ f(a*) ]| -> |[ e ]| where <RetrieveUnfolding> |[ f(a*) ]| => |[ e ]| <+ <UnfoldCall; pe> |[ f(a*) ]| => |[ e ]| ; rules( RetrieveUnfolding.f : |[ f(a*) ]| -> |[ e ]| ) pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ let d* in e end ]| where <map(is-value)> a* ; <zip(BindArg)> (x*, a*) => d* RetrieveUnfolding+f ) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 22. let function square(x : int) : int = x * x function mod(x : int, y : int) : int = x - (x / y) * y function even(x : int) : int = mod(x, 2) = 0 function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(readint(), 5)); print("n") end Problem: Specialize 3 does not specialize partially static calls http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 23. Specialize4: specialization of function definitions pe = ... <+ all(pe); try(... <+ SpecializeCall) pe-declare = ?|[ function f(x*) ta = e ]| ; rules( UnfoldCall : |[ f(a*) ]| -> |[ let d* in e end ]| where <map(is-value)> a* ; <zip(BindArg)> (x*, a*) => d* RetrieveUnfolding+f Specialization+f SpecializeCall : |[ f(a1*) ]| -> |[ g(a2*) ]| where // next slide ) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 24. Specialize4: specialize function to static arguments pe-declare = ?|[ function f(x*) ta = e ]|; rules( SpecializeCall : |[ f(a1*) ]| -> |[ g(a2*) ]| where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*)) ; new => g; !e => e2 ; rules( Specialization.f :+ |[ function f(x*) ta = e’ ]| -> |[ function g(x2*) ta = let d* in e2 end ]| ) ) split-static-dynamic-args = zip; partition(BindArgValue); (not([]), unzip) BindArgValue : (FArg|[ x ta ]|, e) -> |[ var x ta := e ]| where <is-value> e http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 25. Specialize4: specialize function to static arguments pe-declare = ?|[ function f(x*) ta = e ]|; rules( SpecializeCall : |[ f(a1*) ]| -> |[ g(a2*) ]| where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*)) ; new => g; !e => e2 ; rules( Specialization.f :+ |[ function f(x*) ta = e’ ]| -> |[ function g(x2*) ta = let d* in e2 end ]| ) ) split-static-dynamic-args = zip; partition(BindArgValue); (not([]), unzip) BindArgValue : (FArg|[ x ta ]|, e) -> |[ var x ta := e ]| where <is-value> e separate static from dynamic arguments http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 26. Specialize4: specialize function to static arguments pe-declare = ?|[ function f(x*) ta = e ]|; rules( SpecializeCall : |[ f(a1*) ]| -> |[ g(a2*) ]| where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*)) ; new => g; !e => e2 ; rules( Specialization.f :+ |[ function f(x*) ta = e’ ]| -> |[ function g(x2*) ta = let d* in e2 end ]| ) ) split-static-dynamic-args = zip; partition(BindArgValue); (not([]), unzip) BindArgValue : (FArg|[ x ta ]|, e) -> |[ var x ta := e ]| where <is-value> e separate static from dynamic arguments R :+ l -> r — dynamic rule with multiple right-hand sides http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 27. Specialize4: replace functions with their specialization pe-let = |[ let <*id> in <*id> end ]| ; {| PropConst, UnfoldCall, RetrieveUnfolding, Specialization : all(pe) ; |[ let <*filter(|[ <fd*:mapconcat(bagof-Specialization)> ]| <+ not(DeadBinding))> in <*id> end ]| |} ; try(EvalLet) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 28. Specialize4: replace functions with their specialization pe-let = |[ let <*id> in <*id> end ]| ; {| PropConst, UnfoldCall, RetrieveUnfolding, Specialization : all(pe) ; |[ let <*filter(|[ <fd*:mapconcat(bagof-Specialization)> ]| <+ not(DeadBinding))> in <*id> end ]| |} ; try(EvalLet) bagof-R: all rewritings of R http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 29. let function square(x : int) : int = x * x function mod(x : int, y : int) : int = x - (x / y) * y function even(x : int) : int = mod(x, 2) = 0 function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(readint(), 5)) end ⇓ let function a_0(x : int) : int = let var n : int := 5 in if n= 0 then 1 else if even(n) then square(power(x, n / 2)) else x * power(x, n - 1) end in printint(a_0(readint())) end http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 30. let function square(x : int) : int = x * x function mod(x : int, y : int) : int = x - (x / y) * y function even(x : int) : int = mod(x, 2) = 0 function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(readint(), 5)) end ⇓ let function a_0(x : int) : int = let var n : int := 5 in if n= 0 then 1 else if even(n) then square(power(x, n / 2)) else x * power(x, n - 1) end in printint(a_0(readint())) end Problem: body of specialized function is not transformed http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 31. Specialize5: reduce body of specialized function pe-declare = ?|[ function f(x*) ta = e ]|; rules( ... SpecializeCall : |[ f(a1*) ]| -> |[ g(a2*) ]| where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*)) ; new => g ; <pe>|[ let d* in e end ]| => e2 ; rules( Specialization.f :+ |[ function f(x*) ta = e’ ]| -> |[ function g(x2*) ta = e2 ]| ) ) http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 32. Specialize5: reduce body of specialized function pe-declare = ?|[ function f(x*) ta = e ]|; rules( ... SpecializeCall : |[ f(a1*) ]| -> |[ g(a2*) ]| where <split-static-dynamic-args> (x*, a1*) => (d*, (x2*, a2*)) ; new => g ; <pe>|[ let d* in e end ]| => e2 ; rules( Specialization.f :+ |[ function f(x*) ta = e’ ]| -> |[ function g(x2*) ta = e2 ]| ) ) transform instantiated body before declaring specialization http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 33. let function square(x : int) : int = x * x function mod(x : int, y : int) : int = x - (x / y) * y function even(x : int) : int = mod(x, 2) = 0 function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(readint(), 5)) end ⇓ let function a_0(x : int) : int function d_0(x : int) : int function e_0(x : int) : int function g_0(x : int) : int function h_0(x : int) : int in printint(a_0(readint())); print("n") end = = = = = x * d_0(x) square(e_0(x)) square(g_0(x)) x * h_0(x) 1 http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 34. let function square(x : int) : int = x * x function mod(x : int, y : int) : int = x - (x / y) * y function even(x : int) : int = mod(x, 2) = 0 function power(x : int, n : int) : int = if n = 0 then 1 else if even(n) then square(power(x, n/2)) else (x * power(x, n - 1)) in printint(power(readint(), 5)) end ⇓ let function a_0(x : int) : int function d_0(x : int) : int function e_0(x : int) : int function g_0(x : int) : int function h_0(x : int) : int in printint(a_0(readint())); print("n") end = = = = = x * d_0(x) square(e_0(x)) square(g_0(x)) x * h_0(x) 1 Problem: lots of ‘trivial’ functions generated http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 35. Cliffhanger ... Try to unfold as many functions as possible Doing to much will lead to non-termination How to control unfolding? http://www.strategoxt.org Strategies for Online Partial Evaluation
  • 36. Cliffhanger ... Try to unfold as many functions as possible Doing to much will lead to non-termination How to control unfolding? Next week: Memoization to catch calls to specializations in progress Offline partial evaluation: use binding time analysis to decide which calls to unfold and which to specialize http://www.strategoxt.org Strategies for Online Partial Evaluation