Electric field gradient: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
[[]]
en>Monkbot
 
Line 1: Line 1:
{{Other uses}}
[http://Wordpress.org/search/Pleased Pleased] to meet you! My own name is Eusebio Ledbetter. It's not a common thing but what I desire doing is bottle counter tops collecting and now My partner have time to think about on new things. Software developing is how I support my family. My house is but in Vermont. I've been jogging on my website with respect to some time now. Check it out here: http://circuspartypanama.com<br><br>
In [[computer science]] and [[computer programming]], a '''continuation''' is an [[Abstraction (computer science)|abstract representation]] of the [[Control flow|control state]] of a [[computer program]]. A continuation [[Reification (computer science)|reifies]] the program control state, i.e. the continuation is a data structure that represents the computational process at a given point in the process' execution; the created data structure can be accessed by the programming language, instead of being hidden in the [[Run-time system|runtime environment]]. Continuations are useful for encoding other control mechanisms in programming languages such as [[Exception handling|exception]]s, [[Generator (computer science)|generators]], [[coroutine]]s, and so on.


The "'''current continuation'''" or "continuation of the computation step" is the continuation that, from the perspective of running code, would be derived from the current point in a program's execution. The term ''continuations'' can also be used to refer to '''first-class continuations''', which are constructs that give a [[programming language]] the ability to save the execution state at any point and return to that point at a later point in the program.
Also visit my blog; [http://circuspartypanama.com clash of clans cheats no survey no download]
 
==History==
The earliest description of continuations was made by [[Adriaan van Wijngaarden]] in September 1964. Wijngaarden spoke at the IFIP Working Conference on Formal Language Description Languages held in Baden bei Wien, Austria. As part of a formulation for an [[Algol 60]] preprocessor, he called for a transformation of proper procedures into continuation-passing style.<ref name="history_of_continuations">{{harvnb|Reynolds|1993}}</ref>
 
[[Christopher Strachey]], [[Christopher P. Wadsworth]] and [[John C. Reynolds]] brought the term ''continuation'' into prominence in their work in the field of [[denotational semantics]] that makes extensive use of continuations to allow sequential programs to be analysed in terms of [[functional programming]] semantics.<ref name="history_of_continuations"/>
 
[[Steve Russell]] invented the continuation in his second [[Lisp (programming language)|Lisp]] implementation for the [[IBM 704]], though he did not name it.<ref>{{cite web | url = http://www.computernostalgia.net/articles/steveRussell.htm | title = Steve "Slug" Russell | work = Computer History}}</ref> However there is no cross references about that and the given reference is a mere claim.
 
A complete history of the discovery of continuations is given by {{harv|Reynolds|1993}}.
 
==First-class continuations==
<!-- linked from redirect [[First-class continuations]] -->
 
First-class continuations are a language's ability to completely control the execution order of instructions. They can be used to jump to a function that produced the call to the current function, or to a function that has previously exited. One can think of a first-class continuation as saving the state of the program. However, it is important to note that true first-class continuations do not save program data, only the execution context. This is illustrated by the "continuation sandwich" description:
 
<blockquote>
''Say you're in the kitchen in front of the refrigerator, thinking about a sandwich. You take a continuation right there and stick it in your pocket. Then you get some turkey and bread out of the refrigerator and make yourself a sandwich, which is now sitting on the counter. You invoke the continuation in your pocket, and you find yourself standing in front of the refrigerator again, thinking about a sandwich. But fortunately, there's a sandwich on the counter, and all the materials used to make it are gone. So you eat it. :-)''<ref name="cont-sandwich">{{cite web|url=http://groups.google.com/group/perl.perl6.language/msg/b0cfa757f0ce1cfd|title=undo()? ("continuation sandwich" example)|last=Palmer|first=Luke|date=June 29, 2004|work=perl.perl6.language (newsgroup)|accessdate=2009-10-04}}</ref>
</blockquote>
 
[[Scheme (programming language)|Scheme]] was the first full production system, providing first "catch"<ref name="history_of_continuations"/> and then [[call-with-current-continuation|call/cc]]. Bruce Duba introduced call/cc into [[Standard ML|SML]].
 
Continuations are also used in models of computation including [[denotational semantics]], the [[Actor model]], [[process calculi]], and [[lambda calculus]]. These models rely on programmers or semantics engineers to write mathematical functions in the so-called [[continuation-passing style]]. This means that each function consumes a function that represents the rest of the computation relative to this function call. To return a value, the function calls this "continuation function" with a return value; to abort the computation it returns a value.
 
Functional programmers who write their programs in [[continuation-passing style]] gain the expressive power to manipulate the flow of control in arbitrary ways. The cost is that they must maintain the invariants of control and continuations by hand, which is a highly complex undertaking.
 
===Uses===
Continuations are useful because they simplify and clarify the implementation of several other common [[programming design pattern]]s,  including [[coroutine]]s (aka [[green thread]]s), and [[exception handling]]. Continuations provide the basic, low-level primitive unifying these otherwise seemingly unconnected patterns.  Continuations can provide elegant solutions to some difficult high-level problems, for example the problem of programming a web server that supports multiple pages, accessed by the user's use of the forward and back buttons and by following links.  The [[Seaside (software)|Seaside]] web framework in [[Smalltalk]] uses continuations to great effect, allowing one to program the web server in procedural style, by switching continuations  when switching pages.
 
More complex constructs exist as well, for which ''"continuations provide an elegant description"''.<ref name="history_of_continuations"/> For example, in [[C (programming language)|C]], [[setjmp]] can be used to jump from the middle of one [[Function (computer science)|function]] to another function, provided the second function lies deeper in the stack (if it is waiting for the first function to return, possibly among others). Other more complex examples include [[coroutine]]s in [[Simula|Simula 67]], [[Lua (programming language)|Lua]], and [[Perl]]; tasklets in [[Stackless Python]]; [[Generator (computer science)|generators]] in [[Icon (programming language)#Generators|Icon]] and [[Python (programming language)|Python]]; continuations in [[Scala (programming language)|Scala]] (starting in 2.8); [[Fiber (computer science)|fibers]] in [[Ruby (programming language)|Ruby]] (starting in 1.9.1); the [[backtracking]] mechanism in [[Prolog]]; [[Monad (functional programming)|monads]] in [[functional programming]]; and [[Thread (computer science)|threads]].
 
===Examples===
 
The [[Scheme (programming language)|Scheme]] programming language includes the control operator [[call-with-current-continuation]] (abbreviated as: call/cc) with which a Scheme program can manipulate the flow of control:
 
<source lang="scheme">
(define the-continuation #f)
 
(define (test)
  (let ((i 0))
    ; call/cc calls its first function argument, passing
    ; a continuation variable representing this point in
    ; the program as the argument to that function.
    ;
    ; In this case, the function argument assigns that
    ; continuation to the variable the-continuation.
    ;
    (call/cc (lambda (k) (set! the-continuation k)))
    ;
    ; The next time the-continuation is called, we start here.
    (set! i (+ i 1))
    i))
</source>
 
Defines a function <code>test</code> that sets <code>the-continuation</code> to the future execution state of itself:
 
<source lang="scheme">
> (test)
1
> (the-continuation)
2
> (the-continuation)
3
> ; stores the current continuation (which will print 4 next) away
> (define another-continuation the-continuation)
> (test) ; resets the-continuation
1
> (the-continuation)
2
> (another-continuation) ; uses the previously stored continuation
4
</source>
 
For a gentler introduction to this mechanism, see [[call-with-current-continuation]].
 
====Coroutines====
 
This example shows a possible usage of continuations to implement [[coroutines]] as separate threads.<ref>Haynes, C. T., Friedman, D. P., and Wand, M. 1984. Continuations and coroutines. In Proceedings of the 1984 ACM Symposium on LISP and Functional Programming (Austin, Texas, United States, August 06–08, 1984). LFP '84. ACM, New York, NY, 293-298.</ref>
 
<source lang="scheme">
;;; A naive queue for thread scheduling.
;;; It holds a list of continuations "waiting to run".
 
  (define *queue* '())
 
  (define (empty-queue?)
    (null? *queue*))
 
  (define (enqueue x)
    (set! *queue* (append *queue* (list x))))
 
  (define (dequeue)
    (let ((x (car *queue*)))
      (set! *queue* (cdr *queue*))
      x))
 
  ;;; This starts a new thread running (proc).
 
  (define (fork proc)
    (call/cc
      (lambda (k)
        (enqueue k)
        (proc))))
 
  ;;; This yields the processor to another thread, if there is one.
 
  (define (yield)
    (call/cc
      (lambda (k)
        (enqueue k)
        ((dequeue)))))
 
  ;;; This terminates the current thread, or the entire program
  ;;; if there are no other threads left.
 
  (define (thread-exit)
    (if (empty-queue?)
        (exit)
        ((dequeue))))
</source>
 
The functions defined above allow for defining and executing threads through [[Computer_multitasking#Cooperative_multitasking|cooperative multitasking]], i.e. threads that yield control to the next one in a queue:
 
<source lang="scheme">
  ;;; The body of some typical Scheme thread that does stuff:
 
  (define (do-stuff-n-print str)
    (lambda ()
      (let loop ((n 0))
        (format #t "~A ~A\n" str n)
        (yield)
        (loop (1+ n)))))
 
  ;;; Create two threads, and start them running.
  (fork (do-stuff-n-print "This is AAA"))
  (fork (do-stuff-n-print "Hello from BBB"))
  (thread-exit)
</source>
 
The previous code will produce this output:
  This is AAA 0
  Hello from BBB 0
  This is AAA 1
  Hello from BBB 1
  This is AAA 2
  Hello from BBB 2
  ...
 
===Implementation===
 
A program must allocate space in memory for the variables its functions use. Most programming languages use a [[call stack]] for storing the variables needed because it allows for fast and simple allocating and automatic deallocation of memory. Other programming languages use a [[Dynamic memory allocation|heap]] for this, which allows for flexibility at a higher cost for allocating and deallocating memory. Both of these implementations have benefits and drawbacks in the context of continuations.<ref>{{cite web | url = http://community.schemewiki.org/?call-with-current-continuation-for-C-programmers | title = Call with current continuation for C programmers | work = Community-Scheme-Wiki | date = 12 October 2008}}</ref>
 
===Programming language support===
Many programming languages exhibit first-class continuations under various names; specifically:
 
*[[Common Lisp]]: [http://common-lisp.net/project/cl-cont/ cl-cont]. One can also use custom macros
*[[C Sharp (programming language)|C#]] / [[VB.NET]]: <code>async</code> and <code>await</code>: “sign up the rest of method as the continuation, and then return to your caller immediately; the task will invoke the continuation when it completes.” [http://msdn.microsoft.com/en-us/vstudio/gg316360 Asynchronous Programming for C#]
*[[Java (programming language)|Java]]: [http://lightwolf.sourceforge.net/index.html Lightwolf]
*[[Rhino (JavaScript engine)|JavaScript Rhino]] : <code>Continuation</code>
*[[Factor (programming language)|Factor]]: <code>callcc0</code> and <code>callcc1</code>
*[[Haskell (programming language)|Haskell]]: The Continuation Monad in <code>[http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-Cont.html Control.Monad.Cont]</code>
*[[Haxe]]: [https://github.com/Atry/haxe-continuation haxe-continuation]
*[[Icon (programming language)|Icon]], [[Unicon (programming language)|Unicon]] : <code>create, suspend, @</code> operator: coexpressions
*[[Parrot virtual machine|Parrot]]: <code>Continuation</code> PMC; uses [[continuation-passing style]] for all control flow
*[[Perl]]: [https://metacpan.org/module/Coro Coro] and [https://metacpan.org/module/Continuity Continuity]
*[[Pico (programming language)|Pico]]: <code>call(exp())</code> and <code>continue(aContinuation, anyValue)</code>
*[[Ruby (programming language)|Ruby]]: <code>callcc</code>
*[[Scala (programming language)|Scala]]: <code>scala.util.continuations</code> provides <code>shift</code>/<code>reset</code>
*[[Scheme (programming language)|Scheme]]: <code>[[call-with-current-continuation]]</code> (commonly shortened to <code>call/cc</code>)
*[[Smalltalk]]: <code>Continuation currentDo:</code>, in most modern Smalltalk environments continuations can be implemented without additional VM support.
*[[Standard ML of New Jersey]]: <code>SMLofNJ.Cont.callcc</code>
*[[Unlambda]]: <code>c</code>, the flow control operation for call with current continuation
<!--
Please do not re-add the following entries:
* Python: generators are not first-class continuations
* Stackless Python: provides tasklets/coroutines only;  first-class continuations were a target for the first implementation, but dropped
-->
 
In any language which supports [[closure (computer science)|closures]] and [[tail recursion|proper tail calls]], it is possible to write programs in [[continuation-passing style]] and manually implement call/cc.  (In [[continuation-passing style]], call/cc becomes a simple function that can be written with [[lambda calculus|lambda]].)  This is a particularly common strategy in [[Haskell (programming language)|Haskell]], where it is easy to construct a "continuation-passing [[Monad (functional programming)|monad]]" (for example, the <code>Cont</code> monad and <code>ContT</code> monad transformer in the <code>mtl</code> library). The support for [[tail recursion|proper tail calls]] is needed because in [[continuation-passing style]] no function ever returns; ''all'' calls are tail calls.
 
==In Web development==
One area that has seen practical use of continuations is in [[Web programming]].<ref>[http://readscheme.org/xml-web/]</ref><ref>[http://www.double.co.nz/pdf/continuations.pdf]</ref> The use of continuations shields the programmer from the [[stateless server|stateless]] nature of the [[HTTP]] protocol.  In the traditional model of web programming, the lack of state is reflected in the program's structure, leading to code constructed around a model that lends itself very poorly to expressing computational problems. Thus continuations enable code that has the useful properties associated with [[inversion of control]], while avoiding its problems. <cite>[http://pagesperso-systeme.lip6.fr/Christian.Queinnec/PDF/www.pdf Inverting back the inversion of control or, Continuations versus page-centric programming]</cite> is a paper that provides a good introduction to continuations applied to web programming.
 
Some of the more popular continuation-aware [[Web server]]s are the [[Racket (programming language)|Racket]] [http://docs.racket-lang.org/web-server/ Web Server], the [http://common-lisp.net/project/ucw UnCommon Web Framework] and [http://common-lisp.net/project/cl-weblocks/ Weblocks Web framework] for [[Common Lisp]], the [[Seaside (software)|Seaside framework]] for [[Smalltalk]], [[Ocsigen|Ocsigen/Eliom]] for [[OCaml]], [https://metacpan.org/module/Continuity Continuity] for [[Perl]], [http://github.com/mneumann/wee Wee] for [[Ruby (programming language)|Ruby]], [http://www.talesframework.org/ Tales Framework] for [[Fantom_(programming_language)|Fantom]] and the [http://www.nagare.org/ Nagare framework] for [[Python (programming language)|Python]], [http://webtoolkit.eu/wt Wt] for [[C++]], [https://github.com/agocorona/MFlow MFlow] for [[Haskell]]. The [[Apache Cocoon]] [[Web application framework]] also provides continuations (see the [http://cocoon.apache.org/2.1/userdocs/flow/continuations.html Cocoon manual]).
 
==Kinds==
Support for continuations varies widely. A programming language supports ''re-invocable'' continuations if a continuation may be invoked repeatedly (even after it has already returned).  Re-invocable continuations were introduced by [[Peter J. Landin]] using his '''J''' (for Jump) operator that could transfer the flow of control back into the middle of a procedure invocation.  Re-invocable continuations have also been called "re-entrant" in the [[Racket (programming language)|Racket]] language.  However this use of the term "re-entrant" can be easily confused with its use in discussions of [[Thread (computer science)|multithreading]].
 
A more limited kind is the ''escape continuation'' that may be used to escape the current context to a surrounding one. Many languages which do not explicitly support continuations support [[exception handling]], which is equivalent to escape continuations and can be used for the same purposes. C's <code>[[setjmp/longjmp]]</code> are also equivalent: they can only be used to unwind the stack.  Escape continuations can also be used to implement [[tail call elimination]].
 
One generalization of continuations are [[delimited continuation]]s. Continuation operators like <code>call/cc</code> capture the ''entire'' remaining computation at a given point in the program and provide no way of delimiting this capture. Delimited continuation operators address this by providing two separate control mechanisms: a ''prompt'' that delimits a continuation operation and a ''reification'' operator such as <code>shift</code> or <code>control</code>. Continuations captured using delimited operators thus only represent a slice of the program context.
 
==Disadvantages==
Continuations are the functional expression of the [[GOTO]] statement, and the same caveats apply.<ref>{{cite web | url = http://www.jquigley.com/files/talks/continuations.pdf | title =Computational Continuations | first = John | last = Quigley |date=September 2007 | format = PDF | page = 38}}</ref>  While they are a sensible option in some special cases such as web programming, use of continuations can result in code that is difficult to follow. In fact, the [[esoteric programming language]] [[Unlambda]] includes [[call-with-current-continuation]] as one of its features solely because of its resistance to understanding{{Citation needed|date=July 2010}}. The external links below illustrate the concept in more detail.
 
==Linguistics==
 
In "Continuations and the nature of quantification", Chris Barker introduced the "continuation hypothesis", that
<blockquote>
some linguistic expressions (in particular, QNPs [quantificational noun phrases]) have denotations that manipulate their own continuations.<ref>Chris Barker, [http://www.semanticsarchive.net/Archive/902ad5f7/barker.continuations.pdf Continuations and the nature of quantification], 2002 Natural Language Semantics 10:211-242.</ref>
</blockquote>
Barker argued that this hypothesis could be used to explain phenomena such as ''duality of NP meaning'' (e.g., the fact that the QNP "everyone" behaves very differently from the non-quantificational noun phrase "Bob" in contributing towards the meaning of a sentence like "Alice sees [Bob/everyone]"), ''scope displacement'' (e.g., that "a raindrop fell on every car" is interpreted typically as <math>\forall c \exists r, \mbox{fell}(r,c)</math> rather than as <math>\exists r \forall c, \mbox{fell}(r,c)</math>), and ''scope ambiguity'' (that a sentence like "someone saw everyone" may be ambiguous between <math>\exists x \forall y, \mbox{saw}(x,y)</math> and <math>\forall y \exists x, \mbox{saw}(x,y)</math>).  He also observed that this idea is in a way just a natural extension of [[Montague grammar|Richard Montague's approach]] in "The Proper Treatment of Quantification in Ordinary English" (PTQ), writing that "with the benefit of hindsight, a limited form of continuation-passing is clearly discernible at the core of Montague’s (1973) PTQ treatment of NPs as generalized quantifiers".
 
The extent to which continuations can be used to explain other general phenomena in natural language is a topic of current research.<ref>See for example Chris Barker,  [http://www.cs.bham.ac.uk/~hxt/cw04/barker.pdf Continuations in Natural Language] (Continuations Workshop 2004), or Chung-chieh Shan,  [http://www.cs.rutgers.edu/~ccshan/brown/paper.pdf Linguistic Side Effects]
(in "Direct compositionality,'' ed. Chris Barker and Pauline Jacobson, pp. 132-163, Oxford University Press, 2007).</ref>
 
==See also==
*[[Call-with-current-continuation]]
*[[Closure (computer science)|Closure]]
*[[COMEFROM]]
*[[Continuation-passing style]]
*[[Control flow]]
*[[Coroutine]]
*[[Delimited continuation]]
*[[Denotational semantics]]
*[[GOTO]]
*[[Spaghetti stack]]
 
==References==
{{Refimprove|date=July 2010}}
{{Reflist}}
 
==Further reading==
*[[Peter Landin]].  ''A Generalization of Jumps and Labels''  Report.  UNIVAC Systems Programming Research.  August 1965.  Reprinted in Higher Order and Symbolic Computation, 11(2):125-143, 1998, with a foreword by Hayo Thielecke.
*[[Drew McDermott]] and [[Gerry Sussman]]. ''The Conniver Reference Manual''  MIT AI Memo 259.  May 1972.
*[[Daniel Bobrow]]: ''A Model for Control Structures for Artificial Intelligence Programming Languages'' IJCAI 1973.
*[[Carl Hewitt]], [[Peter Bishop]] and [[Richard Steiger]]. ''A Universal Modular Actor Formalism for Artificial Intelligence''  IJCAI 1973.
*[[Christopher Strachey]] and [[Christopher P. Wadsworth]].  ''Continuations: a Mathematical semantics for handling full jumps'' Technical Monograph PRG-11.  Oxford University Computing Laboratory.  January 1974.  Reprinted in Higher Order and Symbolic Computation, 13(1/2):135—152, 2000, with a foreword by Christopher P. Wadsworth.
*[[John C. Reynolds]]. ''Definitional Interpreters for Higher-Order Programming Languages'' Proceedings of 25th ACM National Conference, pp.&nbsp;717–740, 1972. Reprinted in Higher-Order and Symbolic Computation 11(4):363-397, 1998, with a foreword.
*John C. Reynolds. ''On the Relation between Direct and Continuation Semantics'' Proceedings of Second Colloquium on Automata, Languages, and Programming.  LNCS Vol. 14, pp.&nbsp;141–156, 1974.
* {{cite journal|first=John C.|last=Reynolds|year=1993|url=ftp://ftp.cs.cmu.edu/user/jcr/histcont.pdf|title=The discoveries of continuations|journal=[[Lisp and Symbolic Computation]]|volume=6|issue=3/4|pages=233&ndash;248|ref=harv}}
*[[Gerald Sussman]] and [[Guy Steele]]. ''SCHEME: An Interpreter for Extended Lambda Calculus'' AI Memo 349, MIT Artificial Intelligence Laboratory, Cambridge, Massachusetts, December 1975. Reprinted in Higher-Order and Symbolic Computation 11(4):405-439, 1998, with a foreword.
*[[Robert Hieb]], [[R. Kent Dybvig]], [[Carl Bruggeman]]. ''Representing Control in the Presence of First-Class Continuations'' Proceedings of the ACM SIGPLAN '90 Conference on Programming Language Design and Implementation, pp.&nbsp;66–77.
*[[Will Clinger]], [[Anne Hartheimer]], [[Eric Ost]]. ''Implementation Strategies for Continuations'' Proceedings of the 1988 ACM conference on LISP and Functional Programming, pp.&nbsp;124–131, 1988.  Journal version: Higher-Order and Symbolic Computation, 12(1):7-45, 1999.
 
==External links==
* [[Association for Computing Machinery|ACM]] [[SIGPLAN]] [http://logic.cs.tsukuba.ac.jp/cw2011/ Workshop on Continuations 2011] at the [[ICFP]].
*[http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons Continuations for Curmudgeons] by Sam Ruby
*[http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-15.html#node_chap_13 Teach Yourself Scheme in Fixnum Days] by Dorai Sitaram features a nice chapter on continuations.
*[http://www.stackless.com/spcpaper.htm Continuations and Stackless Python] by Christian Tismer
*[http://www.cs.bham.ac.uk/~hxt/cw04/cw04-program.html On-line proceedings of the Fourth ACM SIGPLAN Workshop on Continuations]
*[http://www.brics.dk/~cw97/ On-line proceedings of the Second ACM SIGPLAN Workshop on Continuations]
*[http://www.cs.bham.ac.uk/~hxt/research/Logiccolumn8.pdf Continuation, functions and jumps]
*[http://okmij.org/ftp/continuations/ http://okmij.org/ftp/continuations/] by Oleg Kiselyov
*[http://wiki.apache.org/cocoon/RhinoWithContinuations Rhino With Continuations]
*[http://rifers.org/wiki/display/RIFE/Web+continuations Continuations in pure Java] from the [[RIFE]] web application framework
*[http://rifers.org/theater/debugging_continuations Debugging continuations in pure Java] from the [[RIFE]] web application framework
* [http://mail.python.org/pipermail/python-dev/1999-July/000467.html Comparison of generators, coroutines, and continuations, source of the above example]
 
[[Category:Continuations| ]]
[[Category:Control flow]]
[[Category:Articles with example Scheme code]]

Latest revision as of 23:12, 7 May 2014

Pleased to meet you! My own name is Eusebio Ledbetter. It's not a common thing but what I desire doing is bottle counter tops collecting and now My partner have time to think about on new things. Software developing is how I support my family. My house is but in Vermont. I've been jogging on my website with respect to some time now. Check it out here: http://circuspartypanama.com

Also visit my blog; clash of clans cheats no survey no download