-
Notifications
You must be signed in to change notification settings - Fork 116
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
feat: Laws for monads with Alternative
instances.
#1152
base: main
Are you sure you want to change the base?
Conversation
Mathlib CI status (docs):
|
I wouldn't immediately go for defining class LawfulAlternative (m : Type u → Type v) [Alternative m] extends LawfulApplicative m where
map_failure {α β : Type u} (f : α → β) : f <$> (failure : m α) = failure
failure_seq {α β : Type u} (x : m α) : (failure : m (α → β)) <*> x = failure
-- ...
export LawfulAlternative (map_failure failure_seq)
class AlternativeMonad (m : Type u → Type v) extends Alternative m, Monad m
theorem failure_bind [AlternativeMonad m] [LawfulMonad m] [LawfulAlternative m] (x : α → m β) : failure >>= x = failure := by
conv => lhs; rw [← map_failure PEmpty.elim]
conv => rhs; rw [← bind_pure failure]
conv => rhs; rw [← map_failure PEmpty.elim]
simp only [bind_map_left]
congr 1; funext a
exact a.elim |
@Rob23oba updated to that definition for the class, using |
What's the procedure for updating mathlib after upstreaming these |
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
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.
LGTM, thanks!
You should get a mathlib adaption PR ready to go before this gets merged, so as not to hold up merging other PRs.
This needs a Mathlib adaptation before merging. Ideally, this just involves pushing changes to the Mathlib branch batteries-pr-testing-1152 until it works. Let us know if you have questions of if you run into complications. |
Thanks! Could you create an actual Mathlib PR out of batteries-pr-testing-1152. I think this needs a review on the Mathlib end. You may have to ping someone to review it. Ask @eric-wieser if they are interested or if they have alternate recommendations for that review. Once that's done, ping back here since Batteries maintainers won't be notified automatically. |
I'd be happy to review it |
Sorry about the slow response. This PR has the needed changes. Should I change the mathlib lakefile back to main batteries repo before this gets merged here? |
@fgdorais, I'm happy with the adaptation PR, just the stray comment to delete above. |
I made a few style changes because Batteries prefers to avoid section variables and also makes systematic use of autoimplicits. Please have a quick look at my changes in case there are instances where Lean doesn't automatically make the right choice. |
This PR adds type-classes
AlternativeMonad
,LawfulAlternative
, andLawfulAlternativeLift
that enforce laws regardingfailure
andorElse
.Lemmas about
OptionT
are also upstreamed from mathlib to use in theLawfulAlternative
proof.OptionT.run_bind
was changed as part of this move to useOption.elimM
rather than explicit pattern matching in the lemma.Additional instances for
Set
andList
need to be written to mathlib as that is where the main monad instances for those types are.