简化问题
给定
class Foo f where
frobnicate :: f -> Float
我怎么能允许任何instance人Foo进入
data Bar = Bar { <here> }
?
实际问题
给定
-- Typically lightweight geometric objects, e.g. spheres
class Shape s where
intersect :: (RealFrac t, Floating t, Ord t, Shape s)
=> Ray t -> s t -> Maybe (DifferentialGeometry t)
和
-- Primitives contain higher level informations, like material properties
class Primitive p where
intersect :: (RealFrac t, Floating t, Ord t, Primitive p)
=> Ray t -> p t -> Maybe (Intersection t)
Primitive.intersect注意和的签名的唯一区别Shape.intersect在于返回类型。
现在我想添加一个包装器,它基本上将任何Shape转换为Primitive .
我认为它的工作原理大致是这样的:
data ShapeWrapperPrimitive t = ShapeWrapperPrimitive {
shape :: (Shape s) => s t
}
或者换句话说,我想添加一个任意shape成员,该成员属于Shape该类。
但是,这给了我Illegal polymorphic or qualified type.