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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! Types that form a Heyting algebra (bounded lattice with implication).
//!
//! ### Examples
//!
//! ```
//! use fp_library::classes::HeytingAlgebra;
//!
//! assert_eq!(bool::conjoin(true, false), false);
//! assert_eq!(bool::disjoin(true, false), true);
//! assert_eq!(bool::not(true), false);
//! ```
#[fp_macros::document_module]
mod inner {
use fp_macros::*;
/// A type class for types that form a Heyting algebra.
///
/// A Heyting algebra is a bounded lattice with conjunction, disjunction, implication, and negation.
///
/// ### Laws
///
/// * Associativity: `disjoin(a, disjoin(b, c)) = disjoin(disjoin(a, b), c)` and
/// `conjoin(a, conjoin(b, c)) = conjoin(conjoin(a, b), c)`
/// * Commutativity: `disjoin(a, b) = disjoin(b, a)` and `conjoin(a, b) = conjoin(b, a)`
/// * Absorption: `disjoin(a, conjoin(a, b)) = a` and `conjoin(a, disjoin(a, b)) = a`
/// * Idempotence: `disjoin(a, a) = a` and `conjoin(a, a) = a`
/// * Identity: `disjoin(a, false_value) = a` and `conjoin(a, true_value) = a`
/// * Implication: `imply(a, a) = true_value` and `conjoin(a, imply(a, b)) = conjoin(a, b)`
/// * Complementation: `not(a) = imply(a, false_value)`
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// // Identity: conjoin(a, true_value) = a
/// assert_eq!(bool::conjoin(true, bool::true_value()), true);
/// assert_eq!(bool::conjoin(false, bool::true_value()), false);
///
/// // Implication: imply(a, a) = true_value
/// assert_eq!(bool::imply(true, true), bool::true_value());
/// assert_eq!(bool::imply(false, false), bool::true_value());
/// ```
pub trait HeytingAlgebra {
/// Returns the bottom element (false).
#[document_signature]
///
#[document_returns("The bottom element.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::false_value(), false);
/// ```
fn false_value() -> Self;
/// Returns the top element (true).
#[document_signature]
///
#[document_returns("The top element.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::true_value(), true);
/// ```
fn true_value() -> Self;
/// Computes material implication.
#[document_signature]
///
#[document_parameters("The antecedent.", "The consequent.")]
///
#[document_returns("The result of implication.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::imply(true, false), false);
/// assert_eq!(bool::imply(false, true), true);
/// ```
fn imply(
a: Self,
b: Self,
) -> Self;
/// Computes the conjunction (logical AND).
#[document_signature]
///
#[document_parameters("The first value.", "The second value.")]
///
#[document_returns("The conjunction of the two values.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::conjoin(true, true), true);
/// assert_eq!(bool::conjoin(true, false), false);
/// ```
fn conjoin(
a: Self,
b: Self,
) -> Self;
/// Computes the disjunction (logical OR).
#[document_signature]
///
#[document_parameters("The first value.", "The second value.")]
///
#[document_returns("The disjunction of the two values.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::disjoin(false, false), false);
/// assert_eq!(bool::disjoin(true, false), true);
/// ```
fn disjoin(
a: Self,
b: Self,
) -> Self;
/// Computes the logical negation.
#[document_signature]
///
#[document_parameters("The value to negate.")]
///
#[document_returns("The negation.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::not(true), false);
/// assert_eq!(bool::not(false), true);
/// ```
fn not(a: Self) -> Self;
}
/// Returns the bottom element (false).
///
/// Free function version that dispatches to [`HeytingAlgebra::false_value`].
#[document_signature]
///
#[document_type_parameters("The Heyting algebra type.")]
///
#[document_returns("The bottom element.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::heyting_algebra::false_value;
///
/// assert_eq!(false_value::<bool>(), false);
/// ```
pub fn false_value<H: HeytingAlgebra>() -> H {
H::false_value()
}
/// Returns the top element (true).
///
/// Free function version that dispatches to [`HeytingAlgebra::true_value`].
#[document_signature]
///
#[document_type_parameters("The Heyting algebra type.")]
///
#[document_returns("The top element.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::heyting_algebra::true_value;
///
/// assert_eq!(true_value::<bool>(), true);
/// ```
pub fn true_value<H: HeytingAlgebra>() -> H {
H::true_value()
}
/// Computes material implication.
///
/// Free function version that dispatches to [`HeytingAlgebra::imply`].
#[document_signature]
///
#[document_type_parameters("The Heyting algebra type.")]
///
#[document_parameters("The antecedent.", "The consequent.")]
///
#[document_returns("The result of implication.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::heyting_algebra::imply;
///
/// assert_eq!(imply(true, false), false);
/// ```
pub fn imply<H: HeytingAlgebra>(
a: H,
b: H,
) -> H {
H::imply(a, b)
}
/// Computes the conjunction (logical AND).
///
/// Free function version that dispatches to [`HeytingAlgebra::conjoin`].
#[document_signature]
///
#[document_type_parameters("The Heyting algebra type.")]
///
#[document_parameters("The first value.", "The second value.")]
///
#[document_returns("The conjunction of the two values.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::heyting_algebra::conjoin;
///
/// assert_eq!(conjoin(true, false), false);
/// ```
pub fn conjoin<H: HeytingAlgebra>(
a: H,
b: H,
) -> H {
H::conjoin(a, b)
}
/// Computes the disjunction (logical OR).
///
/// Free function version that dispatches to [`HeytingAlgebra::disjoin`].
#[document_signature]
///
#[document_type_parameters("The Heyting algebra type.")]
///
#[document_parameters("The first value.", "The second value.")]
///
#[document_returns("The disjunction of the two values.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::heyting_algebra::disjoin;
///
/// assert_eq!(disjoin(true, false), true);
/// ```
pub fn disjoin<H: HeytingAlgebra>(
a: H,
b: H,
) -> H {
H::disjoin(a, b)
}
/// Computes the logical negation.
///
/// Free function version that dispatches to [`HeytingAlgebra::not`].
#[document_signature]
///
#[document_type_parameters("The Heyting algebra type.")]
///
#[document_parameters("The value to negate.")]
///
#[document_returns("The negation.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::heyting_algebra::not;
///
/// assert_eq!(not(true), false);
/// ```
pub fn not<H: HeytingAlgebra>(a: H) -> H {
H::not(a)
}
impl HeytingAlgebra for bool {
/// Returns `false`.
#[document_signature]
///
#[document_returns("`false`.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::false_value(), false);
/// ```
fn false_value() -> Self {
false
}
/// Returns `true`.
#[document_signature]
///
#[document_returns("`true`.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::true_value(), true);
/// ```
fn true_value() -> Self {
true
}
/// Computes material implication (`!a || b`).
#[document_signature]
///
#[document_parameters("The antecedent.", "The consequent.")]
///
#[document_returns("The result of implication.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::imply(true, false), false);
/// ```
fn imply(
a: Self,
b: Self,
) -> Self {
!a || b
}
/// Computes conjunction (`a && b`).
#[document_signature]
///
#[document_parameters("The first value.", "The second value.")]
///
#[document_returns("The conjunction.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::conjoin(true, false), false);
/// ```
fn conjoin(
a: Self,
b: Self,
) -> Self {
a && b
}
/// Computes disjunction (`a || b`).
#[document_signature]
///
#[document_parameters("The first value.", "The second value.")]
///
#[document_returns("The disjunction.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::disjoin(true, false), true);
/// ```
fn disjoin(
a: Self,
b: Self,
) -> Self {
a || b
}
/// Computes logical negation (`!a`).
#[document_signature]
///
#[document_parameters("The value to negate.")]
///
#[document_returns("The negation.")]
#[document_examples]
///
/// ```
/// use fp_library::classes::HeytingAlgebra;
///
/// assert_eq!(bool::not(true), false);
/// ```
fn not(a: Self) -> Self {
!a
}
}
}
pub use inner::*;