Peter
Peter

Reputation: 1472

Trivial parsec example produces a type error

I'm trying to get this trivial parsec code to compile

import Text.Parsec
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

Upvotes: 16

Views: 1536

Answers (1)

fuz
fuz

Reputation: 93127

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:

  1. Give simple an explicit signature:

    simple :: Stream s m Char => ParsecT s u m Char
    simple = letter
    
  2. Disable the monorphism restriction:

    {-# LANGUAGE NoMonomorphismRestriction #-}
    import Text.Parsec
    simple = letter
    

See What is the monomorphism restriction? for more information on the monomorphism restriction.

Upvotes: 19

Related Questions