feat(Control/Monad): Weakest preconditions and mcvgen for free monads#604
Open
tannerduve wants to merge 2 commits into
Open
feat(Control/Monad): Weakest preconditions and mcvgen for free monads#604tannerduve wants to merge 2 commits into
tannerduve wants to merge 2 commits into
Conversation
d3d44c8 to
d3b6959
Compare
eric-wieser
reviewed
May 28, 2026
d3b6959 to
a00790f
Compare
eric-wieser
reviewed
May 28, 2026
Add `Cslib.Foundations.Control.Monad.Free.WP`: a weakest-precondition framework for `FreeM`-based effectful programs via Std.Do's predicate-transformer monad `PredTrans ps`. A logical effect handler `LHandler F ps` lifts through the universal property of `FreeM` to a unique monad morphism `wpH H : FreeM F → PredTrans ps`. Structural rules (`wpH_pure`, `wpH_lift`, `wpH_bind`) follow from `liftM` being a monad morphism, and the adequacy theorem `wpH_ofInterp_eq_wp_liftM` identifies WP-via-handler with Std.Do's WP of the `liftM` interpretation. Handlers and @[spec] lemmas for the existing `StateF` and `ReaderF` effects let `mvcgen` step through `FreeState`/`FreeReader` programs. `CslibTests/FreeMonadWP` demonstrates: `FreeState` triples, a custom counter effect, `FailF` and `DemonicF` effect signatures with logical handlers, sum composition via `LHandler.sum`, and multi-effect triples discharged by `mvcgen`. Follows Vistrup–Sammler–Jung, *Program Logics à la Carte* (POPL 2025), adapted from coinductive ITrees to inductive `FreeM` and from Iris to Std.Do.
a00790f to
07774bb
Compare
eric-wieser
reviewed
May 28, 2026
eric-wieser
reviewed
May 28, 2026
eric-wieser
reviewed
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds
Cslib/Foundations/Control/Monad/Free/WP.lean, equippingFreeM-based monads with aStd.Do.WPinterpretation and hooking them intoStd.Do.Tripleandmvcgen.A logical handler
LHandler F psassigns each operation of the effect signatureFaPredTrans psspecification. SincePredTrans psis a monad andFreeMis free, the universal property ofFreeMextends a handler uniquely to a monad morphismFreeM F → PredTrans ps. This morphism,wpH H = liftM H.run, is the weakest precondition and is compositional wrt the monadic structure.A
HasHandler F pstypeclass selects a default handler forFand inducesWP (FreeM F) psandWPMonad (FreeM F) psinstances, so thatStd.Do.Tripleandmvcgenapply toFreeM Fprograms.Handlers are provided for cslib's existing
FreeStateandFreeReader, along with@[spec]lemmasSpec.get_FreeState,Spec.set_FreeState, andSpec.read_FreeReaderthat letmvcgenstep through state and reader programs.CslibTests/FreeMonadWP.leancontains example Triples discharged bymvcgen, covering the supplied state and reader handlers, and multiple custom effectsThe design follows Vistrup, Sammler, Jung, Program Logics à la Carte (POPL 2025), which uses coinductive free monads (ITrees) and Iris separation logic. Here we use inductive free monads and
Std.Doprogram logic.