aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/external/metalang99/include/metalang99/bool.h
blob: 0213cb21202ac1eb8036e57255cb61a3d4c7d84c (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * @file
 * Boolean algebra.
 */

#ifndef ML99_BOOL_H
#define ML99_BOOL_H

#include <metalang99/priv/bool.h>
#include <metalang99/priv/tuple.h>

#include <metalang99/lang.h>

/**
 * Truth.
 */
#define ML99_true(...) ML99_callUneval(ML99_true, )

/**
 * Falsehood.
 */
#define ML99_false(...) ML99_callUneval(ML99_false, )

/**
 * Logical negation.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * // 1
 * ML99_not(v(0))
 *
 * // 0
 * ML99_not(v(1))
 * @endcode
 */
#define ML99_not(x) ML99_call(ML99_not, x)

/**
 * Logical conjunction.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * // 0
 * ML99_and(v(0), v(0))
 *
 * // 0
 * ML99_and(v(0), v(1))
 *
 * // 0
 * ML99_and(v(1), v(0))
 *
 * // 1
 * ML99_and(v(1), v(1))
 * @endcode
 */
#define ML99_and(x, y) ML99_call(ML99_and, x, y)

/**
 * Logical inclusive OR.
 *
 * # Examples
 * @code
 * #include <metalang99/bool.h>
 *
 * // 0
 * ML99_or(v(0), v(0))
 *
 * // 1
 * ML99_or(v(0), v(1))
 *
 * // 1
 * ML99_or(v(1), v(0))
 *
 * // 1
 * ML99_or(v(1), v(1))
 * @endcode
 */
#define ML99_or(x, y) ML99_call(ML99_or, x, y)

/**
 * Logical exclusive OR.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * // 0
 * ML99_xor(v(0), v(0))
 *
 * // 1
 * ML99_xor(v(0), v(1))
 *
 * // 1
 * ML99_xor(v(1), v(0))
 *
 * // 0
 * ML99_xor(v(1), v(1))
 * @endcode
 */
#define ML99_xor(x, y) ML99_call(ML99_xor, x, y)

/**
 * Tests @p x and @p y for equality.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * // 1
 * ML99_boolEq(v(0), v(0))
 *
 * // 0
 * ML99_boolEq(v(0), v(1))
 *
 * // 0
 * ML99_boolEq(v(1), v(0))
 *
 * // 1
 * ML99_boolEq(v(1), v(1))
 * @endcode
 */
#define ML99_boolEq(x, y) ML99_call(ML99_boolEq, x, y)

/**
 * Matches @p x against the two cases: if it is 0 or 1.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * #define MATCH_1_IMPL() v(Billie)
 * #define MATCH_0_IMPL() v(Jean)
 *
 * // Billie
 * ML99_boolMatch(v(1), v(MATCH_))
 *
 * // Jean
 * ML99_boolMatch(v(0), v(MATCH_))
 * @endcode
 *
 * @note This function calls @p f with #ML99_call, so no partial application occurs, and so
 * arity specifiers are not needed.
 */
#define ML99_boolMatch(x, matcher) ML99_call(ML99_boolMatch, x, matcher)

/**
 * The same as #ML99_boolMatch but provides additional arguments to all branches.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * #define MATCH_1_IMPL(x, y, z) v(Billie ~ x y z)
 * #define MATCH_0_IMPL(x, y, z) v(Jean ~ x y z)
 *
 * // Billie ~ 1 2 3
 * ML99_boolMatchWithArgs(v(1), v(MATCH_), v(1, 2, 3))
 *
 * // Jean ~ 1 2 3
 * ML99_boolMatchWithArgs(v(0), v(MATCH_), v(1, 2, 3))
 * @endcode
 */
#define ML99_boolMatchWithArgs(x, matcher, ...)                                                    \
    ML99_call(ML99_boolMatchWithArgs, x, matcher, __VA_ARGS__)

/**
 * If @p cond is true, evaluates to @p x, otherwise @p y.
 *
 * # Examples
 *
 * @code
 * #include <metalang99/bool.h>
 *
 * // 123
 * ML99_if(v(1), v(123), v(18))
 *
 * // 18
 * ML99_if(v(0), v(123), v(18))
 * @endcode
 */
#define ML99_if(cond, x, y) ML99_call(ML99_if, cond, x, y)

/**
 * The plain version of #ML99_if.
 *
 * This macro can imitate lazy evaluation: `ML99_IF(<cond>, <term>, <another-term>)` will expand to
 * one of the two terms, which can be evaluated further; if `<cond>` is 0, then `<term>` will
 * **not** be evaluated, and the same with `<another-term>`.
 *
 * @note @p x and @p y can possibly expand to commas. It means that you can supply `ML99_TERMS(...)`
 * as a branch, for example.
 */
#define ML99_IF(cond, x, y) ML99_PRIV_UNTUPLE(ML99_PRIV_IF(cond, (x), (y)))

#define ML99_TRUE(...)  1
#define ML99_FALSE(...) 0

#define ML99_NOT(x)        ML99_PRIV_NOT(x)
#define ML99_AND(x, y)     ML99_PRIV_AND(x, y)
#define ML99_OR(x, y)      ML99_PRIV_OR(x, y)
#define ML99_XOR(x, y)     ML99_PRIV_XOR(x, y)
#define ML99_BOOL_EQ(x, y) ML99_PRIV_BOOL_EQ(x, y)

#ifndef DOXYGEN_IGNORE

#define ML99_true_IMPL(...)  v(ML99_TRUE())
#define ML99_false_IMPL(...) v(ML99_FALSE())

#define ML99_not_IMPL(x)       v(ML99_NOT(x))
#define ML99_and_IMPL(x, y)    v(ML99_AND(x, y))
#define ML99_or_IMPL(x, y)     v(ML99_OR(x, y))
#define ML99_xor_IMPL(x, y)    v(ML99_XOR(x, y))
#define ML99_boolEq_IMPL(x, y) v(ML99_BOOL_EQ(x, y))

#define ML99_boolMatch_IMPL(x, matcher)              ML99_callUneval(matcher##x, )
#define ML99_boolMatchWithArgs_IMPL(x, matcher, ...) ML99_callUneval(matcher##x, __VA_ARGS__)

#define ML99_if_IMPL(cond, x, y) v(ML99_PRIV_IF(cond, x, y))

// Arity specifiers {

#define ML99_true_ARITY              1
#define ML99_false_ARITY             1
#define ML99_not_ARITY               1
#define ML99_and_ARITY               2
#define ML99_or_ARITY                2
#define ML99_xor_ARITY               2
#define ML99_boolEq_ARITY            2
#define ML99_boolMatch_ARITY         2
#define ML99_boolMatchWithArgs_ARITY 3
#define ML99_if_ARITY                3
// } (Arity specifiers)

#endif // DOXYGEN_IGNORE

#endif // ML99_BOOL_H