Git Product home page Git Product logo

haskell-calculator's Introduction

Haskell Calculator

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.

Demonstration

Here's a web-based demonstration built with GHCJS and Reflex.

Implementation

The implementation includes:

Also included:

  • A simple demonstration of using a GADT to restrict the shape of a tree-based data structure (the Exp type).

Building

This project supports both GHC and GHCJS.

The dependencies for GHC and GHCJS are slightly different.

Building with GHC

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

Running the command line interface

The command line interface is built with GHC.

To run it, issue the following command:

stack exec calculator-cli

Building the web-based graphical user interface

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.

Running the test suite

The test suite is built with GHC.

To run the suite, issue the following command:

stack test

Experimenting with the calculator library

To load the calculator library into GHCI, issue the following command:

stack ghci

haskell-calculator's People

Contributors

jonathanknowles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

cvnb sztwiorok

haskell-calculator's Issues

Add a "random" button.

The button, when pressed, should populate the input box with a random arithmetic expression. (Similar to the "I'm feeling lucky!" button.)

Offer a colour scheme that more clearly demonstrates the level of nesting.

The current colour scheme nests expressions with colours according to the expression type.

  • additions are represented with the colour red
  • multiplications are represented with the colour blue
  • subtractions are represented with the colour green
  • divisions are represented with the colour yellow

Consider adding a colour scheme that depends purely on the level of nesting, so that it's easier to comprehend deeply-nested expressions, regardless of the binary operators in use.

`stack setup` fails

I'm on macOS Sierra, if it matters

$ stack setup
...
  --  While building package ghci-8.0.1 using:
      /Users/chen/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
    Logs have been written to: /Users/chen/.stack/programs/x86_64-osx/ghcjs-0.2.1.9007001_ghc-8.0.1/src/.stack-work/logs/ghci-8.0.1.log

    Configuring ghci-8.0.1...
    Warning: This package indirectly depends on multiple versions of the same
    package. This is highly likely to cause a compile failure.
    package ghc-boot-8.0.1 requires ghc-boot-th-8.0.1
    package template-haskell-2.11.1.0 requires ghc-boot-th-8.0.2
    Building ghci-8.0.1...
    Preprocessing library ghci-8.0.1...
    [ 1 of 13] Compiling SizedSeq         ( SizedSeq.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/SizedSeq.o )
    [ 2 of 13] Compiling GHCi.TH.Binary   ( GHCi/TH/Binary.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/TH/Binary.o )
    [ 3 of 13] Compiling GHCi.Signals     ( GHCi/Signals.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/Signals.o )
    [ 4 of 13] Compiling GHCi.RemoteTypes ( GHCi/RemoteTypes.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/RemoteTypes.o )
    [ 5 of 13] Compiling GHCi.ObjLink     ( GHCi/ObjLink.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/ObjLink.o )
    [ 6 of 13] Compiling GHCi.InfoTable   ( .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/InfoTable.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/InfoTable.o )
    [ 7 of 13] Compiling GHCi.FFI         ( .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/FFI.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/FFI.o )
    [ 8 of 13] Compiling GHCi.BreakArray  ( GHCi/BreakArray.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/BreakArray.o )
    [ 9 of 13] Compiling GHCi.Message     ( GHCi/Message.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/Message.o )
    [10 of 13] Compiling GHCi.TH          ( GHCi/TH.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/GHCi/TH.o )

    /private/var/folders/xz/z3wrll111zn03hdhkd_pxs5w0000gp/T/stack37990/ghci-8.0.1/GHCi/TH.hs:127:42: error:
        • Couldn't match expected type ‘ghc-boot-th-8.0.1:GHC.LanguageExtensions.Type.Extension’
                      with actual type ‘TH.Extension’
          NB: ‘TH.Extension’
                is defined in ‘GHC.LanguageExtensions.Type’
                    in package ‘ghc-boot-th-8.0.2’
              ‘ghc-boot-th-8.0.1:GHC.LanguageExtensions.Type.Extension’
                is defined in ‘GHC.LanguageExtensions.Type’
                    in package ‘ghc-boot-th-8.0.1’
        • In the first argument of ‘IsExtEnabled’, namely ‘x’
          In the first argument of ‘ghcCmd’, namely ‘(IsExtEnabled x)’
          In the expression: ghcCmd (IsExtEnabled x)

Document that this project requires Linux

It turns out that I can't build on macOS because I can't find webkitgtk there. Homebrew used to provide it but deleted it because of build problems, apparently.

    Configuring webkitgtk3-0.14.2.1...
    setup: The pkg-config package 'webkitgtk-3.0' version >=1.8 is required but it
    could not be found.

"stack test" fails during compilation of hlint

Logs:

$ stack test
hlint-1.9.35: configure
hlint-1.9.35: build
Progress: 1/7
--  While building package hlint-1.9.35 using:
      /home/jsk/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.24.0.0-ghcjs-0.2.1.9007001_ghc-8.0.1 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs build --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
    Logs have been written to: /private/jsk/projects/current/haskell-calculator/.stack-work/logs/hlint-1.9.35.log

    Configuring hlint-1.9.35...
    Building hlint-1.9.35...
    Preprocessing library hlint-1.9.35...
    [ 1 of 46] Compiling Test.Util        ( src/Test/Util.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/Test/Util.js_o )
    [ 2 of 46] Compiling Test.InputOutput ( src/Test/InputOutput.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/Test/InputOutput.js_o )
    [ 3 of 46] Compiling HSE.Type         ( src/HSE/Type.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/HSE/Type.js_o )
    [ 4 of 46] Compiling HSE.FreeVars     ( src/HSE/FreeVars.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/HSE/FreeVars.js_o )
    [ 5 of 46] Compiling HSE.Util         ( src/HSE/Util.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/HSE/Util.js_o )
    [ 6 of 46] Compiling HSE.Match        ( src/HSE/Match.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/HSE/Match.js_o )
    [ 7 of 46] Compiling HSE.Scope        ( src/HSE/Scope.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/HSE/Scope.js_o )
    [ 8 of 46] Compiling HsColour         ( src/HsColour.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/HsColour.js_o )
    [ 9 of 46] Compiling Parallel         ( src/Parallel.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/Parallel.js_o )
    [10 of 46] Compiling Paths_hlint      ( .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/autogen/Paths_hlint.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/Paths_hlint.js_o )
    [11 of 46] Compiling Util             ( src/Util.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/Util.js_o )
    [12 of 46] Compiling CmdLine          ( src/CmdLine.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0_ghcjs/build/CmdLine.js_o )

    /tmp/stack5931/hlint-1.9.35/src/CmdLine.hs:69:12: error:
        Ambiguous occurrence ‘isWindows’
        It could refer to either ‘System.FilePath.isWindows’,
                                 imported from ‘System.FilePath’ at src/CmdLine.hs:13:1-22
                                 (and originally defined in ‘System.FilePath.Current’)
                              or ‘System.Info.Extra.isWindows’,
                                 imported from ‘System.Info.Extra’ at src/CmdLine.hs:20:1-24

This issue probably shares a root cause with ghcjs/ghcjs-boot#38.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.