|
| 1 | +export module libfork.core:concepts_semigroup; |
| 2 | + |
| 3 | +import std; |
| 4 | + |
| 5 | +import :concepts_invocable; |
| 6 | +import :concepts_indirect; |
| 7 | + |
| 8 | +namespace lf { |
| 9 | + |
| 10 | +// === Semigroup |
| 11 | + |
| 12 | +namespace sync { |
| 13 | + |
| 14 | +template <typename R, typename Fn, typename... Args> |
| 15 | +concept invocable_to = std::invocable<Fn, Args...> && std::same_as<std::invoke_result_t<Fn, Args...>, R>; |
| 16 | + |
| 17 | +template <typename R, typename Fn, typename T> |
| 18 | +concept semigroup_r = // |
| 19 | + std::constructible_from<R, T> && // |
| 20 | + invocable_to<R, Fn, T, T> && // |
| 21 | + invocable_to<R, Fn, T, R> && // |
| 22 | + invocable_to<R, Fn, R, T> && // |
| 23 | + invocable_to<R, Fn, R, R>; // |
| 24 | + |
| 25 | +template <typename R, typename Fn, typename I> |
| 26 | +concept indirect_semigroup_r = // |
| 27 | + semigroup_r<R, Fn &, indirect_value_t<I>> && // |
| 28 | + semigroup_r<R, Fn &, std::iter_reference_t<I>> && // |
| 29 | + invocable_to<R, Fn &, indirect_value_t<I>, std::iter_reference_t<I>> && // |
| 30 | + invocable_to<R, Fn &, std::iter_reference_t<I>, indirect_value_t<I>>; // |
| 31 | + |
| 32 | +/** |
| 33 | + * @brief A semigroup is a set `S` and an associative binary operation `·`, such that `S` is closed under `·`. |
| 34 | + * |
| 35 | + * Associativity means that for all `a, b, c` in `S`, `(a · b) · c = a · (b · c)`. |
| 36 | + * |
| 37 | + * Example: `(Z, +)` is a semigroup, since we can add any two integers and the result is also an integer. |
| 38 | + * |
| 39 | + * Example: `(Z, /)` is not a semigroup, since `2/3` s not an integer. |
| 40 | + * |
| 41 | + * Example: `(Z, -)` is not a semigroup, since `(1 - 1) - 1 != 1 - (1 - 1)`. |
| 42 | + * |
| 43 | + * Let `t`, `u` and `f` be objects of types `T`, `U` and `Fn` respectively. |
| 44 | + * Then the following expression must be valid: |
| 45 | + * |
| 46 | + * ``` |
| 47 | + * f(u, t) |
| 48 | + * ``` |
| 49 | + * |
| 50 | + * And return the same type `R` for all combinations of `T` and `U` being `R`, |
| 51 | + * `indirect_value_t<I>` and `std::iter_reference_t<I>`. |
| 52 | + */ |
| 53 | +export template <typename Fn, typename I> |
| 54 | +concept indirect_semigroup = // |
| 55 | + std::indirectly_readable<I> && // |
| 56 | + std::copy_constructible<Fn> && // |
| 57 | + std::regular_invocable<Fn &, indirect_value_t<I>, indirect_value_t<I>> && // |
| 58 | + indirect_semigroup_r< // |
| 59 | + std::invoke_result_t<Fn &, indirect_value_t<I>, indirect_value_t<I>>, // |
| 60 | + Fn, // |
| 61 | + I // |
| 62 | + >; // |
| 63 | + |
| 64 | +} // namespace sync |
| 65 | + |
| 66 | +namespace async { |
| 67 | + |
| 68 | +template <typename R, typename Fn, typename Context, typename T> |
| 69 | +concept semigroup_r = // |
| 70 | + std::constructible_from<R, T> && // |
| 71 | + std::default_initializable<R> && // |
| 72 | + async_invocable_to<Fn, R, Context, T, T> && // |
| 73 | + async_invocable_to<Fn, R, Context, T, R> && // |
| 74 | + async_invocable_to<Fn, R, Context, R, T> && // |
| 75 | + async_invocable_to<Fn, R, Context, R, R>; // |
| 76 | + |
| 77 | +template <typename R, typename Fn, typename Context, typename I> |
| 78 | +concept indirect_semigroup_r = // |
| 79 | + semigroup_r<R, Fn &, Context, indirect_value_t<I>> && // |
| 80 | + semigroup_r<R, Fn &, Context, std::iter_reference_t<I>> && // |
| 81 | + async_invocable_to<Fn &, R, Context, indirect_value_t<I>, std::iter_reference_t<I>> && // |
| 82 | + async_invocable_to<Fn &, R, Context, std::iter_reference_t<I>, indirect_value_t<I>>; // |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief A semigroup is a set `S` and an associative binary operation `·`, such that `S` is closed under `·`. |
| 86 | + * |
| 87 | + * Associativity means that for all `a, b, c` in `S`, `(a · b) · c = a · (b · c)`. |
| 88 | + * |
| 89 | + * Example: `(Z, +)` is a semigroup, since we can add any two integers and the result is also an integer. |
| 90 | + * |
| 91 | + * Example: `(Z, /)` is not a semigroup, since `2/3` s not an integer. |
| 92 | + * |
| 93 | + * Example: `(Z, -)` is not a semigroup, since `(1 - 1) - 1 != 1 - (1 - 1)`. |
| 94 | + * |
| 95 | + * Let `t`, `u` and `f` be objects of types `T`, `U` and `Fn` respectively. |
| 96 | + * Then the following expression must be valid: |
| 97 | + * |
| 98 | + * ``` |
| 99 | + * R ret; |
| 100 | + * co_await scope.call(std::addressof(ret), f, u, t) |
| 101 | + * ``` |
| 102 | + * |
| 103 | + * And return the same type `R` for all combinations of `T` and `U` being `R`, |
| 104 | + * `indirect_value_t<I>` and `std::iter_reference_t<I>`. |
| 105 | + */ |
| 106 | +export template <typename Fn, typename Context, typename I> |
| 107 | +concept indirect_semigroup = // |
| 108 | + std::indirectly_readable<I> && // |
| 109 | + worker_context<Context> && // |
| 110 | + std::copy_constructible<Fn> && // |
| 111 | + async_invocable<Fn &, Context, indirect_value_t<I>, indirect_value_t<I>> && // |
| 112 | + indirect_semigroup_r< // |
| 113 | + async_result_t<Fn &, Context, indirect_value_t<I>, indirect_value_t<I>>, // |
| 114 | + Fn, // |
| 115 | + Context, // |
| 116 | + I // |
| 117 | + >; // |
| 118 | + |
| 119 | +} // namespace async |
| 120 | + |
| 121 | +/** |
| 122 | + * @brief Either a synchronous or asynchronous semigroup. |
| 123 | + */ |
| 124 | +export template <typename Fn, typename Context, typename I> |
| 125 | +concept indirect_semigroup = async::indirect_semigroup<Fn, Context, I> || sync::indirect_semigroup<Fn, I>; |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief A semantic requirement that the semigroup operation is commutative. |
| 129 | + * |
| 130 | + * Commutativity requires `a · b = b · a` for all `a`, `b` in the set `S`. |
| 131 | + */ |
| 132 | +export template <typename Fn, typename Context, typename I> |
| 133 | +concept indirect_commutative_semigroup = indirect_semigroup<Fn, Context, I>; |
| 134 | + |
| 135 | +template <typename Fn, typename Context, typename I> |
| 136 | +struct indirect_semigroup_result { |
| 137 | + using type = std::invoke_result_t<Fn &, indirect_value_t<I>, indirect_value_t<I>>; |
| 138 | +}; |
| 139 | + |
| 140 | +template <typename Fn, typename Context, typename I> |
| 141 | + requires async::indirect_semigroup<Fn, Context, I> |
| 142 | +struct indirect_semigroup_result<Fn, Context, I> { |
| 143 | + using type = async_result_t<Fn &, Context, indirect_value_t<I>, indirect_value_t<I>>; |
| 144 | +}; |
| 145 | + |
| 146 | +/** |
| 147 | + * @brief Get the result type of an indirect semigroup operation. |
| 148 | + * |
| 149 | + * This is the type of the result of applying the semigroup operation to two elements of the set. |
| 150 | + */ |
| 151 | +export template <typename Fn, typename Context, typename I> |
| 152 | + requires indirect_semigroup<Fn, Context, I> |
| 153 | +using indirect_semigroup_t = indirect_semigroup_result<Fn, Context, I>::type; |
| 154 | + |
| 155 | +} // namespace lf |
0 commit comments