简体   繁体   中英

How to Define Typeclass Synonyms

In an attempt to make a sane(r) alternative to Haskell's numeric type system, the devs of numeric-prelude slipped up and decided to name all of their type classes C . Aside from making the docs totally confusing, this means that I have to fully qualify all uses of the typeclasses:

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 = ...

Also, since NumericPrelude has finer grained typeclasses, I usually have to import several different NumericPrelude modules. I can simplify this a little by defining top-level constraint synonyms :

{-# LANGUAGE ConstraintKinds #-}

module NPSynonyms (Additive) where

import qualified Algebra.Additive (C)

type Additive a = (Algebra.Additive.C a)

which allows me to make sane functions:

myadd :: (Additive a) => a -> a -> a
myadd a b = ...

However, when I need to define an instance, I still have to (also) import the original NumericPrelude class:

{-# LANGUAGE ConstraintKinds #-}

import NPSynonyms
import Algebra.Additive (C)

newtype Foo a = Foo a

instance (Additive a) => Algebra.Additive.C (Foo a) where ...

So instead of making Additive a type synonym with kind Constraint , what I'd really like is to define a typeclass synonym for the typeclass Algebra.Additive.C . Is there any way to do this in GHC 7.8, or is there any sane alternative?

you have to fully qualify

No, not fully qualify. Consider:

import qualified Algebra.Additive as Add

myadd :: Add.C a => a -> a -> a

That looks fairly readable to me.

EDIT:

Also consider making a superclass and treating that as an alias:

class (Add.C a, Ring.C a) => Num a where
instance Num Int
instance Num Word

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM