Published October 21, 2026 | Version v3

Compiling index notation to sums of products: a verified functional pearl

Authors/Creators

Description

Index notation writes a repeated index name for a summation, and any implementation must turn it into explicit loops. The translation is a small functional program, yet it has a classic trap: reusing the name of a contracted index as the name of the loop variable captures free occurrences of that name elsewhere in a product. We present a compiler from a calculus of named indices to sums of products (the plans of an einsum), written in a few lines of Lean 4 and proved correct. Bound names come from a counter, so capture cannot happen; typing is erased, so the correctness proof does not use it; and the whole proof rests on three small lemmas about summation, renaming and fresh names. Composed with a verified inference of the contracted names, the compiler turns an unannotated expression such as A^i_j B^j_k into the plan "ac,cb->ab". The development is about 600 lines, uses only the Lean core library, and is executable.

The record contains the manuscript (PDF and LaTeX sources, in the format of the Journal of Functional Programming) and the Lean 4 artifact (tic_lean_v1.0.zip), which builds with "bash build.sh" using Lean 4.35.0.

Files

pavesi_jfp_pearl_index_notation.pdf

Files (1.8 MB)

Name Size
md5:6f88e63bf68ab7e5e3fd6479cf9240d0
1.7 MB Preview Download
md5:092f09f4d196542551973fafb84a44bd
37.9 kB Preview Download
md5:cf4c803fd7fdb858661fdfcb682c36ee
23.5 kB Preview Download

Additional details

Software

Repository URL
https://zenodo.org/records/235335885
Programming language
Lean
Development Status
Active