What is the meaning of Parsec String () (String, String)?
I understand the Parsec
modules function parse
, which takes a rule argument, an error message, and an input string:
parse rule text = Parsec.parse rule "(source)" text
However, I don't understand the meaning Parsec.Parsec
or how it differs from Parsec.ParsecT
. Why do custom parser type signatures use this name?
For example, in the following code snippet taken from this blogpost ,
myParser :: Parsec.Parsec String () (String,String)
myParser = do
letters <- Parsec.many1 Parsec.letter
Parsec.spaces
digits <- Parsec.many1 Parsec.digit
return (letters,digits)
meaning Parsec.Parsec
, and ()
in the type of signature myParser
?
source to share
ParsecT
and Parsec
In Parsec
3, ParsecT
and are Parsec
defined and explained in Text.Parsec.Prim
module :
data ParsecT s u m a
ParsecT s u m a
is a parser with streams
type, user state typeu
, base monadm
and return typea
.
(Examples of the types of streams: String
, ByteString
and Text
.)
Parsec
It is a version ParsecT
specialized for Identity
monad :
type Parsec s u = ParsecT s u Identity
Signature myParser
explained
Going back to your type signature, in
myParser :: Parsec.Parsec String () (String,String)
- stream type
String
; - the user state is just an empty tuple (also known as "one"); in other words,
myParser
parses something, but does not track any useful state; - The result type is a pair of
String
s.
In addition, the signature type used Parsec.Parsec
(not just Parsec
), because in a blog that you refer to , Text.Parsec
imported qualified like Parsec
.
Synonym for type Parser
If all of your parsers are stream type String
and don't track any state, you probably want to deflect some of this complexity Parsec
. In this case, you must use a type synonym Parser
that the module Text.Parsec.String
defines as
type Parser = Parsec String ()
For example, with the following import
import Text.Parsec.String ( Parser )
you can simplify the type signature myParser
to
myParser :: Parser (String, String)
source to share