Skip to main content

The NumFu programming language

Project description

PyPI - Version PyPI - License PyPI - Python Version PyPI Downloads Docs GitHub stars

NumFu Programming Language

NumFu is a pure, interpreted, functional programming language designed for readable & expressive code, extensibility, and ease of learning for beginners.

NumFu's simple syntax and semantics make it well-suited for educational applications, such as courses in functional programming and general programming introductions. At the same time, as its name suggests, NumFu is also ideal for exploring mathematical ideas and sketching algorithms, thanks to its native support for arbitrary-precision arithmetic.

Features

  • Arbitrary Precision Arithmetic - Reliable mathematical computing powered by Python's mpmath
  • First-Class Functions - Automatic currying, partial application, and function composition
  • Expressive Syntax - Infix operators, spread/rest operators, and lots of syntactic sugar
  • Tail Call Optimization for efficient recursive algorithms without stack overflow
  • Interactive Development - Friendly REPL and helpful error messages
  • Minimal Complexity - Only four core types: Number, Boolean, List, and String
  • Python Integration - Large & reliable standard library through NumFu's Python runtime
  • Extensible - NumFu is written entirely in Python with the goal of being extensible and easy to understand.

Quick Start

Installation

From PyPI

pip install numfu-lang

From Source

git clone https://github.com/rphle/numfu
cd numfu
make install

Hello NumFu!

Create hello.nfu:

import sqrt from "math"

// Mathematical computing with arbitrary precision
let golden = {depth ->
  let recur =
    {d -> if d <= 0 then 1 else 1 + 1 / recur(d - 1)}
  in recur(depth)
} in golden(10) // โ‰ˆ 1.618

// Function composition & piping
let add1 = {x -> x + 1},
    double = {x -> x * 2}
in 5 |> (add1 >> double) // 12

// Partial Application
{a, b, c -> a+b+c}(_, 5, _)
// {a,c -> a+5+c}

// Assertions
sqrt(49) ---> $ == 7

// Built-in testing with assertions
let square = {x -> x * x} in
  square(7) ---> $ == 49  // โœ“ passes

Run it:

numfu hello.nfu

Interactive REPL

numfu repl
NumFu REPL. Type 'exit' or press Ctrl+D to exit.
>>> 2 + 3 * 4
14
>>> let square = {x -> x * x} in square(7)
49
>>> import max from "math"
>>> [1, 2, 3, 4, 5, 6, 7] |> filter(_, {x -> x%2 == 0}) |> max
6

๐Ÿ“– Documentation

[!NOTE] As a language interpreted in Python, which is itself an interpreted language, NumFu is not especially fast. Therefore, it is not recommended for performance-critical applications or large-scale projects. However, NumFu has not yet been thoroughly optimized so you can expect some performance improvements in the future.

๐Ÿ› ๏ธ Development

Prerequisites

  • Python โ‰ฅ 3.10

Setup Development Environment

git clone https://github.com/rphle/numfu
cd numfu
make dev

The make dev command also installs Pyright and Ruff via Pip. To format code and check types, it is strongly recommended to run both ruff check --fix and pyright before committing.

Building NumFu

make build

NumFu contains built-ins written in NumFu itself (src/numfu/stdlib/builtins.nfu). make build first installs NumFu without the built-ins, then parses and serializes the file, and finally performs a full editable install. The script also builds NumFu and creates wheels.

Building Documentation

cd docusaurus && npm i && cd .. # make sure to install dependencies

make serve  # local preview
make docs   # build to 'docs-build'

Project Structure

numfu/
โ”œโ”€โ”€ src/numfu/
โ”‚   โ”œโ”€โ”€ __init__.py         # Package exports
โ”‚   โ”œโ”€โ”€ _version.py         # Version & metadata
โ”‚   โ”œโ”€โ”€ classes.py          # Basic dataclasses
โ”‚   โ”œโ”€โ”€ parser.py           # Lark-based parser & AST generator
โ”‚   โ”œโ”€โ”€ interpreter.py      # Complete Interpreter
โ”‚   โ”œโ”€โ”€ modules.py          # Import/export & module resolving
โ”‚   โ”œโ”€โ”€ ast_types.py        # AST node definitions
โ”‚   โ”œโ”€โ”€ builtins.py         # Built-in functions
โ”‚   โ”œโ”€โ”€ cli.py              # Command-line interface
โ”‚   โ”œโ”€โ”€ repl.py             # Interactive REPL
โ”‚   โ”œโ”€โ”€ errors.py           # Error handling & display
โ”‚   โ”œโ”€โ”€ typechecks.py       # Built-in type system
โ”‚   โ”œโ”€โ”€ reconstruct.py      # Code reconstruction for printing
โ”‚   โ”œโ”€โ”€ grammar/            # Lark grammar files
โ”‚   โ””โ”€โ”€ stdlib/             # Standard library modules
โ”œโ”€โ”€ docs/                   # Language documentation
โ”‚   โ”œโ”€โ”€ guide/              # User guides
โ”‚   โ””โ”€โ”€ reference/          # Reference
โ”œโ”€โ”€ docusaurus/             # Docusaurus website
โ”œโ”€โ”€ tests/                  # Test files
โ”œโ”€โ”€ scripts/                # Build and utility scripts
โ””โ”€โ”€ pyproject.toml          # Configuration

Testing

NumFu is tested with over 300 tests covering core features, edge cases, and real-world examples โ€” including most snippets from the documentation. Tests are grouped by category and include handwritten cases as well as tests generated by LLMs (mostly Claude Sonnet 4).

Every test is self-validating using assertions and fails with an error if the output isnโ€™t exactly as expected.

To run all tests from the tests folder:

make test

Contributing

Found a bug or have an idea? Open an issue.

Want to contribute code?

  • Check existing issues and TODO.md for open tasks.
  • Run all tests before committing.
  • Please consider running ruff check and pyright to format code and check types before committing.
  • Pull requests are welcome!

License

This project is licensed under Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numfu_lang-0.2.0.tar.gz (44.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

numfu_lang-0.2.0-py3-none-any.whl (44.6 kB view details)

Uploaded Python 3

File details

Details for the file numfu_lang-0.2.0.tar.gz.

File metadata

  • Download URL: numfu_lang-0.2.0.tar.gz
  • Upload date:
  • Size: 44.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for numfu_lang-0.2.0.tar.gz
Algorithm Hash digest
SHA256 89266b0ac242d932162e6a5dadc4827578a157ccc101b70ae935c53790fb3d38
MD5 6505cfcd5dd6b596d310e8f284af10d8
BLAKE2b-256 46c6d7d5b7aa1f8ed90942b079d36c4caaead3e95bca72d3bd91061960c6e6ad

See more details on using hashes here.

File details

Details for the file numfu_lang-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: numfu_lang-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 44.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for numfu_lang-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98c32c2f9d11d77e377d65a16eb0d51d9570b9c132ff0d254d2b325192961125
MD5 6cc1c482d8165eff4c074b8df997918d
BLAKE2b-256 3e5a326af29114816aa62d4a0a1451ea50fce78b2bf173200fa61125ff3242b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page