如何在记录中声明我的属性属于某种类型 class?

How do I declare that my attributes belong to some type class in a record?

我想说的是,我的记录属性必须属于一种类型 class,因此它们不受限于该类型 class 中的特定类型。我想做的代码示例:

data Complex = Complex {
    real :: Num a => a,
    imag :: Num b => b
}

这可能吗,如果可能,怎么做?

通常,您会参数化类型本身

data Complex a b = Complex { real :: a, image :: b }

或更有可能

data Complex a = Complex { real :: a, image :: a }

并对使用以下类型的任何函数施加约束:

foo :: Num a => Complex a -> a
foo c = 3 * real c - 5 * imag c

bar :: Num a => Complex a -> Complex a
bar (Complex r i) = Complex (3 * r) (negate (5 * i))