代码的出现意味着一段时间以来我第一次使用megaparsec。自上次以来,情况已有所改变。假设
import qualified Text.Megaparsec as MP
import qualified Text.Megaparsec.Char as C
我尝试了
MP.parseTest (C.char '+') "+"
GHCI中的,它给出以下相当无用的错误消息:
interactive>:121:1: error:
* Ambiguous type variable `e0' arising from a use of `MP.parseTest'
prevents the constraint `(MP.ShowErrorComponent
e0)' from being solved.
Probable fix: use a type annotation to specify what `e0' should be.
These potential instance exist:
one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the expression: MP.parseTest (C.char '+') "+"
In an equation for `it': it = MP.parseTest (C.char '+') "+"
<interactive>:121:15: error:
* Ambiguous type variable `e0' arising from a use of `C.char'
prevents the constraint `(Ord e0)' from being solved.
Probable fix: use a type annotation to specify what `e0' should be.
These potential instances exist:
instance (Ord a, Ord b) => Ord (Either a b)
-- Defined in `Data.Either'
instance Ord Ordering -- Defined in `ghc-prim-0.5.3:GHC.Classes'
instance Ord Integer
-- Defined in `integer-gmp-1.0.2.0:GHC.Integer.Type'
...plus 25 others
...plus 131 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the first argument of `MP.parseTest', namely `(C.char '+')'
In the expression: MP.parseTest (C.char '+') "+"
In an equation for `it': it = MP.parseTest (C.char '+') "+"
我正确使用了吗?我该怎么做才能解决此问题?
答案 0 :(得分:1)
用户定义的最新版本的“ megaparsec”支持custom error types。 ParsecT
类型由自定义错误的类型参数化。为了支持精美打印(例如parseTest
),此类自定义错误必须是Ord
和ShowErrorComponent
类型类的实例。
如果我们从不抛出任何自定义错误,则错误类型将保持多态。但是最后,在打印结果时,我们必须提供一个具体的类型。我们可以使用“ void”包中的无人居住类型Void
,并通过使用显式类型签名来通知类型检查器。
文档recommends定义了type Parser = Parsec Void Text
之类的类型同义词,并在您的签名中使用它。
另一种避免歧义的方法是使用visible type application:
Prelude Text.Megaparsec Data.Void> :t parseTest
parseTest
:: (ShowErrorComponent e, Show a, Stream s) =>
Parsec e s a -> s -> IO ()
Prelude Text.Megaparsec Data.Void> :set -XTypeApplications
Prelude Text.Megaparsec Data.Void> :t parseTest @Void
parseTest @Void
:: (Show a, Stream s) => Parsec Void s a -> s -> IO ()