I'm implementing a simple interpreter in Haskell but I have this problem. The code is this:
import Control.Applicative
import Data.Char
newtype Parser a = P (String -> [(a,String)])
parse :: Parser a -> String -> [(a,String)]
parse (P p) inp = p inp
item :: Parser Char
item = P (\inp -> case inp of
[] -> []
(x:xs) -> [(x,xs)])
instance Functor Parser where
fmap :: (a -> b) -> Parser a -> Parser b
fmap g p = P (\inp -> case parse p inp of
[] -> []
[(v,out)] -> [(g v, out)])
instance Monad Parser where
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = P (\inp -> case parse p inp of
[] -> []
[(v,out)] -> parse (f v) out)
three :: Parser (Char,Char)
three = do {x <- item;
item;
z <- item;
return (x,z);}
If i run the script in hugs everything seems to be ok. But when I try to run the command
parse three "abcdef"
I get an error:
Program error: undefined member: >>=
Please can someone help me?