0
Upcoming SlideShare
×

# Continuation calculus at Term Rewriting Seminar

598

Published on

Continuation calculus (CC) is an alternative to lambda calculus, where the order of evaluation is determined by programs themselves. Owing to its simplicity, continuations are no unusual terms. This makes it natural to model programs with nonlocal control flow, as with exceptions and call-by-name functions.

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
598
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript of "Continuation calculus at Term Rewriting Seminar"

1. 1. Continuation calculusBram Geron1 Herman Geuvers1,21Eindhoven University of Technology2Radboud University NijmegenTerm Rewriting Seminar, May 2013Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 1 / 22
2. 2. Outline1 First look2 Deﬁnition3 Long-term goal4 Interfaces, not pattern matching5 The importance of head reduction6 Relation to lambda calculus7 ConclusionGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 2 / 22
3. 3. First look Rules, names, variables, termsCC is a constrained term rewriting systemComp.f .g.xdef−→ f .(g.x)Comp.AddOne.Fact.4 → AddOne.(Fact.4)RulesOne deﬁnition per name(max)No pattern matchingOnly head reductionConsequencesDeterministicSimple operational semanticsSuitable for modelingcontinuations, hence exceptionsGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 3 / 22
4. 4. First look Rules, names, variables, termsCC is a constrained term rewriting systemruleCompname(constant).f .g.xvariablesdef−→ f .(g.x)Comp.AddOne.Fact.4term→ AddOne.(Fact.4)termRulesOne deﬁnition per name(max)No pattern matchingOnly head reductionConsequencesDeterministicSimple operational semanticsSuitable for modelingcontinuations, hence exceptionsGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 3 / 22
5. 5. First look Rules, names, variables, termsCC is a constrained term rewriting systemruleCompname(constant).f .g.xvariablesdef−→ f .(g.x)Comp.AddOne.Fact.4term→ AddOne.(Fact.4)termRulesOne deﬁnition per name(max)No pattern matchingOnly head reductionConsequencesDeterministicSimple operational semanticsSuitable for modelingcontinuations, hence exceptionsGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 3 / 22
6. 6. First look Head reduction: continuation passing styleFunctions ﬁll in the result in their continuation parameterComp.f .g.xdef−→ f .(g.x)Comp.AddOne.Fact.4 → AddOne.(Fact).4Realistic names:Fact.r.x r.x!AddOne.r.x r.(x +1)Fact.(AddOne.r).4 AddOne.r.24r.25Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
7. 7. First look Head reduction: continuation passing styleFunctions ﬁll in the result in their continuation parameterComp.f .g.xdef−→ f .(g.x)Comp.AddOne.Fact.4 → AddOne.(Fact).4Realistic names:Fact.r.x r.x!AddOne.r.x r.(x +1)Fact.(AddOne.r).4 AddOne.r.24r.25Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
8. 8. First look Head reduction: continuation passing styleFunctions ﬁll in the result in their continuation parameterComp.f .g.xdef−→ f .(g.x)Comp.AddOne.Fact.4 → AddOne.(Fact).4Realistic names:Fact.r.x r.x!AddOne.r.x r.(x +1)Fact.(AddOne.r).4 AddOne.r.24r.25Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
9. 9. First look Head reduction: continuation passing styleFunctions ﬁll in the result in their continuation parameterComp.r.f .g.xdef−→ g.(f .r).xComp.r.AddOne.Fact.4 → Fact.(AddOne.r).4Realistic names:Fact.r.x r.x!AddOne.r.x r.(x +1)Fact.(AddOne.r).4 AddOne.r.24r.25Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
10. 10. First look Head reduction: continuation passing styleFunctions ﬁll in the result in their continuation parameterComp.r.f .g.xdef−→ g.(f .r).xComp.r.AddOne.Fact.4 → Fact.(AddOne.r).4Realistic names:Fact.r.x r.x!AddOne.r.x r.(x +1)Fact.(AddOne.r).4 AddOne.r.24r.25Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 4 / 22
11. 11. DeﬁnitionDeﬁnitionsThere is an inﬁnite 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.··· .variabledef−→ term over those variablesThat rule deﬁnes that name.Each rule deﬁnes a diﬀerent name.If “N.x1 .··· .xkdef−→ r” ∈ P, then we reduce N.t → r[t/x].Term N.x1.··· .xl for l = k does not reduce at all.There is only head reduction.Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 5 / 22
12. 12. DeﬁnitionDeﬁnitionsThere is an inﬁnite 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.··· .variabledef−→ term over those variablesThat rule deﬁnes that name.Each rule deﬁnes a diﬀerent name.If “N.x1 .··· .xkdef−→ r” ∈ P, then we reduce N.t → r[t/x].Term N.x1.··· .xl for l = k does not reduce at all.There is only head reduction.Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 5 / 22
13. 13. DeﬁnitionDeﬁnitionsThere is an inﬁnite 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.··· .variabledef−→ term over those variablesThat rule deﬁnes that name.Each rule deﬁnes a diﬀerent name.If “N.x1 .··· .xkdef−→ r” ∈ P, then we reduce N.t → r[t/x].Term N.x1.··· .xl for l = k does not reduce at all.There is only head reduction.Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 5 / 22
14. 14. Long-term goalLong-term goalFormally modeling programming languagesHopefully as a base for better languagesOperational semanticsRunning time: #steps ∼ seconds CPU timeWarning: some unsubstantiated claimsCompositionalFacilitates proving propertiesDeterministic by natureWorks with continuationsExplained laterGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 6 / 22
15. 15. Long-term goalLong-term goalFormally modeling programming languagesHopefully as a base for better languagesOperational semanticsRunning time: #steps ∼ seconds CPU timeWarning: some unsubstantiated claimsCompositionalFacilitates proving propertiesDeterministic by natureWorks with continuationsExplained laterGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 6 / 22
16. 16. Interfaces, not pattern matchingOutline1 First look2 Deﬁnition3 Long-term goal4 Interfaces, not pattern matching5 The importance of head reduction6 Relation to lambda calculus7 ConclusionGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 7 / 22
17. 17. Interfaces, not pattern matchingOutline1 First look2 Deﬁnition3 Long-term goal4 Interfaces, not pattern matching5 The importance of head reduction6 Relation to lambda calculus7 ConclusionGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 8 / 22
20. 20. Interfaces, not pattern matchingNatural numbersSlightly similar to lambda calculusZero.z.sdef−→ zS.x.z.sdef−→ s.xAdd.r.x.ydef−→ y.(r.x).(Add.r.(S.x))Algorithm:x +0 = xx +S(y) = S(x)+yWhat can we feed to Add?ZeroS.(··· .(S.Zero)···)Something else?Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 10 / 22
21. 21. Interfaces, not pattern matchingNatural numbersSlightly similar to lambda calculusZero.z.sdef−→ zS.x.z.sdef−→ s.xAdd.r.x.ydef−→ y.(r.x).(Add.r.(S.x))Algorithm:x +0 = xx +S(y) = S(x)+yWhat can we feed to Add?ZeroS.(··· .(S.Zero)···)Something else? Yes!Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 10 / 22
22. 22. Interfaces, not pattern matchingNatural numbersCompatible naturalsZero.z.sdef−→ zS.x.z.sdef−→ s.xAdd.r.x.ydef−→ y.(r.x).(Add.r.(S.x))Algorithm:x +0 = xx +S(y) = S(x)+yWe deﬁne when a term t represents natural number n.1 t represents 0 if ∀z,s : t.z.s zInformally: t “behaves the same as” Zero2 t represents n +1 if ∀z,s : t.z.s s.q, and q represents nInformally: t “behaves the same as” S.qWith this, we can deﬁne LazyFact such that LazyFact.x represents (x!).(Omitted.)Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 11 / 22
23. 23. Interfaces, not pattern matchingNatural numbersCompatible naturalsZero.z.sdef−→ zS.x.z.sdef−→ s.xAdd.r.x.ydef−→ y.(r.x).(Add.r.(S.x))Algorithm:x +0 = xx +S(y) = S(x)+yWith this, we can deﬁne 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
24. 24. Interfaces, not pattern matchingNatural numbersCompatible naturalsZero.z.sdef−→ zS.x.z.sdef−→ s.xAdd.r.x.ydef−→ y.(r.x).(Add.r.(S.x))Algorithm:x +0 = xx +S(y) = S(x)+yWith this, we can deﬁne LazyFact such that LazyFact.x represents (x!).(Omitted.)Add.r.Zero.(LazyFact.(S.Zero)) r.(S.Zero)Two types of function namesCall-by-value / eager: calculate result, ﬁll in in continuationCall-by-name / lazy: compatible with Sn.Zero but delayedcomputationGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 11 / 22
25. 25. The importance of head reductionOutline1 First look2 Deﬁnition3 Long-term goal4 Interfaces, not pattern matching5 The importance of head reduction6 Relation to lambda calculus7 ConclusionGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 12 / 22
26. 26. The importance of head reductionOutline1 First look2 Deﬁnition3 Long-term goal4 Interfaces, not pattern matching5 The importance of head reduction6 Relation to lambda calculus7 ConclusionGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 13 / 22
27. 27. The importance of head reductionOperational semanticsLambda calculus (normal order)Reduction using two instructions on the top levelM N Push N on stack, continue in Mλx.M Pop N oﬀ stack, continue in M[N/x]as presented in [Levy(2001)]Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 14 / 22
28. 28. The importance of head reductionBack to modeling programsLambda calculusλx.+ (f x) (g x)M N Push N on stack,continue in Mλx.M Pop N oﬀ stack,continue in M[N/x]Programming languagefun x → (f x) + (g x)M N Apply function objectto argumentfun x → M Make function objectIn “real languages”, functions throw exceptions and have other side eﬀects.Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 15 / 22
29. 29. The importance of head reductionBack to modeling programsLambda calculusλx.+ (f x) (g x)M N Push N on stack,continue in Mλx.M Pop N oﬀ stack,continue in M[N/x]Programming languagefun x → (f x) + (g x)throws exceptionnow what?M N Apply function objectto argumentfun x → M Make function objectIn “real languages”, functions throw exceptions and have other side eﬀects.Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 15 / 22
30. 30. The importance of head reductionOperational semanticsComputational models with controlLambda calculus + continuations (λC)Reduction using four instructions on the top levelM N Push N on stack, continue in Mλx.M Pop N oﬀ stack, continue in M[N/x]A M Empty the stack, continue in MC M 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 λ termsGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 16 / 22
31. 31. The importance of head reductionCPS transformationλCCPS transformation−−−−−−−−−−−→ subset of λ, makes minimal use of stackTwo calculi in action:λC calculus: M N, λx.M, A M, C MExpressiveSubset of λ calculus: M N, λx.MSimplerGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
32. 32. The importance of head reductionCPS transformationλCCPS transformation−−−−−−−−−−−→ subset of λ, makes minimal use of stackTwo calculi in action:λC calculus: M N, λx.M, A M, C MExpressiveSubset of λ calculus: M N, λx.MSimplerWhat is this subset, really?Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
33. 33. The importance of head reductionCPS transformationλCCPS transformation−−−−−−−−−−−→ subset of λ, makes minimal use of stackTwo calculi in action:λC calculus: M N, λx.M, A M, C MExpressiveSubset of λ calculus: M N, λx.MSimplerWhat is this subset, really?Can we describe it?Geron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 17 / 22
34. 34. The importance of head reductionCPS transformationλCCPS transformation−−−−−−−−−−−→ subset of λ, makes minimal use of stackTwo calculi in action:λC calculus: M N, λx.M, A M, C MExpressiveSubset of λ calculus: M N, λx.MSimplerWhat 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
35. 35. The importance of head reductionLambda calculus vs. continuation calculusλCFour instructions on the top levelM N Push N on stack, continue in Mλx.M Pop N oﬀ stack, continue in M[N/x]A M Empty the stack, continue in MC M Empty the stack, continue in M (λx.S x)where function S ‘restores’ the stackContinuation calculusOne instruction on the top level, using program Pn.t1.··· .tk Empty the stack, continue in r[t/x]if n.x1 .··· .xkdef−→ r ∈ PGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 18 / 22
36. 36. The importance of head reductionLambda calculus vs. continuation calculusλCFour instructions on the top levelM N Push N on stack, continue in Mλx.M Pop N oﬀ stack, continue in M[N/x]A M Empty the stack, continue in MC M Empty the stack, continue in M (λx.S x)where function S ‘restores’ the stackContinuation calculusOne instruction on the top level, using program Pn.t1.··· .tk Empty the stack, continue in r[t/x]if n.x1 .··· .xkdef−→ r ∈ PGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 18 / 22
37. 37. The importance of head reductionContinuation calculusContinuation calculusOne instruction on the top level, using program Pn.t1.··· .tk Continue in r[t/x]if n.x1 .··· .xkdef−→ r ∈ PNo stack needed, becausen.t1.··· .tl does not reduce for l = k.Subterms are not reducedGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 19 / 22
38. 38. Relation to lambda calculusRelation to lambda calculusλ,λCCPS transformation−−−−−−−−−−−→ subset of λsubset of λλx to rules∗−−−−−−−−−−−−−−−−−−−−−−unfold†rules to λxCC∗ λx to rules: involves a supercombinator transformation† Unfold rules to λx: ﬁrst apply a ﬁxed-point elimination to eliminate cyclic referencesCatchphraseCC is more limited than λ,thus more suitable to model continuationsGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 20 / 22
39. 39. Relation to lambda calculusRelation to lambda calculusλ,λCCPS transformation−−−−−−−−−−−→ subset of λsubset of λλx to rules∗−−−−−−−−−−−−−−−−−−−−−−unfold†rules to λxCC∗ λx to rules: involves a supercombinator transformation† Unfold rules to λx: ﬁrst apply a ﬁxed-point elimination to eliminate cyclic referencesCatchphraseCC is more limited than λ,thus more suitable to model continuationsGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 20 / 22
40. 40. ConclusionConclusionCC is a constrained term rewriting systemCC is deterministicOnly head reductionAs 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-nameGeron, Geuvers (TU/e, RU) Continuation calculus TERESE May 2013 21 / 22
41. 41. AppendixBibliographyP.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
1. #### A particular slide catching your eye?

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