Trivial parsec example produces a type error

风格不统一 提交于 2019-12-06 17:24:12

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/6723208/trivial-parsec-example-produces-a-type-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!