Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/stdlib/control.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
5
Some(-1)
()
53 changes: 53 additions & 0 deletions examples/stdlib/control.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

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
}

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())
}
13 changes: 13 additions & 0 deletions libraries/common/effekt.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,19 @@ def loop { f: {Control} => Unit }: Unit = try {
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()
} 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 } = {
def loop(i: Int): Unit =
Expand Down