Search code examples
haskellghciapplicative

Understanding how the pure function is resolved in Haskell


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.


Solution

  • 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.