Newbie "Instance Of Floating Int" Error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Newbie "Instance Of Floating Int" Error

Aditya Siram
Hi all,
I just started working with Haskell and am having some difficulties with its
type system.
Here is function that is supposed to calculate the distance between two
coordinates:
distBetween (x1,y1) (x2,y2) = sqrt((x2-x1)^2  + (y2-y1)^2)

I am trying to explictly give it a type signature. Here is what I have tried
and the errors generated by Hugs:

type Point = (Int,Int)
distBetween :: Point -> Point -> Float
>>ERROR - Type error in explicitly typed binding
*** Term           : distBetween
*** Type           : Point -> Point -> Int
*** Does not match : Point -> Point -> Float

distBetween :: Point -> Point -> Int
>>Instance of Floating Int required for definition of distBetween

Any help is appreciated...
Deech


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Newbie "Instance Of Floating Int" Error

Cale Gibbard
One thing to try:
type Point = (Float, Float)

The problem is that x1, y1, x2, and y2 are Ints, and so (x2-x1)^2  +
(y2-y1)^2 is also an Int, but then you try to take the square root,
which is an operation only available on floating point values (more
specifically, types in the class Floating, which also includes things
like Complex Double).

Another option is to leave the Point type as-is, but do a conversion
just before applying the sqrt, say, by applying the fromIntegral
function, which serves to convert from types in the class Integral,
like Int and Integer to any numeric type you need.
distBetween (x1,y1) (x2,y2) = sqrt . fromIntegral $ (x2-x1)^2 + (y2-y1)^2

hope this helps,
 - Cale

On 27/04/06, Aditya Siram <[hidden email]> wrote:

> Hi all,
> I just started working with Haskell and am having some difficulties with its
> type system.
> Here is function that is supposed to calculate the distance between two
> coordinates:
> distBetween (x1,y1) (x2,y2) = sqrt((x2-x1)^2  + (y2-y1)^2)
>
> I am trying to explictly give it a type signature. Here is what I have tried
> and the errors generated by Hugs:
>
> type Point = (Int,Int)
> distBetween :: Point -> Point -> Float
> >>ERROR - Type error in explicitly typed binding
> *** Term           : distBetween
> *** Type           : Point -> Point -> Int
> *** Does not match : Point -> Point -> Float
>
> distBetween :: Point -> Point -> Int
> >>Instance of Floating Int required for definition of distBetween
>
> Any help is appreciated...
> Deech
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Newbie "Instance Of Floating Int" Error

Jared Updike
In reply to this post by Aditya Siram
It looks like somewhere else in your program (or a type signature
somewhere) is trying to force the result of sqrt to be an Int which
won't work since square roots are irrational (represented by the
computer as a Float or Double).

You might try (1) making sure the place where distBetween is used
isn't trying to use only Ints or Integers and (2) taking off explicit
type signatures and seeing if that works (the compiler can usually get
things working for you as long as you don't give it misinformation in
type signatures). Also, a more useful type of Point if you are taking
distances would be

type Point = (Float,Float)

instead (or you can leave that out because the function type for
distBetween will be inferred to be the "right thing", i.e.
(Float,Float) is already a valid type without needing to be named).

Hope that helps,
  Jared.
--
http://www.updike.org/~jared/
reverse ")-:"

On 4/27/06, Aditya Siram <[hidden email]> wrote:

> Hi all,
> I just started working with Haskell and am having some difficulties with its
> type system.
> Here is function that is supposed to calculate the distance between two
> coordinates:
> distBetween (x1,y1) (x2,y2) = sqrt((x2-x1)^2  + (y2-y1)^2)
>
> I am trying to explictly give it a type signature. Here is what I have tried
> and the errors generated by Hugs:
>
> type Point = (Int,Int)
> distBetween :: Point -> Point -> Float
> >>ERROR - Type error in explicitly typed binding
> *** Term           : distBetween
> *** Type           : Point -> Point -> Int
> *** Does not match : Point -> Point -> Float
>
> distBetween :: Point -> Point -> Int
> >>Instance of Floating Int required for definition of distBetween
>
> Any help is appreciated...
> Deech
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Newbie "Instance Of Floating Int" Error

Evan Martin
In reply to this post by Aditya Siram
On 4/28/06, Aditya Siram <[hidden email]> wrote:
> type Point = (Int,Int)
> distBetween :: Point -> Point -> Float
> >>ERROR - Type error in explicitly typed binding
> *** Term           : distBetween
> *** Type           : Point -> Point -> Int
> *** Does not match : Point -> Point -> Float
>
> distBetween :: Point -> Point -> Int
> >>Instance of Floating Int required for definition of distBetween

It's saying that you explicitly gave the function the type "Point ->
Point -> Int" but that it actually has "Point -> Point -> Int".

If you look at the type of sqrt:
  Prelude> :t sqrt
  sqrt :: (Floating a) => a -> a
You'll see that it returns a floating-point number.

(Also, a minor style point:  you should probably get in the habit of
putting a space between the "sqrt" and its arguments.  It'll make more
sense as you gain more experience.)
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Newbie "Instance Of Floating Int" Error

Aditya Siram
Thanks to all for their assistance....
By changing the output of distBetween to Float the function seemed to work.
And using the fromIntegral I was able to get my program, which accepts only
Ints, to work.

I was a little antsy about using fromIntegral because it isn't covered in my
text (Haskell:SOE) until later chapters. But I can find no other way

Deech


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe