[Haskell-cafe] How to define instance for type synonym with parameter.

Ryan Ingram ryani.spam at gmail.com
Mon Mar 15 17:44:33 EDT 2010


TypeSynonymInstances isn't doing anything you couldn't do yourself.
It's just expanding the type synonym in the instance declaration for
you.

In this case, the error is because type synonyms must be fully
applied.  However, your type synonym can be eta-contracted:
    type SomeMonad = ErrorT String (ReaderT String IO)

Now you can use it in an instance declaration.  But this doesn't work
in general; this type synonym cannot be made into a monad:
    type St s a = s -> (a,s)

However, I think that adding instances this way is bad style.  I
suggest turning on newtype deriving instead, and doing:

newtype SomeMonad a = SomeMonad (ErrorT String (Reader T String IO) a)
   deriving (Monad, MonadError, MonadReader String, MonadIO, Functor)
instance Applicative SomeMonad where
   pure = return
   (<*>) = ap

  -- ryan


On Mon, Mar 15, 2010 at 2:34 PM, Vasyl Pasternak
<vasyl.pasternak at gmail.com> wrote:
> Hello,
>
> I am start with example. Suppose I have the following type synonym:
>
> type SomeMonad a = ErrorT String (ReaderT  String IO) a
>
> this is monad, and I want to make it instance of Applicative, so, the
> obvious way is to write the following:
>
> instance Applicative SomeMonad where
>  pure = return
>  (<*>) = ap
>
> GHCi warns me, that I have to use -XTypeSynonymInstances option to
> allow this construction, but than I have following
> error:
>
>    Type synonym `SomeMonad' should have 1 argument, but has been given 0
>    In the instance declaration for `Applicative SomeMonad'
>
> Neither `instance Applicative (SomeMonad a)` nor `instance Applicative
> SomeMonad a` help. But works the following:
>
> instance Applicative (ErrorT String (ReaderT String IO)) where
>  pure = return
>  (<*>) = ap
>
> Which is the same (from my point of view).
>
> Could anyone tell me what is going on, and how to declare SomeMonad as
> instance of Applicative ?
>
> Thanks,
> Vasyl
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list