--- --- Calc

Project 2: Calculator

For this project, you will implement the guts of a (fancy) calculator. I have provided you with a simple read-eval-print interface (in CalcREPL.hs) that lets the user type in expressions to be evaluated. You will ultimately provide a function of type String -> String which accepts the user’s input and produces a response. Of course, your String -> String function should be decomposed into multiple phases, just like all of the language implementations we have been considering (such as parsing, pretty-printing, interpreting, and so on). This project intentionally gives you much less guidance than the first project in terms of what specific data types and functions you should write, and how to decompose your solution. However, you can of course use the modules we have done as templates to help guide you.

Getting started

Level 1

Your calculator must support the following features:

For example, a sample interaction with your calculator might look like this:

> 2+3
5
> (((3*5)   -   9)  + -8.3)
-2.3000000000000007
> 2 ^ 2 ^ 2 ^ 2
65536.0
> (3+3)*3
18.0
> 3+
(line 1, column 3):
unexpected end of input
expecting end of "+", number, or "("

Your calculator must never crash with a runtime error or pattern-match failure.

Get started by editing the starter code below and adding to it as appropriate!

General notes and hints

Starter code

{-# LANGUAGE GADTs #-}

module Calc where

import           Parsing2
import qualified Data.Map as M

Edit this description and replace it with your own! It gets printed when the calculator interface first starts up.

description :: String
description = unlines
  [ "Welcome to my calculator."
  , "This boring message is being shown because"
  , "I have not bothered to update it."
  , "Features this calculator supports: none."
  , "Type an expression, :help, or :quit."
  ]

Edit this help message and replace it with your own! It gets printed when the user types :help. Adding some well-chosen examples could be a good way to concisely show off the different features of your calculator.

helpMsg :: String
helpMsg = unlines
  [ "You can use integers or floating point values,"
  , "negation, or standard arithmetic operators + - * / ^ ."
  ]

This is the main function that is called by CalcREPL to evaluate user input.

calc :: String -> String
calc input = "Implement me!"

Level 2

To complete this project to Level 2, in addition to the requirements for Level 1:

Level 3

To complete this project to Level 3, in addition to the requirements for Level 2, you must complete at least two extensions. You may pick from the following list of suggested extensions (ordered roughly from easier to harder), or propose your own.

  1. Add support for the constants π and e, along with at least five functions such as sine, cosine, tangent, log, floor, ceiling, round, square root, or absolute value. For example, a sample interaction might look like this:

    > sin(pi/6)
    sin(π / 6.0)
      = 0.49999999999999994
    > cos(tan(log(abs(-2))))
    cos(tan(log(abs(-2.0))))
      = 0.6744026976311414
    > ((1 + sqrt(5))/2)^2 - 1
    ((1.0 + sqrt(5.0)) / 2.0) ^ 2.0 - 1.0
      = 1.618033988749895
  2. Support for complex numbers. For example, the user should be able to enter expressions like 3 + 2i. Note that real numbers should never be pretty-printed with an imaginary component, and purely imaginary numbers should not be pretty-printed with a real component. For example,

    > 2
    2.0
      = 2.0
    > 3i
    3.0i
      = 3.0i
    > i + 2
    i + 2.0
      = 2.0 + i
    > 2 + 3i
    2.0 + 3.0i
      = 2.0 + 3.0i
    > (2 + 3i) * (4 + 6i)
    (2.0 + 3.0i) * (4.0 + 6.0i)
      = -10.0 + 24.0i
    > sqrt(2 + 3i)
    sqrt(2.0 + 3.0i)
      = 1.6741492280355401 + 0.8959774761298381i

    (The last example works only if you have also implemented the first extension.)

    You can import the Complex.Double module to work with complex numbers in Haskell.

    Note there is a slight wrinkle to deal with when parsing a literal imaginary value: if you see a number you do not yet know whether it will be followed by i or not. The problem is that by default, if a parsec parser consumes some input before failing, it does not backtrack to try re-parsing the same input. So, as an example, something like this:

    Imag <$> (integer <* reserved "i") <|> Real <$> integer

    does not work, since if there is an integer not followed by an i, the first parser will irreversibly consume the integer before failing to find an i; when the second parser is tried there will no longer be an integer for it to find.

    The solution is that any parser which you would like to backtrack can be wrapped in the try function. So

    Imag <$> try (integer <* reserved "i") <|> Real <$> integer

    works as expected: if there is no i following an integer and the first parser fails, the input gets rewound to the beginning of the integer before trying the second parser.

  3. Support for units of measurement. Pick a domain (e.g. length, mass, time, …) and allow the user to add units in that domain to their calculations. For example (yours does not have to work exactly like this):

    > 1
    1.0
      = 1.0
    > 1 inch
    1.0 in
      = 1.0 in
    > 1 inch + 3 inches
    1.0 in + 3.0 in
      = 4.0 in
    > 1 meter + 1 inch
    1.0 m + 1.0 in
      = 1.0254 m
    > (1 meter + 1 inch) as inches
    (1.0 m + 1.0 in) as in
      = 40.370078740157474 in
    > ((1.6 ft * 700 + 8.1 ft) / 2) as miles
    ((1.6 ft * 700.0 + 8.1 ft) / 2.0) as mi
      = 0.10678412422360248 mi
    > 5 feet * 2 meters
    5.0 ft * 2.0 m
      = Error: tried to multiply two values with units, namely 5.0 ft and 2.0 m
    > 5 km + 6
    5.0 km + 6.0
      = Error: tried to add values with and without units, namely 5.0 km and 6.0
    > (5 km) mi
    5.0 km mi
      = Error: tried to apply units mi to a value that already had units km
    > (5 km) as mi
    5.0 km as mi
      = 3.105590062111801 mi
    > 6 as cm
    6.0 as cm
      = Error: can't convert scalar 6.0 to cm

    Some hints:

  4. Support for simple algebraic expressions involving polynomials. For example:

    > (x+1)^2
    (x + 1)^2
      = x^2 + 2*x + 1
    > (x+1)*(y-3)
    (x + 1) * (y - 3)
      = x * y - 3 * x + y - 3
    > (x^2 + 3*x + 1) / (x + 1)
      Sorry, division of polynomials is not supported.

    If you want to be really fancy you could support polynomial division too:

    > (x^2 + 3*x + 1) / (x + 1)
    (x^2 + 3 * x + 1) / (x + 1)
      = x + 2 - 1 / (x + 1)
  5. You should also feel free to propose your own extensions; just be sure to run them by me to make sure you choose something with an appropriate level of difficulty.