The pure function returns a polymorphic value:
Prelude> :t pure "foo" pure "foo" :: Applicative f => f [Char]
For Maybe , pure is defined as Just :
instance Applicative Maybe where pure = Just -- ...
Other types provide different definitions; as an example, it is defined for lists as
instance Applicative [] where pure x = [x] -- ...
Specifying the return type indicates Haskell, for the Applicative instance, to define pure .
Prelude> pure "foo" :: [[Char]] ["foo"] Prelude> pure "foo" :: Maybe [Char] Just "foo"
Besides providing an explicit type, Haskell can infer which type to use the base to use the value. For example, (<*> [1..5]) :: (Num a, Enum a) => [a -> b] -> [b] , therefore in pure (+3) <*> [1..5] we know that pure (+3) must be of type [a -> b] . Similarly, in pure (+3) <*> Just 5 we know that pure (+3) must be of type Maybe (a->b) .
In general, in any expression pure f <*> g type g will determine the return type of pure f .
source share