Haskell 重叠实例和类型函数 [英] Haskell overlapping instances and type functions

查看:51
本文介绍了Haskell 重叠实例和类型函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型类来模拟类似 SQL 的查询优化:

I have the following typeclass which models a SQL-like query optimization:

class OptimizableQuery q where
  type Optimized q :: *
  optimize :: q -> Optimized q

instance Query q => OptimizableQuery q where
  type Optimized q = q
  optimize q = q

instance (Query q, OptimizableQuery q) => OptimizableQuery (Select (Select q p) p) where
  type Optimized (Select (Select q p) p) = Select (Optimized q) p
  optimize (Select (Select q _) p) = Select (optimize q) p

问题是我在优化类型函数上收到错误冲突的族实例声明".为什么会这样,我该如何解决?拥有一个后备实例"而不是用尽所有案例(可能很多)真的很好......

the problem is that I get the error "Conflicting family instance declarations" on the Optimized type function. Why is that and how can I solve it? It would really be nice to have a "fallback instance" instead of having to exhaust all cases (which might be quite many)...

推荐答案

实例与类型系列重叠是非法的.请参阅GHC 手册,详情请参见类型同义词实例的重叠".

It's illegal to have overlapping instances with type families. See the GHC manual, "Overlap of type synonym instances" for details.

原因是类型函数应用程序根据可用实例有两种不同的可能结果会导致不健全.

The reason is that having two different possible results for a type function application depending on the available instances can lead to unsoundness.

这篇关于Haskell 重叠实例和类型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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