namespace example;
+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 function Widmogrod\Functional\append;
-use function Widmogrod\Functional\bind;
-use function Widmogrod\Functional\match;
-use function Widmogrod\Functional\pipeline;
-use function Widmogrod\Monad\Free\liftF;
-use function Widmogrod\Monad\Free\runFree;
-use function Widmogrod\Monad\IO\getLine;
interface TeletypeF
{
// putStrLn' :: String -> Teletype ()
function putStrLn_($str)
{
- return liftF(new PutStrLn($str, null));
+ return ff\liftF(new PutStrLn($str, null));
}
const getLine_ = 'example\getLine_';
// getLine' :: Teletype String
function getLine_()
{
- return liftF(new GetLine());
+ return ff\liftF(new GetLine());
}
const exitSuccess_ = 'example\exitSuccess_';
// exitSuccess' :: Teletype r
function exitSuccess_()
{
- return liftF(new ExitSuccess());
+ return ff\liftF(new ExitSuccess());
}
const interpretIO = 'example\interpretIO';
// run :: TeletypeF IO ()
function interpretIO(TeletypeF $r)
{
- return match([
+ return f\match([
PutStrLn::class => function (PutStrLn $a) {
return IO\putStrLn($a->str);
},
GetLine::class => function (GetLine $a) {
- return getLine();
+ return IO\getLine();
},
ExitSuccess::class => function (ExitSuccess $a) {
return exit();
// runTest :: TeletypeF State []
function interpretState(TeletypeF $r)
{
- return match([
+ return f\match([
PutStrLn::class => function (PutStrLn $a) {
return State::of(function ($state) use ($a) {
- return ['PutStrLn', append($state, 'PutStrLn')];
+ return ['PutStrLn', f\append($state, 'PutStrLn')];
});
},
GetLine::class => function (GetLine $a) {
return State::of(function ($state) {
- return ['GetLine', append($state, 'GetLine')];
+ return ['GetLine', f\append($state, 'GetLine')];
});
},
ExitSuccess::class => function (ExitSuccess $a) {
return State::of(function ($state) {
- return ['ExitSuccess', append($state, 'ExitSuccess')];
+ return ['ExitSuccess', f\append($state, 'ExitSuccess')];
});
},
], $r);
function echo_composition_()
{
- return pipeline(
+ return call_user_func(f\pipeline(
getLine_,
- bind(putStrLn_),
- bind(exitSuccess_),
- bind(putStrLn_) // In interpretation of IO Monad this place will never be reached
- )(null);
+ f\bind(putStrLn_),
+ f\bind(exitSuccess_),
+ f\bind(putStrLn_) // In interpretation of IO Monad this place will never be reached
+ ));
}
class FreeMonadTest extends \PHPUnit_Framework_TestCase
*/
public function test_it_should_allow_to_interpret_as_a_state_monad(MonadFree $echo)
{
- $result = runFree(interpretState, $echo);
+ $result = ff\runFree(interpretState, $echo);
$this->assertInstanceOf(State::class, $result);
$result = State\execState($result, []);
*/
public function test_it_should_allow_to_interpret_as_IO(MonadFree $echo)
{
- $result = runFree(interpretIO, $echo);
+ $result = ff\runFree(interpretIO, $echo);
$this->assertInstanceOf(IO::class, $result);
// Since in PHPUnit STDIN is closed
// this run will not work, but serves as an example