continuations

C++ force stack unwinding inside function

非 Y 不嫁゛ 提交于 2019-12-01 09:51:33
问题 I'm in the process of learning C++ and currently I'm fiddling with the following code: class Bar; struct Callback { virtual void Continue(Bar&) = 0; }; // ... void Foo(Bar& _x, Callback& result) { // Do stuff with _x if(/* some condition */) { // TODO: Force unwind of stack result.Continue(_x); return; } // Do more stuff with _x if(/* some other condition */) { // TODO: Force unwind of stack result.Continue(_x); return; } // TODO: Force unwind of stack Bar y; // allocate something on the

How to interpret callCC in Haskell?

倖福魔咒の 提交于 2019-12-01 09:16:50
In Scheme executing a continuation obtained from a call/cc effectively jumps back to that initial call/cc and reinstates the saved call stack. I just started learning Haskell and I am trying to figure out how to comprehend callCC . That is try to comprehend callCC in terms of understanding of Scheme's call/cc . The implementation of callCC is callCC f = cont $ \h -> runCont (f (\a -> cont $ \_ -> h a)) h As far as I can tell there is nothing mentioned relating to call stacks saved or reinstated. How does one interpret callCC in Haskell coming from familiarity with Scheme's call/cc . Edit:

How to interpret callCC in Haskell?

若如初见. 提交于 2019-12-01 06:11:02
问题 In Scheme executing a continuation obtained from a call/cc effectively jumps back to that initial call/cc and reinstates the saved call stack. I just started learning Haskell and I am trying to figure out how to comprehend callCC . That is try to comprehend callCC in terms of understanding of Scheme's call/cc . The implementation of callCC is callCC f = cont $ \h -> runCont (f (\a -> cont $ \_ -> h a)) h As far as I can tell there is nothing mentioned relating to call stacks saved or

Rewriting code with continuations

依然范特西╮ 提交于 2019-11-30 21:00:06
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 | Block [Statement] | Return Int deriving (Show, Eq) data Value = Undefined | Value Int deriving (Show, Eq

I just don't get continuations!

冷暖自知 提交于 2019-11-30 10:29:24
问题 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? 回答1: 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

Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 06:20:47
问题 Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario: You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that are calls to the workflow engine. A blocking call, such as a wait or join sets up a listener condition in an event-dispatching engine with a persistent backing store of some sort. You have a

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

穿精又带淫゛_ 提交于 2019-11-30 05:28:52
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 (&rest retvals) `(funcall *cont* ,@retvals)) The following code to traverse a tree t2 for each leaf

How to make callCC more dynamic?

不羁岁月 提交于 2019-11-30 04:55:27
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 e = ContT $ \ k -> runContT e return >>= k shift :: Monad m => (forall r. (a -> ContT m r) -> ContT m

Can call-with-current-continuation be implemented only with lambdas and closures?

谁说胖子不能爱 提交于 2019-11-30 03:39:11
Does anyone know if call/cc can be implemented with just lambdas and closures? It seems that call/cc interrupts the program's flow (like an exception) but lambdas and closures can't do that. Therefore I think call/cc can't be implemented via lambdas and closures. Any more ideas? The question is not particularly clear, since what exactly does "implemented with just lambdas and closures" mean? In any case, continuations can be used in any language with closures by manually writing in continuation passing style . Then automatic translation into this form can be implemented by extending the

generator/block to iterator/stream conversion

匆匆过客 提交于 2019-11-30 01:16:40
问题 Basically I want to convert this: def data(block: T => Unit) to a Stream (dataToStream is a hypothetical function that do this conversion): val dataStream: Stream[T] = dataToStream(data) I suppose this problem could be resolved by continuations: // let's assume that we don't know how data is implemented // we just know that it generates integers def data(block: Int => Unit) { for (i <- 0 to 10) block(i) } // here we can print all data integers data { i => println(i) } // >> but what we really