Successfully reported this slideshow.
Upcoming SlideShare
×

Colloquium presentation

852 views

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.

This presentation includes the list multiplication example.

Published in: Technology, Education
• Full Name
Comment goes here.

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

• Be the first to like this

Colloquium presentation

1. 1. Continuation calculusAn alternative to lambda calculusMidterm presentationBram Geron(supervised by Herman Geuvers)Eindhoven University of TechnologyFSA colloquium, June 2013Bram Geron (TU/e) Continuation calculus FSA colloquium 1 / 31
2. 2. Modeling programming languagesGoals:Assign exact meaning to programsAssign exact meaning to functionsReason on functionsApproaches:Big-step semanticsSmall-step semanticsReduction to simpler calculusBram Geron (TU/e) Continuation calculus FSA colloquium 2 / 31
3. 3. Modeling programming languagesGoals:Assign exact meaning to programsAssign exact meaning to functionsReason on functionsApproaches:Big-step semantics: lambda calculusSmall-step semantics: lambda calculus, continuation calculusReduction to simpler calculus: programming languages to λ or CCBram Geron (TU/e) Continuation calculus FSA colloquium 2 / 31
4. 4. Modeling programming languagesSimple calculiLambda calculusTree of expressions(fact 4)+1β 25Continuation calculusTree of “continuation terms”Fact.(AddOne.Return).4Return.25Bram Geron (TU/e) Continuation calculus FSA colloquium 3 / 31
5. 5. First lookOutline1 First look2 Deﬁnition3 Long-term goal4 The importance of head reduction5 Case study: list multiplication6 ConclusionBram Geron (TU/e) Continuation calculus FSA colloquium 4 / 31
6. 6. First look Rules, names, variables, termsCC is a term rewriting system in a constrained shapeComp.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 exceptionsBram Geron (TU/e) Continuation calculus FSA colloquium 5 / 31
7. 7. First look Rules, names, variables, termsCC is a term rewriting system in a constrained shaperuleCompname(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 exceptionsBram Geron (TU/e) Continuation calculus FSA colloquium 5 / 31
8. 8. First look Rules, names, variables, termsCC is a term rewriting system in a constrained shaperuleCompname(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 exceptionsBram Geron (TU/e) Continuation calculus FSA colloquium 5 / 31
9. 9. 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.25−→ CPS transformationBram Geron (TU/e) Continuation calculus FSA colloquium 6 / 31
10. 10. 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.25−→ CPS transformationBram Geron (TU/e) Continuation calculus FSA colloquium 6 / 31
11. 11. 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.25−→ CPS transformationBram Geron (TU/e) Continuation calculus FSA colloquium 6 / 31
12. 12. 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.25−→ CPS transformationBram Geron (TU/e) Continuation calculus FSA colloquium 6 / 31
13. 13. 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.25−→ CPS transformationBram Geron (TU/e) Continuation calculus FSA colloquium 6 / 31
14. 14. 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.25−→ CPS transformationBram Geron (TU/e) Continuation calculus FSA colloquium 6 / 31
15. 15. First look Another exampleNatural numbersRepresentation and AddOneWe represent 0 by Zero, 3 by S.(S.(S.Zero)), and n byS.(··· .(S.Zero)···). These terms are denoted n .We implement the function AddOne, such that AddOne.r. n r. n +1 .AddOne.r.mdef−→ r.(S.m)Bram Geron (TU/e) Continuation calculus FSA colloquium 7 / 31
16. 16. First look Another exampleNatural numbersBehavior and DoubleWe represent 0 by Zero, 3 by S.(S.(S.Zero)), and n byS.(··· .(S.Zero)···). These terms are denoted n .There are rules for Zero and S:Zero.z.sdef−→ z s.t. 0 .z.s zS.m.z.sdef−→ s.m s.t. m +1 .z.s s. mWe can now make a function Double such that Double.r. n r. 2n .Double.r.mdef−→ m.(r.Zero).(Double.(AddOne.(AddOne.r)))Bram Geron (TU/e) Continuation calculus FSA colloquium 8 / 31
17. 17. First look Another exampleNatural numbersBehavior and DoubleWe represent 0 by Zero, 3 by S.(S.(S.Zero)), and n byS.(··· .(S.Zero)···). These terms are denoted n .We can now make a function Double such that Double.r. n r. 2n .Double.r.mdef−→ m.(r.Zero).(Double.(AddOne.(AddOne.r)))Bram Geron (TU/e) Continuation calculus FSA colloquium 8 / 31
18. 18. First look Another exampleNatural numbersBehavior and DoubleWe represent 0 by Zero, 3 by S.(S.(S.Zero)), and n byS.(··· .(S.Zero)···). These terms are denoted n .We can now make a function Double such that Double.r. n r. 2n .Double.r.mdef−→ m.(r.Zero).(Double.(AddOne.(AddOne.r)))Double.r. 0 → Zero.(r. 0 ).(Double.(AddOne.(AddOne.r)))→ r. 0Bram Geron (TU/e) Continuation calculus FSA colloquium 8 / 31
20. 20. 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.Bram Geron (TU/e) Continuation calculus FSA colloquium 9 / 31
21. 21. 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.Bram Geron (TU/e) Continuation calculus FSA colloquium 9 / 31
22. 22. 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.Bram Geron (TU/e) Continuation calculus FSA colloquium 9 / 31
23. 23. 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 laterBram Geron (TU/e) Continuation calculus FSA colloquium 10 / 31
24. 24. 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 laterBram Geron (TU/e) Continuation calculus FSA colloquium 10 / 31
25. 25. The importance of head reductionOutline1 First look2 Deﬁnition3 Long-term goal4 The importance of head reduction5 Case study: list multiplication6 ConclusionBram Geron (TU/e) Continuation calculus FSA colloquium 11 / 31
26. 26. The importance of head reductionOutline1 First look2 Deﬁnition3 Long-term goal4 The importance of head reduction5 Case study: list multiplication6 ConclusionBram Geron (TU/e) Continuation calculus FSA colloquium 12 / 31
27. 27. 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.Bram Geron (TU/e) Continuation calculus FSA colloquium 13 / 31
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)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.Bram Geron (TU/e) Continuation calculus FSA colloquium 13 / 31
29. 29. 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 λ termsBram Geron (TU/e) Continuation calculus FSA colloquium 14 / 31
30. 30. 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.MSimplerBram Geron (TU/e) Continuation calculus FSA colloquium 15 / 31
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.MSimplerWhat is this subset, really?Bram Geron (TU/e) Continuation calculus FSA colloquium 15 / 31
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?Can we describe it?Bram Geron (TU/e) Continuation calculus FSA colloquium 15 / 31
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?An elegant model of computation, perhaps?Bram Geron (TU/e) Continuation calculus FSA colloquium 15 / 31
34. 34. 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 ∈ PBram Geron (TU/e) Continuation calculus FSA colloquium 16 / 31
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 ∈ PBram Geron (TU/e) Continuation calculus FSA colloquium 16 / 31
36. 36. 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 reducedBram Geron (TU/e) Continuation calculus FSA colloquium 17 / 31
37. 37. Case study: list multiplicationOutline1 First look2 Deﬁnition3 Long-term goal4 The importance of head reduction5 Case study: list multiplication6 ConclusionBram Geron (TU/e) Continuation calculus FSA colloquium 18 / 31
38. 38. Case study: list multiplicationList multiplicationAn algorithmproduct [4,2,6] = 4∗2∗6 = 48product l = case l of| [] → 1| [x]++xs → x ∗product xsproduct [4,2,6] = 4∗(2∗(6∗1)) = 48Bram Geron (TU/e) Continuation calculus FSA colloquium 19 / 31
39. 39. Case study: list multiplicationList multiplicationAn algorithmproduct [4,2,6] = 4∗2∗6 = 48product l = case l of| [] → 1| [x]++xs → x ∗product xsproduct [4,2,6] = 4∗(2∗(6∗1)) = 48Bram Geron (TU/e) Continuation calculus FSA colloquium 19 / 31
40. 40. Case study: list multiplicationList multiplicationA better algorithmproduct [4,0,6] = 4∗0∗6 = 0product l = case l of| [] → 1| [x]++xs → x ∗product xsproduct [4,0,6] = 4∗(0∗(6∗1)) = 0Bram Geron (TU/e) Continuation calculus FSA colloquium 20 / 31
41. 41. Case study: list multiplicationList multiplicationA better algorithmproduct [4,0,6] = 4∗0∗6 = 0product l =try:let subproduct l = case l of| [] → 1| [0]++xs → abort| [x]++xs → x ∗subproduct xsin subproduct lcatch abort :0Bram Geron (TU/e) Continuation calculus FSA colloquium 20 / 31
42. 42. Case study: list multiplicationList multiplicationA better algorithmproduct l =try:let subproduct l = case l of| [] → 1| [0]++xs → abort| [x]++xs → x ∗subproduct xsin subproduct lcatch abort :0product [4,0,6] = try: {4∗abort} catch abort : 0= 0Bram Geron (TU/e) Continuation calculus FSA colloquium 20 / 31
43. 43. Case study: list multiplicationList multiplicationImplementationImplemented ListMult with this behavior in continuation calculusincludes modeling of naturals and lists with CC rulesProved correctListMult. l .r r. product l for all l ∈ ListNEmpirical evidence of eﬃciencyListMult. [4,0,6] .r r. 0 in 10 stepsListMult. [4,2,6] .r r. 48 in 339 stepsBram Geron (TU/e) Continuation calculus FSA colloquium 21 / 31
44. 44. ConclusionConclusionCC is a term rewriting system in a constrained shapeCC is deterministicOnly head reductionAs a consequence,Suitable for control with continuations (exceptions)CC is similar to a subset of lambda calculusMimics running times of real programs to some degreeBram Geron (TU/e) Continuation calculus FSA colloquium 22 / 31
45. 45. ConclusionCurrent state and future workJoint paper with Herman GeuversAccepted in proceedings of COS 2013, 24–25 JuneFuture work:Systematic translation of functional programming language to CCProve that this translation preserves semanticsType systemModel hierarchical side-eﬀects using an extensionBram Geron (TU/e) Continuation calculus FSA colloquium 23 / 31
46. 46. ConclusionThe EndQuestions?Bram Geron (TU/e) Continuation calculus FSA colloquium 24 / 31
47. 47. BibliographyBibliographyBram Geron (TU/e) Continuation calculus FSA colloquium 25 / 31
48. 48. Extra slidesExtra slidesExtra slidesBram Geron (TU/e) Continuation calculus FSA colloquium 26 / 31
49. 49. Extra slides 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 continuationsBram Geron (TU/e) Continuation calculus FSA colloquium 27 / 31
50. 50. Extra slides 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 continuationsBram Geron (TU/e) Continuation calculus FSA colloquium 27 / 31