continuations

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

允我心安 提交于 2019-11-28 17:04:34
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 workflow script, which calls the Wait condition in the engine, waiting for some condition that will be

What exactly is a “continuation prompt?”

别说谁变了你拦得住时间么 提交于 2019-11-28 16:02:16
问题 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

How to implement continuations?

人盡茶涼 提交于 2019-11-28 13:36:57
问题 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? 回答1: I remember reading an article that may be of help to you: Cheney on the M.T.A. :-)

How do I enable continuations in Scala?

血红的双手。 提交于 2019-11-28 05:53:38
Question says it all. (Yet, the details of how to get access to the shift and reset operations has changed over the years. Old blog entries and Stack Overflow answers may have out of date information.) See also What are Scala continuations and why use them? which talks about what you might want to do with shift and reset once you have them. Scala 2.11 The easiest way is to use sbt: scalaVersion := "2.11.6" autoCompilerPlugins := true addCompilerPlugin( "org.scala-lang.plugins" % "scala-continuations-plugin_2.11.6" % "1.0.2") libraryDependencies += "org.scala-lang.plugins" %% "scala

Constructing efficient monad instances on `Set` (and other containers with constraints) using the continuation monad

主宰稳场 提交于 2019-11-28 05:48:14
Set , similarly to [] has a perfectly defined monadic operations. The problem is that they require that the values satisfy Ord constraint, and so it's impossible to define return and >>= without any constraints. The same problem applies to many other data structures that require some kind of constraints on possible values. The standard trick (suggested to me in a haskell-cafe post ) is to wrap Set into the continuation monad. ContT doesn't care if the underlying type functor has any constraints. The constraints become only needed when wrapping/unwrapping Set s into/from continuations: import

Goto in Haskell: Can anyone explain this seemingly insane effect of continuation monad usage?

谁说我不能喝 提交于 2019-11-28 03:23:44
From this thread (Control.Monad.Cont fun, 2005), Tomasz Zielonka introduced a function (commented in a clear and nice manner by Thomas Jäger). Tomasz takes the argument (a function) of a callCC body and returns it for later usage with the following two definitions: import Control.Monad.Cont ... getCC :: MonadCont m => m (m a) getCC = callCC (\c -> let x = c x in return x) getCC' :: MonadCont m => a -> m (a, a -> m b) getCC' x0 = callCC (\c -> let f x = c (x, f) in return (x0, f)) Those are also mentioned in Haskellwiki . Using them, you can resemble goto semantics in haskell which looks really

What is call/cc?

拥有回忆 提交于 2019-11-28 03:23:20
I've tried several times to grasp the concept of continuations and call/cc . Every single attempt was a failure. Can somebody please explain me these concepts, ideally with more realistic examples than these on Wikipedia or in other SO posts. I have background in web programming and OOP. I also understand 6502 assembly and had a minor randez-vous with Erlang. However still, I can't wrap my head around call/cc. temoto Look, i've found this Continuation Passing Style best description on this topic. Here's stripped of details copy of that article: Author: Marijn Haverbeke Date: July 24th 2007

How and why does the Haskell Cont monad work?

廉价感情. 提交于 2019-11-28 02:50:51
This is how the Cont monad is defined: newtype Cont r a = Cont { runCont :: (a -> r) -> r } instance Monad (Cont r) where return a = Cont ($ a) m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c Could you explain how and why this works? What is it doing? The first thing to realize about the continuation monad is that, fundamentally, it's not really doing anything at all. It's true! The basic idea of a continuation in general is that it represents the rest of a computation . Say we have an expression like this: foo (bar x y) z . Now, extract just the parenthesized portion, bar x y --this

Implement yield and send in Scheme

扶醉桌前 提交于 2019-11-27 23:19:47
I'm trying to port yield and yield from from Python to Scheme. Here is an implementation I've done: (define (coroutine routine) (let ((current routine) (status 'new)) (lambda* (#:optional value) (let ((continuation-and-value (call/cc (lambda (return) (let ((returner (lambda (value) (call/cc (lambda (next) (return (cons next value))))))) (if (equal? status 'new) (begin (set! status 'running) (current returner)) (current (cons value returner))) (set! status 'dead)))))) (if (pair? continuation-and-value) (begin (set! current (car continuation-and-value)) (cdr continuation-and-value)) continuation

call-with-current-continuation - state saving concept

蹲街弑〆低调 提交于 2019-11-27 22:31:37
After reading The Seasoned Schemer I felt I understood call/cc properly. But, after seeing some WOW tricks with call/cc I found I was wrong. (define cc 0) (define (f) (call/cc (lambda (k) (set! cc k) 3))) (+ 1 2 4 (f)) ; eval's to 10 (cc 13) ; eval's to 20 That perfectly match my understanding. I think when I reach a call/cc call I am just saving the program state. and calling the function next to it with a function. If that function ( k ) is called from somewhere than I just replacing the entire (call/cc ...) stuff with the parameter given to it. The above program seems to worked that way too