In GHCi when I type pure 2
it returns 2
; or pure "aa"
returns "aa"
. I wonder how this applicative instance is resolved for 2 or "aa" by GHCi.
Understanding how the pure function is resolved in Haskell
164 views Asked by milad zahedi At
1
GHCi performs some magic to be user-friendly.
When entering an expression whose type is of the form
... => f a
, it tries to instantiatef
toIO
. In your case, this is possible sinceIO
is an applicative (and a monad).Secondly, when an expression having a type of the form
... => IO a
is entered, it is run as an IO action.Finally, if
a
is of classShow
, the result is printed. In your case"aa"
is the result (and the typea
isString
), so GHCi prints that.