continuations

Is there a way to chain functions like withCString?

谁都会走 提交于 2019-12-04 00:33:45
Is there a way to chain functions like withCString ? By that I mean any function that looks something like f :: Foo -> (CFoo -> IO a) -> IO a . For example, lets say there is a function cFunc :: CString -> CFoo -> CBar -> IO () Usualy, I would do something like: haskellFunc string foo bar = withCString string $ \ cString -> withCFoo foo $ \ cFoo -> withCBar bar $ \ cBar -> cFunc cString cFoo cBar But i would like to do something like: haskellFunc = (withCString |.| withCFoo |.| withCBar) cFunc with some appropriate composition operator |.| . I'm writing library with a lot of C bindings, and

Correct terminology for continuations

余生长醉 提交于 2019-12-03 23:28:28
I've been poking around continuations recently, and I got confused about the correct terminology. Here Gabriel Gonzalez says: A Haskell continuation has the following type: newtype Cont r a = Cont { runCont :: (a -> r) -> r } i.e. the whole (a -> r) -> r thing is the continuation (sans the wrapping) The wikipedia article seems to support this idea by saying a continuation is an abstract representation of the control state of a computer program. However, here the authors say that Continuations are functions that represent "the remaining computation to do." but that would only be the (a->r) part

How to handle Jetty exception - a long running HTTP request times out, but the process it calls never terminates and Jetty is unhappy

℡╲_俬逩灬. 提交于 2019-12-03 13:02:01
I have a Jetty server handling long running HTTP requests- the responses are generated by an a different process X and end up in a collector hash which Jetty requests periodically check. There are 3 cases: Process X finishes before the timeout period of the HTTP request - no problem Process X finishes after the timeout period of the request - no problem Process X never finishes - below exception occurs How do I detect this situation (3) and prevent the exception while allowing the other two cases to properly work? Exception: 2012-06-18 00:13:31.055:WARN:oejut.QueuedThreadPool: java.lang

How to split and dispatch an async control-flow using Continuations?

我们两清 提交于 2019-12-03 12:50:50
问题 I have an asynchronous control-flow like the following: ActorA ! DoA(dataA, callback1, callbackOnErrorA) def callback1() = { ... ActorB ! DoB(dataB, callback2, callbackOnErrorB) } def callback2() = { ActorC ! DoC(dataC, callback3, callbackOnErrorC) } ... How would I divide this flow into several parts (continuations) and sequentially dispatch these to different actors (or threads/tasks) while maintaining the overall state? Any hint appreciated, Thanks 回答1: This is very simplified, but shows

Use MonadRef to implement MonadCont

拥有回忆 提交于 2019-12-03 09:17:06
There is a well known issue that we cannot use forall types in the Cont return type . However it should be OK to have the following definition: class Monad m => MonadCont' m where callCC' :: ((a -> forall b. m b) -> m a) -> m a shift :: (forall r.(a -> m r) -> m r) -> m a reset :: m a -> m a and then find an instance that makes sense. In this paper the author claimed that we can implement MonadFix on top of ContT r m providing that m implemented MonadFix and MonadRef . But I think if we do have a MonadRef we can actually implement callCC' above like the following: --satisfy law: mzero >>= f ==

Using Cont to acquire values from the future and the past

帅比萌擦擦* 提交于 2019-12-03 06:56:06
问题 I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again.

Precise flow control in Haskell

偶尔善良 提交于 2019-12-03 06:50:01
问题 The Idea Hello! I'm trying to implement in Haskell an image processing library based on dataflow ideology. I've got a problem connected to how I want to handle the flow of control. The main idea is to introduce a time . The time is a Float , which could be accessed anywhere in the code (you can think of it like about State monad, but a little funnier). The funny thing about it, is that we can use timeShift operation on results, affecting the time corresponding operations would see. An example

C# first class continuation via C++ interop or some other way?

不问归期 提交于 2019-12-03 06:34:20
问题 We have a very high performance multitasking, near real-time C# application. This performance was achieved primarily by implementing cooperative multitasking in-house with a home grown scheduler. This is often called micro-threads. In this system all the tasks communicate with other tasks via queues. The specific problem that we have seems to only be solvable via first class continuations which C# does not support. Specifically the problem arises in 2 cases dealing with queues. Whenever any

Are continuations monads?

馋奶兔 提交于 2019-12-03 05:55:08
问题 Can continuations be said to be monads? Are they a subset of monads or are they simply a way of implementing monads? Edit: Or maybe I got it wrong and monads is a more abstract concept than continuations ? (So I'm really comparing apples to oranges here) 回答1: Briefly, since the 'bind' of a monad takes an effective continuation (a lambda of the 'rest of the computation') as an argument, monads are continuations in that sense. On the flip side, continuation-passing style can be effectively

Continuations in Clojure

允我心安 提交于 2019-12-03 02:42:50
问题 I read somewhere where rich hickey said: "I think continuations might be neat in theory, but not in practice" I am not familiar with clojure. 1. Does clojure have continuations? 2. If no, don't you need continuations? I have seen a lot of good examples especially from this guy. What is the alternative? 3. If yes, is there a documentation? 回答1: When talking about continuations, you’ll have to distinguish between two different kinds of them: First-class continuations – Continuation-support that