After playing around with haskell a bit I stumbled over this function:
Prelude Data.Maclaurin> :t ((+) . ($) . (+))
((+) . ($) . (+)) :: (Num a) => a -> (a -> a) -> a -> a
(Data.Maclaurin is exported by the package vector-space.) So it takes a Num, a function, another Num and ultimately returns a Num. What magic makes the following work?
Prelude Data.Maclaurin> ((+) . ($) . (+)) 1 2 3
6
2 is obviously not a function (a->a) or did I miss out on something?
The
Data.NumInstancesmodule of the same package defines aNuminstance for functions that return numbers:In Haskell an integer literal like
2is generic so that it can represent a number for any instance ofNum:To convert it to an actual number of the type required in a specific context,
fromIntegerfrom theNumclass is called.Since the helper module mentioned above defines an instance of
Numfor functions,2can now be converted to a function with thefromIntegermethod specified there.So ghci calls
fromInteger 2to get the function required as the second parameter of the construct in the question. The whole expression then happens to evaluate to6.