Type Variable Constraint

I am trying to use the Parsec library to parse a list of values Token. I would like to use a function Tokenin Text.Parsec.Prim to match a single value. It seems like this should work:

type TokenParser a = Parsec [Token] () a

mytoken :: (Token -> Bool) -> TokenParser Token
mytoken test = token showTok posFromTok testTok
  where -- and so on

This gives a compilation error:

No instance for (Stream [Token] Identity Token)
  arising from a use of `Prim.token'
Possible fix:
  add an instance declaration for (Stream [Token] Identity Token)

Ok, change the type declaration to mytoken:

mytoken :: Stream [Token] Identity Token => (Token -> Bool) -> TokenParser Token

This works after adding the extension {-# LANGUAGE FlexibleContexts #-}.

What's happening? First of all, the class definition Streamin Text.Parsec.Prim has Monad m => Stream [tok] m tokas one of the instances. Shouldn't Stream [Token] Identity Tokenthis instance be covered? Secondly, how does all this limit? There mytokenare no type restrictions in a type.

, "" mytoken , ​​ , No instance for (Stream [Token] Identity Token) arising from... , , -op- , mytoken.

- , , .

+4
1

, , . ( .)

import Text.Parsec.Prim

data Token

type TokenParser a = Parsec [Token] () a

mytoken :: (Token -> Bool) -> TokenParser Token
mytoken test = token showTok posFromTok testTok

showTok = undefined
posFromTok = undefined
testTok = undefined

Text.Parsec.Prim Text.Parsec . ( Text.Parsec.String, Text.Parsec.Prim.) , , :

mytoken :: Stream [Token] Identity Token => (Token -> Bool) -> TokenParser Token

, "C = > T" : " , T, , C ". , mytoken , , , mytoken , (Token -> Bool) -> TokenParser Token, , , Stream [Token] Identity Token . , .

+9

Source: https://habr.com/ru/post/1534964/


All Articles