next up previous contents
Next: Implementation Tools Up: Implementation Previous: Haskell:

User Interface

The user interface is implemented entirely in Haskell. Although a functional language is perhaps not the most obvious choice for implementing the user interface, techniques and tools exist which make the task not only possible, but even relatively straightforward. The advantage of implementing entirely in one language is that we do not need to worry about interfacing with another. This would be generally complicated and make testing with the user interface harder, especially because it would prevent us using a Haskell interpreter. As we only require a simple user interface for demonstration purposes, using another language to implement it is an entirely unnecessary complication.

The user interface operates by using Haskell I/O facilities to obtain a string. A lexer, which is automatically generated using the tool ``Alex'' (see section 6.2.3), is used to convert the string into a list of tokens. Next a parser, which is automatically generated using the tool ``Happy'' (see section 6.2.3) converts the list of tokens into an expression tree. Because Haskell is a pure functional language, we require the use of Monads [32] to perform error checking and to simulate exceptions.

The calculator itself takes the expression tree and uses this to generate a function which will return the desired result in signed binary (mantissa, exponent). Conversions from input strings of decimal numbers are performed to generate streams representing constants in the expression, and a simple database is accessed to obtain expressions which are substituted for the variables in the expression. Finally at the top level this function is evaluated to an appropriate precision, its result converted into signed decimal and decimal to an appropriate precision, and the output from this displayed to the user.

In some cases, such as with division or exponentiation by an integer, there are two possible algorithms that could be used. Either we treat the integer constant as an integer and use the simpler algorithm, or compute it using the full division algorithm. In this implementation we have adopted the more general (but slower) approach. Performance of the calculator as accessed through the user interface could be improved my making the parser slightly more sophisticated and making these and similar optimisations.


next up previous contents
Next: Implementation Tools Up: Implementation Previous: Haskell:
Martin Escardo
5/11/2000