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.
Asked
Active
Viewed 103 times
1

milad zahedi
- 787
- 6
- 21
-
1http://stackoverflow.com/a/28979021/3608068 – user3608068 Dec 29 '16 at 12:53
-
thank you @user3608068. The link you provided exactly answers my question but I was not able to find it when I searched for it. – milad zahedi Dec 31 '16 at 06:01
1 Answers
6
GHCi performs some magic to be user-friendly.
When entering an expression whose type is of the form ... => f a
, it tries to instantiate f
to IO
. In your case, this is possible since IO
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 class Show
, the result is printed. In your case "aa"
is the result (and the type a
is String
), so GHCi prints that.

chi
- 111,837
- 3
- 133
- 218