13

Does boost, or any other common C++ library, provide semiring or monoid abstractions (such as a template class)?

I have some algorithms that I would like to express in terms of these abstract structures, but so far I haven't come across anything. I can write my own, but ideally these would be in a library I'm already using such as boost.

Thanks!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
  • 1
    Wow, I thought I'd never hear these words applied to real-life problems, assuming that they were reserved for torturing students in colleges. +1 for that :) – Sergey Kalinichenko Apr 10 '13 at 00:29
  • @dasblinkenlight One of the algorithms I want to implement is written in Cormen et al's Algorithms book in terms of semirings and monoids :) – Jason Dagit Apr 10 '13 at 00:34
  • Ah, these guys... Their flair for understatement is visible in naming their book "Introduction to algorithms" rather than "All most of you would ever need to know about algorithms" :):):) – Sergey Kalinichenko Apr 10 '13 at 00:38
  • 1
    @dasblinkenlight the monoid concept also pops up in parallel versions of `std::accumulate`, as for example Cilk reducer hyper objects (by Leiserson, one of those guys) – TemplateRex Apr 10 '13 at 07:14

3 Answers3

11

SGI STL has MonoidOperation concept. For instance power function is implemented for MonoidOperation.

Boost.Graph library also defines Monoid concept.

In addition to already suggested Elements of Programming you may look to Notes on Programming by Alexander Stepanov (one of authors of EoP). Notes are freely available, and have some overlap with EoP book.

There are style difference between EoP and Notes - EoP is very terse like math textbook, but Notes are more "informal" - there some small stories, etc.

By the way, both have some discussion of referenced above power function implementation.

P.S. there are great talks by Alexander Stepanov:

P.P.S. Collected Papers of Alexander A. Stepanov

Community
  • 1
  • 1
Evgeny Panasyuk
  • 9,076
  • 1
  • 33
  • 54
7

To the best of my knowledge the C++ standard library doesn't have any abstractions around these structures. However, Alex Stepanov, inventor of the STL, wrote a book entitled Elements of Programming in which he writes a variety of useful functions that operate on monoids, groups, binary operators, unary functions, etc. It's not necessarily a standard, but it might be a good starting point for further exploration.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
3

Boost.Operators provides a convenient way to define groups of arithmetic operators for a class.

Pre-defined concepts (syntax duck-typing only) include ring, ordered ring, euclidean ring, orderered euclidean ring, field and ordered field. You should be able to define your own classes for semi-ring or monoid by deriving from the appropriate groups of operator classes.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • These are not concepts, but rather helpers for "groups of arithmetic operators" as you said. Concept would include things like Additive_Identity, Multiplicative_Identity, Additive_Inverse, Multiplicative_Inverse, etc. – Evgeny Panasyuk Apr 10 '13 at 07:32
  • @EvgenyPanasyuk tnx, it indeed appears that boost.operators don't provide identity elements. AFAICS, it should be possible to use the same `friend` injection trick for them. E.g. let `templateclass MultId: field {};` and define mixed `operator*` versions to reflect commutativity. – TemplateRex Apr 10 '13 at 07:50
  • Concepts are focused on interface, i.e. which syntax is legal, and which semantic it have. That's allow to do implementation of algorithms (and other stuff) abstracted from concrete models. But Boost.Operators are just helpers which aids implementation of concrete models. – Evgeny Panasyuk Apr 10 '13 at 07:56
  • @EvgenyPanasyuk tnx, updated answer with clarification about concept duck typing – TemplateRex Apr 10 '13 at 08:15