必须存在一种良好的惯用方式来表达 Haskell 中类型级别的一般计算。我能想到的就是这个(非法的)OO 模仿。
class Computation where
compute :: Computation -> Double -> Double
data Id = Id
instance Computation Id where
compute _ = id
data Square a = Computation a => Square a
instance Computation (Square a) where
compute (Square underlying) x = sqr $ compute underlying x where square x = x*x
data Scale a = Computation a => Scale a Double
compute (Scale underlying c) x = c * compute underlying x
理想情况下,我想保持开放,所以这种方法对我没有吸引力。我要求太多了吗?