MathFP is a functional, expression-oriented programming language designed for mathematical modeling and rapid prototyping. Built with Rust, it prioritizes safety and mathematical correctness.
- Expression-First: Everything in MathFP is an expression, which allows for more elegant composition of logic.
- First-Class Functions: Functions are first-class values. They can be passed as arguments and returned from functions or other expressions.
- Rich Error Reporting: Detailed scanner and parser error messages, including line and column tracking.
Clone the repository and build the project using Cargo:
git clone https://github.com/varunvf/mathfp-rs.git
cd mathfp-rs/
cargo build --releaseA simple syntax highlighting extension for VS Code is available (located in editors/vscode/).
Most editor themes should work alongside with this extension.
Run the REPL to start evaluating expressions:
cargo runYou can also run a script by passing the filename as an argument.
cargo run -- script.mfpVariables are declared using the := operator.
A variable can only be declared once in the same scope.
x := 10; y := x * 5
Most variables can be modified using the = operator.
x = 2 * y
Any expression can be used in the then and else branches of an if-expression.
if y then (z := 1) else (z := 2)
If you omit the else branch but the condition is false, nil is implicitly returned.
res := if 0 then 5 // res is now nil
Functions use the |-> (maps-to) operator:
f := x |-> x * x
f(2)
Functions can have a more complex body with multiple statements.
The last expression is implicitly returned. Bindings created inside a function are locally scoped and do not affect their outer scope.
hypotenuse := a |-> b |-> {
a2 := a * a;
b2 := b * b;
sqrt(a2 + b2)
}
hypotenuse(3)(4)
Common math functions like sin and sqrt are defined as native functions, and can be used anywhere.
square := x |-> x * x;
square(sin(9)) + square(cos(9))
Run the testing suite with cargo:
cargo testThe project documentation is automatically updated on every push to main.
View the online documentation
You can also view the local version by running:
cargo doc --open