|
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 CList. |
|
17
|
|
|
* |
|
18
|
|
|
* phpcs:disable Generic.Files.LineLength.TooLong |
|
19
|
|
|
*/ |
|
20
|
|
|
final class CList |
|
21
|
|
|
{ |
|
22
|
2 |
|
public static function concat(): Closure |
|
23
|
|
|
{ |
|
24
|
2 |
|
return static fn (callable $xs): Closure => static fn (callable $ys): Closure => self::foldr()(self::cons())($ys)($xs); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
6 |
|
public static function cons(): Closure |
|
28
|
|
|
{ |
|
29
|
6 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => Pair::of()(Boolean::CFalse())(Pair::of()($a)($b)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public static function foldl() |
|
36
|
|
|
{ |
|
37
|
4 |
|
return static fn (callable $f): Closure => static fn ($a): Closure => static fn (callable $xs) => self::foldr()( |
|
38
|
4 |
|
static fn (callable $x): Closure => static fn (callable $g): Closure => static fn ($y) => $g($f($y)($x)) |
|
39
|
4 |
|
)(Combinators::I())($xs)($a); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public static function foldr() |
|
46
|
|
|
{ |
|
47
|
4 |
|
return Combinators::Y()( |
|
48
|
4 |
|
static fn (callable $r): Closure => static fn (callable $f): Closure => static fn ($a): Closure => static fn (callable $xs) => Boolean::CIf()(self::isNil()($xs))(static fn () => $a)(static fn () => $f(self::head()($xs))($r($f)($a)(self::tail()($xs))))() |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
5 |
|
public static function head(): Closure |
|
53
|
|
|
{ |
|
54
|
5 |
|
return static fn (callable $a): Closure => Pair::first()(Pair::second()($a)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
5 |
|
public static function isNil(): Closure |
|
58
|
|
|
{ |
|
59
|
5 |
|
return Pair::first(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public static function length(): Closure |
|
63
|
|
|
{ |
|
64
|
3 |
|
return self::foldl()(static fn (callable $a): Closure => static fn (callable $b): Closure => Numeral::succ()($a))(Numeral::zero()); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
public static function nil(): Closure |
|
68
|
|
|
{ |
|
69
|
6 |
|
return Pair::of()(Boolean::CTrue())(Boolean::CTrue()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
public static function range(): Closure |
|
73
|
|
|
{ |
|
74
|
2 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => Numeral::minus()(Numeral::succ()($b))($a)(static fn (callable $c): Closure => CList::cons()(Numeral::minus()($b)(CList::length()($c)))($c))(CList::nil()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
public static function repeat(): Closure |
|
78
|
|
|
{ |
|
79
|
2 |
|
return static fn (callable $a): Closure => static fn (callable $b): Closure => $b( |
|
80
|
2 |
|
static fn ($c): Closure => CList::cons()($a)($c) |
|
81
|
2 |
|
)(CList::nil()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public static function sum(): Closure |
|
85
|
|
|
{ |
|
86
|
1 |
|
return self::foldl()(Numeral::plus())(Numeral::zero()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
public static function tail(): Closure |
|
90
|
|
|
{ |
|
91
|
4 |
|
return static fn (callable $a): Closure => Pair::second()(Pair::second()($a)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.