[Haskell-cafe] fromIntegral not enough?

Christopher Howard christopher.howard at frigidcode.com
Tue May 14 00:08:26 CEST 2013


This is probably a haskell-beginners sort of question, but I usually get
about 4x as many responses from cafe, about 10x as fast.

I have code like so:

code:
--------
data Xy a = Xy a a

class Coord2 a where

  coords2 :: Fractional b => a b -> Xy b

data CircAppr a b = CircAppr a b b -- number of points, rotation angle,
radius
    deriving (Show)

instance Integral a => Coord2 (CircAppr a) where

  coords2 (CircAppr divns ang rad) =
      let dAng = 2 * pi / (fromIntegral divns) in
      let angles = map (* dAng) [0..divns] in
      undefined -- To be coded...
--------

In the instance definition divns is an integral trying to divide a
fractional. I hoped wrapping it in fromIntegral would coerce, but
apparently not:

quote:
--------
    Could not deduce (Fractional a) arising from a use of `/'
    from the context (Integral a)
      bound by the instance declaration
      at /scratch/cmhoward/pulse-spin/pulse-spin.hs:34:10-42
    or from (Fractional b)
      bound by the type signature for
                 coords2 :: Fractional b => CircAppr a b -> Xy b
      at /scratch/cmhoward/pulse-spin/pulse-spin.hs:(36,3)-(39,15)
    Possible fix:
      add (Fractional a) to the context of
        the type signature for
          coords2 :: Fractional b => CircAppr a b -> Xy b
        or the instance declaration
    In the expression: 2 * pi / (fromIntegral divns)
    In an equation for `dAng': dAng = 2 * pi / (fromIntegral divns)
    In the expression:
      let dAng = 2 * pi / (fromIntegral divns) in
      let angles = map (* dAng) [0 .. divns] in undefined
--------

So, I'm wondering how I can do what I'm trying to do here, while still
keeping my types as generic as possible.

-- 
frigidcode.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 555 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130513/08ca277f/attachment.pgp>


More information about the Haskell-Cafe mailing list