|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace loophp\churchencoding; |
|
11
|
|
|
|
|
12
|
|
|
use Closure; |
|
13
|
|
|
use loophp\combinator\Combinators; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Boolean. |
|
17
|
|
|
* |
|
18
|
|
|
* phpcs:disable Generic.Files.LineLength.TooLong |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Boolean |
|
21
|
|
|
{ |
|
22
|
5 |
|
public static function CAnd(): Closure |
|
23
|
|
|
{ |
|
24
|
5 |
|
return static fn (callable $l): Closure => static fn (callable $c): Closure => $l($c)(self::CFalse()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
23 |
|
public static function CFalse(): Closure |
|
28
|
|
|
{ |
|
29
|
23 |
|
return Combinators::Ki(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
5 |
|
public static function CIf(): Closure |
|
33
|
|
|
{ |
|
34
|
5 |
|
return Combinators::I(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
5 |
|
public static function CNot(): Closure |
|
38
|
|
|
{ |
|
39
|
5 |
|
return Combinators::C(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
public static function COr(): Closure |
|
43
|
|
|
{ |
|
44
|
3 |
|
return Combinators::M(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
22 |
|
public static function CTrue(): Closure |
|
48
|
|
|
{ |
|
49
|
22 |
|
return Combinators::K(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public static function CXor(): Closure |
|
53
|
|
|
{ |
|
54
|
3 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => ($a($b(self::CFalse())(self::CTrue())))($b(self::CTrue())(self::CFalse())); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public static function eq(): Closure |
|
58
|
|
|
{ |
|
59
|
1 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => self::CAnd()(self::lte()($a)($b))(self::lte()($b)($a)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public static function gt(): Closure |
|
63
|
|
|
{ |
|
64
|
1 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => self::CNot()(self::lte()($a)($b)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public static function gte(): Closure |
|
68
|
|
|
{ |
|
69
|
2 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => self::isZero()(Numeral::minus()($b)($a)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
public static function isZero(): Closure |
|
73
|
|
|
{ |
|
74
|
6 |
|
return static fn (callable $a): Closure => $a(Combinators::K()(self::CFalse()))(self::CTrue()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public static function lt(): Closure |
|
78
|
|
|
{ |
|
79
|
1 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => self::CNot()(self::gte()($a)($b)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
public static function lte(): Closure |
|
83
|
|
|
{ |
|
84
|
3 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => self::isZero()(Numeral::minus()($a)($b)); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|