comparable.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*!
  2. @file
  3. Forward declares `boost::hana::Comparable`.
  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_COMPARABLE_HPP
  9. #define BOOST_HANA_FWD_CONCEPT_COMPARABLE_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! @ingroup group-concepts
  13. //! @defgroup group-Comparable Comparable
  14. //! The `Comparable` concept defines equality and inequality.
  15. //!
  16. //! Intuitively, `Comparable` objects must define a binary predicate named
  17. //! `equal` that returns whether both objects represent the same abstract
  18. //! value. In other words, `equal` must check for deep equality. Since
  19. //! "representing the same abstract value" is difficult to express
  20. //! formally, the exact meaning of equality is partially left to
  21. //! interpretation by the programmer with the following guidelines:\n
  22. //! 1. Equality should be compatible with copy construction; copy
  23. //! constructing a value yields an `equal` value.
  24. //! 2. Equality should be independent of representation; an object
  25. //! representing a fraction as `4/8` should be `equal` to an object
  26. //! representing a fraction as `2/4`, because they both represent
  27. //! the mathematical object `1/2`.
  28. //!
  29. //! Moreover, `equal` must exhibit properties that make it intuitive to
  30. //! use for determining the equivalence of objects, which is formalized
  31. //! by the laws for `Comparable`.
  32. //!
  33. //!
  34. //! Minimal complete definition
  35. //! ---------------------------
  36. //! 1. `equal`\n
  37. //! When `equal` is defined, `not_equal` is implemented by default as its
  38. //! complement. For all objects `x`, `y` of a `Comparable` tag,
  39. //! @code
  40. //! not_equal(x, y) == not_(equal(x, y))
  41. //! @endcode
  42. //!
  43. //!
  44. //! Laws
  45. //! ----
  46. //! `equal` must define an [equivalence relation][1], and `not_equal` must
  47. //! be its complement. In other words, for all objects `a`, `b`, `c` with
  48. //! a `Comparable` tag, the following must hold:
  49. //! @code
  50. //! equal(a, a) // Reflexivity
  51. //! if equal(a, b) then equal(b, a) // Symmetry
  52. //! if equal(a, b) && equal(b, c) then equal(a, c) // Transitivity
  53. //! not_equal(a, b) is equivalent to not_(equal(a, b))
  54. //! @endcode
  55. //!
  56. //!
  57. //! Concrete models
  58. //! ---------------
  59. //! `hana::integral_constant`, `hana::map`, `hana::optional`, `hana::pair`,
  60. //! `hana::range`, `hana::set`, `hana::string`, `hana::tuple`,
  61. //! `hana::type`
  62. //!
  63. //!
  64. //! Free model for `EqualityComparable` data types
  65. //! ----------------------------------------------
  66. //! Two data types `T` and `U` that model the cross-type EqualityComparable
  67. //! concept presented in [N3351][2] automatically model the `Comparable`
  68. //! concept by setting
  69. //! @code
  70. //! equal(x, y) = (x == y)
  71. //! @endcode
  72. //! Note that this also makes EqualityComparable types in the
  73. //! [usual sense][3] models of `Comparable` in the same way.
  74. //!
  75. //!
  76. //! Equality-preserving functions
  77. //! -----------------------------
  78. //! Let `A` and `B` be two `Comparable` tags. A function @f$f : A \to B@f$
  79. //! is said to be equality-preserving if it preserves the structure of the
  80. //! `Comparable` concept, which can be rigorously stated as follows. For
  81. //! all objects `x`, `y` of tag `A`,
  82. //! @code
  83. //! if equal(x, y) then equal(f(x), f(y))
  84. //! @endcode
  85. //! Equivalently, we simply require that `f` is a function in the usual
  86. //! mathematical sense. Another property is [injectivity][4], which can be
  87. //! viewed as being a "lossless" mapping. This property can be stated as
  88. //! @code
  89. //! if equal(f(x), f(y)) then equal(x, y)
  90. //! @endcode
  91. //! This is equivalent to saying that `f` maps distinct elements to
  92. //! distinct elements, hence the "lossless" analogy. In other words, `f`
  93. //! will not collapse distinct elements from its domain into a single
  94. //! element in its image, thus losing information.
  95. //!
  96. //! These functions are very important, especially equality-preserving
  97. //! ones, because they allow us to reason simply about programs. Also
  98. //! note that the property of being equality-preserving is taken for
  99. //! granted in mathematics because it is part of the definition of a
  100. //! function. We feel it is important to make the distinction here
  101. //! because programming has evolved differently and as a result
  102. //! programmers are used to work with functions that do not preserve
  103. //! equality.
  104. //!
  105. //!
  106. //! Cross-type version of the methods
  107. //! ---------------------------------
  108. //! The `equal` and `not_equal` methods are "overloaded" to handle
  109. //! distinct tags with certain properties. Specifically, they are
  110. //! defined for _distinct_ tags `A` and `B` such that
  111. //! 1. `A` and `B` share a common tag `C`, as determined by the
  112. //! `common` metafunction
  113. //! 2. `A`, `B` and `C` are all `Comparable` when taken individually
  114. //! 3. @f$ \mathtt{to<C>} : A \to C @f$ and @f$\mathtt{to<C>} : B \to C@f$
  115. //! are both equality-preserving and injective (i.e. they are embeddings),
  116. //! as determined by the `is_embedding` metafunction.
  117. //!
  118. //! The method definitions for tags satisfying the above properties are
  119. //! @code
  120. //! equal(x, y) = equal(to<C>(x), to<C>(y))
  121. //! not_equal(x, y) = not_equal(to<C>(x), to<C>(y))
  122. //! @endcode
  123. //!
  124. //!
  125. //! Important note: special behavior of `equal`
  126. //! -------------------------------------------
  127. //! In the context of programming with heterogeneous values, it is useful
  128. //! to have unrelated objects compare `false` instead of triggering an
  129. //! error. For this reason, `equal` adopts a special behavior for
  130. //! unrelated objects of tags `T` and `U` that do not satisfy the above
  131. //! requirements for the cross-type overloads. Specifically, when `T` and
  132. //! `U` are unrelated (i.e. `T` can't be converted to `U` and vice-versa),
  133. //! comparing objects with those tags yields a compile-time false value.
  134. //! This has the effect that unrelated objects like `float` and
  135. //! `std::string` will compare false, while comparing related objects that
  136. //! can not be safely embedded into the same super structure (like
  137. //! `long long` and `float` because of the precision loss) will trigger a
  138. //! compile-time assertion. Also note that for any tag `T` for which the
  139. //! minimal complete definition of `Comparable` is not provided, a
  140. //! compile-time assertion will also be triggered because `T` and `T`
  141. //! trivially share the common tag `T`, which is the expected behavior.
  142. //! This design choice aims to provide more flexibility for comparing
  143. //! objects, while still rejecting usage patterns that are most likely
  144. //! programming errors.
  145. //!
  146. //!
  147. //! [1]: http://en.wikipedia.org/wiki/Equivalence_relation#Definition
  148. //! [2]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3351.pdf
  149. //! [3]: http://en.cppreference.com/w/cpp/named_req/EqualityComparable
  150. //! [4]: http://en.wikipedia.org/wiki/Injective_function
  151. template <typename T>
  152. struct Comparable;
  153. BOOST_HANA_NAMESPACE_END
  154. #endif // !BOOST_HANA_FWD_CONCEPT_COMPARABLE_HPP