FACTOID # 145: Three of the top ten countries for GDP per capita are island nations: Bermuda, Cayman Islands, and Iceland.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Continuation passing style

Continuation passing style (CPS) is a term used within functional programming to describe a style of programming wherein functions never return. Instead of "returning" values as in the more familiar direct style, a function written in CPS takes an explicit continuation argument which is meant to receive the result of the computation performed within the function. Thus, when a subroutine is invoked within a CPS function, the calling function is required to supply a procedure to be invoked with the subroutine's "return" value. Due to its "non-intuitive" nature, CPS is used more frequently by compilers than by programmers. Functional and logic compilers often use CPS as an intermediate representation where a compiler for an imperative or procedural language might employ SSA. The Haskell programming language logo Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ... In computing, a continuation is a representation of an execution point (for example, the instruction pointer and stack frame). ... In computing, an intermediate representation is a data structure that is constructed from input data to a program, and from which part or all of the output data of the program is constructed in turn. ... Wikipedia does not yet have an article with this exact name. ...


In Scheme, the simplest of direct-style functions is the identity function: The Knights of the Lambda Calculus recursive emblem celebrates Schemes theoretical foundation, the lambda calculus. ...

 (lambda (x) x) 

which in CPS becomes:

 (lambda (x return) (return x)) 

where return is clearly the continuation argument.


Increasing the complexity, here is another Scheme example which mixes direct style and CPS:

Direct style
Continuation passing style
 (define (mysqrt x) (sqrt x)) (display (mysqrt 4)) 
 (define (mysqrt x k) (k (sqrt x))) (mysqrt 4 display) 
 (+ (mysqrt 4) 2) 
 (mysqrt 4 (lambda (x) (+ x 2))) 
 (define (factorial n) (if (<= n 1) 1 (* n (factorial (- n 1))))) (+ (factorial 4) 2) 
 (define (factorial n k) (if (<= n 1) (k 1) (factorial (- n 1) (lambda (ret) (k (* n ret)))))) (factorial 4 (lambda (x) (+ x 2))) 

Continuation passing style can also be used to implement continuations in a functional language that does not feature first-class continuations but does have first class functions. In computing, a continuation is a representation of an execution point (for example, the instruction pointer and stack frame). ...


As continuation passing style renders return values virtually useless, it can also be used to eliminate the need for a runtime stack. Several compilers and interpreters for functional programming languages use this ability internally in novel ways.


Outside of computer science, CPS is of more general interest as an alternative to the conventional method of composing simple expressions into complex expressions. For example, within linguistic semantics, Rober Barker has suggested that specifying the truth conditions of sentences using CPS might explain certain ambiguities found in natural language [1]. Wikibooks Wikiversity has more about this subject: School of Computer Science Open Directory Project: Computer Science Downloadable Science and Computer Science books Collection of Computer Science Bibliographies Belief that title science in computer science is inappropriate nacho Categories: Computer science ... In the main, semantics (from the Greek semantikos, or significant meaning, derived from sema, sign) is the study of meaning, in some sense of that term. ... The term natural language is used to distinguish languages spoken by humans for general-purpose communication from constructs such as computer-programming languages or the languages used in the study of formal logic, especially mathematical logic. ...


See also

Andrew Appel describes the construction of a CPS-based compiler for ML in:


Appel, Andrew W. (1992) Compiling with Continuations, Cambridge University Press. ISBN 0521416957



 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m