Haskell overlapping instances and type functions

梦想的初衷 提交于 2020-06-27 07:24:28

问题


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)...


回答1:


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.



来源:https://stackoverflow.com/questions/4463154/haskell-overlapping-instances-and-type-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!