`empty',`<|>' : not a (visible) method of class `Alternative'

Viewed 355

I'm trying to write a parser in Haskell, but I'm having issues with the Alternative definition of it ( Functor, Applicative and Monad are done and working ):

instance Alternative Parser where
empty = P (\env input -> [])
p <|> q = P (\env input -> case parse p env input of 
                [] -> parse q env input 
                [(env, v, out)]-> [(env, v, out)])

As soon as I compile with Stack (stack build; stack run;), I get these errors, for both empty and <|> operators:

`empty' is not a (visible) method of class `Alternative'
`<|>' is not a (visible) method of class `Alternative'

Any ideas?

1 Answers

You need to import empty and <|> in order to implement them:

import Control.Applicative (Alternative, empty, (<|>))

Alternatively, it's possible to avoid bringing them into scope by importing Control.Applicative as qualified as well:

import Control.Applicative (Alternative)
import qualified Control.Applicative
Related