-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathFreeMonadTest.php
218 lines (191 loc) · 5.4 KB
/
FreeMonadTest.php
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
<?php
declare(strict_types=1);
namespace example2;
use FunctionalPHP\FantasyLand\Functor;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Widmogrod\Functional as f;
use Widmogrod\Monad\Free as ff;
use Widmogrod\Monad\Free\MonadFree;
use Widmogrod\Monad\IO;
use Widmogrod\Monad\State;
use Widmogrod\Primitive\Listt;
use function Widmogrod\Functional\fromNil;
use function Widmogrod\Useful\matchPatterns;
use const Widmogrod\Monad\IO\pure;
use const Widmogrod\Monad\State\value;
interface TeletypeF extends Functor
{
}
class PutStrLn implements TeletypeF
{
public $str;
public $next;
public function __construct($str, ff\MonadFree $next)
{
$this->str = $str;
$this->next = $next;
}
/**
* @inheritdoc
*/
public function map(callable $function): Functor
{
return new self(
$this->str,
$function($this->next)
);
}
}
class GetLine implements TeletypeF
{
/**
* @var callable
*/
public $processor;
public function __construct(callable $processor)
{
$this->processor = $processor;
}
/**
* @inheritdoc
*/
public function map(callable $function): Functor
{
return new self(function ($x) use ($function) {
return $function(($this->processor)($x));
});
}
}
class ExitSuccess implements TeletypeF
{
/**
* @inheritdoc
*/
public function map(callable $function): Functor
{
return $this;
}
}
const putStrLn_ = 'example2\putStrLn_';
// putStrLn' :: String -> Teletype ()
function putStrLn_($str)
{
return ff\liftF(new PutStrLn($str, ff\Pure::of(null)));
}
const getLine_ = 'example2\getLine_';
// getLine' :: Teletype String
function getLine_()
{
return ff\liftF(new GetLine(ff\Pure::of));
}
const exitSuccess_ = 'example2\exitSuccess_';
// exitSuccess' :: Teletype r
function exitSuccess_()
{
return ff\liftF(new ExitSuccess());
}
const interpretIO = 'example2\interpretIO';
// run :: TeletypeF -> IO ()
function interpretIO(TeletypeF $r)
{
return matchPatterns([
PutStrLn::class => function (PutStrLn $a) {
return IO\putStrLn($a->str)->map(function () use ($a) {
return $a->next;
});
},
GetLine::class => function (GetLine $a) {
return IO\getLine()->bind($a->processor);
},
ExitSuccess::class => function (ExitSuccess $a) {
return IO\putStrLn('exit')->bind(ff\Pure::of);
},
], $r);
}
const interpretState = 'example2\interpretState';
// runTest :: TeletypeF -> State MonadFree []
function interpretState(TeletypeF $r)
{
return matchPatterns([
PutStrLn::class => function (PutStrLn $a) {
return State::of(function (Listt $state) use ($a) {
return [
$a->next,
f\append($state, f\fromValue('PutStrLn'))
];
});
},
GetLine::class => function (GetLine $a) {
return State::of(function (Listt $state) use ($a) {
return [
($a->processor)('demo'),
f\append($state, f\fromValue('GetLine'))
];
});
},
ExitSuccess::class => function (ExitSuccess $a) {
return State::of(function (Listt $state) use ($a) {
return [
ff\Pure::of('exit'),
f\append($state, f\fromValue('ExitSuccess'))
];
});
},
], $r);
}
function echo_chaining_()
{
return getLine_()
->bind(function ($str) {
return putStrLn_($str)
->bind(function () {
return exitSuccess_()
->bind(function () {
// In interpretation of IO Monad this place will never be reached
return putStrLn_('Finished');
});
});
});
}
function echo_composition_()
{
return f\pipeline(
getLine_,
f\bind(putStrLn_),
f\bind(exitSuccess_),
f\bind(putStrLn_) // In interpretation of IO Monad this place will never be reached
)();
}
class FreeMonadTest extends TestCase
{
#[DataProvider('provideEchoImplementation')]
public function test_it_should_allow_to_interpret_as_a_state_monad(MonadFree $echo)
{
$result = ff\foldFree(interpretState, $echo, value);
$this->assertInstanceOf(State::class, $result);
$result = State\execState($result, fromNil());
$this->assertEquals($result, f\fromIterable([
'GetLine',
'PutStrLn',
'ExitSuccess',
]));
}
#[DataProvider('provideEchoImplementation')]
public function test_it_should_allow_to_interpret_as_IO(MonadFree $echo)
{
$result = ff\foldFree(interpretIO, $echo, pure);
$this->assertInstanceOf(IO::class, $result);
// Since this requires input, which would block unit
// This test serves as an example, uncomment line bellow
// for your local test
// $result->run();
}
public static function provideEchoImplementation()
{
return [
'echo implementation via explicit chaining (bind)' => [echo_chaining_()],
'echo implementation via function composition' => [echo_composition_()],
];
}
}