Widmogrod\Helpful\MonadLaws::test PHP Method

test() public static method

Generic test to verify if a type obey the monad laws.
public static test ( callable $assertEqual, callable $return, callable $f, callable $g, mixed $x )
$assertEqual callable Asserting function (Monad $m1, Monad $m2, $message)
$return callable Monad "constructor"
$f callable Monadic function
$g callable Monadic function
$x mixed Value to put into a monad
    public static function test(callable $assertEqual, callable $return, callable $f, callable $g, $x)
    {
        // Make reading bellow tests easier
        $m = $return($x);
        // left identity: (return x) >>= f ≡ f x
        $assertEqual(f\bind($f, $return($x)), $f($x), 'left identity');
        // right identity: m >>= return ≡ m
        $assertEqual(f\bind($return, $m), $m, 'right identity');
        // associativity: (m >>= f) >>= g ≡ m >>= ( \x -> (f x >>= g) )
        $assertEqual(f\bind($g, f\bind($f, $m)), f\bind(function ($x) use($f, $g) {
            return f\bind($g, $f($x));
        }, $m), 'associativity');
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @dataProvider provideData
  */
 public function test_if_io_monad_obeys_the_laws($f, $g, $x)
 {
     MonadLaws::test(function (IO $f, IO $g, $message) {
         $this->assertEquals($f->run(), $g->run(), $message);
     }, function ($x) {
         return IO::of(function () use($x) {
             return $x;
         });
     }, $f, $g, $x);
 }
All Usage Examples Of Widmogrod\Helpful\MonadLaws::test
MonadLaws