-
Notifications
You must be signed in to change notification settings - Fork 0
/
2021-11-27-fallback-of-actions.html
463 lines (401 loc) · 28.4 KB
/
2021-11-27-fallback-of-actions.html
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="alternate"
type="application/rss+xml"
href="https://magnus.therning.org/feed.xml"
title="RSS feed for https://magnus.therning.org/">
<title>Fallback of actions</title>
<meta name="author" content="Magnus Therning"><meta name="referrer" content="no-referrer"><link href= "static/style.css" rel="stylesheet" type="text/css" /><link href= "static/htmlize.css" rel="stylesheet" type="text/css" /><link href= "static/extra_style.css" rel="stylesheet" type="text/css" /></head>
<body>
<div id="preamble" class="status"><div class="nav-bar"><a class="nav-link" href="./index.html">Top</a><a class="nav-link" href="./archive.html">Archive</a><a class="nav-link align-right" href="./feed.xml"><img src="static/rss-feed-icon.png" style="height: 24px;" /></a></div></div>
<div id="content">
<div class="post-date">27 Nov 2021</div><h1 class="post-title"><a href="https://magnus.therning.org/2021-11-27-fallback-of-actions.html">Fallback of actions</a></h1>
<p>
In a tool I'm writing I want to load a file that may reside on the local disk,
but if it isn't there I want to fetch it from the web. Basically it's very
similar to having a cache and dealing with a miss, except in my case I don't
populate the cache.
</p>
<p>
Let me first define the functions to play with
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">loadFromDisk</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">String</span> <span class="org-haskell-operator">-></span> <span class="org-haskell-type">IO</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> <span class="org-haskell-type">Int</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">loadFromDisk</span> k<span class="org-haskell-operator">@</span><span class="org-string">"bad key"</span> <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">do</span>
putStrLn <span class="org-haskell-operator">$</span> <span class="org-string">"local: "</span> <span class="org-haskell-operator"><></span> k
pure <span class="org-haskell-operator">$</span> <span class="org-haskell-constructor">Left</span> <span class="org-haskell-operator">$</span> <span class="org-string">"no such local key: "</span> <span class="org-haskell-operator"><></span> k
<span class="org-haskell-definition">loadFromDisk</span> k <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">do</span>
putStrLn <span class="org-haskell-operator">$</span> <span class="org-string">"local: "</span> <span class="org-haskell-operator"><></span> k
pure <span class="org-haskell-operator">$</span> <span class="org-haskell-constructor">Right</span> <span class="org-haskell-operator">$</span> length k
<span class="org-haskell-definition">loadFromWeb</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">String</span> <span class="org-haskell-operator">-></span> <span class="org-haskell-type">IO</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> <span class="org-haskell-type">Int</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">loadFromWeb</span> k<span class="org-haskell-operator">@</span><span class="org-string">"bad key"</span> <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">do</span>
putStrLn <span class="org-haskell-operator">$</span> <span class="org-string">"web: "</span> <span class="org-haskell-operator"><></span> k
pure <span class="org-haskell-operator">$</span> <span class="org-haskell-constructor">Left</span> <span class="org-haskell-operator">$</span> <span class="org-string">"no such remote key: "</span> <span class="org-haskell-operator"><></span> k
<span class="org-haskell-definition">loadFromWeb</span> k <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">do</span>
putStrLn <span class="org-haskell-operator">$</span> <span class="org-string">"web: "</span> <span class="org-haskell-operator"><></span> k
pure <span class="org-haskell-operator">$</span> <span class="org-haskell-constructor">Right</span> <span class="org-haskell-operator">$</span> length k
</pre>
</div>
<div id="outline-container-org06d71ff" class="outline-2">
<h2 id="org06d71ff">Discarded solution: using the <code>Alternative</code> of <code>IO</code> directly</h2>
<div class="outline-text-2" id="text-org06d71ff">
<p>
It's fairly easy to get the desired behaviour but <code>Alternative</code> of <code>IO</code> is based
on exceptions which doesn't strike me as a good idea unless one is using <code>IO</code>
directly. That is fine in a smallish application, but in my case it makes sense
to use tagless style (or <code>ReaderT</code> pattern) so I'll skip exploring this option
completely.
</p>
</div>
</div>
<div id="outline-container-org393493a" class="outline-2">
<h2 id="org393493a">First attempt: lifting into the <code>Alternative</code> of <code>Either e</code></h2>
<div class="outline-text-2" id="text-org393493a">
<p>
There's an instance of <code>Alternative</code> for <code>Either e</code> in version 0.5 of
<a href="https://hackage.haskell.org/package/transformers-0.5.6.2/docs/Control-Monad-Trans-Error.html#section.orphans">transformers</a>. It's deprecated and it's gone in newer versions of the library as
one really should use <code>Except</code> or <code>ExceptT</code> instead. Even if I don't think it's
where I want to end up, it's not an altogether bad place to start.
</p>
<p>
Now let's define a function using <code>liftA2 (<|>)</code> to make it easy to see what the
behaviour is
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">fallBack</span> <span class="org-haskell-operator">::</span>
<span class="org-haskell-type">Applicative</span> m <span class="org-haskell-operator">=></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> res<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> res<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> res<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">fallBack</span> <span class="org-haskell-operator">=</span> liftA2 <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-operator"><|></span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<pre class="example" id="orgfc4f34e">
λ> loadFromDisk "bad key" `fallBack` loadFromWeb "good key"
local: bad key
web: good key
Right 8
λ> loadFromDisk "bad key" `fallBack` loadFromWeb "bad key"
local: bad key
web: bad key
Left "no such remote key: bad key"
</pre>
<p>
The first example shows that it falls back to loading form the web, and the
second one shows that it's only the last failure that survives. The latter part,
that only the last failure survives, isn't ideal but I think I can live with
that. If I were interested in collecting all failures I would reach for
<code>Validation</code> from <a href="https://hackage.haskell.org/package/validation-selective"><code>validation-selective</code></a> (there's one in <a href="https://hackage.haskell.org/package/validation-selective"><code>validation</code></a> that
should work too).
</p>
<p>
So far so good, but the next example shows a behaviour I don't want
</p>
<pre class="example" id="orgfe77c40">
λ> loadFromDisk "good key" `fallBack` loadFromWeb "good key"
local: good key
web: good key
Right 8
</pre>
<p>
or to make it even more explicit
</p>
<pre class="example" id="org0b4b72a">
λ> loadFromDisk "good key" `fallBack` undefined
local: good key
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
undefined, called at <interactive>:451:36 in interactive:Ghci4
</pre>
<p>
There's no short-circuiting!<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup>
</p>
<p>
The behaviour I want is of course that if the first action is successful, then
the second action shouldn't take place at all.
</p>
<p>
It looks like either <code><|></code> is strict in its second argument, or maybe it's
<code>liftA2</code> that forces it. I've not bothered digging into the details, it's enough
to observe it to realise that this approach isn't good enough.
</p>
</div>
</div>
<div id="outline-container-second-attempt" class="outline-2">
<h2 id="second-attempt">Second attempt: cutting it short, manually</h2>
<div class="outline-text-2" id="text-second-attempt">
<p>
Fixing the lack of short-circuiting the evaluation after the first success isn't
too difficult to do manually. Something like this does it
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">fallBack</span> <span class="org-haskell-operator">::</span>
<span class="org-haskell-type">Monad</span> m <span class="org-haskell-operator">=></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> a<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> a<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> <span class="org-haskell-type">String</span> a<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">fallBack</span> first other <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">do</span>
first <span class="org-haskell-operator">>>=</span> <span class="org-haskell-operator">\</span><span class="org-haskell-keyword">case</span>
r<span class="org-haskell-operator">@</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">Right</span> <span class="org-haskell-keyword">_</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span> pure r
r<span class="org-haskell-operator">@</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">Left</span> <span class="org-haskell-keyword">_</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span> <span class="org-rainbow-delimiters-depth-1">(</span>r <span class="org-haskell-operator"><|></span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator"><$></span> other
</pre>
</div>
<p>
It does indeed show the behaviour I want
</p>
<pre class="example" id="org3813a45">
λ> loadFromDisk "bad key" `fallBack` loadFromWeb "good key"
local: bad key
web: good key
Right 8
λ> loadFromDisk "bad key" `fallBack` loadFromWeb "bad key"
local: bad key
web: bad key
Left "no such remote key: bad key"
λ> loadFromDisk "good key" `fallBack` undefined
local: good key
Right 8
</pre>
<p>
Excellent! And to switch over to use <code>Validation</code> one just have to switch
constructors, <code>Right</code> becomes <code>Success</code> and <code>Left</code> becomes <code>Failure</code>. Though
collecting the failures by concatenating strings isn't the best idea of course.
Switching to some other <code>Monoid</code> (that's the constraint on the failure type)
isn't too difficult.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">fallBack</span> <span class="org-haskell-operator">::</span>
<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Monad</span> m, <span class="org-haskell-type">Monoid</span> e<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">=></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Validation</span> e a<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Validation</span> e a<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Validation</span> e a<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">fallBack</span> first other <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">do</span>
first <span class="org-haskell-operator">>>=</span> <span class="org-haskell-operator">\</span><span class="org-haskell-keyword">case</span>
r<span class="org-haskell-operator">@</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">Success</span> <span class="org-haskell-keyword">_</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span> pure r
r<span class="org-haskell-operator">@</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">Failure</span> <span class="org-haskell-keyword">_</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span> <span class="org-rainbow-delimiters-depth-1">(</span>r <span class="org-haskell-operator"><|></span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator"><$></span> other
</pre>
</div>
</div>
</div>
<div id="outline-container-org5d3ae21" class="outline-2">
<h2 id="org5d3ae21">Third attempt: pulling failures out to <code>MonadPlus</code></h2>
<div class="outline-text-2" id="text-org5d3ae21">
<p>
After writing the <code>fallBack</code> function I still wanted to explore other solutions.
There's almost always something more out there in the Haskell eco system, right?
So I asked in the <i>#haskell-beginners</i> channel on the Functional Programming
Slack. The way I asked the question resulted in answers that iterates over a
list of actions and cutting at the first success.
</p>
<p>
The first suggestion had me a little confused at first, but once I re-organised
the helper function a little it made more sense to me.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">mFromRight</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">MonadPlus</span> m <span class="org-haskell-operator">=></span> m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span> m res
<span class="org-haskell-definition">mFromRight</span> <span class="org-haskell-operator">=</span> <span class="org-rainbow-delimiters-depth-1">(</span>either <span class="org-rainbow-delimiters-depth-2">(</span>const mzero<span class="org-rainbow-delimiters-depth-2">)</span> return <span class="org-haskell-operator">=<<</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
To use it put the actions in a list, map the helper above, and finally run
<code>asum</code> on it all<sup><a id="fnr.2" class="footref" href="#fn.2" role="doc-backlink">2</a></sup>. I think it makes it a little clearer what happens if
it's rewritten like this.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">firstRightM</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">MonadPlus</span> m <span class="org-haskell-operator">=></span> <span class="org-rainbow-delimiters-depth-1">[</span>m <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">]</span> <span class="org-haskell-operator">-></span> m res
<span class="org-haskell-definition">firstRightM</span> <span class="org-haskell-operator">=</span> asum <span class="org-haskell-operator">.</span> fmap go
<span class="org-haskell-keyword">where</span>
go m <span class="org-haskell-operator">=</span> m <span class="org-haskell-operator">>>=</span> either <span class="org-rainbow-delimiters-depth-1">(</span>const mzero<span class="org-rainbow-delimiters-depth-1">)</span> return
</pre>
</div>
<pre class="example" id="orgc0d84cf">
λ> firstRightM [loadFromDisk "bad key", loadFromWeb "good key"]
local: bad key
web: good key
8
λ> firstRightM [loadFromDisk "good key", undefined]
local: good key
8
</pre>
<p>
So far so good, but I left out the case where both fail, because that's sort of
the fly in the ointment here
</p>
<pre class="example" id="org9b2d311">
λ> firstRightM [loadFromDisk "bad key", loadFromWeb "bad key"]
local: bad key
web: bad key
*** Exception: user error (mzero)
</pre>
<p>
It's not nice to be back to deal with exceptions, but it's possible to recover,
e.g. by appending <code><|> pure 0</code>.
</p>
<pre class="example" id="org1198c83">
λ> firstRightM [loadFromDisk "bad key", loadFromWeb "bad key"] <|> pure 0
local: bad key
web: bad key
0
</pre>
<p>
However that removes the ability to deal with the situation where all actions
fail. Not nice! Add to that the difficulty of coming up with a <i>good</i>
<code>MonadPlus</code> instance for an application monad; one basically have to resort to
the same thing as for <code>IO</code>, i.e. to throw an exception. Also not nice!
</p>
</div>
</div>
<div id="outline-container-fourth-attempt" class="outline-2">
<h2 id="fourth-attempt">Fourth attempt: wrapping in <code>ExceptT</code> to get its <code>Alternative</code> behaviour</h2>
<div class="outline-text-2" id="text-fourth-attempt">
<p>
This was another suggestion from the Slack channel, and it is the one I like the
most. Again it was suggested as a way to stop at the first successful action in
a list of actions.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">firstRightM</span> <span class="org-haskell-operator">::</span>
<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Foldable</span> t, <span class="org-haskell-type">Functor</span> t, <span class="org-haskell-type">Monad</span> m, <span class="org-haskell-type">Monoid</span> err<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">=></span>
t <span class="org-rainbow-delimiters-depth-1">(</span>m <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">firstRightM</span> <span class="org-haskell-operator">=</span> runExceptT <span class="org-haskell-operator">.</span> asum <span class="org-haskell-operator">.</span> fmap <span class="org-haskell-constructor">ExceptT</span>
</pre>
</div>
<p>
Which can be used similarly to the previous one. It's also easy to write a
variant of <code>fallBack</code> for it.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">fallBack</span> <span class="org-haskell-operator">::</span>
<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Monad</span> m, <span class="org-haskell-type">Monoid</span> err<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">=></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span>
m <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Either</span> err res<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-definition">fallBack</span> first other <span class="org-haskell-operator">=</span> runExceptT <span class="org-haskell-operator">$</span> <span class="org-haskell-constructor">ExceptT</span> first <span class="org-haskell-operator"><|></span> <span class="org-haskell-constructor">ExceptT</span> other
</pre>
</div>
<pre class="example" id="orgb08a34f">
λ> loadFromDisk "bad key" `fallBack` loadFromWeb "good key"
local: bad key
web: good key
Right 8
λ> loadFromDisk "good key" `fallBack` undefined
local: good key
Right 8
λ> loadFromDisk "bad key" `fallBack` loadFromWeb "bad key"
local: bad key
web: bad key
Left "no such local key: bad keyno such remote key: bad key"
</pre>
<p>
Yay! This solution has the short-circuiting behaviour I want, as well as
collecting all errors on failure.
</p>
</div>
</div>
<div id="outline-container-org63037a5" class="outline-2">
<h2 id="org63037a5">Conclusion</h2>
<div class="outline-text-2" id="text-org63037a5">
<p>
I'm still a little disappointed that <code>liftA2 (<|>)</code> isn't short-circuiting as I
still think it's the easiest of the approaches. However, it's a problem that one
has to rely on a deprecated instance of <code>Alternative</code> for <code>Either String</code>,
but switching to use <code>Validation</code> would be only a minor change.
</p>
<p>
Manually writing the <code>fallBack</code> function, as I did in the <a href="#second-attempt">second attempt</a>,
results in very explicit code which is nice as it often reduces the cognitive
load for the reader. It's a contender, but using the deprecated <code>Alternative</code>
instance is problematic and introducing <code>Validition</code>, an arguably not very
common type, takes away a little of the appeal.
</p>
<p>
In the end I prefer the <a href="#fourth-attempt">fourth attempt</a>. It behaves exactly like I want and even
though <code>ExpectT</code> lives in <i>transformers</i> I feel that it (I pull it in via <i>mtl</i>)
is in such wide use that most Haskell programmers will be familiar with it.
</p>
<p>
One final thing to add is that the <a href="https://hackage.haskell.org/package/validation-selective-0.1.0.1/docs/Validation.html">documentation of <code>Validation</code></a> is an excellent
inspiration when it comes to the behaviour of its instances. I wish that the
documentation of other packages, in particular commonly used ones like <i>base</i>,
<i>transformers</i>, and <i>mtl</i>, would be more like it.
</p>
</div>
</div>
<div id="outline-container-org1914c53" class="outline-2">
<h2 id="org1914c53">Comments, feedback, and questions</h2>
<div class="outline-text-2" id="text-org1914c53">
</div>
<div id="outline-container-org5082aab" class="outline-3">
<h3 id="org5082aab"><span class="timestamp-wrapper"><span class="timestamp">[2021-11-28 Sun] </span></span> Dustin Sallings</h3>
<div class="outline-text-3" id="text-org5082aab">
<div class="notes" id="org1f46424">
<p>
Dustin sent me a comment via email a while ago, it's now March 2022 so it's
taken me embarrassingly long to publish it here.
</p>
<p>
I removed a bit from the beginning of the email as it doesn't relate to this
post.
</p>
</div>
<p>
… a thing I've written code for before that I was reasonably pleased with. I
have a suite of software for managing my GoPro media which involves doing some
metadata extraction from images and video. There will be multiple transcodings
of each medium with each that contains the metadata having it completely intact
(i.e., low quality encodings do not lose metadata fidelity). I also run this on
multiple machines and store a cache of Metadata in S3.
</p>
<p>
Sometimes, I've already processed the metadata on another machine. Often, I can
get it from the lowest quality. Sometimes, there's no metadata at all. The core
of my extraction looks like this:
</p>
<div class="org-src-container">
<pre class="src src-haskell">ms <span class="org-haskell-operator"><-</span> asum <span class="org-rainbow-delimiters-depth-1">[</span>
<span class="org-haskell-constructor">Just</span> <span class="org-haskell-operator">.</span> BL.toStrict <span class="org-haskell-operator"><$></span> getMetaBlob mid,
fv <span class="org-string">"mp4_low"</span> <span class="org-rainbow-delimiters-depth-2">(</span>fn <span class="org-string">"low"</span><span class="org-rainbow-delimiters-depth-2">)</span>,
fv <span class="org-string">"high_res_proxy_mp4"</span> <span class="org-rainbow-delimiters-depth-2">(</span>fn <span class="org-string">"high"</span><span class="org-rainbow-delimiters-depth-2">)</span>,
fv <span class="org-string">"source"</span> <span class="org-rainbow-delimiters-depth-2">(</span>fn <span class="org-string">"src"</span><span class="org-rainbow-delimiters-depth-2">)</span>,
pure <span class="org-haskell-constructor">Nothing</span><span class="org-rainbow-delimiters-depth-1">]</span>
</pre>
</div>
<p>
The first version grabs the processed blob from S3. The next three fetch (and
process) increasingly larger variants of the uploaded media. The last one just
gives up and says there's no metadata available (and memoizes that in the local
DB and S3).
</p>
<p>
Some of these objects are in the tens of gigs, and I had a really bad internet
connection when I first wrote this software, so I needed it to work.
</p>
</div>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
I'm not sure if it's a good term to use in this case as <a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation">Wikipedia</a> says
it's for Boolean operators. I hope it's not too far a stretch to use it in this
context too.
</p></div></div>
<div class="footdef"><sup><a id="fn.2" class="footnum" href="#fnr.2" role="doc-backlink">2</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
In the version of <i>base</i> I'm using there is no <code>asum</code>, so I simply copied
the implementation from a later version:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">asum</span> <span class="org-haskell-operator">::</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-type">Foldable</span> t, <span class="org-haskell-type">Alternative</span> f<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">=></span> t <span class="org-rainbow-delimiters-depth-1">(</span>f a<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-haskell-operator">-></span> f a
<span class="org-haskell-definition">asum</span> <span class="org-haskell-operator">=</span> foldr <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-operator"><|></span><span class="org-rainbow-delimiters-depth-1">)</span> empty
</pre>
</div></div></div>
</div>
</div><div class="taglist"><a href="https://magnus.therning.org/tags.html">Tags</a>: <a href="https://magnus.therning.org/tag-alternative_typeclass.html">alternative_typeclass</a> <a href="https://magnus.therning.org/tag-caching.html">caching</a> <a href="https://magnus.therning.org/tag-fallback.html">fallback</a> <a href="https://magnus.therning.org/tag-haskell.html">haskell</a> </div></div>
<div id="postamble" class="status"><!-- org-static-blog-page-postamble --></div>
</body>
</html>