Is there a way to capture the continuation in the notes?

Since the following do block:

do x <- foo y <- bar return x + y 

played out in the following form:

 foo >>= (\x -> bar >>= (\y -> return x + y)) 

aren't \x -> ... and y -> ... valid extensions here?

I was wondering if there is a way to capture the continuation in the bind definition, but I cannot use the types correctly. I.e:

 data Pause a = Pause a | Stop instance Monad Pause where return x = Stop m >>= k = Pause k -- this doesn't work of course 

Now I tried to get confused with types:

 data Pause a = Pause a (a -> Pause ???) | Stop ------- k ------ 

But that will not work either. Is there a way to capture these implicit continuations?

By the way, I know about the Cont monad, I'm just experimenting and sorting things out.

+4
source share
2 answers

OK. I'm not sure, but let me make a few points. I’m not quite sure what this should mean to seize the sequel. You could, for example, capture the entire do block in the structure:

 {-# LANGUAGE ExistentialQuantification #-} import Control.Monad data MonadExp b = Return b | forall a. Bind (MonadExp a) (a -> MonadExp b) instance Monad MonadExp where return x = Return x f >>= g = Bind fg 

For instance:

 block :: MonadExp Int block = do x <- return 1 y <- return 2 return $ x + y instance Show (MonadExp a) where show (Return _) = "Return _" show (Bind _ _) = "Bind _ _" print block >> Bind _ _ 

And then evaluate it all:

 finish :: MonadExp a -> a finish (Return x) = x finish (Bind fg) = finish $ g (finish f) print $ finish block >> 3 

Or go through it and see the parts

 step :: MonadExp a -> MonadExp a step (Return _) = error "At the end" step (Bind fg) = g $ finish f print $ step block >> Bind _ _ print $ step $ step block >> Return _ 

Well, now that I think about it more, this is probably not what you are asking. But perhaps this will help you think.

+1
source

Well, I don’t know if your lambdas are extensions in the strict sense of the word, but they also look like this concept in my eyes.

But note that if they are continuations, then the conditionally released monadic code is already written in the style of continuation of passage (CPS). The usual concept of a control operator that “captures” a continuation is based on direct-style programs; The "captured" continuation is only implicit in the direct-style program, but the CPS conversion makes it explicit.

Since the highlighted monadic code is already in CPS or something like that, well, maybe the way to formulate your question is whether the monadic code can express some flow control tricks that CPS code might have. Typically, these tricks come down to the idea that, although in CPS mode, to end a function, it usually ends by causing it to continue, the function can replace its continuation with another choice. This continuation of the replacement can be built with reference to the original continuation, so that it can, in turn, “restore” the original, if it decides. So, for example, coroutines are executed as a mutual replace / restore cycle.

And looked in this light, I think that your answer is basically absent; CPS requires that in foo >>= bar , foo should be able to choose whether to call bar at all, and foo should be available to replace bar , but (>>=) by itself do not suggest a mechanism for foo , and, more importantly, (>>=) controls the flow of execution, not foo . Some specific monads implement parts or all of this (for example, the Maybe monad allows foo to refuse to execute bar , creating the result Nothing ), but others do not.

The closest I can get is to discard (>>=) and use this instead:

 -- | Execute action @ foo@ with its "continuation" @ bar@. callCC :: Monad m => ((a -> mb) -> mb) -> (a -> mb) -> mb foo `callCC` bar = foo bar 

Here foo can choose whether to use bar . But notice that this callCC really simple ($) !

+1
source

All Articles