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/dict.{type Dict} |
| 2 | import std/io |
| 3 | import std/list |
| 4 | import std/option |
| 5 | |
| 6 | // Perceus optimisations (issue #34): a matched cell that is unique is |
| 7 | // freed or reused in place; a shared one is left intact. Parameters only |
| 8 | // read are borrowed. Every result must be the same as without them. |
| 9 | |
| 10 | @derive Show |
| 11 | pub type Tree { Leaf, Node(Tree, Int, Tree) } |
| 12 | |
| 13 | @derive Show |
| 14 | pub type Shape { One(Int), Two(Int, Int), Three(Int, Int, Int), Flip(Int) } |
| 15 | |
| 16 | fn insert :: t x |
| 17 | { |
| 18 | match t |
| 19 | { Leaf -> Node(Leaf, x, Leaf) |
| 20 | , Node(l, v, r) -> if x < v |
| 21 | { |
| 22 | Node(insert l x, v, r) |
| 23 | } |
| 24 | else if x > v |
| 25 | { |
| 26 | Node(l, v, insert r x) |
| 27 | } |
| 28 | else |
| 29 | { |
| 30 | t |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn incr :: t |
| 36 | { |
| 37 | match t { Leaf -> Leaf, Node(l, v, r) -> Node(incr l, v + 1, incr r) } |
| 38 | } |
| 39 | |
| 40 | fn total :: t |
| 41 | { |
| 42 | match t { Leaf -> 0, Node(l, v, r) -> total l + v + total r } |
| 43 | } |
| 44 | |
| 45 | fn build :: xs |
| 46 | { |
| 47 | list.fold xs Leaf insert |
| 48 | } |
| 49 | |
| 50 | // Reuse across sizes and constructors: the same size reuses the cell |
| 51 | // (with a new tag); another size cannot. |
| 52 | fn reshape :: s |
| 53 | { |
| 54 | match s |
| 55 | { One(a) -> Flip(a) |
| 56 | , Flip(a) -> One(a + 1) |
| 57 | , Two(a, b) -> Three(a, b, a + b) |
| 58 | , Three(a, b, c) -> Two(a + b, c) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // A tuple matched without a `switch`. |
| 63 | fn swap :: p |
| 64 | { |
| 65 | let (a, b) = p |
| 66 | (b, a) |
| 67 | } |
| 68 | |
| 69 | // Pairs neighbours: two cells matched, one built. |
| 70 | fn pairs :: xs |
| 71 | { |
| 72 | match xs { [a, b, ..rest] -> [a * 10 + b, ..pairs rest], other -> other } |
| 73 | } |
| 74 | |
| 75 | // A borrowed parameter whose field escapes into the result. |
| 76 | fn label :: p suffix |
| 77 | { |
| 78 | let (name, _) = p |
| 79 | (name <> suffix, name) |
| 80 | } |
| 81 | |
| 82 | // A borrowed dictionary and a value read from a borrowed map. |
| 83 | fn lookup_all :: d: Dict<Int, String> keys: List<Int> -> List<String> |
| 84 | { |
| 85 | match keys |
| 86 | { [] -> [] |
| 87 | , [k, ..rest] -> |
| 88 | { |
| 89 | let found = match dict.get d k { Some(v) -> v, None -> "-" } |
| 90 | [found, ..lookup_all d rest] |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Generic arithmetic: the `Num` dictionary is passed along unchanged. |
| 96 | fn sum_to :: n: a i: a step: a acc: a -> a where Num<a>, Ord<a> |
| 97 | { |
| 98 | if i > n |
| 99 | { |
| 100 | acc |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | sum_to n (i + step) step (acc + i) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub fn main :: -> () / <io> |
| 109 | { |
| 110 | let xs = list.range 1 10 |
| 111 | // Unique: map, filter and reverse reuse the cells of their input. |
| 112 | io.println |
| 113 | ( show |
| 114 | ( list.reverse |
| 115 | ( list.filter |
| 116 | (list.map (list.range 1 10) fn x { x * x }) |
| 117 | fn x { x % 2 == 1 } |
| 118 | ) |
| 119 | ) |
| 120 | ) |
| 121 | // Shared: `xs` is used again, so its cells must not be overwritten. |
| 122 | let doubled = list.map xs fn x { x * 2 } |
| 123 | io.println (show doubled <> " from " <> show xs) |
| 124 | io.println (show (pairs xs) <> " " <> show (pairs [1, 2, 3])) |
| 125 | let t = build [5, 2, 8, 1, 9, 3] |
| 126 | let u = incr t |
| 127 | io.println |
| 128 | ( show (total t) <> " " <> show (total u) <> " " |
| 129 | <> show (total (incr (build [4, 4, 6]))) |
| 130 | ) |
| 131 | io.println (show t) |
| 132 | io.println |
| 133 | (show (list.map [One(1), Two(2, 3), Three(4, 5, 6), Flip(7)] reshape)) |
| 134 | let shapes = [One(1), Two(2, 3)] |
| 135 | let again = list.map shapes reshape |
| 136 | io.println (show again <> " from " <> show shapes) |
| 137 | let p = (1, "one") |
| 138 | let q = swap p |
| 139 | let r = swap (swap p) |
| 140 | io.println |
| 141 | (q.0 <> show q.1 <> " " <> show r.0 <> r.1 <> " " <> show p.0 <> p.1) |
| 142 | let named = ("ada", 36) |
| 143 | let (long, short) = label named "!" |
| 144 | io.println (long <> " " <> short <> " " <> show named.1) |
| 145 | let d = dict.from_list [(1, "one"), (2, "two"), (3, "three")] |
| 146 | io.println (show (lookup_all d [3, 4, 1]) <> " " <> show (dict.size d)) |
| 147 | io.println (show (sum_to 100 0 1 0) <> " " <> show (sum_to 2.0 0.0 0.5 0.0)) |
| 148 | } |
| 149 | //> [81, 49, 25, 9, 1] |
| 150 | //> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] from [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 151 | //> [12, 34, 56, 78, 100] [12, 3] |
| 152 | //> 28 34 12 |
| 153 | //> Node(Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)), 5, Node(Leaf, 8, Node(Leaf, 9, Leaf))) |
| 154 | //> [Flip(1), Three(2, 3, 5), Two(9, 6), One(8)] |
| 155 | //> [Flip(1), Three(2, 3, 5)] from [One(1), Two(2, 3)] |
| 156 | //> one1 1one 1one |
| 157 | //> ada! ada 36 |
| 158 | //> [three, -, one] 3 |
| 159 | //> 5050 5.0 |