Fun with types! Resolving multiple instance declarations

前端 未结 2 1007
梦如初夏
梦如初夏 2020-12-20 23:06

I\'m trying to write some Haskell code in which there are multiple data types, each of which can have multiple implementations. To do this, I define each data type as a

2条回答
  •  一生所求
    2020-12-20 23:39

    There's no really good way to do this; the best practice is to define some constants like

    plusA, minusA :: (A a, Num x) => a x -> a x -> a x
    

    which makes writing the Num instances more mechanical after you have an A instance:

    instance A Foo where ...
    instance Num x => Num (Foo x) where
        (+) = plusA
        (-) = minusA
    

提交回复
热议问题