fix.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. fix.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_FIX_H
  8. #define BOOST_HOF_GUARD_FUNCTION_FIX_H
  9. /// fix
  10. /// ===
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `fix` function adaptor implements a fixed-point combinator. This can be
  16. /// used to write recursive functions.
  17. ///
  18. /// When using `constexpr`, a function can recurse to a depth that is defined by
  19. /// `BOOST_HOF_RECURSIVE_CONSTEXPR_DEPTH`(default is 16). There is no limitiation on
  20. /// recursion depth for non-constexpr functions. In addition, due to the
  21. /// eagerness of `constexpr` to instantiation templates, in some cases, an
  22. /// explicit return type must be specified in order to avoid reaching the
  23. /// recursion limits of the compiler. This can be accomplished using
  24. /// [`boost::hof::result`](/include/boost/hof/result):
  25. ///
  26. /// int r = boost::hof::result<int>(factorial)(5);
  27. ///
  28. /// Synopsis
  29. /// --------
  30. ///
  31. /// template<class F>
  32. /// constexpr fix_adaptor<F> fix(F f);
  33. ///
  34. /// Semantics
  35. /// ---------
  36. ///
  37. /// assert(fix(f)(xs...) == f(fix(f), xs...));
  38. ///
  39. /// Requirements
  40. /// ------------
  41. ///
  42. /// F must be:
  43. ///
  44. /// * [ConstFunctionObject](ConstFunctionObject)
  45. /// * MoveConstructible
  46. ///
  47. /// Example
  48. /// -------
  49. ///
  50. /// #include <boost/hof.hpp>
  51. /// #include <cassert>
  52. ///
  53. /// int main() {
  54. /// auto factorial = boost::hof::fix(
  55. /// [](auto recurse, auto x) -> decltype(x) {
  56. /// return x == 0 ? 1 : x * recurse(x-1);
  57. /// }
  58. /// );
  59. /// int r = boost::hof::result<int>(factorial)(5);
  60. /// assert(r == 5*4*3*2*1);
  61. /// }
  62. ///
  63. /// References
  64. /// ----------
  65. ///
  66. /// * [Fixed-point combinator](https://en.wikipedia.org/wiki/Fixed-point_combinator)
  67. /// * [Recursive](Recursive)
  68. ///
  69. #include <boost/hof/always.hpp>
  70. #include <boost/hof/detail/callable_base.hpp>
  71. #include <boost/hof/reveal.hpp>
  72. #include <boost/hof/detail/delegate.hpp>
  73. #include <boost/hof/detail/move.hpp>
  74. #include <boost/hof/detail/make.hpp>
  75. #include <boost/hof/detail/static_const_var.hpp>
  76. #include <boost/hof/indirect.hpp>
  77. #include <boost/hof/result.hpp>
  78. #include <boost/hof/detail/recursive_constexpr_depth.hpp>
  79. namespace boost { namespace hof {
  80. namespace detail{
  81. template<class F>
  82. struct compute_indirect_ref
  83. { typedef indirect_adaptor<const F*> type; };
  84. template<class F>
  85. struct compute_indirect_ref<indirect_adaptor<F*>>
  86. { typedef indirect_adaptor<F*> type; };
  87. template<class F>
  88. constexpr indirect_adaptor<const F*> make_indirect_ref(const F& f) noexcept
  89. {
  90. return indirect_adaptor<const F*>(&f);
  91. }
  92. template<class F>
  93. constexpr indirect_adaptor<const F*> make_indirect_ref(const indirect_adaptor<F*>& f) noexcept
  94. {
  95. return f;
  96. }
  97. template<class F, class=void>
  98. struct fix_result
  99. {
  100. template<class... Ts>
  101. struct apply
  102. {
  103. typedef decltype(std::declval<F>()(std::declval<Ts>()...)) type;
  104. };
  105. };
  106. template<class F>
  107. struct fix_result<F, typename holder<
  108. typename F::result_type
  109. >::type>
  110. {
  111. template<class...>
  112. struct apply
  113. {
  114. typedef typename F::result_type type;
  115. };
  116. };
  117. template<class F, class Result, int N>
  118. struct fix_adaptor_base : F
  119. {
  120. BOOST_HOF_INHERIT_CONSTRUCTOR(fix_adaptor_base, F);
  121. typedef typename compute_indirect_ref<F>::type base_ref_type;
  122. typedef fix_adaptor_base<base_ref_type, Result, N-1> derived;
  123. template<class... Ts>
  124. constexpr const F& base_function(Ts&&... xs) const noexcept
  125. {
  126. return boost::hof::always_ref(*this)(xs...);
  127. }
  128. template<class... Ts>
  129. constexpr derived derived_function(Ts&&... xs) const noexcept
  130. {
  131. return derived(boost::hof::detail::make_indirect_ref(this->base_function(xs...)));
  132. }
  133. struct fix_failure
  134. {
  135. template<class Failure>
  136. struct apply
  137. {
  138. template<class... Ts>
  139. struct of
  140. : Failure::template of<derived, Ts...>
  141. {};
  142. };
  143. };
  144. struct failure
  145. : failure_map<fix_failure, F>
  146. {};
  147. BOOST_HOF_RETURNS_CLASS(fix_adaptor_base);
  148. template<class... Ts>
  149. constexpr BOOST_HOF_SFINAE_RESULT(const F&, id_<derived>, id_<Ts>...)
  150. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  151. (
  152. BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...))
  153. (
  154. BOOST_HOF_MANGLE_CAST(derived)(BOOST_HOF_CONST_THIS->derived_function(xs...)),
  155. BOOST_HOF_FORWARD(Ts)(xs)...
  156. )
  157. );
  158. };
  159. template<class F, class Result>
  160. struct fix_adaptor_base<F, Result, 0> : F
  161. {
  162. BOOST_HOF_INHERIT_CONSTRUCTOR(fix_adaptor_base, F);
  163. template<class... Ts>
  164. const F& base_function(Ts&&...) const noexcept
  165. {
  166. return *this;
  167. }
  168. struct fix_failure
  169. {
  170. template<class Failure>
  171. struct apply
  172. {
  173. template<class... Ts>
  174. struct of
  175. : Failure::template of<fix_adaptor_base, Ts...>
  176. {};
  177. };
  178. };
  179. struct failure
  180. : failure_map<fix_failure, F>
  181. {};
  182. BOOST_HOF_RETURNS_CLASS(fix_adaptor_base);
  183. template<class... Ts>
  184. typename Result::template apply<fix_adaptor_base, Ts...>::type
  185. operator()(Ts&&... xs) const
  186. {
  187. return this->base_function(xs...)(*this, BOOST_HOF_FORWARD(Ts)(xs)...);
  188. }
  189. };
  190. }
  191. template<class F>
  192. struct fix_adaptor : detail::fix_adaptor_base<F, detail::fix_result<F>, BOOST_HOF_RECURSIVE_CONSTEXPR_DEPTH>
  193. {
  194. typedef fix_adaptor fit_rewritable1_tag;
  195. typedef detail::fix_adaptor_base<F, detail::fix_result<F>, BOOST_HOF_RECURSIVE_CONSTEXPR_DEPTH> base;
  196. BOOST_HOF_INHERIT_CONSTRUCTOR(fix_adaptor, base);
  197. };
  198. template<class Result, class F>
  199. struct result_adaptor<Result, fix_adaptor<F>>
  200. : fix_adaptor<result_adaptor<Result, F>>
  201. {
  202. typedef fix_adaptor<result_adaptor<Result, F>> base;
  203. BOOST_HOF_INHERIT_CONSTRUCTOR(result_adaptor, base)
  204. };
  205. BOOST_HOF_DECLARE_STATIC_VAR(fix, detail::make<fix_adaptor>);
  206. }} // namespace boost::hof
  207. #endif