Sign inSign up

irfanuruchi/integral-calculator

By irfanuruchi

Updated 3 months ago

Mini CAS in Haskell with symbolic calculus, numeric integration, and an interactive web interface.

Image
Integration & delivery
Developer tools
Content management system
0

1.3K

irfanuruchi/integral-calculator repository overview

Integral Calculator

A small Computer Algebra System written in Haskell.

This project parses mathematical expressions, simplifies them, differentiates them symbolically, computes symbolic and numeric integrals, evaluates expressions safely, and visualizes the results through a minimal web interface.

It was built for the Logical & Functional Programming course, but designed as a real, self-contained system rather than a throwaway assignment.


What it does

  • Parses mathematical expressions (including implicit multiplication and nested functions)
  • Represents expressions as a symbolic AST
  • Simplifies expressions using rule-based rewrites
  • Computes symbolic derivatives using standard calculus rules
  • Computes symbolic antiderivatives for supported patterns
  • Performs numeric integration using adaptive Simpson’s rule
  • Evaluates expressions safely for plotting and numeric checks
  • Renders symbolic output using LaTeX (via MathJax)
  • Plots functions and derivatives interactively in the browser
  • Optionally generates short natural-language explanations using an LLM

All actual computation is done locally by the Haskell engine.
The LLM is only used to explain results, never to compute them.


Structure

The system is built around a single expression type, which keeps everything predictable and easy to follow.

Main components:

  • Expr – symbolic expression type
  • Parse – hand-written recursive-descent parser
  • Simplify – deterministic simplification rules
  • Differentiate – symbolic differentiation engine
  • Integrate – symbolic antiderivatives and numeric integration
  • Eval – safe numeric evaluation
  • Pretty – LaTeX-style pretty printing
  • LLM – optional explanation layer (no computation)
  • Main – web server, visualization, and glue logic

Each module has a single responsibility and operates directly on the expression tree.


Web interface

The application runs as a small web service. You can:

  • enter an expression
  • see the simplified form
  • view the symbolic derivative
  • compute symbolic and numeric integrals
  • plot the function and its derivative
  • adjust bounds interactively

The interface is intentionally minimal and focuses on clarity rather than visual complexity.


LLM explanation layer

The project includes a small, tightly constrained LLM module that generates short explanations of results in plain English.

Important constraints:

  • it only receives results that were already computed
  • it never evaluates expressions
  • it never differentiates or integrates
  • it cannot modify or correct output

This keeps the system deterministic while still providing a readable explanation when desired(you need a CEREBAS API key for it).


A prebuilt multi-architecture Docker image is available.

docker run --rm -p 3000:3000 irfanuruchi/integral-calculator:latest

Then open:

http://localhost:3000

The image supports both linux/amd64 and linux/arm64, so the same command works on Windows, Linux, and macOS (including Apple Silicon).

Available tags
  • latest
  • v1
  • 0.1.0

To enable the optional LLM explanations:

docker run --rm -p 3000:3000 \
  -e CEREBRAS_API_KEY=your_key_here \
  irfanuruchi/integral-calculator:latest

If the key is not set, the application still runs normally.


Running locally (without Docker)

cabal build
cabal run

The server will start on port 3000.


Notes on design

The goal of this project was not to build a large CAS, but to understand how such systems work internally.

Writing the parser, simplifier, and calculus rules by hand made the behavior predictable and transparent. Keeping everything centered around one expression type made symbolic transformations feel natural and easy to reason about.

The system is intentionally compact, but it covers the core ideas behind symbolic computation, numeric integration, and functional program structure.


Why this project exists

This started as a course assignment, but it was treated as an opportunity to build something closer to a real tool — small enough to understand end-to-end, but complete enough to be genuinely useful.

The result is a minimal CAS that demonstrates how symbolic math, numeric methods, functional programming, and a lightweight web interface can fit together cleanly.

Tag summary

Content type

Image

Digest

sha256:33956109f

Size

38.2 MB

Last updated

3 months ago

docker pull irfanuruchi/integral-calculator