blob: 939bd28f58cfe58246b76b73a5af755ef36caf55 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
(;module: {#;doc "Algebraic effects."}
lux
(lux (control ["F" functor]
applicative
["M" monad #*])
[io #- run]
(data (coll [list "List/" Monad<List> Monoid<List>])
[number "Nat/" Codec<Text,Nat>]
text/format
[ident "Ident/" Eq<Ident>]
[text])
[macro]
(macro [code]
["s" syntax #+ syntax: Syntax]
(syntax ["cs" common]
(common ["csr" reader]
["csw" writer])))
[type]))
## [Type]
(sig: #export (Handler E M)
{#;doc "A way to interpret effects into arbitrary monads."}
(: (Monad M)
monad)
(: (All [a] (-> (E a) (M a)))
handle))
## [Values]
(type: #hidden (|@ L R)
(All [a] (| (L a) (R a))))
(def: #hidden (combine-functors left right)
(All [L R]
(-> (F;Functor L) (F;Functor R)
(F;Functor (|@ L R))))
(struct
(def: (map f l|r)
(case l|r
(+0 l) (+0 (:: left map f l))
(+1 r) (+1 (:: right map f r)))
)))
(def: #hidden (combine-handlers Monad<M> left right)
(All [L R M]
(-> (Monad M)
(Handler L M) (Handler R M)
(Handler (|@ L R) M)))
(struct
(def: monad Monad<M>)
(def: (handle l|r)
(case l|r
(#;Left l) (:: left handle l)
(#;Right r) (:: right handle r)
))))
## [Syntax]
(syntax: #export (|E [effects (s;many s;any)])
{#;doc (doc "A way to combine smaller effect into a larger effect."
(type: EffABC (|E EffA EffB EffC)))}
(wrap (list (` ($_ ;;|@ (~@ effects))))))
(syntax: #export (|F [functors (s;many s;any)])
{#;doc (doc "A way to combine smaller effect functors into a larger functor."
(def: Functor<EffABC>
(Functor EffABC)
(|F Functor<EffA> Functor<EffB> Functor<EffC>)))}
(wrap (list (` ($_ ;;combine-functors (~@ functors))))))
(syntax: #export (|H monad [handlers (s;many s;any)])
{#;doc (doc "A way to combine smaller effect handlers into a larger handler."
(def: Handler<EffABC,IO>
(Handler EffABC io;IO)
(|H io;Monad<IO>
Handler<EffA,IO> Handler<EffB,IO> Handler<EffC,IO>)))}
(do @
[g!combiner (macro;gensym "")]
(wrap (list (` (let [(~ g!combiner) (;;combine-handlers (~ monad))]
($_ (~ g!combiner) (~@ handlers))))))))
(type: Op
{#name Text
#inputs (List Code)
#output Code})
(def: op^
(Syntax Op)
(s;form (s;either ($_ s;seq
s;local-symbol
(s;tuple (s;some s;any))
s;any)
($_ s;seq
s;local-symbol
(:: s;Monad<Syntax> wrap (list))
s;any))))
(syntax: #export (effect: [exp-lvl csr;export]
[name s;local-symbol]
[ops (s;many op^)])
{#;doc (doc "Define effects by specifying which operations and constants a handler must provide."
(effect: #export EffA
(opA [Nat Text] Bool)
(fieldA Nat))
"In this case, 'opA' will be a function (-> Nat Text Bool)."
"'fieldA' will be a value provided by a handler.")}
(do @
[g!output (macro;gensym "g!output")
#let [op-types (List/map (function [op]
(let [g!tag (code;tag ["" (get@ #name op)])
g!inputs (` [(~@ (get@ #inputs op))])
g!output (` (-> (~ (get@ #output op)) (~ g!output)))]
(` ((~ g!tag) (~ g!inputs) (~ g!output)))))
ops)
type-name (code;symbol ["" name])
type-def (` (type: (~@ (csw;export exp-lvl))
((~ type-name) (~ g!output))
(~@ op-types)))
op-tags (List/map (|>. (get@ #name) [""] code;tag (list) code;tuple)
ops)
functor-def (` (struct: (~@ (csw;export exp-lvl)) (~' _) (F;Functor (~ type-name))
(def: ((~' map) (~' f) (~' fa))
(case (~' fa)
(^template [(~' <tag>)]
((~' <tag>) (~' params) (~' cont))
((~' <tag>) (~' params) (. (~' f) (~' cont))))
((~@ op-tags))))
))
function-defs (List/map (function [op]
(let [g!name (code;symbol ["" (get@ #name op)])
g!tag (code;tag ["" (get@ #name op)])
g!params (: (List Code)
(case (list;size (get@ #inputs op))
+0 (list)
s (|> (list;n.range +0 (n.dec s))
(List/map (|>. Nat/encode
(format "_")
[""]
code;symbol)))))]
(` (def: (~@ (csw;export exp-lvl)) ((~ g!name) (~@ g!params))
(-> (~@ (get@ #inputs op))
((~ type-name) (~ (get@ #output op))))
((~ g!tag) [(~@ g!params)] ;id)))))
ops)]]
(wrap (list& type-def
functor-def
function-defs))))
(type: Translation
{#effect Ident
#target-type Code
#target-monad Code})
(def: translation^
(Syntax Translation)
(s;form (do s;Monad<Syntax>
[_ (s;this (' =>))]
(s;seq s;symbol
(s;tuple (s;seq s;any
s;any))))))
(syntax: #export (handler: [exp-lvl csr;export]
[name s;local-symbol]
[[effect target-type target-monad] translation^]
[defs (s;many (csr;definition *compiler*))])
{#;doc (doc "Define effect handlers by implementing the operations and values of an effect."
(handler: _
(=> EffA [IO Monad<IO>])
(def: (opA length sample)
(:: Monad<IO> wrap (n.< length
(size sample))))
(def: fieldA (:: Monad<IO> wrap +10)))
"Since a name for the handler was not specified, 'handler:' will generate the name as Handler<EffA,IO>.")}
(do @
[(^@ effect [e-module _]) (macro;un-alias effect)
g!input (macro;gensym "g!input")
g!cont (macro;gensym "g!cont")
g!value (macro;gensym "value")
g!wrap (macro;gensym "wrap")
#let [g!cases (|> defs
(List/map (function [def]
(let [g!tag (code;tag [e-module (get@ #cs;definition-name def)])
g!args (List/map (|>. [""] code;symbol)
(get@ #cs;definition-args def))
eff-calc (case (get@ #cs;definition-type def)
#;None
(get@ #cs;definition-value def)
(#;Some type)
(` (: (~ type) (~ (get@ #cs;definition-value def)))))
invocation (case g!args
#;Nil
eff-calc
_
(` ((~ eff-calc) (~@ g!args))))]
(list (` ((~ g!tag) [(~@ g!args)] (~ g!cont)))
(` (do (~ target-monad)
[(~' #let) [(~ g!wrap) (~' wrap)]
(~ g!value) (~ invocation)]
((~ g!wrap) ((~ g!cont) (~ g!value)))))
))))
List/join)]]
(wrap (list (` (struct: (~@ (csw;export exp-lvl)) (~ (code;symbol ["" name]))
(;;Handler (~ (code;symbol effect)) (~ target-type))
(def: (~' monad) (~ target-monad))
(def: ((~' handle) (~ g!input))
(case (~ g!input)
(~@ g!cases))
)))))))
(def: #export (with-handler handler body)
{#;doc "Handles an effectful computation with the given handler to produce a monadic value."}
(All [E M a] (-> (Handler E M) (Free E a) (M a)))
(case body
(#M;Pure value)
(:: handler wrap value)
(#M;Effect effect)
(do (get@ #monad handler)
[result (:: handler handle effect)]
(with-handler handler result))
))
(def: (un-apply type-app)
(-> Type Type)
(case type-app
(#;App effect value)
effect
_
(error! (format "Wrong type format: " (%type type-app)))))
(def: (clean-effect effect)
(-> Type Type)
(case effect
(#;UnivQ env body)
(#;UnivQ (list) body)
_
(error! (format "Wrong effect format: " (%type effect)))))
(def: g!functor Code (code;symbol ["" "\t@E\t"]))
(syntax: #export (doE functor [bindings (s;tuple (s;some s;any))] body)
{#;doc (doc "An alternative to the 'do' macro for monads."
(with-handler Handler<EffABC,IO>
(doE Functor<EffABC>
[a (lift fieldA)
b (lift fieldB)
c (lift fieldC)]
(wrap ($_ n.+ a b c)))))}
(do @
[g!output (macro;gensym "")]
(wrap (list (` (let [(~ g!functor) (~ functor)]
(do (Monad<Free> (~ g!functor))
[(~@ bindings)
(~ g!output) (~ body)]
(#M;Pure (~ g!output)))))))))
(def: (flatten-effect-stack stack)
(-> Type (List Type))
(case stack
(#;Sum left right)
(List/append (flatten-effect-stack left)
(flatten-effect-stack right))
(^ (#;App branches (#;Var _)))
(flatten-effect-stack branches)
(^ (#;App (#;App (#;Named (ident-for ;;|@) _)
left)
right))
(#;Cons left (flatten-effect-stack right))
(^ (#;App (#;App (#;Named (ident-for M;Free) _)
effect)
param))
(list effect)
_
(list stack)
))
(def: (same-effect? expected actual)
(case [expected actual]
[(#;Named e-name _) (#;Named a-name _)]
(Ident/= e-name a-name)
_
false))
(def: (nest-effect idx total base)
(-> Nat Nat Code Code)
(cond (n.= +0 idx)
(` (+0 (~ base)))
(n.> +2 total)
(` (+1 (~ (nest-effect (n.dec idx) (n.dec total) base))))
## else
(` (+1 (~ base)))
))
(syntax: #export (lift [value (s;alt s;symbol
s;any)])
{#;doc (doc "A way to (automatically) lift effectful fields and operations from simple effects into the larger space of composite effects."
(with-handler Handler<EffABC,IO>
(doE Functor<EffABC>
[a (lift fieldA)
b (lift fieldB)
c (lift fieldC)]
(wrap ($_ n.+ a b c)))))}
(case value
(#;Left var)
(do @
[input (macro;find-type var)
output macro;expected-type]
(case [input output]
(^multi [(#;App eff0 _) (#;App stackT0 recT0)]
[(type;apply-type stackT0 recT0) (#;Some unfoldT0)]
[stackT0 (^ (#;App (#;Named (ident-for M;Free) _)
stackT1))]
[(type;apply-type stackT1 recT0) (#;Some unfoldT1)]
[(flatten-effect-stack unfoldT1) stack]
[(|> stack list;enumerate
(list;find (function [[idx effect]]
(same-effect? effect eff0))))
(#;Some [idx _])])
(wrap (list (` (#M;Effect (:: (~ g!functor) (~' map) (~' wrap)
(~ (nest-effect idx (list;size stack) (code;symbol var))))))))
_
(macro;fail (format "Invalid type to lift: " (%type output)))))
(#;Right node)
(do @
[g!value (macro;gensym "")]
(wrap (list (` (let [(~ g!value) (~ node)]
(;;lift (~ g!value)))))))))
|