Trivial parsec示例产生一个类型错误 [英] Trivial parsec example produces a type error

查看:148
本文介绍了Trivial parsec示例产生一个类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个简单的parsec代码编译

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

import Text.Parsec
simple = letter

但我不断收到此错误

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


推荐答案

我认为你已经违反了单态限制。这个限制意味着:如果一个变量没有明确的参数声明,它的类型必须是单形的。这强制类型检查器选择 Stream 的特定实例,但它无法做出决定。

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.

有两个打击它的方法:
$ b

There are two ways to fight it:


  1. 简单显式签名:

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.

    这篇关于Trivial parsec示例产生一个类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆