Parsec String()(String,String)的含义是什么? [英] What is the meaning of Parsec String () (String,String)?
问题描述
我理解了 Parsec
模块的 parse
函数,它带有规则参数,错误消息和输入字符串:
解析规则文本= Parsec.parse规则(源)文本
$ c $不过,我不明白 Parsec.Parsec
的含义,或者它是如何不同于 Parsec.ParsecT
。为什么自定义分析器的类型签名使用这个名称?
例如,在下面的代码片段中取自 this blogpost ,
myParser :: Parsec.Parsec String()(String,String)
myParser = do
letters< - Parsec.many1 Parsec.letter
Parsec.spaces
digits< - Parsec.many1 Parsec.digit
返回(字母,数字)
Parsec.Parsec
和()
是否在 myParser
的类型签名中?
解决方案 ParsecT
和 Parsec
在 parsec
3, ParsecT
和 Parsec
在 Text.Parsec.Prim
模块:
data ParsecT suma
ParsecT suma
是一个流类型为 s
,用户状态类型 u
,底层monad m
和返回类型 a
。
(流类型的例子是 String
, ByteString
和 Text
。)
Parsec
只是 ParsecT
专门用于 身份
monad :
type Parsec su = ParsecT su Identity < code $ <
解释 myParser
的签名
返回到您的类型签名,位于
myParser :: Parsec.Parsec String ()(String,String)
- 流类型是
ST ring
;
- 用户状态只是空元组(也称为单元);换句话说,
myParser
解析了一些内容,但没有记录任何有用的状态;
- 结果类型是一对
String
s。
此外,类型签名使用因为在 haskell-parsec-basicsrel =nofollow>您链接的博文, Text.Parsec
被导入 qualified 为 Parsec
。
解析器
类型同义词
如果所有解析器都有流类型 String
并且不跟踪任何状态,您可能想要抽取一些 parsec
复杂度。在这种情况下,应该使用 Parser
类型同义词, Text.Parsec.String
模块定义为
type Parser = Parsec String()
例如,使用以下导入
import Text.Parsec.String(Parser)
您可以将 myParser
的类型签名简化为
myParser :: Parser(String,String)
I understand the Parsec
modules' parse
function, 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 of Parsec.Parsec
, or how it's different from Parsec.ParsecT
. Why do type signatures of custom parsers 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)
what does Parsec.Parsec
and ()
mean in myParser
's type signature?
解决方案 ParsecT
and Parsec
In parsec
3, ParsecT
and Parsec
are defined and explained in the Text.Parsec.Prim
module:
data ParsecT s u m a
ParsecT s u m a
is a parser with stream type s
, user state type u
, underlying monad m
and return type a
.
(Examples of stream types are String
, ByteString
, and Text
.)
Parsec
is simply a version of ParsecT
specialised to the Identity
monad:
type Parsec s u = ParsecT s u Identity
The signature of myParser
explained
Going back to your type signature, in
myParser :: Parsec.Parsec String () (String,String)
- the stream type is
String
;
- the user state is simply the empty tuple (also known as "unit"); in other words,
myParser
parses something but doesn't keep track of any useful state;
- the result type is a pair of
String
s.
Moreover, the type signature uses Parsec.Parsec
(and not simply Parsec
) because, in the blogpost you link to, Text.Parsec
is imported qualified as Parsec
.
The Parser
type synonym
If all your parsers have stream type String
and don't keep track of any state, you probably want to abstract some of that parsec
complexity away. In that case, you should use the Parser
type synonym, which the Text.Parsec.String
module defines as
type Parser = Parsec String ()
For instance, with the following import
import Text.Parsec.String ( Parser )
you can simplify myParser
's type signature to
myParser :: Parser (String, String)
这篇关于Parsec String()(String,String)的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文