(en.wikipedia.org) Continuation-passing style - Wikipedia

ROAM_REFS: https://en.wikipedia.org/wiki/Continuation-passing_style

In functional programming, continuation-passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. This is contrasted with direct style, which is the usual style of programming. Gerald Jay Sussman and Guy L. Steele, Jr. coined the phrase in AI Memo 349 (1975), which sets out the first version of the programming language Scheme. John C. Reynolds gives a detailed account of the many discoveries of continuations.

A function written in continuation-passing style takes an extra argument: an explicit continuation; i.e., a function of one argument. When the CPS function has computed its result value, it "returns" it by calling the continuation function with this value as the argument. That means that when invoking a CPS function, the calling function is required to supply a procedure to be invoked with the subroutine's "return" value. Expressing code in this form makes a number of things explicit which are implicit in direct style. These include: procedure returns, which become apparent as calls to a continuation; intermediate values, which are all given names; order of argument evaluation, which is made explicit; and tail calls, which simply call a procedure with the same continuation, unmodified, that was passed to the caller.

Programs can be automatically transformed from direct style to CPS. Functional and logic compilers often use CPS as an intermediate representation where a compiler for an imperative or procedural programming language would use static single assignment form (SSA). SSA is formally equivalent to a subset of CPS (excluding non-local control flow, which does not occur when CPS is used as intermediate representation). Functional compilers can also use A-normal form (ANF) (but only for languages requiring eager evaluation), rather than with thunks (described in the examples below) in CPS. CPS is used more frequently by compilers than by programmers as a local or global style.

See Also

Local Graph

org-roam 8d97c9f0-dbd7-479c-aa43-625a81d41f68 (en.wikipedia.org) Continuation-passi... //en.wikipedia.org/wiki/Functional_programming https://en.wikipedia.org/wiki/Functional_programming 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Functional_programming //en.wikipedia.org/wiki/Control_flow https://en.wikipedia.org/wiki/Control_flow 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Control_flow //en.wikipedia.org/wiki/Continuation https://en.wikipedia.org/wiki/Continuation 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Continuation //en.wikipedia.org/wiki/Gerald_Jay_Sussman https://en.wikipedia.org/wiki/Gerald_Jay_Sussman 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Gerald_Jay_Sussman //en.wikipedia.org/wiki/Guy_L._Steele,_Jr. https://en.wikipedia.org/wiki/Guy_L._Steele,_Jr. 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Guy_L._Steele,_Jr. //en.wikipedia.org/wiki/AI_Memo https://en.wikipedia.org/wiki/AI_Memo 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/AI_Memo //en.wikipedia.org/wiki/Scheme_(programming_language) https://en.wikipedia.org/wiki/Scheme_(programming_language) 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Scheme_(programming_language) //en.wikipedia.org/wiki/John_C._Reynolds https://en.wikipedia.org/wiki/John_C._Reynolds 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/John_C._Reynolds //en.wikipedia.org/wiki/Tail_call https://en.wikipedia.org/wiki/Tail_call 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Tail_call //en.wikipedia.org/wiki/Logic_programming https://en.wikipedia.org/wiki/Logic_programming 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Logic_programming //en.wikipedia.org/wiki/Intermediate_representation https://en.wikipedia.org/wiki/Intermediate_representation 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Intermediate_representation //en.wikipedia.org/wiki/Imperative_programming https://en.wikipedia.org/wiki/Imperative_programming 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Imperative_programming //en.wikipedia.org/wiki/Procedural_programming https://en.wikipedia.org/wiki/Procedural_programming 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Procedural_programming //en.wikipedia.org/wiki/Programming_language https://en.wikipedia.org/wiki/Programming_language 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Programming_language //en.wikipedia.org/wiki/Static_single_assignment_form https://en.wikipedia.org/wiki/Static_single_assignment_form 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Static_single_assignment_form //en.wikipedia.org/wiki/A-normal_form https://en.wikipedia.org/wiki/A-normal_form 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/A-normal_form //en.wikipedia.org/wiki/Thunk https://en.wikipedia.org/wiki/Thunk 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Thunk //en.wikipedia.org/wiki/Compiler https://en.wikipedia.org/wiki/Compiler 8d97c9f0-dbd7-479c-aa43-625a81d41f68->//en.wikipedia.org/wiki/Compiler 3ec9ac9a-4d9a-4959-9c56-1c36d154180d (en.wikipedia.org) Thunk - Wikipedia 8d97c9f0-dbd7-479c-aa43-625a81d41f68->3ec9ac9a-4d9a-4959-9c56-1c36d154180d