Your filter type isn't a Monad.<br><br>In particular<br><br>bind :: (a -> EitherT e (State FilterState) a) -> (a -> b -> EitherT e (State FilterState) b) -> b -> EitherT e (State FilterState) b<br><br>
can't be implemented, as you have no place to grab an 'a' to pass to the initial computation.<br><br>If you fix the input type, you can do<br><br>newtype Filter r e a = F {<br> runFilter :: r -> EitherT e (State FilterState) a<br>
}<br><br>which is isomorphic to<br><br>newtype Filter r e a = F {<br>
runFilter :: ReaderT r (EitherT e (State FilterState)) a<br>
}<br><br>which newtype deriving will be able to deal with easily.<br><br> -- ryan<br>
<br>
<br><div class="gmail_quote">On Sat, Oct 8, 2011 at 4:28 PM, Captain Freako <span dir="ltr"><<a href="mailto:capn.freako@gmail.com">capn.freako@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi all,<br>
<br>
I'm trying to use the State Monad to help implement a digital filter:<br>
<br>
<span style="font-family:courier new,monospace"> 17 newtype Filter e a = F {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> 18 runFilter :: a -> EitherT e (State FilterState) a</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> 19 } deriving (Monad, MonadState FilterState)</span><br style="font-family:courier new,monospace">
<br>
but I'm getting these compiler errors:<br>
<br>
<span style="font-family:courier new,monospace">Filter.hs:19:14:</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> Can't make a derived instance of `Monad (Filter e)'</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> (even with cunning newtype deriving):</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> cannot eta-reduce the representation type enough</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> In the newtype declaration for `Filter'</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">Filter.hs:19:21:</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> Can't make a derived instance of</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> `MonadState FilterState (Filter e)'</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> (even with cunning newtype deriving):</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> cannot eta-reduce the representation type enough</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> In the newtype declaration for `Filter'</span><br style="font-family:courier new,monospace">
<br>
If I change the code to this:<br>
<br>
<span style="font-family:courier new,monospace"> 17 newtype Filter e a = F {</span><br style="font-family:courier new,monospace">
<b><span style="font-family:courier new,monospace">
18 runFilter :: EitherT e (State FilterState) a</span><br style="font-family:courier new,monospace">
</b><span style="font-family:courier new,monospace"><b>
</b> 19 } deriving (Monad, MonadState FilterState)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">
</span><br>
it compiles, but I can't figure out how I'd feed the input to the filter, in that case.<br>
<br>
In comparing this to the tricks used in constructing the State Monad based version of the `Parser' type,<br>
I notice that Parser gets around this issue, by having the input (i.e. - input stream) be a part of the initial state,<br>
but I'm not sure that's appropriate for a digital filter.<br>
<br>
Thanks,<br>
-db<br>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>