Search code examples
haskelluu-parsinglib

Using uu-parsinglib to parse a sequence of Word8s


I'm trying to use uu-parsinglib to operate on [Word8] rather than [Char]. (I want to use uu-parsinglib for the error reporting.) I need a parser that will fetch me the next Word8 in the sequence, whatever it is. Once I have that, I can build more complex parsers. But I'm having trouble figuring out how to write it. The closest I've been able to get is:

{-# 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)

However, that implementation apparently returns the wrong type.

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)

This surprises me, because I don't see how the type signature for pSatisfy is restricting me to returning a Char instead of a Word8.

How can I implement pRawWord8?


Solution

  • pSatisfy has type:

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

    So the Parser returns the same type as the input (a). Since Parser is

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

    The input to Parser is a ListLike of Char, and hence pSatisfy can only return a Parser Char. So the types do forbid what you're trying to do.

    Maybe you should type your function as something like

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

    Or define your own type synonym along these lines.