Defining a new monad in haskell raises no instance for Applicative -
i trying define new monad , getting strange error
newmonad.hs
newtype wrapped = wrap {unwrap :: a} instance monad wrapped (>>=) (wrap x) f = f x return x = wrap x main = putstrln "yay"
$ ghc --version glorious glasgow haskell compilation system, version 7.10.1 $ ghc newmonad.hs [1 of 1] compiling main ( newmonad.hs, newmonad.o ) newmonad.hs:2:10: no instance (applicative wrapped) arising superclasses of instance declaration in instance declaration ‘monad wrapped’
why need define instance of applicative
?
this applicative monad proposal (amp). whenever declare monad
, have declare applicative
(and therefore functor
). mathematically speaking, every monad is applicative functor, makes sense.
you can following remove error:
instance functor wrap fmap f (wrap x) = wrap (f x) instance applicative wrap pure = wrap wrap f <*> wrap x = wrap (f x)
https://wiki.haskell.org/functor-applicative-monad_proposal
edit: maybe should point out more recent thing? code posted used work before, recent versions of ghc you'll error. it's breaking change.
edit: following declarations should work any monad:
import control.applicative -- otherwise can't applicative instance. import control.monad (liftm, ap) instance functor ??? fmap = liftm instance applicative ??? pure = return (<*>) = ap
depending on monad in question, there may more efficient implementations possible, simple starting point.
Comments
Post a Comment