Friday, 15 May 2015

haskell - Pattern matching error on instance declaration -



haskell - Pattern matching error on instance declaration -

i need declare monad instance specific info type:

data m m = mk (m (maybe a)) instance (monad m) => monad (m m) homecoming x = mk (m (just x)) mk (m (nothing)) >>= f = mk (m (nothing)) mk (m (just x)) >>= f = f x

but get:

test.hs:6:7: parse error in pattern: m failed, modules loaded: none.

it may simple, cant figure out!

the type variable m not can pattern match on, not in order distinguish between just , nothing. think mean different possible types used in place of m--in many cases, such pattern match outright impossible.

to write instance, you'll need this:

instance (monad m) => monad (m m) homecoming x = mk (return (just x)) mk mx >>= f = -- ??

note return used create value of type m (maybe a)--that's possible because of monad m constraint, , in general case (with no constraint @ all) there'd no way create such value.

to implement (>>=) you'll need similar, likewise making utilize of (>>=) monad instance of m.

incidentally, should consider using newtype m, unless have specific reason wanting data. of time, if can utilize newtype, should.

haskell instance monads

No comments:

Post a Comment