demux.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*!
  2. @file
  3. Defines `boost::hana::demux`.
  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_FUNCTIONAL_DEMUX_HPP
  9. #define BOOST_HANA_FUNCTIONAL_DEMUX_HPP
  10. #include <boost/hana/basic_tuple.hpp>
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/detail/decay.hpp>
  13. #include <cstddef>
  14. #include <utility>
  15. BOOST_HANA_NAMESPACE_BEGIN
  16. //! @ingroup group-functional
  17. //! Invoke a function with the results of invoking other functions
  18. //! on its arguments.
  19. //!
  20. //! Specifically, `demux(f)(g...)` is a function such that
  21. //! @code
  22. //! demux(f)(g...)(x...) == f(g(x...)...)
  23. //! @endcode
  24. //!
  25. //! Each `g` is called with all the arguments, and then `f` is called
  26. //! with the result of each `g`. Hence, the arity of `f` must match
  27. //! the number of `g`s.
  28. //!
  29. //! This is called `demux` because of a vague similarity between this
  30. //! device and a demultiplexer in signal processing. `demux` takes what
  31. //! can be seen as a continuation (`f`), a bunch of functions to split a
  32. //! signal (`g...`) and zero or more arguments representing the signal
  33. //! (`x...`). Then, it calls the continuation with the result of
  34. //! splitting the signal with whatever functions where given.
  35. //!
  36. //! @note
  37. //! When used with two functions only, `demux` is associative. In other
  38. //! words (and noting `demux(f, g) = demux(f)(g)` to ease the notation),
  39. //! it is true that `demux(demux(f, g), h) == demux(f, demux(g, h))`.
  40. //!
  41. //!
  42. //! Signature
  43. //! ---------
  44. //! The signature of `demux` is
  45. //! \f[
  46. //! \mathtt{demux} :
  47. //! (B_1 \times \dotsb \times B_n \to C)
  48. //! \to ((A_1 \times \dotsb \times A_n \to B_1)
  49. //! \times \dotsb
  50. //! \times (A_1 \times \dotsb \times A_n \to B_n))
  51. //! \to (A_1 \times \dotsb \times A_n \to C)
  52. //! \f]
  53. //!
  54. //! This can be rewritten more tersely as
  55. //! \f[
  56. //! \mathtt{demux} :
  57. //! \left(\prod_{i=1}^n B_i \to C \right)
  58. //! \to \prod_{j=1}^n \left(\prod_{i=1}^n A_i \to B_j \right)
  59. //! \to \left(\prod_{i=1}^n A_i \to C \right)
  60. //! \f]
  61. //!
  62. //!
  63. //! Link with normal composition
  64. //! ----------------------------
  65. //! The signature of `compose` is
  66. //! \f[
  67. //! \mathtt{compose} : (B \to C) \times (A \to B) \to (A \to C)
  68. //! \f]
  69. //!
  70. //! A valid observation is that this coincides exactly with the type
  71. //! of `demux` when used with a single unary function. Actually, both
  72. //! functions are equivalent:
  73. //! @code
  74. //! demux(f)(g)(x) == compose(f, g)(x)
  75. //! @endcode
  76. //!
  77. //! However, let's now consider the curried version of `compose`,
  78. //! `curry<2>(compose)`:
  79. //! \f[
  80. //! \mathtt{curry_2(compose)} : (B \to C) \to ((A \to B) \to (A \to C))
  81. //! \f]
  82. //!
  83. //! For the rest of this explanation, we'll just consider the curried
  84. //! version of `compose` and so we'll use `compose` instead of
  85. //! `curry<2>(compose)` to lighten the notation. With currying, we can
  86. //! now consider `compose` applied to itself:
  87. //! \f[
  88. //! \mathtt{compose(compose, compose)} :
  89. //! (B \to C) \to (A_1 \to A_2 \to B) \to (A_1 \to A_2 \to C)
  90. //! \f]
  91. //!
  92. //! If we uncurry deeply the above expression, we obtain
  93. //! \f[
  94. //! \mathtt{compose(compose, compose)} :
  95. //! (B \to C) \times (A_1 \times A_2 \to B) \to (A_1 \times A_2 \to C)
  96. //! \f]
  97. //!
  98. //! This signature is exactly the same as that of `demux` when given a
  99. //! single binary function, and indeed they are equivalent definitions.
  100. //! We can also generalize this further by considering
  101. //! `compose(compose(compose, compose), compose)`:
  102. //! \f[
  103. //! \mathtt{compose(compose(compose, compose), compose)} :
  104. //! (B \to C) \to (A_1 \to A_2 \to A_3 \to B)
  105. //! \to (A_1 \to A_2 \to A_3 \to C)
  106. //! \f]
  107. //!
  108. //! which uncurries to
  109. //! \f[
  110. //! \mathtt{compose(compose(compose, compose), compose)} :
  111. //! (B \to C) \times (A_1 \times A_2 \times A_3 \to B)
  112. //! \to (A_1 \times A_2 \times A_3 \to C)
  113. //! \f]
  114. //!
  115. //! This signature is exactly the same as that of `demux` when given a
  116. //! single ternary function. Hence, for a single n-ary function `g`,
  117. //! `demux(f)(g)` is equivalent to the n-times composition of `compose`
  118. //! with itself, applied to `g` and `f`:
  119. //! @code
  120. //! demux(f)(g) == fold_left([compose, ..., compose], id, compose)(g, f)
  121. //! // ^^^^^^^^^^^^^^^^^^^^^ n times
  122. //! @endcode
  123. //!
  124. //! More information on this insight can be seen [here][1]. Also, I'm
  125. //! not sure how this insight could be generalized to more than one
  126. //! function `g`, or if that is even possible.
  127. //!
  128. //!
  129. //! Proof of associativity in the binary case
  130. //! -----------------------------------------
  131. //! As explained above, `demux` is associative when it is used with
  132. //! two functions only. Indeed, given functions `f`, `g` and `h` with
  133. //! suitable signatures, we have
  134. //! @code
  135. //! demux(f)(demux(g)(h))(x...) == f(demux(g)(h)(x...))
  136. //! == f(g(h(x...)))
  137. //! @endcode
  138. //!
  139. //! On the other hand, we have
  140. //! @code
  141. //! demux(demux(f)(g))(h)(x...) == demux(f)(g)(h(x...))
  142. //! == f(g(h(x...)))
  143. //! @endcode
  144. //!
  145. //! and hence `demux` is associative in the binary case.
  146. //!
  147. //!
  148. //! Example
  149. //! -------
  150. //! @include example/functional/demux.cpp
  151. //!
  152. //! [1]: http://stackoverflow.com/q/5821089/627587
  153. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  154. constexpr auto demux = [](auto&& f) {
  155. return [perfect-capture](auto&& ...g) {
  156. return [perfect-capture](auto&& ...x) -> decltype(auto) {
  157. // x... can't be forwarded unless there is a single g
  158. // function, or that could cause double-moves.
  159. return forwarded(f)(forwarded(g)(x...)...);
  160. };
  161. };
  162. };
  163. #else
  164. template <typename F>
  165. struct pre_demux_t;
  166. struct make_pre_demux_t {
  167. struct secret { };
  168. template <typename F>
  169. constexpr pre_demux_t<typename detail::decay<F>::type> operator()(F&& f) const {
  170. return {static_cast<F&&>(f)};
  171. }
  172. };
  173. template <typename Indices, typename F, typename ...G>
  174. struct demux_t;
  175. template <typename F>
  176. struct pre_demux_t {
  177. F f;
  178. template <typename ...G>
  179. constexpr demux_t<std::make_index_sequence<sizeof...(G)>, F,
  180. typename detail::decay<G>::type...>
  181. operator()(G&& ...g) const& {
  182. return {make_pre_demux_t::secret{}, this->f, static_cast<G&&>(g)...};
  183. }
  184. template <typename ...G>
  185. constexpr demux_t<std::make_index_sequence<sizeof...(G)>, F,
  186. typename detail::decay<G>::type...>
  187. operator()(G&& ...g) && {
  188. return {make_pre_demux_t::secret{}, static_cast<F&&>(this->f), static_cast<G&&>(g)...};
  189. }
  190. };
  191. template <std::size_t ...n, typename F, typename ...G>
  192. struct demux_t<std::index_sequence<n...>, F, G...> {
  193. template <typename ...T>
  194. constexpr demux_t(make_pre_demux_t::secret, T&& ...t)
  195. : storage_{static_cast<T&&>(t)...}
  196. { }
  197. basic_tuple<F, G...> storage_;
  198. template <typename ...X>
  199. constexpr decltype(auto) operator()(X&& ...x) const& {
  200. return hana::at_c<0>(storage_)(
  201. hana::at_c<n+1>(storage_)(x...)...
  202. );
  203. }
  204. template <typename ...X>
  205. constexpr decltype(auto) operator()(X&& ...x) & {
  206. return hana::at_c<0>(storage_)(
  207. hana::at_c<n+1>(storage_)(x...)...
  208. );
  209. }
  210. template <typename ...X>
  211. constexpr decltype(auto) operator()(X&& ...x) && {
  212. return static_cast<F&&>(hana::at_c<0>(storage_))(
  213. static_cast<G&&>(hana::at_c<n+1>(storage_))(x...)...
  214. );
  215. }
  216. };
  217. template <typename F, typename G>
  218. struct demux_t<std::index_sequence<0>, F, G> {
  219. template <typename ...T>
  220. constexpr demux_t(make_pre_demux_t::secret, T&& ...t)
  221. : storage_{static_cast<T&&>(t)...}
  222. { }
  223. basic_tuple<F, G> storage_;
  224. template <typename ...X>
  225. constexpr decltype(auto) operator()(X&& ...x) const& {
  226. return hana::at_c<0>(storage_)(
  227. hana::at_c<1>(storage_)(static_cast<X&&>(x)...)
  228. );
  229. }
  230. template <typename ...X>
  231. constexpr decltype(auto) operator()(X&& ...x) & {
  232. return hana::at_c<0>(storage_)(
  233. hana::at_c<1>(storage_)(static_cast<X&&>(x)...)
  234. );
  235. }
  236. template <typename ...X>
  237. constexpr decltype(auto) operator()(X&& ...x) && {
  238. return static_cast<F&&>(hana::at_c<0>(storage_))(
  239. static_cast<G&&>(hana::at_c<1>(storage_))(static_cast<X&&>(x)...)
  240. );
  241. }
  242. };
  243. constexpr make_pre_demux_t demux{};
  244. #endif
  245. BOOST_HANA_NAMESPACE_END
  246. #endif // !BOOST_HANA_FUNCTIONAL_DEMUX_HPP