comonad.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_HANA_TEST_LAWS_COMONAD_HPP
  5. #define BOOST_HANA_TEST_LAWS_COMONAD_HPP
  6. #include <boost/hana/assert.hpp>
  7. #include <boost/hana/concept/comonad.hpp>
  8. #include <boost/hana/concept/comparable.hpp>
  9. #include <boost/hana/core/when.hpp>
  10. #include <boost/hana/concept/foldable.hpp>
  11. #include <boost/hana/concept/functor.hpp>
  12. #include <laws/base.hpp>
  13. namespace boost { namespace hana { namespace test {
  14. template <typename W, typename = when<true>>
  15. struct TestComonad : TestComonad<W, laws> {
  16. using TestComonad<W, laws>::TestComonad;
  17. };
  18. template <typename W>
  19. struct TestComonad<W, laws> {
  20. template <typename Ws>
  21. TestComonad(Ws ws) {
  22. hana::for_each(ws, [](auto w) {
  23. static_assert(Comonad<decltype(w)>{}, "");
  24. // extract(duplicate(w)) == w
  25. BOOST_HANA_CHECK(hana::equal(
  26. hana::extract(hana::duplicate(w)),
  27. w
  28. ));
  29. // transform(duplicate(w), extract) == w
  30. BOOST_HANA_CHECK(hana::equal(
  31. hana::transform(hana::duplicate(w), extract),
  32. w
  33. ));
  34. // duplicate(duplicate(w)) == transform(duplicate(w), duplicate)
  35. BOOST_HANA_CHECK(hana::equal(
  36. hana::duplicate(hana::duplicate(w)),
  37. hana::transform(hana::duplicate(w), duplicate)
  38. ));
  39. });
  40. }
  41. };
  42. }}} // end namespace boost::hana::test
  43. #endif // !BOOST_HANA_TEST_LAWS_COMONAD_HPP