I am reading the excellent article Understanding map and apply by Scott Wlaschin and running some Haskell code to understand the concepts (Functor
, Applicative
, ...). I stumbled upon a behaviour I do not understand.
Why evaluating pure add1
prints nothing ? What is the value of the evaluated expression ? Why pure add1 "abc"
gives me back the function add1
?
I understand that pure
lifts a value into the elevated world (so called in the article). Since I do not provide a concrete lifted value somewhere or enough type information, the type constraint is general and stays Applicative f
. Thus I understand the type of pure add1
. But the rest of what's happening here eludes me.
$ stack ghci
GHCi, version 8.8.2
λ: add1 :: Int -> Int ; add1 x = x + 1
λ: :t add1
add1 :: Int -> Int
λ: add1 100
101
λ: :t pure
pure :: Applicative f => a -> f a
λ: pure add1
λ: :t pure add1
pure add1 :: Applicative f => f (Int -> Int)
λ: pure add1 "abc"
<interactive>:8:1: error:
• No instance for (Show (Int -> Int)) arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
λ: :t pure add1 "abc"
pure add1 "abc" :: Int -> Int
λ: pure add1 "abc" 100
101
EDIT I think the two comments by @chi and the answer by @sarah answers the question because it shows the applicative chosen by GHCi to evaluate the expression and that explains the observed behaviour.