部分应用的等式约束 [英] Partially applied equality constraint

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

问题描述

考虑以下几点:

  class(a〜b)=>等于b 
实例(a〜b)=>等于ab

可以说我以后有一个数据类型:

  data D(c :: *  - > Constraint)其中
D :: Proxy c - > D c

然后像这样有效:

  D(Proxy ::(Proxy(Equal Int)))

我的问题是,有没有办法写 Equal ,而不是我用过的重复类/实例符号?

当我将 Equal 定义为类型族 Equal Int 不起作用,因为它是一个不完整的应用程序。



类/实例hack看起来很糟糕,但至少可以工作,但是还有其他可能更干净的方法实现这个吗?



注意我在实际的代码中使用比平等更复杂的约束,我只是认为这将是一个很好的例子。 b $ b

解决方案

您根本不需要类,您可以在(〜)中使用前缀符号。

GHC 8.0.1有一个错误,有时会导致崩溃,但它只是一个错误,它在8.0.2中修复。您所描述的类/实例组合对于此错误确实是一种有效的解决方法。



如果您不需要向后兼容8.0.1,则可以使用(〜)Int 代理服务器((〜)Int)直接。


Consider the following:

class (a ~ b) => Equal a b
instance (a ~ b) => Equal a b

Lets say I later have a data type:

data D (c :: * -> Constraint) where
  D :: Proxy c -> D c

Then something like this is valid:

D (Proxy :: (Proxy (Equal Int)))

My question is, is there any way to write Equal other than the repetitive class/instance notation I've used?

It seems that when I define Equal as a type family Equal Int doesn't work because it's an incomplete application.

The class/instance hack looks bad but at least it works, but is there any other maybe cleaner way of achieving this?

Note I'll be using constraints more complex than equality in my actual code, I just thought this would be a good example.

解决方案

You do not need a class at all, you can just use (~) in prefix notation.

There is a bug in GHC 8.0.1 which sometimes makes it crash, but it is just a bug, and it's fixed in 8.0.2. The class / instance combo you're describing is indeed an effective workaround for this bug.

If you do not need backwards compatibility with 8.0.1, you can use (~) Int or Proxy ((~) Int) directly.

这篇关于部分应用的等式约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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