了解 Text.Regex.Posix 设计选择

Understanding a Text.Regex.Posix design choice

我在 Text.Regex.Posix 上玩了一会儿,我发现了这个奇怪的设计选择。

这是我的 GHCi 会话:

λ> "Needle, Haystack, foo, and bar." =~ "[^ ]+" :: String
"Needle,"
λ> "Needle, Haystack, foo, and bar." =~ "[^ ]+" :: [[String]]
[["Needle,"],["Haystack,"],["foo,"],["and"],["bar."]]
λ> "Needle, Haystack, foo, and bar." =~ "[^ ]+" :: [String]

<interactive>:27:35:
    No instance for (RegexContext Regex [Char] [String])
      arising from a use of ‘=~’
    In the expression:
        "Needle, Haystack, foo, and bar." =~ "[^ ]+" :: [String]
    In an equation for ‘it’:
        it = "Needle, Haystack, foo, and bar." =~ "[^ ]+" :: [String]

我惊讶地发现,在 RegexContext 的实例中,没有 RegexLike a b => RegexContext a b [b] 的实例,只有 RegexLike a b => RegexContext a b [[b]].

我无法理解为什么要使用这种设计。为什么没有上述 [String] 的实例,为什么只有 [[String]]

那个实例 确实 存在于 Text.Regex.Base.Context 回到 regex-base-0.83 (uploaded March 5, 2007), but was removed in regex-base-0.90(2007 年 3 月 13 日上传)。没有更改日志或解释(此时的模块文档有一个 XXX THIS HADDOCK DOCUMENTATION IS OUT OF DATE XXX 注释,该注释一直保留到当前版本。)

然而,我最好猜测为什么这样做是因为 String[Char] 的类型同义词,所以有实例对于 String[b] 都会导致实例重叠问题 - 我认为不是直接的,但足以使类型推断工作不佳,特别是如果你使用 OverloadedStrings (虽然我不不知道那个扩展当时是否已经存在)。