- <sect2>
- <title>Using <literal>do-</literal>notation at the prompt</title>
- <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
- <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
-
- <para>GHCi actually accepts <firstterm>statements</firstterm>
- rather than just expressions at the prompt. This means you can
- bind values and functions to names, and use them in future
- expressions or statements.</para>
-
- <para>The syntax of a statement accepted at the GHCi prompt is
- exactly the same as the syntax of a statement in a Haskell
- <literal>do</literal> expression. However, there's no monad
- overloading here: statements typed at the prompt must be in the
- <literal>IO</literal> monad.</para>
-
- <para>Here's an example:</para>
-<screen>
-Prelude> x <- return 42
-Prelude> print x
-42
-Prelude>
-</screen>
- <para>The statement <literal>x <- return 42</literal> means
- “execute <literal>return 42</literal> in the
- <literal>IO</literal> monad, and bind the result to
- <literal>x</literal>”. We can then use
- <literal>x</literal> in future statements, for example to print
- it as we did above.</para>
-
- <para>Of course, you can also bind normal non-IO expressions
- using the <literal>let</literal>-statement:</para>
-<screen>
-Prelude> let x = 42
-Prelude> print x
-42
-Prelude>
-</screen>
- <para>An important difference between the two types of binding
- is that the monadic bind (<literal>p <- e</literal>) is
- <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
- whereas with the <literal>let</literal> form, the expression
- isn't evaluated immediately:</para>
-<screen>
-Prelude> let x = error "help!"
-Prelude> print x
-*** Exception: help!
-Prelude>
-</screen>
- <para>Any exceptions raised during the evaluation or execution
- of the statement are caught and printed by the GHCi command line
- interface (for more information on exceptions, see the module
- <literal>Control.Exception</literal> in the libraries
- documentation).</para>
-
- <para>Every new binding shadows any existing bindings of the
- same name, including entities that are in scope in the current
- module context.</para>
-
- <para>WARNING: temporary bindings introduced at the prompt only
- last until the next <literal>:load</literal> or
- <literal>:reload</literal> command, at which time they will be
- simply lost. However, they do survive a change of context with
- <literal>:module</literal>: the temporary bindings just move to
- the new location.</para>
-
- <para>HINT: To get a list of the bindings currently in scope, use the
- <literal>:show bindings</literal> command:</para>
-
-<screen>
-Prelude> :show bindings
-x :: Int
-Prelude></screen>
-
- <para>HINT: if you turn on the <literal>+t</literal> option,
- GHCi will show the type of each variable bound by a statement.
- For example:</para>
- <indexterm><primary><literal>+t</literal></primary></indexterm>
-<screen>
-Prelude> :set +t
-Prelude> let (x:xs) = [1..]
-x :: Integer
-xs :: [Integer]
-</screen>
-
- </sect2>