-
Notifications
You must be signed in to change notification settings - Fork 9
/
fol.tex
1698 lines (1446 loc) · 124 KB
/
fol.tex
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
\chapter{First-order logic}
\label{chap:fol}
\section{Predicate logic}
\label{sec:fol}
In \cref{sec:logic} we saw that the posetal reduction of a simple type theory can be regarded as a deductive system for logic (intuitionistic, linear, relevant, classical, etc.\ depending on the type theory).
However, these logics are only \emph{propositional}, lacking variables and the ability to quantify over them with statements such as ``for all $x$'' or ``there exists an $x$ such that''.
Similarly, in \cref{sec:fp-theories} we saw that simple type theory is adequate to express finite-product theories such as groups and rings, but not more complicated theories such as categories or fields.
The solution to both of these problems is the same: we combine \emph{two} type theories, one representing the objects (like a finite-product theory) and one representing the logic in which we speak about those objects.
The types in the second type theory, which we will henceforth call \textbf{propositions} instead of types to avoid confusion, will be \emph{dependent} on the types in the first type theory (which we sometimes call the \emph{base type theory}).
This means that terms belonging to types can appear in propositions.
More formally, it means that unlike the judgment $\types A \type$ for types (in the base type theory), the judgment for propositions \emph{has a context of types}, so we write it $\Gamma\types \ph\prop$.
We will have rules such as
\[ \inferrule*{\Gamma \types M:A \\ \Gamma\types N:A}{\Gamma\types (M =_A N)\prop} \]
allowing the logic (the type theory of propositions) to ``talk about'' equality of terms (morphisms between types).
Finally, since propositions depend on a context of types, their morphism judgment (which we also call \textbf{entailment}) must also depend on such a context.
Thus it has \emph{two} contexts, one of types and one of propositions, which we separate with a vertical bar: $\Gamma \cb \Theta \types \ph$.
In this section, we will describe and study type theories of this sort, with one simple type theory dependent on another simple type theory.
Unlike the type theories considered in \cref{chap:simple}, which were directly motivated by a corresponding categorical structure, in the present case it seems more natural to describe the type theory first and then define an appropriate categorical structure in order to match it.
(This is not to say that there are not lots of naturally occurring examples of this categorical structure; there are!
It's just that without the type theory in mind, we might not be led to define and study that exact class of categorical structures.)
Thus, we postpone consideration of their categorical semantics to \cref{sec:hyperdoctrines,sec:subobjects}.
We will also make several simplifying assumptions in this section.
Firstly, the base type theory will always be a bare theory of cartesian multicategories under some multigraph, with no type operations and no axioms.
The lack of axioms is not much of a problem, since once we have equality propositions we can use those instead.
The lack of type operations is a temporary simplification, but identifies our current subject as \emph{first-order} logic; in \cref{chap:hol} on ``higher-order logic'' we will reintroduce type operations.
The \emph{cartesianness} of the base type theory is also a simplifying assumption, but one that we will not (in this book) ever generalize away from.
People have attempted to define first-order logics over non-cartesian base theories, but in general the results are more complicated and less intuitive, and there are fewer interesting categorical examples.
Secondly, in this section the logic will be posetal, so that we care only about the existence of derivations rather than their values, and hence we will not introduce terms belonging to propositions.
We will generalize away from this assumption in \cref{sec:indexed-moncat}.
\subsection{Structural rules and simple rules}
\label{sec:fol-struc}
With all that out of the way, we move on to actually describing the rules.
As mentioned above, the base type theory is that for cartesian multicategories under a multigraph \cG:
\begin{mathpar}
\inferrule{\types A\type \\ (x:A)\in \Gamma}{\Gamma\types x:A}\;\idfunc
\and
\inferrule{f\in \cG(A_1,\dots,A_n;B) \\ \Gamma\types M_1:A_1 \\ \cdots \\ \Gamma \types M_n:A_n}{\Gamma\types f(M_1,\dots,M_n):B}\;f
\end{mathpar}
As usual, cut/substitution is admissible for this theory.
For the propositions, we have two kinds of judgment:
\begin{mathpar}
\Gamma\types \ph\prop \and
\Gamma\cb\Theta\types \ph
\end{mathpar}
where $\Theta$ is a context (i.e.\ a list) of propositions.
Here the proposition $\ph$ should be regarded as a sort of ``term'' for the proposition judgment, that can be shown to uniquely determine a derivation of $\Gamma\types \ph\prop$.
Before discussing the rules for these judgments, however, we have to decide what to do about the structural rules such as cut.
As with propositional logic, we can formulate first-order logic in either a natural deduction or a sequent calculus style to make cut admissible.
However, I feel that both choices require formulating at least one of the rules for quantifiers and equality in a less-than-maximally-intuitive way.
Of course, intuitions differ from person to person.
But it is also a more objective fact that the most \emph{categorically natural} versions of the rules, as we will see in \cref{sec:hyperdoctrines}, also do not exactly match either the sequent calculus or the natural deduction versions.
There are also new structural rules, namely substitution of terms into propositions and entailments, that we would eventually like to be admissible as well.
For these reasons we take the following approach.
In this section we state all the structural rules, including both those that will stay primitive and those that will eventually be admissible.
Then in \crefrange{sec:forall}{sec:equality} we discuss the rules for the quantifiers and equality, mentioning all the ways that each rule can be formulated and showing that they are equivalent in the presence of all the structural rules.
Finally, in \cref{sec:fol-natded} we show that the natural deduction rules do make an appropriate selection of the structural rules admissible.
%Finally, in \cref{sec:fol-seqcalc,sec:fol-natded} we show that with a consistent choice of either the sequent calculus rules or the natural deduction rules we can make an appropriate selection of the structural rules admissible.
\begin{figure}
\centering
\begin{mathpar}
\inferrule*[right=exchange]{\Gamma\cb\Theta,\ph,\psi,\Delta\types \chi}{\Gamma\cb\Theta,\psi,\ph,\Delta\types \chi}\and
\inferrule*[right=weakening (maybe)]{\Gamma\cb \Theta,\Delta\types \chi}{\Gamma\cb\Theta,\ph,\Delta\types \chi}\and
\inferrule*[right=contraction (maybe)]{\Gamma\cb\Theta,\ph,\ph,\Delta\types \chi}{\Gamma\cb\Theta,\ph,\Delta\types \chi}\and
\inferrule*{ }{\Gamma\cb \ph\types\ph}\and
\inferrule*{\Gamma\cb\Theta \types \ph \\ \Gamma\cb \Psi,\ph\types \psi}{\Gamma\cb \Psi,\Theta\types \psi}\and
\inferrule*{\Gamma\types M:A \\ \Gamma,x:A\types \ph\prop}{\Gamma \types \ph[M/x] \prop}\and
\inferrule*{\Gamma\types M:A \\ \Gamma,x:A\cb\Theta\types \ph}{\Gamma\cb\Theta[M/x] \types \ph[M/x]}\and
\end{mathpar}
\caption{Structural rules for first-order logic}
\label{fig:fol-struc}
\end{figure}
The complete list of structural rules is shown in \cref{fig:fol-struc}.
As in \cref{sec:logic}, we always have exchange for propositions, but we allow ourselves the freedom to take or omit weakening and contraction, corresponding to a choice of a faithful cartesian club \fS (as in \cref{sec:cartmulti}).
Depending on which we choose, we speak of \textbf{intuitionistic first-order logic} (all the structural rules), \textbf{intuitionistic first-order linear logic} (exchange only), etc.
Then there are the identity and cut rule for propositions; the latter is just the cut rule from \cref{sec:monoidal-lattices} with an extra type context $\Gamma$.
There are also two new structural rules arising from the dependency of propositions on types: substitution of terms into propositions and into entailments.
Of all these structural rules, there is one that it is most important (for the purpose of categorical semantics) to make admissible: substitution of terms into propositions.
This is for the same reason that we want substitution into terms to be admissible.
Namely, we certainly want to be \emph{able} to make such substitutions, but if we asserted them as primitive then (to maintain the unique correspondence between names for propositions $\ph$ and the derivations of $\Gamma\types \ph\prop$) we would have to introduce ``$\ph[M/x]$'' as basic syntax, rather than an operation on syntax.
For instance, we want to be able to substitute $M$ for $x$ and $N$ for $y$ into $x=y$, and we want to be able to actually \emph{do} that substitution on the syntax to get $M=N$, rather than having to write $(x=y)[M/x,N/y]$ everywhere.
Another possibility would be to break the ``propositions are derivations'' correspondence and allow one proposition to have multiple derivations, but that has the same problems as breaking the ``terms are derivations'' correspondence in simple type theory; we do care about \emph{which} proposition we are talking about.
Fortunately, it is just as easy to ensure that substitution into propositions is admissible as it is to ensure that cut is admissible in a natural deduction.
We just make sure to ``build enough substitutions'' into the rules for the proposition judgment, so that their conclusions always have a fully general context.
Thus, we will always do this in our rules for the proposition judgment.
The other structural rules are all for entailment, and since at the moment we are interested in semantics where entailment corresponds to inequality in a poset, we only care about \emph{whether or not} an entailment is derivable rather than what all its derivations are.
Thus, it makes little difference (for the purpose of categorical semantics) whether these rules are primitive or admissible.
(However, there are still other technical advantages to admissibility.)
% some of which we will see later.
Now we move on to the logical rules for the proposition and entailment judgments.
To start with, there will be the usual rules for propositional logic from \cref{sec:logic}.
We import these rules into our present theory by assigning all of them an arbitrary context of types in the base theory that remains unchanged between the premises and the conclusion.
For instance, the rules for $\join$ are
\begin{mathpar}
\inferrule{\Gamma\cb\Theta\types A}{\Gamma\cb\Theta\types A\join B}\;\joinI1
\and
\inferrule{\Gamma\cb\Theta\types B}{\Gamma\cb\Theta\types A\join B}\;\joinI2
\and
% \inferrule{
% \Gamma\cb\Theta,A \types C \\ \Gamma\cb\Theta,B\types C
% }{\Gamma\cb\Theta,A\join B \types C}\;\joinL
\inferrule{
\Gamma\cb\Psi\types A\join B \\ \Gamma\cb\Theta,A \types C \\ \Gamma\cb\Theta,B\types C
}{\Gamma\cb\Theta,\Psi \types C}\;\joinE
\end{mathpar}
and likewise we have rules for $\bot,\meet,\top,\tensor,\one$, and $\hom$.
%\footnote{Since cut is primitive, we could simplify the rules $\joinI1$ and $\joinI2$ to $\Gamma\cb A\types A\join B$ and $\Gamma\cb B\types A\join B$, or write $\joinE$ as a sequent-calculus-style left rule; but we will stick with the more familiar natural deduction versions.}
Of course, in the cartesian case we can dispense with $\tensor$ and $\one$ (since they coincide with $\meet$ and $\top$), and write $\hom$ instead as $\To$ or $\to$.
The modularity of type theory means we can also mix and match, choosing the rules corresponding to some of these connectives but not others; in \cref{sec:subobjects} we will see that some groups of connectives are particularly natural from a categorical perspective.
The interesting new things happen with the \emph{new} operations on propositions that \emph{do} change the type context.
We will consider three such operations, which are particularly natural both categorically and logically.
The first two are the \emph{quantifiers} ``for all'' (the ``universal quantifier'') and ``there exists'' (the ``existential quantifier'').
The rules introducing these two propositions both look the same:
\begin{mathpar}
\inferrule{\Gamma,x:A\types \ph\prop}{\Gamma\types (\forall x:A.\ph) \prop}\and
\inferrule{\Gamma,x:A\types \ph\prop}{\Gamma\types (\exists x:A.\ph) \prop}\and
\end{mathpar}
(Note that in both cases the variable $x$ is \emph{bound} in the resulting proposition, just as it is in $\lambda x.M$.
If there is no danger of confusion, we may abbreviate these to $\all x.\ph$ and $\exis x.\ph$, but in general the type annotation is necessary to make type-checking possible.)
But the rules governing entailments involving them, of course, are different.
Recall that in natural deduction each type operation has either \emph{introduction} and \emph{elimination} rules, while in sequent calculus these are reformulated as \emph{right} and \emph{left} rules.
In the past we have motivated these rules by appeal to universal properties in a categorical structure, with one group of rules giving the basic data and the other giving their universal factorization property.
The rules for $\exis$ and $\all$ do correspond to universal properties, but since we have postponed the semantics of first-order logic to \cref{sec:hyperdoctrines} we will attempt to instead motivate their rules from an intuitive understanding of logic.
\subsection{The universal quantifier}
\label{sec:forall}
Informally, how do we prove that $\forall x:A.\ph$?
Arguably the most basic way to do it is to assume given an arbitrary $x:A$ and prove that $\ph$ is true (here $\ph$ is a statement involving $x$, hence involving our arbitrary assumed $x:A$).
This suggests the following introduction (or right) rule:
\begin{mathpar}
\inferrule{\Gamma,x:A\cb \Theta\types \ph}{\Gamma\cb\Theta\types \forall x:A.\ph}\;\forallI
\end{mathpar}
Note that since $\Theta$ appears in the conclusion, where $x$ is no longer in the type context, $\Theta$ cannot depend on $x$, even though syntactically the premise would allow that.
Similarly, what good does it do to know that $\forall x:A.\ph$?
The most basic thing it tells us is that if we have any particular element $M$ of $A$, then $\ph$ is true about $M$, i.e.\ with $M$ replacing $x$.
The simplest way to formulate this is
\begin{mathpar}
\inferrule{\Gamma\types M:A}{\Gamma\cb (\forall x:A.\ph) \types \ph[M/x]}\;\forallS
\end{mathpar}
But there are many other ways to say the same thing, including a sequent-calculus-style left rule, a natural-deduction-style elimination rule, and the opposite of the introduction rule:
\begin{mathpar}
\inferrule{\Gamma\types M:A \\ \Gamma\cb\Theta\types \forall x:A.\ph}{\Gamma\cb\Theta\types \ph[M/x]}\;\forallE\and
\inferrule{\Gamma\types M:A \\ \Gamma\cb \Theta, \ph[M/x] \types \psi}{\Gamma\cb \Theta, (\forall x:A.\ph) \types \psi}\;\forallL\and
\inferrule{\Gamma\cb\Theta\types \forall x:A.\ph}{\Gamma,x:A\cb \Theta\types \ph}\;\forallI^{-1}
\end{mathpar}
All of these rules are inter-derivable in the presence of cut and substitution.
For instance, we can derive $\forallE$ from $\forallI^{-1}$ using substitution:
\begin{mathpar}
\inferrule*[right=sub]{\Gamma\types M:A \\
\inferrule*[Right=$\forallI^{-1}$]{\Gamma\cb\Theta\types \forall x:A.\ph}{\Gamma,x:A\cb \Theta\types \ph}
}{\Gamma\cb\Theta\types \ph[M/x]}
\end{mathpar}
We can derive $\forallS$ as a special case of $\forallE$ using the identity rule:
\begin{mathpar}
\inferrule*[right=$\forallE$]{\Gamma\types M:A\\
\inferrule*{ }{\Gamma\cb (\forall x:A.\ph)\types \forall x:A.\ph}
}{\Gamma\cb (\forall x:A.\ph) \types \ph[M/x]}
\end{mathpar}
We can derive $\forallL$ from $\forallS$ using cut:
\begin{mathpar}
\inferrule*[right=cut]{
\inferrule*[right=$\forallS$]{\Gamma\types M:A}{\Gamma\cb (\forall x:A.\ph) \types \ph[M/x]} \\
\Gamma\cb \Theta, \ph[M/x] \types \psi}{\Gamma\cb \Theta, (\forall x:A.\ph) \types \psi}
\end{mathpar}
and finally $\forallI^{-1}$ from $\forallL$ using cut and weakening:
\begin{mathpar}
\inferrule*[right=cut]{
\inferrule*[right=weak]{\Gamma\cb\Theta\types \forall x:A.\ph}{\Gamma,x:A\cb\Theta\types \forall x:A.\ph}\\
\inferrule*[Right=$\forallL$]{
\inferrule*{ }{\Gamma,x:A\types x:A} \\
\inferrule*{ }{\Gamma,x:A\cb \ph \types \ph}}
{\Gamma,x:A\cb (\forall x:A.\ph) \types \ph}}
{\Gamma,x:A\cb \Theta\types \ph}
\end{mathpar}
In practice, therefore, we are free to use whichever rule we find most intuitive or convenient.
To make substitition and cut admissible in \cref{sec:fol-natded} we will use $\forallE$, while for categorical semantics in \cref{sec:hyperdoctrines} we will use $\forallI^{-1}$.
\begin{rmk}
Note that many of these rules involve substitution into propositions.
Thus, formally speaking we have to state all the rules for the proposition judgment $\Gamma\types \ph\prop$ first, \emph{then} prove that substitution into propositions is admissible (thereby defining the notation $\ph[M/x]$), and only after that can we state all the rules for the entailment judgment $\Gamma\cb\Theta\types\ph$.
A similar situation obtained for the equality judgment $\equiv$ for simple and unary type theories, which often involved substitution into terms (e.g.\ $(\lambda x.M)N \equiv M[N/x]$), so that we had to prove the admissibility of the latter before stating the rules for $\equiv$ (and likewise, when proving the initiality theorems, we had to show that our functor-in-progress took substitution to composition before defining it on equalities).
However, in practice we actually state all the rules at once, with the implicit understanding that afterwards we will define substitution so that the rules involving it make sense.
We do have to be careful, when taking such a shortcut, to notice whether we are introducing any ``cyclic dependencies''.
For instance, if there are any rules for the term or proposition judgments whose premises involve the entailment judgment, it is no longer possible to complete the definition of the former, \emph{then} define substitution for them, and \emph{then} give the definition of the latter: we would have to give the definition all at once, including (somehow) defining substitution at the same time.
It is possible to do this, but it is much more difficult and leads us into the realm of dependent type theory; see \cref{chap:dtt}.
In this chapter and \cref{chap:hol} none of our rules will introduce such cyclic dependencies.
We mention the possibility only as a warning to the reader, because it is easy (especially when adding rules to a type theory one by one) to fail to notice a cyclic dependency when it appears.
%See also \cref{rmk:subset-quotient}.
\end{rmk}
\subsection{The existential quantifier}
\label{sec:exists}
% [TODO: Remind the extra context as needed for modeling natural reasoning here?]
The most basic way to prove $\exists x:A.\ph$ is to exhibit a particular element $M$ of $A$ and prove that it has the property $\ph$ (that is, $\ph$ with $M$ replacing $x$ is true).
This is of course a ``constructive'' proof.
In classical mathematics one can also give ``nonconstructive'' existence proofs, but these arise by use of the law of excluded middle or its equivalent law of double negation.
The \emph{basic} way to prove existence, which uses no other logical laws than the meaning of ``existence'', is to supply a witness.
This leads to the following introduction (or right) rule for $\exis$:
\begin{mathpar}
\inferrule{\Gamma\types M:A \\ \Gamma\cb\Theta\types \ph[M/x]}{\Gamma\cb\Theta\types \exists x:A.\ph}\;\existsI
\end{mathpar}
On the other hand, what good does it do to know that $\exists x:A.\ph$?
It means we are free to assume that we have some element of $A$ satisfying $\ph$ (but about which we assume nothing else).
This is most simply expressed by a left rule:
\begin{mathpar}
\inferrule{\Gamma,x:A\cb\Theta,\ph\types \psi}{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}\;\existsL\and
\end{mathpar}
This is perhaps the least intuitive of the quantifier rules: it says that if we can prove some other statement $\psi$ under the assumption of some arbitrary $x:A$ that satisfies $\ph$, then we can also conclude $\psi$ under the assumption of $\exists x:A.\ph$.
(Note the similarity in structure between $\existsL$ and $\tensorL$; this suggests the eventual universal property we will find corresponding to $\exis$.)
By building in a cut, we can re-express $\existsL$ as an elimination rule instead:
\begin{mathpar}
\inferrule{\Gamma\cb\Psi\types \exists x:A.\ph \\ \Gamma,x:A\cb\Theta,\ph\types \psi}{\Gamma\cb\Theta,\Psi\types \psi}\;\existsE\and
\end{mathpar}
Of course $\existsE$ follows from $\existsL$ plus cut, while we can obtain $\existsL$ from $\existsE$ by taking $\Psi$ to be $(\exists x:A.\ph)$.
Technically, we should actually add some additional premises to $\existsE$ and $\existsL$ to ensure that $\psi$ and $\Theta$ are defined in context $\Gamma$ rather than $\Gamma,x:A$, since otherwise the premises would permit the latter.
Otherwise we would not want to let ourselves write $\Gamma\cb\Theta\types \psi$ (with $x$ not appearing in $\Gamma$, as implied by our conventions and the fact that in a premise we wrote $\Gamma,x:A$).
Thus we ought to write them as
\begin{mathpar}
\inferrule{\Gamma \types \psi\prop \\
\Gamma \types \Theta \ctx \\
\Gamma,x:A\cb\Theta,\ph\types \psi
}{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}\;\existsL\and
\inferrule{\Gamma \types \psi\prop \\
\Gamma \types \Theta \ctx \\
\Gamma\cb\Psi\types \exists x:A.\ph \\
\Gamma,x:A\cb\Theta,\ph\types \psi
}{\Gamma\cb\Theta,\Psi\types \psi}\;\existsE\and
\end{mathpar}
where $\Gamma \types \Theta \ctx$ is an abbreviation for
\[ \Gamma \types B_1\prop \quad\cdots\quad \Gamma\types B_n \prop\]
if $\Theta =(B_1,\dots, B_n)$.
However, we often neglect to write such conditions explicitly.
Finally, now that we have $\existsE/\existsL$, we note that $\existsI$ can also be reformulated in a couple of other ways:
\begin{mathpar}
\inferrule{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}{\Gamma,x:A\cb\Theta,\ph\types \psi}\;\existsL^{-1}\and
\inferrule{ }{\Gamma,x:A\cb\ph\types \exists x:A.\ph}\;\existsS
\end{mathpar}
These are both inter-derivable with $\existsI$ in the presence of cut and substitution.
For instance, we can deduce $\existsS$ as a special case of $\existsI$:
\begin{mathpar}
\inferrule*[right=$\existsI$]{\inferrule*{ }{\Gamma,x:A\types x:A} \\
\inferrule*{ }{\Gamma,x:A\cb\ph\types \ph}}
{\Gamma,x:A\cb \ph\types \exists x:A.\ph}
\end{mathpar}
We can get $\existsL^{-1}$ from $\existsS$ and cut:
\begin{mathpar}
\inferrule*[right=cut]{
\inferrule*[right=$\existsS$]{ }{\Gamma,x:A\cb\ph\types \exists x:A.\ph}\\
\inferrule*[Right=weak]{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}{\Gamma,x:A\cb\Theta,(\exists x:A.\ph)\types \psi}
}{\Gamma,x:A\cb\Theta,\ph\types \psi}
\end{mathpar}
And we can get $\existsI$ by substituting and cutting in $\existsL^{-1}$:
\begin{mathpar}
\inferrule*[right=cut]{
\Gamma\cb\Theta\types \ph[M/x]\\
\inferrule*[Right=sub]{\Gamma\types M:A \\
\inferrule*[Right=$\existsL^{-1}$]{\Gamma\cb (\exists x:A.\ph)\types \exists x:A.\ph}{\Gamma,x:A\cb\ph\types \exists x:A.\ph
}}{\Gamma\cb \ph[M/x] \types \exists x:A.\ph}
}{\Gamma\cb\Theta\types \exists x:A.\ph}
\end{mathpar}
Thus, we are free to use whichever of these rules is most convenient.
To make substitition and cut admissible in \cref{sec:fol-natded} we will use $\existsE$ and $\existsI$, while for categorical semantics in \cref{sec:hyperdoctrines} we will use $\existsL$ and $\existsL^{-1}$.
\subsection{Equality}
\label{sec:equality}
The third and last new operation on propositions is perhaps the subtlest of all: the \emph{equality proposition}.
Its formation rule is unsurprising: it says that for any two terms of the same type, we can consider the proposition that they are equal.
\begin{mathpar}
\inferrule{\Gamma\types M:A \\ \Gamma\types N:A}{\Gamma\types (M =_A N)\prop}
\end{mathpar}
(The subscript annotation $A$ in $M=_A N$ is needed for type-checking; but as usual, we will often omit it.)
But how are we to describe its behavior?
The most classical approach to equality is to assert that it is reflexive, symmetric, transitive, and ``substitutive'' (i.e.\ if $\ph[M/x]$ and $M=N$, then also $\ph[N/x]$).
This is very much like how we described the equality \emph{judgment} $M\equiv N$ in \cref{chap:unary,chap:simple}.
It works here too, but it doesn't fit the general introduction/elimination pattern of natural deduction, and therefore its categorical semantics are not as obvious.
It is one of the great insights of Lawvere~\cite{lawvere:comprehension} (presaged by Leibniz, and approximately contemporaneous with a similar observation by Martin-L\"of) that the rules of reflexivity, symmetry, transitivity, and substitutivity are equivalent to the following pair of rules:
\begin{mathpar}
\inferrule{ }{\Gamma,x:A\cb \ec\types (x =_A x)}\;\eqR\and
\inferrule{\Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]}{\Gamma,x:A,y:A\cb\Theta,(x =_A y)\types \ph}\;\eqL
% \inferrule*{\Gamma\types M:A \\ \Gamma\types N:A \\\\ \Gamma,x:A,y:A\types \ph\prop \\ \Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]}{\Gamma\cb\Theta[M/x,N/y],(M=_A N)\types \ph[M/x,N/y]}
\end{mathpar}
The first, right/introduction, rule is simply reflexivity.
When combined with a substitution (to make substitution into entailment admissible) it becomes
\begin{equation}\label{eq:refl-withsub}
\inferrule{\Gamma\types M:A}{\Gamma\cb \ec\types (M=_A M)}
\end{equation}
and if we have weakening, we can more generally derive $\Gamma\cb \Theta\types (M=_A M)$ for any proposition context $\Theta$.
The left rule is the tricky one to understand.
Intuitively, it says that if we have a statement about $x$ and $y$, and that statement becomes true when we substitute $x$ for $y$, then that statement is true under the hypothesis that $x=y$.
More generally, we can replace the truth of a statement with the truth of an entailment $\Theta\types \ph$, where we also substitute $x$ for $y$ in $\Theta$ in the premise.
In other words, \emph{if we have a hypothesis that $x=y$, then we may as well write $x$ instead of $y$ everywhere that it appears}.
To help motivate this rule further, let us derive symmetry and transitivity from it.
Here is symmetry:
\begin{mathpar}
\inferrule*{x:A,y:A \types (y=_A x) \prop \\ \inferrule*{ }{x:A \cb \ec \types (x=_A x)}}{x:A,y:A \cb (x=_A y) \types (y=_A x)}
\end{mathpar}
We use the left rule once, with $\ph$ being $y=_A x$, so that $\ph[x/y]$ is $x=_A x$, which we can prove by reflexivity.
And here is transitivity:
\begin{mathpar}
\inferrule*{
x:A,y:A,z:A \types (x=_A z) \prop \\
\inferrule*{ }{x:A,y:A \cb (x=_A y) \types (x=_A y)}
}{x:A,y:A,z:A \cb (x=_A y),(y=_A z) \types (x=_A z)}
\end{mathpar}
We again use the left rule once on the hypothesis $y=_A z$, with $\ph$ being $x=_A z$, so that $\ph[y/z]$ is $x=_A y$, which we can prove by the identity rule from the other hypothesis.
Note that both symmetry and transitivity are \emph{derivable rules} in the sense of \cref{rmk:admissible-derivable-1}.
As with so many things, the only way to really understand this rule is to practice it.
We recommend the reader try their hand at \cref{ex:equality}.
There are a few more technical things to be said about $\eqL$.
Firstly, like $\existsL$, it should technically have additional premises making clear what $\Theta$ and $\ph$ are:
\begin{mathpar}
\inferrule{
\Gamma,x:A,y:A \types \ph\prop\\
\Gamma,x:A,y:A \types \Theta\ctx\\
\Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]
}{\Gamma,x:A,y:A\cb\Theta,(x =_A y)\types \ph}
\end{mathpar}
Secondly, to make substitution into entailments admissible, it needs substitutions for $M$ and $N$ built in:
\begin{equation}\label{eq:J-withsub}
\inferrule{
\Gamma,x:A,y:A \types \ph\prop\\
\Gamma,x:A,y:A \types \Theta\ctx\\\\
\Gamma\types M:A\\
\Gamma\types N:A\\
\Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]
}{\Gamma\cb\Theta[M/x,N/y],(M =_A N)\types \ph[M/x,N/y]}
\end{equation}
Thirdly, to make cut for propositions admissible, it needs another cut built in as well; see \cref{fig:fol-natded}.
\subsection{First-order theories}
\label{sec:first-order-theories}
The last thing we need is some ``generator'' rules that would allow us to speak of a ``first-order theory''.
In addition to our multigraph \cG giving the base types and terms, we would like to also have a set \cP of ``base propositions'' (usually called \textbf{atomic propositions}).
Each of these should have an assigned \emph{type context}, i.e.\ a list of objects of \cG; we write $\cP(A_1,\dots,A_n)$ for the set of atomic propositions with context $A_1,\dots,A_n$.
Then we will have a generator rule for propositions, with substitutions built in just like the generator rule for terms:
\[ \inferrule{P\in \cP(A_1,\dots,A_n) \\ \Gamma\types M_1:A_1 \\ \cdots\\ \Gamma\types M_n:A_n}{\Gamma \types P(M_1,\dots,M_n) \prop} \]
\begin{rmk}
Note that while we write $\ph$ for a \emph{generic} proposition that might contain a variable $x$, and $\ph[M/x]$ for the result of substituting $M$ for that variable $x$, if $P$ is an \emph{atomic} proposition we write $P(x)$ and $P(M)$ for its instantiations at a variable $x$ or a more general term $M$.
As always, substitution $\ph[M/x]$ is an \emph{operation} on propositions; while the application $P(M)$ is, like the application of a function symbol $f(M)$, a \emph{primitive} part of syntax.
The relationship between them is that $(P(x))[M/x]$ is, by definition, $P(M)$ (see \cref{thm:fol-subprop-adm}).
\end{rmk}
\begin{rmk}
There is a substantial tradition of terminology according to which the phrase \emph{atomic proposition} includes not just these ``generating'' propositions, but also \emph{equality} propositions $(M=_A N)$.
This is entirely understandable historically, since when equality is presented using laws such as reflexivity, symmetry, transitivity, and substitution it appears ``axiomatic'' rather than governed by principled rules like those of the connectives and quantifiers.
However, from a modern (i.e.\ post-Lawvere~\cite{lawvere:comprehension}) perspective, we can see that the rules $\eqR$ and $\eqL$ have the same shape as those of the other connectives and quantifiers, and in \cref{sec:hyperdoctrines} we will see that they similarly express a categorical universal property.
Thus, it makes much more sense to call the equality rules \emph{logical}, like those of the connectives and quantifiers, and restrict the adjective \emph{atomic} to the generating propositions.
\end{rmk}
Finally, we should have some generating entailments, i.e.\ \emph{axioms}.
Each of these should have an assigned type context $A_1,\dots,A_n$, an assigned proposition context $\Theta$, and an assigned consequent $\ph$.
Here $\ph$ and the elements of $\Theta$ should be propositions in context $x_1:A_1,\dots,x_n:A_n$ --- not just atomic propositions, but arbitrary ones derivable from the atomic ones and the rules for making new propositions.
If we write $\cA(A_1,\dots,A_n;\Theta;\ph)$ for the assertion that there is such an axiom, then simplest form of the generator rule introducing axioms will be
\[ \inferrule{\cA(A_1,\dots,A_n;\Theta;\ph)}{x_1:A_1,\dots,x_n:A_n \cb \Theta \types \ph} \]
To make substitution into entailments admissible, we should build one in:
\[ \inferrule{\cA(A_1,\dots,A_n;\Theta;\ph) \\ \Gamma\types M_1:A_1 \\ \cdots\\ \Gamma\types M_n:A_n}{\Gamma \cb \Theta[\vec M/\vec x]\types \ph[\vec M/\vec x]} \]
And to make cut admissible we should also build in a cut; see \cref{fig:fol-natded}.
With this rule added to the other rules for entailment, we complete the definition of \textbf{intuitionistic first-order \fS-logic}.
If we include both weakening and contraction, we speak simply of \textbf{intuitionistic first-order logic}, while other values of \fS have appropriate names like \textbf{intuitionistic first-order linear logic} (no weakening or contraction) and so on.
A \textbf{first-order theory} in any such logic consists of all the generating data:
\begin{enumerate}
\item A set of \emph{objects} (also called \emph{types} or \emph{sorts});
\item A set of \emph{morphisms} (also called \emph{function symbols}), each with a list of objects as its domain and a single object as its codomain;
\item A set of \emph{atomic propositions} (also called \emph{predicates} or \emph{relation symbols}), each with a list of objects as its domain or arity; and
\item A set of \emph{axioms}, each consisting of a type context, a proposition context, and a consequent.
\end{enumerate}
The qualifier ``intuitionistic'' is because, like in \cref{sec:logic}, we cannot prove the law of excluded middle $\ph\join\neg\ph$ (where $\neg\ph$ means $\ph\hom\bot$), or its equivalent the law of double negation $\neg\neg\ph\hom\ph$.
In \cref{sec:logic} we motivated this by noting that leaving it out just means our ``logic'' has models in all Heyting algebras rather than just Boolean algebras.
We will be able to say something similar, and hopefully even more convincing, about first-order logic in \cref{sec:subobjects}.
A few important subsystems of intuitionistic first-order logic that will reappear later are:
\begin{itemize}
\item \emph{Coherent} logic: includes $\meet,\top,\join,\bot,\exis,=$ but not $\To$ or $\all$ (hence also not $\neg$).
\item \emph{Regular} logic: includes $\meet,\top,\exis,=$ but not $\join,\bot,\To,\neg,\all$.
\item \emph{Horn} logic: includes $\meet,\top,=$ but not $\join,\bot,\To,\neg,\all,\exis$.
% \item \emph{Left-exact} or \emph{finite-limit} logic: includes $\meet,\top,=$ but not $\join,\bot,\To,\all,\exis$. [TODO: Need a restricted form of $\exis$, which ties a partial knot between propositions and entailment, hence including substitution too.]
\item Another important logic is \emph{geometric} logic, which is like coherent logic but also includes the ``infinitary disjunction'' from \cref{ex:frames}.
\item In \cref{sec:lex-theories} we will study a somewhat more complicated logic to define called \emph{finite-limit} or \emph{lex logic}.
\end{itemize}
\subsection{Natural deduction}
\label{sec:fol-natded}
At last we are ready to consider admissibility of substitution and cut.
To be precise, we work with the \textbf{natural deduction for first-order intuitionistic \fS-logic} consisting of:
\begin{enumerate}
\item The rules for forming terms and propositions; % summarized in \cref{fig:fol-common};
\item The exchange and possibly (depending on \fS) weakening and contraction rules;
\item The identity rule $\Gamma \cb \ph\types\ph$ for all propositions $\ph$;
\item The natural deduction rules for intuitionistic \fS-logic from \cref{fig:natded-logic}, with an arbitrary type context $\Gamma$; and
\item The natural deduction rules for quantifiers, equality, and axioms summarized in \cref{fig:fol-natded}.
\end{enumerate}
Of course, the modularity of type theory means we can mix and match these rules, removing any number of type operations and their corresponding rules without altering the main theorems.
%(In particular, in the cartesian case we generally omit $\tensor$ and $\one$, since they are isomorphic to $\meet$ and $\top$.)
% \begin{figure}
% \centering
% \begin{mathpar}
% \inferrule{\types A\type \\ (x:A)\in \Gamma}{\Gamma\types x:A}\and
% \inferrule{f\in \cG(A_1,\dots,A_n;B) \\ \Gamma\types M_1:A_1 \\ \cdots \\ \Gamma \types M_n:A_n}{\Gamma\types f(M_1,\dots,M_n):B}\and
% \inferrule{P\in \cP(A_1,\dots,A_n) \\ \Gamma\types M_1:A_1 \\ \cdots\\ \Gamma\types M_n:A_n}{\Gamma \types P(M_1,\dots,M_n) \prop}\and
% \inferrule{\Gamma\types \ph\prop \\ \Gamma\types\psi\prop}{\Gamma\types (\ph\tensor\psi)\prop}\and
% \inferrule{ }{\Gamma\types \one\prop}\and
% \inferrule{\Gamma\types \ph\prop \\ \Gamma\types\psi\prop}{\Gamma\types (\ph\meet\psi)\prop}\and
% \inferrule{ }{\Gamma\types \top\prop}\and
% \inferrule{\Gamma\types \ph\prop \\ \Gamma\types\psi\prop}{\Gamma\types (\ph\join\psi)\prop}\and
% \inferrule{ }{\Gamma\types \bot\prop}\and
% \inferrule{\Gamma\types \ph\prop \\ \Gamma\types\psi\prop}{\Gamma\types (\ph\hom\psi)\prop}\and
% \inferrule{\Gamma,x:A\types \ph\prop}{\Gamma\types (\forall x:A.\ph) \prop}\and
% \inferrule{\Gamma,x:A\types \ph\prop}{\Gamma\types (\exists x:A.\ph) \prop}\and
% \inferrule{\Gamma\types M:A \\ \Gamma\types N:A}{\Gamma\types (M =_A N)\prop}
% \end{mathpar}
% \caption{Rules for terms and propositions}
% \label{fig:fol-common}
% \end{figure}
\begin{figure}
\centering
\begin{mathpar}
\inferrule{\Gamma,x:A\cb \Theta\types \ph}{\Gamma\cb\Theta\types \forall x:A.\ph}\;\forallI\and
\inferrule{\Gamma\types M:A \\ \Gamma\cb\Theta\types \forall x:A.\ph}{\Gamma\cb\Theta\types \ph[M/x]}\;\forallE\and
\inferrule{\Gamma\types M:A \\ \Gamma\cb\Theta\types \ph[M/x]}{\Gamma\cb\Theta\types \exists x:A.\ph}\;\existsI\and
\inferrule{\Gamma\cb\Psi\types \exists x:A.\ph \\ \Gamma,x:A\cb\Theta,\ph\types \psi}{\Gamma\cb\Theta,\Psi\types \psi}\;\existsE\and
\inferrule{\Gamma\types M:A}{\Gamma\cb \ec\types (M=_A M)}\;\eqI\and
\inferrule{
\Gamma\types M:A\\
\Gamma\types N:A\\
\Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]\\
\Gamma\cb \Psi \types (M =_A N)\\
\Gamma\cb \Phi\types \Theta[M/x,N/y]
}{\Gamma\cb\Phi,\Psi\types \ph[M/x,N/y]}\;\eqE\and
\inferrule{\cA(A_1,\dots,A_n;\Theta;\ph) \\
\Gamma\types M_1:A_1 \\ \cdots\\ \Gamma\types M_n:A_n \\
\Gamma \cb \Phi \types \Theta[\vec M/\vec x]
}{\Gamma \cb \Phi \types \ph[\vec M/\vec x]}\;\textsc{axiom}
\end{mathpar}
\caption{Natural deduction rules for quantifiers, equality, and axioms}
\label{fig:fol-natded}
\end{figure}
Note also that the rules $\eqE$ and \textsc{axiom} from \cref{fig:fol-natded} incorporate an additional cut on the left.
For this we have used a shortcut notation $\Gamma\cb \Phi\types \Theta$ where $\Theta$ is a proposition context, meaning that $\Gamma\cb \Phi\types \theta$ for each $\theta\in\Theta$.
We start with the admissibility of substitution into propositions.
\begin{thm}\label{thm:fol-subprop-adm}
Substitution into propositions is admissible: given derivations of $\Gamma,x:A \types \ph\prop$ and $\Gamma\types M:A$, we can construct a derivation of $\Gamma \types \ph[M/x]\prop$.
\end{thm}
\begin{proof}
As with substitution into terms, this is entirely straightforward because we have written all the rules for such judgments with an arbitrary type context.
Some of the defining clauses are
\begin{align*}
(\ph\meet\psi)[M/x] &= \ph[M/x] \meet \psi[M/x]\\
(\forall y:B.\ph)[M/x] &= \forall y:B.\ph[M/x]\\
(N=_B P)[M/x] &= (N[M/x] =_B P[M/x])
\end{align*}
In the case of $\all$ (and also $\exis$), we have to ensure (by $\alpha$-equivalence if necessary) that $x$ and $y$ are distinct variables, and that $y$ does not occur in $M$.
This is the same issue that arose in \cref{sec:multicat-moncat,sec:multicat-prod-coprod,sec:stlc} when substituting into terms with bound variables such as $\case$ and $\lambda$-abstractions.
As always, this is only an issue when representing derivations by terms; the underlying operation on derivations has no notion of ``bound variable''.
Note also that substitution into an equality \emph{proposition} is defined using substitution into the \emph{terms} appearing in it.
But since terms never involve propositions, there is no cyclic dependency: we can first prove the admissibility of substitution into terms, and then use it to prove the admissibility of substitution into propositions.
\end{proof}
Just as substitution into terms is associative, substitution into propositions satisfies as ``functoriality'' property that can be proven in the same way:
\begin{equation}
\ph[N/y][M/x] = \ph[M/x][N[M/x]/y]\label{eq:fol-subprop-funct}
\end{equation}
\begin{thm}\label{thm:fol-subent-adm}
% If we avoid the rules $\forallI^{-1}$, $\existsL^{-1}$, and $\existsS$, and formulate the equality rules with substitutions as in~\eqref{eq:refl-withsub} and~\eqref{eq:J-withsub}, then
Substitution into entailments is admissible: if we have derivations of $\Gamma,x:A \types\Theta\types \ph$ and $\Gamma\types M:A$, we can construct a derivation of $\Gamma \types \Theta[M/x] \types \ph[M/x]$.
\end{thm}
\begin{proof}
Just like \cref{thm:fol-subprop-adm}, we substitute recursively into the derivation of $\Gamma,x:A \types\Theta\types \ph$.
This works because all the entailment rules have a fully general type context in the conclusion, so the substitution can always be done inductively on their premises.
\end{proof}
\begin{thm}\label{thm:fol-natded-cutadm}
Cut for propositions is admissible in the natural deduction for first-order intuitionistic \fS-logic: given derivations of $\Gamma\cb\Theta \types \ph$ and $\Gamma\cb \Psi,\ph\types \psi$, we can construct a derivation of $\Gamma\cb \Psi,\Theta\types \psi$.
\end{thm}
\begin{proof}
As usual, we induct on the derivation of $\Gamma\cb \Psi,\ph\types \psi$.
This works because all the rules for the natural deduction have a fully general proposition context in the conclusion as well.
\end{proof}
As in \cref{sec:heyting-algebras}, in the cartesian case we can make exchange, weakening, and contraction admissible as well, by reformulating the rules $\existsE$ and $\eqE$ to keep the same proposition context in the premises and the conclusion, and the identity rule to incorporate a weakening.
The reformulated rules are
\begin{mathpar}
\inferrule{\Gamma\cb\Theta\types \exists x:A.\ph \\ \Gamma,x:A\cb\Theta,\ph\types \psi}{\Gamma\cb\Theta\types \psi}\;\existsE'\and
\inferrule{
\Gamma\types M:A\\
\Gamma\types N:A\\
\Gamma,x:A\cb\Phi,\Theta[x/y]\types \ph[x/y]\\
\Gamma\cb \Phi \types (M =_A N)\\
\Gamma\cb \Phi\types \Theta[M/x,N/y]
}{\Gamma\cb\Phi\types \ph[M/x,N/y]}\;\eqE'\and
\end{mathpar}
We leave the proof to the reader (\cref{ex:fol-natded-strucadm}).
Recall also from \cref{sec:natded-logic} that with such a reformulation, the natural deduction of intuitionistic propositional logic can be formulated without explicit contexts, instead ``discharging'' temporary assumptions by crossing them out.
The same is true for intuitionistic first-order logic with $\all$ and $\exis$, if we also allow ``assumptions of variables'' (i.e.\ additions to the type context, in addition to the proposition context, as we move up the derivation tree) that can be discharged by the quantifier rules.
Usually we do not bother to include the derivations of term judgments in this style; we just write down the terms wherever they are needed.
For instance, here is a derivation in this style of the intuitionistic tautology $\ph\meet (\exists x:A.\psi) \To (\exists x:A. (\ph\meet\psi))$.
\[
\inferrule*[Right=$\ToI$]{
\inferrule*[Right=$\existsE'$]{
\inferrule*{\cancel{\ph\meet (\exists x:A.\psi)}}{\exists x:A.\psi}\\
\inferrule*[Right=$\existsI$]{
\cancel{x:A}\\
\inferrule*[Right=$\meetI$]{
\inferrule*{\cancel{\ph\meet (\exists x:A.\psi)}}{\ph} \\
\cancel{\psi}
}{\ph \meet \psi}
}{\exists x:A. (\ph\meet\psi)}}{\exists x:A. (\ph\meet\psi)}
}{\ph\meet (\exists x:A.\psi) \To (\exists x:A. (\ph\meet\psi))}
\]
% (In a perhaps-misguided attempt at clarification, we have used a different variable for one of the quantifiers.)
% \[
% \inferrule*[right=$\alpha$-equiv]{
% \inferrule*[Right=$\ToI$]{
% \inferrule*[Right=$\existsE'$]{
% \inferrule*{\cancel{\ph\meet (\exists x:A.\psi)}}{\exists x:A.\psi}\\
% \inferrule*[Right=$\existsI$]{
% \cancel{x:A}\\
% \inferrule*[Right=$\meetI$]{
% \inferrule*{\cancel{\ph\meet (\exists x:A.\psi)}}{\ph} \\
% \inferrule*[Right=i.e.]{\cancel{\psi}}{\psi[y/x][x/y]}
% }{\ph \meet \psi[y/x][x/y]}
% }{\exists x:A. (\ph\meet\psi[y/x])}}{\exists y:A. (\ph\meet\psi[y/x])}
% }{\ph\meet (\exists x:A.\psi) \To (\exists y:A. (\ph\meet\psi[y/x]))}}
% {\ph\meet (\exists x:A.\psi) \To (\exists x:A. (\ph\meet\psi))}
% \]
The hypotheses $x:A$ and $\psi$ are discharged by the $\existsE'$, while the two occurences of the hypothesis ${\ph\meet (\exists x:A.\psi)}$ are discharged by the $\ToI$.
Without too much stretch, this can be regarded as a direct formalization of the following informal English proof:
\begin{quote}
Suppose ${\ph\meet (\exists x:A.\psi)}$, that is $\ph$ and $\exists x:A.\psi$.
By the latter, we may assume given an $x$ such that $\psi$.
Now we have both $\ph$ and $\psi$, so $\ph\meet\psi$.
Thus, $\exists x:A. (\ph\meet\psi)$, and so we have $\ph\meet (\exists x:A.\psi) \To (\exists x:A. (\ph\meet\psi))$.
\end{quote}
\begin{rmk}
In particular, this (nontrivial!)\ interaction between $\meet$ and $\exis$ is, like the distributive law of $\meet/\tensor$ over $\join$ from \cref{ex:monpos-jslat} and \cref{sec:logic}, implied automatically by the structure of our contexts and how they interact with the rules for $\meet$ and $\exis$.
There is sometimes a temptation to ``simplify'' logic by presenting it as a unary type theory, arguing that a context $\Theta = (\ph_1,\dots,\ph_n)$ can always be replaced by the conjunction $\ph_1\meet \dots \meet\ph_n$, and perhaps even replacing entailments $\ph\types \psi$ by implications $\ph\To\psi$.
This is technically possible, but it forces one to assert laws like these ``by hand'', breaking principle~\eqref{princ:independence} and making for a less congenial theory.
More importantly, as remarked in \cref{sec:natded-logic}, allowing arbitrarily many propositions in the context yields a formal theory that matches informal reasoning much better: as in the example above, informally we frequently apply inference rules in the presence of other unaffected hypotheses.
Furthermore, for categorical semantics it is important to maintain the distinction between entialment and implication, since entailment corresponds to a morphism in a category, whereas implication corresponds to an internal-hom in a category.
In particular, the former always exists, but the latter may not.
Phrasing the rules for logical operations such as $\exis$ and $\join$ in a way that matches ordinary reasoning, \emph{and} doesn't refer to any other operations such as $\To$, ensures that ordinary informal (constructive) reasoning can be formalized and remain valid in any category as long as it uses only operations that exist in that category.
This is important because we will see in \cref{sec:subobjects} that certain fairly natural conditions on categories allow them to model some, but not always all, of the logical operations.
\end{rmk}
The rule $\eqE'$ is a bit tricker to write in this style because of the arbitrary context $\Theta$ that has to be substituted into.
One approach is to use \cref{ex:eq-frob-from-hom}, which shows that as long as we also have implication we can get around this.
A more direct approach is to allow the proof of $\ph[x,y]$ to discharge an arbitrary number of hypotheses of the form $\theta[x/y]$, as long as we also supply corresponding proofs of $\theta[M/x,N/y]$ to the rule $\eqE'$.
For instance, with one $\theta$ formula the rule would look like this:
\[
\inferrule*{
\inferrule*{\vdots}{M =_A N}\\
\inferrule*{\vdots}{\theta[M/x,N/y]}\\
\inferrule*{\mprset{fraction={\relax\relax\relax}}\inferrule*{\cancel{x:A} \\ \cancel{\theta[x/y]}}{\vdots}}{\ph[x/y]}
}{\ph[M/x,N/y]}
\]
% \subsection{Sequent calculus}
% \label{sec:fol-seqcalc}
% The rules for \textbf{first-order intuitionistic \fS-sequent-calculus} consist of
% \begin{enumerate}
% \item The standard rules for types, terms, and propositions shown in \cref{fig:fol-common} (omitting $\tensor$ and $\one$ in the cartesian case);
% \item The exchange and possibly (depending on \fS) weakening and contraction rules;
% \item The identity rule $\Gamma \cb \ph\types\ph$ \emph{only when $\ph$ is an instance of an atomic proposition};
% \item The sequent calculus rules for intuitionistic \fS-logic from \cref{sec:seqcalc-logic}, with an arbitrary type context $\Gamma$; and
% \item The sequent calculus rules for quantifiers and equality summarized in \cref{fig:fol-seqcalc}.
% \end{enumerate}
% \begin{figure}
% \centering
% \begin{mathpar}
% \inferrule{\Gamma,x:A\cb \Theta\types \ph}{\Gamma\cb\Theta\types \forall x:A.\ph}\;\forallR\and
% \inferrule{\Gamma\types M:A \\ \Gamma\cb \Theta, \ph[M/x] \types \psi}{\Gamma\cb \Theta, (\forall x:A.\ph) \types \psi}\;\forallL\and
% \inferrule{\Gamma\types M:A \\ \Gamma\cb\Theta\types \ph[M/x]}{\Gamma\cb\Theta\types \exists x:A.\ph}\;\existsR\and
% \inferrule{\Gamma,x:A\cb\Theta,\ph\types \psi}{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}\;\existsL\and
% \inferrule{\Gamma\types M:A}{\Gamma\cb \ec\types (M=_A M)}\;\eqR\and
% \inferrule{
% \Gamma\types M:A\\
% \Gamma\types N:A\\
% \Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]
% }{\Gamma\cb\Theta[M/x,N/y],(M =_A N)\types \ph[M/x,N/y]}\;\eqL\and
% % TODO: What to do with this?
% \end{mathpar}
% \caption{Sequent calculus rules for quantifiers and equality}
% \label{fig:fol-seqcalc}
% \end{figure}
% TODO: Has cut-admissibility for sequent calculus been done using Lawvere's equality rule?
% Need to build in cuts on both sides of axioms in general, as mentioned in \cref{sec:unary-theories}.
\subsection*{Exercises}
\begin{ex}\label{ex:eq-frob-from-hom}
Assuming we have $\hom$, show that the rule $\eqR$ is derivable (recall \cref{rmk:admissible-derivable-1}) from the following simpler rule with no proposition context $\Theta$:
\begin{mathpar}
\inferrule{\Gamma\types M:A \\ \Gamma\types N:A \\\\ \Gamma,x:A,y:A\types \ph\prop \\ \Gamma\types\Theta\ctx \\ \Gamma,x:A\cb\Theta \types \ph[x/y]}{\Gamma\cb\Theta,(M=_A N)\types \ph[M/x,N/y]}
\end{mathpar}
\end{ex}
\begin{ex}\label{ex:quantifier-laws}
Three of the following four sequents are derivable in intuitionistic first-order logic (for any type $A$, context $\Gamma$, and proposition $\Gamma,x:A\types\ph\prop$); derive them.
\begin{align*}
\Gamma \cb \exists x:A. \neg \ph &\types \neg\forall x:A. \ph\\
\Gamma \cb \forall x:A. \neg \ph &\types \neg\exists x:A. \ph\\
\Gamma \cb \neg\forall x:A. \ph &\types \exists x:A. \neg \ph\\
\Gamma \cb \neg\exists x:A. \ph &\types \forall x:A. \neg \ph
\end{align*}
\end{ex}
\begin{ex}\label{ex:equality}
% CREDIT: Peter Lumsdaine
In a first-order theory with
three types $A$, $B$, $C$,
two generating arrows $f : A \to B$ and $g : B \to A$,
one atomic proposition $P$ with domain $(A,B)$, and
no axioms,
derive the following judgments:
\begin{enumerate}
\item $x_1: A, x_2 : A, y : B \cb \ph(x_1,y), (x_1 =_A x_2) \types \ph(x_2,y)$
\item $x_1 : A, x_2 : A \cb (x_1 =_A x_2) \types f(x_1) =_B f(x_2)$
\item $\ec \cb ( \forall x : A. g(f(x)) =_A x ) \types \forall x_1:A. \forall x_2 : A. \left((f(x_1) =_B f(x_2)) \to (x_1 =_A x_2) \right)$
\end{enumerate}
\end{ex}
\begin{ex}\label{ex:fol-egs}
Write down a first-order theory for each of the following structures.
If you can, formulate them so that they fit inside the specified fragment.
\begin{enumerate}
\item Partially ordered sets (Horn)
\item Totally ordered sets (coherent)
\item Fields (coherent)
\item Categories (regular)
\end{enumerate}
\end{ex}
\begin{ex}\label{ex:fol-natded-strucadm}
Prove that in intuitionistic first-order logic with $\existsE$ and $\eqE$ replaced by $\existsE'$ and $\eqE'$ as mentioned at the end of the section, the structural rules of exchange, weakening, and contraction for proposition contexts are admissible.
\end{ex}
\section{First-order hyperdoctrines}
\label{sec:hyperdoctrines}
Now we move on to the categorical semantics of first-order logic.
Continued adherence to principle~\eqref{princ:structural} suggests that the \emph{structural rules}, including for instance the substitution of terms into propositions and entailments, should correspond to basic operations in an appropriate categorical structure.
This would lead us to the following structure.
Let \fS be a faithful cartesian club, and recall from \cref{sec:cartmulti} the notion of \fS-multicategory and \fS-multiposet.
In contrast to \cref{chap:unary,chap:simple}, in this chapter we will assume for simplicity that our multiposets \emph{do} satisfy antisymmetry: if $x\le y$ and $y\le x$ then $x=y$.
Allowing distinct isomorphic objects, while morally correct, would lead us down a 2-categorical road that we prefer to postpone until \cref{sec:indexed-moncat}.
% TODO: A multicategory with a set-valued presheaf is the same as a polycategory with only 0,1-ary targets.
\begin{defn}
Let \cS be a cartesian multicategory and \bC a category.
A \textbf{\bC-valued presheaf on \cS} consists of
\begin{enumerate}
\item For each list $(A_1,\dots,A_n)$ of objects of \cS, an object $\cP(A_1,\dots,A_n)\in\bC$.
\item For each list $(f_1,\dots,f_m)$ of morphisms of \cS, with $f_i:(A_{i1},\dots,A_{in_i})\to B_i$, a morphism in \bC:
\[ (f_1,\dots,f_n)^* : \cP(B_1,\dots,B_m) \to \cP(A_{11},\dots,A_{mn_m}) \]
\item These morphisms are associative and unital with respect to composition in \cS:
\begin{align*}
(f_{11},\dots,f_{mn_m})^* \circ (g_1,\dots,g_m)^* &=
(g_1\circ (f_{11},\dots,f_{1n_1}), \dots, g_m \circ (f_{m1},\dots,f_{mn_m}))^*
\\
(\idfunc_{A_1},\dots,\idfunc_{A_n})^* &= \idfunc_{\cP(A_1,\dots,A_n)}
\end{align*}
\item For each $\sigma : \{1,\dots,m\} \to \{1,\dots,n\}$, a morphism in \bC:
\[ \cP(A_{\sigma 1},\dots,A_{\sigma m}; B) \to \cP(A_1,\dots,A_n;B) \]
satisfying analogues of the axioms in \cref{defn:fS-multicategory}.
\end{enumerate}
\end{defn}
One way to understand the definition is that it is precisely the structure possessed by the contravariant representables: for any object $B$ in a cartesian multicategory \cS, there is a \bSet-valued presheaf $\cS(-;B)$.
Now the categorical structure corresponding to first-order logic should consist of a cartesian multicategory \bS and a presheaf \cP on \bS valued in the category of \fS-multiposets.
The objects and morphisms of \cS represent the types and terms, respectively; while
the objects of $\cP(A_1,\dots,A_n)$ represent the propositions in context $(A_1,\dots,A_n)$ and its morphisms/inequalities represent the entailments in that same context.
Composition in \cS represents substitution into terms, composition in each $\cP(A_1,\dots,A_n)$ represents the cut rule for propositions, and the functorial action of \cP represents substitution of terms into propositions and entailments.
However, in addition to being nonstandard, this structure is rather unnecessarily complicated.
It can be simplified greatly by the following observation, whose proof we leave to the reader (\cref{ex:pshf-multi-catprod}).
\begin{lem}\label{thm:pshf-multi-catprod}
\bC-valued presheaves on a cartesian multicategory \cS are equivalent to ordinary \bC-valued presheaves on the category with finite products freely generated by \cS as in \cref{thm:free-catprod-cartmulti}.\qed
\end{lem}
Moreover, in practice we rarely care about semantics in cartesian multicategories that do not arise from categories with products.
Thus, we retreat slightly from the principled position of \cref{chap:simple}, and simplify our lives by taking the base \cS to be a category with products rather than a cartesian multicategory throughout.
This leads to the following definition.
\begin{defn}
An \textbf{\cS-indexed \fS-multiposet} is a functor \cP from $\cS\op$ to the category of \fS-multiposets.
\end{defn}
Since we do not include product types in our base theory, this means that the free structure generated from a first-order logic will involve the \emph{category of contexts} introduced in \cref{sec:fp-theories}, and possess a universal property only up to equivalence.
For this reason we will often use letters like $\Gamma,\Delta$ for objects of \cS.
Thus, in this definition we have categorical counterparts of the type contexts (objects of \cS), terms (morphisms of \cS), substitution into terms (composition in \cS), propositions (objects of $\cP(\Gamma)$), entailments (morphisms of $\cP(\Gamma)$), cut for propositions (composition in $\cP(\Gamma)$), and substitution of terms into propositions and entailments (the functorial action of $\cP$).
In general for a morphism $f:\Gamma\to\Delta$ in \cS, we write $f^* : \cP(\Delta) \to \cP(\Gamma)$ for this latter action and call it a \textbf{reindexing} or \textbf{substitution} functor.
The propositional operations imported from \cref{sec:logic} are also easy to describe categorically.
\begin{defn}\label{defn:hyperdoctrine-heyting-fibers}
Let \cP be an \cS-indexed \fS-multiposet.
We say that \cP has \textbf{products}, \textbf{coproducts}, is \textbf{representable}, or is \textbf{closed}, if each \fS-multiposet $\cP(\Gamma)$ has the corresponding structure, and that structure is preserved by the reindexing functors $f^*$.
\end{defn}
We did not define formally in \cref{sec:multicats-catth} what it means for a functor to preserve all these properties of a multicategory, but we trust the reader can do it.
The requirement that $f^*$ preserve these properties is necessary because substitution in type theory does, by definition, preserve the type operations: $(\ph\meet\psi)[M/x] = (\ph[M/x] \meet \psi[M/x])$ and so on.
Thus, in the free structure built from type theory the reindexing functors do preserve all the relevant structure, so we can't hope for it to be initial except in a world where that structure is always preserved.
Of course, one may naturally wonder, where do indexed multiposets with these properties come from?
We will consider this question in more depth in \cref{sec:subobjects}, but here are three fundamental examples to help the intuition.
\begin{eg}\label{eg:subset-hyperdoctrine}
Let $\cS=\bSet$ be the category of sets, and define $\cP(\Gamma)$ to be the poset of subsets of the set $\Gamma$, with its cartesian multiposet structure.
The latter is in fact a Heyting algebra, and moreover a Boolean algebra: $\meet$ is intersection, $\join$ is union, $\neg$ is complement.
\end{eg}
\begin{eg}\label{eg:subobject-hyperdoctrine}
Let \cS be any category with finite limits, and define $\cP(\Gamma)$ to be the poset of subobjects of $\Gamma$, i.e.\ isomorphism classes of monomorphisms with codomain $\Gamma$.
The reindexing functors are given by pullback.
When $\cS=\bSet$, this reproduces \cref{eg:subset-hyperdoctrine} up to isomorphism.
In general, we need more structure on \cS to ensure that this \cP has the structure of \cref{defn:hyperdoctrine-heyting-fibers}; we will study this question in \cref{sec:subobjects}.
\end{eg}
\begin{eg}\label{eg:family-hyperdoctrine}
Let $H$ be any complete Heyting algebra, let $\cS=\bSet$, and define $\cP(\Gamma) = H^\Gamma$, the poset of $\Gamma$-indexed families $\{h_i\}_{i\in\Gamma}$ of objects of $H$.
The Heyting algebra operations on $H$ applied pointwise (e.g. $\{h_i\}_{i\in\Gamma} \meet \{k_i\}_{i\in\Gamma}= \{h_i\meet k_i\}_{i\in\Gamma}$) make $\cP(\Gamma)$ a Heyting algebra as well.
Note that when $H=\tv$, this again reproduces \cref{eg:subset-hyperdoctrine} up to isomorphism.
\end{eg}
It remains to consider categorical analogues of the quantifiers and equality.
Lawvere's fundamental insight~\cite{lawvere:adjointness,lawvere:comprehension} was that these correspond categorically to \emph{adjoint functors}.
Consider, for instance, the universal quantifier.
We saw in \cref{sec:forall} that its rules could be given as the following pair:
\begin{mathpar}
\inferrule{\Gamma,x:A\cb \Theta\types \ph}{\Gamma\cb\Theta\types \forall x:A.\ph}\;\forallI\and
\inferrule{\Gamma\cb\Theta\types \forall x:A.\ph}{\Gamma,x:A\cb\Theta\types \ph}\;\forallI^{-1}
\end{mathpar}
which are clearly inverses to each other.
Categorically, they say that to have a morphism from $\Theta$ to $\forall x:A.\ph$ in $\cP(\Gamma)$ is equivalent to having a morphism from $\Theta$ to $\ph$ in $\cP(\Gamma,A)$.
Here the second $\Theta$ technically denotes the weakening of $\Theta$ to the context $\Gamma,x:A$, which categorically will be the functorial action of $\cP$ applied to the projection $(\Gamma,A)\to \Gamma$.
Note that the latter is one of the projections of a cartesian product in the category of contexts.
This leads to the following definition.
\begin{defn}\label{defn:multicat-radj}
Let $F:\cM\to\cN$ be a functor of \fS-multicategories.
We say it \textbf{has a right adjoint} if for each object $B\in\cN$ there is an object $GB\in\cM$ and a morphism $\ep_B:FGB\to B$ in \cN such that for any $A_1,\dots,A_n\in \cM$, the composite
\[ \cM(A_1,\dots,A_n;GB) \xto{F} \cN(FA_1,\dots,FA_n;FGB) \xto{\ep_B\circ -} \cN(FA_1,\dots,FA_n;B) \]
is a bijection.
\end{defn}
The case $n=1$ of this definition implies immediately that the underlying ordinary functor of $F$ has a right adjoint in the usual sense.
Conversely, in the case when \cM and \cN are representable, it is sufficient to have such an underlying adjoint together with the fact that $F$ preserves tensor products; see \cref{ex:moncat-radj}.
Moreover, if $G$ exists, it can be made into a functor $\cN\to\cM$, that is right adjoint to \cM in an appropriate 2-category of \fS-multicategories; see \cref{ex:multicat-radj}.
We need one more thing for a categorical analogue of $\all$: we need to know that this structure is ``preserved by the reindexing functors'' in an appropriate sense.
The appropriate sense is the following.
\begin{defn}\label{defn:bc}
Let \cS be a category, let $\cP:\cS\op\to\bCat$ be a functor, and suppose we have a commutative square in \cS:
\[ \xymatrix{ A \ar[r]^h \ar[d]_f & C \ar[d]^g \\ B \ar[r]_k & D. } \]
Suppose furthermore that the functors $f^*:\cP(B) \to \cP(A)$ and $g^*:\cP(D) \to\cP(C)$ have right adjoints $f_*$ and $g_*$.
We say that \cP satisfies the \textbf{right Beck--Chevalley condition} with respect to this square (or sometimes that the square satisfies the Beck-Chevalley condition with respect to \cP) if the composite natural transformation
\[ k^* g_* \xto{\eta k^* g_*} f_* f^* k^* g_* = f_* h^* g^* g_* \xto{f_* h^* \ep} f_* h^* \]
is an isomorphism.
Dually, if $f^*$ and $g^*$ have left adjoints $f_!$ and $g_!$, we say \cP satisfies the \textbf{left Beck--Chevalley condition} with respect to the above square if the composite
\[ f_! h^* \xto{f_! h^* \eta} f_! h^* g^* g_! = f_! f^* k^* g_! \xto{\ep k^* g_!} k^* g_! \]
is an isomorphism.
\end{defn}
When \cP is an \cS-indexed \fS-multiposet, we apply this definition to its underlying functor into posets (regarded as categories).
Since our posets are antisymmetric, every isomorphism is an equality, and so in this case we have $k^* g_* = f_* h^*$ (or $f_! h^* = k^* g_!$).
Now we can state:
\begin{defn}
An \cS-indexed \fS-multiposet \textbf{has universal quantifiers} if
\begin{enumerate}
\item For any objects $\Gamma,A\in \cS$, the reindexing functor $\cP(\Gamma) \to \cP(\Gamma\times A)$ has a right adjoint in the sense of \cref{defn:multicat-radj}; and
\item For any morphism $f:\Gamma\to\Delta$ and object $A$ in \cS, \cP satisfies the right Beck--Chevalley condition with respect to the square
\[ \xymatrix@C=3pc{ \Gamma\times A \ar[r]^{f\times \idfunc_A} \ar[d] & \Delta\times A \ar[d] \\ \Gamma \ar[r]_f & \Delta. } \]
\end{enumerate}
\end{defn}
Note that the Beck--Chevalley condition is true in the syntax because the universal quantifier is preserved by substitution, by definition of substitution: $(\forall x:A.\ph)[M/y] = \forall x:A. \ph[M/y]$ as long as $y\neq x$.
For the indexed poset of subsets from \cref{eg:subset-hyperdoctrine}, the right adjoint to $(\pi_A)^* : \cP(\Gamma) \to \cP(\Gamma\times A)$ is similarly defined by
\[ (\pi_A)_*(\ph) = \setof{ x\in \Gamma | \all y\in A. (x,y)\in\ph }. \]
Such right adjoints for \cref{eg:subobject-hyperdoctrine} will be studied in \cref{sec:heyting-categories}; while for \cref{eg:family-hyperdoctrine}, they can defined by
\[ (\pi_A)_*\left(\{h_{(i,a)}\}_{(i,a)\in\Gamma\times A}\right) = \left\{\bigwedge_a h_{(i,a)}\right\}_{i\in\Gamma} \]
The existential quantifier is similar, but a bit more subtle.
We saw in \cref{sec:exists} that its rules could be expressed as the pair
\begin{mathpar}
\inferrule{\Gamma,x:A\cb\Theta,\ph\types \psi}{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}\;\existsL\and
\inferrule{\Gamma\cb\Theta,(\exists x:A.\ph)\types \psi}{\Gamma,x:A\cb\Theta,\ph\types \psi}\;\existsL^{-1}\and
\end{mathpar}
which likewise seem to express some kind of adjunction; but there is an extra context $\Theta$ hanging around.
Translating directly across the correspondence to multicategories, this leads to the following definition.
\begin{defn}\label{defn:multicat-hopf-ladj}
Let $G:\cM\to\cN$ be a functor of \fS-multicategories.
We say it \textbf{has a Hopf left adjoint} if for each object $B\in\cN$ there is an object $FB\in\cM$ and a morphism $\eta:B\to GFB$ in \cN such that for any objects $A_1,\dots,A_n,C_1,\dots,C_m,D\in \cM$, the composite
\[ \cM(\vec A,FB,\vec C;D) \xto{G} \cN(G\vec A, GFB, G\vec C; GD) \xto{-\circ_{(n+1)} \eta} \cN(G\vec A, B, G\vec C; GD) \]
is a bijection.
\end{defn}
As before, the case $n=m=1$ implies that the underlying ordinary functor has a left adjoint in the usual sense.
Conversely, when \cM and \cN are representable and $G$ preserves tensor products, an underlying left adjoint is a Hopf left adjoint just when some canonical maps are isomorphisms; see \cref{ex:hopf-ladj}.
Unlike the case of right adjoints, however, in general a Hopf left adjoint cannot be made into a functor of multicategories.
\begin{defn}
An \cS-indexed \fS-multiposet \textbf{has existential quantifiers} if
\begin{enumerate}
\item For any objects $\Gamma$ and $A$ of \cS, the reindexing functor $\cP(\Gamma) \to \cP(\Gamma\times A)$ has a Hopf left adjoint in the sense of \cref{defn:multicat-hopf-ladj}; and
\item For any morphism $f:\Gamma\to\Delta$ and object $A$ in \cS, \cP satisfies the left Beck--Chevalley condition with respect to the square
\[ \xymatrix@C=3pc{ \Gamma\times A \ar[r]^{f\times \idfunc_A} \ar[d] & \Delta\times A \ar[d] \\ \Gamma \ar[r]_f & \Delta. } \]
\end{enumerate}
\end{defn}
Unsurprisingly, for the indexed poset of subsets from \cref{eg:subset-hyperdoctrine}, the left adjoint to $(\pi_A)^*:\cP(\Gamma) \to \cP(\Gamma\times A)$ is similarly defined by
\[ (\pi_A)_!(\ph) = \setof{ x\in \Gamma | \exis y\in A. (x,y)\in\ph }. \]
Left adjoints for \cref{eg:subobject-hyperdoctrine} will be studied in \cref{sec:regular-categories}, while those in \cref{eg:family-hyperdoctrine} can be defined like the right adjoints using joins instead of meets:
\[ (\pi_A)_!\left(\{h_{(i,a)}\}_{(i,a)\in\Gamma\times A}\right) = \left\{\bigvee_a h_{(i,a)}\right\}_{i\in\Gamma} \]
Finally, we consider the rules for equality:
\begin{mathpar}
\inferrule{ }{\Gamma,x:A\cb \ec\types (x=_A x)}\and
\inferrule{\Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]}{\Gamma,x:A,y:A\cb\Theta,(x=_A y)\types \ph}
\end{mathpar}
Although we didn't mention it in \cref{sec:equality}, the first of these rules is equivalent to the opposite of the second, similarly to what happened for the quantifiers.
One direction is immediate:
\begin{mathpar}
\inferrule*{\inferrule*{ }{\Gamma,x:A,y:A\cb(x=_A y)\types (x=_A y)}}{\Gamma,x:A\cb\ec\types (x=_A x)}
\end{mathpar}
while the other uses a cut and a substitution:
\begin{mathpar}
\inferrule*[right=cut]{
\inferrule*{ }{\Gamma,x:A \cb \ec \types x=_A x}\\
\inferrule*[Right=subst $x/y$]{\Gamma,x:A,y:A\cb\Theta,(x=_A y)\types \ph}
{\Gamma,x:A\cb\Theta[x/y],(x=_A x)\types \ph[x/y]}
}{\Gamma,x:A\cb\Theta[x/y]\types \ph[x/y]}
\end{mathpar}
Thus, what we have looks very much like a (Hopf) left adjoint to substitution along the diagonal $(\Gamma,A) \to (\Gamma,A,A)$ in the category of contexts; but there is \emph{no} proposition in context $(\Gamma,A)$ that it is applied to.
This suggests the following definitions.
\begin{defn}\label{defn:multicat-hopf-ladj-empty}
Let $G:\cM\to\cN$ be a functor of \fS-multicategories.
We say it \textbf{has a Hopf left adjoint at $()$} if there is an object $F\in\cM$ and a morphism $\eta:()\to GF$ in \cN such that for any objects $A_1,\dots,A_n,C_1,\dots,C_m,D\in \cM$, the composite
\[ \cM(\vec A,F,\vec C;D) \xto{G} \cN(G\vec A, GF, G\vec C; GD) \xto{-\circ_{(n+1)} \eta} \cN(G\vec A, G\vec C; GD) \]
is a bijection.
\end{defn}
\begin{defn}
Suppose given an \cS-indexed \fS-multiposet \cP and a commutative square in \cS:
\[ \xymatrix{ A \ar[r]^h \ar[d]_f & C \ar[d]^g \\ B \ar[r]_k & D, } \]
and suppose that the reindexing functors $f^*$ and $g^*$ have Hopf left adjoints at $()$, given by objects $f_!\in \cP(B)$ and $g_!\in \cP(D)$.
Then there is a unique morphism $f_! \to k^* g_!$ in $\cP(B)$ such that the composite $() \xto{\eta} f^* f_! \to f^* k^* g_!$ in $\cP(A)$ is equal to the composite $() \xto{h^* \eta} h^* g^* g_!$ (note $f^* k^* = h^* g^*$).
We say that \cP satisfies the \textbf{left Beck--Chevalley condition at $()$} with respect to this square if this morphism $f_! \to k^* g_!$ is an isomorphism.
\end{defn}
\begin{defn}
An \cS-indexed \fS-multiposet with unit objects \textbf{has equality} if
\begin{enumerate}
\item For any objects $\Gamma$ and $A$ of \cS, the reindexing functor $\cP(\Gamma\times A\times A) \to \cP(\Gamma\times A)$ has a Hopf left adjoint at $()$; and
\item For any morphism $f:\Gamma\to\Delta$ and object $A$ in \cS, \cP satisfies the left Beck--Chevalley condition at $()$ with respect to the square
\[ \xymatrix@C=5pc{ \Gamma\times A \ar[r]^{f\times \idfunc_A} \ar[d] & \Delta\times A \ar[d] \\
\Gamma\times A\times A \ar[r]_{f\times \idfunc_A \times \idfunc_A} & \Delta\times A\times A. } \]
\end{enumerate}
\end{defn}
As before, the Beck--Chevalley condition is true in the syntax because ``equality is preserved by substitution''.
The relevant substitution here is not the one built into the equality rule, though, but the substitution for different variables, which doesn't change the equality proposition at all: $(x=_A y)[M/z] = (x=_A y)$ as long as $z\neq x$ and $z\neq y$.
For the indexed poset of subsets from \cref{eg:subset-hyperdoctrine}, the left adjoint to $(\Delta_A)^* : \cP(\Gamma\times A\times A) \to \cP(\Gamma\times A)$ at $()$ is defined by
\[ (\Delta_A)_! = \setof{ (i,x,y)\in \Gamma\times A\times A | x=y }. \]
Left adjoints in \cref{eg:subobject-hyperdoctrine} actually always exist (see \cref{thm:horn-subobjects}), while those in \cref{eg:family-hyperdoctrine} can be defined by
\[ ((\Delta_A)_!)_{(i,x,y)\in \Gamma\times A\times A} =
\begin{cases}
\top &\text{if } x=y\\
\bot &\text{if } x\neq y.
\end{cases}
\]
Note that we are sticking doggedly to the principle that just as the rules for a given type operation should be independent of any other type operations, the corresponding universal property should be statable without reference to any other objects with universal properties.\footnote{At least, other universal properties in the multiposets $\cP(\Gamma)$.
We do still refer to cartesian products in \cS, but we could also remove those by working with ``presheaves on multicategories'' as sketched at the beginning of this section.}
If we \emph{do} have additional structure, particularly tensor products and units in the multiposets $\cP(\Gamma)$, then our various kinds of adjoints can be formulated in terms of those and ordinary adjunctions --- see \cref{ex:multicat-radj,ex:moncat-radj,ex:hopf-ladj,ex:hopf-ladj-at-one} --- and our examples in \cref{sec:subobjects} will mainly arise in this way.
However, to make a closer connection to the type theory we prefer to formulate them independently first.
\begin{defn}
A \textbf{first-order \fS-hyperdoctrine} consists of a category \cS with finite products together with an \cS-indexed \fS-multiposet that is closed and representable and has products (finite meets), coproducts (finite joins), universal and existential quantifiers, and equality.
By default, a \textbf{first-order hyperdoctrine} refers to the cartesian case where $\fS$ contains all functions; in this case representability is equivalent to having finite meets.
More generally, an \cS-indexed cartesian multiposet is called a:
\begin{enumerate}
\item \textbf{coherent hyperdoctrine} if it has finite meets, finite joins, existential quantifiers, and equality;
\item \textbf{geometric hyperdoctrine} if it has finite meets, infinite joins, existential quantifiers, and equality;
\item \textbf{regular hyperdoctrine} if it has finite meets, existential quantifiers, and equality; and a
\item \textbf{Horn hyperdoctrine} if it has finite meets and equality.
\end{enumerate}
\end{defn}
Note that since all the structure of a first-order \fS-hyperdoctrine is determined by universal properties, it is unique up to isomorphism, and hence unique on the nose in an (antisymmetric) poset.
Thus, there is no need to suppose separately that we have \emph{chosen} such operations.
\begin{thm}\label{thm:fol-initial}
The free first-order \fS-hyperdoctrine generated by a first-order \fS-theory can be presented, up to equivalence, by the type theory of the latter:
\begin{itemize}
\item \cS is the category of type contexts; and
\item the poset $\cP(\Gamma)$ is obtained from the poset of proposition judgments $\Gamma\types \ph\prop$ and derivable entailments $\Gamma\cb\Theta\types\ph$ by identifying isomorphic objects (since in this section our posets are antisymmetric).
\end{itemize}
(And similarly for the other fragments with fewer type operations.)
\end{thm}
\begin{proof}
We have already observed that this structure defines an indexed \fS-multicategory and that the simple type operations $\meet,\top,\join,\bot,\tensor,\one,\hom$ yield the appropriate multicategorical strurcture.
Moreover, we defined the categorical notions of universal and existential quantifiers and equality precisely so that they would hold in the syntax; thus the description above does yield a first-order \fS-hyperdoctrine.
Now, the underlying multigraph of a first-order \fS-theory is of course a finite-product theory without axioms, and we showed in \cref{sec:fp-theories} that the category of contexts of its type theory is, up to equivalence, the free category with products it generates.
Thus, it maps uniquely (up to isomorphism) into the base category of any other first-order \fS-hyperdoctrine; it remains to show that this map extends uniquely to a map of hyperdoctrines, i.e.\ a natural transformation between the $\cP$-functors preserving all the structure.
As usual, we do this by induction on derivations.
The proposition judgment $\Gamma\types \ph\prop$ is easy: each rule corresponds to one of the objects with a universal property that we have assumed to exist in any first-order \fS-hyperdoctrine.
Next, since the rules for entailment involve substitution of terms into propositions, before defining our functor on entailments we have to first prove that it maps such substitutions to the reindexing functors in the target; this is another straightforward induction on derivations of $\Gamma\types \ph\prop$.
Now the rules for entailment involving simple type operations are also easy, just as in \cref{sec:logic}.
Finally, in \crefrange{sec:forall}{sec:equality} and this section we showed that the natural deduction rules for quantifiers and equality are inter-derivable (in the presence of substitution and cut) with the rules that exactly express the appropriate kind of adjunctions.
This completes the definition on entailments.
Since everything is posetal there is not much left to do: we show that our map preserves all the hyperdoctrine structure, essentially by definition, and then that it is unique (modulo the up-to-isomorphism uniqueness of the functor on base categories), because its definition was forced at every step.
\end{proof}
\begin{rmk}\label{rmk:fol-soundness-completeness}
As noted in \cref{rmk:soundness-completeness} for propositional logic, \cref{thm:fol-initial} implies the traditional \emph{soundness} and \emph{completeness} theorems for first-order logic with respect to hyperdoctrines.
The soundness theorem says that if we can prove $\Gamma\cb \ec\types \ph$, then when we interpret our logic into any hyperdoctrine, $\ph$ must go to the top element, i.e.\ it must ``be true''.
In particular, this applies to models in the hyperdoctrine of sets and subsets from \cref{eg:subset-hyperdoctrine}, which are the classical notion of ``model''.
(In \cref{sec:subobjects} we will construct hyperdoctrines from more general categories than \bSet.)
Conversely, the completeness theorem says that if something is true in all hyperdoctrines, then it must in particular be true in the free one constructed from the type theory, and therefore must be provable in the type theory.
\end{rmk}
\begin{rmk}
Note that all categorical structure corresponding to quantifiers and equality takes the form of \emph{certain} adjoints to reindexing functors.