Skip to content

sum as an accumulating fold; sum-++ by way of fold-++ #1712

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

Closed
wants to merge 1 commit into from

Conversation

jamesmckinna
Copy link
Contributor

@jamesmckinna jamesmckinna commented Feb 11, 2022

At present, Data.Vec.Base.sum specialises the base case of its defining fold to be 0.
This toy/low-hanging fruit PR introduces,
in Data.Vec.Base:

  • fold-sum n xs with accumulating parameter n
  • sum = fold-sum 0

and in Data.Vec.Properties:

  • fold-sum n xs ≡ sum xs + n
    and then a new (equivalent) proof of
  • sum-++ using foldr-++ and the above lemma

@MatthewDaggitt
Copy link
Contributor

Hmm so I have to confess I don't quite see the advantage of writing fold-sum n xs over sum xs + n?

@jamesmckinna
Copy link
Contributor Author

Hmmm... fair comment, except: it irked me (what a world, where my pet peeves become PRs ;-)) that the the proof of sum,-++ appears to be an opportunistic property of sum, rather than a generic property of folds, cashed out in a specific instance...
... but to then to be able exploit that genericity, one needs an accumulator-style presentation of sum... fold-sum (terrible name), of which sum itself then becomes an instance. The function fold-sum, and its characteristic property, are all and only to mediate between sum-as-special-instance, and sum-as-generic-fold-with-accumulator. But the two expressions you identify above are not defintionally equal*, only provably so.

So there's a systematisation pay-off, I suppose, on the one hand, at the cost of a lemma factorisation on the other. Potayto, potahto.

@MatthewDaggitt
Copy link
Contributor

MatthewDaggitt commented Feb 14, 2022

Hmm, I kind of see where you're coming from, but I think fold-sum as a function is unlikely to be a useful addition in its own right. If you're concerned about making the proof more general, I think better to generalise even further to something like the following lemma about fold:

foldr-extractBase : (M : CommutativeMonoid a l) (open CommutativeMonoid M) →
                    ∀ (e xs : Vec ℕ m) → foldr _∙_ e xs ≡ foldr ε xs + e

@jamesmckinna
Copy link
Contributor Author

Interesting comment! And related (perhaps) to @Taneb 's PR #1281 ... for a more comprehensive treatment?

@jamesmckinna
Copy link
Contributor Author

But for now, suggest we label this one status: abandoned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition status: won't-merge Decided against merging the PR in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants