-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.hpp
186 lines (143 loc) · 6.04 KB
/
groups.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#pragma once
#ifndef YK_GROUPS_HPP
#define YK_GROUPS_HPP
#include <concepts>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <version>
#include "tag_invoke.hpp"
#if defined(__cpp_lib_constexpr_string)
#define CXX20_STRING_CONSTEXPR constexpr
#else
#define CXX20_STRING_CONSTEXPR
#endif
namespace yk ::groups {
template <class F, class T, class U>
concept binary_op = std::invocable<F, T, U>;
template <class F, class T>
concept magma = binary_op<F, T, T>;
template <class T>
struct identity_fn {
template <magma<T> F>
requires tag_invocable<identity_fn, F>
constexpr auto operator()(F&& f) const noexcept(nothrow_tag_invocable<identity_fn, F>) -> tag_invoke_result_t<identity_fn, F> {
return tag_invoke(*this, std::forward<F>(f));
}
};
template <class T>
inline constexpr identity_fn<T> identity{};
template <class F, class T>
concept unital_impl = magma<F, T> && std::invocable<tag_t<identity<T>>, F>;
template <class T>
struct left_identity_fn {
template <magma<T> F>
requires tag_invocable<left_identity_fn, F>
constexpr auto operator()(F&& f) const noexcept(nothrow_tag_invocable<left_identity_fn, F>) -> tag_invoke_result_t<left_identity_fn, F> {
return tag_invoke(*this, std::forward<F>(f));
}
template <unital_impl<T> F>
constexpr auto operator()(F&& f) const noexcept(std::is_nothrow_invocable_v<identity_fn<T>, F>) -> std::invoke_result_t<identity_fn<T>, F> {
return identity<T>(std::forward<F>(f));
}
template <unital_impl<T> F>
requires tag_invocable<left_identity_fn, F>
constexpr auto operator()(F&& f) const noexcept(std::is_nothrow_invocable_v<identity_fn<T>, F>) -> std::invoke_result_t<identity_fn<T>, F> {
return identity<T>(std::forward<F>(f));
}
};
template <class T>
inline constexpr left_identity_fn<T> left_identity{};
template <class F, class T>
concept left_unital_impl = magma<F, T> and std::invocable<tag_t<left_identity<T>>, F>;
template <class T>
struct right_identity_fn {
template <magma<T> F>
requires tag_invocable<right_identity_fn, F>
constexpr auto operator()(F&& f) const noexcept(nothrow_tag_invocable<right_identity_fn, F>) -> tag_invoke_result_t<right_identity_fn, F> {
return tag_invoke(*this, std::forward<F>(f));
}
template <unital_impl<T> F>
constexpr auto operator()(F&& f) const noexcept(std::is_nothrow_invocable_v<identity_fn<T>, F>) -> std::invoke_result_t<identity_fn<T>, F> {
return identity<T>(std::forward<F>(f));
}
template <unital_impl<T> F>
requires tag_invocable<right_identity_fn, F>
constexpr auto operator()(F&& f) const noexcept(std::is_nothrow_invocable_v<identity_fn<T>, F>) -> std::invoke_result_t<identity_fn<T>, F> {
return identity<T>(std::forward<F>(f));
}
};
template <class T>
inline constexpr right_identity_fn<T> right_identity{};
template <class F, class T>
concept right_unital_impl = magma<F, T> and std::invocable<tag_t<right_identity<F>>, T>;
template <class F, class T>
concept left_unital = left_unital_impl<F, T> or unital_impl<F, T>;
template <class F, class T>
concept right_unital = right_unital_impl<F, T> or unital_impl<F, T>;
template <class F, class T>
concept unital = (unital_impl<F, T>) or (left_unital_impl<F, T> and right_unital_impl<F, T>);
template <class F, class T>
inline constexpr bool enable_semigroup = false;
template <class F, class T>
concept semigroup = magma<F, T> && enable_semigroup<std::remove_cvref_t<F>, T>;
template <class F, class T>
concept monoid = semigroup<F, T> && unital<F, T>;
inline constexpr struct plus_fn {
template <class T, class U>
requires tag_invocable<plus_fn, T, U>
constexpr auto operator()(T&& x, U&& y) const noexcept(nothrow_tag_invocable<plus_fn, T, U>) -> tag_invoke_result_t<plus_fn, T, U> {
return tag_invoke(*this, std::forward<T>(x), std::forward<U>(y));
}
// arithmeric
template <class T>
requires(std::is_arithmetic_v<std::remove_cvref_t<T>>)
friend constexpr auto tag_invoke(plus_fn, T x, T y) noexcept { return x + y; }
template <class T>
requires(std::is_arithmetic_v<std::remove_cvref_t<T>>)
friend constexpr auto tag_invoke(identity_fn<T>, plus_fn) noexcept -> std::remove_cvref_t<T> { return 0; }
// string
template <class CharT, class Traits, class Alloc>
friend CXX20_STRING_CONSTEXPR auto tag_invoke(plus_fn, std::basic_string<CharT, Traits, Alloc> x, std::basic_string<CharT, Traits, Alloc> y) {
return x + y;
}
template <class CharT, class Traits, class Alloc>
friend CXX20_STRING_CONSTEXPR auto tag_invoke(identity_fn<std::basic_string<CharT, Traits, Alloc>>, plus_fn) {
return std::basic_string<CharT, Traits, Alloc>{};
}
} plus{};
template <class T>
requires(std::is_arithmetic_v<T>)
inline constexpr bool enable_semigroup<plus_fn, T> = true;
template <class CharT, class Traits, class Alloc>
inline constexpr bool enable_semigroup<plus_fn, std::basic_string<CharT, Traits, Alloc>> = true;
inline constexpr struct minus_fn {
template <class T, class U>
requires tag_invocable<minus_fn, T, U>
constexpr auto operator()(T&& x, U&& y) const noexcept(nothrow_tag_invocable<minus_fn, T, U>) -> tag_invoke_result_t<minus_fn, T, U> {
return tag_invoke(*this, std::forward<T>(x), std::forward<U>(y));
}
template <class T>
requires(std::is_arithmetic_v<T>)
friend constexpr auto tag_invoke(minus_fn, T x, T y) noexcept { return x - y; }
template <class T>
requires(std::is_arithmetic_v<std::remove_cvref_t<T>>)
friend constexpr auto tag_invoke(right_identity_fn<T>, minus_fn) noexcept -> std::remove_cvref_t<T> { return 0; }
} minus{};
template <class T, semigroup<T> S>
inline constexpr bool enable_semigroup<S, std::optional<T>> = true;
template <class T, magma<T> M>
constexpr std::optional<T> tag_invoke(identity_fn<std::optional<T>>, M) {
return std::nullopt;
}
} // namespace yk::groups
template <class T, yk::groups::semigroup<T> S>
constexpr std::optional<T> tag_invoke(S s, std::optional<T> x, std::optional<T> y) {
if (!x) return y;
if (!y) return x;
return s(x.value(), y.value());
}
#endif // !YK_GROUPS_HPP