问题描述
遵循的(接受)答案,我期待以下工作:
Prelude Text.Regex.Posix Text.Regex.Base.RegexLike Text.Regex.Posix.String> makeRegex。*
( makeRegex
是 makeregexOpts
带有预定义选项的快捷方式)
然而,它并没有:
< interactive>:1:0:
没有用于(RegexMaker正则表达式compOpt execOpt [Char])
的实例< interactive> 1:0-13
可能的修正:
为
添加实例声明(RegexMaker正则表达式compOpt execOpt [Char])
在&表达式:makeRegex。*
在`it'的定义中:it = makeRegex。*
Prelude Text.Regex.Posix Text.Regex.Base.RegexLike Text.Regex.Posix.String> ; make
Regex。*:: Regex
< interactive>:1:0:
(RegexMaker正则表达式compOpt execOpt [Char])
在< interactive> 1:0-13
处使用`makeRegex'引起的可能的修正:
为
添加实例声明(RegexMaker Regex compOpt execOpt [Char])
在表达式中:makeRegex。*:: Regex
在`it'的定义中:it = makeRegex。*:: Regex
我真的不明白为什么。
编辑
Haskell Platform 2009.02.02(GHC 6.10.4)on Windows
EDIT2
Prelude Text.Regex.Base.RegexLike Text.Regex.Posix.String> :i RegexMaker
class(RegexOptions regex compOpt execOpt)=> RegexMaker正则表达式compOpt execOpt源|正则表达式 - > compOpt execOpt,compOpt - >正则表达式execOpt,execOpt - >正则表达式compOpt其中
makeRegex :: source - >正则表达式
makeRegexOpts :: compOpt - > execOpt - >来源 - >正则表达式
makeRegexM ::(Monad m)=>来源 - > m正则表达式
makeRegexOptsM ::
(Monad m)=> compOpt - > execOpt - >来源 - > m regex
- 在Text.Regex.Base.RegexLike中定义
您的第一次尝试不起作用,因为makeRegex具有多态返回类型(称为 regex
)。由于RegexMaker没有用于任何类型的实例,因此您会收到消息。
为了使它工作,您需要指定返回类型。看起来你已经想出了自己的想法,因为这就是你在第二次尝试时所做的事情,当我在我的ghci中尝试时偶然发生了。
编辑:我应该添加使用regexen的最直接的方法是只使用 =〜〜
,而不用为makeRegex而烦恼。例如:
> lale=〜。*:: Bool
True
> lale=〜lo:: Bool
False
> lale=〜l。 :: String
la
> lale=〜l。 :: [String]
[la,le]
Following the (accepted) answer from this question, I am expecting the following to work:
Prelude Text.Regex.Posix Text.Regex.Base.RegexLike Text.Regex.Posix.String> makeRegex ".*"
(makeRegex
is a shortcut for makeRegexOpts
with predefined options)
However, it doesn't:
<interactive>:1:0:
No instance for (RegexMaker regex compOpt execOpt [Char])
arising from a use of `makeRegex' at <interactive>:1:0-13
Possible fix:
add an instance declaration for
(RegexMaker regex compOpt execOpt [Char])
In the expression: makeRegex ".*"
In the definition of `it': it = makeRegex ".*"
Prelude Text.Regex.Posix Text.Regex.Base.RegexLike Text.Regex.Posix.String> make
Regex ".*"::Regex
<interactive>:1:0:
No instance for (RegexMaker Regex compOpt execOpt [Char])
arising from a use of `makeRegex' at <interactive>:1:0-13
Possible fix:
add an instance declaration for
(RegexMaker Regex compOpt execOpt [Char])
In the expression: makeRegex ".*" :: Regex
In the definition of `it': it = makeRegex ".*" :: Regex
And I really don't understand why.
EDIT
Haskell Platform 2009.02.02 (GHC 6.10.4) on Windows
EDIT2
Prelude Text.Regex.Base.RegexLike Text.Regex.Posix.String> :i RegexMaker
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
makeRegexM :: (Monad m) => source -> m regex
makeRegexOptsM ::
(Monad m) => compOpt -> execOpt -> source -> m regex
-- Defined in Text.Regex.Base.RegexLike
Your first try does not work because makeRegex has a polymorphic return type (called regex
). Since there is no instance of RegexMaker for abitrary types, you get the message you do.
To make it work you need to specify a return type. It seems like you figured that out yourself because that's what you did in your second try, which incidentally works when I try it out in my ghci.
Edit: I should add that the most straight-forward way to use regexen is to just use =~
and not bother with makeRegex at all. For example:
> "lale" =~ ".*" :: Bool
True
> "lale" =~ "lo" :: Bool
False
> "lale" =~ "l." :: String
"la"
> "lale" =~ "l." :: [String]
["la","le"]
这篇关于Haskell:由于Regex库的普通用法而导致'没有实例'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!