[Haskell-cafe] multi type addition

william murphy will.t.murphy at gmail.com
Tue Dec 28 22:45:50 CET 2010

We were trying to make an addition function that, unlike the library one,
could add numbers of different types from the typeclass 'Num.' We originally
ran into a problem when trying to add (5 :: Integer) + 4.8, and were trying
to define a new function that would be able to get that:

(+') :: (Num a, Num b) => a -> b -> Float
x +' b = d + e
         where d :: Float
               d = x
               e :: Float
               e = b

This gave us the error:

    Couldn't match expected type `Float' against inferred type `b'
      `b' is a rigid type variable bound by
          the type signature for `pl' at test.hs:9:18
    In the expression: b
    In the definition of `e': e = b
    In the definition of `pl':
        pl x b
             = d + e
                 d :: Float
                 d = x
                 e :: Float
                 e = b
Failed, modules loaded: none.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20101228/f2e15172/attachment.htm>

More information about the Haskell-Cafe mailing list