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

Commutative monoid #7

@tonyhffong

Description

@tonyhffong

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions