为了成为Haskell的数字类型系统的替代品(sa),numeric-prelude的开发人员滑倒并决定命名所有类型类C
。除了使文档完全混淆之外,这意味着我必须完全限定类型类的所有用法:
import qualified Algebra.Additive (C)
import qualified Algebra.Ring (C)
...
newtype Foo a = Foo a
instance (Algebra.Additive.C a) => Algebra.Additive.C (Foo a) where ...
myadd :: (Algebra.Additive.C a) => a -> a -> a
myadd a b = ...
此外,由于NumericPrelude具有更细粒度的类型类,因此我通常必须导入几个不同的NumericPrelude模块。我可以通过定义顶级约束同义词来简化这一点:
{-# LANGUAGE ConstraintKinds #-}
module NPSynonyms (Additive) where
import qualified Algebra.Additive (C)
type Additive a = (Algebra.Additive.C a)
允许我做出理智的功能:
myadd :: (Additive a) => a -> a -> a
myadd a b = ...
但是,当我需要定义一个实例时,我仍然必须(也)导入原始的NumericPrelude类:
{-# LANGUAGE ConstraintKinds #-}
import NPSynonyms
import Algebra.Additive (C)
newtype Foo a = Foo a
instance (Additive a) => Algebra.Additive.C (Foo a) where ...
因此,我没有将Additive
与类型同义词与Constraint
类型相提并论,而是真正类似于定义<类型Algebra.Additive.C
的em> typeclass 同义词。在GHC 7.8中有没有办法做到这一点,还是有任何理智的选择?
答案 0 :(得分:5)
你必须完全符合资格
不,不完全符合资格。考虑:
import qualified Algebra.Additive as Add
myadd :: Add.C a => a -> a -> a
这看起来对我来说相当可读。
编辑:
还要考虑创建一个超类并将其视为别名:
class (Add.C a, Ring.C a) => Num a where
instance Num Int
instance Num Word