Skip to content

Conversation

@tannerduve
Copy link
Contributor

@tannerduve tannerduve commented Aug 29, 2025

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.

@tannerduve tannerduve requested a review from chenson2018 August 29, 2025 20:31
@tannerduve tannerduve force-pushed the tannerduve/free-monad branch from 80d0b23 to 1f7c6f4 Compare August 29, 2025 20:46
@chenson2018
Copy link
Collaborator

@tannerduve This all looks good to me. As you mentioned, in leanprover-community/mathlib4#25491 this has been approved and marked as maintainer-merge . Before I approve/merge here, can we make sure that is resolved first? I'd like to avoid accidental duplication of code and review effort. Maybe ping the previous Zulip thread and see what folks think of this being in Cslib since there were mixed opinions previously.

@chenson2018
Copy link
Collaborator

@fmontesi This looks good to me, the corresponding Mathlib PR has been closed.

Copy link
Collaborator

@fmontesi fmontesi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see free monads!

A comment I couldn't put in the files view: please move Control/Monad to Foundations/Control/Monad.

@tannerduve tannerduve requested a review from fmontesi September 8, 2025 17:46
@tannerduve tannerduve force-pushed the tannerduve/free-monad branch from bd13a7f to c56a44a Compare September 8, 2025 20:39
@fmontesi fmontesi merged commit 4dce491 into leanprover:main Sep 17, 2025
2 checks passed
fmontesi pushed a commit that referenced this pull request Sep 20, 2025
* create Free.lean

* create Effects.lean

* import Free.lean from Mathlib PR

* import Effects.lean from Mathlib PR

* add test file

* add monad fold

* adding code for fold

* formatting

* adding some comments

* add effect language example in tests

* docstrings and suggestions

* references and file organization

* use LC context

* Move Control/Monad to Foundations/Control/Monad and example to tests
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.

3 participants