Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise ignored LLVM tests #776

Merged
merged 9 commits into from
Jan 27, 2025
Prev Previous commit
Next Next commit
Implement show instances for nim and probabilistic
marvinborner committed Jan 27, 2025
commit 7d423f702620de1b3fcc6821399d8526742c369f
48 changes: 30 additions & 18 deletions examples/pos/nim.effekt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module examples/pos/nim

import list

// Example from "Liberating effects with rows and handlers"
// Translated from Koka https://github.com/koka-lang/koka/blob/master/test/algeff/nim.kk

@@ -8,6 +10,11 @@ type Player {
Alice()
}

def show(p: Player): String = p match {
case Bob() => "Bob()"
case Alice() => "Alice()"
}

effect Move(p: Player, sticks: Int): Int

def aliceTurn(n: Int): Player / Move =
@@ -46,6 +53,16 @@ type GTree {
Winner(p: Player)
}

def show(moves: Moves[GTree]): String = moves match {
case Done() => "Done()"
case Step(n, g, rest) => "Step(" ++ show(n) ++ ", " ++ show(g) ++ ", " ++ show(rest) ++ ")"
}

def show(tree: GTree): String = tree match {
case Take(p, moves) => "Take(" ++ show(p) ++ ", " ++ show(moves) ++ ")"
case Winner(p) => "Winner(" ++ show(p) ++ ")"
}

def range(from: Int, to: Int) { block: Int => Unit } = {
var i = from;
while (i <= to) {
@@ -74,7 +91,7 @@ def printError { prog: => Unit / error } = try { prog() } with error { (msg) =>

def cheatReport[R] { prog: => R / cheat }: R / error =
try { prog() } with cheat { (p) =>
do error(genericShow(p) ++ " cheated!")
do error(show(p) ++ " cheated!")
}

def check { prog: => Player / Move } = try { prog() } with Move { (p, n) =>
@@ -91,15 +108,6 @@ def pc { prog: => Player / Move } = try { prog() } with Move { (p, n) =>

effect Choose(): Bool

type List[A] {
Nil();
Cons(head: A, tail: List[A])
}
def concat[A](l1: List[A], l2: List[A]): List[A] = l1 match {
case Nil() => l2
case Cons(a, rest) => concat(rest, Cons(a, l2))
}

def bobChooses { prog: => Player / Move } =
if (do Choose()) {
pc { prog() }
@@ -108,7 +116,7 @@ def bobChooses { prog: => Player / Move } =
}

def allResults[R] { prog: => R / Choose } = try { [prog()] } with Choose { () =>
concat(resume(true), resume(false))
append(resume(true), resume(false))
}

def coin[R] { prog: => R / Choose } = try { prog() } with Choose { () =>
@@ -133,6 +141,10 @@ type Scoreboard {
Board(alice: Int, bob: Int)
}

def show(board: Scoreboard): String = board match {
case Board(alice, bob) => "Board(" ++ show(alice) ++ ", " ++ show(bob) ++ ")"
}

effect GetScore(): Scoreboard
effect UpdateScore(p: Player): Unit

@@ -163,26 +175,26 @@ def testPrint2() = {
bobChooses { val p = game(7); do UpdateScore(p); p }; ()
}
};
println(do GetScore())
println(show(do GetScore()))
}
}

def main() = {
println(perfect { game(7) }); // Alice
println(perfect { game(12) }); // Bob
println(gametree { game(3) }); // Figure 1 in the paper
println(perfect { show(game(7)) }); // Alice
println(perfect { show(game(12)) }); // Bob
println(show(gametree { game(3) })); // Figure 1 in the paper
printError {
val result = perfect { cheatReport { check { game(7) } } }; // alice
println("result is " ++ genericShow(result))
println("result is " ++ show(result))
};
printError {
pc { cheatReport { check { game(12) } } }; () // bob cheats
};
printError { perfect {
val result = cheatReport { check { pc { game(12) } } }; // bob
println("result is " ++ genericShow(result))
println("result is " ++ show(result))
}};
println(allResults { bobChooses { game(7) } }); // List(Bob, Alice)
println(show(allResults { bobChooses { game(7) } }) { e => show(e) }); // List(Bob, Alice)
deterministic {
println(do Choose()); // true
println(do Choose()); // false
27 changes: 14 additions & 13 deletions examples/pos/probabilistic.effekt
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
module probabilistic

effect indexOutOfBounds(): Nothing

type List[A] {
Nil();
Cons(head: A, tail: List[A])
}
import list

def concat[A](l1: List[A], l2: List[A]): List[A] = l1 match {
case Nil() => l2
case Cons(a, rest) => concat(rest, Cons(a, l2))
}
effect indexOutOfBounds(): Nothing

def lookup[A](l: List[A], idx: Int): A / indexOutOfBounds = l match {
case Nil() => do indexOutOfBounds()
@@ -80,6 +72,15 @@ def choose3[R] { x: => R } { y: => R } { z: => R } : R / fork =

type Weighted[R] { MkWeighted(weight: Double, value: R) }

def show(l: Weighted[Bool]): String = l match {
case MkWeighted(weight, value) => "MkWeighted(" ++ show(weight) ++ ", " ++ show(value) ++ ")"
}

def show(l: Weighted[Var]): String = l match {
case MkWeighted(weight, Observed(value)) => "MkWeighted(" ++ show(weight) ++ ", Observed(" ++ show(value) ++ "))"
case MkWeighted(weight, Unobserved()) => "MkWeighted(" ++ show(weight) ++ ", Unobserved())"
}

def handleProb[R] { prog: => R / { score, fork, fail } } = {
val empty: List[Weighted[R]] = Nil();
try {
@@ -88,7 +89,7 @@ def handleProb[R] { prog: => R / { score, fork, fail } } = {
val res = prog();
Cons(MkWeighted(current, res), empty)
} with score { p => current = current * p; resume(()) }
} with fork { () => concat(resume(true), resume(false)) }
} with fork { () => append(resume(true), resume(false)) }
with fail { () => empty }
}

@@ -167,7 +168,7 @@ def main() = {
val res = handleProb {
test()
};
println(res);
println(show(res) { e => show(e) })

catch {
heapTest()
@@ -179,7 +180,7 @@ def main() = {
do disj(do flip(0.2), r)
}
};
println(res)
println(show(res) { e => show(e) })
}

}