如果类实例是一个循环,GHC会警告吗? [英] Can GHC warn if class instance is a loop?

查看:82
本文介绍了如果类实例是一个循环,GHC会警告吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真正的世界Haskell有这样的例子:

  class BasicEq3 a其中
isEqual3 :: a - > a - > Bool
isEqual3 x y = not(isNotEqual3 x y)

isNotEqual3 :: a - > a - > Bool
isNotEqual3 xy = not(isEqual3 xy)

instance BasicEq3 Bool

当我在GHCI中运行时:

 #> isEqual3 False False 
内存不足

所以,您必须至少实现其中一个2种方法或它会循环。你可以灵活地选择哪一个是整齐的。



我的问题是,有没有办法得到警告或某些东西,如果没有覆盖足够的默认值和默认值形成一个循环?对我来说这似乎很奇怪,编译器如此疯狂聪明对这个例子来说没什么问题。

解决方案

我觉得这很好GHC在不间断循环依赖的情况下发出警告。甚至还有一张这样的票: http://hackage.haskell.org/trac/ghc/ticket/6028

仅仅因为某些东西是不可判定的并不意味着不能有效地解决问题的任何实例。 GHC(或任何其他Haskell编译器)已经有相当多的信息需要它,如果用户在没有破坏循环依赖的情况下实例化一个类,它将很可能发出警告。如果编译器在前几篇文章中举例说明的极少数情况下出错,那么用户可以使用 -nowarnundefinedcyclicmethods 或类似的机制告诉GHC安静。在几乎所有其他情况下,警告将是最受欢迎的,并会增加程序员的生产力;避免几乎总是愚蠢的错误。


Real World Haskell has this example:

class BasicEq3 a where
    isEqual3 :: a -> a -> Bool
    isEqual3 x y = not (isNotEqual3 x y)

    isNotEqual3 :: a -> a -> Bool
    isNotEqual3 x y = not (isEqual3 x y) 

instance BasicEq3 Bool

And when I run it in GHCI:

#> isEqual3 False False
out of memory

So, you have to implement at least one of the 2 methods or it will loop. And you get the flexibility of choosing which one, which is neat.

The question I have is, is there a way to get a warning or something if didn't override enough of the defaults and the defaults form a loop? It seems strange to me that the compiler that is so crazy smart is fine with this example.

解决方案

I think it's perfectly fine for GHC to issue a warning in case of an "unbroken" cyclic dependency. There's even a ticket along those lines: http://hackage.haskell.org/trac/ghc/ticket/6028

Just because something is "undecidable" doesn't mean no instance of the problem can be solved effectively. GHC (or any other Haskell compiler) already has quite a bit of the information it needs, and it'd be perfectly possible for it to issue a warning if the user is instantiating a class without "breaking" the cyclic dependency. And if the compiler gets it wrong in the rare cases as exemplified in previous posts, then the user can have a -nowarnundefinedcyclicmethods or a similar mechanism to tell GHC to be quiet. In nearly every other case, the warning will be most welcome and would add to programmer productivity; avoiding what's almost always a silly bug.

这篇关于如果类实例是一个循环,GHC会警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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