[Haskell-cafe] Numeric literals

Lauri Oksanen lassoken at gmail.com
Fri Mar 20 16:09:04 EDT 2009


Thanks for answers. Here is some working code if somebody plays later
with similar things.

{-# OPTIONS
 -XNoImplicitPrelude
 -XFunctionalDependencies
 -XMultiParamTypeClasses
 -XFlexibleInstances
#-}
module Test (
      Integer
    , Double
    , fromInteger
    , fromRational
    , (+)
) where
import Prelude (Integer, Double)
import qualified Prelude as P
import qualified GHC.Real

fromInteger :: Integer -> Integer
fromInteger = P.id

fromRational :: P.Rational -> Double
fromRational (n GHC.Real.:% d) = let
    n' = P.fromInteger n :: Double
    d' = P.fromInteger d :: Double
    in n' P./ d'


-- Prelude types ---------

instance Semigroup Integer where plus = (P.+)
instance Semigroup Double where plus = (P.+)
instance Subset Integer Double where embed = P.fromInteger


-- Class hierarchy ---------

class Plus a b c | a b -> c where
   (+) :: a -> b -> c

class Semigroup a where
    plus :: a -> a -> a

class Subset a b where
    embed :: a -> b

instance (Semigroup a) => (Plus a a a) where (+) = plus


-- Coercion rules ---------

instance Plus Double Integer Double where
   x + j = x + (embed j :: Double)
instance Plus Integer Double Double where
   j + x = (embed j :: Double) + x


Ps. I'm very interested in hearing, if somebody has ideas, how to
generalize the coercion rules to something like

instance (Semigroup a) => (Subset b a) => (Plus a b a) where
   x + j = x + (embed j)


- Lauri

On Fri, Mar 20, 2009 at 3:58 PM, Lennart Augustsson
<lennart at augustsson.net> wrote:
> I think your best bet is -fno-implicit-prelude, and defining
> fromInteger = id :: Integer->Integer.
>


More information about the Haskell-Cafe mailing list