2 - Expressions, types and values
Published online by Cambridge University Press: 05 November 2014
Summary
In Haskell every well-formed expression has, by definition, a well-formed type. Each well-formed expression has, by definition, a value. Given an expression for evaluation,
• GHCi checks that the expression is syntactically correct, that is, it conforms to the rules of syntax laid down by Haskell.
• If it is, GHCi infers a type for the expression, or checks that the type supplied by the programmer is correct.
• Provided the expression is well-typed, GHCi evaluates the expression by reducing it to its simplest possible form to produce a value. Provided the value is printable, GHCi then prints it at the terminal.
In this chapter we continue the study of Haskell by taking a closer look at these processes.
A session with GHCi
One way of finding out whether or not an expression is well-formed is of course to use GHCi. There is a command :type expr which, provided expr is well-formed, will return its type. Here is a session with GHCi (with some of GHCi's responses abbreviated):
ghci> 3 +4)
<interactive>:1:5: parse error on input ‵)'
GHCi is complaining that on line 1 the character ')' at position 5 is unexpected; in other words, the expression is not syntactically correct.
- Type
- Chapter
- Information
- Thinking Functionally with Haskell , pp. 22 - 48Publisher: Cambridge University PressPrint publication year: 2014