" =〜" (RegexContext Regex [Char] [String])“No instance for” [英] "=~" raise "No instance for (RegexContext Regex [Char] [String])"
问题描述
GHC和Haskell平台来自brew。
GHCi,7.0版。 4:http://www.haskell.org/ghc/:?寻求帮助
加载包ghc-prim ...链接...完成。
加载包integer-gmp ...链接...完成。
加载程序包库...链接...完成。
加载包ffi-1.0 ...链接...完成。
前奏> :m + Text.Regex.Posix
Prelude Text.Regex.Posix> foo=〜o:: [String]
< interactive>:1:7:
(RegexContext Regex [Char] [String])
使用'=〜'
可能的修正:
为
添加一个实例声明(RegexContext Regex [Char] [String])
在表达式中:foo =〜o:: [String]
在'it'的等式中:it =foo=〜o:: [String]
Prelude Text.Regex.Posix> foo=〜o:: String
加载package array-0.3.0.2 ...链接... done。
加载包bytestring-0.9.1.10 ...链接...完成。
加载包装容器-0.4.0.0 ...链接...完成。
加载软件包变换器-0.2.2.0 ...链接...完成。
加载包mtl-2.0.1.0 ...链接...完成。
加载包regex-base-0.93.2 ...链接...完成。
加载包regex-posix-0.95.1 ...链接...完成。
o
我相信图书馆已更新。我认为foo=〜o:: [String]
的输出是 [o,o]
任何建议都将不胜感激。
ghci的> getAllTextMatches(foo=〜o:: AllTextMatches [] String)
[o,o]
我没有在Haskell中使用正则表达式(这是我认为 Dan Burton的答案更具惯用性)。
所以我想到的方式是我看着你的类型错误没有针对(RegexContext Regex [Char] [String])
的实例,并弹出到ghci中: ghci的> :t(=〜)
(=〜)
::(RegexMaker Regex CompOption ExecOption source,
RegexContext Regex source1 target)=>
source1 - >来源 - >目标
所以 RegexContext正则表达式[Char] [String]
是一个包含foo=〜o:: [String]
的返回类型的类。所以我期待看到这个类的实例存在,所以我可以找出返回值允许的内容:
ghci中> :i RegexContext
class RegexLike
regex source => RegexContext正则表达式源目标其中
match :: regex - >来源 - >目标
matchM :: Monad m =>正则表达式 - >来源 - > m目标
- 在Text.Regex.Base.RegexLike中定义
实例RegexContext正则表达式字符串字符串
- 在Text.Regex.Posix.String中定义
实例RegexLike ab => ; RegexContext a b [[b]]
- 在Text.Regex.Base.Context中定义
...
实例RegexLike a b => RegexContext ab(AllTextMatches [] b)
- 在Text.Regex.Base.Context中定义
...
AllTextMatches
名称似乎表明您要查找的内容,所以我查了一下:
ghci> :i AllTextMatches
newtype AllTextMatches f b
= AllTextMatches {getAllTextMatches :: f b}
- 在Text.Regex.Base.RegexLike中定义
实例RegexLike a b => RegexContext ab(AllTextMatches [] b)
- 在Text.Regex.Base.Context中定义
<正如我怀疑的那样,这是用来提取所有文本匹配的类型。所有我需要做的是表明我想要一个这种类型的返回值。
还要注意可能的返回类型 [[b]] ,我假设它返回一个列表,其中包含每个完整匹配及其所有子匹配:
ghci> ; foo=〜o:: [[String]]
[[o],[o]]
ghci> foo bar baz=〜[aeiou](。):: [[String]]
[[oo,o],[ar,r],[az ,z]]
所以也许这就是你想要使用的类型,而不是 [字符串]
。当 [[String]]
存在时,我可以看到 [String]
有点不明确 - 应该foo bar baz=〜[aeiou](。):: [String]
be fst(foo bar baz=〜[aeiou](。 ):: [[String]])
或 map fst(foo bar baz=〜[aeiou](。):: [[String]])
。
OS: MacOSX 10.7.1 GHC and Haskell-platform from brew.
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :m +Text.Regex.Posix
Prelude Text.Regex.Posix> "foo" =~ "o" :: [String]
<interactive>:1:7:
No instance for (RegexContext Regex [Char] [String])
arising from a use of `=~'
Possible fix:
add an instance declaration for
(RegexContext Regex [Char] [String])
In the expression: "foo" =~ "o" :: [String]
In an equation for `it': it = "foo" =~ "o" :: [String]
Prelude Text.Regex.Posix> "foo" =~ "o" :: String
Loading package array-0.3.0.2 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package transformers-0.2.2.0 ... linking ... done.
Loading package mtl-2.0.1.0 ... linking ... done.
Loading package regex-base-0.93.2 ... linking ... done.
Loading package regex-posix-0.95.1 ... linking ... done.
"o"
I believe libraries updated. And I think the output of "foo" =~ "o" :: [String]
is ["o", "o"]
Any suggestion will be appreciate.
ghci> getAllTextMatches ("foo" =~ "o" :: AllTextMatches [] String)
["o", "o"]
I haven't used regexes in Haskell much (which is I think Dan Burton's answer is more idiomatic).
So the way I figured this out is I looked at your type error No instance for (RegexContext Regex [Char] [String])
, and popped into ghci:
ghci> :t (=~)
(=~)
:: (RegexMaker Regex CompOption ExecOption source,
RegexContext Regex source1 target) =>
source1 -> source -> target
So RegexContext Regex [Char] [String]
is a class that includes the return type of "foo" =~ "o" :: [String]
. So I looked to see what instances of this class did exist, so I could find out what the return value was allowed to be:
ghci> :i RegexContext
class RegexLike
regex source => RegexContext regex source target where
match :: regex -> source -> target
matchM :: Monad m => regex -> source -> m target
-- Defined in Text.Regex.Base.RegexLike
instance RegexContext Regex String String
-- Defined in Text.Regex.Posix.String
instance RegexLike a b => RegexContext a b [[b]]
-- Defined in Text.Regex.Base.Context
...
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
-- Defined in Text.Regex.Base.Context
...
The AllTextMatches
name seemed to indicate what you were looking for, so I checked that out:
ghci> :i AllTextMatches
newtype AllTextMatches f b
= AllTextMatches {getAllTextMatches :: f b}
-- Defined in Text.Regex.Base.RegexLike
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
-- Defined in Text.Regex.Base.Context
So this was the type to use to extract all the text matches, as I suspected. All I needed to do was indicate that I wanted a return value of that type.
Note also the possible return type of [[b]]
, which I assume returns a list of lists containing each complete match and all its submatches:
ghci> "foo" =~ "o" :: [[String]]
[["o"],["o"]]
ghci> "foo bar baz" =~ "[aeiou](.)" :: [[String]]
[["oo","o"],["ar","r"],["az","z"]]
So maybe that's the type you meant to use, instead of [String]
. I could see [String]
as being slightly ambiguous when [[String]]
existed - should "foo bar baz" =~ "[aeiou](.)" :: [String]
be fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]])
or map fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]])
.
这篇关于" =〜" (RegexContext Regex [Char] [String])“No instance for”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!