tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

77 lignes · 1382 octetsBrut · Historique
1import std/io
2import std/list
3
4// A `multi ctl` continuation cannot hold another handler of `ctl`
5// operations: natively that handler's body runs on a fiber of its own,
6// which is not copied (ADR 0017). Both backends refuse to resume it.
7
8effect amb
9{
10 multi ctl choose :: -> Bool
11}
12
13effect gen
14{
15 ctl yield :: x: Int -> ()
16}
17
18fn all :: action: fn() -> a / <amb | e> -> List<a> / <e>
19{
20 handle
21 {
22 [action ()]
23 }
24 with
25 { choose -> list.append (resume True) (resume False) }
26}
27
28fn total :: action: fn() -> () / <gen | e> -> Int / <e>
29{
30 handle
31 {
32 action ()
33 }
34 with
35 { yield x -> x + resume (), return _ -> 0 }
36}
37
38pub fn main :: -> () / <io>
39{
40 // A generator that finished before `choose` is fine.
41 let fine =
42 all
43 fn
44 {
45 let t = total fn { yield 1 }
46 if choose ()
47 {
48 t
49 }
50 else
51 {
52 t + 1
53 }
54 }
55 io.println ("fine " <> show fine)
56 // One still running when `choose` captures is not.
57 let refused =
58 all
59 fn
60 {
61 total
62 fn
63 {
64 if choose ()
65 {
66 yield 1
67 }
68 else
69 {
70 yield 2
71 }
72 }
73 }
74 io.println ("not reached " <> show refused)
75}
76//> fine [1, 2]
77//! panic: a `multi ctl` continuation holding a `ctl` handler cannot be resumed