feat: add a predicate for monad morphisms - #856
Conversation
We show that various list operations are monad morphisms, and that FreeM.liftM is.
a3cbe56 to
2798f88
Compare
These can be used to: * replace the specialized timeM version * implement sorts that log as they sort * specialize to FreeM or PFunctor.FreeM
The generic List.orderedInsertM/insertionSortM commute with any monad morphism, stated with the IsMonadHom laws of leanprover#856 inlined and needing no lawfulness on either side. Since evaluation against an oracle is a monad morphism to Id, the executable Id instantiation is List.insertionSort with no separate proof about the generic definition, and the framework's complexity bounds apply to the generic program definitionally. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pxy48TaP92UgEq28KGm8BG
| namespace IsFunctorHom | ||
| variable {m n p : Type _ → Type _} [Functor m] [Functor n] [Functor p] | ||
|
|
||
| attribute [grind .] map_map map_mapConst |
There was a problem hiding this comment.
attribute [grind .] map_map map_mapConst
Maybe these grind patterns work better?
-- namespace IsFunctorHom
grind_pattern map_map => IsFunctorHom m n f, g <$> x
grind_pattern map_mapConst => IsFunctorHom m n f, Functor.mapConst a x
-- namespace IsApplicativeHom
grind_pattern map_pure => IsApplicativeHom m n f, (pure a : m α)
grind_pattern map_seq => IsApplicativeHom m n f, Seq.seq x y
grind_pattern map_seqLeft => IsApplicativeHom m n f, SeqLeft.seqLeft x y
grind_pattern map_seqRight => IsApplicativeHom m n f, SeqRight.seqRight x y
-- namespace IsMonadHom
grind_pattern map_bind => IsMonadHom m n f, x >>= y
-- namespace IsAlternativeHom
grind_pattern map_failure => IsAlternativeHom m n f, (Alternative.failure : m α)
grind_pattern map_orElse => IsAlternativeHom m n f, HOrElse.hOrElse x y
There was a problem hiding this comment.
How do these compare to the patterns I already have? Can I print out the pattern generated by grind_pattern to compare with grind??
There was a problem hiding this comment.
set_option trace.grind.ematch.pattern true
There was a problem hiding this comment.
In particular grind . produces:
map_map: [@Functor.map m _ α β g x, @Functor.map n _ _ _ g _, IsFunctorHom _ _ _ _ f]
map_pure: [@pure m _ α a, @pure n _ _ a, IsApplicativeHom _ _ _ _ f]
map_bind: [@bind m _ α β x k, @bind n _ _ _ _ _, IsMonadHom _ _ _ _ f]
but the proposed grind_patterns give:
map_map: [IsFunctorHom m n f, @Functor.map _ _ α β g x]
map_pure: [IsApplicativeHom m n f, @pure _ _ α a]
map_bind: [IsMonadHom m n f, @bind _ _ α β x k]
There was a problem hiding this comment.
On second thoughts, maybe the proposed ones are too aggressive anyway. Without some actual proofs to try these out on, maybe it is premature optimization.
There was a problem hiding this comment.
I wasn't able to get either spelling to help with the proofs, so have left this as is for now.
…o eric-wieser/mergeM
Co-authored-by: Kim Morrison <477956+kim-em@users.noreply.github.com>
We show that various list operations are preserved under monad morphisms, and that FreeM.liftM is.
Note that PolyFun already has the bundled version, but having the unbundled version now does not preclude adding the bundled version later.