-
Notifications
You must be signed in to change notification settings - Fork 299
feat(category/fold): foldl
and foldr
for traversable
structures
#376
Conversation
Could we get a brief top-level overview comment for users? In particular, the first half or so of the file is internal implementation stuff--possibly confusing for someone who wants to understand what the module is for.
|
Good catch, thanks! I also forgot the copyright statement. What do you think of:
For the generalization, I'm not sure which direction to go because we could also rewrite most of the |
I'm not sure exactly what the uses of such lemmas would look like, but my guess is that it would generally be easier to turn everything into lists and then apply list lemmas. What kind of lemmas for By the way, one other lemma that would probably be useful is that |
You might be right about turning everything into lists. Especially since I'm starting to doubt that the generalization is provable. One idea is to to use the composition law but it involves two transformations applied on the same structure of type
Me too. The problem with having a type class for |
@rwbarton I've started proving that |
I guess the problem stems from the fact that Maybe a first question to ask is: do you have a good sense of the operational behavior of |
I believe I have a somewhat good intuition of the operational behavior at least of
|
Sorry, the first part of my previous comment didn't make much sense. Let me try again.
Then the other thing one would like to know is that |
Ok, I proved it and it's kind of anticlimactic: I'm using the following applicative: def fold (α : Type u) (β : Type v) := list α |
What does the naturality laws of |
Yes, you can also do it that way and then prove it is equal to the original by using the naturality law (using an applicative transformation from |
Like the list versions. @[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) : foldl f a (map g l) = foldl (λx y, f x (g y)) a l :=
by revert a; induction l; intros; simp *
@[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) : foldr f a (map g l) = foldr (f ∘ g) a l :=
by revert a; induction l; intros; simp * |
The traverse ((foldl.lift ∘ flip (flip cons)) ∘ f) l nil = map f (traverse (foldl.lift ∘ flip (flip cons)) l nil) which feels like a dead end. At first glance, |
I have the following idea for expressing structure both {α : Type u} {β : Type v} (f : α → β → α) :=
(one : α → α)
(two : list β)
(one_eq_two : ∀ a, one a = list.foldl f a two) This has applicative morphisms to both I probably have some of the details wrong, but this approach seems like it might work. |
hamster in wheel in brain running, running, running I think I can get some milage out of that. Thanks! Do you suggest that this new applicative would be an intermediate value in the proof so that |
Nice! I figured out what was bothering me about my |
Thanks for the hints @rwbarton, it worked! It's really beautiful too :) |
I was wondering how much we could erase |
I take back what I said, I don't think |
It works! And removing |
With these results in mind, I think a case could be made for a class |
foldl
and foldr
for traversable
struc…foldl
and foldr
for traversable
structures
category/fold.lean
Outdated
def to_list (xs : t α) : list α := | ||
traverse (foldr.mk ∘ list.cons) xs [] | ||
def to_list : t α → list α := | ||
foldr list.cons [] |
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.
@spl You are truly evil
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.
😕 Why?
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.
I'm joking. Because your initial commit was worded as a massacre and now you're coming back for more??
But seriously, I'm truly grateful for these improvements.
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.
I'm joking. Because your initial commit was worded as a massacre and now you're coming back for more??
Ah. 😆 🔨🔪
But seriously, I'm truly grateful for these improvements.
I'm glad!
This is looking good! I made a few cosmetic changes after @cipher1024. One thing I was wondering was if there was a way to unify the different types used for their def const (α : Type u) (β : Type v) : Type u := α |
This is something I thought about too. This is how it would be done in Haskell. It feels weird to have a multiplicative monoid instance for lists. It's a bit less weird for endofunctions though. |
Also, @cipher1024, could you add a few comments to the |
Yes, good idea! |
We could use |
Even if they only went on the |
30d04f0
to
972315d
Compare
src/category_theory/opposites.lean
Outdated
comp := λ _ _ _ f g, g ≫ f, | ||
id := λ X, 𝟙 X } | ||
instance opposite : category.{v₁} (Cᵒᵖ) := | ||
{ .. opposite.hom } |
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.
@cipher1024 @rwbarton I tried to resolve this with the opposite
PR #627 but now it fails at this point, obviously
doesn't prove the laws for free
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.
I can look into what's going on there, but this PR shouldn't be using the opposite category anyways. Instead it should be using the opposite monoid (#538).
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.
@rwbarton Does that make a difference?
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.
We might end up merging the opposite
types. But the instances added in this PR have nothing to do with categories so they should not be in category_theory
; they belong in algebra
as in #538.
Is the renaming |
Also, if it is intentional, it is better to do this kind of renaming in a separate PR, merge it, and rebase this one. That way this PR will not accumulate conflicts with the renamed files as they are modified by other commits. |
The renaming is intentional because |
Sounds good. Note that I already put the |
Yes, please split this PR into separate parts. |
8c60e8e
to
8b29987
Compare
@cipher1024 I'm looking at it now. 💯 on the documentation and presentation! I currently have just one small concern that has no bearing whatsoever on the acceptance of the PR. I really like the example proofs in the comments, but I don't really like that the example proofs are in comments 😉 because they are not checked. Is it appropriate for |
Properly checked examples sounds good to me. Someday we'll work out how to
get a faster computer for Kenny. :-)
…On Tue, Feb 5, 2019, 6:08 PM Sean Leather ***@***.*** wrote:
@cipher1024 <https://github.com/cipher1024> I'm looking at it now. 💯 on
the documentation and presentation!
I currently have just one small concern that has no bearing whatsoever on
the acceptance of the PR. I really like the example proofs in the comments,
but I don't really like that the example proofs *are in* comments 😉
because they are not checked. Is it appropriate for examples to be here,
or is there another appropriate place for them?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#376 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAdLBFNkXBNunPDU6dQ4OCXVMZSQatlcks5vKS3wgaJpZM4W9hBZ>
.
|
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.
Just a few minor things.
I love the idea! Thanks for suggesting it. The only issue I have is that, if I make those into examples, they won't come up as part of |
The last calculation in the docstring doesn't type check and I feel like what I have to do to get it to type check obscurs the ideas. I think I'm not going to add the examples. I'll remember the idea nonetheless and see if there's a nice way of applying it. |
Let's finally merge this. |
TO CONTRIBUTORS:
Make sure you have:
For reviewers: code review check list