Understanding how the pure function is resolved in Haskell

107 views Asked by At

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.

1

There are 1 answers

1
chi On BEST ANSWER

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.