-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.html
861 lines (810 loc) · 56.3 KB
/
example.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Sam Derbyshire, Well-Typed">
<title>GHC’s renamer (Part II)</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="reveal.js/dist/reset.css">
<link rel="stylesheet" href="reveal.js/dist/reveal.css">
<style>
.reveal .sourceCode { /* see #7635 */
overflow: visible;
}
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<link rel="stylesheet" href="reveal.js/dist/theme/welltyped_theme.css" id="theme">
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title-slide">
<h1 class="title">GHC’s renamer (Part II)</h1>
<p class="subtitle">GHC Contributors’ Workshop</p>
<p class="author">Sam Derbyshire, Well-Typed</p>
<p class="date">June 7th, 2023</p>
</section>
<section class="slide level2">
<!--
Compile the HTML slides with Pandoc:
pandoc -t revealjs -s -o example.html example.md -V revealjs-url=reveal.js -V theme=welltyped_theme -V controlsTutorial=false -V controlsLayout=edges -V slideNumber='c/t' -V transition=slide
Custom theme in reveal.js/dist/themes/welltyped_theme.css
-->
</section>
<section id="the-problem" class="slide level2">
<h2>The problem</h2>
<div class="sourceCode" id="cb1"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# COMPLETE U #-}</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">pattern</span> <span class="dt">U</span><span class="ot"> ::</span> ()</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">pattern</span> <span class="dt">U</span> <span class="ot">=</span> ()</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="ot">foo ::</span> <span class="dt">Identity</span> <span class="dt">Char</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>foo <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">U</span> <span class="ot"><-</span> <span class="fu">return</span> ()</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span> <span class="ch">'c'</span></span></code></pre></div>
<div class="sourceCode" id="cb2"><pre
class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="an">error:</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co"> * No instance for `MonadFail Identity'</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"> arising from a do statement</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co"> with the failable pattern `U'</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="co"> * In a stmt of a 'do' block: U <- return ()</span></span></code></pre></div>
<p class="indicator">
⭲
</p>
</section>
<section id="context" class="slide level2">
<h2>Context</h2>
<p>This has been reported many times: <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/15681">#15681</a> <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/16470">#16470</a> <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/22004">#22004</a> <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/23458">#23458</a>
(related: <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/16618">#16618</a>).</p>
<p>Fundamental problem:</p>
<ul>
<li>the renamer decides how to elaborate <code>do</code>-notation,</li>
<li>the pattern-match checker runs after type-checking (it needs
types).</li>
</ul>
<p><br/></p>
<div class="element: fragment">
<p>However, in this situation, we don’t need types to be able to see
that the pattern match succeeds (this is different from situation which
involve GADT pattern matches).</p>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="investigation" class="slide level2">
<h2>Investigation</h2>
<p>From the error message, we can see that we emit a
<code>MonadFail Identity</code> Wanted constraint when typechecking
<code>foo</code>.</p>
<p>Where? We can search for <code>MonadFail</code> to find out!</p>
<p class="indicator">
⭲
</p>
</section>
<section id="in-the-renamer" class="slide level2">
<h2>In the renamer</h2>
<p><code>GHC.Rename.Expr</code>:</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>rnStmt ctxt rnBody (<span class="dt">L</span> loc (<span class="dt">BindStmt</span> _ pat (<span class="dt">L</span> lb body))) thing_inside</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="kw">do</span> { <span class="co">-- ...</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> ; (fail_op, fvs2) <span class="ot"><-</span> monadFailOp pat ctxt</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> ; <span class="co">-- ...</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> }</span></code></pre></div>
<div class="sourceCode" id="cb4"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ot">monadFailOp ::</span> <span class="dt">LPat</span> <span class="dt">GhcPs</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">-></span> <span class="dt">HsStmtContext</span> <span class="dt">GhcRn</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">-></span> <span class="dt">RnM</span> (<span class="dt">FailOperator</span> <span class="dt">GhcRn</span>, <span class="dt">FreeVars</span>)</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>monadFailOp pat ctxt <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> dflags <span class="ot"><-</span> getDynFlags</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">if</span> <span class="op">|</span> isIrrefutableHsPat dflags pat <span class="ot">-></span> <span class="fu">return</span> (<span class="dt">Nothing</span>, emptyFVs)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">not</span> (isMonadStmtContext ctxt) <span class="ot">-></span> <span class="fu">return</span> (<span class="dt">Nothing</span>, emptyFVs)</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">-></span> getMonadFailOp ctxt</span></code></pre></div>
<p class="indicator">
⭲
</p>
</section>
<section id="in-the-typechecker" class="slide level2">
<h2>In the typechecker</h2>
<p><code>GHC.Tc.Gen.Match</code> (<code>Gen</code> = constraint
generator):</p>
<div class="sourceCode" id="cb5"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>tcDoStmt ctxt (<span class="dt">BindStmt</span> xbsrn pat rhs) res_ty thing_inside</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="kw">do</span> { <span class="co">-- ...</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> ; fail_op' <span class="ot"><-</span> <span class="fu">fmap</span> join <span class="op">.</span> forM (xbsrn_failOp xbsrn) <span class="op">$</span> \<span class="fu">fail</span> <span class="ot">-></span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> tcMonadFailOp (<span class="dt">DoPatOrigin</span> pat) pat' <span class="fu">fail</span> new_res_ty</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> ; <span class="co">-- ...</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> ; <span class="fu">return</span> (<span class="dt">BindStmt</span> xbstc pat' rhs', thing) }</span></code></pre></div>
<div class="sourceCode" id="cb6"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>tcMonadFailOp orig pat fail_op res_ty <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> dflags <span class="ot"><-</span> getDynFlags</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">if</span> isIrrefutableHsPat dflags pat</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">then</span> <span class="fu">return</span> <span class="dt">Nothing</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">else</span> <span class="dt">Just</span> <span class="op">.</span> <span class="fu">snd</span> <span class="op"><$></span> (tcSyntaxOp orig fail_op [synKnownType stringTy]</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> (mkCheckExpType res_ty) <span class="op">$</span> \_ _ <span class="ot">-></span> <span class="fu">return</span> ())</span></code></pre></div>
<div class="element: fragment">
<code>tcSyntaxOp</code> is going to instantiate
<code>fail :: MonadFail m => String -> m a</code>, which will emit
a <code>MonadFail Identity</code> Wanted constraint.
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="irrefutable-patterns" class="slide level2">
<h2>Irrefutable patterns</h2>
<div class="element: fragment">
<div class="sourceCode" id="cb7"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ot">isIrrefutableHsPat ::</span> <span class="dt">Bool</span> <span class="co">-- ^ Is @-XStrict@ enabled?</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">-></span> <span class="dt">Pat</span> (<span class="dt">GhcPass</span> p) <span class="ot">-></span> <span class="dt">Bool</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>isIrrefutableHsPat is_strict <span class="ot">=</span> \<span class="kw">case</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">WildPat</span> {} <span class="ot">-></span> <span class="dt">True</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">VarPat</span> {} <span class="ot">-></span> <span class="dt">True</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="co">-- ...</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">ConPat</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> { pat_con <span class="ot">=</span> con</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> , pat_args <span class="ot">=</span> details } <span class="ot">-></span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> ghcPass <span class="op">@</span>p <span class="kw">of</span></span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="dt">GhcPs</span> <span class="ot">-></span> <span class="dt">False</span> <span class="co">-- Conservative</span></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="dt">GhcRn</span> <span class="ot">-></span> <span class="dt">False</span> <span class="co">-- Conservative</span></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="dt">GhcTc</span> <span class="ot">-></span> <span class="kw">case</span> con <span class="kw">of</span></span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="dt">L</span> _ (<span class="dt">PatSynCon</span> _pat) <span class="ot">-></span> <span class="dt">False</span> <span class="co">-- Conservative</span></span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> <span class="dt">L</span> _ (<span class="dt">RealDataCon</span> con) <span class="ot">-></span></span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> isJust (tyConSingleDataCon_maybe (dataConTyCon con))</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="op">&&</span> <span class="fu">all</span> goL (hsConPatArgs details)</span></code></pre></div>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="approach" class="slide level2">
<h2>Approach</h2>
<p>Let’s fix <code>isIrrefutableHsPat</code> for <code>ConPat</code> at
<code>GhcRn</code> and <code>GhcTc</code> stages.</p>
<div class="sourceCode" id="cb8"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Pat</span> p</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="co">-- ...</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="dt">ConPat</span> {</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="ot"> pat_con_ext ::</span> <span class="dt">XConPat</span> p,</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="ot"> pat_con ::</span> <span class="dt">XRec</span> p (<span class="dt">ConLikeP</span> p),</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a><span class="ot"> pat_args ::</span> <span class="dt">HsConPatDetails</span> p</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> }</span></code></pre></div>
<div class="element: fragment">
<div class="sourceCode" id="cb9"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ConLikeP</span> <span class="dt">GhcPs</span> <span class="ot">=</span> <span class="dt">RdrName</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ConLikeP</span> <span class="dt">GhcRn</span> <span class="ot">=</span> <span class="dt">Name</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ConLikeP</span> <span class="dt">GhcTc</span> <span class="ot">=</span> <span class="dt">ConLike</span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">XConPat</span> <span class="dt">GhcPs</span> <span class="ot">=</span> <span class="dt">EpAnn</span> [<span class="dt">AddEpAnn</span>]</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">XConPat</span> <span class="dt">GhcRn</span> <span class="ot">=</span> <span class="dt">NoExtField</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">XConPat</span> <span class="dt">GhcTc</span> <span class="ot">=</span> <span class="dt">ConPatTc</span></span></code></pre></div>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="what-to-change" class="slide level2">
<h2>What to change?</h2>
<p>We need two things:</p>
<ul>
<li>for a <code>DataCon</code>, whether it is irrefutable
(<code>tyConSingleDataCon_maybe</code>);</li>
<li>for a <code>PatSyn</code>, whether it is the unique member of a
COMPLETE set.</li>
</ul>
<p>For <code>PatSyn</code>s, we have the choice of storing whether the
<code>PatSyn</code> is irrefutable in the <code>PatSyn</code> itself, or
of threading through the <code>COMPLETE</code> pragmas to
<code>isIrrefutableHsPat</code>.</p>
<p><br/></p>
<div class="element: fragment">
<p>I’ve chosen to thread through <code>COMPLETE</code> pragmas, as that
would also allow us to handle <code>or</code> patterns:</p>
<div class="sourceCode" id="cb10"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# COMPLETE P, Q #-}</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="kw">do</span> (one <span class="kw">of</span> {<span class="dt">P</span>; <span class="dt">Q</span>}) <span class="ot"><-</span> <span class="op">...</span></span></code></pre></div>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="using-the-extension-field" class="slide level2">
<h2>Using the extension field</h2>
<p>Idea: let’s store whether the constructor pattern is a
<code>DataCon</code> or a <code>PatSyn</code>, and if it’s a
<code>DataCon</code> whether it’s irrefutable, in
<code>XConPat GhcRn</code>.</p>
<div class="sourceCode" id="cb11"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">ConInfoType</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="dt">ConIsData</span> {<span class="ot"> conIsSingleDataCon ::</span> <span class="dt">Bool</span> }</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="dt">ConIsPatSyn</span></span></code></pre></div>
<p>(Alternatively we could change <code>ConLikeP GhcRn</code> to
<code>ConLikeName</code>, but that wouldn’t handle the
<code>tyConSingleDataCon</code> situation.)</p>
<p><br/></p>
<div class="element: fragment">
This leaves the task of passing the collection of <code>COMPLETE</code>
pragmas to <code>isIrrefutableHsPat</code>.
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="complete-pragmas" class="slide level2">
<h2>COMPLETE pragmas</h2>
<p>We want to pass <code>COMPLETE</code> sets to
<code>isIrrefutableHsPat</code>.<br />
So let’s start off by figuring out how these are represented in the
compiler.</p>
<div class="element: fragment">
<div class="sourceCode" id="cb12"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- In Language.Haskell.Syntax.Binds</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Sig</span> pass</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="co">-- ...</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="dt">CompleteMatchSig</span></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> (<span class="dt">XCompleteMatchSig</span> pass) <span class="co">-- (NB: only exact-print info here)</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> (<span class="dt">XRec</span> pass [<span class="dt">LIdP</span> pass])</span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> (<span class="dt">Maybe</span> (<span class="dt">LIdP</span> pass))</span></code></pre></div>
<div class="sourceCode" id="cb13"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- In GHC.Types.CompleteMatch</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">CompleteMatch</span> <span class="ot">=</span> <span class="dt">CompleteMatch</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> {<span class="ot"> cmConLikes ::</span> <span class="dt">UniqDSet</span> <span class="dt">ConLike</span>,<span class="ot"> cmResultTyCon ::</span> <span class="dt">Maybe</span> <span class="dt">TyCon</span> }</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">TcGblEnv</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="dt">TcGblEnv</span></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> { <span class="co">-- ...</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> tcg_complete_matches ::</span> <span class="op">!</span><span class="dt">CompleteMatches</span> }</span></code></pre></div>
</div>
</section>
<section id="information-flow" class="slide level2">
<h2>Information flow</h2>
<p>Now let’s look into passing <code>COMPLETE</code> sets to
<code>isIrrefutableHsPat</code>.<br />
Let’s audit its calls; the HLS call hierarchy functionality is very
useful for that.</p>
<ol type="1" class="fragment">
<li>
In the renamer:
<ol type="a">
<li>
<code>stmtTreeToStmts .. (StmtTreeApplicative ..)</code>
</li>
<li>
<code>monadFailOp</code> in <code>rnStmt .. (BindStmt ..)</code> and
<code>rn_rec_stmt .. (BindStmt ..)</code>
</li>
</ol>
</li>
<li>
In the typechecker
<ol type="a">
<li>
When creating a <code>PatSyn</code> in <code>tcPatSynMatcher</code>
(from <code>tcPatSynDecl</code>).
</li>
<li>
Calls to <code>tcMonadFailOp</code> in <code>tcApplicativeStmts</code>,
<code>tcDoStmt</code>, <code>tcMcStmt</code>.
</li>
</ol>
</li>
</ol>
<div class="element: fragment">
This means that we are going to need <code>COMPLETE</code> pattern
information in the renamer, and before we have actually typechecked
<code>PatSyn</code>s in the typechecker.
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="threading-it-through" class="slide level2">
<h2>Threading it through</h2>
<p>Let’s add a field to the typechecker environment (used for both the
typechecker and the renamer):</p>
<div class="sourceCode" id="cb14"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">TcGblEnv</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="dt">TcGblEnv</span> { <span class="co">-- ...</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> tcg_complete_matches ::</span> <span class="op">!</span><span class="dt">CompleteMatches</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="co">{- NEW -}</span> ,<span class="ot"> tcg_complete_matches_rn ::</span> <span class="op">!</span><span class="dt">CompleteMatchesRn</span></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> , <span class="co">-- ...</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a><span class="co">-- NEW</span></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">CompleteMatchesRn</span> <span class="ot">=</span> [<span class="dt">CompleteMatchRn</span>]</span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">CompleteMatchRn</span> <span class="ot">=</span> <span class="dt">CompleteMatchRn</span></span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a> {<span class="ot"> cmConLikesRn ::</span> <span class="dt">NameSet</span> <span class="co">-- ^ The set of `ConLike` values</span></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a> }</span></code></pre></div>
<p class="indicator">
⭲
</p>
</section>
<section id="populating-the-field" class="slide level2">
<h2>Populating the field</h2>
<p>Let’s see then how to thread through the correct information; we need
to ensure that we first rename pattern synonym COMPLETE signatures and
add that information to the <code>TcGblEnv</code> environment before we
call <code>isIrrefutableHsPat</code>.</p>
<p class="indicator">
⭲
</p>
</section>
<section id="call-hierarchy-of-monadfailop" class="slide level2">
<h2>Call hierarchy of <code>monadFailOp</code></h2>
<p>We want to find what to change to pass on the correct
<code>COMPLETE</code> information.<br />
Let’s use HLS to find the call hierarchy of
<code>GHC.Rename.Expr.monadFailOp</code>.</p>
<div class="element: fragment">
<ul>
<li><code>monadFailOp</code></li>
<li><code>rnStmt</code></li>
<li><code>rnStmtsWithFreeVars</code></li>
<li><code>rnExpr</code></li>
<li><code>rnGRHS'</code></li>
<li><code>rnGHRS</code></li>
<li><code>rnGHRSs</code></li>
<li><code>rnBind</code></li>
<li><code>rnLBind</code></li>
<li><code>rnValBindsRHS</code>
<p class="indicator">
⭲
</p></li>
<li><code>rnSrcDecls</code></li>
</ul>
</div>
</section>
<section id="focus-on-rnvalbindsrhs" class="slide level2">
<h2>Focus on <code>rnValBindsRHS</code></h2>
<p><code>rnValBindsRHS</code> is interesting, as this is where we also
rename signatures:</p>
<div class="sourceCode" id="cb15"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="ot">rnValBindsRHS ::</span> <span class="dt">HsSigCtxt</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">-></span> <span class="dt">HsValBindsLR</span> <span class="dt">GhcRn</span> <span class="dt">GhcPs</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">-></span> <span class="dt">RnM</span> (<span class="dt">HsValBinds</span> <span class="dt">GhcRn</span>, <span class="dt">DefUses</span>)</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>rnValBindsRHS ctxt (<span class="dt">ValBinds</span> _ mbinds sigs)</span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="kw">do</span> { (sigs', sig_fvs) <span class="ot"><-</span> renameSigs ctxt sigs</span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a> ; binds_w_dus <span class="ot"><-</span> mapBagM (rnLBind (mkScopedTvFn sigs')) mbinds</span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a> <span class="co">-- ...</span></span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a> }</span></code></pre></div>
<div class="element: fragment">
<p>Here we clearly see that we rename <code>Sig</code>s first (which
includes <code>COMPLETE</code> pragmas), and then rename value bindings.
So we can simply extend the <code>TcGblEnv</code>, e.g.:</p>
<div class="sourceCode" id="cb16"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="kw">do</span> { (sigs', sig_fvs) <span class="ot"><-</span> renameSigs ctxt sigs</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> ; binds_w_dus <span class="ot"><-</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> updGblEnv (add_complete_sigs_rn sigs') <span class="op">$</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> mapBagM (rnLBind (mkScopedTvFn sigs')) mbinds</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a> <span class="co">-- ...</span></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a> }</span></code></pre></div>
</div>
</section>
<section id="typechecker" class="slide level2">
<h2>Typechecker</h2>
<p>Now, this does the trick in the renamer. However, we also need to
make sure to persist the information in
<code>tcg_complete_matches</code>. So we should return the
<code>CompleteMatchesRn</code> at the end of <code>rnValBindsRHS</code>,
and make sure to add them to the final typechecker environment at the
end of <code>rnSrcDecls</code>.</p>
<p class="indicator">
⭲
</p>
</section>
<section id="using-tcg_complete_matches" class="slide level2">
<h2>Using <code>tcg_complete_matches</code></h2>
<p>We then need to look this information up in
<code>isIrrefutableHsPat</code>.</p>
<div class="sourceCode" id="cb17"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="ot">isIrrefutableHsPat ::</span> <span class="dt">IsPass</span> p <span class="ot">=></span> <span class="dt">LPat</span> (<span class="dt">GhcPass</span> p) <span class="ot">-></span> <span class="dt">TcM</span> <span class="dt">Bool</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>isIrrefutableHsPat pat</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="kw">do</span> { strict <span class="ot"><-</span> xoptM <span class="dt">LangExt.Strict</span></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> ; comps <span class="ot"><-</span> get_complete_matches_rn</span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> ; <span class="fu">return</span> <span class="op">$</span> is_irrefutable_pat comps strict pat }</span></code></pre></div>
<div class="element: fragment">
<p>However, we need to make sure we include <strong>all</strong>
complete matches, not just those from the local module.</p>
<p>Let’s find how GHC already does that, for
<code>tcg_complete_matches</code>.</p>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="taking-inspiration" class="slide level2">
<h2>Taking inspiration</h2>
<p>Inspired by <code>mkDsEnvsFromTcGbl</code> and
<code>initDsWithModGuts</code> in <code>GHC.HsToCore.Monad</code>:</p>
<div class="sourceCode" id="cb18"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ot">get_complete_matches_rn ::</span> <span class="dt">TcM</span> <span class="dt">CompleteMatchesRn</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>get_complete_matches_rn</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="kw">do</span> { hsc_env <span class="ot"><-</span> getTopEnv</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> ; tcg_env <span class="ot"><-</span> getGblEnv</span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> ; eps <span class="ot"><-</span> liftIO <span class="op">$</span> hscEPS hsc_env</span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a> ; <span class="fu">return</span> <span class="op">$</span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a> completeMatchesRn</span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a> ( hptCompleteSigs hsc_env <span class="co">-- from the home package</span></span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a> <span class="op">++</span> eps_complete_matches eps ) <span class="co">-- from imports</span></span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a> <span class="op">++</span> tcg_complete_matches_rn tcg_env <span class="co">-- from the current module</span></span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a> }</span></code></pre></div>
<code>completeMatchesRn</code> throws away the <code>COMPLETE</code>
pragmas with a result <code>TyCon</code>, as in the renamer we can’t
know whether they apply or not.
<p class="indicator">
⭲
</p>
</section>
<section id="updating-isirrefutablehspat" class="slide level2">
<h2>Updating <code>isIrrefutableHsPat</code></h2>
<p>In the worker function <code>is_irrefutable_pat</code> we can then
make use of this information, in the <code>ConPat</code> case for a
<code>PatSyn</code>. We want to check whether the <code>PatSyn</code>
<code>Name</code> is the single member of one of the
<code>COMPLETE</code> sets.</p>
<div class="element: fragment">
<div class="sourceCode" id="cb19"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>is_irrefutable_hs_pat complete_matches strict_enabled <span class="ot">=</span> \<span class="kw">case</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="co">-- ...</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">ConPat</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> { pat_con_ext <span class="ot">=</span> ext</span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> , pat_con <span class="ot">=</span> con</span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> , pat_args <span class="ot">=</span> details } <span class="ot">-></span></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a> con_irref <span class="op">&&</span> <span class="fu">all</span> go (hsConPatArgs details)</span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span></span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a> con_irref <span class="ot">=</span> <span class="kw">case</span> ghcPass <span class="op">@</span>p <span class="kw">of</span></span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a> <span class="dt">GhcPs</span> <span class="ot">-></span> <span class="dt">False</span> <span class="co">-- Conservative</span></span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a> <span class="dt">GhcRn</span> <span class="ot">-></span> <span class="kw">case</span> ext <span class="kw">of</span></span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a> <span class="dt">ConIsData</span> { conIsSingleDataCon <span class="ot">=</span> irref } <span class="ot">-></span> irref</span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a> <span class="dt">ConIsPatSyn</span> <span class="ot">-></span> <span class="fu">any</span> (single_match con) complete_matches</span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a> <span class="dt">GhcTc</span> <span class="ot">-></span> <span class="co">-- ...</span></span></code></pre></div>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="outcome" class="slide level2">
<h2>Outcome</h2>
<p>Now that everything is in place, we try some tests programs.</p>
<div class="element: fragment">
<div class="sourceCode" id="cb20"><pre
class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>panic! (the 'impossible' happened)</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> GHC version 9.7.20230607:</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> missing fail op</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> Pattern match: U is failable, and fail_expr was left unset</span></code></pre></div>
</div>
<div class="element: fragment">
<p>Debug this by working backwards: search for “missing fail op”.</p>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="dshandlemonadicfailure" class="slide level2">
<h2><code>dsHandleMonadicFailure</code></h2>
<div class="sourceCode" id="cb21"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>dsHandleMonadicFailure ctx pat match m_fail_op <span class="ot">=</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> shareFailureHandler match <span class="kw">of</span></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">MR_Infallible</span> body <span class="ot">-></span> body</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">MR_Fallible</span> body <span class="ot">-></span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> m_fail_op <span class="kw">of</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a><span class="co">-- Note that (non-monadic) list comprehension, pattern guards, etc could</span></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a><span class="co">-- have fallible bindings without an explicit failure op, but this is</span></span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a><span class="co">-- handled elsewhere. See Note [Failing pattern matches in Stmts] the</span></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- breakdown of regular and special binds.</span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a> <span class="dt">Nothing</span> <span class="ot">-></span> pprPanic <span class="st">"missing fail op"</span> <span class="op">$</span></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a> text <span class="st">"Pattern match:"</span> <span class="op"><+></span> ppr pat <span class="op"><+></span></span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a> text <span class="st">"is failable, and fail_expr was left unset"</span></span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a> <span class="dt">Just</span> fail_op <span class="ot">-></span> <span class="co">-- ...</span></span></code></pre></div>
<div class="element: fragment">
Where are we constructing <code>MR_Fallible</code>?
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="mkcosyncasematchresult" class="slide level2">
<h2><code>mkCoSynCaseMatchResult</code></h2>
<p><code>mkCoSynCaseMatchResult</code> called in
<code>matchPatSyn</code> looks relevant!</p>
<div class="sourceCode" id="cb22"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mkCoSynCaseMatchResult ::</span> <span class="dt">Id</span> <span class="ot">-></span> <span class="dt">Type</span> <span class="ot">-></span> <span class="dt">CaseAlt</span> <span class="dt">PatSyn</span> <span class="ot">-></span> <span class="dt">MatchResult</span> <span class="dt">CoreExpr</span></span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>mkCoSynCaseMatchResult var ty alt <span class="ot">=</span> <span class="dt">MR_Fallible</span> <span class="op">$</span> mkPatSynCase var ty alt</span></code></pre></div>
<div class="element: fragment">
<p>Problem: the pattern synonym matcher always returns a fallible match
result. This is actually correct in general, e.g.:</p>
<div class="sourceCode" id="cb23"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="kw">pattern</span> <span class="dt">Bogus</span><span class="ot"> ::</span> <span class="dt">Int</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="kw">pattern</span> <span class="dt">Bogus</span> <span class="ot">=</span> <span class="dv">3</span></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# COMPLETE Bogus #-}</span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a><span class="ot">bogus ::</span> <span class="dt">Identity</span> <span class="dt">Bool</span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a>bogus <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">Bogus</span> <span class="ot"><-</span> <span class="fu">return</span> <span class="dv">4</span></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span> <span class="dt">False</span></span></code></pre></div>
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="handling-monadic-failure" class="slide level2">
<h2>Handling monadic failure</h2>
<p>We should accept the previous program, but crash at runtime, using a
non-monadic failure operator which raises a pattern match error:</p>
<div class="sourceCode" id="cb24"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>dsHandleMonadicFailure ctx pat match m_fail_op body_ty <span class="ot">=</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> shareFailureHandler match <span class="kw">of</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">MR_Infallible</span> body <span class="ot">-></span> body</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">MR_Fallible</span> body <span class="ot">-></span> <span class="kw">do</span></span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> m_fail_op <span class="kw">of</span></span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">Nothing</span> <span class="ot">-></span></span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">do</span> error_expr <span class="ot"><-</span> mkErrorAppDs pAT_ERROR_ID body_ty (ppr pat)</span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a> body error_expr</span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a> <span class="dt">Just</span> fail_op <span class="ot">-></span> <span class="co">-- ...</span></span></code></pre></div>
Note that we need to know the overall return type of the <code>do</code>
expression to know the return type of the error; so we modify
<code>dsHandleMonadicFailure</code>.
<p class="indicator">
⭲
</p>
</section>
<section id="confirmation" class="slide level2">
<h2>Confirmation</h2>
<p>Let’s make sure we properly handle the problematic program:</p>
<div class="sourceCode" id="cb25"><pre
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="kw">pattern</span> <span class="dt">Bogus</span><span class="ot"> ::</span> <span class="dt">Int</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="kw">pattern</span> <span class="dt">Bogus</span> <span class="ot">=</span> <span class="dv">3</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# COMPLETE Bogus #-}</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="ot">bogus ::</span> <span class="dt">Identity</span> <span class="dt">Bool</span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a>bogus <span class="ot">=</span> <span class="kw">do</span></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">Bogus</span> <span class="ot"><-</span> <span class="fu">return</span> <span class="dv">4</span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span> <span class="dt">False</span></span></code></pre></div>
<div class="element: fragment">
<p>The program compiles successfully, and running it gives:</p>
<div class="sourceCode" id="cb26"><pre
class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>*** Exception: user error (Pattern match failure in 'do' block)</span></code></pre></div>
</div>
<div class="element: fragment">
We can do better by having a custom error message for this situation, as
opposed to re-using <code>pAT_ERROR_ID</code>.
<p class="indicator">
⭲
</p>
</div>
</section>
<section id="finishing-up" class="slide level2">
<h2>Finishing up</h2>
<ul>
<li>Tests.</li>
<li>Notes.</li>
</ul>
<p class="indicator">
⭲
</p>
</section>
<section id="other-tickets" class="slide level2">
<h2>Other tickets</h2>
<ul>
<li><p>Simple error message improvements:</p>
<ul>
<li>Error messages for instances of a non-class <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/22688">#22688</a> and
<a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/23462">#23462</a>.</li>
<li>Improve import suggestions <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/20771">#20771</a>.</li>
</ul></li>
<li><p>Dependency order with view patterns <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/14293">#14293</a> <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/22406">#22406</a>.</p></li>
<li><p>Refactoring of rebindable syntax; see <a
href="https://gitlab.haskell.org/ghc/ghc/-/wikis/Rebindable-syntax">wiki
page</a>.</p></li>
<li><p>Propagate more information about patterns in the renamer to help
record update disambiguation <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/23032">#23032</a> <a
href="https://gitlab.haskell.org/ghc/ghc/-/issues/22746">#22746</a>.</p></li>
</ul>
</section>
</div>
</div>
<script src="reveal.js/dist/reveal.js"></script>
<!-- reveal.js plugins -->
<script src="reveal.js/plugin/notes/notes.js"></script>
<script src="reveal.js/plugin/search/search.js"></script>
<script src="reveal.js/plugin/zoom/zoom.js"></script>
<script>
// Full list of configuration options available at:
// https://revealjs.com/config/
Reveal.initialize({
// Display controls in the bottom right corner
controls: true,
// Help the user learn the controls by providing hints, for example by
// bouncing the down arrow when they first encounter a vertical slide
controlsTutorial: false,
// Determines where controls appear, "edges" or "bottom-right"
controlsLayout: 'edges',
// Visibility rule for backwards navigation arrows; "faded", "hidden"
// or "visible"
controlsBackArrows: 'faded',
// Display a presentation progress bar
progress: true,
// Display the page number of the current slide
slideNumber: false,
// 'all', 'print', or 'speaker'
showSlideNumber: 'all',
// Add the current slide number to the URL hash so that reloading the
// page/copying the URL will return you to the same slide
hash: true,
// Start with 1 for the hash rather than 0
hashOneBasedIndex: false,
// Flags if we should monitor the hash and change slides accordingly
respondToHashChanges: true,
// Push each slide change to the browser history
history: false,
// Enable keyboard shortcuts for navigation
keyboard: true,
// Enable the slide overview mode
overview: true,
// Disables the default reveal.js slide layout (scaling and centering)
// so that you can use custom CSS layout
disableLayout: false,
// Vertical centering of slides
center: true,
// Enables touch navigation on devices with touch input
touch: true,
// Loop the presentation
loop: false,
// Change the presentation direction to be RTL
rtl: false,
// see https://revealjs.com/vertical-slides/#navigation-mode
navigationMode: 'default',
// Randomizes the order of slides each time the presentation loads
shuffle: false,
// Turns fragments on and off globally
fragments: true,
// Flags whether to include the current fragment in the URL,
// so that reloading brings you to the same fragment position
fragmentInURL: true,
// Flags if the presentation is running in an embedded mode,
// i.e. contained within a limited portion of the screen
embedded: false,
// Flags if we should show a help overlay when the questionmark
// key is pressed
help: true,
// Flags if it should be possible to pause the presentation (blackout)
pause: true,
// Flags if speaker notes should be visible to all viewers
showNotes: false,
// Global override for autoplaying embedded media (null/true/false)
autoPlayMedia: null,
// Global override for preloading lazy-loaded iframes (null/true/false)
preloadIframes: null,
// Number of milliseconds between automatically proceeding to the
// next slide, disabled when set to 0, this value can be overwritten
// by using a data-autoslide attribute on your slides
autoSlide: 0,
// Stop auto-sliding after user input
autoSlideStoppable: true,
// Use this method for navigation when auto-sliding
autoSlideMethod: null,
// Specify the average time in seconds that you think you will spend
// presenting each slide. This is used to show a pacing timer in the
// speaker view
defaultTiming: null,
// Enable slide navigation via mouse wheel
mouseWheel: false,
// The display mode that will be used to show slides
display: 'block',
// Hide cursor if inactive
hideInactiveCursor: true,
// Time before the cursor is hidden (in ms)
hideCursorTime: 5000,
// Opens links in an iframe preview overlay
previewLinks: false,
// Transition style (none/fade/slide/convex/concave/zoom)
transition: 'slide',
// Transition speed (default/fast/slow)
transitionSpeed: 'default',
// Transition style for full page slide backgrounds
// (none/fade/slide/convex/concave/zoom)
backgroundTransition: 'fade',
// Number of slides away from the current that are visible
viewDistance: 3,
// Number of slides away from the current that are visible on mobile
// devices. It is advisable to set this to a lower number than
// viewDistance in order to save resources.
mobileViewDistance: 2,
// reveal.js plugins
plugins: [
RevealNotes,
RevealSearch,
RevealZoom
]
});
</script>
</body>
</html>