-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathTypeclassless.kt
143 lines (101 loc) · 3.73 KB
/
Typeclassless.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
package arrow
import arrow.TypeclasslessExamples.ScopeOne.inScopeOne
import arrow.TypeclasslessExamples.ScopeTwo.withAll
import arrow.TypeclasslessExamples.ScopeTwo.withApplicative
import arrow.core.identity
import arrow.data.ForListK
import arrow.data.ListK
import arrow.data.applicative
import arrow.data.k
import arrow.typeclasses.Applicative
import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.FreeSpec
class TypeclasslessExamples : FreeSpec() {
///////////////////////////////////
//// BUILDING THE MACHINERY
///////////////////////////////////
// Complete example of syntax using a simple fake typeclass
interface Identity<F> {
fun <A> identify(a: Kind<F, A>): Kind<F, A> =
a
}
interface IdentifySyntax<F> {
fun ID(): Identity<F>
fun <A> Kind<F, A>.identify(): Kind<F, A> =
ID().identify(this)
}
fun <F> Identity<F>.s() =
object : IdentifySyntax<F> {
override fun ID(): Identity<F> =
this@s
}
// Syntax for existing typeclass
interface ApplicativeSyntax<F> {
fun AP(): Applicative<F>
fun <A> A.just(): Kind<F, A> =
AP().just(this)
fun <A, B> Kind<F, A>.map(f: (A) -> B): Kind<F, B> = AP().run {
this@map.map(f)
}
}
fun <F> Applicative<F>.s() =
object : ApplicativeSyntax<F> {
override fun AP(): Applicative<F> =
this@s
}
// How to define a requirement on multiple typeclasses
interface ApplicativeAndIdentifySyntax<F> : ApplicativeSyntax<F>, IdentifySyntax<F>
fun <F> allSyntax(AP: Applicative<F>, ID: Identity<F>): ApplicativeAndIdentifySyntax<F> =
object : ApplicativeAndIdentifySyntax<F>, ApplicativeSyntax<F> by AP.s(), IdentifySyntax<F> by ID.s() {}
///////////////////////////////////
//// DEFINING OUR DEPENDENCIES
///////////////////////////////////
// Define some trivial instances for Identify and Applicative
val ID_LIST: Identity<ForListK> =
object : Identity<ForListK> {}
val AP =
ListK.applicative()
val ALL_SYNTAX =
allSyntax(AP, ID_LIST)
// Functions depending on syntax
object ScopeOne {
fun <F> ApplicativeSyntax<F>.inScopeOne(): Kind<F, Int> =
1.just()
}
object ScopeTwo {
fun <F> IdentifySyntax<F>.withIdentify(a: Kind<F, Int>): Kind<F, Int> =
a.identify()
fun <F> ApplicativeSyntax<F>.withApplicative(): Kind<F, Int> =
1.just().map { inScopeOne() }.map { 1 }
fun <F> ApplicativeAndIdentifySyntax<F>.withAll(): Kind<F, Int> =
withIdentify(withApplicative()).identify().map(::identity)
}
// It only works inside classes if they extend the syntax, although it's inheritable!
open class Parent<F>(ALL_SYNTAX: ApplicativeAndIdentifySyntax<F>, val nest: Parent<F>?) : ApplicativeAndIdentifySyntax<F> by ALL_SYNTAX {
fun inClass() = withAll()
fun ApplicativeAndIdentifySyntax<F>.insideClass() = withApplicative()
fun insideClassNested() = nest?.insideClass() ?: insideClass()
}
class Child<F>(ALL_SYNTAX: ApplicativeAndIdentifySyntax<F>) : Parent<F>(ALL_SYNTAX, null) {
fun insideClassFromChild() = insideClass()
}
///////////////////////////////////
//// USING TYPECLASSLESS STYLE!
///////////////////////////////////
init {
val expected = listOf(1).k()
"Injection typeclassless style for functions" {
ALL_SYNTAX.inScopeOne() shouldBe expected
ALL_SYNTAX.withApplicative() shouldBe expected
ALL_SYNTAX.withAll() shouldBe expected
}
"Injection typeclassless style for classes" {
val child = Child(ALL_SYNTAX)
val parent = Parent(ALL_SYNTAX, child)
parent.inClass() shouldBe expected
parent.run { insideClass() } shouldBe expected
parent.insideClassNested() shouldBe expected
child.insideClassFromChild() shouldBe expected
}
}
}