(Show([(String,Int)] - > Int))的实例 [英] No instance for (Show ([(String, Int)] -> Int))

查看:148
本文介绍了(Show([(String,Int)] - > Int))的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用lambda表达式来计算表达式在运行时生成规则中的表达式的值不起作用。



例如,代码

  Exp:let var'='Exp在Exp {\p  - > $ 6(($ 2,$ 4 p):p)} 
| Exp1 {$ 1}

Exp1:Exp1'+'Term {\p - > $ 1 p + $ 3 p}
| Exp1' - 'Term {\p - > $ 1 p - $ 3 p}
|期限{$ 1}

期限:期限'*'因子{\ p - > $ 1 p * $ 3 p}
|术语'/'因素{\ p - > $ 1 p`div` $ 3 p}
|因子{$ ​​1}

因子
:int {\ p - > $ 1}
| var {\p - >案例查询$ 1 p
无 - >错误no var
只是我 - >我}
| '('Exp')'{$ 2}

from http://www.haskell.org/happy/doc/html/sec-using.html doesn'
$ b

或者更精确地说,我收到了错误信息

 没有使用`print'引起的(Show([(String,Int)]  - > Int))

可能的修正:
添加一个实例声明( Show([(String,Int)] - > Int))
在一个交互式GHCi命令的列表中:print it

如果你能向我解释我必须改变的东西,那将是非常好的。



它必须与lambda表达式有关,环境变量p。

当我使用数据类型时,一切正常。

解决方案

这里要注意的是,这个解析器的结果是一个函数,它需要一个环境可变绑定的入口。错误信息基本上是GHCi,它告诉你它不能打印函数,大概是因为你忘记了传递一个环境

 > eval1 + 1

当您应该通过空的环境时

 > eval1 + 1[] 

或者带有一些预先定义的变量

 > evalx + x[(x,1)] 


to calculate the value of the expression on the fly at the production rules in happy doesn't work if I'm using the lambda expressions.

For example this code

Exp   : let var '=' Exp in Exp  { \p -> $6 (($2,$4 p):p) }
      | Exp1                    { $1 }

Exp1  : Exp1 '+' Term           { \p -> $1 p + $3 p }
      | Exp1 '-' Term           { \p -> $1 p - $3 p }
      | Term                    { $1 }

Term  : Term '*' Factor         { \p -> $1 p * $3 p }
      | Term '/' Factor         { \p -> $1 p `div` $3 p }
      | Factor                  { $1 }

Factor            
      : int                     { \p -> $1 }
      | var                     { \p -> case lookup $1 p of
                                    Nothing -> error "no var"
                                     Just i  -> i }
      | '(' Exp ')'             { $2 }

from http://www.haskell.org/happy/doc/html/sec-using.html doesn't work.

Or more precisly I 've got an error message

No instance for (Show ([(String, Int)] -> Int))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for (Show ([(String, Int)] -> Int))
    In a stmt of an interactive GHCi command: print it

It would be nice if you could explain me what I have to change.

It must have something to do with the lambda expression and the environment variable p.

When I'm using data types everything is fine.

解决方案

The thing to note here is that the result of this parser is a function which takes an environment of variable bindings. The error message is basically GHCi telling you that it can't print functions, presumably because you forgot to pass an environment

> eval "1 + 1"

when you should have either passed an empty environment

> eval "1 + 1" []

or one with some pre-defined variables

> eval "x + x" [("x", 1)]

这篇关于(Show([(String,Int)] - > Int))的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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