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 | // Calls with more than six arguments: those after the sixth go through |
| 2 | // `ctx->xargs`, the running scheduler thread's slots (ABI 3, ADR 0028). |
| 3 | // 32 actors make such calls at once, are preempted at function entries |
| 4 | // and may move between threads: each callee must read its own arguments. |
| 5 | // Covers a recursive function, a closure with captures (its entry passes |
| 6 | // the arguments on), and a `ctl` operation (the runtime calls its clause). |
| 7 | |
| 8 | import std/io |
| 9 | import std/process.{type Subject, type proc} |
| 10 | |
| 11 | effect mix |
| 12 | { |
| 13 | ctl mix7 :: a: Int b: Int c: Int d: Int e: Int f: Int g: Int -> Int |
| 14 | } |
| 15 | |
| 16 | fn weigh :: |
| 17 | a: Int |
| 18 | b: Int |
| 19 | c: Int |
| 20 | d: Int |
| 21 | e: Int |
| 22 | f: Int |
| 23 | g: Int |
| 24 | h: Int |
| 25 | i: Int |
| 26 | -> Int |
| 27 | { |
| 28 | a + 2 * b + 3 * c + 5 * d + 7 * e + 11 * f + 13 * g + 17 * h + 19 * i |
| 29 | } |
| 30 | |
| 31 | // Nine parameters, recursing: a function entry (and a preemption check) |
| 32 | // on each step. |
| 33 | fn walk :: |
| 34 | n: Int |
| 35 | a: Int |
| 36 | b: Int |
| 37 | c: Int |
| 38 | d: Int |
| 39 | e: Int |
| 40 | f: Int |
| 41 | g: Int |
| 42 | total: Int |
| 43 | -> Int |
| 44 | { |
| 45 | if n == 0 |
| 46 | { |
| 47 | total |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | walk (n - 1) b c d e f g a ((total + weigh n a b c d e f g total) % 1000003) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn mixing :: k: Int n: Int total: Int -> Int / <mix> |
| 56 | { |
| 57 | if n == 0 |
| 58 | { |
| 59 | total |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | mixing |
| 64 | k |
| 65 | (n - 1) |
| 66 | ((total + mix7 n k (n + 1) (k + 1) (n + 2) (k + 2) (n + 3)) % 1000003) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn worker :: k: Int results: Subject<Int> -> () / <proc> |
| 71 | { |
| 72 | let scale = k * 7 |
| 73 | let shift = k + 3 |
| 74 | let add = fn a: Int b: Int c: Int d: Int e: Int f: Int g: Int |
| 75 | { |
| 76 | a + b + c + d + e + f + g + scale * shift |
| 77 | } |
| 78 | let walked = walk 20000 k 1 2 3 4 5 6 0 |
| 79 | let mixed = handle |
| 80 | { |
| 81 | mixing k 5000 0 |
| 82 | } |
| 83 | with |
| 84 | { mix7 a b c d e f g -> resume (add a b c d e f g) } |
| 85 | process.send results (walked + mixed) |
| 86 | } |
| 87 | |
| 88 | fn spawn_all :: k: Int results: Subject<Int> -> () / <proc> |
| 89 | { |
| 90 | if k > 0 |
| 91 | { |
| 92 | let _ = process.spawn fn { worker k results } |
| 93 | spawn_all (k - 1) results |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn collect :: results: Subject<Int> left: Int total: Int -> Int / <proc> |
| 98 | { |
| 99 | if left == 0 |
| 100 | { |
| 101 | total |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | collect results (left - 1) (total + process.receive_forever results) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | pub fn main :: -> () / <io, proc> |
| 110 | { |
| 111 | let results = process.subject () |
| 112 | spawn_all 32 results |
| 113 | io.println (show (collect results 32 0)) |
| 114 | } |
| 115 | //> 33740715 |