Yeah, this is a not super-well documented corner of ghci. When you enter an expression into ghci, it uses the expression's type to decide what to do:
IO ()
: Run the action, do nothing further.
Show a => IO a
: Run the action and print
the result.
- any other
IO a
: Run the action, do nothing further.
- anything else: wrap the whole expression with
print
.
How does it decide which of these types a thing has? Easy: it tries to unify the type of your expression which each of the above signatures in turn and solve all resulting constraints. (For the cognoscenti: this is in addition to the extended defaulting rules! This explains why it appears to be defaulting the m
, even though neither the standard defaulting rules nor the extended defaulting rules say what default to use.)
So, since your expression does not unify with IO ()
but does unify with Show a => IO a
, ghci finds m ~ IO
(and a ~ Int
) during unification, discovers that there is a MonadPlus IO
(and a Show Int
) instance to resolve the constraints, runs your action, and prints the result.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…