No instance for (GHC.Base.Alternative Parser) error using clever Haskell code
I am trying to follow the examples at Graham Hutton Programming in the Haskell book ( http://www.cs.nott.ac.uk/~gmh/book.html ). Although the examples are in competent haskell, I can run ghci to load the examples; for example ghci cipher.lhs
( http://www.cs.nott.ac.uk/~gmh/cipher.lhs ):
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( cipher.lhs, interpreted )
Ok, modules loaded: Main.
*Main> let2int 'a'
0
However, with some examples, due to changes in ghci, I have some problems; for example, in Parsing.ls in chapter 8, I have a bug No instance for (Applicative ...)
.
From https://ghc.haskell.org/trac/ghc/wiki/Migration/7.10 I got hints to remove some bugs by adding some code.
> instance Applicative Parser where
> pure = return
> (<*>) = ap -- defined in Control.Monad
>
> instance Functor Parser where
> fmap = liftM
>
> instance Alternative Parser where
> (<|>) = mplus
> empty = mzero
However, I was unable to resolve this error message:
Not in scope: type constructor or class ‘Alternative’
What is wrong with this and how to fix this problem? The source code causing the problem is: http://www.cs.nott.ac.uk/~gmh/Parsing.lhs
Decision
Adding this code works great:
import qualified Control.Applicative as CA
instance CA.Alternative Parser where ...
source to share
To the right of the link you posted:
GHC says No instance for (Alternate ...)
A side effect of AMP is that Alternative has become a superclass of MonadPlus. Easy remedy:
an instance of Alternative Foo, where (<|>) = mplus empty = mzero
so it should be:
import Control.Applicative
instance Alternative Parser where
(<|>) = mplus
empty = mzero
Unfortunately, I cannot say for sure if this will do, because the links to the book code you provided do not include an instance for MonadPlus
where to find missing definitions
The easiest way is to use hoogle or hayoo - as you can see in the link google for example will tell you what is in base
and namespaceControl.Applicative
related to multiples
Okay I'm bad again - you should be okay
import Control.Applicative (Alternative())
or
import qualified Control.Applicative as CA
instance CA.Alternative Parser where ...
or by defining all material (e.g. using Parsing.many
instead of many
)
again wish i could give you a 100% waterproof compile-time solution - you might have to test a bit (for example i'm not 100% sure about ()
here import Control.Applicative (Alternative())
and you might / p>
source to share
I also came across this problem. For those who want an adapted version of the module, parsing.lhs
simply place it in the import section:
> import qualified Control.Applicative as CA
and after defining the Parser type this code:
> instance Applicative Parser where
> pure = return
> (<*>) = ap -- defined in Control.Monad
>
> instance Functor Parser where
> fmap = liftM
>
> instance CA.Alternative Parser where
> (<|>) = mplus
> empty = mzero
Or take the adapted version of the module here https://gist.github.com/myshov/85badeb087c51631aee3
source to share