A signature of the type (Eq a, Integral c) => [a] -> [(c, a)] means that the function works for any types a and c in the corresponding classes. The actual type is used on the call site.
As a simple example, consider the type of an empty list:
:t [] [a]
This means that [] represents an empty String list and an empty Int list, an empty Maybe [Maybe Bool] list, and any other types that you can imagine. We can imagine how to wrap this in a normal identifier:
empty :: [a] empty = []
empty obviously works just like [] . Thus, you can see that the following definition does not make sense:
empty :: [a] empty = [True]
after all, [True] never be [Int] or [String] or any other empty list you want.
The idea here is the same, except that I have restrictions on the class and variables. For example, you can use encode to return a list of [(Integer, String)] , since Integer also in the Integral class.
So, you have to return something polymorphic, which can be any Integral - just what fromIntegral does. If you just returned Int , encode will only be used as Int , not Integral .
source share