monad_plus.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*!
  2. @file
  3. Forward declares `boost::hana::MonadPlus`.
  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_MONAD_PLUS_HPP
  9. #define BOOST_HANA_FWD_CONCEPT_MONAD_PLUS_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! @ingroup group-concepts
  13. //! @defgroup group-MonadPlus MonadPlus
  14. //! The `MonadPlus` concept represents Monads with a monoidal structure.
  15. //!
  16. //! Intuitively, whereas a Monad can be seen as some kind of container
  17. //! or context, a MonadPlus can be seen as a container or a context that
  18. //! can be concatenated with other containers or contexts. There must
  19. //! also be an identity element for this combining operation. For example,
  20. //! a tuple is a MonadPlus, because tuples can be concatenated and the
  21. //! empty tuple would act as an identity for concatenation. How is this
  22. //! different from a Monad which is also a Monoid? The answer is that the
  23. //! monoidal structure on a MonadPlus must _not_ depend of the contents
  24. //! of the structure; it must not require the contents to be a Monoid
  25. //! in order to work.
  26. //!
  27. //! While sequences are not the only possible model for MonadPlus, the
  28. //! method names used here refer to the MonadPlus of sequences under
  29. //! concatenation. Several useful functions generalizing operations on
  30. //! sequences are included with this concept, like `append`, `prepend`
  31. //! and `filter`.
  32. //!
  33. //! @note
  34. //! This documentation does not go into much details about the nature
  35. //! of the MonadPlus concept. However, there is a nice Haskell-oriented
  36. //! [WikiBook][1] going into further details.
  37. //!
  38. //!
  39. //! Minimal complete definition
  40. //! ---------------------------
  41. //! `concat` and `empty`
  42. //!
  43. //!
  44. //! Laws
  45. //! ----
  46. //! First, a MonadPlus is required to have a monoidal structure. Hence, it
  47. //! is no surprise that for any MonadPlus `M`, we require `M(T)` to be a
  48. //! valid monoid. However, we do not enforce that `M(T)` actually models
  49. //! the Monoid concept provided by Hana. Further, for all objects `a, b, c`
  50. //! of data type `M(T)`,
  51. //! @code
  52. //! // identity
  53. //! concat(empty<M(T)>(), a) == a
  54. //! concat(a, empty<M(T)>()) == a
  55. //!
  56. //! // associativity
  57. //! concat(a, concat(b, c)) == concat(concat(a, b), c)
  58. //! @endcode
  59. //!
  60. //! Secondly, a MonadPlus is also required to obey the following laws,
  61. //! which represent the fact that `empty<M(T)>()` must be some kind of
  62. //! absorbing element for the `chain` operation. For all objects `a` of
  63. //! data type `M(T)` and functions @f$ f : T \to M(U) @f$,
  64. //! @code
  65. //! chain(empty<M(T)>(), f) == empty<M(U)>()
  66. //! chain(a, always(empty<M(T)>())) == empty<M(U)>()
  67. //! @endcode
  68. //!
  69. //!
  70. //! Refined concepts
  71. //! ----------------
  72. //! `Functor`, `Applicative` and `Monad`
  73. //!
  74. //!
  75. //! Concrete models
  76. //! ---------------
  77. //! `hana::optional`, `hana::tuple`
  78. //!
  79. //! [1]: https://en.wikibooks.org/wiki/Haskell/MonadPlus
  80. template <typename M>
  81. struct MonadPlus;
  82. BOOST_HANA_NAMESPACE_END
  83. #endif // !BOOST_HANA_FWD_CONCEPT_MONAD_PLUS_HPP