我在
另一个答案中详细描述了Text.Regex类型类。所有的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
要使用=~
,必须存在LHS的RegexMaker ...
实例和RHS以及结果的RegexContext ...
。
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
编译带有
compOpt,execOpt
选项的
regex
。 (此外,对于给定的一些
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
。(在给定这些特定
regex
和
source
的情况下,其他有效的
target
包括
Int
、
MatchResult String
、
MatchArray
等。)
将它们结合起来,很明显
=~
和
=~~
只是方便的函数。
source1 =~ source
= match (makeRegex source) source1
source1 =~~ source
= matchM (makeRegex source) source1
source
应该是哪种类型的类的实例? - artemave