Transfer — relational proof transfer in Lean

Transfer is a Lean 4 + Mathlib library for reasoning up to a registered relation. A user declares once how an abstract value relates to a concrete representation of it. The library then synthesizes witnesses of that relation, transfers statements and terms across it, and decides it structurally by congruence. Every step produces a kernel-checked proof term.

The design follows Trocq's modular parametricity: its lattice of relation strengths and its combinators. It also draws on relational congruence closure, on functorial type casts and description-based deriving from AdapTT, on heterogeneous congruence (the set-level form of cubical hcongr), and on data refinement from CoqEAL. It builds on Lean's relational automation: grind, gcongr, simp/norm_cast, type classes, conv, aesop and mvcgen. The library adds the witness synthesis, the transfer tactics, and one coordinating tactic, param_auto, that dispatches to all of these surfaces.

The library is used in compiler verification, to relate emitted low-level code to its algebraic specification, and in program verification, to transfer mvcgen / Std.Do triples and to reason about code extracted from Rust by hax up to its representation.

Used by: the CatCrypt verified compiler, where transfer is one of three correctness techniques beside the compiler's descent and ascent (CORRECTNESS.md).

Single entry point: import Transfer. The example suites are the separate library TransferExamples (import Transfer.Examples). Released under LGPL-3.0 (see LICENSE).

Documentation

  • Manual — concepts, tactics, and worked examples
  • API reference — per-declaration signatures and docstrings, no proof bodies

CI publishes both from main to GitHub Pages. To build them locally, see the manual and API-reference subsections under Building.

The Lean code blocks in this README are verbatim excerpts of Transfer/Examples/Readme.lean, which compiles them against the library.

Building

See BUILDING.md for the full instructions: the library, use as a dependency, the manual, and the API reference. The short form:

lake exe cache get            # fetch prebuilt Mathlib oleans — run this FIRST
LEAN_NUM_THREADS=6 lake build Transfer

One relation, three uses

The library operates on a single object: a relation R between a value and a representation of it. The relation takes one of two forms: Related enc a b (enc a = b, the encoding view) or, graded, Param (m n) A B (relation + maps). It is registered once through attributes (@[param], @[transfer], RelatedBinOp instances, deriving Param). Three operations then act on it.

1 — Build R a b (synthesis). Type-class resolution finds or composes a witness. HasParam/Related instance search composes registered witnesses through the structure of a term, as Trocq's Elpi search does in Coq.

example (a b c : F) : Related (id : F → F) (a * b + c) (bbFieldAdd (bbFieldMul a b) c) :=
  inferInstance        -- composed from the registered RelatedBinOp squares

2 — Transfer a goal along R (parametricity). param_transfer moves a ∀-statement to the other representation, and #transfer emits the ⟦·⟧ translation of a term.

-- ℤ ↠ ZMod p: an integer fact transfers to its modular image.
example (p : ℕ) [NeZero p] :
    (∀ i : ℤ, (Int.cast i : ZMod p) + 0 = (Int.cast i : ZMod p)) →
      (∀ x : ZMod p, x + 0 = x) := by
  param_transfer
  intro i x (hix : (Int.cast i : ZMod p) = x) h
  rw [← hix]; exact h

#transfer (fun x : Nat => Nat.succ x * x)   -- ⟦·⟧ : the term + its relatedness proof

3 — Decide R a b structurally (congruence). Relational congruence closure relates two structurally similar terms.

example (a b c : F) : a * b + c = bbFieldAdd (bbFieldMul a b) c := by param_solve

In these examples F is ZMod p for the prime p = 2^31 - 2^27 + 1, and bbFieldMul/bbFieldAdd are two operations (Transfer/Examples/ExampleField.lean) registered as realizations of * and + (Transfer/Base/Related.lean).

The unifying inference rule

All three uses run on one inference rule:

related arguments give related applications — from R aᵢ bᵢ infer R (f a…) (g b…) (R_arrow / R_forall).

The same rule appears in four places:

  • as the application/λ rule of the parametricity translation (use 2),
  • as the congruence step of the closure tactics (use 3),
  • as AdapTT's functoriality of type formers, which lets R compose through →, ×, and inductive types,
  • and as heterogeneous congruence: the general R_forall relates outputs of different fiber types across a representation change (hcongr_hetero, HCongrConnection).

Each use bottoms out in existing Lean machinery. Type classes discharge synthesis, grind discharges the equational congruence-closure core, and simp/norm_cast/gcongr/conv/aesop/mvcgen are native surfaces the library connects to.

The library has two axes. The vertical axis is transfer across representations (parametricity). The horizontal axis is congruence within a representation. The functoriality rule above connects them.

The coordinating tactic — param_auto

param_auto dispatches to every surface above, so one call site closes goals in different relations. It routes each goal to the tactic that closes it (param_solve, rcongr, norm_cast, gcongr, grind):

example (a : Nat) : a + 0 = a := by param_auto                    -- Eq
example (a b : Nat) (h : a ≤ b) : a + 1 ≤ b + 1 := by param_auto  -- ≤, via gcongr
example (a b : Nat) :                                             -- cast graph, via norm_cast
    ((a + b : Nat) : Int) = (a : Int) + b := by param_auto
example (a b c : F) :                                             -- registered rep. change, via rcongr
    a * b + c = bbFieldAdd (bbFieldMul a b) c := by param_auto

The dispatch strategy can be reordered or made goal-directed without changing any proof that calls param_auto. The specialized tactics below remain available for a proof that names one strategy.

The relation, graded

A relation carries more or less structure, and a transfer uses only as much as the goal requires. Param (m n) A B grades this:

What R carriesUse it for
a bare relationrelate values that share no maps
a forward map (a cast A → B)push a value/computation to its representation
a backward decoder (B → A)a retraction — pull a representation back (e.g. ℤ ↠ ZMod p)
both, coherentan equivalence (e.g. ℕ ↔ binary)

A relation is registered at the strength that can be proved. The level solver (ParamInfer) then picks the minimum strength a given goal needs and weakens down the lattice (ParamWeaken, auto_weaken). Worked examples: ParamRetraction (ℤ↠ZMod p / ℕ↠ZMod p), ParamCryptoExamples (ℕ↔binary, Num↦ℕ), and ExampleField, a registered-operation demo with the identity encoding. The cross-type field retraction BitVec 64 ↠ ZMod p is in MachineLimbField.

Composition & derivation (functoriality)

Transfer composes and extends through three mechanisms:

  • The relation R composes. Param_trans glues A↔B and B↔C into A↔C.
  • Casts compose and preserve identity. cast_trans is AdapTT's AdaptComp (cast (R∘S) = cast S ∘ cast R) and cast_id is AdaptId; the combinators distribute over composition (ParamCoherence). These theorems certify the functor laws. In Lean the composite's forward map is function composition, so the laws hold by rfl, and composition through nested type formers needs no further proof. AdapTT obtains the same laws from a definitional cast calculus. The laws are cited explicitly where a proof builds a chain of casts by hand. The dependent formers extend the non-dependent →/×/List laws: paramForall (Π) and paramSigma (Σ, sigma_cast_eq = Adapt Σ = Σ Adapt) carry the same functoriality to dependent types.
  • Inductive R is derived. deriving Param reads an inductive's constructor signature (AdapTT's IndDesc) and generates its constructor-wise relation and a full Param instance. It rejects mixed-variance inductives (AdapTT's Non-example). It covers records, enums, and uniform-recursive (List/Tree-shape) types.
inductive Pair (A B : Type) | mk : A → B → Pair A B
  deriving Param

The congruence layer

Four tactics act on one relation with two strategies and their union (full reference: TACTICS.md):

TacticStrategyCloses
rcongr / hgcongrtop-down descenta registered cross-head op-tree, leaving per-argument relatedness subgoals
param_ccbottom-up closure (via grind)transitivity / context-hypothesis chains
param_solvedescent + closure-leafthe union of the above — the default
param_guarddescent through guarded squaresop-trees whose squares hold only under a side condition; open guards are returned as goals

Guarded squares

A square that holds only on a range is registered with its guard, a proposition on the abstract operands (Base/RelatedOn.lean): the class RelatedBinOpOn enc P op bop has the single field comm : ∀ x y, P x y → enc (op x y) = bop (enc x) (enc y), with P an outParam. RelatedUnOpOn enc P op uop and RelatedRelOn enc P r br (P x y → (r x y ↔ br (enc x) (enc y))) are the unary and relation forms. Every total RelatedBinOp/RelatedUnOp is a guarded square with guard True (RelatedBinOp.toOn), and BinOpHomOn.toRelatedBinOpOn converts a ReprTransfer.BinOpHomOn. The guard of a node is stated on that node's operands, so the guards of a composite are ordinary side conditions:

-- truncated `ℕ` subtraction and shift against wrapping 64-bit words
example (a b s : ℕ) (ha : a < 2 ^ 64) (hb : b ≤ a) (hs : s < 64) :
    enc64 ((a - b) >>> s) = (enc64 a - enc64 b) >>> enc64 s := by
  param_guard          -- guards `b ≤ a` and `a - b < 2 ^ 64 ∧ s < 64`, closed by omega

param_guard discharges each guard with transfer_guard_disch (first | trivial | assumption | omega | bv_omega | decide, extensible by macro_rules) or with param_guard (disch := tac), and returns the guards it cannot close as goals. When a subterm has no square, the error names that subterm and its head pair. param_solve also descends through guarded squares, with the default discharger. Examples: Examples/GuardedSquares.lean.

A function whose argument and result have different encodings is related to its model by a carrier-changing square (Base/RelatedFun.lean): RelatedFun encA encB f g states encB (f a) = g (encA a), and RelatedFun2, RelatedFun3 are the forms with two and three arguments, each argument with its own encoding; RelatedFunOn, RelatedFun2On and RelatedFun3On are the guarded forms. The module also registers data squares for Option, Prod, Except, lists and if-then-else. Their rules are alternatives of transfer_guard_leaf, so param_guard descends through them. param_guard also splits an equation whose left-hand side is a constructor application into the equations of its fields, by constructor congruence or by structure eta, and descends into each.

hgcongr is the heterogeneous generalization of gcongr. It is attribute-driven (@[hgcongr]) and keyed on a pair of heads, so it relates different head functions f ≠ g, a case gcongr rejects by construction. HGCongr.lean carries the upstream Mathlib.Tactic.GCongr.Core patch that would add this to gcongr.

Plugging into Lean

The library reuses native Lean machinery. Transfer is only as sound as the registered witnesses, so a missing witness remains a visible residual goal. For the same reason the aesop rule set is opt-in: it never searches for a square on its own.

MechanismRole
type classesthe synthesis engine (HasParam/Related resolution)
grindthe equational congruence-closure backend for param_cc (foundation squares dual-tagged @[grind =])
gcongrhomogeneous congruence; hgcongr is its cross-head generalization
simp / norm_castthe closed-equation fast path; a @[norm_cast] move-lemma is a transfer witness over the cast graph (ParamNormCast)
convfocused sub-term transfer (transferConv)
aesopan opt-in Transfer rule set bundling the transfer rules
Std.Do / mvcgenHoare-triple transfer via the Kleisli relation RComp (ParamTripleTransfer); rcomp (folded into param_transfer) assembles the witness
coercionsa Coe/CoeTC derived from a forward map (ParamCoe)

RComp Rα c c' is the Kleisli lift of a value relation Rα to monadic computations: for every pair of postconditions related pointwise by Rα, the weakest precondition of c entails that of c'. RCompOk is its success-restricted variant: each value that c returns without an exception has an Rα-related partner among the results of c' (ParamRCompOk, tactic rcomp_ok). Both are instances of Param, and both compose (ParamRCompParam).

Quick start

A file that uses the library starts with

import Transfer
open Transfer Transfer.Param Transfer.ExampleField

and can then register witnesses, transfer statements, derive relations, and close goals by congruence:

-- register a witness
def double (n : ℕ) : ℕ := 2 * n
@[param] theorem doubleR : RArrow Eq Eq double double := fun _ _ h => congrArg double h

-- transfer a ∀-statement across ℤ ↠ ZMod p (domain relation auto-resolved)
example (p : ℕ) [NeZero p] :
    (∀ i : ℤ, (Int.cast i : ZMod p) + 0 = (Int.cast i : ZMod p)) →
      (∀ x : ZMod p, x + 0 = x) := by
  param_transfer
  intro i x (hix : (Int.cast i : ZMod p) = x) h
  rw [← hix]; exact h

-- derive the relation for a data type
inductive Pair (A B : Type) | mk : A → B → Pair A B deriving Param

-- close a representation-change equation by congruence
example (a b c : F) : a * b + c = bbFieldAdd (bbFieldMul a b) c := by param_solve

Full per-tactic / per-attribute reference: TACTICS.md.

File map (reference)

The source under Transfer/ is organized into concept subdirectories. The root Transfer.lean is the single entry point.

Base/ — the Related/RelatedBinOp substrate and the class-hierarchy, level and tactic substrate the engine reuses.

ModuleContent
Corethe repr registry and the repr_transfer tactic
FieldRegistrythe Baby Bear field kernels as registered operations
Hierarchythe relation hierarchy as classes
HigherOrderTransferbinder / higher-order / ∀ transfer as lemmas
LevelRefusalthe univalence-free refusal guard
Levelsrelatedness levels: the three-point view of the Trocq lattice
RelatedRelated: transfer by instance resolution
RelatedAtlevel-annotated relatedness
RelatedFuncarrier-changing function squares RelatedFun, RelatedFun2, RelatedFun3 and their guarded forms
TransferInductiontransported induction, the equiv-level combinator
TransferLevelthe level-aware transfer! elaborator
TransferTacticthe transfer tactic (automatic binder translation)
UnivalenceStatusunivalence in Lean 4 and the relation between universes

Hierarchy/

ModuleContent
ParamEquivthe equivalence bridge and Param weakening
ParamHierarchythe Param relation hierarchy
ParamLevelthe level lattice and the minimal-(m,n) solver core
ParamWeakenthe ⊑-driven Param weakening (level navigation)

Combinators/

ModuleContent
ParamArraythe Array container rule and relational foldl transfer
ParamArrowthe arrow (app / function) parametricity rule
ParamCoherencethe coherence-law suite (functor laws)
ParamDatathe data-type (× / Option / List) parametricity rules
ParamForallthe dependent-Π (∀) parametricity rule; mapHas_forall and paramForallAt give it at every output level, with the inputs the tables forallReq and forallFiberReq compute, none of which asks for map4
ParamSigmathe dependent-pair (Σ) parametricity rule
ParamTransrelation composition (Param_trans)

Synthesis/

ModuleContent
ParamInfervariable-level minimal-class inference
ParamInferMetalevel inference over Expr
ParamResolvethe upward level search (param_resolve)
ParamSynththe Param-witness synthesizer
ParamSynthExtthe multi-level Param-witness synthesizer

Translate/

ModuleContent
ParamDBthe persistent @[param] constant database
ParamTranslatethe term-level ⟦·⟧ synthesizer (the parametricity translation)
ParamTranslateFullthe integrated term-level translator ⟦·⟧
ParamTranslateOpthe operator/arity rule for the term-level translator
ParamTranslateTythe binder type-translation rule (change of representation)

Statements/

ModuleContent
ParamForallNesteddependent-Π transfer for nested ∀-statements
ParamTransfer∀-transfer through the Param engine (Map1_forall)
ParamTransferTacparam_transfer, end-to-end ∀-transfer

Congruence/

ModuleContent
GCongrProbewhether @[gcongr] accepts cross-head realization lemmas
HCongrConnectiona set-level analogue of cubical heterogeneous congruence (hcongr)
HGCongrhgcongr: heterogeneous (cross-head) generalized congruence
HGCongrInitthe hgcongr engine: head-pair-keyed database, attribute, and tactic
ParamAutoparam_auto: one coordinator over every congruence surface
ParamComposeparam_compose: descend-and-dispatch congruence
ParamCongrClosureparam_cc: relational congruence closure over Related
ParamSolveparam_solve: the common generalization of rcongr and param_cc
ParamGuardparam_guard: descent through guarded squares, open guards returned as goals
RCongrrcongr: relational (cross-head) congruence descent

Deriving/

ModuleContent
ParamCongrderive_param_congr: a constructor-congruence Related deriver
ParamDerivehand-ported Param lifts and the specification of the @[derive Param] handler
ParamDeriveHandlerthe description-based @[derive Param] handler

Integrations/

ModuleContent
AesopIntegrationthe Transfer aesop rule set
AesopRuleSetthe declaration module of the Transfer aesop rule set
GrindIntegrationgrind as a transfer leaf discharger
ParamAutoWeakenauto-weakening of witnesses and a unified transfer entry
ParamCoethe coercion integration (Param witness → Coe)
ParamConvconv-mode transfer
ParamForInforIn / do-loop transfer: foldlR meets RComp
ParamNormCastnorm_cast move-lemmas as Param relatedness witnesses
ParamRComprcomp: structural assembly of an RComp witness
ParamRCompOkRCompOk: the success-restricted Kleisli relation
ParamRCompParamRComp/RCompOk as instances of Param, and their composition
ParamRelatedBridgeone witness, both engines: RelatedBinOp ⇒ RArrow
ParamTripleTransfertransferring a Hoare/wp triple through a Param relation; defines the Kleisli relation RComp

Examples/

ModuleContent
EffectfulTransferan effectful transfer across a value-type change, proved by mvcgen
ExampleFielda self-contained example field for the engine demos
IsogenyBoundedProbeBLS12-381 hash-to-curve isogeny identity: bounded per-coefficient probe
MachineLimbFieldmachine limbs ↔ prime field: a non-diagonal heterogeneous dependent example
ParamCryptoDomainsthe Baby Bear field as a first-class transfer domain
ParamCryptoExamplescryptographic representation examples: coherence laws and deriving Param
ParamRetractionthe ℤ ↠ ZMod p retraction transfer domain (non-diagonal)
PeanoBinNatthe peano_bin_nat example, first-order slice
RCompOkExamplesRCompOk over a two-outcome test monad
Readmethe examples of this README
StrongExamplescongruence that the native tactics cannot do
Trocqthe Trocq example suite (index)
Trocq/SummableTrocq summable: summability transfers across an equivalence

Hex suite: worked refinements on the carriers of leanprover/hex.

ModuleContent
HexArrayComputehex's Array-backed storage: carrier-agnostic refinement and verified compute
HexDecidedischarging a hex side condition by computation on the concrete representation
HexEffectfultransferring a hex elimination step across dense storage, effectfully
HexMatrixCorrespondencehex-style dense-storage ↔ Mathlib correspondence, generated by the engine
HexSeqPolyhex's dense polynomial storage: a non-injective refinement at map2a

ZMod suite: deciding modular identities by bounded computation.

ModuleContent
ZModDecidedecide_zmod: ground ring identities over ZMod m by bounded residue computation
ZModPolyDecidedecide_zmod_poly: univariate polynomial identities over ZMod m
ZModPowModpowMod: binary modular exponentiation for kernel-decide modular arithmetic

CoqEAL suite: the library TransferCoqEAL, rooted at Transfer.Examples.CoqEAL, which depends on CompPoly.

ModuleContent
CoqEALthe CoqEAL / Trocq example suite (index)
CoqEAL/BareissDetCoqEAL bareiss: a fraction-free determinant certified against Matrix.det
CoqEAL/BinIntCoqEAL binint: sign + magnitude refines ℤ
CoqEAL/BinNatCoqEAL binnat: List Bool refines the natural numbers
CoqEAL/BinRatCoqEAL rational/binrat: num/den pairs refine ℚ
CoqEAL/ComputePolynomialcomputing with Mathlib's Polynomial through a CompPoly refinement
CoqEAL/GaussPivotStepGaussian elimination: single forward-elimination pivot step
CoqEAL/GaussSweepGaussian elimination: multi-row forward sweep
CoqEAL/KaratsubaCoqEAL Karatsuba polynomial multiplication
CoqEAL/KaratsubaRecrecursive Karatsuba on CPolynomial R
CoqEAL/MultipolyCoqEAL multipoly: multivariate polynomials as finite maps
CoqEAL/RankCoqEAL rank: matrix rank certified against Matrix.rank
CoqEAL/SeqMatrixCoqEAL seqmatrix: List (List R) refines the entry function
CoqEAL/SeqMatrixMulCoqEAL seqmatrix multiplication: List (List R) row-column product
CoqEAL/SeqPolyCoqEAL seqpoly: List R refines the coefficient function
CoqEAL/StrassenStrassen one-level 2×2 block multiplication identity
CoqEAL/StrassenRecrecursive (fuel-driven) Strassen block multiplication
CoqEAL/ToomCookCoqEAL toomcook: the evaluate–multiply–interpolate core

Top-level modules and libraries

ModuleContent
Transferthe single entry point
Transfer/Auditaxiom ledger: a build-time hygiene check (lake build Transfer.Audit)
ReprTransfer (library)representation transfer for emit-realization bridges
ReprTransferExpr (library)compositional transfer via the abstraction theorem

Provenance

The library builds on the work credited in ATTRIBUTION.md: Trocq (Cohen–Crance–Mahboubi), whose parametricity lattice and combinators the engine's core adapts; AdapTT (functorial casts and description-based deriving); CoqEAL (data refinement); the cubical congruence of Gjørup–Spitters; and Lean 4 / Mathlib.