-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch01.v
2251 lines (2045 loc) · 59.7 KB
/
Ch01.v
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
(**
%\part{Foundations}%
%\section*{Introduction}%
The following are solutions to exercises from
_Homotopy Type Theory: Univalent Foundations of Mathematics_. The Coq
code given alongside the by-hand solutions requires the HoTT version of Coq,
available %\href{https://github.com/HoTT}{at the HoTT github repository}%.
*)
Require Export HoTT.
(**
%\tableofcontents%
* Type Theory *)
(** %\exerdone{1.1}{56}%
Given functions $f:A\to B$ and $g:B\to C$, define their composite $g
\circ f : A \to C$. Show that we have $h \circ (g \circ f) \equiv (h
\circ g) \circ f$.
%\soln%
Define $g \circ f \defeq \lam{x:A}g(f(x))$. Then if $h:C \to D$, we
have
%\[
h \circ (g \circ f)
\equiv \lam{x:A}h((g \circ f)x)
\equiv \lam{x:A}h((\lam{y:A}g(fy))x)
\equiv \lam{x:A}h(g(fx))
\]%
and
%\[
(h \circ g) \circ f
\equiv \lam{x:A}(h \circ g)(fx)
\equiv \lam{x:A}(\lam{y:B}h(gy))(fx)
\equiv \lam{x:A}h(g(fx))
\]%
So $h \circ (g \circ f) \equiv (h \circ g) \circ f$. In Coq, we have *)
Module Ex1.
Definition compose {A B C : Type} (g : B -> C) (f : A -> B) := fun x => g (f x).
Lemma compose_assoc (A B C D : Type) (f : A -> B) (g : B-> C) (h : C -> D)
: compose h (compose g f) = compose (compose h g) f.
Proof.
reflexivity.
Defined.
End Ex1.
(** %\exerdone{1.2}{56}%
Derive the recursion principle for products $\rec{A \times B}$ using only the
projections, and verify that the definitional equalities are valid. Do the
same for $\Sigma$-types. *)
(** %\soln%
The recursion principle states that we can define a function $f : A \times B
\to C$ by giving its value on pairs. Suppose that we have projection functions
$\fst : A \times B \to A$ and $\snd : A \times B \to B$. Then we can define a
function of type
%\[
\rec{A\times B}' : \prd{C : \UU} (A \to B \to C) \to A \times B \to C
\]%
in terms of these projections as follows
%\[
\rec{A \times B}'(C, g, p) \defeq g(\fst p)(\snd p)
\]%
or, in Coq,
*)
Module Ex2.
Section Ex2a.
Context (A B : Type).
Definition prod_rect (C : Type) (g : A -> B -> C) (p : A * B)
:= g (fst p) (snd p).
(** We must then show that
%\begin{align*}
\rec{A\times B}'(C, g, (a, b))
\equiv g(\fst (a, b))(\snd (a, b))
\equiv g(a)(b)
\end{align*}%
which in Coq amounts to showing that these this equality is a reflexivity.
*)
Theorem prod_rect_correct : forall (C : Type) (g : A -> B -> C) (a : A) (b : B),
prod_rect C g (a, b) = g a b.
Proof.
reflexivity.
Defined.
End Ex2a.
(** Now for the $\Sigma$-types. Here we have a projection
%\[
\fst : \left(\sm{x : A} B(x) \right) \to A
\]%
and another
%\[
\snd : \prd{p : \sm{x : A} B(x)} B(\fst (p))
\]%
Define a function of type
%\[
\rec{\sm{x:A}B(x)}' : \prd{C:\UU} \left(\tprd{x:A} B(x) \to C \right) \to
\left(\tsm{x:A}B(x) \right) \to C
\]%
by
%\[
\rec{\sm{x:A}B(x)}'(C, g, p) \defeq g(\fst p)(\snd p)
\]%
*)
Section Ex2b.
Context (A : Type).
Context (B : A -> Type).
Definition sigma_rect (C : Type) (g : forall (x : A), B x -> C) (p : {x:A & B x})
:= g (p.1) (p.2).
(** %\noindent%
We then verify that
%\begin{align*}
\rec{\sm{x:A}B(x)}(C, g, (a, b))
\equiv g(\fst (a, b))(\snd (a, b))
\equiv g(a)(b)
\end{align*}%
*)
Theorem sigma_rect_correct : forall (C:Type) (g : forall x, B x -> C) (a:A) (b:B a),
sigma_rect C g (a; b) = g a b.
Proof.
reflexivity.
Defined.
End Ex2b.
End Ex2.
(** %\exerdone{1.3}{56}%
Derive the induction principle for products $\ind{A \times B}$ using only the
projections and the propositional uniqueness principle $\uppt$.
Verify that the definitional equalities are valid. Generalize $\uppt$
to $\Sigma$-types, and do the same for $\Sigma$-types. *)
(** %\soln%
The induction principle has type
%\[
\ind{A\times B} : \prd{C: A\times B \to \UU}\left(\prd{x:A}\prd{y:B}C((x,
y))\right) \to \prd{z:A\times B}C(z)
\]%
For a first pass, we can define our new inductor as
%\[
\lam{C}{g}{z}g(\fst z)(\snd z)
\]%
However, we have $g(\fst x)(\snd x) : C((\fst x, \snd x))$, so the type of this
$\ind{A \times B}'$ is
%\[
\prd{C: A\times B \to \UU}\left(\prd{x:A}\prd{y:B}C((x,
y))\right) \to \prd{z:A\times B}C((\fst z, \snd z))
\]%
To define $\ind{A \times B}'$ with the correct type, we need the
$\mathsf{transport}$ operation from the next chapter. The uniqueness principle
for product types is
%\[
\uppt : \prd{x : A \times B} \big((\fst x, \snd x) =_{A \times B} x\big)
\]%
By the transport principle, there is a function
%\[
(\uppt\, x)_{*} : C((\fst x, \snd x)) \to C(x)
\]%
so
%\[
\ind{A \times B}(C, g, z)
\defeq
(\uppt\, z)_{*}(g(\fst z)(\snd z))
\]%
has the right type. In Coq we use [eta_prod], which is the name for
$\uppt$, then use it with transport to give our $\ind{A\times B}'$. *)
Module Ex3.
Section Ex3a.
Context (A B : Type).
Definition prod_rect (C : A * B -> Type) (g : forall (x:A) (y:B), C (x, y)) (z : A * B)
:= (eta_prod z) # (g (fst z) (snd z)).
(**
We now have to show that
%\[
\ind{A \times B}(C, g, (a, b)) \equiv g(a)(b)
\]%
Unfolding the left gives
%\begin{align*}
\ind{A \times B}(C, g, (a, b))
&\equiv
(\uppt\, (a, b))_{*}(g(\fst (a, b))(\snd (a, b)))
\\&\equiv
\ind{=_{A \times B}}(D, d, (a, b), (a, b), \uppt((a, b)))(g(a)(b))
\\&\equiv
\ind{=_{A \times B}}(D, d, (a, b), (a, b), \refl{(a, b)})
(g(a)(b))
\\&\equiv
\mathsf{id}_{C((a, b))}(g(a)(b))
\\&\equiv
g(a)(b)
\end{align*}%
which was to be proved. *)
Theorem prod_rect_correct :
forall (C : A * B -> Type) (g : forall (x:A) (y:B), C (x, y)) (a:A) (b:B),
prod_rect C g (a, b) = g a b.
Proof.
reflexivity.
Defined.
End Ex3a.
(** For $\Sigma$-types, we define
%\[
\ind{\tsm{x:A}B(x)}' : \prd{C:(\tsm{x:A}B(x)) \to \UU}
\left(\tprd{a:A}\tprd{b:B(a)}C((a, b))\right) \to \prd{p: \tsm{x:A}B(x)}C(p)
\]%
at first pass by
%\[
\lam{C}{g}{p}g(\fst p)(\snd p)
\]%
We encounter a similar problem as in the product case: this gives an output in
$C((\fst p, \snd p))$, rather than $C(p)$. So we need a uniqueness
principle for $\Sigma$-types, which would be a function
%\[
\upst : \prd{p : \sm{x:A}B(x)} \big( (\fst p, \snd p) =_{\sm{x:A}B(x)} p \big)
\]%
As for product types, we can define
%\[
\upst((a, b)) \defeq \refl{(a, b)}
\]%
which is well-typed, since $\fst(a, b) \equiv a$ and $\snd(a, b) \equiv b$.
Thus, we can write
%\[
\ind{\sm{x:A}B(x)}'(C, g, p) \defeq (\upst\, p)_{*}(g(\fst p)(\snd p)).
\]%
and in Coq, *)
Section Ex3b.
Context (A : Type).
Context (B : A -> Type).
Definition sigma_rect (C : {x:A & B x} -> Type)
(g : forall (a:A) (b:B a), C (a; b)) (p : {x:A & B x})
:= (eta_sigma p) # (g (p.1) (p.2)).
(**
Now we must verify that
%\[
\ind{\sm{x:A}B(x)}'(C, g, (a, b)) \equiv g(a)(b)
\]%
We have
%\begin{align*}
\ind{\sm{x:A}B(x)}'(C, g, (a, b))
&\equiv
(\uppt\, (a, b))_{*}(g(\fst(a, b))(\snd(a, b)))
\\&\equiv
\ind{=_{\sm{x:A}B(x)}}'(D, d, (a, b), (a, b), \uppt\, (a, b))
(g(a)(b))
\\&\equiv
\ind{=_{\sm{x:A}B(x)}}'(D, d, (a, b), (a, b), \refl{(a, b)})
(g(a)(b))
\\&\equiv
\mathsf{id}_{C((a, b))}
(g(a)(b))
\\&\equiv
g(a)(b)
\end{align*}% *)
Theorem sigma_rect_correct :
forall (C : {x:A & B x} -> Type)
(g : forall (a:A) (b:B a), C (a; b)) (a : A) (b : B a),
sigma_rect C g (a; b) = g a b.
Proof.
reflexivity.
Defined.
End Ex3b.
End Ex3.
(** %\exerdone{1.4}{56}%
Assuming as given only the _iterator_ for natural numbers
%\[
\ite :
\prd{C:\UU} C \to (C \to C) \to \mathbb{N} \to C
\]%
with the defining equations
%\begin{align*}
\ite(C, c_{0}, c_{s}, 0) &\defeq c_{0}, \\
\ite(C, c_{0}, c_{s}, \suc(n)) &\defeq c_{s}(\ite(C, c_{0}, c_{s}, n)),
\end{align*}%
derive a function having the type of the recursor $\rec{\mathbb{N}}$. Show
that the defining equations of the recursor hold propositionally for this
function, using the induction principle for $\mathbb{N}$. *)
Section Ex4.
Variable C : Type.
Variable c0 : C.
Variable cs : nat -> C -> C.
(** %\soln%
Fix some $C : \UU$, $c_{0} : C$, and $c_{s} : \mathbb{N} \to C \to C$.
$\ite(C)$ allows for the $n$-fold application of a single function to a single
input from $C$, whereas $\rec{\mathbb{N}}$ allows each application to depend on
$n$, as well. Since $n$ just tracks how many applications we've done, we can
construct $n$ on the fly, iterating over elements of $\mathbb{N} \times C$. So
we will use the iterator
%\[
\ite_{\mathbb{N} \times C} : \mathbb{N} \times C \to (\mathbb{N} \times C
\to \mathbb{N} \times C) \to \mathbb{N} \to \mathbb{N} \times C
\]%
to derive a function
%\[
\Phi : \prd{C : \UU} C \to (\mathbb{N} \to C \to C) \to
\mathbb{N} \to C
\]%
which has the same type as $\rec{\mathbb{N}}$.
The first argument of $\ite_{\mathbb{N} \times C}$ is the starting point,
which we'll make $(0, c_{0})$. The second input takes an element of
$\mathbb{N} \times C$ as an argument and uses $c_{s}$ to construct a new
element of $\mathbb{N} \times C$. We can use the first and second elements of
the pair as arguments for $c_{s}$, and we'll use $\suc$ to advance the first
argument, representing the number of steps taken. This gives the function
%\[
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x))
: \mathbb{N} \times C \to \mathbb{N} \times C
\]%
for the second input to $\ite_{\mathbb{N} \times C}$. The third input is just
$n$, which we can pass through. Plugging these in gives
%\[
\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
n
\big)
: \mathbb{N} \times C
\]%
from which we need to extract an element of $C$. This is easily done with the
projection operator, so we have
%\[
\Phi(C, c_{0}, c_{s}, n) \defeq
\snd\bigg(
\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
n
\big)
\bigg)
\]%
which has the same type as $\rec{\mathbb{N}}$. In Coq we first define the
iterator and then our alternative recursor: *)
Fixpoint iter (C : Type) (c0 : C) (cs : C -> C) (n : nat) : C :=
match n with
| O => c0
| S n' => cs(iter C c0 cs n')
end.
Definition Phi (C : Type) (c0 : C) (cs: nat -> C -> C) (n : nat) :=
snd (iter (nat * C)
(O, c0)
(fun x => (S (fst x), cs (fst x) (snd x)))
n).
(**
Now to show that the defining equations hold propositionally for $\Phi$.
For clarity of notation, define
%\[
\Phi'(n) =
\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
n
\big)
\]% *)
Definition Phi' (n : nat) :=
iter (nat * C) (O, c0) (fun x => (S (fst x), cs (fst x) (snd x))) n.
(** %\noindent%
So the propositional equalities can be written
%\begin{align*}
\snd \Phi'(0) &=_{C} c_{0} \\
\prd{n:\mathbb{N}} \snd \Phi'(\suc(n)) &=_{C} c_{s}(n, \snd \Phi'(n)).
\end{align*}%
The first is straightforward:
%\[
\snd \Phi'(0)
\equiv
\snd\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
0
\big)
\equiv
\snd(0, c_{0})
\equiv
c_{0}
\]%
so $\refl{c_{0}} : \snd\Phi'(0) =_{C} c_{0}$. To establish the second, we use
induction on a strengthened hypothesis involving $\Phi'$. We will establish
that for all $n : \mathbb{N}$,
%\[
P(n) \defeq
\Phi'(\suc(n)) =_{C} (\suc(n), c_{s}(n, \snd\Phi'(n)))
\]%
is inhabited.
For the base case, we have
%\begin{align*}
\Phi'(\suc(0)) &\equiv
\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
\suc(0)
\big)
\\&\equiv
\Big(\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x))\Big)
\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
0
\big)
\\&\equiv
\Big(\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x))\Big)
(0, c_{0})
\\&\equiv
(\suc(0), c_{s}(0, c_{0}))
\\&\equiv
(\suc(0), c_{s}(0, \snd\Phi'(0)))
\end{align*}%
using the derivation of the first propositional equality. So $P(0)$ is
inhabited, or $p_{0} : P(0)$. For the induction
hypothesis, suppose that $n : \mathbb{N}$ and that $p_{n} : P(n)$. A little
massaging gives
%\begin{align*}
\Phi'(\suc(\suc(n)))
&\equiv
\ite_{\mathbb{N} \times C}\big(
(0, c_{0}),
\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x)),
\suc(\suc(n))
\big)
\\&\equiv
\Big(\lam{x}(\suc(\fst x), c_{s}(\fst x, \snd x))\Big) \Phi'(\suc(n))
\\&\equiv
(\suc(\fst \Phi'(\suc(n))), c_{s}(\fst \Phi'(\suc(n)), \snd \Phi'(\suc(n))))
\end{align*}%
We now apply based path induction using $p_{n}$. Consider the family
%\[
D : \prd{z:\mathbb{N} \times C}(\Phi'(\suc(n)) = x) \to \UU
\]%
given by
%\[
D(z) \defeq
\Big(\suc(\fst \Phi'(\suc(n))), c_{s}(\fst \Phi'(\suc(n)), \snd
\Phi'(\suc(n)))\Big)
=
(\suc(\fst z), c_{s}(\fst z, \snd \Phi'(\suc(n))))
\]%
Clearly, we have
%\[
\refl{\Phi'(\suc(\suc(n)))} : D(\Phi'(\suc(n)), \refl{\Phi'(\suc(n))})
\]%
so by based path induction, there is an element
%\begin{align*}
f((\suc(n), c_{s}(n, \snd\Phi'(n))), p_{n}) &:
\Big(\suc(\fst \Phi'(\suc(n))), c_{s}(\fst \Phi'(\suc(n)), \snd
\Phi'(\suc(n)))\Big)
\\&=
(\suc(\fst (\suc(n), c_{s}(n, \snd\Phi'(n)))),
\\&\phantom{---}
c_{s}(\fst (\suc(n), c_{s}(n,
\snd\Phi'(n))), \snd\Phi'(\suc(n))))
\end{align*}%
Let $p_{n+1} \defeq f((\suc(n), c_{s}(n, \snd \Phi'(n))))$.
Our first bit of massaging allows us to replace the left hand side of this by
$\Phi'(\suc(\suc(n))$. As for the right, applying the projections gives
%\begin{align*}
p_{n+1} &: \Phi'(\suc(\suc(n)))
=
(\suc(\suc(n)), c_{s}(\suc(n), \snd\Phi'(\suc(n))))
\equiv P(\suc(n))
\end{align*}%
Plugging all this into our induction principle for $\mathbb{N}$, we can
discharge the assumption that $p_{n} : P(n)$ to obtain
%\[
q \defeq \ind{\mathbb{N}}(P, p_{0}, \lam{n}{p_{n}}p_{n+1}, n) : P(n)
\]%
The propositional equality we're after is a consequence of this, which we again
obtain by based path induction. Consider the family
%\[
E : \prd{z:\mathbb{N} \times C}(\Phi'(n) = z) \to \UU
\]%
given by
%\[
E(z, p) \defeq
\snd\Phi'(\suc(n)) = \snd z
\]%
Again, it's clear that
%\[
\refl{\snd\Phi'(\suc(n))} : E(\Phi'(\suc(n)), \refl{\Phi'(\suc(n))}
\]%
So based path induction gives us a function
%\[
g((\suc(n), c_{s}(n, \snd\Phi'(n))), q) :
\snd\Phi'(\suc(n)) = \snd (\suc(n), c_{s}(n, \snd\Phi'(n)))
\]%
and by applying the projection function on the right and discharging the
assumption of $n$, we have shown that
%\[
\prd{n:\mathbb{N}}\snd\Phi'(\suc(n)) = c_{s}(n, \snd\Phi'(n))
\]%
is inhabited. Next chapter we'll prove that functions are functors, and we
won't have to do this based path induction every single time. It'll be great.
Repeating it all in Coq, we have*)
Theorem Phi'_correct1 : snd (Phi' 0) = c0.
Proof.
reflexivity.
Defined.
Theorem Phi'_correct2 : forall n, Phi'(S n) = (S n, cs n (snd (Phi' n))).
Proof.
intros. induction n. reflexivity.
transitivity ((S (fst (Phi' (S n))), cs (fst (Phi' (S n))) (snd (Phi' (S n))))).
reflexivity.
rewrite IHn. reflexivity.
Defined.
End Ex4.
(** %\exerdone{1.5}{56}%
Show that if we define $A + B \defeq \sm{x:\bool} \rec{\bool}(\UU, A,
B, x)$, then we can give a definition of $\ind{A+B}$ for which the
definitional equalities stated in %\S1.7% hold.
%\soln%
Define $A+B$ as stated. We need to define a function of type
%\[
\ind{A+B}' : \prd{C : (A + B) \to \UU}
\left( \tprd{a:A} C(\inl(a))\right)
\to
\left( \tprd{b:B} C(\inr(b))\right)
\to
\tprd{x : A + B} C(x)
\]%
which means that we also need to define $\inl' : A \to A + B$ and $\inr' B \to
A + B$; these are
%\begin{align*}
\inl'(a) \defeq (0_{\bool}, a)
\qquad\qquad
\inr'(b) \defeq (1_{\bool}, b)
\end{align*}%
In Coq, we can use [sigT] to define [coprd] as a
$\Sigma$-type: *)
Module Ex5.
Section Ex5.
Context (A B : Type).
Definition sum := {x:Bool & if x then B else A}.
Definition inl (a : A) := existT (fun x:Bool => if x then B else A) false a.
Definition inr (b : B) := existT (fun x:Bool => if x then B else A) true b.
(**
Suppose that $C : A + B \to \UU$, $g_{0} : \prd{a:A} C(\inl'(a))$, $g_{1} :
\prd{b:B} C(\inr'(b))$, and $x : A+B$; we're looking to define
%\[
\ind{A+B}'(C, g_{0}, g_{1}, x)
\]%
We will use $\ind{\sm{x:\bool}\rec{\bool}(\UU, A, B, x)}$, and for notational
convenience will write $\Phi \defeq \sm{x:\bool}\rec{\bool}(\UU, A, B, x)$.
$\ind{\Phi}$ has signature
%\[
\ind{\Phi} :
\prd{C : \Phi \to \UU}
\left(\tprd{x:\bool}\tprd{y:\rec{\bool}(\UU, A, B, x)}C((x, y))\right)
\to
\tprd{p:\Phi} C(p)
\]%
So
%\[
\ind{\Phi}(C) :
\left(\tprd{x:\bool}\tprd{y:\rec{\bool}(\UU, A, B, x)}C((x, y))\right)
\to
\tprd{p:\Phi} C(p)
\]%
To obtain something of type $\tprd{x:\bool}\tprd{y:\rec{\bool}(\UU, A, B,
x)}C((x, y))$ we'll have to use $\ind{\bool}$. In particular, for $B(x)
\defeq\prd{y:\rec{\bool}(\UU, A, B, x)}C((x, y))$ we have
%\[
\ind{\bool}(B)
:
B(0_{\bool})
\to
B(1_{\bool})
\to
\prd{x:\bool}
B(x)
\]%
along with
%\[
g_{0} :
\prd{a:A} C(\inl'(a))
\equiv
\prd{a:\rec{\bool}(\UU, A, B, 0_{\bool})} C((0_{\bool}, a))
\equiv
B(0_{\bool})
\]%
and similarly for $g_{1}$. So
%\[
\ind{\bool}(B, g_{0}, g_{1}) : \prd{x:\bool}\prd{y:\rec{\bool}(\UU, A, B, x)}
C((x, y))
\]%
which is just what we needed for $\ind{\Phi}$. So we define
%\[
\ind{A+B}'(C, g_{0}, g_{1}, x)
\defeq
\ind{\sm{x:\bool}\rec{\bool}(\UU, A, B, x)}\left(
C,
\ind{\bool}\left(
\prd{y:\rec{\bool}(\UU, A, B, x)} C((x, y)),
g_{0},
g_{1}
\right),
x
\right)
\]%
and, in Coq, we use sigT_rect, which is the built-in
$\ind{\sm{x:A}B(x)}$: *)
Definition sum_rect (C : sum -> Type)
(g0 : forall a : A, C (inl a))
(g1 : forall b : B, C (inr b))
(x : sum)
:=
sigT_rect C
(Bool_ind (fun x:Bool => forall (y : if x then B else A), C (x; y))
g1
g0)
x.
(**
Now we must show that the definitional equalities
%\begin{align*}
\ind{A+B}'(C, g_{0}, g_{1}, \inl'(a)) \equiv g_{0}(a) \\
\ind{A+B}'(C, g_{0}, g_{1}, \inr'(b)) \equiv g_{1}(b)
\end{align*}%
hold. For the first, we have
%\begin{align*}
\ind{A+B}'(C, g_{0}, g_{1}, \inl'(a))
&\equiv
\ind{A+B}'(C, g_{0}, g_{1}, (0_{\bool}, a))
\\&\equiv
\ind{\sm{x:\bool}\rec{\bool}(\UU, A, B, x)}\left(
C,
\ind{\bool}\left(
\prd{y:\rec{\bool}(\UU, A, B, x)} C((x, y)),
g_{0},
g_{1}
\right),
(0_{\bool}, a)
\right)
\\&\equiv
\ind{\bool}\left(
\prd{y:\rec{\bool}(\UU, A, B, x)} C((x, y)),
g_{0},
g_{1},
0_{\bool}
\right)(a)
\\&\equiv
g_{0}(a)
\end{align*}%
and for the second,
%\begin{align*}
\ind{A+B}'(C, g_{0}, g_{1}, \inr'(b))
&\equiv
\ind{A+B}'(C, g_{0}, g_{1}, (1_{\bool}, b))
\\&\equiv
\ind{\sm{x:\bool}\rec{\bool}(\UU, A, B, x)}\left(
C,
\ind{\bool}\left(
\prd{y:\rec{\bool}(\UU, A, B, x)} C((x, y)),
g_{0},
g_{1}
\right),
(1_{\bool}, b)
\right)
\\&\equiv
\ind{\bool}\left(
\prd{y:\rec{\bool}(\UU, A, B, x)} C((x, y)),
g_{0},
g_{1},
1_{\bool}
\right)(b)
\\&\equiv
g_{1}(b)
\end{align*}% *)
Theorem sum_rect_correct1 :
forall C g0 g1 a, sum_rect C g0 g1 (inl a) = g0 a.
Proof.
reflexivity.
Qed.
Theorem sum_rect_correct2 :
forall C g0 g1 a, sum_rect C g0 g1 (inr a) = g1 a.
Proof.
reflexivity.
Qed.
End Ex5.
End Ex5.
(** %\exerdone{1.6}{56}%
Show that if we define $A \times B \defeq \prd{x : \bool}
\rec{\bool}(\UU, A, B, x)$, then we can give a definition of $\ind{A \times
B}$ for which the definitional equalities stated in %\S1.5% hold
propositionally (i.e.%~%using equality types). *)
(** %\soln%
Define
%\[
A \times B \defeq \prd{x : \bool} \rec{\bool}(\UU, A, B, x)
\]%
Supposing that $a : A$ and $b : B$, we have an element $(a, b) : A \times B$
given by
%\[
(a, b) \defeq \ind{\bool}(\rec{\bool}(\UU, A, B), a, b)
\]%
Defining this type and constructor in Coq, we have *)
Module Ex6.
Section Ex6.
Context (A B : Type).
Definition prod := forall x : Bool, if x then B else A.
Definition pair (a : A) (b : B)
:= Bool_ind (fun x : Bool => if x then B else A) b a.
(**
An induction principle for $A \times B$ will, given a family $C : A \times B
\to \UU$ and a function
%\[
g : \prd{x:A}\prd{y:B} C((x, y)),
\]%
give a function $f : \prd{x : A \times B}C(x)$ defined by
%\[
f((x, y)) \defeq g(x)(y)
\]%
So suppose that we have such a $C$ and $g$. Writing things out in terms of the
definitions, we have
%\begin{align*}
C &: \left(\prd{x:\bool}\rec{\bool}(\UU, A, B, x)\right) \to \UU \\
g &: \prd{x:A}\prd{y:B} C(\ind{\bool}(\rec{\bool}(\UU, A, B), x, y))
\end{align*}%
We can define projections by
%\[
\fst p \defeq p(0_{\bool}) \qquad\qquad \snd p \defeq p(1_{\bool})
\]%
Since $p$ is an element of a dependent type, we have
%\begin{align*}
p(0_{\bool}) &: \rec{\bool}(\UU, A, B, 0_{\bool}) \equiv A\\
p(1_{\bool}) &: \rec{\bool}(\UU, A, B, 1_{\bool}) \equiv B
\end{align*}% *)
Definition fst (p : prod) := p false.
Definition snd (p : prod) := p true.
(**
Then we have
%\begin{align*}
g(\fst p)(\snd p)
&: C(\ind{\bool}(\rec{\bool}(\UU, A, B), (\fst p), (\snd p)))
\equiv
C((p(0_{\bool}), p(1_{\bool})))
\end{align*}%
So we have defined a function
%\[
f' : \prd{p : A \times B} C((p(0_{\bool}), p(1_{\bool})))
\]%
But we need one of the type
%\[
f : \prd{p : A \times B} C(p)
\]%
To solve this problem, we need to appeal to function extensionality from %\S2.9%.
This implies that there is a function
%\[
\funext :
\left(\prd{x:\bool} ((\fst p, \snd p)(x) =_{\rec{\bool}(\UU, A, B, x)} p(x))\right)
\to
((\fst p, \snd p) =_{A \times B} p)
\]%
We just need to show that the antecedent is inhabited, which we can do with
$\ind{\bool}$. So consider the family
%\begin{align*}
E &\defeq
\lam{x : \bool}
((p(0_{\bool}), p(1_{\bool}))(x) =_{\rec{\bool}(\UU, A, B, x)} p(x)))
\\&\phantom{:}\equiv
\lam{x : \bool}
(\ind{\bool}(\rec{\bool}(\UU, A, B), p(0_{\bool}), p(1_{\bool}), x)
=_{\rec{\bool}(\UU, A, B, x)} p(x))
\end{align*}%
We have
%\begin{align*}
E(0_{\bool})
&\equiv
(\ind{\bool}(\rec{\bool}(\UU, A, B),
p(0_{\bool}), p(1_{\bool}), 0_{\bool}) =_{\rec{\bool}(\UU, A, B, 0_{\bool})}
p(0_{\bool}))
\\&\equiv
(p(0_{\bool}) =_{\rec{\bool}(\UU, A, B, 0_{\bool})} p(0_{\bool}))
\end{align*}%
Thus $\refl{p(0_{\bool})} : E(0_{\bool})$. The same argument goes through to
show that $\refl{p(1_{\bool})} : E(1_{\bool})$. This means that
%\[
h \defeq
\ind{\bool}(E, \refl{p(0_{\bool})}, \refl{p(1_{\bool})})
:
\prd{x : \bool} ((\fst p, \snd p)(x) =_{\rec{\bool}(\UU, A, B, x)} p(x))
\]%
and thus
%\[
\funext(h)
:
(p(0_{\bool}), p(1_{\bool}))
=_{A \times B}
p
\]%
This allows us to define the uniqueness principle for products:
%\[
\uppt \defeq \lam{p}\funext(h)
: \prd{p:A \times B}
(\fst p, \snd p)
=_{A \times B}
p
\]%
Now we can define $\ind{A\times B}$ as
%\[
\ind{A\times B}(C, g, p) \defeq (\uppt\, p)_{*}(g(\fst p)(\snd p))
\]%
In Coq we can repeat this construction using [Funext]. *)
Definition eta_prod `{Funext} (p : prod) : pair (fst p) (snd p) = p.
apply path_forall.
unfold pointwise_paths; apply Bool_ind; reflexivity.
Defined.
Definition prod_rect `{Funext} (C : prod -> Type)
(g : forall (x:A) (y:B), C (pair x y)) (z : prod)
:= (eta_prod z) # (g (fst z) (snd z)).
(**
Now, we must show that the definitional equality holds propositionally. That
is, we must show that the type
%\[
\ind{A \times B}(C, g, (a, b)) =_{C((a, b))} g(a)(b)
\]%
is inhabited. Unfolding the left gives
%\begin{align*}
\ind{A \times B}(C, g, (a, b))
&\equiv
(\uppt\, (a, b))_{*}(g(\fst (a, b))(\snd (a, b)))
\\&\equiv
\ind{=_{C((a, b))}}(D, d, (a, b), (a, b), \uppt\, (a, b))(g(a)(b))
\end{align*}%
where $D : \prd{x, y : A \times B} (x = y) \to \UU$ is given by $D(x, y, p)
\defeq C(x) \to C(y)$ and
%\[
d \defeq \lam{x}\idfunc{C(x)} : \prd{x:A\times B} D(x, x, \refl{x})
\]%
Now,
%\[
\uppt\, (a, b) \equiv \funext(h) : (a, b) =_{A \times B} (a, b)
\]%
and, in particular, we have $h : x \mapsto \refl{(a, b)(x)}$, so $\funext(h) =
\refl{(a, b)}$. Plugging this into $\ind{=_{C((a, b))}}$ and applying its
defining equality gives
%\begin{align*}
\ind{A \times B}(C, g, (a, b))
&=
\ind{=_{C((a, b))}}(D, d, (a, b), (a, b), \refl{(a, b)})(g(a)(b))
\\&=
d((a, b))(g(a)(b))
\\&=
\idfunc{C((a, b))}(g(a)(b))
\\&=
g(a)(b)
\end{align*}%
Verifying that the definitional equality holds propositionally. The reason we
can only get propositional equality, not judgemental equality, is that
$\funext(h) = \refl{(a, b)}$ is just a propositional equality. Understanding
this better requires stuff from next chapter.
*)
Lemma prod_rect_correct `{Funext} C g a b
: prod_rect C g (pair a b) = g a b.
Proof.
unfold prod_rect.
path_via (transport C 1 (g (fst (pair a b)) (snd (pair a b)))). f_ap.
unfold eta_prod.
path_via (path_forall (pair (fst (pair a b)) (snd (pair a b))) (pair a b)
(fun _ => idpath)).
f_ap. apply path_forall; intro x. destruct x; reflexivity.
apply path_forall_1.
Defined.
End Ex6.
End Ex6.
(** %\exerdone{1.7}{56}%
Give an alternative derivation of $\ind{=_{A}}'$ from $\ind{=_{A}}$ which
avoids the use of universes. *)
(** %\soln%
To avoid universes, we follow the plan from %p.~53% of the text: show that
$\ind{=_{A}}$ entails Lemmas 2.3.1 and 3.11.8, and that these two principles
imply $\ind{=_{A}}'$ directly.
First we have Lemma 2.3.1, which states that for any type family $P$ over $A$
and $p : x =_{A} y$, there is a function $p_{*} : P(x) \to P(y)$. The proof
for this can be taken directly from the text. Consider the type family
%\[
D : \prd{x, y : A}(x = y) \to \UU,
\qquad\qquad
D(x, y, p) \defeq P(x) \to P(y)
\]%
which exists, since $P(x) : \UU$ for all $x : A$ and these can be used to form
function types. We also have
%\[
d \defeq \lam{x}\idfunc{P(x)}
: \prd{x:A}D(x, x, \refl{x})
\equiv \prd{x:A} P(x) \to P(x)
\]%
We now apply $\ind{=_{A}}$ to obtain
%\[
p_{*} \defeq \ind{=_{A}}(D, d, x, y, p) : P(x) \to P(y)
\]%
establishing the Lemma.
Next we have Lemma 3.11.8, which states that for any $A$ and any $a : A$, the
type $\sm{x:A} (a = x)$ is contractible; that is, there is some $w :
\sm{x:A}(a=x)$ such that $w = w'$ for all $w' : \sm{x:A}(a=x)$. Consider the
point $(a, \refl{a}) : \sm{a:A}(a=x)$ and the family $C: \prd{x,y:A}(x=y) \to \UU$ given
by
%\[
C(x, y, p) \defeq
((x, \refl{x}) =_{\sm{z:A}(x=z)} (y, p))
\]%
Take also the function
%\[
\refl{(x, \refl{x})} : \prd{x:A} ((x, \refl{x}) =_{\sm{x:A}(x=z)} (x, \refl{x}))
\]%
By path induction, then, we have a function
%\[
g : \prd{x, y:A}\prd{p:x =_{A} y}
((x, \refl{x}) =_{\sm{z:A}(x=z)} (y, p))
\]%
such that $g(x, x, \refl{x}) \defeq \refl{(x, \refl{x})}$.
This allows us to construct