Overriding fromInteger in Haskell

情到浓时终转凉″ 提交于 2019-12-21 10:49:01

问题


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:


You are looking for GHC's RebindableSyntax extension.

Enable it by putting

{-# LANGUAGE RebindableSyntax #-}

at the top of your source file.



来源:https://stackoverflow.com/questions/13978395/overriding-frominteger-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!