自动haskell类型类实例
下面是演示该问题的简化示例代码。我想做的是摆脱混凝土形状构造器的模式匹配自动haskell类型类实例,haskell,Haskell,下面是演示该问题的简化示例代码。我想做的是摆脱混凝土形状构造器的模式匹配 data Shape a = SphereConst (Sphere a) | PlaneConst (Plane a) class GeomShape arg shape where intersect :: (Floating arg, Ord arg) => shape -> Ray3d arg -> Maybe (V3 arg) instance Geo
data Shape a =
SphereConst (Sphere a)
| PlaneConst (Plane a)
class GeomShape arg shape where
intersect :: (Floating arg, Ord arg) => shape -> Ray3d arg -> Maybe (V3 arg)
instance GeomShape a (Plane a) where
intersect p r = ...
instance GeomShape a (Sphere a) where
intersect s r = ...
与此相反:
instance GeomShape a (Shape a) where
intersect (SphereConst s) ray = s `intersect` ray
intersect (PlaneConst p) ray = p `intersect` ray
我想这样写:
instance GeomShape a (Shape a) where
intersect = intersect :: Sphere a
启用DeriveAnyClass扩展后,将编译以下代码:
data Shape a =
SphereConst (Sphere a)
| PlaneConst (Plane a) deriving (GeomShape a)
但是,它会崩溃,并显示一条消息:“没有用于类操作的实例或默认方法”
为了稍微澄清一下,键入变量“a”作为Double/Float的替代品。可能
GeomShape
需要使用两个参数,因此将GeomShape sha1-sha2分类到其中
,然后intersect::sha1 a->sha2 a->Maybe(V3 a)
。在这种情况下,您可以“填充矩阵”。另请参见Haskell中的表达式问题:我认为您需要使用模板Haskell来完成此操作。泛型需要一种动态解析实例的方法