Determining whether a function is autonomous or part of a type class

When I come across a function, is there a general way to determine if it is completely autonomous or is part of a type class? For instance:

fromIntegral :: (Integral a, Num b) => a -> b 

This is the method I developed to find the answer:

  • Go to GHCi and execute :info in all the class restrictions listed, in this case :info Integral and :info Num .
  • Check if any of these are specified.
  • If so, this is part of this type. If this is not the case, it is a standalone function (which is the case for forIntegral ).

Does my method sound? Does it work at all?

+6
source share
2 answers

You can directly execute :info function.

 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 can see, fromInteger belongs to the Num class, and fromIntegral does not.

+12
source

It’s easier there, 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 listed as part of a type class, but fromIntegral not? This is how you can tell.

+7
source

All Articles