Idris 中接口实例的接口约束

Interface constraints for interface instances in Idris

我刚开始学习来自 Haskell 的 Idris,我正在尝试编写一些简单的线性代数代码。

我想为 Vect 编写一个 Num 接口实例,但专门为 Vect n a 编写一个 a 有一个 Num 实例的约束。

在Haskell中我会写一个这样的类型类实例:

instance Num a => Num (Vect n a) where
  (+) a b = (+) <$> a <*> b
  (*) a b = (*) <$> a <*> b
  fromInteger a = a : Nil

但是阅读 Idris interface docs 似乎没有提到对接口实例的约束。

我能做的最好的是以下,这可以预见地导致编译器抱怨 a 不是数字类型:

Num (Vect n a) where
  (+) Nil Nil = Nil
  (+) (x :: xs) (y :: ys) = x + y :: xs + ys
  (*) Nil Nil = Nil
  (*) (x :: xs) (y :: ys) = x * y :: xs * ys
  fromInteger i = Vect 1 (fromInteger i)

我可以通过使用 Num 约束创建自己的向量类型(不可移植)或在命名空间中重载 (+)(感觉有点笨拙)来解决这个问题:

namespace Vect
  (+) : Num a => Vect n a -> Vect n a -> Vect n a
  (+) xs ys = (+) <$> xs <*> ys

是否有限制接口实现的方法,或者是否有更好的方法来实现这一点,例如使用依赖类型?

在 Idris 中,您将(几乎)与 haskell

一样
Num a => Num (Vect n a) where

像许多事情一样,这在 the book 中,但显然不在文档中。