// Copyright Louis Dionne 2013-2017 // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace hana = boost::hana; using namespace hana::literals; // A function that can have an arbitrary compile-time set of values as a domain // and co-domain. This is most likely purely of theoretical interest, but it // allows creating functions with very complex domains and co-domains that are // computed at compile-time. struct Function { }; template struct function_type { using hana_tag = Function; Domain domain_; Codomain codomain_; F f_; template constexpr auto operator()(X x) const { BOOST_HANA_CONSTANT_ASSERT(boost::hana::contains(domain_, x)); return f_(x); } }; template constexpr auto operator==(function_type f, function_type g) { return hana::equal(f, g); } template constexpr auto operator!=(function_type f, function_type g) { return hana::not_equal(f, g); } auto function = [](auto domain, auto codomain) { return [=](auto definition) { return function_type{ domain, codomain, definition }; }; }; template constexpr auto domain(Function f) { return f.domain_; } template constexpr auto codomain(Function f) { return f.codomain_; } template constexpr auto range(Function f) { // We must convert to hana::tuple first because hana::set is not a Functor return hana::to_set(hana::transform(hana::to_tuple(domain(f)), f)); } template constexpr auto implies(P p, Q q) { return hana::or_(hana::not_(p), q); } template constexpr auto is_injective(F f) { auto dom = hana::to_tuple(domain(f)); auto pairs = hana::cartesian_product(hana::make_tuple(dom, dom)); return hana::all_of(pairs, hana::fuse([&](auto x, auto y) { return implies(hana::not_equal(x, y), hana::not_equal(f(x), f(y))); })); } template constexpr auto is_onto(F f) { return codomain(f) == range(f); } namespace boost { namespace hana { template <> struct equal_impl { template static constexpr auto apply(F f, G g) { return domain(f) == domain(g) && hana::all_of(domain(f), hana::demux(hana::equal)(f, g)); } }; }} // end namespace boost::hana int main() { auto f = function(hana::make_set(1_c, 2_c, 3_c), hana::make_set(1_c, 2_c, 3_c, 4_c, 5_c, 6_c))( [](auto x) { return x + 1_c; } ); auto g = function(hana::make_set(1_c, 2_c, 3_c), hana::make_set(2_c, 3_c, 4_c))( [](auto x) { return x + 1_c; } ); auto h = function(hana::make_set(1_c, 2_c, 3_c), hana::make_set(0_c, 1_c, 2_c))( [](auto x) { return x - 1_c; } ); BOOST_HANA_CONSTANT_CHECK(f == g); BOOST_HANA_CONSTANT_CHECK(f != h); BOOST_HANA_CONSTANT_CHECK(f(1_c) == 2_c); BOOST_HANA_CONSTANT_CHECK(range(f) == hana::make_set(4_c, 3_c, 2_c)); BOOST_HANA_CONSTANT_CHECK(range(g) == hana::make_set(2_c, 3_c, 4_c)); BOOST_HANA_CONSTANT_CHECK(range(h) == hana::make_set(0_c, 1_c, 2_c)); BOOST_HANA_CONSTANT_CHECK(hana::not_(is_onto(f))); BOOST_HANA_CONSTANT_CHECK(is_onto(g)); BOOST_HANA_CONSTANT_CHECK(is_onto(h)); auto even = function(hana::make_set(1_c, 2_c, 3_c), hana::make_set(hana::true_c, hana::false_c))( [](auto x) { return x % 2_c == 0_c; } ); BOOST_HANA_CONSTANT_CHECK(is_injective(f)); BOOST_HANA_CONSTANT_CHECK(is_injective(g)); BOOST_HANA_CONSTANT_CHECK(is_injective(h)); BOOST_HANA_CONSTANT_CHECK(hana::not_(is_injective(even))); }