What type is selected for polymorphic expression when printing?

What is the type return "abc"when printing in ghci?

The point of the question is that it is polymorphic in the monad:

ghci> :t return "abc"
return "abc" :: (Monad m) => m [Char]

and what is printed depends on which monad is selected:

ghci> return "abc" :: Maybe String
Just "abc"

ghci> return "abc" :: [] String
["abc"]

but here, what is actually printed:

ghci> return "abc"
"abc"
+5
source share
2 answers

When you enter an expression exprin GHCi, the following things happen:

  • The expression is checked by type. If there is an error, GHCi informs you of the error and refuses.
  • Otherwise, let's say it exprhas a type t; GHC is trying to match twith IO a.
  • , - it <- expr, a Show (), print it.
  • , t Show, GHCi - let it = expr, print it.
  • .

, GHCi IO , , , . GHCi , : , IO, GHCi , , , (.. ()), . , , , , IO; , <-. , , IO, GHCi , , GHCi ( ), .

return "abc" typechecks IO String, String - Show, GHCi -

it <- return "abc"
print it

,

print "abc"

, .

+7

Haskell , ; . . GHCi, e, :

  • e IO T T, T Show, GHCi T. , .

  • e * * IO, , GHCi . Show, GHCi Show e. , : Maybe String [String] - Show.

    e Show, GHCi . , flip take.

+4

All Articles