14 May 2013 00:08

## fromIntegral not enough?

```This is probably a haskell-beginners sort of question, but I usually get

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,
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:
```

14 May 2013 00:43

### Re: fromIntegral not enough?

```On Mon, May 13, 2013 at 02:08:26PM -0800, Christopher Howard wrote:
> 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...

Your definition of "angles" forces "dAng" to be of type "a".  Then in order
to define "dAng" as the result of a "/" there must be a "Fractional" instance for
"a".

Hope that helps.

Tom
```
14 May 2013 00:53

### Re: fromIntegral not enough?

```On Mon, May 13, 2013 at 11:43:41PM +0100, Tom Ellis wrote:
> On Mon, May 13, 2013 at 02:08:26PM -0800, Christopher Howard wrote:
> > 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...
>
> Your definition of "angles" forces "dAng" to be of type "a".  Then in order
> to define "dAng" as the result of a "/" there must be a "Fractional" instance for
> "a".

You probably want

let angles = map ((* dAng) . fromInteger) [0..divns] in
...

```
14 May 2013 02:07

### Re: fromIntegral not enough?

```On 05/13/2013 02:53 PM, Tom Ellis wrote:
> On Mon, May 13, 2013 at 11:43:41PM +0100, Tom Ellis wrote:
>
> You probably want
>
>     let angles = map ((* dAng) . fromInteger) [0..divns] in
>     ...
>
>

Ah, that works. Thanks all.

--

--
frigidcode.com

```
```_______________________________________________