-
Notifications
You must be signed in to change notification settings - Fork 9
/
Edomaton.scala
308 lines (263 loc) · 9.48 KB
/
Edomaton.scala
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright 2021 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edomata.core
import cats.*
import cats.arrow.FunctionK
import cats.data.*
import cats.implicits.*
import cats.kernel.Eq
import Edomaton.*
/** Represents programs that are event driven state machines (a Mealy machine)
*
* these programs can use input to decide on a state transition, and optionally
* emit a sequence of notifications for communication.
*
* @tparam F
* effect type
* @tparam Env
* input type
* @tparam R
* rejection type
* @tparam E
* internal event type
* @tparam N
* notification type, a.k.a external event, integration event
* @tparam A
* output type
*/
final case class Edomaton[F[_], -Env, R, E, N, A](
run: Env => F[ResponseD[R, E, N, A]]
) extends AnyVal {
/** maps output result */
def map[B](f: A => B)(using Functor[F]): Edomaton[F, Env, R, E, N, B] =
transform(_.map(f))
/** creates a new edomaton that translates some input to what this edomaton
* can understand
*/
def contramap[Env2](f: Env2 => Env): Edomaton[F, Env2, R, E, N, A] =
Edomaton(
run.compose(f)
)
/** binds another edomaton to this one. */
def flatMap[Env2 <: Env, B](
f: A => Edomaton[F, Env2, R, E, N, B]
)(using Monad[F]): Edomaton[F, Env2, R, E, N, B] =
Edomaton(env =>
run(env).flatMap { r =>
r.result.visit(
err => r.copy(result = Decision.Rejected(err)).pure[F],
f(_)
.run(env)
.map(o =>
o.result.visit(
_ => o,
_ => r >> o
)
)
)
}
)
/** alias for flatMap */
inline def >>=[Env2 <: Env, B](
f: A => Edomaton[F, Env2, R, E, N, B]
)(using Monad[F]): Edomaton[F, Env2, R, E, N, B] = flatMap(f)
/** alias for andThen, flatMap(_ => f) */
inline def >>[Env2 <: Env, B](
f: => Edomaton[F, Env2, R, E, N, B]
)(using Monad[F]): Edomaton[F, Env2, R, E, N, B] = andThen(f)
/** sequences another edomaton without considering output for this one
*
* alias for flatMap(_ => f)
*/
inline def andThen[Env2 <: Env, B](
f: => Edomaton[F, Env2, R, E, N, B]
)(using Monad[F]): Edomaton[F, Env2, R, E, N, B] = flatMap(_ => f)
/** transforms underlying response
*/
def transform[R2, E2, N2, B](
f: ResponseD[R, E, N, A] => ResponseD[R2, E2, N2, B]
)(using
Functor[F]
): Edomaton[F, Env, R2, E2, N2, B] =
Edomaton(run.andThen(_.map(f)))
/** translates this program in another language mapped by a natural
* transformation
*/
def mapK[G[_]](fk: FunctionK[F, G]): Edomaton[G, Env, R, E, N, A] =
Edomaton(run.andThen(fk.apply))
/** evaluates an effect using output of this edomaton and uses result of
* evaluation as new output
*/
def evalMap[B](f: A => F[B])(using
Monad[F]
): Edomaton[F, Env, R, E, N, B] =
flatMap(a => liftF(f(a).map(ResponseD.pure)))
/** like evalMap but ignores evaluation result
*/
def evalTap[B](f: A => F[B])(using
Monad[F]
): Edomaton[F, Env, R, E, N, A] =
flatMap(a => liftF(f(a).map(ResponseD.pure)).as(a))
/** evaluates an effect and uses its result as new output */
def eval[B](f: => F[B])(using Monad[F]): Edomaton[F, Env, R, E, N, A] =
evalTap(_ => f)
/** alias for map(_=> b) */
inline def as[B](b: B)(using Functor[F]): Edomaton[F, Env, R, E, N, B] =
map(_ => b)
/** Ignores output value */
def void(using Functor[F]): Edomaton[F, Env, R, E, N, Unit] = as(())
/** Decides based on output */
def decide[B](f: A => Decision[R, E, B])(using
Monad[F]
): Edomaton[F, Env, R, E, N, B] =
flatMap(a => Edomaton.decide(f(a)))
/** Clears all notifications so far */
def reset(using Functor[F]): Edomaton[F, Env, R, E, N, A] =
transform(_.reset)
/** Adds notification without considering decision state */
def publish(ns: N*)(using Functor[F]): Edomaton[F, Env, R, E, N, A] =
transform(_.publish(ns: _*))
/** If this edomaton is rejected, uses given function to decide what to
* publish
*/
def publishOnRejectionWith(
f: NonEmptyChain[R] => Seq[N]
)(using Functor[F]): Edomaton[F, Env, R, E, N, A] = transform(
_.publishOnRejectionWith(f)
)
/** publishes these notifications if this edomaton is rejected */
def publishOnRejection(ns: N*)(using
Functor[F]
): Edomaton[F, Env, R, E, N, A] =
transform(_.publishOnRejection(ns: _*))
}
object Edomaton extends EdomatonInstances, EdomatonConstructors
sealed transparent trait EdomatonInstances {
given [F[_]: Monad, Env, R, E, N]
: Monad[[t] =>> Edomaton[F, Env, R, E, N, t]] =
new Monad {
type G[T] = Edomaton[F, Env, R, E, N, T]
override def pure[A](x: A): G[A] =
Edomaton(_ => ResponseD.pure(x).pure[F])
override def map[A, B](fa: G[A])(f: A => B): G[B] = fa.map(f)
override def flatMap[A, B](fa: G[A])(f: A => G[B]): G[B] = fa.flatMap(f)
override def tailRecM[A, B](a: A)(
f: A => G[Either[A, B]]
): G[B] = Edomaton(env =>
Monad[F].tailRecM(ResponseD.pure(a): ResponseD[R, E, N, A])(rma =>
rma.result.visit(
_ => ???, // This cannot happen
a =>
f(a)
.run(env)
.map(rma >> _)
.map(o =>
o.result
.visit(
rej => o.copy(result = Decision.Rejected(rej)).asRight,
{
case Left(a) => o.as(a).asLeft
case Right(b) => o.as(b).asRight
}
)
)
)
)
)
}
given [F[_], Env, R, E, N, T](using
Eq[Env => F[ResponseD[R, E, N, T]]]
): Eq[Edomaton[F, Env, R, E, N, T]] =
Eq.by(_.run)
given [F[_], R, E, N, T]
: Contravariant[[env] =>> Edomaton[F, env, R, E, N, T]] =
new Contravariant {
override def contramap[A, B](fa: Edomaton[F, A, R, E, N, T])(
f: B => A
): Edomaton[F, B, R, E, N, T] = fa.contramap(f)
}
}
sealed transparent trait EdomatonConstructors {
/** constructs an edomaton that outputs a pure value */
def pure[F[_]: Applicative, Env, R, E, N, T](
t: T
): Edomaton[F, Env, R, E, N, T] =
Edomaton(_ => ResponseD.pure(t).pure)
/** an edomaton with trivial output */
def unit[F[_]: Applicative, Env, R, E, N, T]
: Edomaton[F, Env, R, E, N, Unit] =
pure(())
/** constructs an edomaton from an effect that results in a response */
def liftF[F[_], Env, R, E, N, T](
f: F[ResponseD[R, E, N, T]]
): Edomaton[F, Env, R, E, N, T] = Edomaton(_ => f)
/** constructs an edomaton with given response */
def lift[F[_]: Applicative, Env, R, E, N, T](
f: ResponseD[R, E, N, T]
): Edomaton[F, Env, R, E, N, T] = liftF(f.pure)
/** constructs an edomaton that evaluates an effect */
def eval[F[_]: Applicative, Env, R, E, N, T](
f: F[T]
): Edomaton[F, Env, R, E, N, T] = liftF(f.map(ResponseD.pure))
/** constructs an edomaton that runs an effect using its input */
def run[F[_]: Applicative, Env, R, E, N, T](
f: Env => F[T]
): Edomaton[F, Env, R, E, N, T] = Edomaton(
f.andThen(_.map(ResponseD.pure))
)
/** constructs an edomaton that outputs what's read */
def read[F[_]: Applicative, Env, R, E, N, T]: Edomaton[F, Env, R, E, N, Env] =
run(_.pure[F])
/** constructs an edomaton that publishes given notifications */
def publish[F[_]: Applicative, Env, R, E, N](
ns: N*
): Edomaton[F, Env, R, E, N, Unit] = lift(ResponseD.publish(ns: _*))
/** constructs an edomaton that rejects with given rejections */
def reject[F[_]: Applicative, Env, R, E, N, T](
r: R,
rs: R*
): Edomaton[F, Env, R, E, N, T] = lift(ResponseD.reject(r, rs: _*))
/** constructs an edomaton that decides the given decision */
def decide[F[_]: Applicative, Env, R, E, N, T](
d: Decision[R, E, T]
): Edomaton[F, Env, R, E, N, T] = lift(ResponseD(d))
def validate[F[_]: Applicative, Env, R, E, N, T](
v: ValidatedNec[R, T]
): Edomaton[F, Env, R, E, N, T] = lift(ResponseD.validate(v))
/** Constructs a program from an optional value, that outputs value if exists
* or rejects otherwise
*
* You can also use .toDecision syntax for more convenience
*/
def fromOption[F[_]: Applicative, Env, R, E, N, T](
opt: Option[T],
orElse: R,
other: R*
): Edomaton[F, Env, R, E, N, T] = opt.fold(reject(orElse, other: _*))(pure(_))
/** Constructs a program that either outputs a value or rejects
*/
def fromEither[F[_]: Applicative, Env, R, E, N, T](
eit: Either[R, T]
): Edomaton[F, Env, R, E, N, T] = eit.fold(reject(_), pure(_))
/** Constructs a program that either outputs a value or rejects
*
* You can also use .toDecision syntax for more convenience.
*/
def fromEitherNec[F[_]: Applicative, Env, R, E, N, T](
eit: EitherNec[R, T]
): Edomaton[F, Env, R, E, N, T] =
eit.fold(errs => lift(ResponseD(Decision.Rejected(errs))), pure(_))
}