我写的Text.Regex类型类的相当透彻的描述another answer。
复制大部分在这里......
所有Text.Regex.*
模块大量使用类型类,这是那里的可扩展性和“超载”般的行为,但要使用从只看到不太明显类型。
现在,您可能已经从基本的=~
匹配器开始。
(=~) ::
(RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target)
=> source1 -> source -> target
(=~~) ::
(RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target, Monad m)
=> source1 -> source -> m target
要使用=~
,必须存在的RegexMaker ...
实例的LHS,并RegexContext ...
的RHS和结果。
class RegexOptions regex compOpt execOpt | ...
| regex -> compOpt execOpt
, compOpt -> regex execOpt
, execOpt -> regex compOpt
class RegexOptions regex compOpt execOpt
=> RegexMaker regex compOpt execOpt source
| regex -> compOpt execOpt
, compOpt -> regex execOpt
, execOpt -> regex compOpt
where
makeRegex :: source -> regex
makeRegexOpts :: compOpt -> execOpt -> source -> regex
所有这些类的有效实例(例如,regex=Regex
,compOpt=CompOption
,execOpt=ExecOption
,和source=String
)意味着它是可以从某种形式source
编译regex
与compOpt,execOpt
选项。 (此外,由于一些regex
类型,恰好有一个compOpt,execOpt
组与它一起去。不同source
类型的很多都还可以,虽然)。
class Extract source
class Extract source
=> RegexLike regex source
class RegexLike regex source
=> RegexContext regex source target
where
match :: regex -> source -> target
matchM :: Monad m => regex -> source -> m target
所有这些类的有效实例(例如,regex=Regex
,source=String
,target=Bool
)表示有可能匹配source
和regex
以产生target
。(其他有效target
给出:S这些特定regex
和source
是Int
,MatchResult String
,MatchArray
等)
把这些结合在一起,它是很明显,=~
和=~~
是简单方便的功能
source1 =~ source
= match (makeRegex source) source1
source1 =~~ source
= matchM (makeRegex source) source1
=〜是多态在它的返回类型和它的参数类型中。这意味着如果你期待一个布尔值,它将返回一个布尔值。如果您期待一个字符串列表,它将返回一个捕获列表。等等(例如,多态参数类型可以匹配字符串和字节串) – jrockway
这并不能帮助我理解上下文定义部分。 'source'应该是什么类型的实例? – artemave