feat(Control/Monad/Free): define free monad, prove it lawful, and implement standard effects #25491
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 280 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.337
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #25491"
general
weight: 0.35
tannerduve
This PR introduces the
Freemonad. This implementation uses the "freer monad" approach as the traditional free monad (eg from Haskell) is not safely definable in Lean due to termination checking (it's not strictly positive).The main contributions are:
Definition of the
Freemonad as an inductive type which generates a monad given any type constructorF : Type -> Type.Functor and Monad instances for
Free F, along with proofs of theLawfulFunctorandLawfulMonadlaws.Canonical instances of
Freewith standard effect signatures:FreeState sfor stateful computations, defined via aStateF sfunctor withgetandputoperations.FreeWriter wfor logging computations, defined via aWriterF wfunctor with atelloperation.FreeCont rfor continuation-passing computations, using the CPS functor(α → r) → r.In this construction, computations are represented as trees of effects. Each node (
liftBind)represents a request to perform an effect, accompanied by a continuation specifying how the
computation proceeds after the effect. The leaves (
pure) represent completed computations withfinal results.
A key insight is that
FreeM Fsatisfies the universal property of free monads: for any monadMand effect handlerf : F → M, there exists a unique way to interpretFreeM Fcomputationsin
Mthat respects the effect semantics given byf. This unique interpreter isliftM f, whichacts as the canonical fold for free monads.
To execute or interpret these computations, we provide two approaches:
1. Hand-written interpreters (
FreeState.run,FreeWriter.run,FreeCont.run) that directlypattern-match on the tree structure
2. Canonical interpreters (
FreeState.toStateM,FreeWriter.toWriteT,FreeCont.toContT) derived fromthe universal property via
liftMAnd then prove that these approaches are equivalent
---
This PR adds new files and definitions; no breaking changes.
Tags: free monad, freer monad, effect systems, state monad, writer monad, continuation monad, operational semantics, verified interpreters