From dd1ff7b2bf0d27ce6da988a52a8d0dab71017023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcial=20Gai=C3=9Fert?= Date: Tue, 13 May 2025 11:22:55 +0200 Subject: [PATCH 1/3] Add ControlAt[T] and loop[T] for control with argument --- examples/stdlib/control.check | 2 ++ examples/stdlib/control.effekt | 41 ++++++++++++++++++++++++++++++++++ libraries/common/effekt.effekt | 11 +++++++++ 3 files changed, 54 insertions(+) create mode 100644 examples/stdlib/control.check create mode 100644 examples/stdlib/control.effekt diff --git a/examples/stdlib/control.check b/examples/stdlib/control.check new file mode 100644 index 000000000..c6e7924dc --- /dev/null +++ b/examples/stdlib/control.check @@ -0,0 +1,2 @@ +5 +Some(-1) \ No newline at end of file diff --git a/examples/stdlib/control.effekt b/examples/stdlib/control.effekt new file mode 100644 index 000000000..84f06b39a --- /dev/null +++ b/examples/stdlib/control.effekt @@ -0,0 +1,41 @@ + +type Term { + Add(x: Int, k: Term) + Return(v: Int) +} + +def interp(t: Term): Int = { + var st = t + var pre = 0 + loop{ {l} => + st match { + case Add(x, k) => + pre = pre + x + st = k + case Return(v) => l.break(pre + v) + } + } +} + +def ctl_min(l: List[Int]): Option[Int] = { + var c_min = None() + var c_l = l + loop{ {l} => + c_l match { + case Nil() => l.break() + case Cons(x, k) and c_min is None() => + c_min = Some(x) + c_l = k + case Cons(x, k) and c_min is Some(m) and x < m => + c_min = Some(x) + c_l = k + case Cons(x, k) => c_l = k + } + } + c_min +} + +def main() = { + println(interp(Add(2, Add(3, Return(0))))) + println(ctl_min(Cons(0, Cons(1, Cons(-1, Nil()))))) +} \ No newline at end of file diff --git a/libraries/common/effekt.effekt b/libraries/common/effekt.effekt index 1a73cb767..a43a414f3 100644 --- a/libraries/common/effekt.effekt +++ b/libraries/common/effekt.effekt @@ -722,6 +722,10 @@ interface Control { def break(): Unit def continue(): Unit } +interface ControlAt[T] { + def break(t: T): Unit + def continue(): Unit +} def loop { f: () => Unit }: Unit = { def go(): Unit = { f(); go() } @@ -735,6 +739,13 @@ def loop { f: {Control} => Unit }: Unit = try { def break() = () def continue() = loop { f } } +def loop[T] { f: {ControlAt[T]} => Unit }: T = try { + def go(): T = { f{label}; go() } + go() +} with label: ControlAt[T] { + def break(v) = v + def continue() = loop{f} +} /// Calls provided action repeatedly. `start` is inclusive, `end` is not. def each(start: Int, end: Int) { action: (Int) => Unit } = { From 819ead44c31b1976312c7be404bc4085c1d20c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcial=20Gai=C3=9Fert?= Date: Tue, 13 May 2025 11:27:13 +0200 Subject: [PATCH 2/3] Add test for only continue, restructure --- examples/stdlib/control.effekt | 12 ++++++++++++ libraries/common/effekt.effekt | 10 ++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/stdlib/control.effekt b/examples/stdlib/control.effekt index 84f06b39a..ed24d8a0d 100644 --- a/examples/stdlib/control.effekt +++ b/examples/stdlib/control.effekt @@ -35,7 +35,19 @@ def ctl_min(l: List[Int]): Option[Int] = { c_min } +effect exit(): Unit +def nobreak(): Unit = { + var x = 12 + try{ + loop{ {l} => + do exit() + l.continue() + } + } with exit { () => () } +} + def main() = { println(interp(Add(2, Add(3, Return(0))))) println(ctl_min(Cons(0, Cons(1, Cons(-1, Nil()))))) + println(nobreak()) } \ No newline at end of file diff --git a/libraries/common/effekt.effekt b/libraries/common/effekt.effekt index a43a414f3..3e74cc4fa 100644 --- a/libraries/common/effekt.effekt +++ b/libraries/common/effekt.effekt @@ -722,10 +722,6 @@ interface Control { def break(): Unit def continue(): Unit } -interface ControlAt[T] { - def break(t: T): Unit - def continue(): Unit -} def loop { f: () => Unit }: Unit = { def go(): Unit = { f(); go() } @@ -739,6 +735,12 @@ def loop { f: {Control} => Unit }: Unit = try { def break() = () def continue() = loop { f } } + +interface ControlAt[T] { + def break(t: T): Unit + def continue(): Unit +} + def loop[T] { f: {ControlAt[T]} => Unit }: T = try { def go(): T = { f{label}; go() } go() From daad7c68f23d70e010c3754a069447b73a4fd69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcial=20Gai=C3=9Fert?= Date: Tue, 13 May 2025 11:56:41 +0200 Subject: [PATCH 3/3] Fix check file --- examples/stdlib/control.check | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/stdlib/control.check b/examples/stdlib/control.check index c6e7924dc..87dc9916c 100644 --- a/examples/stdlib/control.check +++ b/examples/stdlib/control.check @@ -1,2 +1,3 @@ 5 -Some(-1) \ No newline at end of file +Some(-1) +() \ No newline at end of file