continuation calculus at term rewriting seminar
TRANSCRIPT
Continuation calculus
Bram Geron1 Herman Geuvers1,2
1Eindhoven University of Technology
2Radboud University Nijmegen
Term Rewriting Seminar, May 2013
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 1 / 22
Outline
1 First look
2 Definition
3 Long-term goal
4 Interfaces, not pattern matching
5 The importance of head reduction
6 Relation to lambda calculus
7 Conclusion
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 2 / 22
First look Rules, names, variables, terms
CC is a constrained term rewriting system
Comp.f .g .x def−→ f .(g .x)
Comp.AddOne.Fact.4 → AddOne.(Fact.4)
Rules
One definition per name(max)No pattern matchingOnly head reduction
Consequences
DeterministicSimple operational semanticsSuitable for modelingcontinuations, hence exceptions
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 3 / 22
First look Rules, names, variables, terms
CC is a constrained term rewriting systemrule
Compname
(constant)
.f .g .xvariables
def−→ f .(g .x)
Comp.AddOne.Fact.4term
→ AddOne.(Fact.4)term
Rules
One definition per name(max)No pattern matchingOnly head reduction
Consequences
DeterministicSimple operational semanticsSuitable for modelingcontinuations, hence exceptions
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 3 / 22
First look Rules, names, variables, terms
CC is a constrained term rewriting systemrule
Compname
(constant)
.f .g .xvariables
def−→ f .(g .x)
Comp.AddOne.Fact.4term
→ AddOne.(Fact.4)term
Rules
One definition per name(max)No pattern matchingOnly head reduction
Consequences
DeterministicSimple operational semanticsSuitable for modelingcontinuations, hence exceptions
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 3 / 22
First look Head reduction: continuation passing style
Functions fill in the result in their continuation parameter
Comp.f .g .x def−→ f .(g .x)Comp.AddOne.Fact.4→ AddOne.(Fact).4
Realistic names:
Fact.r .x � r .x!AddOne.r .x � r .(x+1)
Fact.(AddOne.r).4� AddOne.r .24� r .25
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
First look Head reduction: continuation passing style
Functions fill in the result in their continuation parameter
Comp.f .g .x def−→ f .(g .x)Comp.AddOne.Fact.4→ AddOne.(Fact).4
Realistic names:
Fact.r .x � r .x!AddOne.r .x � r .(x+1)
Fact.(AddOne.r).4� AddOne.r .24� r .25
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
First look Head reduction: continuation passing style
Functions fill in the result in their continuation parameter
Comp.f .g .x def−→ f .(g .x)Comp.AddOne.Fact.4→ AddOne.(Fact).4
Realistic names:
Fact.r .x � r .x!AddOne.r .x � r .(x+1)
Fact.(AddOne.r).4� AddOne.r .24� r .25
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
First look Head reduction: continuation passing style
Functions fill in the result in their continuation parameter
Comp.r .f .g .x def−→ g .(f .r).xComp.r .AddOne.Fact.4→ Fact.(AddOne.r).4
Realistic names:
Fact.r .x � r .x!AddOne.r .x � r .(x+1)
Fact.(AddOne.r).4� AddOne.r .24� r .25
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
First look Head reduction: continuation passing style
Functions fill in the result in their continuation parameter
Comp.r .f .g .x def−→ g .(f .r).xComp.r .AddOne.Fact.4→ Fact.(AddOne.r).4
Realistic names:
Fact.r .x � r .x!AddOne.r .x � r .(x+1)
Fact.(AddOne.r).4� AddOne.r .24� r .25
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
Definition
Definitions
There is an infinite set of names, indicated by a capital.
A term is either a name, or two terms combined by a dot.
We write a.b.c as shorthand for (a.b).c .
A program P consists of a set of rules, of the formName.variable. · · · .variable def−→ term over those variablesThat rule defines that name.
Each rule defines a different name.
If “N.x1 . · · · .xkdef−→ r ” ∈ P , then we reduce N.~t→ r [~t/~x ].
Term N.x1. · · · .xl for l 6= k does not reduce at all.
There is only head reduction.
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 5 / 22
Definition
Definitions
There is an infinite set of names, indicated by a capital.
A term is either a name, or two terms combined by a dot.
We write a.b.c as shorthand for (a.b).c .
A program P consists of a set of rules, of the formName.variable. · · · .variable def−→ term over those variablesThat rule defines that name.
Each rule defines a different name.
If “N.x1 . · · · .xkdef−→ r ” ∈ P , then we reduce N.~t→ r [~t/~x ].
Term N.x1. · · · .xl for l 6= k does not reduce at all.
There is only head reduction.
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 5 / 22
Definition
Definitions
There is an infinite set of names, indicated by a capital.
A term is either a name, or two terms combined by a dot.
We write a.b.c as shorthand for (a.b).c .
A program P consists of a set of rules, of the formName.variable. · · · .variable def−→ term over those variablesThat rule defines that name.
Each rule defines a different name.
If “N.x1 . · · · .xkdef−→ r ” ∈ P , then we reduce N.~t→ r [~t/~x ].
Term N.x1. · · · .xl for l 6= k does not reduce at all.
There is only head reduction.
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 5 / 22
Long-term goal
Long-term goal
Formally modeling programming languagesHopefully as a base for better languages
Operational semanticsRunning time: #steps ∼ seconds CPU timeWarning: some unsubstantiated claims
CompositionalFacilitates proving propertiesDeterministic by natureWorks with continuationsExplained later
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 6 / 22
Long-term goal
Long-term goal
Formally modeling programming languagesHopefully as a base for better languages
Operational semanticsRunning time: #steps ∼ seconds CPU timeWarning: some unsubstantiated claims
CompositionalFacilitates proving propertiesDeterministic by natureWorks with continuationsExplained later
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 6 / 22
Interfaces, not pattern matching
Outline
1 First look
2 Definition
3 Long-term goal
4 Interfaces, not pattern matching
5 The importance of head reduction
6 Relation to lambda calculus
7 Conclusion
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 7 / 22
Interfaces, not pattern matching
Outline
1 First look
2 Definition
3 Long-term goal
4 Interfaces, not pattern matching
5 The importance of head reduction
6 Relation to lambda calculus
7 Conclusion
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 8 / 22
Interfaces, not pattern matching
Natural numbersSlightly similar to lambda calculus
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
Add .r .(S .Zero).Zero→ Zero.(r .(S .Zero)).(Add .r .(S .(S .Zero)))→ r .(S .Zero)
Add .r .Zero.(S .Zero)→ S .Zero.(r .Zero).(Add .r .(S .Zero))→ Add .r .(S .Zero).Zero→ Zero.(r .(S .Zero)).(Add .r .(S .(S .Zero)))→ r .(S .Zero)
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 9 / 22
Interfaces, not pattern matching
Natural numbersSlightly similar to lambda calculus
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
Add .r .(S .Zero).Zero→ Zero.(r .(S .Zero)).(Add .r .(S .(S .Zero)))→ r .(S .Zero)
Add .r .Zero.(S .Zero)→ S .Zero.(r .Zero).(Add .r .(S .Zero))→ Add .r .(S .Zero).Zero→ Zero.(r .(S .Zero)).(Add .r .(S .(S .Zero)))→ r .(S .Zero)
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 9 / 22
Interfaces, not pattern matching
Natural numbersSlightly similar to lambda calculus
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
What can we feed to Add?ZeroS .(· · · .(S .Zero) · · ·)Something else?
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 10 / 22
Interfaces, not pattern matching
Natural numbersSlightly similar to lambda calculus
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
What can we feed to Add?ZeroS .(· · · .(S .Zero) · · ·)Something else? Yes!
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 10 / 22
Interfaces, not pattern matching
Natural numbersCompatible naturals
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
We define when a term t represents natural number n.1 t represents 0 if ∀z ,s : t.z .s � z
Informally: t “behaves the same as” Zero2 t represents n+1 if ∀z ,s : t.z .s � s.q, and q represents n
Informally: t “behaves the same as” S .q
With this, we can define LazyFact such that LazyFact.x represents (x!).(Omitted.)
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 11 / 22
Interfaces, not pattern matching
Natural numbersCompatible naturals
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
With this, we can define LazyFact such that LazyFact.x represents (x!).(Omitted.)
Add .r .Zero.(LazyFact.(S .Zero))→ LazyFact.(S .Zero).(r .Zero).(Add .r .(S .Zero))�Add .r .(S .Zero).Zero→ Zero.(r .(S .Zero)).(Add .r .(S .(S .Zero)))→ r .(S .Zero)
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 11 / 22
Interfaces, not pattern matching
Natural numbersCompatible naturals
Zero.z .s def−→ z
S .x .z .s def−→ s.x
Add .r .x .y def−→ y .(r .x).(Add .r .(S .x))
Algorithm:
x+0= xx+S(y) = S(x)+ y
With this, we can define LazyFact such that LazyFact.x represents (x!).(Omitted.)
Add .r .Zero.(LazyFact.(S .Zero))� r .(S .Zero)
Two types of function names
Call-by-value / eager: calculate result, fill in in continuationCall-by-name / lazy: compatible with Sn.Zero but delayedcomputation
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 11 / 22
The importance of head reduction
Outline
1 First look
2 Definition
3 Long-term goal
4 Interfaces, not pattern matching
5 The importance of head reduction
6 Relation to lambda calculus
7 Conclusion
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 12 / 22
The importance of head reduction
Outline
1 First look
2 Definition
3 Long-term goal
4 Interfaces, not pattern matching
5 The importance of head reduction
6 Relation to lambda calculus
7 Conclusion
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 13 / 22
The importance of head reduction
Operational semantics
Lambda calculus (normal order)
Reduction using two instructions on the top levelM N Push N on stack, continue in Mλx .M Pop N off stack, continue in M[N/x ]
as presented in [Levy(2001)]
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 14 / 22
The importance of head reduction
Back to modeling programs
Lambda calculusλx .+ (f x) (g x)
M N Push N on stack,continue in M
λx .M Pop N off stack,continue in M[N/x ]
Programming language
fun x → (f x) + (g x)
M N Apply function objectto argument
fun x→M Make function object
In “real languages”, functions throw exceptions and have other side effects.
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 15 / 22
The importance of head reduction
Back to modeling programs
Lambda calculusλx .+ (f x) (g x)
M N Push N on stack,continue in M
λx .M Pop N off stack,continue in M[N/x ]
Programming language
fun x → (f x) + (g x)︸︷︷︸throws exception
now what?
M N Apply function objectto argument
fun x→M Make function object
In “real languages”, functions throw exceptions and have other side effects.
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 15 / 22
The importance of head reduction
Operational semanticsComputational models with control
Lambda calculus + continuations (λC)
Reduction using four instructions on the top levelM N Push N on stack, continue in Mλx .M Pop N off stack, continue in M[N/x ]A M Empty the stack, continue in MCM Empty the stack, continue in M (λx .S x)
where function S ‘restores’ the previous stack
(details omitted)
Can model exception-like facilitiesCPS transformation to transform λC terms to λ terms
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 16 / 22
The importance of head reduction
CPS transformation
λC CPS transformation−−−−−−−−−−−→ subset of λ , makes minimal use of stack
Two calculi in action:λC calculus: M N, λx .M, A M, CMExpressiveSubset of λ calculus: M N, λx .MSimpler
What is this subset, really?Can we describe it?An elegant model of computation, perhaps?
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
The importance of head reduction
CPS transformation
λC CPS transformation−−−−−−−−−−−→ subset of λ , makes minimal use of stack
Two calculi in action:λC calculus: M N, λx .M, A M, CMExpressiveSubset of λ calculus: M N, λx .MSimpler
What is this subset, really?
Can we describe it?An elegant model of computation, perhaps?
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
The importance of head reduction
CPS transformation
λC CPS transformation−−−−−−−−−−−→ subset of λ , makes minimal use of stack
Two calculi in action:λC calculus: M N, λx .M, A M, CMExpressiveSubset of λ calculus: M N, λx .MSimpler
What is this subset, really?Can we describe it?
An elegant model of computation, perhaps?
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
The importance of head reduction
CPS transformation
λC CPS transformation−−−−−−−−−−−→ subset of λ , makes minimal use of stack
Two calculi in action:λC calculus: M N, λx .M, A M, CMExpressiveSubset of λ calculus: M N, λx .MSimpler
What is this subset, really?Can we describe it?An elegant model of computation, perhaps?
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
The importance of head reduction
Lambda calculus vs. continuation calculus
λCFour instructions on the top levelM N Push N on stack, continue in Mλx .M Pop N off stack, continue in M[N/x ]A M Empty the stack, continue in MCM Empty the stack, continue in M (λx .S x)
where function S ‘restores’ the stack
Continuation calculusOne instruction on the top level, using program Pn.t1. · · · .tk Empty the stack, continue in r [~t/~x ]
if n.x1 . · · · .xkdef−→ r ∈ P
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 18 / 22
The importance of head reduction
Lambda calculus vs. continuation calculus
λCFour instructions on the top levelM N Push N on stack, continue in Mλx .M Pop N off stack, continue in M[N/x ]A M Empty the stack, continue in MCM Empty the stack, continue in M (λx .S x)
where function S ‘restores’ the stack
Continuation calculusOne instruction on the top level, using program Pn.t1. · · · .tk Empty the stack, continue in r [~t/~x ]
if n.x1 . · · · .xkdef−→ r ∈ P
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 18 / 22
The importance of head reduction
Continuation calculus
Continuation calculusOne instruction on the top level, using program Pn.t1. · · · .tk Continue in r [~t/~x ]
if n.x1 . · · · .xkdef−→ r ∈ P
No stack needed, becausen.t1. · · · .tl does not reduce for l 6= k .Subterms are not reduced
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 19 / 22
Relation to lambda calculus
Relation to lambda calculus
λ ,λC CPS transformation−−−−−−−−−−−→ subset of λ
subset of λλx to rules∗−−−−−−−−−−−⇀↽−−−−−−−−−−−
unfold† rules to λxCC
∗ λx to rules: involves a supercombinator transformation† Unfold rules to λx : first apply a fixed-point elimination to eliminate cyclic references
Catchphrase
CC is more limited than λ ,thus more suitable to model continuations
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 20 / 22
Relation to lambda calculus
Relation to lambda calculus
λ ,λC CPS transformation−−−−−−−−−−−→ subset of λ
subset of λλx to rules∗−−−−−−−−−−−⇀↽−−−−−−−−−−−
unfold† rules to λxCC
∗ λx to rules: involves a supercombinator transformation† Unfold rules to λx : first apply a fixed-point elimination to eliminate cyclic references
Catchphrase
CC is more limited than λ ,thus more suitable to model continuations
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 20 / 22
Conclusion
Conclusion
CC is a constrained term rewriting systemCC is deterministicOnly head reduction
As a consequence,Suitable for control with continuations (exceptions)CC is similar to a subset of lambda calculusAllows both eager and lazy function termscall-by-value vs. call-by-name
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 21 / 22
Appendix
Bibliography
P.B. Levy.Call-by-push-value.PhD thesis, Queen Mary, University of London, 2001.
Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 22 / 22