tobogganSe connecter

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.

255 lignes · 6188 octetsBrut · Historique
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
5import std/array.{type Array}
6import std/bytes.{type Builder}
7import std/dec.{type Dec}
8import std/ffi.{type Resource}
9import std/process.{type Dynamic, type Pid, type Subject}
10import std/ref.{type Ref}
11import std/u64.{type U64}
12
13fn cross :: x: a -> () where Wire<a>
14{
15 ()
16}
17
18fn send_only :: x: a -> () where Send<a>
19{
20 ()
21}
22
23type Point { Point(x: Int, y: Int) }
24
25type Tree<a> { Leaf, Node(Tree<a>, a, Tree<a>) }
26
27type Message
28{ Ping(reply: Subject<Int>), Points(points: Array<Point>, label: String) }
29
30// No field stores a value of `a`.
31type Proxy<a> { Proxy }
32
33type Box<a> { Box(value: a) }
34
35type Wrapper<a> { Wrapper(inner: Box<a>) }
36
37type Callback { Callback(run: fn(Int) -> Int) }
38
39type Counter<h> { Counter(count: Ref<h, Int>) }
40
41@thread_safe
42type SharedHandle { SharedHandle }
43
44type Socket { Socket(fd: Resource<SharedHandle>) }
45
46// Each holds the other: one function makes both fail.
47type Left { Left(right: Option<Right>) }
48
49type Right { Right(left: Left, reply: fn() -> ()) }
50
51// Recursive through each other, and `Wire`.
52type Even { Zero, Succ(Odd) }
53
54type Odd { OddSucc(Even) }
55
56type AnyWire { AnyWire(b) -> AnyWire where Wire<b> }
57
58type AnySendable { AnySendable(b) -> AnySendable where Send<b> }
59
60type 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))>`, ...
64type Nest<a> { NestNil, NestCons(a, Nest<(a, a)>) }
65
66type HoldsNest { HoldsNest(nest: Nest<Int>) }
67
68// Regular: the recursion passes its parameter unchanged, whatever wraps it.
69type Rose<a> { Rose(a, List<(a, Rose<a>)>) }
70
71// Through a phantom argument, which descriptors erase: not a recursion.
72type Phantom<a> { Phantom(Proxy<Phantom<(a, a)>>) }
73
74// Written with result types, but neither hiding nor refining one.
75type Stack<a> { Empty -> Stack<a>, Push(a, Stack<a>) -> Stack<a> }
76
77// The built-in values, tuples, records, recursive types, subjects.
78fn 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.
113fn unknown :: -> ()
114{
115 cross [] //~ ERROR E0403 cannot tell which type should implement `Wire`
116}
117
118fn a_function :: -> ()
119{
120 cross fn x { x + 1 } //~ ERROR E0706 a function's code
121}
122
123fn a_callback :: c: Callback -> ()
124{
125 cross c //~ ERROR E0706 `Callback` has a field `run` that cannot cross hosts
126}
127
128fn 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
134fn a_tree_of_functions :: t: Tree<fn() -> ()> -> ()
135{
136 cross t //~ ERROR E0706 `Tree` has a field that cannot cross hosts
137}
138
139fn a_subject_of_functions :: s: Subject<fn() -> ()> -> ()
140{
141 cross s //~ ERROR E0706 a `Subject`'s messages must cross hosts too
142}
143
144fn a_ref :: r: Ref<h, Int> -> ()
145{
146 cross r //~ ERROR E0706 a `Ref` belongs to its region
147}
148
149fn 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.
155fn 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
161fn a_socket :: s: Socket -> ()
162{
163 cross s //~ ERROR E0706 `Socket` has a field `fd` that cannot cross hosts
164}
165
166fn a_dynamic :: d: Dynamic -> ()
167{
168 cross d //~ ERROR E0706 a `Dynamic` value's type is known only
169}
170
171fn a_builder :: b: Builder -> ()
172{
173 cross b //~ ERROR E0706 send the `Bytes` it builds
174}
175
176fn 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
182fn 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.
191fn existential :: x: AnyWire -> ()
192{
193 cross x
194 //~^ ERROR E0706 constructor `AnyWire` stores a dictionary (`where Wire<b>`)
195}
196
197fn 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.
204fn opened :: x: AnyWire -> ()
205{
206 match x { AnyWire(b) -> cross [b] }
207}
208
209// Generic code needs the bound, written or inferred.
210fn unbounded :: x: a -> ()
211{
212 cross x //~ ERROR E0402 `a` is not known to implement `Wire`
213}
214
215fn bounded :: x: a -> () where Wire<a>
216{
217 cross [(x, 1)]
218}
219
220fn inferred :: x
221{
222 cross (Some(x), 1)
223}
224//~ TYPE inferred: fn(a) -> () where Wire<a>
225
226fn 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.
232fn phantom :: p: Proxy<a> -> ()
233{
234 cross p
235}
236
237// `Wire<a>` implies `Send<a>`: what can cross hosts can cross actors.
238fn 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.
245fn stored_implies_send :: x: AnyWire -> ()
246{
247 send_only x
248}
249
250fn a_stack_of_functions :: s: Stack<fn() -> ()> -> ()
251{
252 cross s //~ ERROR E0706 `Stack` has a field that cannot cross hosts
253}
254
255impl Wire<Point> {} //~ ERROR E0413 `Wire` cannot be implemented