[[project @ 2005-05-20 12:28:14 by simonpj]
simonpj**20050520122814
Documentation for new GHCi behaviour
] {
hunk ./ghc/docs/users_guide/ghci.xml 364
- evaluates and prints the result. But that's not the whole story:
- if you type something of type IO a for some
- a, then GHCi executes it
- as an IO-computation, and doesn't attempt to print the
- result:.
+ evaluates and prints the result:
+
+Prelude> reverse "hello"
+"olleh"
+Prelude> 5+5
+10
+
+
+
+I/O actions at the prompt
hunk ./ghc/docs/users_guide/ghci.xml 375
+GHCi does more than simple expression evaluation at the prompt.
+If you type something of type IO a for some
+ a, then GHCi executes it
+ as an IO-computation.
hunk ./ghc/docs/users_guide/ghci.xml 385
-
- What actually happens is that GHCi typechecks the
- expression, and if it doesn't have an IO type,
- then it transforms it as follows: an expression
- e turns into
-
- let it = e;
- print it
+Furthermore, GHCi will print the result of the I/O action if (and only
+if):
+
+ The result type is an instance of Show.
+ The result type is not
+ ().
+
+For example, remembering that putStrLn :: String -> IO ():
+
+Prelude> putStrLn "hello"
+hello
+Prelude> do { putStrLn "hello"; return "yes" }
+hello
+"yes"
hunk ./ghc/docs/users_guide/ghci.xml 400
- which is then run as an IO-action.
+
hunk ./ghc/docs/users_guide/ghci.xml 402
- Hence, the original expression must have a type which is an
- instance of the Show class, or GHCi will
- complain:
+
+ Using do-notation at the prompt
+ do-notationin GHCi
+ statementsin GHCi
+
+ GHCi actually accepts statements
+ 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.
hunk ./ghc/docs/users_guide/ghci.xml 412
+ The syntax of a statement accepted at the GHCi prompt is
+ exactly the same as the syntax of a statement in a Haskell
+ do expression. However, there's no monad
+ overloading here: statements typed at the prompt must be in the
+ IO monad.
hunk ./ghc/docs/users_guide/ghci.xml 418
-Prelude> id
-No instance for `Show (a -> a)'
-arising from use of `print'
-in a `do' expression pattern binding: print it
+Prelude> x <- return 42
+42
+Prelude> print x
+42
+Prelude>
hunk ./ghc/docs/users_guide/ghci.xml 424
+ The statement x <- return 42 means
+ “execute return 42 in the
+ IO monad, and bind the result to
+ x”. We can then use
+ x in future statements, for example to print
+ it as we did above.
hunk ./ghc/docs/users_guide/ghci.xml 431
- The error message contains some clues as to the
- transformation happening internally.
+ Of course, you can also bind normal non-IO expressions
+ using the let-statement:
+
+Prelude> let x = 42
+42
+Prelude> print x
+42
+Prelude>
+
+ An important difference between the two types of binding
+ is that the monadic bind (p <- e) is
+ strict (it evaluates e),
+ whereas with the let form, the expression
+ isn't evaluated immediately:
+
+Prelude> let x = error "help!"
+Prelude> print x
+*** Exception: help!
+Prelude>
+
+ 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
+ Control.Exception in the libraries
+ documentation).
+
+GHCi will print the result of a let or
+x<-e statement if and only if:
+
+The statement binds exactly one variable
+The variable's type is not polymorphic, is not
+(),
+and is an instance of Show
+
+
+
+ Every new binding shadows any existing bindings of the
+ same name, including entities that are in scope in the current
+ module context.
+
+ WARNING: temporary bindings introduced at the prompt only
+ last until the next :load or
+ :reload command, at which time they will be
+ simply lost. However, they do survive a change of context with
+ :module: the temporary bindings just move to
+ the new location.
+
+ HINT: To get a list of the bindings currently in scope, use the
+ :show bindings command:
+
+
+Prelude> :show bindings
+x :: Int
+Prelude>
+
+ HINT: if you turn on the +t option,
+ GHCi will show the type of each variable bound by a statement.
+ For example:
+ +t
+
+Prelude> :set +t
+Prelude> let (x:xs) = [1..]
+x :: Integer
+xs :: [Integer]
+
+
+
hunk ./ghc/docs/users_guide/ghci.xml 606
-
- Using do-notation at the prompt
- do-notationin GHCi
- statementsin GHCi
-
- GHCi actually accepts statements
- 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.
-
- The syntax of a statement accepted at the GHCi prompt is
- exactly the same as the syntax of a statement in a Haskell
- do expression. However, there's no monad
- overloading here: statements typed at the prompt must be in the
- IO monad.
-
- Here's an example:
-
-Prelude> x <- return 42
-Prelude> print x
-42
-Prelude>
-
- The statement x <- return 42 means
- “execute return 42 in the
- IO monad, and bind the result to
- x”. We can then use
- x in future statements, for example to print
- it as we did above.
-
- Of course, you can also bind normal non-IO expressions
- using the let-statement:
-
-Prelude> let x = 42
-Prelude> print x
-42
-Prelude>
-
- An important difference between the two types of binding
- is that the monadic bind (p <- e) is
- strict (it evaluates e),
- whereas with the let form, the expression
- isn't evaluated immediately:
-
-Prelude> let x = error "help!"
-Prelude> print x
-*** Exception: help!
-Prelude>
-
- 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
- Control.Exception in the libraries
- documentation).
-
- Every new binding shadows any existing bindings of the
- same name, including entities that are in scope in the current
- module context.
-
- WARNING: temporary bindings introduced at the prompt only
- last until the next :load or
- :reload command, at which time they will be
- simply lost. However, they do survive a change of context with
- :module: the temporary bindings just move to
- the new location.
-
- HINT: To get a list of the bindings currently in scope, use the
- :show bindings command:
-
-
-Prelude> :show bindings
-x :: Int
-Prelude>
-
- HINT: if you turn on the +t option,
- GHCi will show the type of each variable bound by a statement.
- For example:
- +t
-
-Prelude> :set +t
-Prelude> let (x:xs) = [1..]
-x :: Integer
-xs :: [Integer]
-
-
-
hunk ./ghc/docs/users_guide/ghci.xml 621
-
- This is a result of the translation mentioned earlier,
- namely that an expression e is
- translated to
+ What actually happens is that GHCi typechecks the
+ expression, and if it doesn't have an IO type,
+ then it transforms it as follows: an expression
+ e turns into
hunk ./ghc/docs/users_guide/ghci.xml 629
- before execution, resulting in a binding for
- it.
+ which is then run as an IO-action.
+
+ Hence, the original expression must have a type which is an
+ instance of the Show class, or GHCi will
+ complain:
+
+
+Prelude> id
+No instance for `Show (a -> a)'
+arising from use of `print'
+in a `do' expression pattern binding: print it
+
+
+ The error message contains some clues as to the
+ transformation happening internally.
hunk ./ghc/docs/users_guide/ghci.xml 645
- If the expression was of type IO a for
+ If the expression was instead of type IO a for
hunk ./ghc/docs/users_guide/ghci.xml 687
- rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows. If the expression yields a set of
- type constraints that are all from standard classes (Num, Eq etc.),
- and at least one is either a numeric class or the Show,
- Eq, or Ord class,
- GHCi will try to use one of the default types, just as described in the Report.
- The standard defaulting rules require that one of the classes is numeric; the difference here
- is that defaulting is also triggered at least one is Show,
- Eq, or Ord.
+ rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows. The
+ standard rules take each group of constraints (C1 a, C2 a, ..., Cn
+ a) for each type variable a, and defaults the
+ type variable if
+
+ The type variable a
+ appears in no other constraints
+ All the classes Ci are standard.
+ At least one of the classes Ci is
+ numeric.
+
+ At the GHCi prompt, the second and third rules are relaxed as follows
+ (differences italicised):
+
+ Any of the classes Ci is standard.
+ At least one of the classes Ci is
+ numeric, or is Show,
+ Eq, or Ord.
+
}