In ghci:
λ> :t (pure 1)
(pure 1) :: (Applicative f, Num a) => f a
λ> show (pure 1)
<interactive>:1:1:
No instance for (Show (f0 a0))
arising from a use of `show'
Possible fix: add an instance declaration for (Show (f0 a0))
In the expression: show (pure 1)
In an equation for `it': it = show (pure 1)
λ> pure 1
1
Does this mean that ghci execute Applicative and displays the result, just like IO?
Note that pure () and pure (+1) don’t print anything.
You get the same behaviour if you use
returninstead ofpure. To find out what to do, ghci must choose a type for the given expression. ghci’s defaulting rules are such that absent other constraints, it choosesIOfor anApplicativeorMonadinstance. Thus it interpretspure 1as an expression of typeIO Integer. Expressions of typeIO aentered at the prompt are executed and their results are printed, if 1.ahas aShowinstance and 2.ais not(). Thus enteringpure 1at the prompt results inbeing executed (and the magic variable
itbound to the returnedv). Forpure (), the special case applies since()is considered uninteresting, thus onlyreturn ()is executed anditbound to(), forpure (+1), a function is returned, there’s noShowinstance for functions in scope, so nothing is printed. However,with a
Showinstance for functions in scope, it gets printed (not that it’s informative), and the function can then be used (the latter is independent of aShowinstance being in scope, of course).