25 Aug 2012 06:35

## Combining State and List Monads

```Dear Cafe,

It seems to me there should be some simple way of doing this, but thus
far it eludes me.  I am trying to combine the State and List monads to
do the following:

countCalls = do
a <- [1..2]
b <- [1..2]
modify (+1)
return (a,b)

where with some combination of ListT, StateT, List, State, or who
knows what would result in:

([(1,1),(1,2),(2,1),(2,2)],4)

assuming we initialize the state to 0

Is there any way to make this happen?

Henry Laxen
```
25 Aug 2012 07:50

### Re: Combining State and List Monads

Hello,

the state should be inside, to count, so type is ListT (State Int) (Int,Int)

the runner is then runState (runListT countCalls) 0

but [] is not of type ListT m so you need to wrap it in ListT . return

countCalls :: ListT (State Int) (Int,Int)
countCalls = do
a <- ListT . return \$ [1..2]
b <- ListT . return \$ [1..2]
modify (+1)
return (a,b)

regards

paolino

2012/8/25 Henry Laxen
Dear Cafe,

It seems to me there should be some simple way of doing this, but thus
far it eludes me.  I am trying to combine the State and List monads to
do the following:

countCalls = do
a <- [1..2]
b <- [1..2]
modify (+1)
return (a,b)

where with some combination of ListT, StateT, List, State, or who
knows what would result in:

([(1,1),(1,2),(2,1),(2,2)],4)

assuming we initialize the state to 0

Is there any way to make this happen?

Henry Laxen

_______________________________________________

```_______________________________________________
```
25 Aug 2012 07:51

### Re: Combining State and List Monads

```Hello Henry,

In such cases, it is often worth thinking about how you would implement
such a scheme manually, without using pre-existing monads.  You will
quickly see that the two candidate types:

s -> ([a], s)
[s -> (a, s)]

both will not work (exercise: what semantics do they give?)  In fact,
you must use continuation passing style, and you must "resume" the
computation with the latest state value you would extracted from the
last run.  See the LogicT monad for how to implement list-like monads in
continuation passing style.

Cheers,
Edward

Excerpts from Henry Laxen's message of Sat Aug 25 00:35:37 -0400 2012:
> Dear Cafe,
>
> It seems to me there should be some simple way of doing this, but thus
> far it eludes me.  I am trying to combine the State and List monads to
> do the following:
>
> countCalls = do
>   a <- [1..2]
>   b <- [1..2]
>   modify (+1)
>   return (a,b)
>
>
> where with some combination of ListT, StateT, List, State, or who
> knows what would result in:
>
> ([(1,1),(1,2),(2,1),(2,2)],4)
>
> assuming we initialize the state to 0
>
> Is there any way to make this happen?
>
> Henry Laxen
>
```
25 Aug 2012 07:58

### Re: Combining State and List Monads

```Ah, egg in my face, I appear to have misremembered how ListT is implemented ^_^

Edward

Excerpts from Edward Z. Yang's message of Sat Aug 25 01:51:40 -0400 2012:
> Hello Henry,
>
> In such cases, it is often worth thinking about how you would implement
> such a scheme manually, without using pre-existing monads.  You will
> quickly see that the two candidate types:
>
>     s -> ([a], s)
>     [s -> (a, s)]
>
> both will not work (exercise: what semantics do they give?)  In fact,
> you must use continuation passing style, and you must "resume" the
> computation with the latest state value you would extracted from the
> last run.  See the LogicT monad for how to implement list-like monads in
> continuation passing style.
>
> Cheers,
> Edward
>
> Excerpts from Henry Laxen's message of Sat Aug 25 00:35:37 -0400 2012:
> > Dear Cafe,
> >
> > It seems to me there should be some simple way of doing this, but thus
> > far it eludes me.  I am trying to combine the State and List monads to
> > do the following:
> >
> > countCalls = do
> >   a <- [1..2]
> >   b <- [1..2]
> >   modify (+1)
> >   return (a,b)
> >
> >
> > where with some combination of ListT, StateT, List, State, or who
> > knows what would result in:
> >
> > ([(1,1),(1,2),(2,1),(2,2)],4)
> >
> > assuming we initialize the state to 0
> >
> > Is there any way to make this happen?