louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | import std/io |
| 2 | import std/list |
| 3 | |
| 4 | // Perceus v2 (ADR 0040): a matched cell of known size is reused without a |
| 5 | // size check. Tuples and record updates reuse cells too, and a cell can be |
| 6 | // given to a function that builds one of its size. Results must be the |
| 7 | // same whether cells are unique or shared. |
| 8 | |
| 9 | @derive Show |
| 10 | pub type State { State(count: Int, name: String, tags: List<String>) } |
| 11 | |
| 12 | @derive Show |
| 13 | pub type Tree { Leaf, Node(Tree, Int, Tree) } |
| 14 | |
| 15 | effect amb |
| 16 | { |
| 17 | multi ctl choose :: -> Bool |
| 18 | } |
| 19 | |
| 20 | // A record update: `s`'s cell is updated in place. |
| 21 | @fip |
| 22 | fn bump :: s: State -> State |
| 23 | { |
| 24 | State(..s, count: s.count + 1) |
| 25 | } |
| 26 | |
| 27 | // A tuple's cell, reused for the swapped tuple. |
| 28 | @fip |
| 29 | fn swap :: p: (Int, String) -> (String, Int) |
| 30 | { |
| 31 | let (a, b) = p |
| 32 | (b, a) |
| 33 | } |
| 34 | |
| 35 | @fip |
| 36 | fn mirror :: t: Tree -> Tree |
| 37 | { |
| 38 | match t { Leaf -> Leaf, Node(l, k, r) -> Node(mirror r, k, mirror l) } |
| 39 | } |
| 40 | |
| 41 | // The released pair builds nothing here: `pair` builds a cell of its |
| 42 | // size, and takes it. |
| 43 | fn pair :: a: Int b: Int -> (Int, Int) |
| 44 | { |
| 45 | (a + b, a * b) |
| 46 | } |
| 47 | |
| 48 | fn combine :: p: (Int, Int) -> (Int, Int) |
| 49 | { |
| 50 | let (a, b) = p |
| 51 | pair a b |
| 52 | } |
| 53 | |
| 54 | // A token given on through two calls: `combine2` gives it to `choose_pair`, |
| 55 | // whose `else` path builds nothing and gives it to `pair`. |
| 56 | fn choose_pair :: a: Int b: Int flip: Bool -> (Int, Int) |
| 57 | { |
| 58 | if flip |
| 59 | { |
| 60 | (b, a) |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | pair a b |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn combine2 :: p: (Int, Int) flip: Bool -> (Int, Int) |
| 69 | { |
| 70 | let (a, b) = p |
| 71 | choose_pair a b flip |
| 72 | } |
| 73 | |
| 74 | // A token kept across a multi-shot operation: both resumptions build the |
| 75 | // state, from one released cell. |
| 76 | fn relabel :: s: State -> State / <amb> |
| 77 | { |
| 78 | match s |
| 79 | { State(count: c, name: n, tags: t) -> if choose () |
| 80 | { |
| 81 | State(c, n <> "!", t) |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | State(c + 10, n, t) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn all :: action: fn() -> a / <amb | e> -> List<a> / <e> |
| 91 | { |
| 92 | handle |
| 93 | { |
| 94 | [action ()] |
| 95 | } |
| 96 | with |
| 97 | { choose -> list.append (resume True) (resume False) } |
| 98 | } |
| 99 | |
| 100 | fn loop :: s: State n: Int -> State |
| 101 | { |
| 102 | if n == 0 |
| 103 | { |
| 104 | s |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | loop (bump s) (n - 1) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | pub fn main :: -> () / <io> |
| 113 | { |
| 114 | // Unique: updated in place a million times. |
| 115 | io.println "{loop State(0, "a", ["x"]) 1000000}" |
| 116 | // Shared: the original stays intact. |
| 117 | let s = State(1, "b", ["y", "z"]) |
| 118 | let t = bump (bump s) |
| 119 | io.println "{s} {t}" |
| 120 | let p = (1, "one") |
| 121 | io.println "{swap p} {p} {swap (2, "two")}" |
| 122 | let tree = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf))) |
| 123 | io.println "{mirror tree}" |
| 124 | io.println "{tree}" |
| 125 | io.println "{mirror (mirror Node(Leaf, 5, Node(Leaf, 6, Leaf)))}" |
| 126 | let q = (3, 4) |
| 127 | io.println "{combine q} {q} {combine (5, 6)}" |
| 128 | io.println |
| 129 | "{combine2 (3, 4) True} {combine2 (3, 4) False} {combine2 q False} {q}" |
| 130 | io.println "{all fn { relabel State(0, "c", ["w"]) }}" |
| 131 | let shared = State(7, "d", []) |
| 132 | io.println "{all fn { relabel shared }} {shared}" |
| 133 | } |
| 134 | //> State(1000000, a, [x]) |
| 135 | //> State(1, b, [y, z]) State(3, b, [y, z]) |
| 136 | //> (one, 1) (1, one) (two, 2) |
| 137 | //> Node(Node(Node(Leaf, 4, Leaf), 3, Leaf), 2, Node(Leaf, 1, Leaf)) |
| 138 | //> Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf))) |
| 139 | //> Node(Leaf, 5, Node(Leaf, 6, Leaf)) |
| 140 | //> (7, 12) (3, 4) (11, 30) |
| 141 | //> (4, 3) (7, 12) (7, 12) (3, 4) |
| 142 | //> [State(0, c!, [w]), State(10, c, [w])] |
| 143 | //> [State(7, d!, []), State(17, d, [])] State(7, d, []) |