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.

166 lignes · 2998 octetsBrut · Historique
1import std/io
2import std/list
3import std/ref
4
5effect log
6{
7 fn log :: msg: String -> ()
8}
9
10effect ask
11{
12 fn ask :: -> Int
13}
14
15effect gen
16{
17 ctl yield :: x: Int -> ()
18}
19
20effect fail
21{
22 final ctl fail :: reason: String -> Never
23}
24
25fn with_prefix ::
26 prefix: String
27 action: fn() -> a / <log, io | e>
28 -> a / <io | e>
29{
30 handle
31 {
32 action ()
33 }
34 with
35 { log m -> io.println (prefix <> m) }
36}
37
38fn numbers ::
39{
40 yield 1
41 yield 2
42 yield 3
43}
44
45fn collect :: action: fn() -> () / <gen | e> -> List<Int> / <e>
46{
47 handle
48 {
49 action ()
50 }
51 with
52 { yield x -> [x, ..resume ()], return _ -> [] }
53}
54
55fn safe_div :: a b
56{
57 if b == 0
58 {
59 fail "division by zero"
60 }
61 else
62 {
63 a / b
64 }
65}
66
67fn attempt :: action: fn() -> a / <fail | e> -> Result<a, String> / <e>
68{
69 handle
70 {
71 Ok(action ())
72 }
73 with
74 { fail reason -> Error(reason) }
75}
76
77fn render :: r
78{
79 match r { Ok(v) -> "ok " <> show v, Error(e) -> "failed: " <> e }
80}
81
82// Installs its own `log` handler for its internal messages (silenced).
83fn quietly :: action: fn() -> () / <e> -> () / <e>
84{
85 handle
86 {
87 log "internal detail"
88 action ()
89 }
90 with
91 { log m -> () }
92}
93
94pub fn main :: -> () / <io>
95{
96 // fn operations resume implicitly.
97 {
98 use <- with_prefix "[log] "
99 log "starting"
100 log "done"
101 }
102 let answer = handle { ask () + ask () } with { ask -> 21 }
103 io.println ("answer " <> show answer)
104 // ctl operations capture the rest of the body.
105 io.println (show (collect numbers))
106 // final ctl abandons the body.
107 io.println
108 ( render (attempt fn { safe_div 10 2 }) <> "; "
109 <> render (attempt fn { safe_div 1 0 })
110 )
111 // An inner handler forwards other effects to outer ones (here: `st`
112 // to the region, `ask` to the outer handler).
113 let total = region
114 {
115 let counter = ref.new 0
116 handle
117 {
118 handle
119 {
120 log "inside"
121 ref.set counter (ref.get counter + ask ())
122 }
123 with
124 { log m -> io.println ("inner log: " <> m) }
125 }
126 with
127 { ask -> 5 }
128 ref.get counter
129 }
130 io.println ("counter " <> show total)
131 // A handler can stop early: only the first two yields are kept.
132 let first_two = handle
133 {
134 numbers ()
135 []
136 }
137 with
138 { yield x -> if x > 2 { [] } else { [x, ..resume ()] } }
139 io.println (show (list.length first_two) <> " kept")
140 // Lexical scoping (ADR 0010): the callback's `log` goes to the handler
141 // visible where it is written, not to the one `quietly` installs.
142 {
143 use <- with_prefix "[outer] "
144 use <- quietly
145 log "from the callback"
146 }
147 // Handler clauses run outside their handler: a clause performing its own
148 // effect reaches the next handler out.
149 use <- with_prefix "[outer] "
150 handle
151 {
152 log "relayed"
153 }
154 with
155 { log m -> log ("relay: " <> m) }
156}
157//> [log] starting
158//> [log] done
159//> answer 42
160//> [1, 2, 3]
161//> ok 5; failed: division by zero
162//> inner log: inside
163//> counter 5
164//> 2 kept
165//> [outer] from the callback
166//> [outer] relay: relayed