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
1 change: 1 addition & 0 deletions examples/stdlib/search/noteach.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
35 changes: 35 additions & 0 deletions examples/stdlib/search/noteach.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import stream
import search

type Tree[A] {
Nil()
Branch(v: A, l: Tree[A], r: Tree[A])
}

def notEach(t: Tree[Bool]): Tree[Bool] / {fork, fail} = t match {
case Nil() =>
do fail()
case Branch(v, l, r) =>
choice {
Branch(not(v), l, r)
} {
choice {
Branch(v, notEach(l), r)
} {
Branch(v, l, notEach(r))
}
}
}

def count[A] { stream: => Unit / emit[A] }: Int = {
var n = 0
for[A] { stream() } { _ => n = n + 1 }
n
}

def main() = {
val example = Branch(false, Branch(false, Nil(), Branch(false, Nil(), Nil())), Nil())
println(count[Tree[Bool]] { results { notEach(example) } })
}

64 changes: 64 additions & 0 deletions libraries/common/search.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module search

import stream


effect fork(): Bool
effect fail(): Nothing

def select[A] { stream: () => Unit / emit[A] }: A / {fork, fail} = {
def body(): A / emit[A] = {
stream()
do fail()
}
try {
body()
} with emit[A] { value =>
if (do fork()) {
return value
} else {
resume(())
}
}
}

def results[R] { query: () => R / {fork, fail} }: Unit / emit[R] =
try {
do emit(query())
} with fork { () =>
resume(true)
resume(false)
} with fail { () =>
()
}

def choose[A](items: List[A]): A / {fork, fail} =
select { items.each }

def choice[A] { action1: () => A } { action2: () => A }: A / fork =
if (do fork()) { action1() } else { action2() }

def where(condition: Bool): Unit / fail =
if (condition) { () } else { do fail() }


type Key = String
type Option = String

type Menu = List[(Key, List[Option])]
type Meal = List[(Key, Option)]

def allMeals(menu: Menu): Meal / {fork, fail} =
menu.map { case (category, options) => (category, options.choose) }

def main() = {
val menu = [
("Protein", ["Chicken", "Beef", "Tofu"]),
("Carbs", ["Rice", "Pasta", "French Fries"]),
("Veggie", ["Bell Pepper", "Brocolli"])
]
for[Meal] { results { allMeals(menu) } } { meal =>
meal.foreach { case (key, option) => println(key ++ "," ++ option ++ " ") }
println("")
}
}
Loading