Parsec Expected Type

Hi, Does anyone know why this reduced Parsec production stops compilation, and how I can fix it? tester = reserved "parameter" <|> do { reserved "dimension"; symbol ":" } Thanks, Paul

Hi Paul,
2008/3/27 Paul Keir
Hi,
Does anyone know why this reduced Parsec production stops compilation, and how I can fix it?
tester = reserved "parameter" <|> do { reserved "dimension"; symbol ":" }
Look at the types of "reserved" and "symbol" (from http://www.haskell.org/ghc/docs/latest/html/libraries/parsec/Text-ParserComb...): symbol :: String -> CharParser st String reserved :: String -> CharParser st () The type of a do block is the same as the type of its last statement. But (reserved "parameter") and (symbol ";") do not have the same type. Luke

Thanks, I'd thought it was something to do with incompatible types. I can now create a simpler problem:
tester2 = reserved "parameter" <|> symbol ":"
Certainly I could use reserved ":" instead of symbol, but where is my thinking with symbol here going wrong? Surely the above example isn't so odd?
Paul
-----Original Message-----
From: Luke Palmer [mailto:lrpalmer@gmail.com]
Sent: Fri 3/28/2008 12:26 AM
To: Paul Keir
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] Parsec Expected Type
Hi Paul,
2008/3/27 Paul Keir
Hi,
Does anyone know why this reduced Parsec production stops compilation, and how I can fix it?
tester = reserved "parameter" <|> do { reserved "dimension"; symbol ":" }
Look at the types of "reserved" and "symbol" (from http://www.haskell.org/ghc/docs/latest/html/libraries/parsec/Text-ParserComb...): symbol :: String -> CharParser st String reserved :: String -> CharParser st () The type of a do block is the same as the type of its last statement. But (reserved "parameter") and (symbol ";") do not have the same type. Luke

On 28 Mar 2008, at 2:02 AM, Paul Keir wrote:
Thanks, I'd thought it was something to do with incompatible types. I can now create a simpler problem:
tester2 = reserved "parameter" <|> symbol ":"
Certainly I could use reserved ":" instead of symbol, but where is my thinking with symbol here going wrong? Surely the above example isn't so odd?
What type do you expect tester2 to return? jcc

Could tester2 return "some kind of base type", which the two inherit from? I don't know. I'm really only presenting the ugly tester2 function because I'm looking for a Parsec-concordant solution to what appears a simple problem. What I'd like is to parse either the string "parameter", or the string ":". I'm using 'reserved' and 'symbol' because they seem to correspond well to the concepts in the language I'm parsing. I could try, tester3 = reserved "parameter" <|> do { symbol ":"; return () } but that's feels a bit contrived; or I could use 'reserved' twice. Perhaps I'd express my confusion better if I ask: Why are 'reserved' and 'symbol' different types? Paul (Haskell Novice) -----Original Message----- From: Jonathan Cast [mailto:jonathanccast@fastmail.fm] Sent: Fri 3/28/2008 2:05 PM To: Paul Keir Cc: Luke Palmer; haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] Parsec Expected Type On 28 Mar 2008, at 2:02 AM, Paul Keir wrote:
Thanks, I'd thought it was something to do with incompatible types. I can now create a simpler problem:
tester2 = reserved "parameter" <|> symbol ":"
Certainly I could use reserved ":" instead of symbol, but where is my thinking with symbol here going wrong? Surely the above example isn't so odd?
What type do you expect tester2 to return? jcc

On 3/28/08, Paul Keir
What I'd like is to parse either the string "parameter", or the string ":". I'm using 'reserved' and 'symbol' because they seem to correspond well to the concepts in the language I'm parsing. I could try,
tester3 = reserved "parameter" <|> do { symbol ":"; return () }
Actually this is exactly on the right track. But I agree, it looks a bit contrived. Maybe this looks better to you?
tester3 = reserved "parameter" <|> (symbol ":" >> return ())
Or you could factor this behavior out into a new combinator:
or_ :: Parser a -> Parser b -> Parser () or_ x y = (x >> return ()) <|> (y >> return ())
tester3 = reserved "parameter" `or_` symbol ":"
-- ryan

On Mar 28, 2008, at 21:12 , Ryan Ingram wrote:
On 3/28/08, Paul Keir
wrote: What I'd like is to parse either the string "parameter", or the string ":". I'm using 'reserved' and 'symbol' because they seem to correspond well to the concepts in the language I'm parsing. I could try,
tester3 = reserved "parameter" <|> do { symbol ":"; return () }
Or you could factor this behavior out into a new combinator:
or_ :: Parser a -> Parser b -> Parser () or_ x y = (x >> return ()) <|> (y >> return ())
tester3 = reserved "parameter" `or_` symbol ":"
Or if you'd like to be inscrutable: import Data.Function or_ = (>> return ()) `on` (<|>) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Many thanks guys, you've really taught me how to catch a fish here! Paul -----Original Message----- From: Brandon S. Allbery KF8NH [mailto:allbery@ece.cmu.edu] Sent: Sat 3/29/2008 1:41 AM To: haskell-cafe@haskell.org Cafe Cc: Paul Keir Subject: Re: [Haskell-cafe] Parsec Expected Type On Mar 28, 2008, at 21:12 , Ryan Ingram wrote:
On 3/28/08, Paul Keir
wrote: What I'd like is to parse either the string "parameter", or the string ":". I'm using 'reserved' and 'symbol' because they seem to correspond well to the concepts in the language I'm parsing. I could try,
tester3 = reserved "parameter" <|> do { symbol ":"; return () }
Or you could factor this behavior out into a new combinator:
or_ :: Parser a -> Parser b -> Parser () or_ x y = (x >> return ()) <|> (y >> return ())
tester3 = reserved "parameter" `or_` symbol ":"
Or if you'd like to be inscrutable: import Data.Function or_ = (>> return ()) `on` (<|>) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Paul Keir wrote:
What I'd like is to parse either the string "parameter", or the string ":". I'm using 'reserved' and 'symbol' because they seem to correspond well to the concepts in the language I'm parsing.
You may consider using reservedOp for ":", depending on how ":+" should be parsed: for ":+" use reservedOp for ":" "+" use symbol If you use reserved, then ":name" will be parsed as ":name" not ":" "name" as you probably expect. generally, reserved is for identifier-like keywords, and reservedOp for operator-like keywords.
Perhaps I'd express my confusion better if I ask: Why are 'reserved' and 'symbol' different types?
I have no idea. They aren't in the Parsec manual on Daans site: http://legacy.cs.uu.nl/daan/download/parsec/parsec.html You can fix this by defining reserved name = ParsecToken.reserved tokenParser name >> return name instead of reserved = ParsecToken.reserved tokenParser to "import" the reserved component from the tokenParser to the toplevel. Now, reserved :: String -> CharParser st String Another option is to fix it the other way, by defining symbol name = ParsecToken.symbol tokenParser name >> return () or to fix it in a ad-hoc manner, by defining ignored = (>> return ()) and using it in the approbiate places, like parameterOrColon = reserved "parameter" <|> ignored (symbol ":") Tillmann

Thanks. reservedOp is a better fit; ":+" should only be ":+". I also overcame my type issues in an ad-hoc manner, adding
return ()
whenever I needed to. -----Original Message----- From: Tillmann Rendel [mailto:rendel@rbg.informatik.tu-darmstadt.de] Sent: 30 March 2008 12:30 To: Paul Keir; haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] Parsec Expected Type Paul Keir wrote:
What I'd like is to parse either the string "parameter", or the string ":". I'm using 'reserved' and 'symbol' because they seem to correspond well to the concepts in the language I'm parsing.
You may consider using reservedOp for ":", depending on how ":+" should be parsed: for ":+" use reservedOp for ":" "+" use symbol If you use reserved, then ":name" will be parsed as ":name" not ":" "name" as you probably expect. generally, reserved is for identifier-like keywords, and reservedOp for operator-like keywords.
Perhaps I'd express my confusion better if I ask: Why are 'reserved' and 'symbol' different types?
I have no idea. They aren't in the Parsec manual on Daans site: http://legacy.cs.uu.nl/daan/download/parsec/parsec.html You can fix this by defining reserved name = ParsecToken.reserved tokenParser name >> return name instead of reserved = ParsecToken.reserved tokenParser to "import" the reserved component from the tokenParser to the toplevel. Now, reserved :: String -> CharParser st String Another option is to fix it the other way, by defining symbol name = ParsecToken.symbol tokenParser name >> return () or to fix it in a ad-hoc manner, by defining ignored = (>> return ()) and using it in the approbiate places, like parameterOrColon = reserved "parameter" <|> ignored (symbol ":") Tillmann
participants (6)
-
Brandon S. Allbery KF8NH
-
Jonathan Cast
-
Luke Palmer
-
Paul Keir
-
Ryan Ingram
-
Tillmann Rendel