[Haskell-cafe] YAHT: Ex 9.2

Nicolas Frisby nicolas.frisby at gmail.com
Sun Oct 29 14:49:44 EST 2006

Quick fix: add this line to the top of your file

{-# OPTIONS -fglasgow-exts #-}

Having String instead of a free type variable is beyond the basic
limitations of Haskell's type classses.

However, enabling the very common Glasgow extensions expands the rules
to admit your definition. If you for some reason do not wish to add
the Glasgow extensions, the following also works.

newtype EitherStr a = ES (Either String a)

instance Monad EitherStr where
    return a = ES (Right a)
    fail a = ES (Left undefined)
    ES (Right a) >>= f = f a
    ES (Left a) >>= _ = ES (Left a)

On 10/29/06, Magnus Therning <magnus at therning.org> wrote:
> I've been slowly making my way through Yet Another Haskell Tutorial.  As
> a first introduction to the language it doesn't seem bad at all.
> However, I'm wondering about the proposed solution to exercise 9.2.  The
> text itself suggests using "instance Monad (Either String) where", so I
> arrived at
>   instance Monad (Either String) where
>       return a = Right a
>       fail a = Left a
>       Right a >>= f = f a
>       Left a >>= _ = Left a
> However, when loading it in ghci 6.6 I get the following error message:
>      Illegal instance declaration for `Monad (Either String)'
>         (The instance type must be of form (T a b c)
>          where T is not a synonym, and a,b,c are distinct type variables)
>     In the instance declaration for `Monad (Either String)'
> The solution, according to the author is
>   instance Monad (Either String) where
>       return x = Right x
>       fail s = Left s
>       Right x >>= f = f x
>       Left s >>= _ = Left s
> Which results in exactly the same error message.
> I'm suspecting this is a result of my limited grasp of Haskell's syntax
> (an area where YAHT is sorely lacking).  Any tips/pointers appreciated.
> /M
> --
> Magnus Therning                             (OpenPGP: 0xAB4DFBA4)
> magnus at therning.org             Jabber: magnus.therning at gmail.com
> http://therning.org/magnus
> Software is not manufactured, it is something you write and publish.
> Keep Europe free from software patents, we do not want censorship
> by patent law on written works.
> As far as the laws of mathematics refer to reality, they are not
> certain, and as far as they are certain, they do not refer to reality.
>      -- Albert Einstein
> _______________________________________________
> 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