Skip to content

Commit

Permalink
Rename fcontrol in preparation for a proper effect handler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kyay10 committed Jun 7, 2024
1 parent 18783a4 commit ea1ee91
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
22 changes: 11 additions & 11 deletions library/src/commonMain/kotlin/fcontrol.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import kotlin.jvm.JvmInline

@JvmInline
public value class Handle<Error, T> internal constructor(private val reader: Reader<suspend (Error) -> T>) {
public value class Fcontrol<Error, T> internal constructor(private val reader: Reader<suspend (Error) -> T>) {
public constructor() : this(Reader())

@ResetDsl
public suspend fun fcontrol(error: Error): T = reader.ask()(error)

@ResetDsl
public suspend fun <R> resetWithHandler(
handler: suspend (Error, Cont<T, R>) -> R, body: suspend () -> R
public suspend fun <R> resetFcontrol(
handler: suspend (Error, SubCont<T, R>) -> R, body: suspend () -> R
): R {
val prompt = Prompt<R>()
return prompt.pushPrompt(extraContext = reader.context { prompt.control { k -> handler(it, k) } }, body = body)
return prompt.pushPrompt(extraContext = reader.context { prompt.takeSubCont(false) { k -> handler(it, k) } }, body = body)
}

@ResetDsl
public suspend fun <R> resetWithHandler0(
handler: suspend (Error, Cont<T, R>) -> R, body: suspend () -> R
public suspend fun <R> resetFcontrol0(
handler: suspend (Error, SubCont<T, R>) -> R, body: suspend () -> R
): R {
val prompt = Prompt<R>()
return prompt.pushPrompt(extraContext = reader.context { prompt.control0 { k -> handler(it, k) } }, body = body)
return prompt.pushPrompt(extraContext = reader.context { prompt.takeSubCont { k -> handler(it, k) } }, body = body)
}
}

@ResetDsl
public suspend fun <Error, T, R> newResetWithHandler(
handler: suspend Handle<Error, T>.(Error, Cont<T, R>) -> R, body: suspend Handle<Error, T>.() -> R
): R = with(Handle<Error, T>()) {
resetWithHandler({ e, k -> handler(e, k) }) { body() }
public suspend fun <Error, T, R> newResetFcontrol(
handler: suspend Fcontrol<Error, T>.(Error, SubCont<T, R>) -> R, body: suspend Fcontrol<Error, T>.() -> R
): R = with(Fcontrol<Error, T>()) {
resetFcontrol({ e, k -> handler(e, k) }) { body() }
}
14 changes: 7 additions & 7 deletions library/src/commonTest/kotlin/FcontrolTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FcontrolTest {
@Test
fun infiniteSequence() = runTest {
runCC {
suspend fun product(s: Iterator<Int>): Int = newResetWithHandler<_, Nothing, _>({ error, _ -> error }) {
suspend fun product(s: Iterator<Int>): Int = newResetFcontrol<_, Nothing, _>({ error, _ -> error }) {
var acc = 1
for (i in s) {
if (i == 0) fcontrol(0)
Expand All @@ -23,9 +23,9 @@ class FcontrolTest {
val printed = mutableListOf<Int>()
runCC {
runReader(10) {
newResetWithHandler<Int, Unit, _>({ error, cont ->
newResetFcontrol<Int, Unit, _>({ error, cont ->
printed.add(error)
pushReader(ask() + 1) { cont(Unit) }
pushReader(ask() + 1) { cont.pushSubContWith(Result.success(Unit)) }
}) {
fcontrol(ask())
fcontrol(ask())
Expand All @@ -44,11 +44,11 @@ class FcontrolTest {
val printed = mutableListOf<Int>()
runCC {
runReader(10) {
newResetWithHandler<Int, Unit, _>({ error, cont ->
resetWithHandler0({ e, k ->
newResetFcontrol<Int, Unit, _>({ error, cont ->
resetFcontrol0({ e, k ->
printed.add(e)
k(fcontrol(e))
}) { pushReader(ask() + 1) { cont(Unit) } }
k.pushSubCont { fcontrol(e) }
}) { pushReader(ask() + 1) { cont.pushSubContWith(Result.success(Unit)) } }
}) {
fcontrol(ask())
fcontrol(ask())
Expand Down
12 changes: 6 additions & 6 deletions library/src/jvmTest/kotlin/FcontrolJvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ class FcontrolJvmTest {
fun tooBigHandler() = runTest {
data class TooBig(val value: Int)

suspend fun Handle<TooBig, Nothing>.ex2(m: Int) = if (m > 5) fcontrol(TooBig(m)) else m
suspend fun Handle<TooBig, Nothing>.exRec(body: suspend Handle<TooBig, Nothing>.() -> Int): Int =
newResetWithHandler({ error, _ ->
suspend fun Fcontrol<TooBig, Nothing>.ex2(m: Int) = if (m > 5) fcontrol(TooBig(m)) else m
suspend fun Fcontrol<TooBig, Nothing>.exRec(body: suspend Fcontrol<TooBig, Nothing>.() -> Int): Int =
newResetFcontrol({ error, _ ->
if (error.value <= 7) error.value else this@exRec.fcontrol(error)
}, body)

runCC {
val res = newResetWithHandler({ error: TooBig, _ -> error.left() }) {
val res = newResetFcontrol({ error: TooBig, _ -> error.left() }) {
runList {
ex2(listOf(5, 7, 1).bind())
}.right()
Expand All @@ -77,7 +77,7 @@ class FcontrolJvmTest {
}

runCC {
val res = newResetWithHandler({ error: TooBig, _ -> error.left() }) {
val res = newResetFcontrol({ error: TooBig, _ -> error.left() }) {
runList {
exRec {
ex2(listOf(5, 7, 1).bind())
Expand All @@ -88,7 +88,7 @@ class FcontrolJvmTest {
}

runCC {
val res = newResetWithHandler({ error: TooBig, _ -> error.left() }) {
val res = newResetFcontrol({ error: TooBig, _ -> error.left() }) {
runList {
exRec {
ex2(listOf(5, 7, 11, 1).bind())
Expand Down

0 comments on commit ea1ee91

Please sign in to comment.