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

Download files

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

Source Distribution

pythonic_peano_arithmetic-0.4.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

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

pythonic_peano_arithmetic-0.4.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file pythonic_peano_arithmetic-0.4.0.tar.gz.

File metadata

File hashes

Hashes for pythonic_peano_arithmetic-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c9b8f5aaf188de39e92867ea52b243acfbb74af68c29899fac9c343466217c66
MD5 d8d27563c82fa8a747dde18698611894
BLAKE2b-256 5352e06c5877a5f26f807b13c3544abaecf3658306cc1a65916580940d88954f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythonic_peano_arithmetic-0.4.0.tar.gz:

Publisher: publish.yml on yhay81/pythonic-peano-arithmetic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pythonic_peano_arithmetic-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pythonic_peano_arithmetic-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f04d9294c5677615d7978c07b4ce19e81488c3efa9e1ea6ca58807d6364dc6ed
MD5 7c17dab797e8657b136663d5e44eeea2
BLAKE2b-256 ee0144cff88ce0accabc2dcd18cbe09db12888b207de458e93cc004bb6eeef6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythonic_peano_arithmetic-0.4.0-py3-none-any.whl:

Publisher: publish.yml on yhay81/pythonic-peano-arithmetic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.0

2 files

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