[Haskell-cafe] Trouble using State Monad.
Brent Yorgey
byorgey at seas.upenn.edu
Sun Oct 9 04:21:32 CEST 2011
On Sat, Oct 08, 2011 at 04:28:34PM -0700, Captain Freako wrote:
> Hi all,
>
> I'm trying to use the State Monad to help implement a digital
> filter:
(a -> EitherT e (State FilterState) a) is definitely not monadic.
There is an 'a' in a negative position (to the left of an odd number
of arrows) so it is not even a Functor. But I think you want to have
the 'a ->' part separate.
I might do something like
newtype FilterM e a = F { unFilterM :: EitherT e (State FilterState) a }
deriving (Functor, Applicative, Monad, MonadState FilterState)
type Filter e a b = a -> FilterM e b
That is, a Filter is a function from a to b, which can abort with an
error of type e and maintains a FilterState. Then you can chain
Filters using (>=>): if
f1 :: Filter e a b
f2 :: Filter e b c
then
f1 >=> f2 :: Filter e a c
is a Filter which does first f1 then f2. Alternatively, if you wanted
to use an Arrow interface you could define
import Control.Arrow
type Filter e a b = Kleisli (FilterM e) a b
-Brent
More information about the Haskell-Cafe
mailing list