← Back to Pull requests

feat(Control/Monad/Free): define free monad, prove it lawful, and implement standard effects #25491

Closed
unknown closed this pull request without merging on Sep 02, 2025 21:32 UTC
View on GitHub
unknown unknown commented on Jun 05, 2025 15:16 UTC
Author

This PR introduces the Free monad. 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 Free monad as an inductive type which generates a monad given any type constructor F : Type -> Type.
Functor and Monad instances for Free F, along with proofs of the LawfulFunctor and LawfulMonad laws.
Canonical instances of Free with standard effect signatures:

FreeState s for stateful computations, defined via a StateF s functor with get and put operations.
FreeWriter w for logging computations, defined via a WriterF w functor with a tell operation.
FreeCont r for 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 with
final results.

A key insight is that FreeM F satisfies the universal property of free monads: for any monad
M and effect handler f : F → M, there exists a unique way to interpret FreeM F computations
in M that respects the effect semantics given by f. This unique interpreter is liftM f, which
acts 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 directly
pattern-match on the tree structure
2. Canonical interpreters (FreeState.toStateM, FreeWriter.toWriteT, FreeCont.toContT) derived from
the universal property via liftM

And 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

Diff: 280 lines (+210 / -70) across 3 files Reviewer: tannerduve Review duration: 128535m
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
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 39.6% (MEDIUM) · Higher = larger blast radius
39.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 15.7% · Safe to merge
15.7%