在 Idris 中限制记录类型

Constraining a record type in Idris

我正在尝试在 Idris 中编写一条记录,但它有一个通用参数,需要通过接口进行约束。对于普通联合类型,我可以这样写:

data BSTree : (a : Type) -> Type where
  Empty : Ord a => BSTree a
  Node  : Ord a => BSTree a -> a -> BSTree a

但我想弄清楚做同样事情的语法,只是用一条记录。我试过类似的东西:

record Point a where
  constructor MkPoint : Eq a => a -> a -> Point a
  x : a
  y : a

但它无法编译。

有没有办法在 Idris 中做到这一点?

TIA

你可以这样做:

record Point a where
  constructor MkPoint
  x : Eq a => a
  y : Eq a => a

虽然实际上你不应该这样做。相反,最好使用一些智能构造函数,一些其他函数,如 mkPoint : Eq a => a -> a -> MkPoint a。 在现实生活中,您不需要为数据类型限制字段类型。阅读有关 -XDataTypeContexts Haskell 扩展名的信息。