Skip to content

refactor(pfunctor)!: make Handler a one-field structure - #534

Draft
dtumad wants to merge 2 commits into
Verified-zkEVM:mainfrom
dtumad:dtumad/handler-structure
Draft

dtumad wants to merge 2 commits into
Verified-zkEVM:mainfrom
dtumad:dtumad/handler-structure

Conversation

@dtumad

@dtumad dtumad commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

PFunctor.Handler was a reducible function type, so instance search looked through it: an instance stated for handlers would also apply to every function of the same shape. That is why Handler.sum had no +. It is now a structure with the single field toFun : (a : q.A) → m (q.B a), and f + g is an instance for Handler.sum that leaves pointwise addition of functions alone.

  • Use. A handler applies like a function (h a, through CoeFun with the projection registered as a coercion), so code that runs handlers, including CSLib's FreeM.liftM h, is unchanged. A function does not coerce to a handler: write ⟨f⟩ or where toFun a := …. Handler.equivFun is the equivalence with functions. PolyFunTest/ModuleAPI/HandlerCoercion.lean fixes which coercions elaborate and which do not, that application lemmas fire at abstract and literal interfaces, how + behaves on handlers and on functions, and that kernel comparison does not reduce a handler's answers.
  • Proofs. Handlers built by the constructor are used through their application equations, as bundled morphisms are in Mathlib, and are tagged @[simps] (CacheMeter's cache uses -isSimp: its equation leaves a match on the cache lookup, and proofs use run_apply_of_dlookup_eq_some and _none instead). Unfolding a handler instead makes simp traverse its body at every occurrence. Equalities of handlers use ext.
  • Enforcement. The environment linter handlerApply reports a handler built by the constructor that is reducible or has no application lemma; equation lemmas Lean derives from the definition do not count. handlerApplyFor takes a list of structures, so a library with its own handler structure can register it. PolyFunTest/Lint/HandlerApply.lean has a control for each case.

Migration. Wrap the function (⟨f⟩), replace funext on handlers with ext a : 1, and name foo_apply where a proof unfolded foo. Signatures that took a raw answer function for a handler (Display, Wiring) take a Handler.

This is the first of a stack of three draft PRs: VCVio Verified-zkEVM/VCVio#1241 makes QueryImpl a structure on top of this one, and ArkLib Verified-zkEVM/ArkLib#1604 ports a large downstream user to both. Its design record, with timings and what the ports exposed, is VCVio's docs/design/handler-structure.md.

Based on 6aded150. At the head (0bc4e329), ./scripts/validate.sh --lint --test --axioms passed and handlerApply reports nothing on PolyFun. The linter test's control for derived equation lemmas fails without the fix it guards.

dtumad and others added 2 commits November 25, 2026 19:54
Build handlers with `toFun`, apply them through a function coercion, and state their
constructions through application equations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dtumad
dtumad force-pushed the dtumad/handler-structure branch from b7596cf to 0bc4e32 Compare November 26, 2026 01:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant