[Haskell-cafe] Trouble using State Monad.

Ryan Ingram ryani.spam at gmail.com
Wed Oct 12 00:33:20 CEST 2011


Your filter type isn't a Monad.

In particular

bind :: (a -> EitherT e (State FilterState) a) -> (a -> b -> EitherT e
(State FilterState) b) -> b -> EitherT e (State FilterState) b

can't be implemented, as you have no place to grab an 'a' to pass to the
initial computation.

If you fix the input type, you can do

newtype Filter r e a = F {
    runFilter :: r -> EitherT e (State FilterState) a
}

which is isomorphic to

newtype Filter r e a = F {
    runFilter :: ReaderT r (EitherT e (State FilterState)) a
}

which newtype deriving will be able to deal with easily.

  -- ryan


On Sat, Oct 8, 2011 at 4:28 PM, Captain Freako <capn.freako at gmail.com>wrote:

> Hi all,
>
> I'm trying to use the State Monad to help implement a digital filter:
>
>  17 newtype Filter e a = F {
>  18     runFilter :: a -> EitherT e (State FilterState) a
>  19   } deriving (Monad, MonadState FilterState)
>
> but I'm getting these compiler errors:
>
> Filter.hs:19:14:
>     Can't make a derived instance of `Monad (Filter e)'
>       (even with cunning newtype deriving):
>       cannot eta-reduce the representation type enough
>     In the newtype declaration for `Filter'
>
> Filter.hs:19:21:
>     Can't make a derived instance of
>       `MonadState FilterState (Filter e)'
>       (even with cunning newtype deriving):
>       cannot eta-reduce the representation type enough
>     In the newtype declaration for `Filter'
>
> If I change the code to this:
>
>  17 newtype Filter e a = F {
> *  18     runFilter :: EitherT e (State FilterState) a
> ** * 19   } deriving (Monad, MonadState FilterState)
>
> it compiles, but I can't figure out how I'd feed the input to the filter,
> in that case.
>
> In comparing this to the tricks used in constructing the State Monad based
> version of the `Parser' type,
> I notice that Parser gets around this issue, by having the input (i.e. -
> input stream) be a part of the initial state,
> but I'm not sure that's appropriate for a digital filter.
>
> Thanks,
> -db
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111011/4e378ae8/attachment.htm>


More information about the Haskell-Cafe mailing list