Overlapping instances via Nat-kind

◇◆丶佛笑我妖孽 提交于 2019-12-05 13:55:12

Add Bounded (Symmetric (n-1)) and Enum (Symmetric (n-1)) as constraints, because fully resolving those constraints would require knowing the exact value of n.

instance (KnownNat n, 2 <= n, Bounded (Symmetric (n-1)), Enum (Symmetric (n-1))) =>
  Enum (Symmetric n) where
  ...

instance (KnownNat n, 2 <= n, Bounded (Symmetric (n-1))) =>
  Bounded (Symmetric n) where
  ...

To avoid FlexibleInstances (which is not worth it IMO, FlexibleInstances is a benign extension), use Peano numbers data Nat = Z | S Nat instead of GHC's primitive representation. First replace the instance head Bounded (Symmetric n) with Bounded (Symmetric (S (S n'))) (this plays the role of the constraint 2 <= n), and then break up the instance with an auxiliary class (possibly more) to satisfy the standard requirement on instance heads. It might look like this:

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