Repository navigation
Expand file tree
/
Copy pathboolean.hh
More file actions
72 lines (59 loc) · 1.79 KB
/
Copy pathboolean.hh
File metadata and controls
72 lines (59 loc) · 1.79 KB
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
#pragma once
#include "combinator.hh"
namespace boolean {
/**
* True = λab.a = K
*/
inline const auto True = lambda::boolean(true, combinator::K);
/**
* False = λab.b = KI
*/
inline const auto False = lambda::boolean(false, combinator::KI);
// force to bool in case b doesnt originate from a lambda::boolean
inline lambda coherseBool(const lambda &b) {
const auto result = b * True * False;
return result;
}
/**
* Not = λpab.p b a = C
*/
inline const auto Not = coherseBool |= combinator::C;
/**
* And = λab.a b a = λab. C a a b = λa . C a a = λa. C a (I a) = λa. S C I a =
* S C I
*/
inline const auto And = coherseBool ^=
combinator::S * combinator::C * combinator::I;
/**
* Or = λab.a a b = λa . a a = λa. I a (I a) = λa. S I I a = S I I
*/
inline const auto Or = coherseBool ^=
combinator::S * combinator::I * combinator::I;
/**
* Xor = λab.a (Not b) b = λab. C a b (Not b) = λab. S (C a) Not b = λa. S (C a)
* C = λa. C S C (C a) = λa. B (C S C) (C) a = B (C S C) C
*/
inline const auto Xor = coherseBool ^=
combinator::C *combinator::S *combinator::C |= combinator::C;
/**
* Xnor = λab . Not (Xor a b) = BBB Not Xor = BBB C Xor
*/
inline const auto Xnor = coherseBool ^= combinator::C ^= Xor;
/**
* Nand = BBB C And
* See Xor -> Xnor for the derivation
*/
inline const auto Nand = coherseBool ^= combinator::C ^= And;
/**
* Nor = BBB C Or
* See Xor -> Xnor for the derivation
*/
inline const auto Nor = coherseBool ^= combinator::C ^= Or;
inline bool toBool(const lambda &b) {
if (!b.boolValue())
throw std::logic_error("toBool: input is not a boolean marker");
return coherseBool(b).boolValue().value();
}
inline auto fromBool(bool b) { return b ? True : False; }
} // namespace boolean
inline lambda::lambda(bool x) { *this = boolean::fromBool(x); }