Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 320
crockeea Avatar asked Jun 20 '14 15:06

crockeea


People also ask

How to define a typeclass Haskell?

What's a typeclass in Haskell? A typeclass defines a set of methods that is shared across multiple types. For a type to belong to a typeclass, it needs to implement the methods of that typeclass. These implementations are ad-hoc: methods can have different implementations for different types.

What does deriving show mean Haskell?

The second line, deriving (Eq, Show) , is called the deriving clause; it specifies that we want the compiler to automatically generate instances of the Eq and Show classes for our Pair type. The Haskell Report defines a handful of classes for which instances can be automatically generated.

What is instance Haskell?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.


1 Answers

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
like image 171
Thomas M. DuBuisson Avatar answered Nov 06 '22 06:11

Thomas M. DuBuisson