Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding fromInteger in Haskell

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!

like image 773
Joseph Victor Avatar asked Dec 20 '12 18:12

Joseph Victor


1 Answers

You are looking for GHC's RebindableSyntax extension.

Enable it by putting

{-# LANGUAGE RebindableSyntax #-}

at the top of your source file.

like image 148
dave4420 Avatar answered Nov 18 '22 18:11

dave4420