3

I'm trying to execute examples in Graham Hutton's Programming in Haskell book (http://www.cs.nott.ac.uk/~gmh/book.html). Even though the examples are in literate haskell, I could launch 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, because of the changes in ghci, I have some issues; for example in Parsing.ls in chapter 8, I have No instance for (Applicative ...) error.

From https://ghc.haskell.org/trac/ghc/wiki/Migration/7.10 I got hints to remove some of the errors 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 couldn't resolve this error message:

Not in scope: type constructor or class ‘Alternative’

What's wrong with this, and how to solve this issue? The original code that causes the problem is from: http://www.cs.nott.ac.uk/~gmh/Parsing.lhs

Solution

Adding this code works fine:

import qualified Control.Applicative as CA
instance CA.Alternative Parser where ...
prosseek
  • 182,215
  • 215
  • 566
  • 871

2 Answers2

5

Right from the Link you posted:

GHC says No instance for (Alternative ...)

A side-effect of the AMP is that Alternative became a super-class of MonadPlus. The easy remedy:

instance Alternative Foo where (<|>) = mplus empty = mzero

so here it should be:

import Control.Applicative

instance Alternative Parser where
    (<|>) = mplus
    empty = mzero

sadly I cannot tell you exactly if this will do because the links to the books code you did provide don't include the instance for MonadPlus

where do find missing definitions

the easiest way is to use hoogle or hayoo - as you can see in the link Hoogle for example will tell you that's in the base package and the Control.Applicative namespace

dealing with the multiples

Ok my bad again - you should be fine with

import Control.Applicative (Alternative())

or

import qualified Control.Applicative as CA

instance CA.Alternative Parser where ...

or by qualifying all the stuff (like using Parsing.many instead of just many)

again sorry that I cannot give you a 100% waterproof compiling solution - you might have to test a bit (for example I am not 100% sure about the () here import Control.Applicative (Alternative()) and you can probably strip it.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • If you check my original post, you can find that I included the `Alternative Parser` for my addition. I updated the link to show the code that has `MonadPlus` and the error message that I have now. – prosseek Aug 04 '15 at 14:14
  • @prosseek ok sorry - a simple `import Control.Applicative` should do the trick – Random Dev Aug 04 '15 at 14:35
  • please note that those definitions alone will not help me much (I kindof want the code I could load into ghci myself) - yeah I basically know how to implement `Parser` and all the stuff - but I am to lazy to do this or just copy it from the book myself. But you should really try to deal with these minor annoyances yourself - you sooner or later have to and the compiler error in this last case is not to hard to understand IMO – Random Dev Aug 04 '15 at 14:52
  • PS: also please don't edit my answer this much - if you are not fine with it add your own and accept it (you can do so - you just have to wait a bit I think) – Random Dev Aug 04 '15 at 14:54
  • Well, I just make the question and answer as simple as possible; I'll update my question to show the correct way to do it. Thanks for you help. – prosseek Aug 04 '15 at 14:56
1

I've also stumbled upon this issue. For those who want adapted version of the module parsing.lhs just place in import section:

> import qualified Control.Applicative as CA

and after type Parser definition 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 adapted version of the module here https://gist.github.com/myshov/85badeb087c51631aee3

Alexander Myshov
  • 2,881
  • 2
  • 20
  • 31
  • For those who voted negatively. My motivation for this answer was not making of some sort of duplication, but making ready to go solution, for those who just want to grab adapted version of the module for new version GHCI. – Alexander Myshov Jan 31 '16 at 20:01