Skip to main content

RecursionLens

See not just what recursion returns, but where the repeated work happens.

RecursionLens is a zero-runtime-dependency Python package for exploring recursive execution in JupyterLab. It turns a traced run into an expandable call tree, a compressed subproblem DAG, and a quantitative naive-versus-memoized comparison.

JupyterLab tutorial, already executed: Open RecursionLens_Tutorial.ipynb. All eight code cells have saved output. If Jupyter hides HTML/SVG from a downloaded notebook, use its Trust action or click Run All Cells once. For a zero-setup preview, download and open the static HTML companion.

Project team

CS1302A Group 50:

  • Ziyu Jiang
  • Xinyue CAI
  • Kehan CAO
  • Ruoxi LYU
  • Haobo MA

Why RecursionLens?

A recursive function normally exposes only its final return value or traceback. Students still have to reconstruct the execution in their heads. RecursionLens answers three different questions:

  1. Call tree: what actually happened, invocation by invocation?
  2. Subproblem DAG: how many distinct problems and dependencies exist after identical arguments are merged?
  3. Execution comparison: what changes when memoization is enabled?

For fib(5), that story is 15 observed calls → 6 unique subproblems → 6 executed bodies with memoization.

Innovation beyond a call graph

The existing rcviz package documents decorator-based recursion trees with arguments, return values, and Jupyter rendering. RecursionLens deliberately focuses on a different workflow:

  • transform one trace between an invocation tree and a compressed subproblem DAG;
  • show the complete call tree at once in an SVG view or keep a collapsible tree for larger runs;
  • quantify repeated calls, executed bodies, cache hits, and maximum depth;
  • compare naive and memoized runs in one report with side-by-side DAGs;
  • render self-contained HTML/SVG in JupyterLab without Graphviz, native libraries, external JavaScript, or a CDN;
  • export versioned JSON for reproducible analysis and testing.

Installation

RecursionLens requires Python 3.9 or later.

Install from PyPI:

python -m pip install recursionlens

For local development:

git clone https://github.com/ZiyuJiang07/recursionlens.git
cd recursionlens
python -m pip install -e ".[dev]"

The package has no runtime dependencies.

Quickstart in JupyterLab

from recursionlens import trace

@trace
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

naive = fib.run(5)
naive  # expandable call tree in the notebook output area

Switch the same trace to the compressed subproblem DAG:

naive.show(mode="dag")

Or show every call at once as one compact SVG tree, with no nodes collapsed:

naive.show(mode="expanded")

Run the function again with memoization and compare the evidence:

memoized = fib.with_options(memoize=True).run(5)
comparison = naive.compare(memoized)
comparison

The comparison confirms the returned values match and reports the measured changes, including 15 → 6 executed function bodies.

Normal function behavior is preserved:

assert fib(5) == 5
assert fib.last_report.total_calls == 15

Export a report

naive.export_html("fib-tree.html")
naive.export_html("fib-dag.html", mode="dag")
naive.export_json("fib-trace.json")
comparison.export_html("fib-comparison.html")

HTML reports are standalone files that can be opened without a notebook server. JSON contains the tree, DAG nodes and edges, metrics, warnings, and schema version.

Supported scope

Version 0.1 targets synchronous, deterministic teaching examples such as factorial, Fibonacci, and pure mathematical recursion.

  • Memoization requires hashable bound arguments; unhashable arguments produce a warning instead of a crash.
  • Identical arguments do not necessarily imply redundant work for functions with side effects or external state.
  • max_calls bounds the stored tree without changing the function's result. A truncated report explicitly marks partial tree/DAG metrics.
  • RecursionLens executes your function; it is not a sandbox for untrusted code.
  • Async functions, generators, automatic Big-O inference, and automatic code rewriting are non-goals for 0.1.

Documentation and demo

Development and testing

python -m pytest tests -v
python -m build
python -m twine check dist/*

The test suite covers trace invariants, cache behavior, signatures, exceptions, truncation, JSON, HTML escaping, DAG rendering, and comparison reports. Contributions should keep the public API, tests, and user documentation synchronized.

License

MIT © 2026 Ziyu Jiang, Xinyue Cai, Kehan Cao, Ruoxi Lyu, Haobo Ma.

Metadata

Release files for recursionlens 0.1.2

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

Source distribution (sdist)

Source distribution for recursionlens 0.1.2
File Size Uploaded Provenance Actions
recursionlens-0.1.2.tar.gz 19.4 kB Without provenance Details for recursionlens-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for recursionlens 0.1.2
File Interpreter ABI Platform Actions
recursionlens-0.1.2-py3-none-any.whl Python 3 none any Details for recursionlens-0.1.2-py3-none-any.whl

Total release size: 34.9 kB

Release files / recursionlens-0.1.2.tar.gz

Download URL recursionlens-0.1.2.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a1871b78b2163fbf447fed69500c9bd7373a8b6afe235575ab14e78f131d9dde
BLAKE2b-256 checksum
How to use checksums
989ee2f0f280e1c989c75899863b745f56e92c4e169a70d706d2d5bae56d54c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Release files / recursionlens-0.1.2-py3-none-any.whl

Download URL recursionlens-0.1.2-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb391836d26d9f66c4cd8ca17b3079a9e0eb2442c1ecd0534eb4b3d8ac938a6e
BLAKE2b-256 checksum
How to use checksums
c0063d1945be0a548d0c4c6c88344e9709a4ad3938f872bb141d6a309ed1b3e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Security for recursionlens 0.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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