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 | // Record workers, edge shapes (ADR 0069): a result too wide to flatten |
| 2 | // (stays boxed), a two-constructor type (no worker), a record passed |
| 3 | // through a closure, a worker calling a worker, and a record built and |
| 4 | // matched inside one function (a flat local, never allocated). |
| 5 | |
| 6 | import std/io |
| 7 | |
| 8 | type P2 { P2(x: Int, y: Int) } |
| 9 | type P6 { P6(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) } |
| 10 | type Tree { Leaf, Node(l: Int, r: Int) } |
| 11 | |
| 12 | fn sum6 :: p: P6 -> Int |
| 13 | { |
| 14 | match p { P6(a, b, c, d, e, f) -> a + b + c + d + e + f } |
| 15 | } |
| 16 | |
| 17 | fn make6 :: x: Int -> P6 |
| 18 | { |
| 19 | P6(x, x + 1, x + 2, x + 3, x + 4, x + 5) |
| 20 | } |
| 21 | |
| 22 | // A two-constructor type: no record worker (the tag is not static). |
| 23 | fn size :: t: Tree -> Int |
| 24 | { |
| 25 | match t { Leaf -> 0, Node(l, r) -> 1 + l + r } |
| 26 | } |
| 27 | |
| 28 | // The record goes through a closure call (boxed at the boundary). |
| 29 | fn via_closure :: p: P2 f: fn(P2) -> Int -> Int |
| 30 | { |
| 31 | f p + f (match p { P2(x, y) -> P2(y, x) }) |
| 32 | } |
| 33 | |
| 34 | // A worker calling a worker (the callee recurses: never inlined). |
| 35 | fn swap :: p: P2 k: Int -> P2 |
| 36 | { |
| 37 | if k == 0 |
| 38 | { |
| 39 | match p { P2(x, y) -> P2(y, x) } |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | swap (match p { P2(x, y) -> P2(y, x) }) (k - 1) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn swap_sum :: p: P2 -> Int |
| 48 | { |
| 49 | let a = swap p 1 |
| 50 | let b = swap (swap p 1) 2 |
| 51 | match a { P2(x, y) -> x + 10 * y + match b { P2(x2, y2) -> x2 + y2 } } |
| 52 | } |
| 53 | |
| 54 | // Built and matched inside one function: never allocated. |
| 55 | fn local :: x: Int -> Int |
| 56 | { |
| 57 | let p = P2(x, x + 1) |
| 58 | match p { P2(a, b) -> a * b } |
| 59 | } |
| 60 | |
| 61 | pub fn main :: -> () / <io> |
| 62 | { |
| 63 | io.println (show (sum6 (make6 10))) |
| 64 | io.println (show (size (Node(size Leaf, 7)))) |
| 65 | io.println |
| 66 | (show (via_closure (P2(3, 4)) fn p { match p { P2(x, y) -> x - y } })) |
| 67 | io.println (show (swap_sum (P2(5, 6)))) |
| 68 | io.println (show (local 9)) |
| 69 | } |
| 70 | //> 75 |
| 71 | //> 8 |
| 72 | //> 0 |
| 73 | //> 76 |
| 74 | //> 90 |