Making VectorSpace.Scalar instance of Eq

蓝咒 提交于 2019-12-11 04:47:18

问题


Note: I may not need to add Scalar to Eq, although it should solve the problem if I could figure out how to do just that.

So I'm trying to add some functionality to the ForceLayout module. Adding mass to Particles like so:

data Particle v = Particle { 
                         _pos   :: Point v
                       , _vel   :: v
                       , _force :: v
                       , _mass :: Scalar v
                       }
    deriving (Eq, Show)

But Scalar is not in Eq or Show! So this wont compile. Mass should be a scalar "compatible" with the other vectors though. How can I reconcile this? I don't understand type families sufficiently to analyse this situation. I've tried but they are mighty hard to grasp. Not sure if adding Scalar to Eq is necessary or possible.


回答1:


If you want to show the mass field, the show instance is going to have to look like

instance (Show v, Show (Point v), Show (Scalar v)) => Show (Particle v) where

Arguably, the fact that GHC doesn't work this out is a bug, or at least a missing feature. Luckily, with some extension heavy lifting, we can give it the context ourselves:

{-# LANGUAGE StandaloneDeriving, TypeFamilies,
    FlexibleContexts, UndecidableInstances #-}
{- ... -}

deriving instance (Show v, Show (Point v), Show (Scalar v)) => Show (Particle v)

This is a "standalone deriving declaration" as described in the GHC user guide, and its purpose here is basically so that we can specify a context that GHC wouldn't normally be able to work out.

That ought to do the trick. I'm a little worried about the UndecidableInstances, since it does seem like it ought to be possible to define instances of Point and Scalar such that this instance is circular, but despite my best efforts I haven't been able to make the typechecker loop, so it's probably fine.



来源:https://stackoverflow.com/questions/10935985/making-vectorspace-scalar-instance-of-eq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!