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 Numeral. |
17
|
|
|
* |
18
|
|
|
* phpcs:disable Generic.Files.LineLength.TooLong |
19
|
|
|
*/ |
20
|
|
|
final class Numeral |
21
|
|
|
{ |
22
|
9 |
|
public static function eight(): Closure |
23
|
|
|
{ |
24
|
9 |
|
return self::succ()(self::seven()); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public static function exponentiation(): Closure |
28
|
|
|
{ |
29
|
1 |
|
return Combinators::T(); |
30
|
|
|
} |
31
|
|
|
|
32
|
11 |
|
public static function five(): Closure |
33
|
|
|
{ |
34
|
11 |
|
return self::succ()(self::four()); |
35
|
|
|
} |
36
|
|
|
|
37
|
15 |
|
public static function four(): Closure |
38
|
|
|
{ |
39
|
15 |
|
return self::succ()(self::three()); |
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
public static function minus(): Closure |
43
|
|
|
{ |
44
|
8 |
|
return Combinators::V()(self::pred()); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public static function multiply(): Closure |
48
|
|
|
{ |
49
|
1 |
|
return Combinators::B(); |
50
|
|
|
} |
51
|
|
|
|
52
|
9 |
|
public static function nine(): Closure |
53
|
|
|
{ |
54
|
9 |
|
return self::succ()(self::eight()); |
55
|
|
|
} |
56
|
|
|
|
57
|
20 |
|
public static function one(): Closure |
58
|
|
|
{ |
59
|
20 |
|
return self::succ()(self::zero()); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
public static function plus(): Closure |
63
|
|
|
{ |
64
|
4 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => static fn (callable $c): Closure => Combinators::B()($a($c))($b($c)); |
65
|
|
|
} |
66
|
|
|
|
67
|
9 |
|
public static function pred(): Closure |
68
|
|
|
{ |
69
|
9 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => static fn ($c) => $a(static fn (callable $d): Closure => static fn (callable $e) => $e($d($b)))(static fn () => $c)(Combinators::I()); |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
public static function seven(): Closure |
73
|
|
|
{ |
74
|
9 |
|
return self::succ()(self::six()); |
75
|
|
|
} |
76
|
|
|
|
77
|
9 |
|
public static function six(): Closure |
78
|
|
|
{ |
79
|
9 |
|
return self::succ()(self::five()); |
80
|
|
|
} |
81
|
|
|
|
82
|
21 |
|
public static function succ(): Closure |
83
|
|
|
{ |
84
|
21 |
|
return static fn (callable $n): Closure => static fn (callable $f): Closure => Combinators::B()($f)($n($f)); |
85
|
|
|
} |
86
|
|
|
|
87
|
20 |
|
public static function three(): Closure |
88
|
|
|
{ |
89
|
20 |
|
return self::succ()(self::two()); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public static function toInt(callable $numeral): int |
93
|
|
|
{ |
94
|
1 |
|
return $numeral(static fn (int $n): int => $n + 1)(0); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public static function toNumeral(int $n): Closure |
98
|
|
|
{ |
99
|
1 |
|
return 0 === $n ? |
100
|
1 |
|
Numeral::zero() : |
101
|
1 |
|
Numeral::succ()(Numeral::toNumeral($n - 1)); |
102
|
|
|
} |
103
|
|
|
|
104
|
20 |
|
public static function two(): Closure |
105
|
|
|
{ |
106
|
20 |
|
return self::succ()(self::one()); |
107
|
|
|
} |
108
|
|
|
|
109
|
21 |
|
public static function zero(): Closure |
110
|
|
|
{ |
111
|
21 |
|
return static fn (): Closure => Combinators::I(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|