Fun with types! Resolving multiple instance declarations

狂风中的少年 提交于 2019-11-29 14:16:10

This part of your question

I suppose that the 'duplicate instance declarations' message is because a data type could be made an instance of both A and B. I want to be able to make a promise to the compiler that I won't do that, or possibly specify a default class to use in the case that a type is an instance of both classes.

is incorrect. It's actually because you've written two instances,

instance Num (a x)
instance Num (b x)

that the compiler can't tell apart (see the link from @hammar's comment, class contexts don't count for the purpose of differentiating between instance declarations).

One solution is to add a witness type.

{-# LANGUAGE FlexibleInstances, FlexibleContexts, UndecidableInstances, OverlappingInstances #-}

data AWitness

data AImpl witness x = AImpl x deriving (Eq,Show)

instance A (AImpl AWitness) where
    fa (AImpl x) = x
    ga x = AImpl x

instance (A (a AWitness), Num x, Show (a AWitness x), Eq (a AWitness x)) => Num (a AWitness x) where
    a1 + a2 = ga (fa a1 + fa a2)

The compiler can use the witness types to differentiate between your instance declarations.

There's no really good way to do this; the best practice is to define some constants like

plusA, minusA :: (A a, Num x) => a x -> a x -> a x

which makes writing the Num instances more mechanical after you have an A instance:

instance A Foo where ...
instance Num x => Num (Foo x) where
    (+) = plusA
    (-) = minusA
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!