拥抱!部分应用程序错误 [英] Hugs !! Partial Application Bug

查看:124
本文介绍了拥抱!部分应用程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hugs在部分应用程序中似乎遇到了几个未被包含的 !! 问题。



While这在GHCi中运作良好:

 ([[0]] !! 0 !!)0 

拥抱为报告语法错误。



这是Hugs中的一个bug吗?



为第二个列表索引操作符添加一个额外的大括号:

 (([[0]] !!)0 !!)0 

 (([[0] ] !! 0)!!)0 


解决方案

Hugs中的一个已知问题。从表达式的拥抱98用户指南部分:


在Hugs中,表达式必须是 fexp (或者 case do )。像(a + b +)(a * b +)这样的法律表达式被拒绝。


移动提示



也许这就是 FUZXXl 在他的评论中谈到了?



尝试定义您自己的(!!)函数在 ghc 中,并将它设置为右联合固定:

  import prelude hiding((!!))
infixr 5 !! - infixr将使它正确联想
(!!)a b = head。在b



现在该行在 ghci 要么!

  ghci> :t([[0]] !! 0 !!)

< interactive>:1:1:
运算符'!!'[infixr 5]节
的优先级必须低于操作数的优先级,
也就是`[[infixr 5]
的部分:`[[0]] !! 0 !!'

因为(!!)已经被设置为 infixr 并且现在是右关联的。如果您使用 infixl ,该行可以正常工作。



这与您询问的问题完全不同。这是关于左侧vs右侧关联性的问题,而Hugs的问题在于它只是不解析像(a + b +)这样的表达式。


Hugs seems to have a problem with several non-enbraced !! in a partial application.

While this works fine in GHCi:

([[0]]!!0!!)0

Hugs reports a syntax error for the ).

Is this a bug in Hugs?

Adding an extra brace for the second list index operator works though:

(([[0]]!!)0!!)0

or

(([[0]]!!0)!!)0

解决方案

This is a known issue in Hugs. From the Hugs 98 Users Guide section on Expressions:

In Hugs, the expression must be an fexp (or case or do). Legal expressions like (a+b+) and (a*b+) are rejected.

Digression Alert

Maybe this is what FUZxxl was talking about in his comment?

Try defining your own (!!) function in ghc and set it to have right-associative fixity:

import Prelude hiding ((!!))
infixr 5 !! -- infixr will make it right associative
(!!) a b = head . drop b $ a

Now that line won't work in ghci either!

ghci> :t ([[0]] !! 0 !!)

<interactive>:1:1:
    The operator `!!' [infixr 5] of a section
        must have lower precedence than that of the operand,
          namely `!!' [infixr 5]
        in the section: `[[0]] !! 0 !!'

Because (!!) has been set with infixr and is now right-associative. If you use infixl, that line works fine.

This is a completely separate issue from the question you asked though. This is about left vs right associativity, whereas the problem with Hugs is that it just doesn't parse an expression like (a+b+).

这篇关于拥抱!部分应用程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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