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#128 · main · 15 min 51 s
0Étoiles
0Forks
40Issues
| 1 | // Structured messages (issue #156): orders, each a record holding a list |
| 2 | // of items and a string, passed along a pipeline of 8 actors, on all |
| 3 | // threads. Three ways `send` handles a message (docs/runtime-abi.md): |
| 4 | // - moved: each order is built for the send (count 1), so every hop takes |
| 5 | // it over without copying, though the walk still visits each block; |
| 6 | // - copied: the sender keeps every order, so the first hop copies it; |
| 7 | // - promoted: kept orders of more than 64 blocks, which the first hop |
| 8 | // makes shared, and later hops pass on without walking. |
| 9 | |
| 10 | import std/io |
| 11 | import std/list |
| 12 | import std/process.{type Subject, type proc} |
| 13 | |
| 14 | pub type Item { Item(sku: Int, qty: Int) } |
| 15 | pub type Order { Order(id: Int, items: List<Item>, note: String) } |
| 16 | |
| 17 | fn items :: id: Int n: Int acc: List<Item> -> List<Item> |
| 18 | { |
| 19 | if n == 0 |
| 20 | { |
| 21 | acc |
| 22 | } |
| 23 | else |
| 24 | { |
| 25 | items id (n - 1) [Item(id + n, n), ..acc] |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn order :: id: Int size: Int -> Order |
| 30 | { |
| 31 | Order(id, items id size [], "order " <> show id) |
| 32 | } |
| 33 | |
| 34 | fn value :: o: Order -> Int |
| 35 | { |
| 36 | match o |
| 37 | { Order(id, its, _) -> |
| 38 | list.fold |
| 39 | its |
| 40 | id |
| 41 | fn acc it { match it { Item(sku, qty) -> acc + sku * qty } } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Forward every order to `next`. |
| 46 | fn stage :: inbox: Subject<Order> next: Subject<Order> -> () / <proc> |
| 47 | { |
| 48 | process.send next (process.receive_forever inbox) |
| 49 | stage inbox next |
| 50 | } |
| 51 | |
| 52 | /// Add up batches of `batch` orders, reporting each batch's total. |
| 53 | fn sink :: |
| 54 | inbox: Subject<Order> |
| 55 | batch: Int |
| 56 | left: Int |
| 57 | total: Int |
| 58 | report: Subject<Int> |
| 59 | -> () / <proc> |
| 60 | { |
| 61 | if left == 0 |
| 62 | { |
| 63 | process.send report total |
| 64 | sink inbox batch batch 0 report |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | sink |
| 69 | inbox |
| 70 | batch |
| 71 | (left - 1) |
| 72 | (total + value (process.receive_forever inbox)) |
| 73 | report |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// A pipeline of `n` stages ending at `last`; its first inbox. |
| 78 | fn pipeline :: n: Int last: Subject<Order> -> Subject<Order> / <proc> |
| 79 | { |
| 80 | if n == 0 |
| 81 | { |
| 82 | last |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | let ready = process.subject () |
| 87 | let _ = |
| 88 | process.spawn |
| 89 | fn |
| 90 | { |
| 91 | let inbox = process.subject () |
| 92 | process.send ready inbox |
| 93 | stage inbox last |
| 94 | } |
| 95 | pipeline (n - 1) (process.receive_forever ready) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Send `n` orders built for the send. |
| 100 | fn send_fresh :: first: Subject<Order> i: Int n: Int size: Int -> () / <proc> |
| 101 | { |
| 102 | if i < n |
| 103 | { |
| 104 | process.send first (order i size) |
| 105 | send_fresh first (i + 1) n size |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Send each of `orders`, which the caller keeps. |
| 110 | fn send_kept :: first: Subject<Order> orders: List<Order> -> () / <proc> |
| 111 | { |
| 112 | match orders |
| 113 | { [] -> () |
| 114 | , [o, ..rest] -> |
| 115 | { |
| 116 | process.send first o |
| 117 | send_kept first rest |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn build :: i: Int n: Int size: Int acc: List<Order> -> List<Order> |
| 123 | { |
| 124 | if i == n |
| 125 | { |
| 126 | acc |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | build (i + 1) n size [order i size, ..acc] |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Send `orders` and wait for the sink's total; the orders are used again |
| 135 | /// after the send, so the sender keeps them. |
| 136 | fn kept :: |
| 137 | first: Subject<Order> |
| 138 | report: Subject<Int> |
| 139 | orders: List<Order> |
| 140 | -> Int / <proc> |
| 141 | { |
| 142 | send_kept first orders |
| 143 | let total = process.receive_forever report |
| 144 | total - list.fold orders 0 fn acc o { acc + value o } |
| 145 | } |
| 146 | |
| 147 | pub fn main :: -> () / <io, proc> |
| 148 | { |
| 149 | let batch = 8000 |
| 150 | let report = process.subject () |
| 151 | let ready = process.subject () |
| 152 | let _ = |
| 153 | process.spawn |
| 154 | fn |
| 155 | { |
| 156 | let inbox = process.subject () |
| 157 | process.send ready inbox |
| 158 | sink inbox batch batch 0 report |
| 159 | } |
| 160 | let first = pipeline 8 (process.receive_forever ready) |
| 161 | send_fresh first 0 batch 20 |
| 162 | let moved = process.receive_forever report |
| 163 | // Copied and promoted: the difference with the sender's own sum is 0. |
| 164 | let copied = kept first report (build 0 batch 20 []) |
| 165 | let promoted = kept first report (build 0 batch 100 []) |
| 166 | io.println (show moved <> " " <> show copied <> " " <> show promoted) |
| 167 | } |
| 168 | //> 6774116000 0 0 |