/
Kleisli.kt
213 lines (186 loc) · 6.41 KB
/
Kleisli.kt
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
package arrow.data
import arrow.Kind
import arrow.core.Either
import arrow.core.Tuple2
import arrow.core.identity
import arrow.higherkind
import arrow.typeclasses.Applicative
import arrow.typeclasses.Functor
import arrow.typeclasses.Monad
import arrow.typeclasses.MonadError
/**
* Alias that represents an arrow from [D] to a monadic value `Kind<F, A>`
*/
typealias KleisliFun<F, D, A> = (D) -> Kind<F, A>
/**
* [Kleisli] represents an arrow from [D] to a monadic value `Kind<F, A>`.
*
* @param F the context of the result.
* @param D the dependency or environment we depend on.
* @param A resulting type of the computation.
* @property run the arrow from [D] to `Kind<F, A>`.
*/
@higherkind
class Kleisli<F, D, A>(val run: KleisliFun<F, D, A>) : KleisliOf<F, D, A>, KleisliKindedJ<F, D, A> {
/**
* Apply a function `(A) -> B` that operates within the [Kleisli] context.
*
* @param ff function with the [Kleisli] context.
* @param AF [Applicative] for the context [F].
*/
fun <B> ap(AF: Applicative<F>, ff: KleisliOf<F, D, (A) -> B>): Kleisli<F, D, B> =
AF.run { Kleisli { run(it).ap(ff.fix().run(it)) } }
/**
* Map the end of the arrow [A] to [B] given a function [f].
*
* @param f the function to apply.
* @param FF [Functor] for the context [F].
*/
fun <B> map(FF: Functor<F>, f: (A) -> B): Kleisli<F, D, B> = FF.run {
Kleisli { a -> run(a).map { f(it) } }
}
/**
* FlatMap the end of the arrow [A] to another [Kleisli] arrow for the same start [D] and context [F].
*
* @param f the function to flatmap.
* @param MF [Monad] for the context [F].
*/
fun <B> flatMap(MF: Monad<F>, f: (A) -> Kleisli<F, D, B>): Kleisli<F, D, B> = MF.run {
Kleisli { d ->
run(d).flatMap { a -> f(a).run(d) }
}
}
/**
* Zip with another [Kleisli] arrow.
*
* @param o other [Kleisli] to zip with.
* @param MF [Monad] for the context [F].
*/
fun <B> zip(MF: Monad<F>, o: Kleisli<F, D, B>): Kleisli<F, D, Tuple2<A, B>> =
flatMap(MF) { a ->
o.map(MF) { b -> Tuple2(a, b) }
}
/**
* Compose this arrow with another function to transform the input of the arrow.
*
* @param f function that transforms new arrow head [DD] to [D].
*/
fun <DD> local(f: (DD) -> D): Kleisli<F, DD, A> = Kleisli { dd -> run(f(dd)) }
/**
* Compose with another [Kleisli].
*
* @param o other [Kleisli] to compose with.
* @param MF [Monad] for the context [F].
*/
fun <C> andThen(MF: Monad<F>, f: Kleisli<F, A, C>): Kleisli<F, D, C> = andThen(MF, f.run)
/**
* Compose with a function to transform the output of the [Kleisli] arrow.
*
* @param f the function to apply.
* @param MF [Monad] for the context [F].
*/
fun <B> andThen(MF: Monad<F>, f: (A) -> Kind<F, B>): Kleisli<F, D, B> = MF.run {
Kleisli { run(it).flatMap(f) }
}
/**
* Set the end of the arrow to `Kind<F, B>` after running the computation.
*
* @param fb the new end of the arrow.
* @param MF [Monad] for the context [F].
*/
fun <B> andThen(MF: Monad<F>, fb: Kind<F, B>): Kleisli<F, D, B> = andThen(MF) { fb }
/**
* Handle error within context of [F] given a [MonadError] is defined for [F].
*
* @param f function to handle error.
* @param ME [MonadError] for the context [F].
*/
fun <E> handleErrorWith(ME: MonadError<F, E>, f: (E) -> KleisliOf<F, D, A>): Kleisli<F, D, A> = Kleisli {
ME.run { run(it).handleErrorWith { e: E -> f(e).fix().run(it) } }
}
companion object {
/**
* Constructor to create `Kleisli<F, D, A>` given a [KleisliFun].
*
* @param run the arrow from [D] to a monadic value `Kind<F, A>`
*/
operator fun <F, D, A> invoke(run: KleisliFun<F, D, A>): Kleisli<F, D, A> = Kleisli(run)
/**
* Tail recursive function that keeps calling [f] until [arrow.Either.Right] is returned.
*
* @param a initial value to start running recursive call to [f]
* @param f function that is called recusively until [arrow.Either.Right] is returned.
* @param MF [Monad] for the context [F].
*/
fun <F, D, A, B> tailRecM(MF: Monad<F>, a: A, f: (A) -> KleisliOf<F, D, Either<A, B>>): Kleisli<F, D, B> =
Kleisli { b -> MF.tailRecM(a) { f(it).fix().run(b) } }
/**
* Create an arrow for a value of [A].
*
* @param x value of [A].
* @param AF [Applicative] for context [F].
*/
inline fun <F, D, A> just(AF: Applicative<F>, x: A): Kleisli<F, D, A> = Kleisli { _ -> AF.just(x) }
/**
* Ask an arrow from [D] to [D].
*
* @param AF [Applicative] for context [F].
*/
inline fun <F, D> ask(AF: Applicative<F>): Kleisli<F, D, D> = Kleisli { AF.just(it) }
/**
* Raise an error [E].
* @param ME [MonadError] for context [F].
*/
fun <F, D, E, A> raiseError(ME: MonadError<F, E>, e: E): Kleisli<F, D, A> = Kleisli { ME.raiseError(e) }
}
}
/**
* Flatten nested [Kleisli] arrows.
*
* @param MF [Monad] for the context [F].
*/
fun <F, D, A> KleisliOf<F, D, Kleisli<F, D, A>>.flatten(MF: Monad<F>): Kleisli<F, D, A> = fix().flatMap(MF, ::identity)
/**
* Syntax for constructing a [Kleisli]
*
* @receiver [KleisliFun] a function that represents computation dependent on [D] with the result in context [F].
*/
fun <F, D, A> KleisliFun<F, D, A>.kleisli(): Kleisli<F, D, A> = Kleisli(this)
/**
* Alias that represents a computation that has a dependency on [D].
*/
typealias ReaderTFun<F, D, A> = KleisliFun<F, D, A>
/**
* Alias ReaderTHK for [KleisliHK]
*
* @see KleisliHK
*/
typealias ForReaderT = ForKleisli
/**
* Alias ReaderTKind for [KleisliKind]
*
* @see KleisliKind
*/
typealias ReaderTOf<F, D, A> = KleisliOf<F, D, A>
/**
* Alias to partially apply type parameter [F] and [D] to [ReaderT].
*
* @see KleisliKindPartial
*/
typealias ReaderTPartialOf<F, D> = KleisliPartialOf<F, D>
/**
* [Reader] represents a computation that has a dependency on [D] with a result within context [F].
* `ReaderT<F, D, A>` is the monad transfomer variant of [Reader] and an alias for `Kleisli<F, D, A>`.
*
* @param F the context of the result.
* @param D the dependency or environment we depend on.
* @param A resulting type of the computation.
* @see Kleisli
*/
typealias ReaderT<F, D, A> = Kleisli<F, D, A>
/**
* Syntax for constructing a [ReaderT]
*
* @receiver [ReaderTFun] a function that represents computation dependent on [D] with the result in context [F].
*/
fun <F, D, A> ReaderTFun<F, D, A>.readerT(): ReaderT<F, D, A> = ReaderT(this)