Skip to main content

Python playground to learn lambda calculus

Project description

Curry party

curryparty is a library created to explore, visualize and teach lambda-calculus concepts.

Install

Run pip install curryparty or uv add curryparty depending on your package manager.

How to use

from curryparty import L, o

# Build classic lambda calculus expressions

# Identity: λx. x
identity = L("x").o("x").build()

# Const (K combinator): λx. λy. x
const = L("x", "y").o("x").build()

# Omega: λx. x x
omega = L("x").o("x", "x").build()
print(omega(omega))  # (λ0 x0(x0))((λ1 x1(x1)))

# Church numerals
zero = L("f", "x").o("x").build()
one = L("f", "x").o("f", "x").build()
two = L("f", "x").o("f", o("f", "x")).build()

succ = L("n", "f", "x").o("f", o("n", "f", "x")).build()

# S combinator: λf. λg. λx. f x (g x)
s_comb = L("f", "g", "x").o("f", "x", o("g", "x")).build()

# Beta reduction
# Nothing happens until you reduce:
app = identity(identity)
print(app)  # (λ0 x0)((λ1 x1))

# Perform one reduction step:
reduced = app.beta()
print(reduced)  # λ0 x0

# Multiple reductions:
term = succ(zero)
print(f"Steps: {len(list(term.reduction_chain()))}")  # Steps: 3

# Print each step:
for step in term.reduction_chain():
    print(step)

# Or get the final result directly:
result = succ(zero).reduce()
print(result)  # λ0 λ1 x0(x1) (i.e, one)

But the main point of this library is the svg-based display system. If you use a notebook such as jupyternotebook or marimo, you will see something like this:

notebook_display

You can also use term.show_reduction to get an animated version.

Tutorial

lambda.py shows a tutorial in marimo format. Click on the button to try it out:

Open with marimo

How it works

Under the wood, all the terms are converted into a list of nodes, that can either be a lambda, an application (with 2 arguments) or a variable (with 0 arguments).

For efficiency, they are converted to a polars dataframe in prefix traversal order. If you want to understand how it works, start by looking at the .nodes attribute of any term.

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

curryparty-0.4.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

curryparty-0.4.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: curryparty-0.4.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for curryparty-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c008f7b1ef29219ba5ab64d243dcc918e6e81cb84a4008d810ff4bac3ff1d286
MD5 ac859de06d3a71695b6421e38448c916
BLAKE2b-256 9a871f781978c8c0f4147a28ddef5bef4a7c4f0c3f649d983a6e385298d0a7e3

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on rambip/curryparty

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

File details

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

File metadata

  • Download URL: curryparty-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for curryparty-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ec1a3c6dd326b9c9689839c1b9c1c07ebe642f2a93c188c11354d059fd10953
MD5 3b0dcc55db8a3cd14afc2a373daf8126
BLAKE2b-256 b4ba273cd32c59fe1daa8e5b977ec5374f03299abab7d2836a04b61f81554cef

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on rambip/curryparty

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

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