| @@ -0,0 +1,102 @@ |
| 1 | +import std/io |
| 2 | +import std/list |
| 3 | +import std/ref |
| 4 | + |
| 5 | +// Multi-shot resumptions over cells reused in place (issue #217): the |
| 6 | +// copies of a continuation hold the same cells, so an `@fip` function in |
| 7 | +// one copy must not update them under the other. A cleanup runs once per |
| 8 | +// copy, and a region's references are shared by the copies and end after |
| 9 | +// the last one. |
| 10 | + |
| 11 | +@derive Show |
| 12 | +pub type Tree { Leaf, Node(Tree, Int, Tree) } |
| 13 | + |
| 14 | +effect amb |
| 15 | +{ |
| 16 | + multi ctl choose :: -> Bool |
| 17 | +} |
| 18 | + |
| 19 | +@fip |
| 20 | +fn mirror :: t: Tree -> Tree |
| 21 | +{ |
| 22 | + match t { Leaf -> Leaf, Node(l, k, r) -> Node(mirror r, k, mirror l) } |
| 23 | +} |
| 24 | + |
| 25 | +fn all :: action: fn() -> a / <amb | e> -> List<a> / <e> |
| 26 | +{ |
| 27 | + handle |
| 28 | + { |
| 29 | + [action ()] |
| 30 | + } |
| 31 | + with |
| 32 | + { choose -> list.append (resume True) (resume False) } |
| 33 | +} |
| 34 | + |
| 35 | +fn sample :: -> Tree |
| 36 | +{ |
| 37 | + Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)) |
| 38 | +} |
| 39 | + |
| 40 | +fn root_plus :: t: Tree n: Int -> Int |
| 41 | +{ |
| 42 | + match t { Node(_, k, _) -> k + n, Leaf -> n } |
| 43 | +} |
| 44 | + |
| 45 | +pub fn main :: -> () / <io> |
| 46 | +{ |
| 47 | + // Built before the choice: each copy mirrors the same cells. |
| 48 | + let mirrored = |
| 49 | + all |
| 50 | + fn |
| 51 | + { |
| 52 | + let t = sample () |
| 53 | + if choose () |
| 54 | + { |
| 55 | + mirror t |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + mirror (mirror t) |
| 60 | + } |
| 61 | + } |
| 62 | + io.println "{mirrored}" |
| 63 | + // A second choice clones a copy that has reused cells already. |
| 64 | + let twice = |
| 65 | + all |
| 66 | + fn |
| 67 | + { |
| 68 | + let t = mirror (sample ()) |
| 69 | + let u = if choose () { mirror t } else { t } |
| 70 | + if choose () |
| 71 | + { |
| 72 | + mirror u |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + u |
| 77 | + } |
| 78 | + } |
| 79 | + io.println "{twice}" |
| 80 | + // With a cleanup and a region in the copied continuation. |
| 81 | + let counted = |
| 82 | + all |
| 83 | + fn |
| 84 | + { |
| 85 | + defer io.println "copy done" |
| 86 | + region |
| 87 | + { |
| 88 | + let seen = ref.new 0 |
| 89 | + let t = sample () |
| 90 | + let t = if choose () { mirror t } else { t } |
| 91 | + ref.set seen (ref.get seen + 1) |
| 92 | + let n = ref.get seen |
| 93 | + root_plus (mirror t) n |
| 94 | + } |
| 95 | + } |
| 96 | + io.println "{counted}" |
| 97 | +} |
| 98 | +//> [Node(Node(Leaf, 3, Leaf), 2, Node(Leaf, 1, Leaf)), Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf))] |
| 99 | +//> [Node(Node(Leaf, 3, Leaf), 2, Node(Leaf, 1, Leaf)), Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)), Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)), Node(Node(Leaf, 3, Leaf), 2, Node(Leaf, 1, Leaf))] |
| 100 | +//> copy done |
| 101 | +//> copy done |
| 102 | +//> [3, 4] |