<A HREF="manual_contents.html"><img align=center src="contents.gif" ALT="Contents"></A> Up Previous Next

Evaluate expression <expr>


To evaluate an expression, the user simply enters it at the Hugs prompt. This is treated as a special case, without the leading colon that is required for other commands. The expression must fit on a single line; there is no way to continue an expression onto the next line of input to the interpreter. The actual behaviour of the evaluator depends on the type of <expr>: The interpreter will not evaluate an expression that contains a syntax error, a type error, or a reference to an undefined variable:

 Prelude> sum [1..)

 ERROR: Syntax error in expression (unexpected `)')

 Prelude> sum 'a'

 ERROR: Type error in application

 *** expression     : sum 'a'

 *** term           : 'a'

 *** type           : Char

 *** does not match : [a]

 Prelude> sum [1..n]

 ERROR: Undefined variable "n"

 Prelude> 

Another common problem occurs if there is no show function for the expression entered---that is, if its type is not an instance of the Show class. For example, suppose that a module defines a type T without a Show instance:

 module Test where

 data T = A | B

With just these definitions, any attempt to evaluate an expression of type T will cause an error:

 Test> A

 ERROR: Cannot find "show" function for:

 *** expression : A

 *** of type    : T

 Test> 

To avoid problems like this, you will need to add an instance of the Show class to your program. One of the simplest ways to do that is to request a derived instance of Show as part of the datatype definition, as in:

 module Test where

 data T = A | B  deriving Show

Once this has been loaded, Hugs will evaluate and display values of type T:

 Test> A

 A

 Test> take 5 (cycle [A,B])

 [A, B, A, B, A]

 Test>

Values in the IO monad are only treated as programs if they return (). For example, getChar has type IO Char, so it is printed using show:

 Prelude> getChar

 <>

 Prelude>

Hugs will not execute this expression as a program because it does not specify what should be done with the character returned by getChar. If you want to run getChar as a program and, for example, discard its result, then you must do this explicitly:

 Prelude> do getChar; return ()

 w

 Prelude>

You should also note that the behaviour of the evaluator can be changed while the interpreter is running by using the :set command to modify option settings.