-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathApplicativeLaws.php
More file actions
64 lines (57 loc) · 1.85 KB
/
ApplicativeLaws.php
File metadata and controls
64 lines (57 loc) · 1.85 KB
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
<?php
declare(strict_types=1);
namespace FunctionalPHP\FantasyLand\Helpful;
use FunctionalPHP\FantasyLand\Applicative;
use const FunctionalPHP\FantasyLand\compose;
use const FunctionalPHP\FantasyLand\identity;
use function FunctionalPHP\FantasyLand\applicator;
use function FunctionalPHP\FantasyLand\curryN;
class ApplicativeLaws
{
/**
* Generic test to verify if a type obey the applicative laws.
*
* @param callable $assertEqual Asserting function (Applicative $a1, Applicative $a2, $message)
* @param callable $pure Applicative "constructor"
* @param Applicative $u Applicative f => f (a -> b)
* @param Applicative $v Applicative f => f (a -> b)
* @param Applicative $w Applicative f => f (a -> b)
* @param callable $f (a -> b)
* @param mixed $x Value to put into a applicative
*/
public static function test(
callable $assertEqual,
callable $pure,
Applicative $u,
Applicative $v,
Applicative $w,
callable $f,
$x
) {
// identity: pure id <*> v = v
$assertEqual(
$pure(identity)->ap($v),
$v,
'identity'
);
// homomorphism: pure f <*> pure x = pure (f x)
$assertEqual(
$pure($f)->ap($pure($x)),
$pure($f($x)),
'homomorphism'
);
// interchange: u <*> pure x = pure ($ x) <*> u
$assertEqual(
$u->ap($pure($x)),
$pure(applicator($x))->ap($u),
'interchange'
);
// composition: pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
$compose = curryN(2, compose);
$assertEqual(
$pure($compose)->ap($u)->ap($v)->ap($w),
$u->ap($v->ap($w)),
'composition'
);
}
}