Skip to content

Free is not a Semiring #11

@hdgarrood

Description

@hdgarrood

A Semiring requires commutative addition, but the addition operation for Free is list concatenation, which is not commutative.

It looks like Free is an encoding of the idea described in the wikipedia article on Semirings

  • Given an alphabet (finite set) Σ, the set of formal languages over Σ (subsets of Σ∗) is a semiring with product induced by string concatenation and addition as the union of languages (i.e. simply union as sets). The zero of this semiring is the empty set (empty language) and the semiring's unit is the language containing as its sole element the empty string.[12]
  • Generalising the previous example (by viewing Σ∗ as the free monoid over Σ), take M to be any monoid; the power set P M of all subsets of M forms a semiring under set-theoretic union as addition and set-wise multiplication.

If this is what is desired, perhaps a better encoding would be

instance Monoid m => Semiring (Set m) where
  zero = Set.empty
  add = Set.union
  one = Set.singleton mempty 
  mul x y = Set.fromFoldable $ lift2 (<>) (Set.toUnfoldable x) (Set.toUnfoldable y :: Array m)

or a newtype over this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs more infoThis issue needs more info before any action can be done.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions