8
votes

So I like Haskell, but am dissatisfied with the Num class. So I want to make my own typeclass hierarchic for algebraic types.
The problem is, even if I import Prelude hiding Num and everything associated with it, still the only way to make the literal 1 have type t is to make t instance Num.
I would love to make my own fromInteger class and leave Num out of the picture entirely, like this

import Prelude hiding (everything having to do with Num)
import qualified Prelude (everything having to do with Num)

class (Eq fi) => FromInteger fi where
  fromInteger :: Integral -> fi

foo :: (FromInteger fi) => fi -> String
foo 1 = "that was a one"
foo 0 = "that was a zero"
foo n = "that was neither zero nor one"

and then I would implement fromInteger appropriately for brand new types and never have to say anything about Num.

Is there a way to tell the parser to use a different fromInteger method?

Thanks!

1

1 Answers

12
votes

You are looking for GHC's RebindableSyntax extension.

Enable it by putting

{-# LANGUAGE RebindableSyntax #-}

at the top of your source file.