-
Notifications
You must be signed in to change notification settings - Fork 29
/
Shift.scala
209 lines (185 loc) · 6.2 KB
/
Shift.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
package com.thoughtworks.dsl
package keywords
import Dsl.IsKeyword
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.keywords.Shift.{
SameDomainStackSafeShiftDsl,
StackSafeShiftDsl
}
import scala.annotation.tailrec
import scala.language.implicitConversions
import scala.util.control.{NonFatal, TailCalls}
import scala.util.control.TailCalls.TailRec
/** A keyword that extracts the value from a [[domains.Continuation]].
* @note
* This [[Shift]] keyword includes special treatment for exception handling
* and stack safety. Always use `Shift(cont).cpsApply { x => ... }` instead
* of `cont { x => ... }` to register a handler for the continuation,
* otherwise exception might be uncaught or stack might overflow.
* @example
* Given a continuation whose type is `Unit !! Throwable !! Int`, it is
* considered as having an exception handler. When an exception is thrown,
* {{{
* import scala.util.control.TailCalls.TailRec
* type !![R, +A] = (A => R) => R
* val cont: Unit !! Throwable !! Int = _ {
* if (System.nanoTime() > 0) {
* throw new Exception("my-exception")
* } else {
* 42
* }
* }
* }}}
*
* Then `cpsApply` should catch the exception:
* {{{
* Shift(cont).cpsApply[Unit !! Throwable] { i: Int => (failureHandler: Throwable => Unit) =>
* fail("unreachable code")
* } { e: Throwable =>
* e.getMessage should be("my-exception")
* }
* }}}
*
* However, `cont.apply` does not catch the exception:
* {{{
* an[Exception] should be thrownBy {
* cont.apply { i => failureHandler =>
* fail("unreachable code")
* } { e =>
* e.getMessage should be("my-exception")
* }
* }
* }}}
* @author
* 杨博 (Yang Bo)
*/
opaque type Shift[R, A] <: Dsl.Keyword.Opaque = Dsl.Keyword.Opaque.Of[R !! A]
private[keywords] trait LowPriorityShift1 {
@inline
implicit def stackUnsafeShiftDsl[Domain, Value]
: Dsl.Original[Shift[Domain, Value], Domain, Value] =
Dsl.Original { (shift: Shift[Domain, Value], handler: Value => Domain) =>
Shift.flip(shift)(handler)
}
}
private[keywords] trait LowPriorityShift0 extends LowPriorityShift1 {
this: Shift.type =>
given [LeftDomain, RightDomain, Value](using
restDsl: SameDomainStackSafeShiftDsl[LeftDomain, RightDomain]
): SameDomainStackSafeShiftDsl[LeftDomain !! RightDomain, Value] =
SameDomainStackSafeShiftDsl[LeftDomain !! RightDomain, Value] {
val restDsl0 =
Shift
.liftContra[[X] =>> Dsl.Original[X, LeftDomain, RightDomain]](
restDsl
);
{ (keyword, handler) =>
keyword { value =>
restDsl0(handler(value), _)
}
}
}
}
object Shift extends LowPriorityShift0 {
given [Domain, Value]: IsKeyword[Shift[Domain, Value], Value] with {}
opaque type StackSafeShiftDsl[Domain, NewDomain, Value] <: Dsl.Original[Shift[
Domain,
Value
], NewDomain, Value] = Dsl.Original[Shift[Domain, Value], NewDomain, Value]
def StackSafeShiftDsl[Domain, NewDomain, Value]: (
(
Shift[Domain, Value],
Value => NewDomain
) => NewDomain
) =:= StackSafeShiftDsl[Domain, NewDomain, Value] =
Dsl.Original[Shift[Domain, Value], NewDomain, Value]
private[keywords] type SameDomainStackSafeShiftDsl[Domain, Value] =
StackSafeShiftDsl[Domain, Domain, Value]
def SameDomainStackSafeShiftDsl[Domain, Value] =
StackSafeShiftDsl[Domain, Domain, Value]
extension [C, R, A](inline fa: C)(using
inline notKeyword: util.NotGiven[
C <:< Dsl.Keyword
],
inline asContinuation: C <:< (R !! A)
)
transparent inline def unary_! : A =
Dsl.shift[Shift[R, A], A](Shift[R, A](asContinuation(fa))): A
private def shiftTailRec[R, Value](
continuation: TailRec[R] !! Value,
handler: Value => TailRec[R]
) = {
continuation { a =>
val handler1 = handler
TailCalls.tailcall(handler1(a))
}
}
@inline
given tailRecShiftDsl[R, Value]
: SameDomainStackSafeShiftDsl[TailRec[R], Value] =
SameDomainStackSafeShiftDsl {
(
keyword: Shift[TailRec[R], Value],
handler: Value => TailRec[R]
) =>
shiftTailRec(keyword, handler)
}
private def suspend[LeftDomain, Value](
continuation: LeftDomain !! Throwable !! Value,
handler: Value => LeftDomain !! Throwable
): Dsl.TrampolineContinuation[LeftDomain] =
new Dsl.TrampolineContinuation[LeftDomain] {
protected def step() = continuation(handler)
}
@inline
given [LeftDomain, Value]
: SameDomainStackSafeShiftDsl[LeftDomain !! Throwable, Value] =
SameDomainStackSafeShiftDsl[LeftDomain !! Throwable, Value] {
(
keyword: Shift[LeftDomain !! Throwable, Value],
handler: Value => LeftDomain !! Throwable
) =>
suspend(keyword, handler)
}
private def flatMapTrampoline[LeftDomain, RightDomain, Value](
handler: Value => LeftDomain !! Throwable !! RightDomain,
value: Value,
continue: RightDomain => LeftDomain !! Throwable
): Dsl.TrampolineContinuation[LeftDomain] =
new Dsl.TrampolineContinuation[LeftDomain] {
protected def step() = {
handler(value)(continue)
}
}
private def taskFlatMap[LeftDomain, RightDomain, Value](
task: LeftDomain !! Throwable !! Value,
handler0: Value => LeftDomain !! Throwable !! RightDomain
): LeftDomain !! Throwable !! RightDomain = { continue0 =>
val handler1 = handler0
task { value =>
val handler = handler1
val continue = continue0
flatMapTrampoline(handler, value, continue)
}
}
@inline
given [LeftDomain, RightDomain, Value]: StackSafeShiftDsl[
LeftDomain !! Throwable,
LeftDomain !! Throwable !! RightDomain,
Value
] =
StackSafeShiftDsl[
LeftDomain !! Throwable,
LeftDomain !! Throwable !! RightDomain,
Value
] {
(
keyword: Shift[!![LeftDomain, Throwable], Value],
handler: Value => !![!![LeftDomain, Throwable], RightDomain]
) =>
taskFlatMap(keyword, handler)
}
}
def Shift[R, A](using
dummyImplicit: DummyImplicit = DummyImplicit.dummyImplicit
): (R !! A) =:= Shift[R, A] = Dsl.Keyword.Opaque.Of