# User:Benmachine/Cont

(Difference between revisions)

I found a bug with the <hask> tag. I put it on its own page so it doesn't ruin my user page.

It seems to me like
Cont
and
ContT
is way simpler than people make it. Essentially what it seems to be is the ability to give a name to the tail of a do-block. Consider this:
```contstuff :: Magic a
contstuff = do
thing1
thing2
-- Here I want to manipulate the rest of the computation.
-- So I want a magic function that will give me the rest of it to
-- play with.
magic \$ \rest ->
-- Now I can just do it (tm), or do it twice, or discard it, or
-- do it and then use the result to do it again... it's easy to
-- imagine why this might be useful.
thing3```
The question is, what type should
magic
have? Well, let's say the whole do-block results in a thing of type
r
(without thinking too hard about what this means). Then certainly the function we give
magic
should result in type
r
as well, since it can run that do-block. The function should also accept a single parameter, referring to the tail of the computation. That's the rest of the do-block, which has type
r
, right? Well, more or less, with one caveat: we might bind the result of
magic
:
```  x <- magic \$ \rest -> -- ...
thingInvolving x```
so the rest of the do-block has an
x
in it that we need to supply (as well as other variables, but
magic
already has access to those). So the rest of the do-block can be thought of as a bit like
a -> r
. Given access to the rest of that do-block, we need to produce something of type
r
. So our lambda has type
(a -> r) -> r
and hence
magic :: (a -> r) -> r -> Magic a
```Magic a = Cont r a
magic = Cont```

The thing with
Cont
is I could implement it way before I understood it, because the types have really only one implementation, but here's a way of using the intuition above to implement
Functor
without thinking about the types too much:
```instance Functor (Cont r) where
fmap f (Cont g) = -- ...```
Well, we've got to build a
Cont
value, and those always start the same way:
`  fmap f (Cont g) = Cont \$ \rest -> -- ...`
Now what? Well, remember what
g
is. It looks like
\rest -> stuffWith (rest val)
, where
val
is the 'value' of the computation (what would be bound with
<-
). So we want to give it a
rest
, but we don't want it to be called with the 'value' of the computation - we want
f
to be applied to it first. Well, that's easy:
`  fmap f (Cont g) = Cont \$ \rest -> g (\val -> rest (f val))`
Load it in `ghci` and the types check. Amazing! Emboldened, let's try
Applicative
```instance Applicative (Cont r) where
pure x = Cont \$ \rest -> -- ...```

We don't want to do anything special here. The rest of the computation wants a value, let's just give it one:

`  pure x = Cont \$ \rest -> rest x`
<*>
?
`  Cont f <*> Cont x = Cont \$ \rest -> -- ...`
This is a little trickier, but if we look at how we did
fmap
we can guess at how we get the function and the value out to apply one to the other:
`  Cont f <*> Cont x = Cont \$ \rest -> f (\fn -> x (\val -> rest (fn val)))`
is a harder challenge, but the same basic tactic applies. Hint: remember to unwrap the newtype with
runCont
,
case
, or
let
. The latter two might be easier.

The thing with
ContT
is that it's literally exactly the same trick. In fact I think the following definition works fine:
```newtype ContT r m a = ContT (Cont (m r) a)