← Back to Pull requests

feat(Control): free monads #53

Closed
unknown closed this pull request without merging on Sep 17, 2025 12:08 UTC
View on GitHub
unknown unknown commented on Aug 29, 2025 06:13 UTC
Author

This PR introduces the Free monad construction for representing effectful computations as syntax trees, enabling clean separation between program syntax and interpretation semantics. Given a type constructor F : Type ->T Type you get a monad "for free".

Implementation: Uses the "freer monad" approach since the traditional free monad is not strictly positive and thus fails termination checking. The FreeM F α type represents computation trees where nodes (liftBind) are effect requests with continuations, and leaves (pure) are final values.

Core contributions

  • Cslib/Foundations/Control/Monad/Free.lean: FreeM definition with Functor/Monad instances and LawfulFunctor/LawfulMonad proofs. Includes canonical interpreter liftM which is the universal morphism that uniquely defines the free monad.
  • Cslib/Foundations/Control/Monad/Free/Fold.lean: Fold function on the free monad, and uniqueness proof of the universal property of "the unique morphism from an initial algebra" (catamorphism).
  • Cslib/Foundations/Control/Monad/Free/Effects.lean: Standard monad implementations as free monads:

  • - FreeState s: State monad with get/put operations
    - FreeWriter w: Writer monad, logging computations with tell operation
    - FreeCont r: Cont monad, continuation-passing computations

  • CsLibTests/FreeMonad.lean: Complete verified interpreter for an expression language with state, errors, and tracing. Shows how multiple effects can be combined using sum types without monad transformers. Includes big-step operational semantics and correctness proof relating the fold-based interpreter to the semantics.
  • Applications


    Embedded DSLs, compilers and interpreters, program analysis, formalizing cryptography, formalizing computational complexity
    This enables building interpreters where syntax (what effects to perform) is cleanly separated from semantics (how to interpret those effects), supporting multiple interpretations of the same programs.

    Diff: 261 lines (+195 / -66) across 3 files Reviewer: tannerduve Review duration: 27714m
    All Vouch review confidence checks passed
    Residual risk 0.15 = Change Risk (0.39) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 261 lines diff across 3 files
    0.39
    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.333
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #53"
    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.2% (MEDIUM) · Higher = larger blast radius
    39.2%
    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.5% · Safe to merge
    15.5%