Skip to main content

Pythonic Peano Arithmetic

An educational Python library that constructs natural numbers, integers, rational numbers, and polynomials from simple definitions, then uses rational intervals to study algebraic real roots.

The implementation is intentionally small and explicit. Its purpose is to make the correspondence between a mathematical definition and executable Python visible—not to compete with Python's built-in numeric types.

The interactive course runs entirely in the browser with Zensical and Pyodide. It is available in English, 日本語, 简体中文, 繁體中文, Español, Português (Brasil), Français, Deutsch, 한국어, Русский, العربية, and हिन्दी.

What you can observe

  • natural numbers built from 0 and the successor operation;
  • recursive definitions of addition and multiplication;
  • integers as equivalence classes of pairs of natural numbers;
  • rationals as equivalence classes of integer pairs;
  • polynomials as finite coefficient sequences;
  • Sturm sequences and bisection over rational isolating intervals;
  • Python mechanisms that connect notation to implementation: special methods, decorators, frozen dataclasses, coercion, and operator dispatch.

Install

Python 3.10 or later is required.

pip install pythonic-peano-arithmetic

For repository development, install uv and run:

git clone https://github.com/yhay81/pythonic-peano-arithmetic.git
cd pythonic-peano-arithmetic
make install
make check

A five-minute tour

Natural numbers: follow the recursive definition

from peano import natural_number
from peano.utils import config_log

config_log(log_level=4)
two = natural_number(2)
one = natural_number(1)
print(two + one)

The trace names the rule used at each step:

[addition: base] add(S(S(0)), 0) -> S(S(0))
[addition: recursive] add(S(S(0)), S(0)) -> S(add(S(S(0)), 0))
3

These two lines correspond directly to:

n + 0    = n
n + S(m) = S(n + m)

Pass a BCP 47 locale to config_log to localize rule labels. Supported values are en, ja, zh-Hans, zh-Hant, es, pt-BR, fr, de, ko, ru, ar, and hi.

Integers: equality of representatives

An integer is represented by a pair (a, b), read as a - b. Different pairs can represent the same integer:

from peano import integer

print(integer(3, 1) == integer(4, 2))

The implementation checks the defining equivalence:

(a, b) ~ (c, d)  exactly when  a + d = b + c

Rationals: equality by cross multiplication

from peano import rational

print(rational(1, 2) == rational(2, 4))

This follows the definition p/q ~ r/s exactly when p*s = q*r.

Algebraic real roots: approach √2 with rational intervals

from peano import Polynomial, Q_ONE, Q_ZERO, algebraic_root, rational

x_squared_minus_two = Polynomial(rational(-2, 1), Q_ZERO, Q_ONE)
root = algebraic_root(x_squared_minus_two, (1, 1), (2, 1))

for interval in root.trace(5):
    print(interval)

Every endpoint remains rational. The intervals are nested, their widths halve, and each interval still isolates the positive root of x² - 2.

Definition-to-implementation map

Mathematical idea Python implementation
zero and successor S(n) NaturalNumber, successor
0 differs from every successor NaturalNumber.__eq__
successor is injective NaturalNumber.__eq__
n + 0 = n, n + S(m) = S(n + m) NaturalNumber.__add__
n × 0 = 0, n × S(m) = n + n × m NaturalNumber.__mul__
(a,b) ~ (c,d) ↔ a+d=b+c Integer.__eq__
p/q ~ r/s ↔ ps=qr Rational.__eq__
coefficient sequence (a₀,a₁,...) Polynomial
distinct roots in an interval sturm_sequence, count_real_roots
one algebraic root AlgebraicRoot, RationalInterval

Mathematical induction is a proof principle, not a test performed by Python. The finite tests check representative laws and guard the intended mapping between the definitions and code.

Logging

Operations return ordinary values. Internally, selected methods return a value and a lazily constructed explanation; the @log decorator exposes only the value and emits the explanation when logging is enabled.

from peano.utils import config_log

config_log(
    log_level=4,
    max_lines=200,
    # fmt="Level %(levelno)s: %(message)s",  # expose internal levels if needed
)

Lower log levels reveal more detail. The numeric levels are an internal filter, so the default display emphasizes rule names instead.

Numeric tower and canonical forms

Mixed operations promote values through:

NaturalNumber → Integer → Rational → Polynomial

Canonicalization keeps equivalent representatives predictable:

  • Integer.normalize() moves a pair toward (a-b, 0) or (0, b-a);
  • Rational.reduction() makes the denominator positive and divides by the GCD;
  • Polynomial removes trailing zero coefficients.

Equal values have equal hashes even when represented at different levels of the numeric tower.

Scope and limits

This project favors definitions that can be read over efficient arithmetic. Keep examples small:

Operation Suggested values
natural-number comparison/addition 0–10
natural-number multiplication/division/powers 0–5
integer, rational, and polynomial components absolute values up to 5
algebraic-root tracing roughly 12 bisections

AlgebraicRoot is deliberately not a complete algebraic-number type. It validates one isolated root and shrinks its rational interval, but provides no arithmetic between roots and no general mathematical equality.

For interval refinement, sign checks use Python's arbitrary-precision integer ratios internally. This preserves exactness while avoiding enormous intermediate Peano representations; returned endpoints are still Rational.

Documentation development

make docs          # build all 12 course languages
make docs-serve    # preview Japanese
make docs-serve-en # preview English
make docs-a11y     # run WCAG checks on all 120 localized pages

The site is deployed from main to Cloudflare Workers Static Assets. Deployment requires CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID repository secrets.

Release

Releases are built by GitHub Actions and published to PyPI with OpenID Connect. The pypi GitHub environment must be registered as a PyPI Trusted Publisher for this repository and .github/workflows/publish.yml.

License

MIT

Metadata

Release files for pythonic-peano-arithmetic 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pythonic-peano-arithmetic 0.4.0
File Size Uploaded Provenance
pythonic_peano_arithmetic-0.4.0.tar.gz 19.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pythonic-peano-arithmetic 0.4.0
File Interpreter ABI Platform
pythonic_peano_arithmetic-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 42.8 kB

Release files / pythonic_peano_arithmetic-0.4.0.tar.gz

Download URL pythonic_peano_arithmetic-0.4.0.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c9b8f5aaf188de39e92867ea52b243acfbb74af68c29899fac9c343466217c66
BLAKE2b-256 checksum
How to use checksums
5352e06c5877a5f26f807b13c3544abaecf3658306cc1a65916580940d88954f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Release files / pythonic_peano_arithmetic-0.4.0-py3-none-any.whl

Download URL pythonic_peano_arithmetic-0.4.0-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f04d9294c5677615d7978c07b4ce19e81488c3efa9e1ea6ca58807d6364dc6ed
BLAKE2b-256 checksum
How to use checksums
ee0144cff88ce0accabc2dcd18cbe09db12888b207de458e93cc004bb6eeef6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

Security for pythonic-peano-arithmetic 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pythonic_peano_arithmetic-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page