When I come across a function, is there a general way to determine if it's completely stand-alone or a part of a type class? For instance:
fromIntegral :: (Integral a, Num b) => a -> b
This is the method I have developed to find the answer:
:info
on all the listed class constraints, in this case :info Integral
and :info Num
.forIntegral
).Is my method sound? Does it work in general?
You can do :info
on the function directly.
Prelude> :info fromInteger
class Num a where
...
fromInteger :: Integer -> a
-- Defined in `GHC.Num'
Prelude> :info fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
-- Defined in `GHC.Real'
As you see, fromInteger
belongs to the Num
typeclass, while fromIntegral
does not.
There's an easier way, compare the difference in output for :info fromIntegral
and :info fromInteger
:
> :info fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
-- Defined in `GHC.Real'
> :info fromInteger
class Num a where
...
fromInteger :: Integer -> a
-- Defined in `GHC.Num'
See how fromInteger
is specified as part of a type class, but fromIntegral
is not? That's how you can tell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With