[Haskell-beginners] How to run Parser in chapter 8 Graham Hutton Haskell book

Christian Maeder Christian.Maeder at dfki.de
Fri Jan 3 16:50:16 UTC 2014


Hi,

the Monad instance used for the do-notation of your parser p is that
of the function type (Monad ((->) r)) due to your Parser' type synonym.

This instance is not suitable. In p x and y get values of type [(Char, 
String)] via item, but you would want x and y to be of type Char.

In order to fix it, you could desugar the do-Notation and use a proper 
replacement for >>=.

(Alternatively, your Parser type could be turned into a proper data type 
with a proper Monad instance.)

HTH Christian

Am 03.01.2014 16:58, schrieb Angus Comber:
> I am reading Chapter 8 of Programming Haskell by Graham Hutton and
> trying to run the code in the book.
>
> It seems that there are already defined library implementations of
> Parser so I used Parser' and generally ' on the end of each identifier
> to attempt to eliminate this problem.
>
> So the code I copied from the book is:
>
> type Parser' a = String -> [(a, String)]
>
> return' :: a -> Parser' a
> return' v = \x -> [(v, x)]
>
> failure' :: Parser' a
> failure' = \inp -> []
>
> -- item doesn't seem to conflict with anything so no '
> item :: Parser' Char
> item = \inp -> case inp of
>                      [] -> []
>                      (x:xs) -> [(x,xs)]
>
>
> parse' :: Parser' a -> String -> [(a, String)]
> parse' p inp = p inp
>
>
> p :: Parser' (Char, Char)
> p = do  x <- item
>          item
>          y <- item
>          return' (x,y)
>
> When run from WinGHCi I get error:
>
> prog_haskell.hs:458:9:
>     Couldn't match type `[(Char, String)]' with `Char'
>      Expected type: String -> [((Char, Char), String)]
>        Actual type: Parser' ([(Char, String)], [(Char, String)])
>      In the return type of a call of return'
>      In a stmt of a 'do' block: return' (x, y)
>      In the expression:
>        do { x <- item;
>             item;
>             y <- item;
>             return' (x, y) }
>
> 458.9 is line at end with return' (x,y)
>
>
> Also in the chapter was the definition of >>= sequencing.  as in:
>
> (>>=) :: Parser a -> (a -> Parser b) -> Parser b
> p >>= f  =  \inp -> case parse p inp of
>                        [] -> []
>                        [(v,out)] -> parse (f v) out
>
> But I assumed this would already be in standard Haskell library and so I
> didn't need to define.  But even when I did add, still get same error.
>
> How do I fix this?
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list