考虑最小化的代码:
module Parser where
import Text.ParserCombinators.Parsec
import Text.Parsec.Pos
oneTokenP f = token show (\_ -> initialPos "Dummy") f
oneToken t = token show (\_ -> initialPos (show t))
(\t' -> if t == t' then Just () else Nothing)我得到了错误:
Parser.hs:8:1: error:
• Non type-variable argument
in the constraint: Text.Parsec.Prim.Stream
s Data.Functor.Identity.Identity a1
(Use FlexibleContexts to permit this)
• When checking the inferred type
oneTokenP :: forall u s a a1.
(Show a1,
Text.Parsec.Prim.Stream s Data.Functor.Identity.Identity a1) =>
(a1 -> Maybe a) -> Text.Parsec.Prim.Parsec s u a
Parser.hs:9:1: error:
• Non type-variable argument
in the constraint: Text.Parsec.Prim.Stream
s Data.Functor.Identity.Identity a
(Use FlexibleContexts to permit this)
• When checking the inferred type
oneToken :: forall u s a.
(Eq a, Show a,
Text.Parsec.Prim.Stream s Data.Functor.Identity.Identity a) =>
a -> Text.Parsec.Prim.Parsec s u ()我违反了哪种打字条件?
重写了以下@am合金建议:
oneTokenP f = token showTok posFromTok testTok
where
showTok (pos,t) = show t
posFromTok (pos,t) = initialPos "Dummy"
testTok (pos,t) = f t
oneToken x = token showTok posFromTok testTok
where
showTok (pos,t) = show t
posFromTok (pos,t) = initialPos (show t)
testTok (pos,t) = if x == t then Just () else Nothing发布于 2020-09-21 16:27:00
将您的函数与the example in the documentation for token进行比较
mytoken x
= token showTok posFromTok testTok
where
showTok (pos,t) = show t
posFromTok (pos,t) = pos
testTok (pos,t) = if x == t then Just t else Nothing在每种情况下,输入都是位置和标记的元组。你正试图把这样一个元组当作只是一个象征。我不清楚为什么这个错误会产生你所得到的特定错误,但我认为你仍然可以看到这是一个错误。
发布于 2020-09-21 18:31:12
错误信息表明
(Use FlexibleContexts to permit this)所以,我们可以加上
{-# LANGUAGE FlexibleContexts #-}在文件的顶部。在那之后,它会编译得很好。
https://stackoverflow.com/questions/63996024
复制相似问题