Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:58

0001 /*!
0002 @file
0003 Forward declares `boost::hana::Comparable`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_FWD_CONCEPT_COMPARABLE_HPP
0011 #define BOOST_HANA_FWD_CONCEPT_COMPARABLE_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 
0015 
0016 namespace boost { namespace hana {
0017     //! @ingroup group-concepts
0018     //! @defgroup group-Comparable Comparable
0019     //! The `Comparable` concept defines equality and inequality.
0020     //!
0021     //! Intuitively, `Comparable` objects must define a binary predicate named
0022     //! `equal` that returns whether both objects represent the same abstract
0023     //! value. In other words, `equal` must check for deep equality. Since
0024     //! "representing the same abstract value" is difficult to express
0025     //! formally, the exact meaning of equality is partially left to
0026     //! interpretation by the programmer with the following guidelines:\n
0027     //! 1. Equality should be compatible with copy construction; copy
0028     //!    constructing a value yields an `equal` value.
0029     //! 2. Equality should be independent of representation; an object
0030     //!    representing a fraction as `4/8` should be `equal` to an object
0031     //!    representing a fraction as `2/4`, because they both represent
0032     //!    the mathematical object `1/2`.
0033     //!
0034     //! Moreover, `equal` must exhibit properties that make it intuitive to
0035     //! use for determining the equivalence of objects, which is formalized
0036     //! by the laws for `Comparable`.
0037     //!
0038     //!
0039     //! Minimal complete definition
0040     //! ---------------------------
0041     //! 1. `equal`\n
0042     //! When `equal` is defined, `not_equal` is implemented by default as its
0043     //! complement. For all objects `x`, `y` of a `Comparable` tag,
0044     //! @code
0045     //!     not_equal(x, y) == not_(equal(x, y))
0046     //! @endcode
0047     //!
0048     //!
0049     //! Laws
0050     //! ----
0051     //! `equal` must define an [equivalence relation][1], and `not_equal` must
0052     //! be its complement. In other words, for all objects `a`, `b`, `c` with
0053     //! a `Comparable` tag, the following must hold:
0054     //! @code
0055     //!     equal(a, a)                                         // Reflexivity
0056     //!     if equal(a, b) then equal(b, a)                     // Symmetry
0057     //!     if equal(a, b) && equal(b, c) then equal(a, c)      // Transitivity
0058     //!     not_equal(a, b) is equivalent to not_(equal(a, b))
0059     //! @endcode
0060     //!
0061     //!
0062     //! Concrete models
0063     //! ---------------
0064     //! `hana::integral_constant`, `hana::map`, `hana::optional`, `hana::pair`,
0065     //! `hana::range`, `hana::set`, `hana::string`, `hana::tuple`,
0066     //!  `hana::type`
0067     //!
0068     //!
0069     //! Free model for `EqualityComparable` data types
0070     //! ----------------------------------------------
0071     //! Two data types `T` and `U` that model the cross-type EqualityComparable
0072     //! concept presented in [N3351][2] automatically model the `Comparable`
0073     //! concept by setting
0074     //! @code
0075     //!     equal(x, y) = (x == y)
0076     //! @endcode
0077     //! Note that this also makes EqualityComparable types in the
0078     //! [usual sense][3] models of `Comparable` in the same way.
0079     //!
0080     //!
0081     //! Equality-preserving functions
0082     //! -----------------------------
0083     //! Let `A` and `B` be two `Comparable` tags. A function @f$f : A \to B@f$
0084     //! is said to be equality-preserving if it preserves the structure of the
0085     //! `Comparable` concept, which can be rigorously stated as follows. For
0086     //! all objects `x`, `y` of tag `A`,
0087     //! @code
0088     //!     if  equal(x, y)  then  equal(f(x), f(y))
0089     //! @endcode
0090     //! Equivalently, we simply require that `f` is a function in the usual
0091     //! mathematical sense. Another property is [injectivity][4], which can be
0092     //! viewed as being a "lossless" mapping. This property can be stated as
0093     //! @code
0094     //!     if  equal(f(x), f(y))  then  equal(x, y)
0095     //! @endcode
0096     //! This is equivalent to saying that `f` maps distinct elements to
0097     //! distinct elements, hence the "lossless" analogy. In other words, `f`
0098     //! will not collapse distinct elements from its domain into a single
0099     //! element in its image, thus losing information.
0100     //!
0101     //! These functions are very important, especially equality-preserving
0102     //! ones, because they allow us to reason simply about programs. Also
0103     //! note that the property of being equality-preserving is taken for
0104     //! granted in mathematics because it is part of the definition of a
0105     //! function. We feel it is important to make the distinction here
0106     //! because programming has evolved differently and as a result
0107     //! programmers are used to work with functions that do not preserve
0108     //! equality.
0109     //!
0110     //!
0111     //! Cross-type version of the methods
0112     //! ---------------------------------
0113     //! The `equal` and `not_equal` methods are "overloaded" to handle
0114     //! distinct tags with certain properties. Specifically, they are
0115     //! defined for _distinct_ tags `A` and `B` such that
0116     //! 1. `A` and `B` share a common tag `C`, as determined by the
0117     //!    `common` metafunction
0118     //! 2. `A`, `B` and `C` are all `Comparable` when taken individually
0119     //! 3. @f$ \mathtt{to<C>} : A \to C @f$ and @f$\mathtt{to<C>} : B \to C@f$
0120     //!    are both equality-preserving and injective (i.e. they are embeddings),
0121     //!    as determined by the `is_embedding` metafunction.
0122     //!
0123     //! The method definitions for tags satisfying the above properties are
0124     //! @code
0125     //!     equal(x, y)     = equal(to<C>(x), to<C>(y))
0126     //!     not_equal(x, y) = not_equal(to<C>(x), to<C>(y))
0127     //! @endcode
0128     //!
0129     //!
0130     //! Important note: special behavior of `equal`
0131     //! -------------------------------------------
0132     //! In the context of programming with heterogeneous values, it is useful
0133     //! to have unrelated objects compare `false` instead of triggering an
0134     //! error. For this reason, `equal` adopts a special behavior for
0135     //! unrelated objects of tags `T` and `U` that do not satisfy the above
0136     //! requirements for the cross-type overloads. Specifically, when `T` and
0137     //! `U` are unrelated (i.e. `T` can't be converted to `U` and vice-versa),
0138     //! comparing objects with those tags yields a compile-time false value.
0139     //! This has the effect that unrelated objects like `float` and
0140     //! `std::string` will compare false, while comparing related objects that
0141     //! can not be safely embedded into the same super structure (like
0142     //! `long long` and `float` because of the precision loss) will trigger a
0143     //! compile-time assertion. Also note that for any tag `T` for which the
0144     //! minimal complete definition of `Comparable` is not provided, a
0145     //! compile-time assertion will also be triggered because `T` and `T`
0146     //! trivially share the common tag `T`, which is the expected behavior.
0147     //! This design choice aims to provide more flexibility for comparing
0148     //! objects, while still rejecting usage patterns that are most likely
0149     //! programming errors.
0150     //!
0151     //!
0152     //! [1]: http://en.wikipedia.org/wiki/Equivalence_relation#Definition
0153     //! [2]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3351.pdf
0154     //! [3]: http://en.cppreference.com/w/cpp/named_req/EqualityComparable
0155     //! [4]: http://en.wikipedia.org/wiki/Injective_function
0156     template <typename T>
0157     struct Comparable;
0158 }} // end namespace boost::hana
0159 
0160 #endif // !BOOST_HANA_FWD_CONCEPT_COMPARABLE_HPP