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#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 1 | // `Wire` (ADR 0072): what can cross hosts, decided by the compiler from |
| 2 | // a type's structure like `Send`. Generic code says `where Wire<a>`, and |
| 3 | // passes the type's description; it cannot be implemented. |
| 4 | |
| 5 | import std/array.{type Array} |
| 6 | import std/bytes.{type Builder} |
| 7 | import std/dec.{type Dec} |
| 8 | import std/ffi.{type Resource} |
| 9 | import std/process.{type Dynamic, type Pid, type Subject} |
| 10 | import std/ref.{type Ref} |
| 11 | import std/u64.{type U64} |
| 12 | |
| 13 | fn cross :: x: a -> () where Wire<a> |
| 14 | { |
| 15 | () |
| 16 | } |
| 17 | |
| 18 | fn send_only :: x: a -> () where Send<a> |
| 19 | { |
| 20 | () |
| 21 | } |
| 22 | |
| 23 | type Point { Point(x: Int, y: Int) } |
| 24 | |
| 25 | type Tree<a> { Leaf, Node(Tree<a>, a, Tree<a>) } |
| 26 | |
| 27 | type Message |
| 28 | { Ping(reply: Subject<Int>), Points(points: Array<Point>, label: String) } |
| 29 | |
| 30 | // No field stores a value of `a`. |
| 31 | type Proxy<a> { Proxy } |
| 32 | |
| 33 | type Box<a> { Box(value: a) } |
| 34 | |
| 35 | type Wrapper<a> { Wrapper(inner: Box<a>) } |
| 36 | |
| 37 | type Callback { Callback(run: fn(Int) -> Int) } |
| 38 | |
| 39 | type Counter<h> { Counter(count: Ref<h, Int>) } |
| 40 | |
| 41 | @thread_safe |
| 42 | type SharedHandle { SharedHandle } |
| 43 | |
| 44 | type Socket { Socket(fd: Resource<SharedHandle>) } |
| 45 | |
| 46 | // Each holds the other: one function makes both fail. |
| 47 | type Left { Left(right: Option<Right>) } |
| 48 | |
| 49 | type Right { Right(left: Left, reply: fn() -> ()) } |
| 50 | |
| 51 | // Recursive through each other, and `Wire`. |
| 52 | type Even { Zero, Succ(Odd) } |
| 53 | |
| 54 | type Odd { OddSucc(Even) } |
| 55 | |
| 56 | type AnyWire { AnyWire(b) -> AnyWire where Wire<b> } |
| 57 | |
| 58 | type AnySendable { AnySendable(b) -> AnySendable where Send<b> } |
| 59 | |
| 60 | type Expr<a> { IntLit(Int) -> Expr<Int>, BoolLit(Bool) -> Expr<Bool> } |
| 61 | |
| 62 | // Nested (non-regular): `Nest<Int>` holds a `Nest<(Int, Int)>`, which |
| 63 | // holds a `Nest<((Int, Int), (Int, Int))>`, ... |
| 64 | type Nest<a> { NestNil, NestCons(a, Nest<(a, a)>) } |
| 65 | |
| 66 | type HoldsNest { HoldsNest(nest: Nest<Int>) } |
| 67 | |
| 68 | // Regular: the recursion passes its parameter unchanged, whatever wraps it. |
| 69 | type Rose<a> { Rose(a, List<(a, Rose<a>)>) } |
| 70 | |
| 71 | // Through a phantom argument, which descriptors erase: not a recursion. |
| 72 | type Phantom<a> { Phantom(Proxy<Phantom<(a, a)>>) } |
| 73 | |
| 74 | // Written with result types, but neither hiding nor refining one. |
| 75 | type Stack<a> { Empty -> Stack<a>, Push(a, Stack<a>) -> Stack<a> } |
| 76 | |
| 77 | // The built-in values, tuples, records, recursive types, subjects. |
| 78 | fn crossing :: |
| 79 | p: Pid |
| 80 | s: Subject<Message> |
| 81 | st: Stack<Point> |
| 82 | r: Subject<Int> |
| 83 | a: Array<Dec> |
| 84 | u: U64 |
| 85 | b: Bytes |
| 86 | pts: Array<Point> |
| 87 | -> () |
| 88 | { |
| 89 | cross 1 |
| 90 | cross 1.5 |
| 91 | cross "text" |
| 92 | cross b |
| 93 | cross True |
| 94 | cross () |
| 95 | cross (1, "a", [True]) |
| 96 | cross Point(1, 2) |
| 97 | cross Node(Leaf, Point(1, 2), Leaf) |
| 98 | cross p |
| 99 | cross s |
| 100 | cross a |
| 101 | cross u |
| 102 | cross (Some(Ping(r)), [Points(pts, "")]) |
| 103 | cross Succ(OddSucc(Zero)) |
| 104 | cross Wrapper(Box(Point(0, 0))) |
| 105 | cross st |
| 106 | cross Rose(1, []) |
| 107 | cross Phantom(Proxy) |
| 108 | let phantom: Proxy<fn() -> ()> = Proxy |
| 109 | cross phantom |
| 110 | } |
| 111 | |
| 112 | // A type with no known element: no description to pass. |
| 113 | fn unknown :: -> () |
| 114 | { |
| 115 | cross [] //~ ERROR E0403 cannot tell which type should implement `Wire` |
| 116 | } |
| 117 | |
| 118 | fn a_function :: -> () |
| 119 | { |
| 120 | cross fn x { x + 1 } //~ ERROR E0706 a function's code |
| 121 | } |
| 122 | |
| 123 | fn a_callback :: c: Callback -> () |
| 124 | { |
| 125 | cross c //~ ERROR E0706 `Callback` has a field `run` that cannot cross hosts |
| 126 | } |
| 127 | |
| 128 | fn a_boxed_function :: w: Wrapper<fn() -> ()> -> () |
| 129 | { |
| 130 | cross w |
| 131 | //~^ ERROR E0706 `Wrapper` has a field `inner` that cannot cross hosts: `Box` has a field `value` |
| 132 | } |
| 133 | |
| 134 | fn a_tree_of_functions :: t: Tree<fn() -> ()> -> () |
| 135 | { |
| 136 | cross t //~ ERROR E0706 `Tree` has a field that cannot cross hosts |
| 137 | } |
| 138 | |
| 139 | fn a_subject_of_functions :: s: Subject<fn() -> ()> -> () |
| 140 | { |
| 141 | cross s //~ ERROR E0706 a `Subject`'s messages must cross hosts too |
| 142 | } |
| 143 | |
| 144 | fn a_ref :: r: Ref<h, Int> -> () |
| 145 | { |
| 146 | cross r //~ ERROR E0706 a `Ref` belongs to its region |
| 147 | } |
| 148 | |
| 149 | fn a_counter :: c: Counter<h> -> () |
| 150 | { |
| 151 | cross c //~ ERROR E0706 `Counter` has a field `count` that cannot cross hosts |
| 152 | } |
| 153 | |
| 154 | // Even a thread-safe resource: it is a handle to this process's state. |
| 155 | fn a_resource :: r: Resource<SharedHandle> -> () |
| 156 | { |
| 157 | send_only r |
| 158 | cross r //~ ERROR E0706 a `Resource` is a handle to foreign state |
| 159 | } |
| 160 | |
| 161 | fn a_socket :: s: Socket -> () |
| 162 | { |
| 163 | cross s //~ ERROR E0706 `Socket` has a field `fd` that cannot cross hosts |
| 164 | } |
| 165 | |
| 166 | fn a_dynamic :: d: Dynamic -> () |
| 167 | { |
| 168 | cross d //~ ERROR E0706 a `Dynamic` value's type is known only |
| 169 | } |
| 170 | |
| 171 | fn a_builder :: b: Builder -> () |
| 172 | { |
| 173 | cross b //~ ERROR E0706 send the `Bytes` it builds |
| 174 | } |
| 175 | |
| 176 | fn mutually_recursive :: l: Left -> () |
| 177 | { |
| 178 | cross l |
| 179 | //~^ ERROR E0706 `Left` has a field `right` that cannot cross hosts: `Option` has a field that cannot cross hosts: `Right` has a field `reply` |
| 180 | } |
| 181 | |
| 182 | fn nested :: n: Nest<Int> h: HoldsNest -> () |
| 183 | { |
| 184 | cross n |
| 185 | //~^ ERROR E0706 `Nest` is a nested type: it recurses through `Nest<(a, a)>` |
| 186 | cross h |
| 187 | //~^ ERROR E0706 `HoldsNest` has a field `nest` that cannot cross hosts: `Nest` is a nested type |
| 188 | } |
| 189 | |
| 190 | // A constructor that stores a dictionary, or hides or refines a type. |
| 191 | fn existential :: x: AnyWire -> () |
| 192 | { |
| 193 | cross x |
| 194 | //~^ ERROR E0706 constructor `AnyWire` stores a dictionary (`where Wire<b>`) |
| 195 | } |
| 196 | |
| 197 | fn hidden :: x: AnySendable y: Expr<Int> -> () |
| 198 | { |
| 199 | cross x //~ ERROR E0706 constructor `AnySendable` hides or refines a type |
| 200 | cross y //~ ERROR E0706 constructor `IntLit` hides or refines a type |
| 201 | } |
| 202 | |
| 203 | // What a matched constructor stores describes its hidden type. |
| 204 | fn opened :: x: AnyWire -> () |
| 205 | { |
| 206 | match x { AnyWire(b) -> cross [b] } |
| 207 | } |
| 208 | |
| 209 | // Generic code needs the bound, written or inferred. |
| 210 | fn unbounded :: x: a -> () |
| 211 | { |
| 212 | cross x //~ ERROR E0402 `a` is not known to implement `Wire` |
| 213 | } |
| 214 | |
| 215 | fn bounded :: x: a -> () where Wire<a> |
| 216 | { |
| 217 | cross [(x, 1)] |
| 218 | } |
| 219 | |
| 220 | fn inferred :: x |
| 221 | { |
| 222 | cross (Some(x), 1) |
| 223 | } |
| 224 | //~ TYPE inferred: fn(a) -> () where Wire<a> |
| 225 | |
| 226 | fn through_inferred :: f: fn() -> () -> () |
| 227 | { |
| 228 | inferred f //~ ERROR E0706 a function's code |
| 229 | } |
| 230 | |
| 231 | // A phantom argument need not be `Wire`, even unknown. |
| 232 | fn phantom :: p: Proxy<a> -> () |
| 233 | { |
| 234 | cross p |
| 235 | } |
| 236 | |
| 237 | // `Wire<a>` implies `Send<a>`: what can cross hosts can cross actors. |
| 238 | fn implies_send :: x: a -> () where Wire<a> |
| 239 | { |
| 240 | send_only x |
| 241 | send_only (x, [x]) |
| 242 | } |
| 243 | |
| 244 | // A stored `Wire<b>` too: the value was built with it. |
| 245 | fn stored_implies_send :: x: AnyWire -> () |
| 246 | { |
| 247 | send_only x |
| 248 | } |
| 249 | |
| 250 | fn a_stack_of_functions :: s: Stack<fn() -> ()> -> () |
| 251 | { |
| 252 | cross s //~ ERROR E0706 `Stack` has a field that cannot cross hosts |
| 253 | } |
| 254 | |
| 255 | impl Wire<Point> {} //~ ERROR E0413 `Wire` cannot be implemented |