-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stlc.html
1574 lines (1194 loc) · 114 KB
/
Stlc.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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>Stlc: The Simply Typed Lambda-Calculus</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">Stlc<span class="subtitle">The Simply Typed Lambda-Calculus</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="keyword">Types</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab656"></a><h1 class="section">The Simply Typed Lambda-Calculus</h1>
<div class="paragraph"> </div>
The simply typed lambda-calculus (STLC) is a tiny core calculus
embodying the key concept of <i>functional abstraction</i>, which shows
up in pretty much every real-world programming language in some
form (functions, procedures, methods, etc.).
<div class="paragraph"> </div>
We will follow exactly the same pattern as in the previous
chapter when formalizing this calculus (syntax, small-step
semantics, typing rules) and its main properties (progress and
preservation). The new technical challenges (which will take some
work to deal with) all arise from the mechanisms of <i>variable
binding</i> and <i>substitution</i>.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab657"></a><h2 class="section">Overview</h2>
<div class="paragraph"> </div>
The STLC is built on some collection of <i>base types</i> — booleans,
numbers, strings, etc. The exact choice of base types doesn't
matter — the construction of the language and its theoretical
properties work out pretty much the same — so for the sake of
brevity let's take just <span class="inlinecode"><span class="id" type="var">Bool</span></span> for the moment. At the end of the
chapter we'll see how to add more base types, and in later
chapters we'll enrich the pure STLC with other useful constructs
like pairs, records, subtyping, and mutable state.
<div class="paragraph"> </div>
Starting from the booleans, we add three things:
<div class="paragraph"> </div>
<ul class="doclist">
<li> variables
</li>
<li> function abstractions
</li>
<li> application
</li>
</ul>
<div class="paragraph"> </div>
This gives us the following collection of abstract syntax
constructors (written out here in informal BNF notation — we'll
formalize it below).
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Informal concrete syntax:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">t</span> ::= <span class="id" type="var">x</span> <span class="id" type="var">variable</span><br/>
| \<span class="id" type="var">x</span>:<span class="id" type="var">T<sub>1</sub>.t<sub>2</sub></span> <span class="id" type="var">abstraction</span><br/>
| <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">application</span><br/>
| <span class="id" type="var">true</span> <span class="id" type="var">constant</span> <span class="id" type="var">true</span><br/>
| <span class="id" type="var">false</span> <span class="id" type="var">constant</span> <span class="id" type="var">false</span><br/>
| <span class="id" type="keyword">if</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="keyword">then</span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="keyword">else</span> <span class="id" type="var">t<sub>3</sub></span> <span class="id" type="var">conditional</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
The <span class="inlinecode">\</span> symbol (backslash, in ascii) in a function abstraction
<span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">T<sub>1</sub>.t<sub>2</sub></span></span> is generally written as a greek letter "lambda" (hence
the name of the calculus). The variable <span class="inlinecode"><span class="id" type="var">x</span></span> is called the
<i>parameter</i> to the function; the term <span class="inlinecode"><span class="id" type="var">t<sub>2</sub></span></span> is its <i>body</i>. The
annotation <span class="inlinecode">:<span class="id" type="var">T</span></span> specifies the type of arguments that the function
can be applied to.
<div class="paragraph"> </div>
Some examples:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
<div class="paragraph"> </div>
The identity function for booleans.
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> <span class="inlinecode"><span class="id" type="var">true</span></span>
<div class="paragraph"> </div>
The identity function for booleans, applied to the boolean <span class="inlinecode"><span class="id" type="var">true</span></span>.
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">false</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span>
<div class="paragraph"> </div>
The boolean "not" function.
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">true</span></span>
<div class="paragraph"> </div>
The constant function that takes every (boolean) argument to
<span class="inlinecode"><span class="id" type="var">true</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
<div class="paragraph"> </div>
A two-argument function that takes two booleans and returns
the first one. (Note that, as in Coq, a two-argument function
is really a one-argument function whose body is also a
one-argument function.)
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> <span class="inlinecode"><span class="id" type="var">false</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span>
<div class="paragraph"> </div>
A two-argument function that takes two booleans and returns
the first one, applied to the booleans <span class="inlinecode"><span class="id" type="var">false</span></span> and <span class="inlinecode"><span class="id" type="var">true</span></span>.
<div class="paragraph"> </div>
Note that, as in Coq, application associates to the left —
i.e., this expression is parsed as <span class="inlinecode">((\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span>
<span class="inlinecode"><span class="id" type="var">false</span>)</span> <span class="inlinecode"><span class="id" type="var">true</span></span>.
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">\<span class="id" type="var">f</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">f</span></span> <span class="inlinecode">(<span class="id" type="var">f</span></span> <span class="inlinecode"><span class="id" type="var">true</span>)</span>
<div class="paragraph"> </div>
A higher-order function that takes a <i>function</i> <span class="inlinecode"><span class="id" type="var">f</span></span> (from
booleans to booleans) as an argument, applies <span class="inlinecode"><span class="id" type="var">f</span></span> to <span class="inlinecode"><span class="id" type="var">true</span></span>,
and applies <span class="inlinecode"><span class="id" type="var">f</span></span> again to the result.
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(\<span class="id" type="var">f</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">f</span></span> <span class="inlinecode">(<span class="id" type="var">f</span></span> <span class="inlinecode"><span class="id" type="var">true</span>))</span> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">false</span>)</span>
<div class="paragraph"> </div>
The same higher-order function, applied to the constantly
<span class="inlinecode"><span class="id" type="var">false</span></span> function.
</li>
</ul>
<div class="paragraph"> </div>
As the last several examples show, the STLC is a language of
<i>higher-order</i> functions: we can write down functions that take
other functions as arguments and/or return other functions as
results.
<div class="paragraph"> </div>
Another point to note is that the STLC doesn't provide any
primitive syntax for defining <i>named</i> functions — all functions
are "anonymous." We'll see in chapter <span class="inlinecode"><span class="id" type="var">MoreStlc</span></span> that it is easy
to add named functions to what we've got — indeed, the
fundamental naming and binding mechanisms are exactly the same.
<div class="paragraph"> </div>
The <i>types</i> of the STLC include <span class="inlinecode"><span class="id" type="var">Bool</span></span>, which classifies the
boolean constants <span class="inlinecode"><span class="id" type="var">true</span></span> and <span class="inlinecode"><span class="id" type="var">false</span></span> as well as more complex
computations that yield booleans, plus <i>arrow types</i> that classify
functions.
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">T</span> ::= <span class="id" type="var">Bool</span><br/>
| <span class="id" type="var">T<sub>1</sub></span> <span style="font-family: arial;">→</span> <span class="id" type="var">T<sub>2</sub></span>
<div class="paragraph"> </div>
</div>
For example:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">false</span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> <span class="inlinecode"><span class="id" type="var">true</span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span></span> (i.e. <span class="inlinecode"><span class="id" type="var">Bool</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode">(<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>)</span>)
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> <span class="inlinecode"><span class="id" type="var">false</span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> <span class="inlinecode"><span class="id" type="var">false</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> has type <span class="inlinecode"><span class="id" type="var">Bool</span></span>
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab658"></a><h2 class="section">Syntax</h2>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">STLC</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab659"></a><h3 class="section">Types</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ty</span> : <span class="id" type="keyword">Type</span> := <br/>
| <span class="id" type="var">TBool</span> : <span class="id" type="var">ty</span> <br/>
| <span class="id" type="var">TArrow</span> : <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab660"></a><h3 class="section">Terms</h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">tm</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">tvar</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tapp</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tabs</span> : <span class="id" type="var">id</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ty</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">ttrue</span> : <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tfalse</span> : <span class="id" type="var">tm</span><br/>
| <span class="id" type="var">tif</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span>.<br/>
<br/>
</div>
<div class="doc">
Note that an abstraction <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">T.t</span></span> (formally, <span class="inlinecode"><span class="id" type="var">tabs</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span>) is
always annotated with the type <span class="inlinecode"><span class="id" type="var">T</span></span> of its parameter, in contrast
to Coq (and other functional languages like ML, Haskell, etc.),
which use <i>type inference</i> to fill in missing annotations. We're
not considering type inference here, to keep things simple.
<div class="paragraph"> </div>
Some examples...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">x</span> := (<span class="id" type="var">Id</span> 0).<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">y</span> := (<span class="id" type="var">Id</span> 1).<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">z</span> := (<span class="id" type="var">Id</span> 2).<br/>
<div class="togglescript" id="proofcontrol1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')"><span class="show"></span></div>
<div class="proofscript" id="proof1" onclick="toggleDisplay('proof1');toggleDisplay('proofcontrol1')">
<span class="id" type="keyword">Hint</span> <span class="id" type="keyword">Unfold</span> <span class="id" type="var">x</span>.<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="keyword">Unfold</span> <span class="id" type="var">y</span>.<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="keyword">Unfold</span> <span class="id" type="var">z</span>.<br/>
</div>
<br/>
</div>
<div class="doc">
<span class="inlinecode"><span class="id" type="var">idB</span></span> <span class="inlinecode">=</span> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">idB</span> := <br/>
(<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">TBool</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>)).<br/>
<br/>
</div>
<div class="doc">
<span class="inlinecode"><span class="id" type="var">idBB</span></span> <span class="inlinecode">=</span> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">idBB</span> := <br/>
(<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> (<span class="id" type="var">TArrow</span> <span class="id" type="var">TBool</span> <span class="id" type="var">TBool</span>) (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>)).<br/>
<br/>
</div>
<div class="doc">
<span class="inlinecode"><span class="id" type="var">idBBBB</span></span> <span class="inlinecode">=</span> <span class="inlinecode">\<span class="id" type="var">x</span>:(<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>)</span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode">(<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>).</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">idBBBB</span> :=<br/>
(<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> (<span class="id" type="var">TArrow</span> (<span class="id" type="var">TArrow</span> <span class="id" type="var">TBool</span> <span class="id" type="var">TBool</span>) <br/>
(<span class="id" type="var">TArrow</span> <span class="id" type="var">TBool</span> <span class="id" type="var">TBool</span>)) <br/>
(<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>)).<br/>
<br/>
</div>
<div class="doc">
<span class="inlinecode"><span class="id" type="var">k</span></span> <span class="inlinecode">=</span> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">k</span> := (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">TBool</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">y</span> <span class="id" type="var">TBool</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>))).<br/>
<br/>
</div>
<div class="doc">
<span class="inlinecode"><span class="id" type="var">notB</span></span> <span class="inlinecode">=</span> <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">false</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">notB</span> := (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">TBool</span> (<span class="id" type="var">tif</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>) <span class="id" type="var">tfalse</span> <span class="id" type="var">ttrue</span>)).<br/>
<br/>
</div>
<div class="doc">
(We write these as <span class="inlinecode"><span class="id" type="keyword">Notation</span></span>s rather than <span class="inlinecode"><span class="id" type="keyword">Definition</span></span>s to make
things easier for <span class="inlinecode"><span class="id" type="tactic">auto</span></span>.)
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab661"></a><h2 class="section">Operational Semantics</h2>
<div class="paragraph"> </div>
To define the small-step semantics of STLC terms, we begin — as
always — by defining the set of values. Next, we define the
critical notions of <i>free variables</i> and <i>substitution</i>, which are
used in the reduction rule for application expressions. And
finally we give the small-step relation itself.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab662"></a><h3 class="section">Values</h3>
<div class="paragraph"> </div>
To define the values of the STLC, we have a few cases to consider.
<div class="paragraph"> </div>
First, for the boolean part of the language, the situation is
clear: <span class="inlinecode"><span class="id" type="var">true</span></span> and <span class="inlinecode"><span class="id" type="var">false</span></span> are the only values. An <span class="inlinecode"><span class="id" type="keyword">if</span></span>
expression is never a value.
<div class="paragraph"> </div>
Second, an application is clearly not a value: It represents a
function being invoked on some argument, which clearly still has
work left to do.
<div class="paragraph"> </div>
Third, for abstractions, we have a choice:
<div class="paragraph"> </div>
<ul class="doclist">
<li> We can say that <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">T.t<sub>1</sub></span></span> is a value only when <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is a
value — i.e., only if the function's body has been
reduced (as much as it can be without knowing what argument it
is going to be applied to).
<div class="paragraph"> </div>
</li>
<li> Or we can say that <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">T.t<sub>1</sub></span></span> is always a value, no matter
whether <span class="inlinecode"><span class="id" type="var">t<sub>1</sub></span></span> is one or not — in other words, we can say that
reduction stops at abstractions.
</li>
</ul>
<div class="paragraph"> </div>
Coq, in its built-in functional programming langauge, makes the
first choice — for example,
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">Compute</span> (<span class="id" type="keyword">fun</span> <span class="id" type="var">x</span>:<span class="id" type="var">bool</span> ⇒ 3 + 4)
<div class="paragraph"> </div>
</div>
yields <span class="inlinecode"><span class="id" type="keyword">fun</span></span> <span class="inlinecode"><span class="id" type="var">x</span>:<span class="id" type="var">bool</span></span> <span class="inlinecode">⇒</span> <span class="inlinecode">7</span>.
<div class="paragraph"> </div>
Most real-world functional programming languages make the second
choice — reduction of a function's body only begins when the
function is actually applied to an argument. We also make the
second choice here.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">value</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">v_abs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">T</span> <span class="id" type="var">t</span>,<br/>
<span class="id" type="var">value</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T</span> <span class="id" type="var">t</span>)<br/>
| <span class="id" type="var">v_true</span> : <br/>
<span class="id" type="var">value</span> <span class="id" type="var">ttrue</span><br/>
| <span class="id" type="var">v_false</span> : <br/>
<span class="id" type="var">value</span> <span class="id" type="var">tfalse</span>.<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">value</span>.<br/>
<br/>
</div>
<div class="doc">
Finally, we must consider what constitutes a <i>complete</i> program.
<div class="paragraph"> </div>
Intuitively, a "complete" program must not refer to any undefined
variables. We'll see shortly how to define the "free" variables
in a STLC term. A program is "closed", that is, it contains no
free variables.
<div class="paragraph"> </div>
<div class="paragraph"> </div>
Having made the choice not to reduce under abstractions,
we don't need to worry about whether variables are values, since
we'll always be reducing programs "from the outside in," and that
means the <span class="inlinecode"><span class="id" type="var">step</span></span> relation will always be working with closed
terms (ones with no free variables).
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab663"></a><h3 class="section">Substitution</h3>
<div class="paragraph"> </div>
Now we come to the heart of the STLC: the operation of
substituting one term for a variable in another term.
<div class="paragraph"> </div>
This operation will be used below to define the operational
semantics of function application, where we will need to
substitute the argument term for the function parameter in the
function's body. For example, we reduce
<div class="paragraph"> </div>
<div class="code code-tight">
(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>. <span class="id" type="keyword">if</span> <span class="id" type="var">x</span> <span class="id" type="keyword">then</span> <span class="id" type="var">true</span> <span class="id" type="keyword">else</span> <span class="id" type="var">x</span>) <span class="id" type="var">false</span>
<div class="paragraph"> </div>
</div>
to
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">if</span> <span class="id" type="var">false</span> <span class="id" type="keyword">then</span> <span class="id" type="var">true</span> <span class="id" type="keyword">else</span> <span class="id" type="var">false</span>
<div class="paragraph"> </div>
</div>
by substituting <span class="inlinecode"><span class="id" type="var">false</span></span> for the parameter <span class="inlinecode"><span class="id" type="var">x</span></span> in the body of the
function.
<div class="paragraph"> </div>
In general, we need to be able to substitute some given
term <span class="inlinecode"><span class="id" type="var">s</span></span> for occurrences of some variable <span class="inlinecode"><span class="id" type="var">x</span></span> in another term <span class="inlinecode"><span class="id" type="var">t</span></span>.
In informal discussions, this is usually written <span class="inlinecode"></span> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t</span></span> <span class="inlinecode"></span> and
pronounced "substitute <span class="inlinecode"><span class="id" type="var">x</span></span> with <span class="inlinecode"><span class="id" type="var">s</span></span> in <span class="inlinecode"><span class="id" type="var">t</span></span>."
<div class="paragraph"> </div>
Here are some examples:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode">(<span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">false</span>)</span> yields <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">false</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode"><span class="id" type="var">x</span></span> yields <span class="inlinecode"><span class="id" type="var">true</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode">(<span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">y</span>)</span> yields <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode"><span class="id" type="var">y</span></span> yields <span class="inlinecode"><span class="id" type="var">y</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode"><span class="id" type="var">false</span></span> yields <span class="inlinecode"><span class="id" type="var">false</span></span> (vacuous substitution)
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode">(\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">false</span>)</span> yields <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="keyword">if</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode"><span class="id" type="keyword">then</span></span> <span class="inlinecode"><span class="id" type="var">true</span></span> <span class="inlinecode"><span class="id" type="keyword">else</span></span> <span class="inlinecode"><span class="id" type="var">false</span></span>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode">(\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> yields <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">true</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode">(\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">y</span>)</span> yields <span class="inlinecode">\<span class="id" type="var">y</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">y</span></span>
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode">[<span class="id" type="var">x</span>:=<span class="id" type="var">true</span>]</span> <span class="inlinecode">(\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span>)</span> yields <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>
</li>
</ul>
<div class="paragraph"> </div>
The last example is very important: substituting <span class="inlinecode"><span class="id" type="var">x</span></span> with <span class="inlinecode"><span class="id" type="var">true</span></span> in
<span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> does <i>not</i> yield <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">true</span></span>! The reason for
this is that the <span class="inlinecode"><span class="id" type="var">x</span></span> in the body of <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span> is <i>bound</i> by the
abstraction: it is a new, local name that just happens to be
spelled the same as some global name <span class="inlinecode"><span class="id" type="var">x</span></span>.
<div class="paragraph"> </div>
Here is the definition, informally...
<div class="paragraph"> </div>
<div class="code code-tight">
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">x</span> = <span class="id" type="var">s</span><br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">y</span> = <span class="id" type="var">y</span> <span class="id" type="keyword">if</span> <span class="id" type="var">x</span> ≠ <span class="id" type="var">y</span><br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>](\<span class="id" type="var">x</span>:<span class="id" type="var">T<sub>11</sub>.t<sub>12</sub></span>) = \<span class="id" type="var">x</span>:<span class="id" type="var">T<sub>11</sub></span>. <span class="id" type="var">t<sub>12</sub></span> <br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>](\<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub>.t<sub>12</sub></span>) = \<span class="id" type="var">y</span>:<span class="id" type="var">T<sub>11</sub></span>. [<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t<sub>12</sub></span> <span class="id" type="keyword">if</span> <span class="id" type="var">x</span> ≠ <span class="id" type="var">y</span><br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>](<span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>) = ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t<sub>1</sub></span>) ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t<sub>2</sub></span>) <br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">true</span> = <span class="id" type="var">true</span><br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">false</span> = <span class="id" type="var">false</span><br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>](<span class="id" type="keyword">if</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="keyword">then</span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="keyword">else</span> <span class="id" type="var">t<sub>3</sub></span>) = <br/>
<span class="id" type="keyword">if</span> [<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t<sub>1</sub></span> <span class="id" type="keyword">then</span> [<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t<sub>2</sub></span> <span class="id" type="keyword">else</span> [<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t<sub>3</sub></span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
... and formally:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> "'[' x ':=' s ']' t" (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 20).<br/>
<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="tactic">subst</span> (<span class="id" type="var">x</span>:<span class="id" type="var">id</span>) (<span class="id" type="var">s</span>:<span class="id" type="var">tm</span>) (<span class="id" type="var">t</span>:<span class="id" type="var">tm</span>) : <span class="id" type="var">tm</span> :=<br/>
<span class="id" type="keyword">match</span> <span class="id" type="var">t</span> <span class="id" type="keyword">with</span><br/>
| <span class="id" type="var">tvar</span> <span class="id" type="var">x'</span> ⇒ <br/>
<span class="id" type="keyword">if</span> <span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">x'</span> <span class="id" type="keyword">then</span> <span class="id" type="var">s</span> <span class="id" type="keyword">else</span> <span class="id" type="var">t</span><br/>
| <span class="id" type="var">tabs</span> <span class="id" type="var">x'</span> <span class="id" type="var">T</span> <span class="id" type="var">t<sub>1</sub></span> ⇒ <br/>
<span class="id" type="var">tabs</span> <span class="id" type="var">x'</span> <span class="id" type="var">T</span> (<span class="id" type="keyword">if</span> <span class="id" type="var">eq_id_dec</span> <span class="id" type="var">x</span> <span class="id" type="var">x'</span> <span class="id" type="keyword">then</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="keyword">else</span> ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>] <span class="id" type="var">t<sub>1</sub></span>)) <br/>
| <span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> ⇒ <br/>
<span class="id" type="var">tapp</span> ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>] <span class="id" type="var">t<sub>1</sub></span>) ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>] <span class="id" type="var">t<sub>2</sub></span>)<br/>
| <span class="id" type="var">ttrue</span> ⇒ <br/>
<span class="id" type="var">ttrue</span><br/>
| <span class="id" type="var">tfalse</span> ⇒ <br/>
<span class="id" type="var">tfalse</span><br/>
| <span class="id" type="var">tif</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>3</sub></span> ⇒ <br/>
<span class="id" type="var">tif</span> ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>] <span class="id" type="var">t<sub>1</sub></span>) ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>] <span class="id" type="var">t<sub>2</sub></span>) ([<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>] <span class="id" type="var">t<sub>3</sub></span>)<br/>
<span class="id" type="keyword">end</span><br/>
<br/>
<span class="id" type="keyword">where</span> "'[' x ':=' s ']' t" := (<span class="id" type="tactic">subst</span> <span class="id" type="var">x</span> <span class="id" type="var">s</span> <span class="id" type="var">t</span>).<br/>
<br/>
</div>
<div class="doc">
<i>Technical note</i>: Substitution becomes trickier to define if we
consider the case where <span class="inlinecode"><span class="id" type="var">s</span></span>, the term being substituted for a
variable in some other term, may itself contain free variables.
Since we are only interested here in defining the <span class="inlinecode"><span class="id" type="var">step</span></span> relation
on closed terms (i.e., terms like <span class="inlinecode">\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>.</span> <span class="inlinecode"><span class="id" type="var">x</span></span>, that do not mention
variables are not bound by some enclosing lambda), we can skip
this extra complexity here, but it must be dealt with when
formalizing richer languages.
<div class="paragraph"> </div>
<a name="lab664"></a><h3 class="section"> </h3>
<a name="lab665"></a><h4 class="section">Exercise: 3 stars (substi)</h4>
<div class="paragraph"> </div>
The definition that we gave above uses Coq's <span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span> facility
to define substitution as a <i>function</i>. Suppose, instead, we
wanted to define substitution as an inductive <i>relation</i> <span class="inlinecode"><span class="id" type="var">substi</span></span>.
We've begun the definition by providing the <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> header and
one of the constructors; your job is to fill in the rest of the
constructors.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">substi</span> (<span class="id" type="var">s</span>:<span class="id" type="var">tm</span>) (<span class="id" type="var">x</span>:<span class="id" type="var">id</span>) : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> := <br/>
| <span class="id" type="var">s_var1</span> : <br/>
<span class="id" type="var">substi</span> <span class="id" type="var">s</span> <span class="id" type="var">x</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>) <span class="id" type="var">s</span><br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
.<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">substi</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">substi_correct</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">s</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span>,<br/>
[<span class="id" type="var">x</span>:=<span class="id" type="var">s</span>]<span class="id" type="var">t</span> = <span class="id" type="var">t'</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">substi</span> <span class="id" type="var">s</span> <span class="id" type="var">x</span> <span class="id" type="var">t</span> <span class="id" type="var">t'</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab666"></a><h3 class="section">Reduction</h3>
<div class="paragraph"> </div>
The small-step reduction relation for STLC now follows the same
pattern as the ones we have seen before. Intuitively, to reduce a
function application, we first reduce its left-hand side until it
becomes a literal function; then we reduce its right-hand
side (the argument) until it is also a value; and finally we
substitute the argument for the bound variable in the body of the
function. This last rule, written informally as
<div class="paragraph"> </div>
<div class="code code-tight">
(\<span class="id" type="var">x</span>:<span class="id" type="var">T.t<sub>12</sub></span>) <span class="id" type="var">v<sub>2</sub></span> <span style="font-family: arial;">⇒</span> [<span class="id" type="var">x</span>:=<span class="id" type="var">v<sub>2</sub></span>]<span class="id" type="var">t<sub>12</sub></span>
<div class="paragraph"> </div>
</div>
is traditionally called "beta-reduction".
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">value v<sub>2</sub></td>
<td class="infrulenamecol" rowspan="3">
(ST_AppAbs)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(\x:T.t<sub>12</sub>) v<sub>2</sub> <span style="font-family: arial;">⇒</span> [x:=v<sub>2</sub>]t<sub>12</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_App1)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> t<sub>2</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>' t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">value v<sub>1</sub></td>
<td></td>
</td>
<tr class="infruleassumption">
<td class="infrule">t<sub>2</sub> <span style="font-family: arial;">⇒</span> t<sub>2</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_App2)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">v<sub>1</sub> t<sub>2</sub> <span style="font-family: arial;">⇒</span> v<sub>1</sub> t<sub>2</sub>'</td>
<td></td>
</td>
</table></center> ... plus the usual rules for booleans:
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_IfTrue)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(if true then t<sub>1</sub> else t<sub>2</sub>) <span style="font-family: arial;">⇒</span> t<sub>1</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(ST_IfFalse)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(if false then t<sub>1</sub> else t<sub>2</sub>) <span style="font-family: arial;">⇒</span> t<sub>2</sub></td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">t<sub>1</sub> <span style="font-family: arial;">⇒</span> t<sub>1</sub>'</td>
<td class="infrulenamecol" rowspan="3">
(ST_If)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">(if t<sub>1</sub> then t<sub>2</sub> else t<sub>3</sub>) <span style="font-family: arial;">⇒</span> (if t<sub>1</sub>' then t<sub>2</sub> else t<sub>3</sub>)</td>
<td></td>
</td>
</table></center>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Reserved Notation</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒</span>' t<sub>2</sub>" (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">step</span> : <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tm</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ST_AppAbs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">T</span> <span class="id" type="var">t<sub>12</sub></span> <span class="id" type="var">v<sub>2</sub></span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>2</sub></span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tapp</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T</span> <span class="id" type="var">t<sub>12</sub></span>) <span class="id" type="var">v<sub>2</sub></span>) <span style="font-family: arial;">⇒</span> [<span class="id" type="var">x</span>:=<span class="id" type="var">v<sub>2</sub></span>]<span class="id" type="var">t<sub>12</sub></span><br/>
| <span class="id" type="var">ST_App1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">tapp</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span><br/>
| <span class="id" type="var">ST_App2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>2</sub>'</span>,<br/>
<span class="id" type="var">value</span> <span class="id" type="var">v<sub>1</sub></span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>2</sub>'</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">tapp</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">tapp</span> <span class="id" type="var">v<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub>'</span><br/>
| <span class="id" type="var">ST_IfTrue</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
(<span class="id" type="var">tif</span> <span class="id" type="var">ttrue</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub></span><br/>
| <span class="id" type="var">ST_IfFalse</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>,<br/>
(<span class="id" type="var">tif</span> <span class="id" type="var">tfalse</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>) <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>2</sub></span><br/>
| <span class="id" type="var">ST_If</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>3</sub></span>,<br/>
<span class="id" type="var">t<sub>1</sub></span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t<sub>1</sub>'</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">tif</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>3</sub></span>) <span style="font-family: arial;">⇒</span> (<span class="id" type="var">tif</span> <span class="id" type="var">t<sub>1</sub>'</span> <span class="id" type="var">t<sub>2</sub></span> <span class="id" type="var">t<sub>3</sub></span>)<br/>
<br/>
<span class="id" type="keyword">where</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒</span>' t<sub>2</sub>" := (<span class="id" type="var">step</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>).<br/>
<br/>
<span class="id" type="keyword">Hint</span> <span class="id" type="var">Constructors</span> <span class="id" type="var">step</span>.<br/>
<br/>
<span class="id" type="keyword">Notation</span> <span class="id" type="var">multistep</span> := (<span class="id" type="var">multi</span> <span class="id" type="var">step</span>).<br/>
<span class="id" type="keyword">Notation</span> "t<sub>1</sub> '<span style="font-family: arial;">⇒*</span>' t<sub>2</sub>" := (<span class="id" type="var">multistep</span> <span class="id" type="var">t<sub>1</sub></span> <span class="id" type="var">t<sub>2</sub></span>) (<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 40).<br/>
<br/>
<br/>
</div>
<div class="doc">
<a name="lab667"></a><h3 class="section">Examples</h3>
<div class="paragraph"> </div>
Example:
<div class="paragraph"> </div>
<div class="code code-tight">
((\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>) (\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>)) <span style="font-family: arial;">⇒*</span> (\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>)
<div class="paragraph"> </div>
</div>
i.e.
<div class="paragraph"> </div>
<div class="code code-tight">
(<span class="id" type="var">idBB</span> <span class="id" type="var">idB</span>) <span style="font-family: arial;">⇒*</span> <span class="id" type="var">idB</span>
<div class="paragraph"> </div>
</div>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">step_example1</span> :<br/>
(<span class="id" type="var">tapp</span> <span class="id" type="var">idBB</span> <span class="id" type="var">idB</span>) <span style="font-family: arial;">⇒*</span> <span class="id" type="var">idB</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">multi_step</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_AppAbs</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">v_abs</span>.<br/>
<span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">multi_refl</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Example:
<div class="paragraph"> </div>
<div class="code code-tight">
((\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>) ((\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>) (\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>))) <br/>
<span style="font-family: arial;">⇒*</span> (\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>)
<div class="paragraph"> </div>
</div>
i.e.
<div class="paragraph"> </div>
<div class="code code-tight">
(<span class="id" type="var">idBB</span> (<span class="id" type="var">idBB</span> <span class="id" type="var">idB</span>)) <span style="font-family: arial;">⇒*</span> <span class="id" type="var">idB</span>.
<div class="paragraph"> </div>
</div>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">step_example2</span> :<br/>
(<span class="id" type="var">tapp</span> <span class="id" type="var">idBB</span> (<span class="id" type="var">tapp</span> <span class="id" type="var">idBB</span> <span class="id" type="var">idB</span>)) <span style="font-family: arial;">⇒*</span> <span class="id" type="var">idB</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">multi_step</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_App2</span>. <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_AppAbs</span>. <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="tactic">eapply</span> <span class="id" type="var">multi_step</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">ST_AppAbs</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">auto</span>.<br/>
<span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">multi_refl</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Example:
<div class="paragraph"> </div>
<div class="code code-tight">
((\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span><span style="font-family: arial;">→</span><span class="id" type="var">Bool</span>. <span class="id" type="var">x</span>) (\<span class="id" type="var">x</span>:<span class="id" type="var">Bool</span>. <span class="id" type="keyword">if</span> <span class="id" type="var">x</span> <span class="id" type="keyword">then</span> <span class="id" type="var">false</span><br/>