-
Notifications
You must be signed in to change notification settings - Fork 8
/
State.php
186 lines (168 loc) · 4.08 KB
/
State.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
<?php
declare(strict_types=1);
namespace Marcosh\LamPHPda;
use Marcosh\LamPHPda\Brand\StateBrand;
use Marcosh\LamPHPda\HK\HK1;
use Marcosh\LamPHPda\Instances\State\StateApplicative;
use Marcosh\LamPHPda\Instances\State\StateApply;
use Marcosh\LamPHPda\Instances\State\StateFunctor;
use Marcosh\LamPHPda\Instances\State\StateMonad;
use Marcosh\LamPHPda\Typeclass\Applicative;
use Marcosh\LamPHPda\Typeclass\Apply;
use Marcosh\LamPHPda\Typeclass\DefaultInstance\DefaultMonad;
use Marcosh\LamPHPda\Typeclass\Functor;
use Marcosh\LamPHPda\Typeclass\Monad;
/**
* @see https://github.com/marcosh/lamphpda/tree/master/docs/data-structures/State.md
*
* @template S
* @template-covariant A
*
* @implements DefaultMonad<StateBrand<S>, A>
*
* @psalm-immutable
*/
final class State implements DefaultMonad
{
/** @var callable(S): Pair<S, A> */
private $runState;
/**
* @param callable(S): Pair<S, A> $runState
*/
private function __construct(callable $runState)
{
$this->runState = $runState;
}
/**
* @template T
* @template B
* @param callable(T): Pair<T, B> $runState
* @return State<T, B>
*
* @psalm-pure
*/
public static function state(callable $runState): self
{
return new self($runState);
}
/**
* @template B
* @template T
* @param HK1<StateBrand<T>, B> $b
* @return State<T, B>
*
* @psalm-pure
*/
public static function fromBrand(HK1 $b): self
{
/** @var State<T, B> */
return $b;
}
/**
* @param S $state
* @return Pair<S, A>
*/
public function runState(mixed $state): Pair
{
/** @psalm-suppress ImpureFunctionCall */
return ($this->runState)($state);
}
/**
* @param S $state
* @return A
*/
public function evalState(mixed $state): mixed
{
return $this->runState($state)->second();
}
/**
* @param S $state
* @return S
*/
public function execState(mixed $state): mixed
{
return $this->runState($state)->first();
}
/**
* @template B
* @param Functor<StateBrand<S>> $functor
* @param callable(A): B $f
* @return State<S, B>
*/
public function imap(Functor $functor, callable $f): self
{
return self::fromBrand($functor->map($f, $this));
}
/**
* @template B
* @param callable(A): B $f
* @return State<S, B>
*/
public function map(callable $f): self
{
return $this->imap(new StateFunctor(), $f);
}
/**
* @template B
* @param Apply<StateBrand<S>> $apply
* @param HK1<StateBrand<S>, callable(A): B> $f
* @return State<S, B>
*/
public function iapply(Apply $apply, HK1 $f): self
{
return self::fromBrand($apply->apply($f, $this));
}
/**
* @template B
* @param HK1<StateBrand<S>, callable(A): B> $f
* @return State<S, B>
*/
public function apply(HK1 $f): self
{
return $this->iapply(new StateApply(), $f);
}
/**
* @template B
* @template T
* @param Applicative<StateBrand<T>> $applicative
* @param B $a
* @return State<T, B>
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, mixed $a): self
{
return self::fromBrand($applicative->pure($a));
}
/**
* @template B
* @template T
* @param B $a
* @return State<T, B>
*
* @psalm-pure
*/
public static function pure(mixed $a): self
{
return self::ipure(new StateApplicative(), $a);
}
/**
* @template B
* @param Monad<StateBrand<S>> $monad
* @param callable(A): HK1<StateBrand<S>, B> $f
* @return State<S, B>
*/
public function ibind(Monad $monad, callable $f): self
{
return self::fromBrand($monad->bind($this, $f));
}
/**
* @template B
* @param callable(A): HK1<StateBrand<S>, B> $f
* @return State<S, B>
*/
public function bind(callable $f): self
{
return $this->ibind(new StateMonad(), $f);
}
}