jim*_*myt 0 lambda parsing haskell happy
如果我正在使用lambda表达式,那么在生产规则中快速计算表达式的值是不行的.
例如这段代码
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 }
Run Code Online (Sandbox Code Playgroud)
来自http://www.haskell.org/happy/doc/html/sec-using.html不起作用.
或者更准确地说我收到了错误消息
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
Run Code Online (Sandbox Code Playgroud)
如果你能解释一下我必须改变什么,那就太好了.
它必须与lambda表达式和环境变量p有关.
当我使用数据类型时,一切都很好.
这里要注意的是这个解析器的结果是一个带有变量绑定环境的函数.错误信息基本上是GHCi告诉你它无法打印功能,大概是因为你忘记了传递环境
> eval "1 + 1"
Run Code Online (Sandbox Code Playgroud)
什么时候你应该通过一个空的环境
> eval "1 + 1" []
Run Code Online (Sandbox Code Playgroud)
或者有一些预先定义的变量
> eval "x + x" [("x", 1)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1307 次 |
| 最近记录: |