开发者

Trivial parsec example produces a type error

开发者 https://www.devze.com 2023-03-20 17:04 出处:网络
I\'m trying to get this trivial parsec code to compile import Text.P开发者_Go百科arsec simple = letter

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:

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消