0

我正在尝试使用 uu-parsinglib[Word8]而不是 [Char] 进行操作。(我想使用 uu-parsinglib 进行错误报告。)我需要一个解析器来获取Word8序列中的下一个,无论它是什么。一旦有了它,我就可以构建更复杂的解析器。但是我很难弄清楚如何编写它。我能得到的最接近的是:

{-# LANGUAGE FlexibleContexts #-}

module Main where

import Control.Applicative ((<|>))
import Data.Word
import Text.ParserCombinators.UU.BasicInstances

pRawWord8 :: Parser Word8
pRawWord8 = pSatisfy (const True) (Insertion undefined undefined undefined)

但是,该实现显然返回了错误的类型。

amy2.hs:10:13:
    Couldn't match type ‘Char’ with ‘Word8’
    Expected type: Text.ParserCombinators.UU.Core.P
                     (Str Char state loc) Word8
      Actual type: Text.ParserCombinators.UU.Core.P
                     (Str Char state loc) Char
    In the expression:
      pSatisfy (const True) (Insertion undefined undefined undefined)
    In an equation for ‘pRawWord8’:
        pRawWord8
          = pSatisfy (const True) (Insertion undefined undefined undefined)

这让我感到惊讶,因为我看不到 for 的类型签名如何pSatisfy限制我返回 aChar而不是 a Word8

我该如何实施pRawWord8

4

1 回答 1

1

pSatisfy 有类型:

pSatisfy :: forall loc state a. (Show a, loc `IsLocationUpdatedBy` a, ListLike state a) => (a -> Bool) -> Insertion a -> P (Str a state loc) a

因此 Parser 返回与输入 ( ) 相同的类型a。由于解析器是

type Parser a = (IsLocationUpdatedBy loc Char, ListLike state Char) => P (Str Char state loc) a

Parser 的输入是 Char 的 ListLike,因此 pSatisfy 只能返回 Parser Char。因此,类型确实禁止您尝试做的事情。

也许您应该将函数键入为

pRawWord8 :: (IsLocationUpdatedBy loc Word8, ListLike state Word8) => P (Str Word8 state loc) Word8

或者按照这些思路定义您自己的类型同义词。

于 2015-07-21T16:17:52.930 回答