Skip to content

sum and product for lists #1298

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
mechvel opened this issue Sep 26, 2020 · 3 comments
Closed

sum and product for lists #1298

mechvel opened this issue Sep 26, 2020 · 3 comments
Labels
status: duplicate The main contents of the issue or PR already exists in another issue or PR.

Comments

@mechvel
Copy link
Contributor

mechvel commented Sep 26, 2020

Data.List.Base has sum and product for lists over Nat.

Probably it is better to replace this with a generic design:

module Algebra.Properties.Monoid  ... where
C = Carrier

Π₁ : C → List C → C   -- product of a nonempty list of elements                 
Π₁ x = foldr _∙_ x                                                              
                                                                                  
Π : List C → C         -- product of a list of elements                
Π = Π₁ ε                                                                        
    
----------------------------------------
module Algebra.Properties.Semiring ... where
...
sum1 : C → List C → C                                                           
sum1 = Algebra.Properties.Monoid.Π₁  +-0-monoid                                                            
                                                                                  
sum : List C → C                       
sum = Algebra.Properties.Monoid.Π  +-0-monoid 

with adding to Algebra.Properties the needed modules for Monoid and Semiring.

?

@gallais
Copy link
Member

gallais commented Sep 26, 2020

cf. #1281

@gallais gallais added the status: duplicate The main contents of the issue or PR already exists in another issue or PR. label Sep 26, 2020
@mechvel
Copy link
Contributor Author

mechvel commented Sep 26, 2020

I have an impression that #1281 does not relate to what I am suggesting.
In particular, #1281 starts with "List is a free semigroup". Probably the author means that (List A) it isomorphic to the domain of words over A with the operation of the word concatenation, and the latter domain is a free semigroup.

But I do not see its relevance to my simple suggestion.
Do you?
I suggest to have the product function for a list (calling it Π ) over any Monoid instead of only for list over Nat.
And to have sum via Π in +-0-monoid in Semiring.

@JacquesCarette
Copy link
Contributor

That's exactly what fold : (M : Monoid) → List (Monoid.Carrier M) → Monoid.Carrier M is. You can define sum and product as 1-liners from there, by specialization of the Monoid argument.

@gallais gallais closed this as completed Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate The main contents of the issue or PR already exists in another issue or PR.
Projects
None yet
Development

No branches or pull requests

3 participants