Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Commutative monoid #7

Open
tonyhffong opened this issue Jan 21, 2015 · 1 comment
Open

Commutative monoid #7

tonyhffong opened this issue Jan 21, 2015 · 1 comment

Comments

@tonyhffong
Copy link
Collaborator

I'm thinking how to express a commutative monoid using this framework.

For a monoid, I can do this:

@traitdef Monoid{X} begin
    mempty( ::X ) -> X
    mappend( X, X ) -> X
end

immutable Sum{T<:Number}
    value::T
end

mempty{T<:Number}( ::Type{Sum{T}} ) = Sum{T}( zero(T) )
mappend{T<:Number}( a::Sum{T}, b::Sum{T} ) = Sum{T}(a.value+b.value)

At the interface level, a commutative monoid is indistinguishable from a regular monoid. The only difference is an assertion that the mappend function for X commutes.

An approach is to whitelist X this way:

@traitdef CommutativeMonoid{X} <: Monoid{X} begin
    @constraints begin
        X <: Sum ||
        X <: Product ||
        X <: MeanWeighted
    end
end

or via some registration function so that we can add to the whitelist when packages introduce new types. It has a touch of code smell, so I want to create this issue for comments and ideas.

@mauro3
Copy link
Owner

mauro3 commented Apr 1, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants