Re: [Haskell-beginners] functional parser type error

Hi, The parser is defined *type Parser a = String → [(a, String)]* * * But for me it is not pretty clear, why i need to make Parser a newtype instead of working with this one. Can you give me some hints ?. Thanks Felipe

On Wed, Apr 04, 2012 at 10:54:45AM +0100, felipe zapata wrote:
Hi, The parser is defined
*type Parser a = String → [(a, String)]* * * But for me it is not pretty clear, why i need to make Parser a newtype instead of working with this one.
In order to use do-notation, Parser has to be an instance of Monad. However, ((->) String) is already an instance of Monad, and it's not the instance you want for Parser. There cannot be two instances for the same type, so you must wrap it in a newtype in order to make a different instance. The other option is to just implement your own operators (>==) :: Parser a -> (a -> Parser b) -> Parser b returnP :: a -> Parser a and use those directly, though then you cannot use do-notation. -Brent

There is working code to accompany the book on Graham Hutton's website
that wraps Parser as a newtype.
http://www.cs.nott.ac.uk/~gmh/book.html
http://www.cs.nott.ac.uk/~gmh/Parsing.lhs
Graham makes a brief comment about the difference at the end of the
parsing chapter.
On 4 April 2012 18:31, Brent Yorgey
In order to use do-notation, Parser has to be an instance of Monad. However, ((->) String) is already an instance of Monad, and it's not the instance you want for Parser. There cannot be two instances for the same type, so you must wrap it in a newtype in order to make a different instance.
participants (3)
-
Brent Yorgey
-
felipe zapata
-
Stephen Tetley