A simple four-function visual calculator, implemented in Haskell.
This calculator is capable of evaluating (and visualizing) arithmetic expressions such as "1+1
" or "(1+2)*(3+4)
".
It supports:
- natural numbers (of arbitrary size);
- addition, subtraction, multiplication, and division;
- sub-expressions nested within parentheses;
- unary negation.
Here's a web-based demonstration built with GHCJS and Reflex.
The implementation includes:
- Data structures for encoding arithmetic expressions as trees (see
Calculator.Types
) - Functions for parsing arithmetic expressions from textual input (see
Calculator.Parsing
) - Functions for pretty-printing arithmetic expressions (see
Calculator.Printing
) - Functions for evaluating arithmetic expressions (see
Calculator.Evaluation
) - A simple command-line interface (see
Calculator.CLI
) - A simple graphical user interface (see
Calculator.GUI
)
Also included:
- A simple demonstration of using a GADT to restrict the shape of a tree-based data structure (the
Exp
type).
This project supports both GHC and GHCJS.
The dependencies for GHC and GHCJS are slightly different.
On Debian-based systems, first install the following dependencies:
libcairo-dev
libghc-pango-dev
libghc-gtk3-dev
libwebkitgtk-3.0-dev
Then issue the following commands:
stack setup
stack build
The command line interface is built with GHC.
To run it, issue the following command:
stack exec calculator-cli
The web interface is built with GHCJS.
First ensure that you have a recent version of NodeJS installed. (Version 6.6.0 or later should work.)
Then run:
./build-web-gui.sh
If the build is successful, it will notify you of where you can find the build output. For example:
Open the following file with your web browser to view the graphical user interface:
/path/to/haskell/calculator/.stack-work/install/x86_64-linux/lts-7.15/ghcjs-0.2.1.9007015_ghc-8.0.1/bin/calculator-gui.jsexe/index.html
Copy and paste the final line into your web browser to view the user interface.
The test suite is built with GHC.
To run the suite, issue the following command:
stack test
To load the calculator library into GHCI, issue the following command:
stack ghci