continuations

Rewriting code with continuations

萝らか妹 提交于 2019-11-29 21:08:10
问题 I have some code that evaluates primitive programs. Program is a list of statements (expression, block, return statement). Result of evaluation is last evaluated expression. Also evaluator should properly treat return statement (i.e. stop evaluation after first occurrence of return ). To implement this logic I pass special callback function ( NextStep ) which make next evaluating step after current statement. I don't call next step when handling return statement: data Statement = Expr Int |

I just don't get continuations!

南笙酒味 提交于 2019-11-29 21:03:19
What are they and what are they good for? I do not have a CS degree and my background is VB6 -> ASP -> ASP.NET/C#. Can anyone explain it in a clear and concise manner? John Millikin Imagine if every single line in your program was a separate function. Each accepts, as a parameter, the next line/function to execute. Using this model, you can "pause" execution at any line and continue it later. You can also do inventive things like temporarily hop up the execution stack to retrieve a value, or save the current execution state to a database to retrieve later. You probably understand them better

What exactly is a “continuation prompt?”

北慕城南 提交于 2019-11-29 20:11:56
I'm trying to decipher the documentation call-with-continuation-prompt Applies proc to the given arg s with the current continuation extended by a prompt. The prompt is tagged by prompt-tag , which must be a result from either default-continuation-prompt-tag (the default) or make-continuation-prompt-tag . The result of proc is the result of the call-with-continuation-prompt call. I understand the part where it says "Applies proc to the given arg s with the current continuation" and then it's just gibberish from there. What does it even mean for a continuation to be "extended," and how does a

How to implement continuations?

前提是你 提交于 2019-11-29 18:45:05
I'm working on a Scheme interpreter written in C. Currently it uses the C runtime stack as its own stack, which is presenting a minor problem with implementing continuations. My current solution is manual copying of the C stack to the heap then copying it back when needed. Aside from not being standard C, this solution is hardly ideal. What is the simplest way to implement continuations for Scheme in C? I remember reading an article that may be of help to you: Cheney on the M.T.A. :-) Some implementations of Scheme I know of, such as SISC , allocate their call frames on the heap. @ollie: You

Serialization and the Yield statement

微笑、不失礼 提交于 2019-11-29 11:41:57
问题 Is it possible to serialize a method containing yield statements (or a class that contains such a method) such that when you rehydrate the class, the internal state of the generated iterator is retained? 回答1: Yes, you can do this. With caveats. An example of serializing a method with a yield , deserializing and continuing can be found here: http://www.agilekiwi.com/dotnet/CountingDemo.cs (Web Archive Link). In general, trying to serialize without doing some extra work will fail. This is

Does the yin yang continuations puzzle make sense in a typed language?

▼魔方 西西 提交于 2019-11-29 05:10:30
问题 This question is related to "How the yin-yang puzzle works?". The yin yang example of continuations in scheme looks like this, according to Wikipedia article: (let* ((yin ((lambda (cc) (display #\@) cc) (call-with-current-continuation (lambda (c) c)))) (yang ((lambda (cc) (display #\*) cc) (call-with-current-continuation (lambda (c) c))))) (yin yang)) I am trying to write an equivalent piece of code in a (edit: statically ) typed language, such as SML/NJ, but it is giving me typing errors. So

continuation in common lisp by macros — regarding an implemetation in OnLisp

最后都变了- 提交于 2019-11-29 04:09:42
问题 In On Lisp, p. 267, Paul Graham provides an implementation of continuation passing macros: (setq *cont* #'identity) (defmacro =lambda (parms &body body) `#'(lambda (*cont* ,@parms) ,@body)) (defmacro =defun (name parms &body body) (let ((f (intern (concatenate 'string "=" (symbol-name name))))) `(progn (defmacro ,name ,parms `(,',f *cont* ,,@parms)) (defun ,f (*cont* ,@parms) ,@body)))) (defmacro =bind (parms expr &body body) `(let ((*cont* #'(lambda ,parms ,@body))) ,expr)) (defmacro =values

Continuations and for comprehensions — what's the incompatibility?

我是研究僧i 提交于 2019-11-29 04:01:32
I am new to Scala and trying to wrap my head around continuations I'm trying to reproduce the yield return C# statement. Following this post , I have written the following code : package com.company.scalatest import scala.util.continuations._; object GenTest { val gen = new Generator[Int] { def produce = { yieldValue(1) yieldValue(2) yieldValue(3) yieldValue(42) } } // Does not compile :( // val gen2 = new Generator[Int] { // def produce = { // var ints = List(1, 2, 3, 42); // // ints.foreach((theInt) => yieldValue(theInt)); // } // } // But this works? val gen3 = new Generator[Int] { def

How to make callCC more dynamic?

假如想象 提交于 2019-11-29 02:02:29
问题 I thought the right type for ContT should be newtype ContT m a = ContT {runContT :: forall r. (a -> m r) -> m r} and other control operators shift :: Monad m => (forall r. (a -> ContT m r) -> ContT m r) -> ContT m a reset :: Monad m => ContT m a -> ContT m a callCC :: ((a -> (forall r. ContT m r)) -> ContT m a) -> ContT m a Unfortunately, I can not make callCC type check, and don't know how to do it. I managed to make shift and reset type check reset :: Monad m => ContT m a -> ContT m a reset

call/cc in Lua - Possible?

天大地大妈咪最大 提交于 2019-11-28 18:44:56
The Wikipedia article on Continuation says: "In any language which supports closures , it is possible to write programs in continuation passing style and manually implement call/cc ." Either that is true and I need to know how to do it or it is not true and that statement needs to be corrected. If this is true, please show me how to implement call/cc in Lua because I can't see how. I think I'd be able to implement call/cc manually if Lua had the coroutine.clone function as explained here . If closures are not enough to implement call/cc then what else is needed? The text below is optional