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.

381 lignes · 7481 octetsBrut · Historique
1import std/io
2import std/process.{type proc}
3import std/ref
4import std/result
5
6effect log
7{
8 fn log :: msg: String -> ()
9}
10
11effect ask
12{
13 fn ask :: -> Int
14}
15
16effect gen
17{
18 ctl yield :: x: Int -> ()
19}
20
21effect fail
22{
23 final ctl fail :: reason: String -> Never
24}
25
26effect state<s>
27{
28 fn get :: -> s
29 fn put :: x: s -> ()
30}
31
32// Performing an operation adds its effect.
33fn greet :: name
34{
35 log ("hi " <> name)
36}
37//~ TYPE greet: fn(String) -> () / <log>
38
39fn counter ::
40{
41 put (get () + 1)
42}
43//~ TYPE counter: fn() -> () / <state<Int>>
44
45// Higher-order functions are effect-polymorphic.
46fn twice :: f
47{
48 f ()
49 f ()
50}
51//~ TYPE twice: fn(fn() -> a / <e>) -> a / <e>
52
53// A handler removes its effect from the body's row. A callback called
54// inside the handler receives it, so its type must be declared (ADR 0010).
55fn quiet :: action: fn() -> a / <log | e> -> a / <e>
56{
57 handle
58 {
59 action ()
60 }
61 with
62 { log m -> () }
63}
64//~ TYPE quiet: fn(fn() -> a / <log | e>) -> a / <e>
65
66fn quiet_inferred :: action
67{
68 handle
69 {
70 action () //~ ERROR E0703 `log` handler be used outside its scope
71 }
72 with
73 { log m -> () }
74}
75
76// `fn` clauses produce the operation's result.
77fn answer ::
78{
79 handle
80 {
81 ask () + ask ()
82 }
83 with
84 { ask -> 21 }
85}
86//~ TYPE answer: fn() -> Int
87
88// `ctl` clauses produce the handler's result and may resume.
89fn collect :: action: fn() -> a / <gen | e> -> List<Int> / <e>
90{
91 handle
92 {
93 action ()
94 }
95 with
96 { yield x -> [x, ..resume ()], return _ -> [] }
97}
98//~ TYPE collect: fn(fn() -> a / <gen | e>) -> List<Int> / <e>
99
100// `final ctl` never resumes: the handler's result replaces the body's.
101fn attempt :: action: fn() -> a / <fail | e> -> Option<a> / <e>
102{
103 handle
104 {
105 Some(action ())
106 }
107 with
108 { fail _ -> None }
109}
110//~ TYPE attempt: fn(fn() -> a / <fail | e>) -> Option<a> / <e>
111
112// Annotated signatures bound what a body may perform.
113pub fn total :: -> ()
114{
115 log "no" //~ ERROR E0502 performs `log` which is not handled here
116}
117
118pub fn declared :: -> () / <log>
119{
120 log "yes"
121}
122
123// A declared effect the body never performs draws a warning (the row is
124// the contract a caller pays for; issue #203, option A).
125pub fn dead_member ::
126 -> () / <log, ask> //~ WARN W0501
127{
128 log "yes"
129}
130
131// A lambda's own effects are its scope's, not the fn's; the call to it
132// performs them, so this row is honest (no warning).
133pub fn via_lambda :: -> () / <log>
134{
135 let f = fn { log "in a lambda" }
136 f ()
137}
138
139// A callee's row is included in the caller's: a callback with fewer
140// effects than its caller is fine, because operations go to the handlers
141// visible where they are written, never to the caller's (ADR 0010).
142pub fn same_row :: f: fn() -> () / <log, e> -> () / <log, e>
143{
144 f ()
145 log "after"
146}
147
148pub fn fewer_effects :: f: fn() -> () / <e> -> () / <log, e>
149{
150 f ()
151 log "after"
152}
153
154// A total function can be passed where any row is expected (opening).
155fn pure_callback :: -> () / <log>
156{
157 same_row fn { () }
158}
159
160pub fn rigid_row :: f: fn() -> () / <e> -> () / <e>
161{
162 f ()
163 log "extra" //~ ERROR E0502 performs `log` which is not handled here
164}
165
166// Calls `f` once (a stand-in for `result.try`'s callback: anything taking
167// a function).
168fn calls :: f: fn() -> () / <e> -> () / <e>
169{
170 f ()
171}
172
173// In a callback (a lambda), a concrete effect and then the action, whose
174// row's tail is rigid: the callback's flexible tail becomes the callee's
175// extras over the rigid one (was E0502; the postgres package's
176// `savepoint` is this shape).
177pub fn use_callback :: action: fn() -> () / <ask | e> -> () / <log, ask | e>
178{
179 calls
180 fn
181 {
182 log "first"
183 action ()
184 }
185}
186//~ TYPE use_callback: fn(fn() -> () / <ask | e>) -> () / <log, ask | e>
187
188// The same with the concrete effect after the rigid-tail call: the
189// order no longer matters — the rigid tail binds at settle, when the
190// callback's row has finished growing (issue #312).
191pub fn use_callback_late ::
192 action: fn() -> () / <ask | e>
193 -> () / <log, ask | e>
194{
195 calls
196 fn
197 {
198 action ()
199 log "after"
200 }
201}
202//~ TYPE use_callback_late: fn(fn() -> () / <ask | e>) -> () / <log, ask | e>
203
204// A use-continuation may perform more of the row after passing a
205// row-polymorphic parameter (issue #312): the rigid tail binds at
206// settle, not at the call.
207fn read312 ::
208 env: fn(String) -> Option<String> / <e>
209 -> Result<Int, String> / <e>
210{
211 match env "b"
212 { Some(_) -> Ok 1 //~ ERROR E0216
213 , None -> Error "missing" //~ ERROR E0216
214 }
215}
216
217fn twice312 ::
218 env: fn(String) -> Option<String> / <e>
219 -> Result<Int, String> / <proc | e>
220{
221 use a <- result.try (Ok 1) //~ ERROR E0216
222 use b <- result.try (read312 env)
223 let _ = process.subject ()
224 Ok(a + b)
225}
226//~ TYPE twice312: fn(fn(String) -> Option<String> / <e>) -> Result<Int, String> / <proc | e>
227
228// Built-in effects come from the standard library.
229pub fn main :: -> () / <io>
230{
231 io.println "hello"
232}
233
234pub fn counter_ref :: -> Int
235{
236 region
237 {
238 let r = ref.new 0
239 ref.set r (ref.get r + 1)
240 ref.get r
241 }
242}
243
244pub fn state_total :: -> ()
245{
246 counter () //~ ERROR E0502 performs `state<Int>`
247}
248
249// Handler shape errors.
250fn missing_op ::
251{
252 handle
253 { //~ ERROR E0504 does not handle operation `put`
254 counter ()
255 }
256 with
257 { get -> 0 }
258}
259
260fn mixed ::
261{
262 handle
263 {
264 greet "x"
265 }
266 with
267 { log m -> ()
268 , ask -> 1 //~ ERROR E0501 this clause is for `ask`, not `log`
269 }
270}
271
272fn wrong_clause_type ::
273{
274 handle
275 {
276 ask ()
277 }
278 with
279 { ask -> "not an int" //~ ERROR E0202 expected `Int`, found `String`
280 }
281}
282
283fn no_resume_in_fn_clause ::
284{
285 handle
286 {
287 log "x"
288 }
289 with
290 { log m -> resume () //~ ERROR E0101 only available in `ctl` handler clauses
291 }
292}
293
294// Lambdas carry their own effects.
295pub fn lambda_effect :: -> ()
296{
297 let f = fn { log "x" }
298 //~^ ERROR E0502 performs `log` which is not handled here
299 f ()
300}
301
302// Lexical scoping (ADR 0010): an operation goes to the nearest handler
303// where it is written; its type arguments do not pick another one.
304// Here `get()` reaches the inner handler, so that handler's state is an
305// Int and its String clause is wrong.
306fn nearest_handler ::
307{
308 handle
309 {
310 handle
311 {
312 let n: Int = get ()
313 n
314 }
315 with
316 { get -> "inner" //~ ERROR E0202 expected `Int`, found `String`
317 , put x -> ()
318 }
319 }
320 with
321 { get -> 0, put x -> () }
322}
323
324// A closure cannot outlive the handler it uses.
325fn escaping_closure ::
326{
327 let f = handle
328 { //~ ERROR E0703 `log` handler be used outside its scope
329 fn { log "x" }
330 }
331 with
332 { log m -> () }
333 f
334}
335
336// A lambda written inside a handler uses that handler, so it cannot be
337// passed where a callback that receives `log` from its caller is expected.
338fn stored_lambda :: -> () / <io>
339{
340 handle
341 {
342 let f = fn { log "x" }
343 quiet f //~ ERROR E0202 receives the `log` handler from its caller
344 }
345 with
346 { log m -> io.println m }
347}
348
349// Written directly as the argument, it receives `quiet`'s handler.
350fn direct_lambda :: -> ()
351{
352 quiet fn { log "x" }
353}
354
355// Effect rows must contain effects.
356pub fn bad_row ::
357 x: Int
358 -> Int / <Int> //~ ERROR E0505 `Int` is not an effect
359{
360 x
361}
362pub fn row_var_first :: f: fn() -> () / <e, log> -> ()
363//~^ ERROR E0505 must come last
364{
365 ()
366}
367
368// Trait methods declare effects; impls may not add any.
369trait Greeter<a>
370{
371 fn greet_with :: x: a -> String
372}
373
374impl Greeter<Int>
375{
376 fn greet_with :: x
377 {
378 log "side effect" //~ ERROR E0502 performs `log` which is not handled here
379 show x
380 }
381}