Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Haskell type cl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 2793 of 2841
Post > Topic >>

Haskell type classes and ambiguous type variables

by Mike Burrell <mburrel@[EMAIL PROTECTED] > Mar 8, 2008 at 02:17 PM

I'm trying to make a function to convert an Integer to an Int and wish 
more safety than fromInteger supplies. Specifically, I want the 
function to fail if the Integer does not fit in an Int (sadly, 
fromInteger seems to quietly overflow).

My first attempt was:

> safeFromInteger :: (Integral a, Bounded a) => Integer -> a
> safeFromInteger x	| toInteger maxBound >= x = fromInteger x

Unfortunately, this leads to:

>     Ambiguous type variable `a' in the constraints:
>       `Integral a'
>         arising from a use of `toInteger' at xxx.lhs:19:20-37
>       `Bounded a'
>         arising from a use of `maxBound' at xxx.lhs:19:30-37
>     Probable fix: add a type signature that fixes these type variable(s)

I thought maybe the problem was that my constraint didn't offer enough 
constraining, and realistically I was only interested in Ints, so I 
changed it to:

> safeFromInteger :: Integer -> Int
> safeFromInteger x	| toInteger maxBound >= x = fromInteger x

Unfortunately this leads to exactly the same error, so the problem 
doesn't seem to be with the constraints.

Can anyone give some insight on how I need to structure this to get it 
to type correctly?

Much thanks,
Mike




 2 Posts in Topic:
Haskell type classes and ambiguous type variables
Mike Burrell <mburrel@  2008-03-08 14:17:01 
Re: Haskell type classes and ambiguous type variables
Ben Bacarisse <ben.use  2008-03-08 16:24:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 21:39:25 CDT 2008.