首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell parsec令牌类型条件

Haskell parsec令牌类型条件
EN

Stack Overflow用户
提问于 2020-09-21 16:06:00
回答 2查看 105关注 0票数 0

考虑最小化的代码:

代码语言:javascript
复制
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)

我得到了错误:

代码语言:javascript
复制
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合金建议:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2020-09-21 16:27:00

将您的函数与the example in the documentation for token进行比较

代码语言:javascript
复制
 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

在每种情况下,输入都是位置和标记的元组。你正试图把这样一个元组当作只是一个象征。我不清楚为什么这个错误会产生你所得到的特定错误,但我认为你仍然可以看到这是一个错误。

票数 0
EN

Stack Overflow用户

发布于 2020-09-21 18:31:12

错误信息表明

代码语言:javascript
复制
(Use FlexibleContexts to permit this)

所以,我们可以加上

代码语言:javascript
复制
{-# LANGUAGE FlexibleContexts #-}

在文件的顶部。在那之后,它会编译得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63996024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档