Trivial parsec example produces a type error
I'm trying to get this trivial parsec code to compile
import Text.P开发者_Go百科arsec
simple = letter
but I keep getting this error
No instance for (Stream s0 m0 Char)
arising from a use of `letter'
Possible fix: add an instance declaration for (Stream s0 m0 Char)
In the expression: letter
In an equation for `simple': simple = letter
I think you have ran against the monomorphism restriction. This restriction means: If a variable is declared with no explicit arguments, its type has to be monomorphic. This forces the typechecker to pick a particular instance of Stream
, but it can't decide.
There are two ways to fight it:
Give
simple
an explicit signature:simple :: Stream s m Char => ParsecT s u m Char simple = letter
Disable the monorphism restriction:
{-# LANGUAGE NoMonomorphismRestriction #-} import Text.Parsec simple = letter
See What is the monomorphism restriction? for more information on the monomorphism restriction.
精彩评论