[Haskell-beginners] Defaulting the following constraint ....

Benjamin Jones benjaminfjones at gmail.com
Mon Sep 30 22:54:03 CEST 2013


On Mon, Sep 30, 2013 at 1:06 PM, Alan Buxton <alanbuxton at gmail.com> wrote:

> Hi****
>
> ** **
>
> I have something like this – purpose is that I need to drop redundant .0
> in a whole number – so to show 1.2345 as 1.2345 and to show 1.0 as 1****
>
> ** **
>
> *module Test where*
>
> * *
>
> *niceShow x = if (isInt x) then show (floor x) else show x*
>
> * *
>
> *isInt x = x == fromInteger (floor x)*****
>
> **
>
> But the hlint in my vim plugin keeps warning me that****
>
> ** **
>
> test.hs|3 col 38 warning| Defaulting the following constraint(s) to type
> `Integer'****
>
> ||            (Integral a0) arising from a use of `floor' at
> /tmp/test.hs:3:38-42****
>
> ||            (Show a0) arising from a use of `show' at
> /tmp/test.hs:3:32-35****
>
> || In the first argument of `show', namely `(floor x)'****
>
> || In the expression: show (floor x)****
>
> || In the expression: if (isInt x) then show (floor x) else show x****
>
> ** **
>
> What does this mean? And what would I need to do in order to prevent this
> warning?****
>
>
It looks like you have a type mismatch in `isInt`, floor returns a type in
the typeclass Integral (this may or may not be an Integer) but fromInteger
demands its input be an Integer. Here are the type sigs:

ghci> :t floor
floor :: (Integral b, RealFrac a) => a -> b
ghci> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b

Changing `fromInteger` to `fromIntegeral` should fix this problem.

It will also help if you write down type signatures explicitly for your two
functions.

--
Benjamin Jones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130930/4a0a7858/attachment.htm>


More information about the Beginners mailing list