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:
What you can observe
- natural numbers built from
0and 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 locale="ja" to config_log to show Japanese rule labels.
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;Polynomialremoves 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 Japanese at / and English at /en/
make docs-serve # preview Japanese
make docs-serve-en # preview English
make docs-a11y # run WCAG checks on both languages
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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pythonic_peano_arithmetic-0.3.0.tar.gz.
File metadata
- Download URL: pythonic_peano_arithmetic-0.3.0.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c78cd2bda4bfe6f9966bdb580f68efae8086518b697add700fa194aa8a8a149
|
|
| MD5 |
284d93b48ee5e83133fd8ac20b0e4810
|
|
| BLAKE2b-256 |
e1d089df92b0eb21c72fa678dada5aba84aec36003f4cb25c8099fb2f386e411
|
Provenance
The following attestation bundles were made for pythonic_peano_arithmetic-0.3.0.tar.gz:
Publisher:
publish.yml on yhay81/pythonic-peano-arithmetic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pythonic_peano_arithmetic-0.3.0.tar.gz -
Subject digest:
6c78cd2bda4bfe6f9966bdb580f68efae8086518b697add700fa194aa8a8a149 - Sigstore transparency entry: 2263716844
- Sigstore integration time:
-
Permalink:
yhay81/pythonic-peano-arithmetic@31deb8aa34e2450188d7e353ad5af1f7a556dacf -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/yhay81
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@31deb8aa34e2450188d7e353ad5af1f7a556dacf -
Trigger Event:
release
-
Statement type:
File details
Details for the file pythonic_peano_arithmetic-0.3.0-py3-none-any.whl.
File metadata
- Download URL: pythonic_peano_arithmetic-0.3.0-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
351a5dd4c5e005561933bf9df65666a66cabae0c4273f1bfabf0c25c3a74b5c6
|
|
| MD5 |
ee430dcbc43951a014596d0736eb951a
|
|
| BLAKE2b-256 |
881e7c1c92bc99a5c3f62c5a8944b06683238dfb14bc7d2fd115b128a9cf411a
|
Provenance
The following attestation bundles were made for pythonic_peano_arithmetic-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on yhay81/pythonic-peano-arithmetic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pythonic_peano_arithmetic-0.3.0-py3-none-any.whl -
Subject digest:
351a5dd4c5e005561933bf9df65666a66cabae0c4273f1bfabf0c25c3a74b5c6 - Sigstore transparency entry: 2263716978
- Sigstore integration time:
-
Permalink:
yhay81/pythonic-peano-arithmetic@31deb8aa34e2450188d7e353ad5af1f7a556dacf -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/yhay81
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@31deb8aa34e2450188d7e353ad5af1f7a556dacf -
Trigger Event:
release
-
Statement type: