called strict evaluation) Arguments are evaluated before function application. Widely adopted in practical programming languages. Call-by-Name evaluation Arguments are not evaluated until they are needed. If an argument is never used, it is never evaluated; if used multiple times, it is re-evaluated each time. Evaluation strategy can affect both the number of steps (efficiency) and the result of computation. Call-by-Value Example 1: Keep reducing the β-redex inside Ω (λx. λy. x)(λx. x) Ω → (λy. (λx. x)) Ω → · · · Call-by-Name Example 1: Reduce the leftmost β-redex to reach normal form (λx. λy. x)(λx. x) Ω → (λy. (λx. x)) Ω → λx. x Yudai Urabe (Plotkin,1975) 2 / 29
transformation that makes evaluation order explicit. Characteristics of CPS programs: Every function takes an additional argument representing the continuation. Evaluation order is unambiguous. Direct Style let rec fact n = if n == 0 then 1 else n * fact (n - 1) CPS let rec fact_cps n cont = if n = 0 then cont 1 else fact_cps (n - 1, fun x -> cont (n * x )) Yudai Urabe (Plotkin,1975) 3 / 29
be CPS-translated Using M as a meta-variable: M ::= x | λx. M | M M x: variable λx.M: lambda abstraction M1 M2: function application Values Variables and lambda abstractions Yudai Urabe (Plotkin,1975) 6 / 29
Translates the Source Language into CPS λ-Calc. evaluated under CBV. The evaluation order of the target language is unambiguous. x = λκ. κx λx.M = λκ. κ(λx.M) MN = λκ. M(λα. N(λβ. αβκ)) Auxiliary Function Ψ (p.148) Ψ: a function mapping values to values, applied to the result of CBV evaluation. Ψ(x) = x Ψ(λx.M) = λx.M Yudai Urabe (Plotkin,1975) 7 / 29
term, but introduced by the CPS transformation. Except for the substitution of 0 for x in the third line from the bottom, all the other steps are administrative reductions. 2This example is also from Janne van den Hout (2018). Yudai Urabe (Plotkin,1975) 9 / 29
transformation proposed in (Plotkin, 1975), many administrative redexes are introduced, which makes proofs more complicated. To reduce these administrative redexes, we introduce the colon translation using the Ψ defined on the previous slide. M : K expresses the result of performing all administrative reductions contained in MK. Definition (Colon Translation) (p.150 (p.154 for the CBN)) Infix operation “:” L-Closed-Terms × L -Terms −→ L -Terms. N : K = KΨ(N) (N is a closed value) M N : K = M : (λα. N(λβ. αβK)) (M is not a value) M N : K = N : (λβ. Ψ(M) βK) (M is a value but N is not) M N : K = Ψ(M) Ψ(N) K (M and N are values). Yudai Urabe (Plotkin,1975) 10 / 29
M Source Language CBN–CPS M Target Language Value Value EvalV EvalN Thm. 6.2 Ψ Thm. 6.3 CPS Trans. (p.147) Cor. 6.2 EvalV Thm. 6.1 Yudai Urabe (Plotkin,1975) 11 / 29
Thm. 4.2 (Church–Rosser) (p.135) Used in the proof of Thm. 6.1 If λV M1 Mi (i = 2, 3), then for some M4, λV Mi M4 (i = 2, 3). Thm. 4.4 (p.142) EvalV (M) = N iff M ∗ → V N, for closed M and a value N. Thm. 5.2 (p.146) EvalN(M) = N iff M ∗ → N N, for closed M. Yudai Urabe (Plotkin,1975) 12 / 29
= EvalV (M(λx.x)). for any program M. Thm. 6.2 (Simulation) Ψ(EvalV (M)) = EvalN(M(λx.x)), for any program M. Thm. 6.3 (Translation) If λL V M = N then λL V M = N and then λN M = N.a aThe second but not the first implication is reversible. In the following, we prove Thm. 6.2 (Simulation). Yudai Urabe (Plotkin,1975) 13 / 29
M is closed, not a value, and cannot be reduced any further, then M belongs to SticksV .3 Lem. 6.4 M ∈ SticksV ⇒ M : K ∈ SticksL N ⊆ SticksL V . 3See p.151 for the precise definition. Yudai Urabe (Plotkin,1975) 16 / 29
distinguish cases based on whether EvalL v (M) has a value. Case 1: EvalL v (M) has a value Case 2: EvalL v (M) is undefined Case 2a: M gets stuck Case 2b: M diverges Yudai Urabe (Plotkin,1975) 17 / 29
Suppose EvalL v (M) has a value N. By Thm. 4.4 we have M ∗ − → V N (①) (M is indeed closed by the assumptions of Thm. 6.1 & Thm. 6.2). Consider M(λx.x), CBN-CPS transformation of M. Yudai Urabe (Plotkin,1975) 18 / 29
Suppose EvalL v (M) has a value N. By Thm. 4.4 we have M ∗ − → V N (①) (M is indeed closed by the assumptions of Thm. 6.1 & Thm. 6.2). Consider M(λx.x), CBN-CPS transformation of M. M(λx.x) + − → M : (λx.x) (by Lem. 6.2) ∗ − → N : (λx.x) (by (①)) → Ψ(N) (since N is a value, by the definition of colon translation: (λx.x)Ψ(N) → Ψ(N)) = Ψ(EvalL v (M)) Yudai Urabe (Plotkin,1975) 18 / 29
Suppose EvalL v (M) has a value N. By Thm. 4.4 we have M ∗ − → V N (①) (M is indeed closed by the assumptions of Thm. 6.1 & Thm. 6.2). Consider M(λx.x), CBN-CPS transformation of M. M(λx.x) + − → M : (λx.x) (by Lem. 6.2) ∗ − → N : (λx.x) (by (①)) → Ψ(N) (since N is a value, by the definition of colon translation: (λx.x)Ψ(N) → Ψ(N)) = Ψ(EvalL v (M)) Hence EvalN(M(λx.x)) = EvalL v (M(λx.x)) Thm. 6.1(Indifference)(by Thm. 4.2) = Ψ(EvalL v (M)) Thm. 6.2(Simulation)(by Thm. 5.2) Yudai Urabe (Plotkin,1975) 18 / 29
Suppose EvalL v (M) is undefined. By Thm. 4.4 we have (2a) M ∗ − → V N ∈ SticksL v (②) or (2b) M = M1 → V M2 → V · · · (③) Yudai Urabe (Plotkin,1975) 19 / 29
Suppose EvalL v (M) is undefined. By Thm. 4.4 we have (2a) M ∗ − → V N ∈ SticksL v (②) or (2b) M = M1 → V M2 → V · · · (③) In case (2a): M(λx.x) + − → M : (λx.x) (by Lem. 6.2) ∗ − → N : (λx.x) (by (②)) ∈ SticksL v (by Lem. 6.4) Hence, by Thm. 5.2, EvalN(M(λx.x)) is undefined, and by Thm. 4.4, EvalL V (M(λx.x)) is also undefined. Yudai Urabe (Plotkin,1975) 19 / 29
In case (2b): M(λx.x) + − → M : (λx.x) (by Lem. 6.2) = M1 : (λx.x) (by (③)) + − → M : (λx.x) (by Lem. 6.2) + − → · · · Hence both EvalN(M(λx.x)) and EvalL V (M(λx.x)) diverge. Thus, (1) Value case: the CPS-translated term also yields the same value. (2a) Stuck case: the CPS-translated term also gets stuck. (2b) Divergent case: the CPS-translated term also diverges. Hence, Thm. 6.2 holds (Thm. 6.1 as well). Yudai Urabe (Plotkin,1975) 20 / 29
cf. Rabbit (Steele, 1978), ORBIT (N. Adams et al., 1986), SML/NJ Natural language semantics: cf. Chris Barker & Chung-Chieh Shan, Continuations and Natural Language, OUP (2014), Chap.12 Note: In the original formulation, a continuation represents the entire remaining computation, which makes CPS transformation a whole-program rewrite. Cf. The idea of delimited continuations. Yudai Urabe (Plotkin,1975) 21 / 29
higher-order programming languages. In Proc. ACM Annual Conference - Vol.2, pp.717–740. Christopher Strachey and Christopher P. Wadsworth. 1974. Continuations: A mathematical semantics for handling full jumps. Reprinted in Higher-Order and Symbolic Computation (2000). Gordon D. Plotkin. 1975. Call-by-name, call-by-value and the λ-calculus.Theoretical Computer Science 1(2):125 – 159. Janne van den Hout. 2018. Continuations in functional programming languages. BSc thesis, Radboud University. §4.2 gives a detailed account of CPS and colon translation, with examples (Ex.4.2.1, Ex.4.2.2). Gordon D. Plotkin. 2004. The origins of structural operational semantics. The Journal of Logic and Algebraic Programming, 60–61:3–15. Yudai Urabe (Plotkin,1975) 22 / 29
and Matthias Felleisen. The Essence of Compiling with Continuations. In PLDI’ 93, pp.237–247. Introduced ANF. Olivier Danvy and Andrzej Filinski. 1992. Representing Control: A Study of the CPS Transformation. Mathematical Structures in Computer Science, 2(4):361–391. Olivier Danvy and Lasse R. Nielsen. 2002. A First-Order One-Pass CPS Transformation. In FoSSaCS, LNCS 2303, pp.98–113. Springer. Defines a CPS/colon translation eliminating more administrative redexes than Plotkin’s original colon translation. Olivier Danvy. 2004. On Evaluation Contexts, Continuations, and the Rest of Computation. In Proc. Fourth ACM SIGPLAN Workshop on Continuations, Technical Report CSR-04-1, School of Computer Science, University of Birmingham, pp.13–23. Encyclopedia of Theoretical Computer Science (in Japanese). Yudai Urabe (Plotkin,1975) 23 / 29
Rabbit: A Compiler for Scheme. Technical Report, MIT. Norman Adams, David Kranz, Richard Kelsey, Jonathan Rees, Paul Hudak, and James Philbin. 1986. ORBIT: An Optimizing Compiler for Scheme. SIGPLAN Not. 21(7):219–233. Andrew W. Appel. 1992. Compiling with Continuations. Cambridge University Press. Chris Barker. 2002. Continuations and the Nature of Quantification. Natural Language Semantics 10:211–242. Chris Barker & Chung-Chieh Shan. 2014. Continuations and Natural Language. Oxford University Press. Masaya Taniguchi. 2022. Unprovability of Continuation-Passing Style Transformation in Lambek Calculus. ESSLLI Student Session, Galway. Yudai Urabe (Plotkin,1975) 24 / 29
λ-term that cannot be further β-reduced. Includes not only values but also stuck terms. Normal Order (p.126, p.142) Reduces the leftmost β-redex. Similar to call-by-name in programming languages, but call-by-name does not reduce inside λ-abstractions (weak reduction). Program (p.148) Closed terms. Theorems 6.1 and 6.2 hold for programs, while Theorem 6.4 applies to arbitrary terms. Yudai Urabe (Plotkin,1975) 26 / 29
(p.145) EvalN(λx. M) = (λx. M) EvalN(M N) = EvalN([N/x]M’ ) (if EvalN(M) = (λx. M))a aevalV is defined similarly. The correspondence between evalV and the SECD machine semantics EvalV is given in Thm.3.1. Yudai Urabe (Plotkin,1975) 27 / 29
Reduction → V (p.136) (λx. M) N → V [N/x]M (N is a value) M → M M N → M N M → M N M → N M N is a value Unified → for CBV and CBN From the proof of Lemma 6.1 (p.150) onward, → is used to denote both → V and → N . The definition of → N is on p.146. Yudai Urabe (Plotkin,1975) 28 / 29