product.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*!
  2. @file
  3. Forward declares `boost::hana::Product`.
  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_PRODUCT_HPP
  9. #define BOOST_HANA_FWD_CONCEPT_PRODUCT_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! @ingroup group-concepts
  13. //! @defgroup group-Product Product
  14. //! Represents types that are generic containers of two elements.
  15. //!
  16. //! This concept basically represents types that are like `std::pair`.
  17. //! The motivation for making such a precise concept is similar to the
  18. //! motivation behind the `Sequence` concept; there are many different
  19. //! implementations of `std::pair` in different libraries, and we would
  20. //! like to manipulate any of them generically.
  21. //!
  22. //! Since a `Product` is basically a pair, it is unsurprising that the
  23. //! operations provided by this concept are getting the first and second
  24. //! element of a pair, creating a pair from two elements and other
  25. //! simmilar operations.
  26. //!
  27. //! @note
  28. //! Mathematically, this concept represents types that are category
  29. //! theoretical [products][1]. This is also where the name comes
  30. //! from.
  31. //!
  32. //!
  33. //! Minimal complete definition
  34. //! ---------------------------
  35. //! `first`, `second` and `make`
  36. //!
  37. //! `first` and `second` must obviously return the first and the second
  38. //! element of the pair, respectively. `make` must take two arguments `x`
  39. //! and `y` representing the first and the second element of the pair,
  40. //! and return a pair `p` such that `first(p) == x` and `second(p) == y`.
  41. //! @include example/product/make.cpp
  42. //!
  43. //!
  44. //! Laws
  45. //! ----
  46. //! For a model `P` of `Product`, the following laws must be satisfied.
  47. //! For every data types `X` and `Y`, there must be a unique function
  48. //! @f$ \mathtt{make} : X \times Y \to P @f$ such that for every `x`, `y`,
  49. //! @code
  50. //! x == first(make<P>(x, y))
  51. //! y == second(make<P>(x, y))
  52. //! @endcode
  53. //!
  54. //! @note
  55. //! This law is less general than the universal property typically used to
  56. //! define category theoretical products, but it is vastly enough for what
  57. //! we need.
  58. //!
  59. //! This is basically saying that a `Product` must be the most general
  60. //! object able to contain a pair of objects `(P1, P2)`, but nothing
  61. //! more. Since the categorical product is defined by a universal
  62. //! property, all the models of this concept are isomorphic, and
  63. //! the isomorphism is unique. In other words, there is one and only
  64. //! one way to convert one `Product` to another.
  65. //!
  66. //! Another property that must be satisfied by `first` and `second` is
  67. //! that of @ref move-independence, which ensures that we can optimally
  68. //! decompose a `Product` into its two members without making redundant
  69. //! copies.
  70. //!
  71. //!
  72. //! Refined concepts
  73. //! ----------------
  74. //! 1. `Comparable` (free model)\n
  75. //! Two products `x` and `y` are equal iff they are equal element-wise,
  76. //! by comparing the first element before the second element.
  77. //! @include example/product/comparable.cpp
  78. //!
  79. //! 2. `Orderable` (free model)\n
  80. //! Products are ordered using a lexicographical ordering as-if they
  81. //! were 2-element tuples.
  82. //!
  83. //! 3. `Foldable` (free model)\n
  84. //! Folding a `Product` `p` is equivalent to folding a list containing
  85. //! `first(p)` and `second(p)`, in that order.
  86. //!
  87. //!
  88. //! Concrete models
  89. //! ---------------
  90. //! `hana::pair`
  91. //!
  92. //!
  93. //! [1]: http://en.wikipedia.org/wiki/Product_(category_theory)
  94. template <typename P>
  95. struct Product;
  96. BOOST_HANA_NAMESPACE_END
  97. #endif // !BOOST_HANA_FWD_CONCEPT_PRODUCT_HPP