monoid.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*!
  2. @file
  3. Forward declares `boost::hana::Monoid`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_CONCEPT_MONOID_HPP
  9. #define BOOST_HANA_FWD_CONCEPT_MONOID_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! @ingroup group-concepts
  13. //! @defgroup group-Monoid Monoid
  14. //! The `Monoid` concept represents data types with an associative
  15. //! binary operation that has an identity.
  16. //!
  17. //! Specifically, a [Monoid][1] is a basic algebraic structure typically
  18. //! used in mathematics to construct more complex algebraic structures
  19. //! like `Group`s, `Ring`s and so on. They are useful in several contexts,
  20. //! notably to define the properties of numbers in a granular way. At its
  21. //! core, a `Monoid` is a set `S` of objects along with a binary operation
  22. //! (let's say `+`) that is associative and that has an identity in `S`.
  23. //! There are many examples of `Monoid`s:
  24. //! - strings with concatenation and the empty string as the identity
  25. //! - integers with addition and `0` as the identity
  26. //! - integers with multiplication and `1` as the identity
  27. //! - many others...
  28. //!
  29. //! As you can see with the integers, there are some sets that can be
  30. //! viewed as a monoid in more than one way, depending on the choice
  31. //! of the binary operation and identity. The method names used here
  32. //! refer to the monoid of integers under addition; `plus` is the binary
  33. //! operation and `zero` is the identity element of that operation.
  34. //!
  35. //!
  36. //! Minimal complete definition
  37. //! ---------------------------
  38. //! `plus` and `zero` satisfying the laws
  39. //!
  40. //!
  41. //! Laws
  42. //! ----
  43. //! For all objects `x`, `y` and `z` of a `Monoid` `M`, the following
  44. //! laws must be satisfied:
  45. //! @code
  46. //! plus(zero<M>(), x) == x // left zero
  47. //! plus(x, zero<M>()) == x // right zero
  48. //! plus(x, plus(y, z)) == plus(plus(x, y), z) // associativity
  49. //! @endcode
  50. //!
  51. //!
  52. //! Concrete models
  53. //! ---------------
  54. //! `hana::integral_constant`
  55. //!
  56. //!
  57. //! Free model for non-boolean arithmetic data types
  58. //! ------------------------------------------------
  59. //! A data type `T` is arithmetic if `std::is_arithmetic<T>::%value` is
  60. //! true. For a non-boolean arithmetic data type `T`, a model of `Monoid`
  61. //! is automatically defined by setting
  62. //! @code
  63. //! plus(x, y) = (x + y)
  64. //! zero<T>() = static_cast<T>(0)
  65. //! @endcode
  66. //!
  67. //! > #### Rationale for not making `bool` a `Monoid` by default
  68. //! > First, it makes no sense whatsoever to define an additive `Monoid`
  69. //! > over the `bool` type. Also, it could make sense to define a `Monoid`
  70. //! > with logical conjunction or disjunction. However, C++ allows `bool`s
  71. //! > to be added, and the method names of this concept really suggest
  72. //! > addition. In line with the principle of least surprise, no model
  73. //! > is provided by default.
  74. //!
  75. //!
  76. //! Structure-preserving functions
  77. //! ------------------------------
  78. //! Let `A` and `B` be two `Monoid`s. A function `f : A -> B` is said
  79. //! to be a [Monoid morphism][2] if it preserves the monoidal structure
  80. //! between `A` and `B`. Rigorously, for all objects `x, y` of data
  81. //! type `A`,
  82. //! @code
  83. //! f(plus(x, y)) == plus(f(x), f(y))
  84. //! f(zero<A>()) == zero<B>()
  85. //! @endcode
  86. //! Functions with these properties interact nicely with `Monoid`s, which
  87. //! is why they are given such a special treatment.
  88. //!
  89. //!
  90. //! [1]: http://en.wikipedia.org/wiki/Monoid
  91. //! [2]: http://en.wikipedia.org/wiki/Monoid#Monoid_homomorphisms
  92. template <typename M>
  93. struct Monoid;
  94. BOOST_HANA_NAMESPACE_END
  95. #endif // !BOOST_HANA_FWD_CONCEPT_MONOID_HPP