import std/io import std/list // Perceus v2 (ADR 0040): a matched cell of known size is reused without a // size check. Tuples and record updates reuse cells too, and a cell can be // given to a function that builds one of its size. Results must be the // same whether cells are unique or shared. @derive Show pub type State { State(count: Int, name: String, tags: List) } @derive Show pub type Tree { Leaf, Node(Tree, Int, Tree) } effect amb { multi ctl choose :: -> Bool } // A record update: `s`'s cell is updated in place. @fip fn bump :: s: State -> State { State(..s, count: s.count + 1) } // A tuple's cell, reused for the swapped tuple. @fip fn swap :: p: (Int, String) -> (String, Int) { let (a, b) = p (b, a) } @fip fn mirror :: t: Tree -> Tree { match t { Leaf -> Leaf, Node(l, k, r) -> Node(mirror r, k, mirror l) } } // The released pair builds nothing here: `pair` builds a cell of its // size, and takes it. fn pair :: a: Int b: Int -> (Int, Int) { (a + b, a * b) } fn combine :: p: (Int, Int) -> (Int, Int) { let (a, b) = p pair a b } // A token given on through two calls: `combine2` gives it to `choose_pair`, // whose `else` path builds nothing and gives it to `pair`. fn choose_pair :: a: Int b: Int flip: Bool -> (Int, Int) { if flip { (b, a) } else { pair a b } } fn combine2 :: p: (Int, Int) flip: Bool -> (Int, Int) { let (a, b) = p choose_pair a b flip } // A token kept across a multi-shot operation: both resumptions build the // state, from one released cell. fn relabel :: s: State -> State / { match s { State(count: c, name: n, tags: t) -> if choose () { State(c, n <> "!", t) } else { State(c + 10, n, t) } } } fn all :: action: fn() -> a / -> List / { handle { [action ()] } with { choose -> list.append (resume True) (resume False) } } fn loop :: s: State n: Int -> State { if n == 0 { s } else { loop (bump s) (n - 1) } } pub fn main :: -> () / { // Unique: updated in place a million times. io.println "{loop State(0, "a", ["x"]) 1000000}" // Shared: the original stays intact. let s = State(1, "b", ["y", "z"]) let t = bump (bump s) io.println "{s} {t}" let p = (1, "one") io.println "{swap p} {p} {swap (2, "two")}" let tree = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf))) io.println "{mirror tree}" io.println "{tree}" io.println "{mirror (mirror Node(Leaf, 5, Node(Leaf, 6, Leaf)))}" let q = (3, 4) io.println "{combine q} {q} {combine (5, 6)}" io.println "{combine2 (3, 4) True} {combine2 (3, 4) False} {combine2 q False} {q}" io.println "{all fn { relabel State(0, "c", ["w"]) }}" let shared = State(7, "d", []) io.println "{all fn { relabel shared }} {shared}" } //> State(1000000, a, [x]) //> State(1, b, [y, z]) State(3, b, [y, z]) //> (one, 1) (1, one) (two, 2) //> Node(Node(Node(Leaf, 4, Leaf), 3, Leaf), 2, Node(Leaf, 1, Leaf)) //> Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf))) //> Node(Leaf, 5, Node(Leaf, 6, Leaf)) //> (7, 12) (3, 4) (11, 30) //> (4, 3) (7, 12) (7, 12) (3, 4) //> [State(0, c!, [w]), State(10, c, [w])] //> [State(7, d!, []), State(17, d, [])] State(7, d, [])