-
Notifications
You must be signed in to change notification settings - Fork 43
feat(Control): free monads #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
80d0b23 to
1f7c6f4
Compare
|
@tannerduve This all looks good to me. As you mentioned, in leanprover-community/mathlib4#25491 this has been approved and marked as |
|
@fmontesi This looks good to me, the corresponding Mathlib PR has been closed. |
fmontesi
left a comment
There was a problem hiding this 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.
bd13a7f to
c56a44a
Compare
* 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
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 Typeyou 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:FreeMdefinition withFunctor/Monadinstances andLawfulFunctor/LawfulMonadproofs. Includes canonical interpreterliftMwhich 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 withget/putoperationsFreeWriter w: Writer monad, logging computations withtelloperationFreeCont r: Cont monad, continuation-passing computationsCsLibTests/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.