-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleftovers.tex
1803 lines (1594 loc) · 132 KB
/
leftovers.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
% 1. Это более общее определение морфизмов алгебраических теорий.
% Оно мне пока не нужно, а определение более сложное, чем для теорий с фиксированными сортами.
% Поэтому я решил его пока не включать, но потом может пригодиться.
There are several equivalent ways of defining essentially algebraic theories (\cite{LPC}, \cite{GAT}, \cite{PHL}, \cite[D 1.3.4]{elephant}).
We use approach introduced in \cite{PHL} under the name of partial Horn theories since it is the most convenient one.
There is a structure of a category on partial Horn theories.
A \emph{generalized morphism} between theories $\mathbb{T}$ and $\mathbb{T}'$ is a model of $\mathbb{T}$ in $\mathcal{C}_{\mathbb{T}'}$,
where $\mathcal{C}_{\mathbb{T}'}$ is the classifying category for $\mathbb{T}'$.
We will define another notion of morphisms between theories, which is more explicit.
\subsection{Morphisms of partial Horn theories}
Let $\mathbb{T}$ be a partial Horn theory.
A \emph{restricted term} of $\mathbb{T}$ is a term $t$ together with a formula $\varphi$.
We denote such a restricted term by $t|_\varphi$.
A \emph{derived sort} of $\mathbb{T}$ is a sequence of sorts $s_1, \ldots s_k$ together with a formula $\varphi$ succh that $FV(\varphi) \subseteq \{ x_1 : s_1, \ldots x_k : s_k \}$.
We will use the following abbreviations:
\begin{align*}
R(t_1|_{\varphi_1}, \ldots t_k|_{\varphi_k}) & \Longleftrightarrow R(t_1, \ldots t_k) \land \varphi_1 \land \ldots \land \varphi_k \\
t|_\varphi = s|_\psi & \Longleftrightarrow t = s \land \varphi \land \psi \\
t|_\varphi\!\downarrow & \Longleftrightarrow t\!\downarrow\!\land \varphi \\
\chi \sststile{}{V} t|_\varphi \cong s|_\psi & \Longleftrightarrow \chi \land t|_\varphi\!\downarrow\,\sststile{}{V} t = s \land \psi \text{ and } \chi \land s|_\psi\!\downarrow\,\sststile{}{V} t = s \land \varphi
\end{align*}
We define morphisms of theories $\mathbb{T}$ and $\mathbb{T}'$ as equivalence classes of functions $h$ satisfying the following conditions:
\begin{enumerate}
\item For every sort $s$ of $\mathbb{T}$, it determines a derived sort $h(s)$ of $\mathbb{T}'$,
\item For every predicate symbol $P : s_1 \times \ldots \times s_k$ of $\mathbb{T}$, it determines a formula $h(P)$ of $\mathbb{T}'$
such that $FV(h(P)) = \{ x^1_1 : s^1_1, \ldots x^{n_1}_1 : s^{n_1}_1, \ldots x^1_k : s^1_k, \ldots x^{n_k}_k : s^{n_k}_k \}$
and the sequent $h(P) \sststile{}{FV(h(P))} \varphi_1 \land \ldots \land \varphi_k$ is derivable,
where $\varphi_i$ are the formulas that correspond to the derived sorts $h(s_i)$ and $FV(\varphi_i) = \{ x^{n_i}_i, \ldots x^{n_i}_i \}$.
\item For every function symbol $\sigma : s_1 \times \ldots \times s_k \to s$ of $\mathbb{T}$,
it determines a sequence of restricted terms $h(\sigma)_1$, \ldots $h(\sigma)_m$ of $\mathbb{T}'$
such that $FV(h(\sigma)_i) = \{ x^1_1 : s^1_1, \ldots x^{n_1}_1 : s^{n_1}_1, \ldots x^1_k : s^1_k, \ldots x^{n_k}_k : s^{n_k}_k \}$
and the sequents $h(\sigma)_i\!\downarrow\ \sststile{}{FV(h(\sigma)_i)} \varphi^i_1 \land \ldots \land \varphi^i_k$
and $\bigwedge_{1 \leq i \leq m} h(\sigma)_i\!\downarrow\ \sststile{}{FV(h(\sigma)_i)} \psi[h(\sigma)_1/y_1, \ldots h(\sigma)_m/y_m]$ are derivable,
where $\varphi_i$ are the formulas that correspond to the derived sorts $h(s_i)$ and $FV(\varphi_i) = \{ x^{n_i}_i, \ldots x^{n_i}_i \}$
and $\psi$ is the formula that correspond to the derived sort $h(s)$ and $FV(\psi) = \{ y_1, \ldots y_m \}$.
\item For every axiom $S$ of $\mathbb{T}$, the sequent $h(S)$ is derivable in $\mathbb{T}'$.
\end{enumerate}
We will say that functions $h$ and $h'$ as described above are equivalent if the following conditions hold:
\begin{enumerate}
\item For every sort $s$ of $\mathbb{T}$, the sequences of sorts that correspond to $h(s)$ and $h'(s)$ are equal
and the formulas $\varphi$ and $\psi$ that correspond to $h(s)$ and $h'(s)$ are equivalent;
that is, the sequents $\varphi \ssststile{}{FV(\varphi) \cup FV(\psi)} \psi$ are derivable.
\item For every predicate symbol $P : s_1 \times \ldots \times s_k$ of $\mathbb{T}$, the formulas $h(P)$ and $h'(P)$ are equivalent.
\item For every function symbol $\sigma : s_1 \times \ldots \times s_k \to s$ of $\mathbb{T}$, restricted terms $h(\sigma)_i$ and $h'(\sigma)_i$ are equivalent for every $i$;
that is, the sequents $\sststile{}{FV(h(\sigma)_i)} h(\sigma)_i \cong h'(\sigma)_i$ are derivable.
\end{enumerate}
The identity morphism is defined in the obvious way.
To define the composition of morphisms, we need to extend the definition of a function $h : \mathbb{T} \to \mathbb{T}'$ to terms and formulas.
Let $t$ be a term of $\mathbb{T}$ of sort $s$ with free variables $x_1 : s_1$, \ldots $x_k : s_k$.
Suppose that $h(s)$ is a sequence $s_1$, \ldots $s_m$ together with a formula $\psi$
and $h(s_i)$ is a sequence $s^1_i$, \ldots $s^{n_i}_i$ together with a formula $\varphi_i$.
Then, by induction on $t$, we define a sequence of restricted terms $h(t)_1$, \ldots $h(t)_m$ of $\mathbb{T}'$
with free variables $x^1_1 : s^1_1, \ldots x^{n_1}_1 : s^{n_1}_1, \ldots x^1_k : s^1_k, \ldots x^{n_k}_k : s^{n_k}_k$.
If $t = x$ is a variable, then let $h(x)_i = x^i$.
% Это я не закончил.
% 2. Это раздел про partial Horn theories из моей старой версии статьи про алгебраические теории типов.
% В основном это шлак, но там есть пара интересных лемм, например про мономорфизмы.
\section{Partial Horn theories}
\label{sec:PHT}
There are several equivalent ways of defining essentially algebraic theories (\cite{LPC}, \cite{GAT}, \cite{PHL}, \cite[D 1.3.4]{elephant}).
We use approach introduced in \cite{PHL} under the name of partial Horn theories since it is the most convenient one.
We define morphisms of partial Horn theories in terms of morphisms of monads and left modules over them.
In this section we review necessary for our development parts of the theory of monads, left modules over them and partial Horn theories.
We also define algebraic dependent type theories as certain partial Horn theories.
\subsection{Monads and left modules over them}
We recall definitions of monads and left modules over a monad.
For our purposes the following definitions (see \cite{manes-algebraic-theories}) will be more convenient than the ordinary ones.
\begin{defn}
A \emph{monad} $(T,\eta,(-)^*)$ on a category $\C$ consists of a function $T : Ob(\C) \to Ob(\C)$,
a function $\eta$ that to each $A \in Ob(\C)$ assign a morphism $\eta_A : A \to T(A)$,
and a function that to each $A,B \in Ob(\C)$ assigns a function $(-)^* : Hom_\C(A,T(B)) \to Hom_\C(T(A),T(B))$, satisfying the following conditions:
\begin{itemize}
\item $\eta_A^* = id_{T(A)}$.
\item For every $\rho : A \to T(B)$, $\rho^* \circ \eta_A = \rho$.
\item For every $\rho : A \to T(B)$, $\sigma : B \to T(C)$, $\sigma^* \circ \rho^* = (\sigma^* \circ \rho)^*$.
\end{itemize}
A \emph{left module} $(M,(-)^\circ)$ over a monad $(T,\eta,(-)^*)$ with values in a category $\D$ consists of a function $M : Ob(\C) \to Ob(\D)$
and a function that to each $A,B \in Ob(\C)$ assigns a function $(-)^\circ : Hom_\C(A,T(B)) \to Hom_\D(M(A),M(B))$, satisfying the following conditions:
\begin{itemize}
\item $\eta_A^\circ = id_{M(A)}$.
\item For every $\rho : A \to T(B)$, $\sigma : B \to T(C)$, $\sigma^\circ \circ \rho^\circ = (\sigma^* \circ \rho)^\circ$.
\end{itemize}
\end{defn}
These data and axioms imply that $T$ and $M$ are functorial: if $f : A \to B$, then we can define $T(f)$ as $(\eta_B \circ f)^*$ and $M(f)$ as $(\eta_B \circ f)^\circ$.
Moreover, $\eta$, $(-)^*$ and $(-)^\circ$ are natural.
\begin{defn}
A morphism of monads $(T,\eta,(-)^*)$ and $(T',\eta',(-)^{*'})$ on $\C$ is a function $\alpha$ that to each $A \in Ob(\C)$ assigns a morphism $\alpha_A : T(A) \to T'(A)$,
satisfying the following conditions:
\begin{itemize}
\item $\alpha_A \circ \eta_A = \eta'_A$.
\item For every $\rho : A \to T(B)$, $\alpha_B \circ \rho^* = (\alpha_B \circ \rho)^{*'} \circ \alpha_A$.
\end{itemize}
Let $(M,(-)^\circ)$ and $(M',(-)^{\circ'})$ be left modules with values in $\D$ over monads $(T,\eta,(-)^*)$ and $(T',\eta',(-)^{*'})$ respectively.
A morphism between them is a pair of functions $(\alpha,\beta)$, where $\alpha$ is a morphism of monads $T$ and $T'$,
and $\beta$ assigns to each $A \in Ob(\C)$ a morphism $\beta_A : M(A) \to M'(A)$,
such that, for every $\rho : A \to T(B)$, $\beta_B \circ \rho^\circ = (\alpha_B \circ \rho)^{\circ'} \circ \beta_A$.
\end{defn}
These data and axioms imply that $\alpha$ and $\beta$ are natural.
Let $\mathcal{S}$ be a set of sorts, and let $(T,\eta,(-)^*)$ be a monad on the category of $\mathcal{S}$-sets.
We think of elements of $T(V)_s$ as terms of sort $s$ with free variables in $V$.
Given $t \in T(V)_s$ and $\rho : V \to T(V')$, we will write $t[\rho] \in T(V')_s$ for $\rho^*(t)$.
Let $(F,(-)^\circ)$ be a left module over $T$ with values in $\Set$.
We think of elements of $F(V)$ as formulas with free variables in $V$.
Given $\varphi \in F(V)$ and $\rho : V \to T(V')$, we will write $\varphi[\rho] \in F(V')$ for $\rho^\circ(\varphi)$.
Let $T : \Set^\mathcal{S} \to \Set^\mathcal{S}$ be a monad.
Then \emph{a free variables structure} on $T$ is a function $FV$ that to each $t \in T(V)_s$ assigns a subset of $V$, that is $FV(t) \subseteq V$, called the set of free variables of $t$.
This function must satisfy the following conditions:
\begin{align*}
FV(\eta(x)) & = x \\
FV(t[\rho]) & = \bigcup_{x \in FV(t)} FV(\rho(x))
\end{align*}
Let $F : \Set^\mathcal{S} \to \Set$ be a left module over $T$.
Then \emph{a free variables structure} on $F$ is a function $FV$ that to each $\varphi \in F(V)$ assigns a subset of $V$, that is $FV(\varphi)$, called the set of free variables of $\varphi$.
This function must satisfy the following condition:
\[ FV(\varphi[\rho]) = \bigcup_{x \in FV(\varphi)} FV(\rho(x)) \]
\emph{A module of formulas} over $T$ is a left module $F$ over $T$ together with a function
$\land : F(V) \times F(V) \to F(V)$ and a constant $\top \in F(V)$ for every $V \in \Set^\mathcal{S}$, satisfying the following conditions:
\begin{itemize}
\item For every $\rho : V \to T(V')$, $\top[\rho] = \top$.
\item For every $\rho : V \to T(V')$, $(\varphi \land \psi)[\rho] = \varphi[\rho] \land \psi[\rho]$.
\end{itemize}
For every monad $T$ on $\Set^\mathcal{S}$ we define a left module $E$ with values in $\Set$.
For every $V \in \Set^\mathcal{S}$, let $E(V)$ be the set of triples $(s,t,t')$, where $s \in \mathcal{S}$, and $t,t' \in T(V)_s$.
For every $\rho : V \to T(V')$ and $(s,t,t') \in E(V)$, we let $(s,t,t')[\rho] = (s,t[\rho],t'[\rho])$.
We think of $(s,t,t')$ as a formula asserting the equality of terms $t$ and $t'$.
We write $t =_s t'$ (or simply $t = t'$) for $(s,t,t')$.
\emph{A module of formulas with equality} over $T$ is a module $F$ of formulas over $T$ together with a morphism $e : E \to F$.
\begin{defn}[mon-pres]
A \emph{monadic presentation of a partial Horn theory} is a triple $(T,F,\mu)$, where
$T : \Set^\mathcal{S} \to \Set^\mathcal{S}$ is a finitary monad with a free variables structure,
$F : \Set^\mathcal{S} \to \Set$ is a finitary module of formulas with equality and a free variables structure, and
$\mu_V : T(V) \times F(V) \to T(V)$ is a function such that the following conditions hold:
\begin{itemize}
\item For every $\rho : V \to T(V')$, $\mu_V(t,\varphi)[\rho] = \mu_{V'}(t[\rho],\varphi[\rho])$.
\item $\mu_V(t, \top) = t$.
\item $\mu_V(t, \varphi \land \psi) = \mu_V(\mu_V(t, \varphi), \psi)$.
\end{itemize}
A morphism of triples $(T,F,\mu)$ and $(T',F',\mu')$ is a morphism $f$ of left modules $(T,F)$ and $(T',F')$ such that $f$ preserves free variables, equality, $\top$, $\land$ and $\mu$.
The category of monadic presentations of partial Horn theories with $\mathcal{S}$ as the set of sorts is denoted by $\PMnd_\mathcal{S}$.
\end{defn}
\subsection{The category of partial Horn theories}
\label{sec:PHT-rules}
Let $\mathcal{S}$ be a set of sorts, $T : \Set^\mathcal{S} \to \Set^\mathcal{S}$ a monad with a free variables structure,
and $\mathcal{P}$ a set of predicate symbols together with a function that to each $R \in \mathcal{P}$
assigns its signature $R : s_1 \times \ldots \times s_n$, where $s_1, \ldots s_n \in \mathcal{S}$.
Let $\mathcal{F}$ be a set of function symbols together with a function that to each $\sigma \in \mathcal{F}$ assigns its signature $\sigma : s_1 \times \ldots \times s_n \to s$, where $s_1, \ldots s_n, s \in \mathcal{S}$.
Then we can define an example of a monad over $\Set^\mathcal{S}$.
For each $V \in \Set^\mathcal{S}$ we can define a set $Term_\mathcal{F}(V)_s$ of terms of sort $s$ inductively:
\begin{itemize}
\item If $x \in V_s$, then $x \in Term_\mathcal{F}(V)_s$.
\item If $\sigma : s_1 \times \ldots \times s_n \to s$ and $t_i \in Term_\mathcal{F}(V)_{s_i}$, then $\sigma(t_1, \ldots t_n) \in Term_\mathcal{F}(V)_s$.
\end{itemize}
If $\rho : V \to Term_\mathcal{F}(V')$, then substitution is defined as follows:
\begin{align*}
x[\rho] & = \rho(x) \\
\sigma(a_1, \ldots a_k)[\rho] & = \sigma(a_1[\rho], \ldots a_k[\rho])
\end{align*}
Thus $Term_\mathcal{F} : \Set^\mathcal{S} \to \Set^\mathcal{S}$ is a monad, which we call the standard monad (over $\mathcal{F}$).
An \emph{atomic formula} with free variables in $V$ is an expression either of the form $t_1 =_s t_2$ (we will usually omit $s$ in the notation),
where $s \in \mathcal{S}$ and $t_1, t_2 \in T(V)_s$, or of the form $R(t_1, \ldots t_n)$, where $R \in \mathcal{P}$, $R : s_1 \times \ldots \times s_n$ and $t_i \in T(V)_{s_i}$.
A \emph{Horn formula} (over $\mathcal{P}$) with free variables in $V$ is an expression of the form $\varphi_1 \land \ldots \land \varphi_n$ where $\varphi_i$ are atomic formulas.
If $n = 0$, then we write such a formula as $\top$.
The set of Horn formulas with free variables in $V$ is denoted by $Form_\mathcal{P}(V)$.
If $\varphi \in Form_\mathcal{P}(V)$ and $\rho : V \to T(V')$, then we will write $\varphi[\rho]$ for a formula defined as follows:
\begin{align*}
(t = t')[\rho] & = (t[\rho] = t'[\rho]) \\
R(t_1, \ldots t_k)[\rho] & = R(t_1[\rho], \ldots t_k[\rho]) \\
(\varphi_1 \land \ldots \land \varphi_n)[\rho] & = \varphi_1[\rho] \land \ldots \land \varphi_n[\rho]
\end{align*}
Thus $Form_\mathcal{P}$ is a left module over $T$.
Moreover, a free variables structure on $Form_\mathcal{P}$ is defined as follows:
\begin{align*}
FV(t = t') & = FV(t) \cup FV(t') \\
FV(R(t_1, \ldots t_k)) & = FV(t_1) \cup \ldots \cup FV(t_k) \\
FV(\varphi_1 \land \ldots \land \varphi_n) & = FV(\varphi_1) \cup \ldots \cup FV(\varphi_n)
\end{align*}
A \emph{Horn sequent} is an expression of the form $\varphi \sststile{}{V} \psi$, where $\varphi$ and $\psi$ are Horn formulas with free variables in $V$.
We will often write $\varphi_1, \ldots \varphi_n \sststile{}{V} \psi_1, \ldots \psi_k$ instead of $\varphi_1 \land \ldots \land \varphi_n \sststile{}{V} \psi_1 \land \ldots \land \psi_k$.
A \emph{partial Horn theory} is a set of Horn sequents.
The rules of \emph{partial Horn logic} are listed below.
If $\mathcal{A}$ is a partial Horn theory, then a \emph{theorem} of $\mathcal{A}$ is a sequent derivable from $\mathcal{A}$ in this logic.
\begin{center}
$\varphi \sststile{}{V} \varphi$ \axlabel{b1}
\qquad
\AxiomC{$\varphi \sststile{}{V} \psi$}
\AxiomC{$\psi \sststile{}{V} \chi$}
\RightLabel{\axlabel{b2}}
\BinaryInfC{$\varphi \sststile{}{V} \chi$}
\DisplayProof
\qquad
$\varphi \sststile{}{V} \top$ \axlabel{b3}
\end{center}
\medskip
\begin{center}
$\varphi \land \psi \sststile{}{V} \varphi$ \axlabel{b4}
\qquad
$\varphi \land \psi \sststile{}{V} \psi$ \axlabel{b5}
\qquad
\AxiomC{$\varphi \sststile{}{V} \psi$}
\AxiomC{$\varphi \sststile{}{V} \chi$}
\RightLabel{\axlabel{b6}}
\BinaryInfC{$\varphi \sststile{}{V} \psi \land \chi$}
\DisplayProof
\end{center}
\medskip
\begin{center}
$\sststile{}{x} x\!\downarrow$ \axlabel{a1}
\qquad
$x = y \land \varphi \sststile{}{V,x,y} \varphi[y/x]$ \axlabel{a2}
\end{center}
\medskip
\begin{center}
\AxiomC{$\varphi \sststile{}{V} \psi$}
\RightLabel{, $x \in FV(\varphi)$, $t \in T(V')$ \axlabel{a3}}
\UnaryInfC{$\varphi[t/x] \sststile{}{V,V'} \psi[t/x]$}
\DisplayProof
\end{center}
\medskip
Here, $t/x$ denotes a function $\rho : V \to T(V \cup V')$ such that $\rho(x) = t$ and $\rho(y) = y$ if $y \neq x$.
Note that this set of rules is a generalization of the one described in \cite{PHL}.
If $T$ is the standard monad $Term_\mathcal{F}$, then these rules are equivalent to the rules from \cite{PHL}.
In particular, the following sequents are derivable if $x \in FV(t)$:
\begin{align*}
R(t_1, \ldots t_k) & \sststile{}{V} t_i = t_i \axtag{a4} \\
t_1 = t_2 & \sststile{}{V} t_i = t_i \axtag{a4'} \\
t[t'/x]\!\downarrow & \sststile{}{V} t' = t' \axtag{a5}
\end{align*}
We will need the following lemmas from \cite{PHL}:
\begin{lem}[cong-a]
For every $u_i,v_i \in T(V)_{s_i}$ and $t \in T(\{ x_1 : s_1, \ldots x_n : s_n\})_s$,
sequents $u_1 = v_1 \land \ldots \land u_n = v_n \sststile{}{V} t[x_i \mapsto u_i] \cong t[x_i \mapsto v_i]$ are theorems of any theory.
\end{lem}
\begin{lem}
Sequent $y = x \land \varphi[y/x] \sststile{}{V} \varphi$ is a theorem of any theory.
\end{lem}
Using the previous lemma we prove the following fact:
\begin{lem}[cong-b]
For every $u_i,v_i \in T(V)_{s_i}$ and $\varphi \in Form_\mathcal{P}(\{ x_1 : s_1, \ldots x_n : s_n\})$,
sequent $u_1 = v_1 \land \ldots \land u_n = v_n \land \varphi[x_i \mapsto u_i] \sststile{}{V} \varphi[x_i \mapsto v_i]$ is a theorem of any theory.
\end{lem}
\begin{proof}
By the previous lemma we have $y_n = x_n \land \varphi[y_n/x_n] \sststile{}{x_1 : s_1, \ldots x_n : s_n, y_n : s_n} \varphi$ is provable.
If we take $\varphi$ to be equal to $y_n = x_n \land \varphi[y_n/x_n]$, then we get sequent
$y_{n-1} = x_{n-1} \land y_n = x_n \land \varphi[y_n/x_n,y_{n-1}/x_{n-1}] \sststile{}{x_1 : s_1, \ldots x_n : s_n, y_{n-1} : s_{n-1}, y_n : s_n} y_n = x_n \land \varphi[y_n/x_n]$.
By \axref{b2} we get sequent
\[ y_{n-1} = x_{n-1} \land y_n = x_n \land \varphi[y_n/x_n,y_{n-1}/x_{n-1}] \sststile{}{x_1 : s_1, \ldots x_n : s_n, y_{n-1} : s_{n-1}, y_n : s_n} \varphi. \]
Repeating this argument we can conclude that
\[ y_1 = x_1 \land \ldots \land y_n = x_n \land \varphi[y_1/x_1, \ldots y_n/x_n] \sststile{}{x_1 : s_1, \ldots x_n : s_n, y_1 : s_1, y_n : s_n} \varphi. \]
By \axref{a3} we conclude that the required sequent is derivable.
\end{proof}
Now we define a functor $PT : \Set^\mathcal{S} \to \Set^\mathcal{S}$ of restricted terms.
We let $PT(V)_s$ to be the set of expressions $t|_\varphi$ where $t \in T(V)_s$ and $\varphi \in Form_\mathcal{P}(V)$.
If $\varphi = \top$, then we will write $t|_\varphi$ simply as $t$.
If $p \in PT(V)_s$, $p = t|_\varphi$ and $\psi \in Form_\mathcal{P}(V)$, then we will write $p|_\psi$ for $t|_{\varphi \land \psi}$.
We will use the following abbreviations:
\begin{align*}
t\!\downarrow & \Longleftrightarrow t = t \\
\varphi \sststile{}{V} t \leftrightharpoons s & \Longleftrightarrow \varphi \land t\!\downarrow \land s\!\downarrow\,\sststile{}{V} t = s \\
\varphi \sststile{}{V} t \cong s & \Longleftrightarrow \varphi \land t\!\downarrow\,\sststile{}{V} t = s \text{ and } \varphi \land s\!\downarrow\,\sststile{}{V} t = s \\
\varphi \ssststile{}{V} \psi & \Longleftrightarrow \varphi \sststile{}{V} \psi \text{ and } \psi \sststile{}{V} \varphi \\
R(t_1|_{\varphi_1}, \ldots t_k|_{\varphi_k}) & \Longleftrightarrow R(t_1, \ldots t_k) \land \varphi_1 \land \ldots \land \varphi_k \\
t|_\varphi = s|_\psi & \Longleftrightarrow t = s \land \varphi \land \psi \\
t|_\varphi\!\downarrow & \Longleftrightarrow t\!\downarrow\!\land \varphi \\
\chi \sststile{}{V} t|_\varphi \leftrightharpoons s|_\psi & \Longleftrightarrow \chi \land t|_\varphi\!\downarrow, s|_\psi\!\downarrow\,\sststile{}{V} t = s \\
\chi \sststile{}{V} t|_\varphi \cong s|_\psi & \Longleftrightarrow \chi \land t|_\varphi\!\downarrow\,\sststile{}{V} t = s \land \psi \text{ and } \chi \land s|_\psi\!\downarrow\,\sststile{}{V} t = s \land \varphi
\end{align*}
Now we define substitution functions for restricted terms.
For every $\rho : V \to PT(V')$, $t \in T(V)_s$ and $\varphi \in Form_\mathcal{P}(V)$,
we define $t[\rho] \in PT(V')_s$, $\varphi[\rho] \in Form_\mathcal{P}(V')$ and $t_\varphi[\rho] \in PT(V')_s$ as follows:
\begin{align*}
t[\rho] & = t[\rho_1]|_{\bigcup_{x \in FV(t)} \rho_2(x)} \\
R(t_1, \ldots t_k)[\rho] & = R(t_1[\rho], \ldots t_k[\rho]) \\
(\varphi_1 \land \ldots \land \varphi_n)[\rho] & = \varphi_1[\rho] \land \ldots \land \varphi_n[\rho] \\
t|_\varphi[\rho] & = t[\rho]|_{\varphi[\rho]}
\end{align*}
where if $\rho(x) = t|_\varphi$, then $\rho_1(x) = t$ and $\rho_2(x) = \varphi$.
Free variables of $t|_\varphi$ is defined as follows: $FV(t|_\varphi) = FV(t) \cup FV(\varphi)$.
Note that $PT$ is not a monad in general since this substitution does not satisfy axioms.
To fix this we introduce an equivalence relation on sets $PT(V)_s$ and $Form_\mathcal{P}(V)$.
Let $\mathbb{T}$ be a partial Horn theory.
For every $t, t' \in PT(V)_s$, $t \sim t'$ if and only if $FV(t) = FV(t')$ and $\sststile{}{V} t \cong t'$ is a theorem of $\mathbb{T}$.
For every $\varphi, \psi \in Form_\mathcal{P}(V)$, $\varphi \sim \psi$ if and only if $FV(\varphi) = FV(\psi)$ and $\varphi \ssststile{}{V} \psi$ is a theorem of $\mathbb{T}$.
Then let $P(V)_s = PT(V)_s/\!\!\sim$ and $F(V) = Form_\mathcal{P}(V)/\!\!\sim$.
For every $x \in V_s$, $\eta_V(x)$ is the equivalence class of $x|_\top$.
Substitution functions respect equivalence relations, and it is easy to see that they define a structure of a monad and of a left module over it on $T$ and $F$.
For every $t,t' \in T(V)_s$, $e(s,t,t')$ is the equivalence class of $t = t'$.
For every $t \in T(V)_s$ and $\varphi \in F(V)$, let $\mu_V(t,\varphi) = t|_\varphi$.
It is easy to see that $(P,F,\mu)$ satisfies axioms of monadic presentations.
We will call it the monadic presentation of partial Horn theory $\mathbb{T}$ and denote by $P(\mathbb{T})$.
The category of partial Horn theories over $\mathcal{S}$ has tuples $(T,\mathcal{P},\mathcal{A})$ as objects,
where $T$ is a finitary monad with a free variables structure, $\mathcal{P}$ is a set of predicate symbols and $\mathcal{A}$ is a set of axioms.
Morphisms of partial Horn theories $\mathbb{T}$ and $\mathbb{T}'$ are morphisms of their monadic presentations.
The category of partial Horn theories over $\mathcal{S}$ is denoted by $\Th^T_\mathcal{S}$.
\begin{prop}[mor-def]
Let $\mathbb{T} = (T,\mathcal{P},\mathcal{A})$ and $\mathbb{T}' = (T',\mathcal{P}',\mathcal{A}')$ be partial Horn theories,
and let $P(\mathbb{T}) = (P,F,\mu)$ and $P(\mathbb{T}') = (P',F',\mu')$ be their monadic presentations.
To construct a morphism of these theories, it is enough to specify the following data:
\begin{itemize}
\item A morphism of monads $\alpha : T \to P'$ that preserves free variables.
\item For every $R \in \mathcal{P}$, $R : s_1 \times \ldots \times s_k$,
a formula $\beta(R) \in F'(\{ x_1 : s_1, \ldots x_k : s_k \})$ such that $FV(\beta(R)) = \{ x_1, \ldots x_k \}$.
\end{itemize}
Then there is a morphism of left modules $f : (T,Form_\mathcal{P}) \to (T',F')$
such that $f(\sigma(x_1, \ldots x_k)) = \alpha(\sigma)$ and $f(R(x_1, \ldots x_k)) = \beta(R)$.
If $f$ preserves axioms of $\mathbb{T}$, then it extends to a morphism of theories.
Moreover, there is at most one morphism with these properties.
\end{prop}
\begin{proof}
Morphism $f$ is already defined on terms, and we can define it on formulas as follows:
\begin{align*}
f(a = b) & = f(a) = f(b) \\
f(R(a_1, \ldots a_k)) & = \beta(R)[x_i \mapsto f(a_i)] \\
f(\varphi_1 \land \ldots \land \varphi_n) & = f(\varphi_1) \land \ldots \land f(\varphi_n)
\end{align*}
We also can define $f$ on restricted terms:
\[ f(t|_\varphi) = f(t)|_{f(\varphi)} \]
It is easy to see that $f$ preserves substitution.
Thus to prove that $f$ extends to a morphism of theories, we only need to show that it preserves theorems of $\mathbb{T}$.
By assumption, it preserves axioms, thus we only need to check that application of $f$ preserves inference rules.
This is obvious for \axref{b1}-\axref{b6} and \axref{a1}.
For \axref{a2} and \axref{a3} it follows from the facts that $f(\varphi[t/x]) = f(\varphi)[f(t)/x]$ and $FV(f(\varphi)) = FV(\varphi)$.
Now, let us prove that $f$ is unique.
Let $f$ and $f'$ be morphisms of theories such that $f(t) = f'(t)$ for every $t \in T(V)_s$, and
$f(R(x_1, \ldots x_k)) = f'(R(x_1, \ldots x_k))$ for every $R \in \mathcal{P}$.
Then we prove that $f = f'$.
Let us prove that $f(\varphi) = f'(\varphi)$ for every $\varphi \in Form_\mathcal{P}(V)$.
It is enough to prove this for atomic formulas $\varphi$.
If $\varphi$ equals to $t = t'$, then $f(\varphi)$ equals to $f(t) = f(t')$ and $f'(\varphi)$ equals to $f'(t) = f'(t')$.
We know that $\sststile{}{V} f(t) \cong f'(t)$ and $\sststile{}{V} f(t') \cong f'(t')$.
Thus by transitivity and symmetry we can conclude that $f(t) = f(t)' \sststile{}{V} f'(t) = f'(t')$.
If $\varphi = R(t_1, \ldots t_k)$, then $f(\varphi) = f(R(x_1, \ldots x_k))[x_i \mapsto f(t_i)]$
and $f'(\varphi) = f'(R(x_1, \ldots x_k))[x_i \mapsto f'(t_i)]$.
We know that $f(R(x_1, \ldots x_k)) \sststile{}{x_1, \ldots x_k}$ \linebreak $f'(R(x_1, \ldots x_k))$.
Since $FV(f(R(x_1, \ldots x_k))) = \{ x_1, \ldots x_k \}$, by \axref{a3} we can conclude that $f(\varphi) \sststile{}{V} f'(R(x_1, \ldots x_k))[x_i \mapsto f(t_i)]$.
Since $f'(R(x_1, \ldots x_k))[x_i \mapsto f(t_i)] \sststile{}{V} f(t_i)\!\downarrow$, \rlem{cong-b} implies that
$f'(R(x_1, \ldots x_k))[x_i \mapsto f(t_i)] \sststile{}{V} f'(\varphi)$.
By \axref{b2} we conclude that $f(\varphi) \sststile{}{V} f'(\varphi)$.
The same argument shows that $f'(\varphi) \sststile{}{V} f(\varphi)$.
Finally, it is easy to see that $f(t) = f'(t)$ for every $t \in PT(V)_s$.
Thus $f = f'$.
\end{proof}
Note that if $T$ is the standard monad $Term_\mathcal{F}$, then to define a morphism of monads $T \to T'$,
it is enough to specify for every $\sigma \in \mathcal{F}$, $\sigma : s_1 \times \ldots \times s_k \to s$,
a restricted term $\alpha(\sigma) \in T'(\{ x_1 : s_1, \ldots x_k : s_k \})$ such that $FV(\alpha(\sigma)) = \{ x_1, \ldots x_k \}$.
Then there is a unique morphism of monads $f : T \to T'$ such that $f(\sigma(x_1, \ldots x_k)) = \alpha(\sigma)$.
Now, let us define a category $\Th_\mathcal{S}$ of standard partial Horn theories.
Its objects are tuples $((\mathcal{S},\mathcal{F},\mathcal{P}),\mathcal{A})$, where $\mathcal{F}$ is a set of function symbols,
$\mathcal{P}$ is a set of relation symbols, and $\mathcal{A}$ is a set of axioms over $(Term_\mathcal{F},Form_\mathcal{P})$.
Morphisms of standard partial Horn theories are morphisms of corresponding partial Horn theories.
Thus $\Th_\mathcal{S}$ is (equivalent to) a full subcategory of $\Th^T_\mathcal{S}$.
\subsection{Models of partial Horn theories}
Given a monad $T : \Set^\mathcal{S} \to \Set^\mathcal{S}$, we define a category of its partial algebras.
A \emph{partial algebra} over $T$ is a pair $(A,\alpha)$, where $A$ is an $\mathcal{S}$-set and $\alpha_V : Hom_{\PSet^\mathcal{S}}(V,A) \to Hom_{\PSet^\mathcal{S}}(T(V),A)$,
where $\PSet$ is the category of sets and partial functions between them.
This pair must satisfy the following conditions:
\begin{itemize}
\item For every partial function $f : V \to A$, $\alpha_V(f) \circ \eta_V = f$.
\item For every total function $\rho : V \to T(V')$ and every partial function $f : V' \to A$, $\alpha_V(\alpha_{V'}(f) \circ \rho) = \alpha_{V'}(f) \circ \rho^*$.
\end{itemize}
A morphism of partial algebras $(A,\alpha)$ and $(A',\alpha')$ is a total morphism $h : A \to A'$ of $\mathcal{S}$-sets
such that, for every partial function $f : V \to A$ and every $t \in T(V)_s$, if $\alpha_V(f)(t)$ is defined,
then $\alpha'_V(h \circ f)(t)$ is also defined and $h(\alpha_V(f)(t)) = \alpha'_V(h \circ f)(t)$.
\begin{lem}[par-alg-str]
If $Term_\mathcal{F}$ is the standard monad, then categories of partial algebras over $Term_\mathcal{F}$
and partial structures for signature $(\mathcal{S},\mathcal{F},\varnothing)$ as defined in \cite{PHL} are isomorphic.
\end{lem}
\begin{proof}
A partial structure for signature $(\mathcal{S},\mathcal{F},\varnothing)$ is an $\mathcal{S}$-set $A$ together with a collection of partial functions
$A(\sigma) : A_{s_1} \times \ldots \times A_{s_n} \to A_s$ for every $\sigma \in \mathcal{F}$, $\sigma : s_1 \times \ldots \times s_n \to s$.
Given such partial structure, we define a partial algebra $F(A)$ over $Term_\mathcal{F}$ as $(A,\alpha)$, where $\alpha$ is defined as follows:
\begin{align*}
\alpha_V(f)(x) & = f(x) \\
\alpha_V(f)(\sigma(t_1, \ldots t_n)) & = A(\sigma)(\alpha_V(f)(t_1), \ldots \alpha_V(f)(t_n))
\end{align*}
For every morphism $h : A \to A'$ of partial structures, let $F(h) = h$.
For every partial algebra $(A,\alpha)$, we define a partial structure $G(A,\alpha)$.
Let $G(A,\alpha) = A$ and $G(A,\alpha)(\sigma)(a_1, \ldots a_n) = \alpha_{x_1, \ldots x_n}(x_i \mapsto a_i)(\sigma(x_1, \ldots x_n))$.
For every morphism $h : (A,\alpha) \to (A',\alpha')$ of partial algebras, let $G(h) = h$.
It is easy to see that functors $F$ and $G$ determine isomorphisms of categories.
\end{proof}
If $F : \Set^\mathcal{S} \to \Set$ is a left module of formulas over $T$, then we define a category of its partial algebras.
A \emph{partial algebra} over $(T,F)$ is a partial algebra $(A,\alpha)$ over $T$ together with a function $\beta_V : Hom_{\PSet^\mathcal{S}}(V,A) \to Hom_\Set(F(V),\Omega)$,
where $\Omega = \{ \top, \bot \}$ is the set of truth-values.
This function must satisfy the following conditions:
\begin{itemize}
\item For every total function $\rho : V \to T(V')$ and every partial function $f : V' \to A$, $\beta_V(\alpha_{V'}(f) \circ \rho) = \beta_{V'}(f) \circ \rho^\circ$.
\item For every partial function $f : V \to A$, $\beta_V(f)(\top) = \top$.
\item For every partial function $f : V \to A$, $\beta_V(f)(\varphi \land \psi) = \beta_V(f)(\varphi) \land \beta_V(f)(\psi)$,
where $P \land Q = \top$ if and only if $P = \top$ and $Q = \top$.
\end{itemize}
A morphism of partial algebras $(A,\alpha,\beta)$ and $(A',\alpha',\beta')$ is a morphism $h$ of partial algebras $(A,\alpha)$ and $(A',\alpha')$
such that, for every partial function $f : V \to A$ and every $\varphi \in F(V)$, if $\beta_V(f)(\varphi) = \top$, then $\beta'_V(h \circ f)(\varphi) = \top$.
We define a function $\epsilon_V : Hom_{\PSet^\mathcal{S}}(V,A) \to Hom_\Set(E(V),\Omega)$ for the left module $E$ of equality.
Let $\epsilon_V(e(s,t,t')) = \top$ if and only if $\alpha_V(f)(t)$ and $\alpha_V(f)(t')$ are defined and equal.
If $F$ is a left module of formulas with equality over $T$, then we say that a partial algebra $(A,\alpha,\beta)$ is standard
if, for every partial function $f : V \to A$, $e_V \circ \beta_V(f) = \epsilon_V(f)$, where $e_V : E(V) \to F(V)$.
\begin{lem}[par-alg-pred]
If $Term_\mathcal{F}$ is the standard monad and $Form_\mathcal{P}$ is the left module of Horn formulas,
then categories of partial algebras over $(Term_\mathcal{F},Form_\mathcal{P})$ and partial structures for signature $(\mathcal{S},\mathcal{F},\mathcal{P})$ are isomorphic.
\end{lem}
\begin{proof}
A partial structure for signature $(\mathcal{S},\mathcal{F},\mathcal{P})$ is a partial structure $A$ for signature $(\mathcal{S},\mathcal{F},\varnothing)$
together with a relation $A(R) \subseteq A_{s_1} \times \ldots \times A_{s_n}$ for every $R \in \mathcal{P}$, $R : s_1 \times \ldots \times s_n$.
Given such partial structure, we define a partial algebra $F(A)$ over $(Term_\mathcal{F},Form_\mathcal{P})$ as $(A,\alpha,\beta)$,
where $(A,\alpha)$ is the partial algebra defined in \rlem{par-alg-str}, and $\beta$ defined as follows:
\begin{align*}
\beta_V(f)(t =_s t') & = \epsilon_V(e(s,t,t')) \\
\beta_V(f)(R(t_1, \ldots t_n)) & = \top \text{ if and only if } (\alpha_V(f)(t_1), \ldots \alpha_V(f)(t_n)) \in A(R) \\
\beta_V(f)(\varphi_1 \land \ldots \land \varphi_n) & = \beta_V(f)(\varphi_1) \land \ldots \land \beta_V(f)(\varphi_n)
\end{align*}
For every morphism $h : A \to A'$ of partial structures, let $F(h) = h$.
For every partial algebra $(A,\alpha,\beta)$, we define a partial structure $G(A,\alpha,\beta)$.
We already defined interpretation of function symbols in \rlem{par-alg-str}.
For every $R \in \mathcal{P}$, let $G(A,\alpha,\beta)(R) = \{ (a_1, \ldots a_n)\ |\ \beta_{x_1, \ldots x_n}(x_i \mapsto a_i)(R(x_1, \ldots x_n)) = \top \}$.
For every morphism $h : (A,\alpha,\beta) \to (A',\alpha',\beta')$ of partial algebras, let $G(h) = h$.
It is easy to see that functors $F$ and $G$ determine isomorphisms of categories.
\end{proof}
If $(T,F,\mu)$ is a monadic presentation, then we define a category of its partial algebras as a full subcategory of partial algebras over $(T,F)$.
A partial algebra $(A,\alpha,\beta)$ over $(T,F)$ is a partial algebra over $(T,F,\mu)$ if, for every partial function $f : V \to A$,
every $t \in T(V)_s$ and every $\varphi \in F(V)$, $\alpha_V(f)(\mu_V(t,\varphi))$ is defined if and only if $\alpha_V(f)(t)$ is defined and $\beta_V(f)(\varphi) = \top$,
and $\alpha_V(f)(\mu_V(t,\varphi))$ equals to $\alpha_V(f)(t)$ when it is defined.
The category of partial algebras over $(T,F,\mu)$ will be denoted by $\PAlg{(T,F,\mu)}$.
\begin{lem}
If $Term_\mathcal{F}$ is the standard monad and $\mathbb{T} = (Term_\mathcal{F},\mathcal{P},\mathcal{A})$ is a partial Horn theory,
then categories of partial algebras over $P(\mathbb{T})$ and models of $\mathbb{T}$ as defined in \cite{PHL} are isomorphic.
\end{lem}
\begin{proof}
Using \rlem{par-alg-pred}, models of $\mathbb{T}$ can be described as partial algebras $(A,\alpha',\beta')$ over $(Term_\mathcal{F},Form_\mathcal{P})$
such that, for every derivable sequent $\varphi \ssststile{}{V} \psi$ of $\mathbb{T}$ and every partial function $f : V \to A$, $\beta'_V(f)(\varphi) = \beta'_V(f)(\psi)$.
Let $(A,\alpha,\beta)$ be a partial algebra over $P(\mathbb{T})$.
Then we define a partial algebra $F(A,\alpha,\beta)$ over $(Term_\mathcal{F},Form_\mathcal{P})$.
Let $F(A,\alpha,\beta) = (A,\alpha',\beta')$, where $\alpha'_V(f)(t) = \alpha_V(f)([t|_\top]_\sim)$ and $\beta'_V(f)(\varphi) = \alpha_V(f)([\varphi]_\sim)$,
where $[t|_\top]_\sim$ and $[\varphi]_\sim$ are equivalence classes of $t_\top$ and $\varphi$ in $P(V)$ and $F(V)$ respectively.
Then $F(A,\alpha,\beta)$ is a model of $\mathbb{T}$.
Indeed, if $\varphi \ssststile{}{V} \psi$ is a theorem of $\mathbb{T}$, then $\varphi' \ssststile{}{V} \psi'$
is also a theorem of $\mathbb{T}$, where $\varphi' = \varphi \land x_1 \land \ldots \land x_n$, $\psi' = \psi \land y_1 \land \ldots \land y_k$,
$x_1, \ldots x_n$ is the set of free variables of $\psi$, and $y_1, \ldots y_k$ is the set of free variables of $\varphi$.
It follows that $[\varphi']_\sim = [\psi']_\sim$; hence $\beta'_V(f)(\varphi') = \beta'_V(f)(\varphi')$.
But $\beta'_V(f)(\varphi) = \beta'_V(f)(\varphi')$ and $\beta'_V(f)(\psi) = \beta'_V(f)(\psi')$; hence $F(A,\alpha,\beta)$ is a model of $\mathbb{T}$.
If $h$ is a morphism of partial algebras over $P(\mathbb{T})$, then let $F(h) = h$.
Let $(A,\alpha',\beta')$ be a model of $\mathbb{T}$.
Then we define a partial algebra $G(A,\alpha',\beta')$ over $P(\mathbb{T})$.
Let $G(A,\alpha',\beta') = (A,\alpha,\beta)$, where $\beta_V(f)([\varphi]_\sim) = \beta'_V(f)(\varphi)$, and $\alpha_V(f)([t|_\varphi]_\sim)$ is defined
if and only if $\alpha'_V(f)(t)$ is defined and $\beta'_V(f)(\varphi) = \top$, and in this case $\alpha_V(f)([t|_\varphi]_\sim) = \alpha'_V(f)(t)$.
These definitions do not depend on the choice of a representative of the equivalence classes.
Indeed, if $\varphi \sim \psi$, then $\varphi \ssststile{}{V} \psi$ is a theorem of $\mathbb{T}$,
and in this case $\beta'_V(f)(\varphi) = \beta'_V(f)(\psi)$ since $A$ is a model of $\mathbb{T}$.
The same argument shows that the definition of $\alpha$ does not depend on the choice of a representative of $[t|_\varphi]_\sim$.
If $h$ is a morphism of models, then let $G(h) = h$.
It is easy to see that functors $F$ and $G$ determine isomorphisms of categories.
\end{proof}
Finally, we prove a proposition which shows that if $\mathbb{T}'$ is a partial Horn theory under $\mathbb{T}$,
then we can think of models of $\mathbb{T}'$ as models of $\mathbb{T}$ with additional structure.
\begin{prop}[func-mod]
For every morphism of monadic presentations $f : (P,F,\mu) \to (P',F',\mu')$, there is a faithful functor $f^* : \PAlg{(P',F',\mu')} \to \PAlg{(P,F,\mu)}$
such that $id_{(P,F,\mu)}^*$ is the identity functor and $(g \circ f)^* = f^* \circ g^*$.
\end{prop}
\begin{proof}
If $(A,\alpha,\beta)$ is a partial algebra over $(P',F',\mu')$, then let $f^*(A,\alpha,\beta) = (A, e \mapsto \alpha_V(e) \circ f_V, e \mapsto \beta_V(e) \circ f_V)$.
If $h : (A,\alpha,\beta) \to (A',\alpha',\beta')$ is a morphism of partial algebras, then let $f^*(h) = h$.
It is easy to see that these definitions satisfy all required conditions.
\end{proof}
\subsection{Properties of the category of theories}
\label{sec:prop}
Now we prove a few properties of the category of theories.
We begin with a proof of the existence of colimits.
\begin{prop}[th-cocomplete]
Category $\Th_\mathcal{S}$ is cocomplete.
\end{prop}
\begin{proof}
First, let $\{ \mathbb{T}_i \}_{i \in S} = \{ ((\mathcal{S},\mathcal{F}_i,\mathcal{P}_i),\mathcal{A}_i) \}_{i \in S}$ be a set of theories.
Then we can define its coproduct $\coprod\limits_{i \in S} \mathbb{T}_i$ as the theory with $\coprod\limits_{i \in S} \mathcal{F}_i$ as the set of function symbols and $\coprod\limits_{i \in S} \mathcal{A}_i$ as the set of axioms.
Morphisms $f_i : \mathbb{T}_i \to \coprod\limits_{i \in S} \mathbb{T}_i$ are defined in the obvious way.
If $g_i : \mathbb{T}_i \to X$ is a collection of morphisms, then \rprop{mor-def} implies that there is a unique morphism $g : \coprod\limits_{i \in S} \mathbb{T}_i \to X$
satisfying $g(\sigma(x_1, \ldots x_n)) = g_i(\sigma(x_1, \ldots x_n))$ and $f(R(x_1, \ldots x_n)) = f_i(R(x_1, \ldots x_n))$
for every $\sigma \in \mathcal{F}_i$ and $R \in \mathcal{P}_i$.
Now, let $f,g : \mathbb{T}_1 \to \mathbb{T}_2$ be a pair of morphisms of theories.
Then we can define their coequalizer $\mathbb{T}$ as the theory with the same set of function and predicate symbols as $\mathbb{T}_2$ and the set of axioms which consists of the axioms of $\mathbb{T}_2$
together with $\sststile{}{x_1, \ldots x_n} f(\sigma(x_1, \ldots x_n)) \cong g(\sigma(x_1, \ldots x_n))$ for each function symbols $\sigma$ of $\mathbb{T}_1$
and $f(R(x_1, \ldots x_n)) \ssststile{}{x_1, \ldots x_n} g(R(x_1, \ldots x_n))$ for each predicate symbols $R$ of $\mathbb{T}_1$.
Then we can define $e : \mathbb{T}_2 \to \mathbb{T}$ as identity function on terms and formulas.
By \rprop{mor-def}, $e \circ f = e \circ g$.
If $h : \mathbb{T}_2 \to X$ is such that $h \circ f = h \circ g$, then it extends to a morphism $\mathbb{T} \to X$ since additional axioms are preserved by the assumption on $h$.
This extension is unique since $e$ is an epimorphism.
\end{proof}
Now we give a characterization of monomorphisms.
\begin{prop}[mono]
A morphism of theories $f : \mathbb{T}_1 \to \mathbb{T}_2$ is a monomorphism if and only if, for every sequent $\varphi \sststile{}{V} \psi$ of $\mathbb{T}_1$,
if $f(\varphi) \sststile{}{V} f(\psi)$ is a theorem of $\mathbb{T}_2$, then $\varphi \sststile{}{V} \psi$ is a theorem of $\mathbb{T}_1$.
\end{prop}
\begin{proof}
First, let us prove the ``if'' part.
Let $g,h : \mathbb{T} \to \mathbb{T}_1$ be a pair of morphisms such that $f \circ g = f \circ h$.
If $t \in RTerm_\Sigma(V)_s$, then $\sststile{}{V} f(g(t)) \cong f(h(t))$; hence $\sststile{}{V} g(t) \cong h(t)$.
If $\varphi \in Form_\mathcal{P}(V)$, then $f(g(\varphi)) \ssststile{}{V} f(h(\varphi))$; hence $g(\varphi) \ssststile{}{V} h(\varphi)$.
Thus $g = h$.
Now, let us prove the ``only if'' part.
Suppose that $f$ is a monomorphism.
Let $\varphi \sststile{}{V} \psi$ be a sequent of $\mathbb{T}_1$ such that $f(\varphi) \sststile{}{V} f(\psi)$ is a theorem of $\mathbb{T}_2$.
Let $\mathbb{T}$ be a theory which consists of a single predicate symbol $R : s_1 \times \ldots \times s_n \times s'_1 \times \ldots \times s'_k$
where $s_1, \ldots s_n$ are sorts of variables in $FV(\varphi)$ and $s'_1, \ldots s'_k$ are sorts of variables in $FV(\psi)$.
Let $g : \mathbb{T} \to \mathbb{T}_1$ be a morphism defined by $g(R(x_1, \ldots x_n, y_1, \ldots y_k)) = \varphi \land y_1\!\downarrow \land \ldots \land y_k\!\downarrow$ and
let $h : \mathbb{T} \to \mathbb{T}_1$ be a morphism defined by $h(R(x_1, \ldots x_n, y_1, \ldots y_k)) = \varphi \land \psi$.
By \rprop{mor-def}, $f \circ g = f \circ h$, hence $g = h$ which implies that $\varphi \sststile{}{V} \psi$.
\end{proof}
Let $\mathbb{T} = ((\mathcal{S},\mathcal{F},\mathcal{P}),\mathcal{A})$ and $\mathbb{T}' = ((\mathcal{S}',\mathcal{F}',\mathcal{P}'),\mathcal{A}')$ be a pair of theories.
Then we say that $\mathbb{T}'$ is a \emph{subtheory} of $\mathbb{T}$ if $\mathcal{S}' \subseteq \mathcal{S}$, $\mathcal{F}' \subseteq \mathcal{F}$, $\mathcal{P}' \subseteq \mathcal{P}$ and $\mathcal{A}' \subseteq \mathcal{A}$.
If $\mathbb{T}'$ is a subtheory of a theory $\mathbb{T}$, then we often need to know when a theorem of $\mathbb{T}$ is a theorem of $\mathbb{T}'$.
The lemma below gives us a simple criterion for this.
First, we need to introduce a bit of notation.
Let $t$ is a term over the signature of $\mathbb{T}$ such that there is no subterm of a sort that does not belong to $\mathcal{S}'$.
Then we define a term $Ret(t)$ over the signature of $\mathbb{T}'$ as follows:
\begin{align*}
Ret(x) & = x \\
Ret(\sigma(t_1, \ldots t_n)) & = \sigma(Ret(t_1), \ldots Ret(t_n)) \text{, if $\sigma \in \mathcal{F}'$} \\
Ret(\sigma(t_1, \ldots t_n)) & = x_s \text{, if $\sigma \notin \mathcal{F}'$ and $\sigma : s_1 \times \ldots \times s_n \to s$}
\end{align*}
where $x_s$ is a variable of sort $s$ that is not a free variable of $t$.
If $\varphi$ is an atomic formula over the signature of $\mathbb{T}$, then we define a formula $Ret(\varphi)$ over the signature of $\mathbb{T}'$ as follows:
\begin{align*}
Ret(t = t') & = (Ret(t) = Ret(t')) \text{, if $Ret(t)$ and $Ret(t')$ are defined} \\
Ret(R(t_1, \ldots t_n)) & = R(Ret(t_1), \ldots Ret(t_n)) \text{, if $Ret(t_i)$ is defined for every $i$} \\
Ret(\varphi) & = \top \text{, otherwise}
\end{align*}
For an arbitrary Horn formula $\varphi$ we define $Ret(\varphi)$ as follows:
\[ Ret(\varphi_1 \land \ldots \land \varphi_n) = Ret(\varphi_1) \land \ldots \land Ret(\varphi_n) \]
For every restricted term $t|_\varphi$, let $Ret(t|_\varphi) = Ret(t)|_{Ret(\varphi)}$.
If $S$ is sequent $\varphi \sststile{}{V} \psi$ in the signature of $\mathbb{T}$,
then we define sequent $Ret(S)$ in the signature of $\mathbb{T}'$ as $Ret(\varphi) \sststile{}{V \cup FV(Ret(\varphi)) \cup FV(Ret(\psi))} Ret(\psi)$.
\begin{lem}[subtheory]
Let $\mathbb{T}'$ be a subtheory of $\mathbb{T}$.
Suppose that, for every axiom $S$ of $\mathbb{T}$, $Ret(S)$ is a theorem of $\mathbb{T}'$.
Then if a sequent in the signature of $\mathbb{T}'$ is provable in $\mathbb{T}$, then it is also provable in $\mathbb{T}'$.
\end{lem}
\begin{proof}
If $S$ is a sequent in the signature of $\mathbb{T}'$, then $Ret(S) = S$.
Thus we only need to prove that if $S$ is a theorem of $\mathbb{T}$, then $Ret(S)$ is a theorem of $\mathbb{T}'$.
For axioms this is true by assumption.
We need to check that $Ret(-)$ preserves inference rules.
This is clearly true for rules \axref{b1}-\axref{b6} and \axref{a1}.
Let us consider rule \axref{a2}.
Let $S$ equals $x = y \land \varphi \sststile{}{x:s,y:s,V} \varphi[y/x]$.
Note that $Ret(\varphi[y/x])$ is defined if and only if $Ret(\varphi)$ is defined, and in this case $Ret(\varphi[y/x]) = Ret(\varphi)[y/x]$.
Thus $Ret(S)$ is either of the form $x = y \land Ret(\varphi) \sststile{}{x:s,y:s,V,FV(Ret(\varphi))} Ret(\varphi)[y/x]$,
or of the form $x = y \sststile{}{x:s,y:s,V} \top$, or of the form $\top \sststile{}{x:s,y:s,V} \top$.
In all of these cases $Ret(S)$ is a theorem of $\mathbb{T}'$.
Finally, let us consider rule \axref{a3}.
To prove that it preserves the required property, it is enough to show that $\varphi$ is a formula of $(\mathcal{S}',\mathcal{F}',\mathcal{P}')$ if and only if $\varphi[t/x]$ is.
If $x \notin FV(\varphi)$, then $\varphi = \varphi[t/x]$.
Suppose that $x \in FV(\varphi)$ and $\varphi$ is a formula of $(\mathcal{S}',\mathcal{F}',\mathcal{P}')$.
If $x$ has sort $s$, then $s \in \mathcal{S}'$.
We need to show that a term of sort $s$ is a term of $(\mathcal{S}',\mathcal{F}',\mathcal{P}')$.
But this follows from the assumption on the set of function symbols.
\end{proof}
Sometimes it is convenient to have a sort which consists of a single element.
Let $\mathcal{S}$ be a set of sorts and let $s_0$ be a sort in $\mathcal{S}$.
Then we define a theory $\mathbb{T}_{s_0}$ which consists of a single function symbol $\emptyCtx : s_0$
and two axioms: $\sststile{}{} \emptyCtx\!\downarrow$ and $\sststile{}{x} x = \emptyCtx$.
Then, for every theory $\mathbb{T} \in \Th_\mathcal{S}$, there is at most one morphism from $\mathbb{T}_{s_0}$ to $\mathbb{T}$.
If such morphism exists, we will say that $s_0$ is \emph{trivial} in $\mathbb{T}$.
Thus $\mathbb{T}_{s_0}/\Th_\mathcal{S}$ is (equivalent to) a full subcategory of $\Th_\mathcal{S}$.
As an application of the previous results we will prove that adding a trivial sort does not change the category of theories.
Every theory $\mathbb{T} \in \Th_\mathcal{S}$ is naturally a theory in $\Th_{\mathcal{S} \amalg \{ s_0 \}}$.
Thus we have a functor $i : \Th_\mathcal{S} \to \mathbb{T}_{s_0}/\Th_{\mathcal{S} \amalg \{ s_0 \}}$ such that $i(\mathbb{T}) = \mathbb{T} \amalg \mathbb{T}_{s_0}$.
\begin{prop}[triv-sort]
Functor $i : \Th_\mathcal{S} \to \mathbb{T}_{s_0}/\Th_{\mathcal{S} \amalg \{ s_0 \}}$ is an equivalence of categories.
\end{prop}
\begin{proof}
Let $\mathbb{T}_1,\mathbb{T}_2 \in \Th_\mathcal{S}$ be theories with $P(\mathbb{T}_i) = (T_i,F_i,\mu_i)$, $i = 1,2$.
Let $\alpha,\beta : \mathbb{T}_1 \to \mathbb{T}_2$ be morphisms such that $i(\alpha) = i(\beta)$.
Then, for every $t \in T_1$, sequent $\sststile{}{V} i(\alpha)(t) \cong i(\beta)(t)$ is a theorem of $i(\mathbb{T}_2)$.
Since $\mathbb{T}_2$ is (isomorphic to) a subtheory of $i(\mathbb{T}_2)$, by \rlem{subtheory}, sequent $\sststile{}{V} \alpha(t) \cong \beta(t)$ is a theorem of $\mathbb{T}_2$.
Analogously, we can show that $\alpha(\varphi) \ssststile{}{V} \beta(\varphi)$ is a theorem of $\mathbb{T}_2$ for every $\varphi \in F_1$.
Thus $i$ is faithful.
Let $\alpha : i(\mathbb{T}_1) \to i(\mathbb{T}_2)$ be a morphism.
For every $t \in T_1(V)_s$, let $\beta(t) = Ret(\alpha(t))$ and, for every $\varphi \in F_1(V)$, let $\beta(\varphi) = Ret(\alpha(\varphi))$.
Since $Ret$ preserves substitution, $\land$ and $\top$, this defines a morphism $\beta : \mathbb{T}_1 \to \mathbb{T}_2$.
Since $s_0$ is trivial in $i(\mathbb{T}_2)$, $Ret(t) = t$ and $Ret(\varphi) = \varphi$ for every restricted term $t$ and every formula $\varphi$.
Thus $i(\beta) = \alpha$; hence $i$ is full.
Let $\mathbb{T} \in \Th_{\mathcal{S} \amalg \{ s_0 \}}$ be a theory with trivial $s_0$.
Then we define a theory $\mathbb{T}' \in \Th_\mathcal{S}$.
It has a predicate symbol $R : s'_1 \times \ldots \times s'_n$ for every predicate symbol $R : s_1 \times \ldots \times s_n$ of $\mathbb{T}$,
where $s'_1, \ldots s'_n$ is the subsequence of $s_1, \ldots s_n$ consisting of sorts from $\mathcal{S}$.
It has a function symbol $\sigma : s'_1 \times \ldots \times s'_n \to s$ for every function symbol
$\sigma : s_1 \times \ldots \times s_n \to s$ of $\mathbb{T}$ such that $s \in \mathcal{S}$.
Also, for every function symbol $\sigma : s_1 \times \ldots \times s_n \to s_0$ of $\mathbb{T}$,
there is a predicate symbol $R_\sigma : s_1' \times \ldots \times s'_n$ in $\mathbb{T}'$.
For every term $t$ of $\mathbb{T}$ of a sort from $\mathcal{S}$, we can define a term $r(t)$ of $\mathbb{T}'$.
Term $r(t)$ is obtained from $t$ by omitting subterms of sort $s_0$.
For every formula $\varphi$ of $\mathbb{T}$, we can define a formula $r(\varphi)$ of $\mathbb{T}'$:
\begin{align*}
r(t =_{s_0} t') & = \top \\
r(t =_s t') & = (r(t) =_s r(t')) \\
r(R(t_1, \ldots t_n)) & = R(r(t'_1), \ldots r(t'_n)) \\
r(\varphi_1 \land \ldots \land \varphi_n) & = r(\varphi_1) \land \ldots \land r(\varphi_n)
\end{align*}
where $t'_1, \ldots t'_n$ is the subsequence of $t_1, \ldots t_n$ consisting of the terms of sorts from $\mathcal{S}$.
Axioms of $\mathbb{T}'$ are sequents of the form $r(\varphi) \sststile{}{FV(r(\varphi)) \cup FV(r(\psi))} r(\psi)$ for every axiom $\varphi \sststile{}{V} \psi$ of $\mathbb{T}$.
It is easy to see that $i(\mathbb{T}')$ is isomorphic to $\mathbb{T}$.
Thus $i$ is essentially surjective on objects.
\end{proof}
% 3. Конструирование стабилизации что-то не пригодилось.
The construction of colimits in \rprop{th-cocomplete} implies that $L$ preserves colimits.
It follows that $\PSt_{\mathcal{S}_0}$ is cocomplete.
Since $L$ preserves colimits, the forgetful functor $\PSt_{\mathcal{S}_0} \to \mathbb{T}_{\mathcal{S}_0}/\Th_\mathcal{S}$
has a left adjoint $pst : \mathbb{T}_{\mathcal{S}_0}/\Th_\mathcal{S} \to \PSt_{\mathcal{S}_0}$, which we call the prestabilization functor.
More generally, for every $(\mathbb{T}_a,\alpha) \in \PSt_{\mathcal{S}_0}$,
we define a left adjoint $pst_{(\mathbb{T}_a,\alpha)} : \mathbb{T}_a/\Th_{\mathcal{S}} \to (\mathbb{T}_a,\alpha)/\PSt_{\mathcal{S}_0}$
to the forgetful functor $U_{(\mathbb{T}_a,\alpha)} : (\mathbb{T}_a,\alpha)/\PSt_{\mathcal{S}_0} \to \mathbb{T}_a/\Th_{\mathcal{S}}$.
Let $a : \mathbb{T}_a \to \mathbb{T}$ be an object of $\mathbb{T}_a/\Th_{\mathcal{S}}$.
Let $e : L^\infty(\mathbb{T}) \to E$ be the coequalizer of the following maps:
\[ \xymatrix{ \coprod\limits_{n \in \mathbb{N}} L^{n+1}(T_a) \ar@<+1ex>[rr]^{\coprod\limits_{n \in \mathbb{N}} L^n(f)} \ar@<-1ex>[rr]_{\coprod\limits_{n \in \mathbb{N}} L^n(g)}
& & \coprod\limits_{n \in \mathbb{N}} L^n(L^\infty(\mathbb{T})) \ar@{^{(}->}[r]^-{i^n} & L^\infty(\mathbb{T}) } \]
where $L^\infty(X)$ is the following colimit:
\[ X \to X \amalg L(X) \to X \amalg L(X \amalg L(X)) \to \ldots \]
and $f,g : L(\mathbb{T}_a) \to L^\infty(\mathbb{T})$ are defined as follows:
$f$ is the composite $L(\mathbb{T}_a) \xrightarrow{\alpha} \mathbb{T}_a \xrightarrow{a} \mathbb{T} \hookrightarrow L^\infty(\mathbb{T})$,
and $g$ is the composite $L(\mathbb{T}_a) \xrightarrow{L(a)} L(\mathbb{T}) \hookrightarrow L^\infty(\mathbb{T})$.
Since $L$ preserves colimits, $L(E)$ is a coequalizer of $i^{n+1} \circ \coprod_{n \in \mathbb{N}} L^{n+1}(f)$ and $i^{n+1} \circ \coprod_{n \in \mathbb{N}} L^{n+1}(g)$.
By the universal property of coequalizers we have a map $\beta : L(E) \to E$.
We define $pst_{(\mathbb{T}_a,\alpha)}(a)$ as $(E,\beta)$, and morphism $(\mathbb{T}_a,\alpha) \to (E,\beta)$
as the composite $\mathbb{T}_a \xrightarrow{a} \mathbb{T} \hookrightarrow L^\infty(\mathbb{T}) \xrightarrow{e} E$.
This map is a morphism of algebras for $L$ since $e$ coequalizes $f$ and $g$.
Moreover, if $(D,\delta)$ is an object of $(\mathbb{T}_a,\alpha)/\PSt_{\mathcal{S}_0} $,
then a map $L^\infty(\mathbb{T}) \to D$ is a morphism of algebras if and only if it factors through $E$.
It follows that $pst_{(\mathbb{T}_a,\alpha)}$ is left adjoint to $U_{(\mathbb{T}_a,\alpha)}$.
The categories of stable and $c$-stable theories are cocomplete.
The inclusion functors $\St_{\mathcal{S}_0} \to \PSt_{\mathcal{S}_0}$ and $\cSt_{\mathcal{S}_0} \to \PSt_{\mathcal{S}_0}$ have left adjoints,
which are defined as the functors that add the required stability axioms.
We call these left adjoints \emph{the stabilization functors}.
% 4. Два куска про локализацию моделей и правые морфизмы.
\section{Simplicial categories corresponding to a model}
In this section we construct two simplicial categories $C_R(M)$ and $L(M)$ corresponding to a model $M$ of a dependent type theory $T$.
We prove that they are equivalent to each other and to the simplicial localization of $M$ as a category with weak equivalences.
We will use the following notation in this section: $N : \Cat \to \sSet$ is the ordinary nerve functor,
$N_\Delta : \Cat_\Delta \to \sSet$ and $\mathfrak{C}_\Delta : \sSet \to \Cat_\Delta$ are the homotopy coherent nerve functor and its left adjoint defined in \cite{lurie-topos},
and $N_T : \Mod{T} \to \sSet$ and $\mathfrak{C}_T : \sSet \to \Mod{T}$ are the nerve functor and its left adjoint constructed in section~\ref{sec:nerve}.
\subsection{Space of right morphisms}
For every quasicategory $C$ and every pair of objects $X$ and $Y$ of $C$, there is a Kan complex $Hom^R_C(X,Y)$ of right morphisms from $X$ to $Y$.
The set $Hom^R_C(X,Y)_{[n]}$ is defined as the set of simplices $s \in C_{[n+1]}$ such that $s|_{\Delta^{\{n+1\}}} = Y$ and $s|_{\Delta^{\{0, \ldots n\}}}$ is a constant simplex at $X$.
The fact that this simplicial set is a Kan complex is proved in \cite[Proposition~1.2.2.3]{lurie-topos}.
If $C$ is an arbitrary quasicategory, then there is no natural composition map $Hom^R_C(Y,Z) \times Hom^R_C(X,Y) \to Hom^R_C(X,Z)$.
But if $C = N_T(M)$, then there is such a map and it is associative and unital.
Let $(O^1,H^1) \in N_T(M)_{[n+1]}$ and $(O^2,H^2) \in N_T(M)_{[n+1]}$ be functions that represent simplices in $Hom^R_{N_T(M)}(Y,Z)$ and $Hom^R_{N_T(M)}(X,Y)$, respectively.
Then we define functions $(O,H) \in N_T(M)_{[n+1]}$ representing a simplex in $Hom^R_{N_T(M)}(X,Z)$.
There is a unique way to define $O$: $O_{n+1} = Z$ and $O_i = X$ for every $0 \leq i \leq n$.
For every $J = \{ j_1 < \ldots < j_k \} \subseteq [n+1]$, let $x : X, x_{j_2} : I, \ldots x_{j_{k-1}} : I \vdash H_J : Z$ be the term defined as $H^1_J[y \repl H^2_J]$.
It is easy to see that relation~\eqref{rel:right} holds.
Let us verify relation~\eqref{rel:left}:
\begin{align*}
H_{J \cup \{j\}}[x_j \repl left] & = \\
H^1_{J \cup \{j\}}[x_j \repl left][y \repl H^2_{J \cup \{j\}}[x_j \repl left]] & = \\
H^1_{\{j < \ldots < j_k\}}[x \repl H^1_{\{j_1 < \ldots < j\}}][y \repl H^2_{\{j < \ldots j_k\}}[x \repl H^2_{\{j_1 < \ldots < j\}}]] & = \\
H^1_{\{j < \ldots < j_k\}}[y \repl H^2_{\{j < \ldots j_k\}}] & = \\
H^1_{\{j < \ldots < j_k\}}[y \repl H^2_{\{j < \ldots < j_k\}}][x \repl H^1_{\{j_1 < \ldots j\}}[y \repl H^2_{\{j_1 < \ldots j\}}]] & = \\
H_{\{j < \ldots < j_k\}}[x \repl H_{\{j_1 < \ldots j\}}] & ,
\end{align*}
where $H^1_{\{j_1 < \ldots < j\}} = y$ and $H^2_{\{j_1 < \ldots < j\}} = x$ by assumption that $H^i$ represent simplices that are constant on the left face.
It is easy to see that $H$ also satisfies this assumption and that this function respects the face and degeneracy maps.
Thus, we have defined a map of simplicial sets $Hom^R_{N_T(M)}(Y,Z) \times Hom^R_{N_T(M)}(X,Y) \to Hom^R_{N_T(M)}(X,Z)$.
The fact that it is associative and unital follows from the fact that these properties hold for the substitution operation.
Thus, we have a fibrant simplicial category, which we denote by $C_R(M)$.
\subsection{Simplicial localization of a model}
Every model $M$ of a theory $T$ with $\Id$-types and $\Sigma$-types has the underlying fibration category $C(M)$ (see \cite{tt-fibr-cat}).
In particular, $C(M)$ is a category with weak equivalences.
In this section we prove that the homotopy coherent nerve of the simplicial localization of $C(M)$ is categorically equivalent to the quasicategory $N_T(M)$.
For every model $M$ of a theory with $\Id$-types, we define a simplicial category $L(M)$ which is weakly equivalent to $L^H C(M)$,
the usual hammock localization of $C(M)$, defined in \cite{Dwyer1980}.
First, let us define a $\Cat$-enriched category $L_C(M)$.
Its objects are contexts of $M$, that is elements of $\coprod_{n \in \mathbb{N}} M_{(ctx,n)}$.
If $\Gamma$ and $\Delta$ are contexts of $M$, then objects of $Hom(\Gamma,\Delta)$ are spans of the following form:
\[ \xymatrix{ & \Gamma' \ar[ld] \ar[rd] & \\
\Gamma & & \Delta
} \]
where the right map is any context morphism and the left map is a trivial fibration, that is, $\Gamma' = (\Gamma, x_1 : A_1, \ldots x_n : A_n)$
and the map $\Gamma' \to \Gamma$ is a weak equivalence in $C(M)$.
We will denote such a span by $(\Gamma',p,f)$, where $p : \Gamma' \to \Gamma$ and $f : \Gamma' \to \Delta$.
Morphisms between two such spans $(\Gamma_1,p_1,f_1)$ and $(\Gamma_2,p_2,f_2)$ are maps $g : E_1 \to E_2$ such that $p_2 \circ g = p_1$ and $f_2 \circ g = f_1$.
The identity morphisms and compositions are defined in the obvious way.
This construction of $Hom(\Gamma,\Delta)$ appears in \cite{cis10b} and \cite{Nikolaus2015}.
The canonical inclusion $N(Hom(\Gamma,\Delta)) \to L^H C(M)(\Gamma,\Delta)$ is a weak equivalence.
This is proved in \cite[Proposition~3.23]{cis10b} and \cite[Theorem~3.61]{Nikolaus2015}.
This construction of $L_C(M)$ works for any fibration category, but gives us only a bicategory.
If $M$ is a model of a type theory, then we can show that it is actually a strict 2-category.
The identity morphism is $(\Gamma,id_\Gamma,id_\Gamma)$.
The composition $\circ : Hom(\Delta,E) \times Hom(\Gamma,\Delta) \to Hom(\Gamma,E)$ is defined as follows:
if $(\Gamma',p_1,f_1)$ is an object of $Hom(\Gamma,\Delta)$ and $(\Delta',p_2,f_2)$ is an object of $Hom(\Delta,E)$,
then let $(\Delta',p_2,f_2) \circ (\Gamma,p_1,f_1)$ be the following pullback:
\[ \xymatrix{ & & f_1^*(\Delta') \ar[dl] \ar[dr] & & \\
& \Gamma' \ar[dl]_{p_1} \ar[dr]^{f_1} & & \Delta' \ar[dl]_{p_2} \ar[dr]^{f_2} & \\
\Gamma & & \Delta & & E
} \]
The composition is defined in the obvious way on morphisms.
Since pullbacks in models of type theories are strictly associative, this composition functor is also associative.
Analogously, the identity morphism is a strict unit for the composition.
Let $L(M)$ be the simplicial category obtained from $L_C(M)$ by appyling the nerve functor to $Hom$-categories.
Then there is a functor $L^H C(M) \to L(M)$.
This functor is defined as the identity function on objects.
If $\Gamma$ and $\Delta$ are two objects, then 0-simplices of $L^H C(M)(\Gamma,\Delta)$ are reduced diagrams of the form
$\Gamma \xleftarrow{p_1} \Gamma_1' \xrightarrow{f_1} \Gamma_2 \xleftarrow{p_2} \ldots \xrightarrow{f_{n-1}} \Gamma_n \xleftarrow{p_n} \Gamma_n' \xrightarrow{f_n} \Delta$,
where $p_i$ are trivial fibrations.
Such a diagram is reduced if all the maps in it are not identity morphisms except possibly $p_1$ and $f_n$.
For every such diagram, we define an object of $Hom(\Gamma,\Delta)$ as $(\Gamma_n',p_n,f_n) \circ \ldots \circ (\Gamma_1',p_1,f_1)$.
Morphisms of such diagrams are 1-simplices of $L^H C(M)(\Gamma,\Delta)$.
Every such morphism induces a morphism in $Hom(\Gamma,\Delta)$ between the corresponding objects.
Finally, $n$-simplices of $L^H C(M)(\Gamma,\Delta)$ are sequences of such morphisms of length $n$.
Every such sequence induces a sequence of morphisms in $Hom(\Gamma,\Delta)$.
Since such sequences correspond to simplices in $L(M)$, we obtain a map $L^H C(M)(\Gamma,\Delta) \to L(M)(\Gamma,\Delta)$.
It is easy to see that this defines a functor $L^H C(M) \to L(M)$.
The functor $L^H C(M) \to L(M)$ acts as the identity function on objects and, for every pair of objects $\Gamma$ and $\Delta$,
the map $L^H C(M)(\Gamma,\Delta) \to L(M)(\Gamma,\Delta)$ is a retraction of the canonical inclusion $L(M)(\Gamma,\Delta) \to L^H C(M)(\Gamma,\Delta)$.
In particular, it is a weak equivalence.
It follows that the functor $L^H C(M) \to L(M)$ is a weak equivalence of simplicial categories.
% 5. Кусок про симплициальные теории типов.
\section{Simplicial type theories}
In this section we define regular theory $T_\Delta$ under $coe_1$.
We prove that for every theory $T$ under $B$, the categories of models of $T$ and $T \amalg_{coe_1} T_\Delta$ are Quillen equivalent.
The advantage of theories under $T_\Delta$ is that there is a forgetful functor from the category of models of simplicial type theories to the category of simplicial categories.
Theory $T_\Delta$ is a regular theory with the following function symbols:
\begin{align*}
\Delta^n & : (ty,0) \text{, for every } n \geq 0 \\
\Delta^1_l & : (tm,0) \\
\Delta^1_r & : (tm,0) \\
\cmap{f} & : (tm,0) \to (tm,0) \text{, for every map $f$ in $\Delta$} \\
fill^n_i & : (ty,1) \times (tm,1)^n \times (tm,0) \to (tm,0) \text{, for every } n \geq 1 \text{, } 0 \leq i \leq n
\end{align*}
Theory $T_\Delta$ has the following typing axioms:
\medskip
\begin{center}
\AxiomC{$\Gamma \vdash$}
\RightLabel{, for every $n \geq 1$}
\UnaryInfC{$\Gamma \vdash \Delta^n\ type$}
\DisplayProof
\qquad
\AxiomC{$\Gamma \vdash$}
\UnaryInfC{$\Gamma \vdash \Delta^1_l : \Delta^1$}
\DisplayProof
\qquad
\AxiomC{$\Gamma \vdash$}
\UnaryInfC{$\Gamma \vdash \Delta^1_r : \Delta^1$}
\DisplayProof
\end{center}
\medskip
\begin{center}
\AxiomC{$\Gamma \vdash a : \Delta^m$}
\RightLabel{, for every $f : \Delta^m \to \Delta^n$}
\UnaryInfC{$\Gamma \vdash \cmap{f}(a) : \Delta^n$}
\DisplayProof
\end{center}
\medskip
\begin{center}
\def\extraVskip{1pt}
\Axiom$\fCenter \Gamma, x : \Delta^n \vdash A\ type$
\noLine
\UnaryInf$\fCenter \Gamma, y : \Delta^{n-1} \vdash a_j : A[x \repl \cmap{\delta^n_j}(y)] \text{, for every } 0 \leq j \leq n, j \neq i$
\noLine
\UnaryInf$\fCenter \Gamma \vdash d : \Delta^n$
\noLine
\UnaryInf$\fCenter S^n_i$
\RightLabel{, for every $0 \leq i \leq n$}
\def\extraVskip{2pt}
\UnaryInf$\fCenter \Gamma \vdash fill^n_i(x. A, y.a_0, \ldots y.a_{\hat{i}}, \ldots y.a_n, d) : A[x \repl d]$
\DisplayProof
\end{center}
\medskip
where $S^n_i$ consists of equations $\Gamma, z : \Delta^{n-2} \vdash a_j[y \repl \cmap{\delta^{n-1}_{k-1}}(z)] \deq a_k[y \repl \cmap{\delta^{n-1}_j}(z)]$
for every $0 \leq j < k \leq n$ such that $j \neq i$ and $k \neq i$.
The theory also has the following equality axioms:
\medskip
\begin{center}
\AxiomC{$\Gamma \vdash a : \Delta^n$}
\UnaryInfC{$\Gamma \vdash \cmap{id}(a) \deq a : \Delta^n$}
\DisplayProof
\end{center}
\medskip
\begin{center}
\AxiomC{$\Gamma \vdash a : \Delta^m$}
\RightLabel{, $f : \Delta^m \to \Delta^n$, $f' : \Delta^n \to \Delta^k$}
\UnaryInfC{$\Gamma \vdash \cmap{f'}(\cmap{f}(a)) \deq \cmap{f' \circ f}(a) : \Delta^k$}
\DisplayProof
\end{center}
\medskip
Finally, for every $0 \leq i \leq n$ and $f : \Delta^m \to \Delta^n$ which factors through inclusion $\Lambda^n_i \to \Delta^n$, we have the following axiom:
\medskip
\begin{center}
\def\extraVskip{1pt}
\Axiom$\fCenter \Gamma, x : \Delta^n \vdash A\ type$
\noLine
\UnaryInf$\fCenter \Gamma, y : \Delta^{n-1} \vdash a_j : A[x \repl \cmap{\delta^n_j}(y)] \text{, for every } 0 \leq j \leq n, j \neq i$
\noLine
\UnaryInf$\fCenter \Gamma \vdash d : \Delta^m$
\noLine
\UnaryInf$\fCenter S^n_i$
\def\extraVskip{2pt}
\UnaryInf$\fCenter \Gamma \vdash fill^n_i(x.A, y.a_0, \ldots y.a_{\hat{i}}, \ldots y.a_n, \cmap{f}(d)) \deq a_k[y \repl \cmap{f'}(d)] : A[x \repl \cmap{f}(d)]$
\DisplayProof
\end{center}
where $k$ and $f'$ are such that $k \neq i$, $f = \delta^n_k \circ f'$, and $k$ is the minimal such number.
There is a morphism of theories $F : I \to T_\Delta$, which is defined as follows: $F(I) = \Delta^1$, $F(left) = \Delta^1_l$, and $F(right) = \Delta^1_r$.
This morphism has a retraction $G : T_\Delta \to I$, which is defined as follows: $G(\Delta^n) = I$, $G(\Delta^1_l) = left$, $G(\Delta^1_r) = right$, $G(\cmap{f})(a) = a$,
and $G(fill^n_i)(x.A, a_0, \ldots a_{i-1}, a_{i+1}, \ldots a_n, d) = a_k[y \repl d]$, where $k = 0$ if $i > 0$ and $k = 1$ if $i = 0$.
% 6. Более привычный синтаксис для алгебраических теорий типов.
We will give several proves by induction on a derivation of a sequent $\Gamma \vdash A \deq A'$ later in the paper.
Now, we describe another inference system in which it is easier to give such proofs.
The main problem with the system that we described is that it uses terms which contain a lot of redundant information.
The system that we are going to describe is also closer to the usual presentation of type theories.
Let $T$ be a contextual algebraic dependent type theory.
If we want to omit contexts in terms, then function symbols $ft$ and $ty$ do not make sense.
We can always infer the context of a term, but not its type, so we need an additional assumption on the theory.
We assume that, for every function symbol $\sigma : s_1 \times \ldots \times s_k \to (tm,n)$ of $T$,
there exists a term $\sigma_{ty}$ such that $ty$ does not appear in $\sigma_{ty}$ and the sequent $\sststile{}{x_1, \ldots x_k} ty(\sigma(x_1, \ldots x_k)) \cong \sigma_{ty}$ is derivable.
This is not a serious restriction since every theory is isomorphic to a theory with this property.
Indeed, we can just add a new function symbol $\sigma_{ty} : s_1 \times \ldots \times s_k \to (ty,n)$
and a new axiom $\sststile{}{x_1, \ldots x_k} ty(\sigma(x_1, \ldots x_k)) \cong \sigma_{ty}(x_1, \ldots x_k)$ for each function symbol $\sigma$ as above.
Let $\mathcal{C} = \{ ctx, tm \} \times \mathbb{N}$ be the set of sorts of $T$.
Let $Var$ be a set of variables and let $V$ be a $\mathcal{C}$-set of metavariables.
Then $Term^C_T(V)$ is the $\{ ty, tm \}$-set of (classes of $\alpha$-equivalence of) metaterms constructed inductively as follows:
\begin{enumerate}
\item If $x$ is a variable, then $x$ is a metaterm of sort $tm$.
\item If $z$ is a metavariable of sort $(tm,n)$ and $a_1, \ldots a_n$ are metaterms of sort $tm$, then $z[a_1, \ldots a_n]$ is a metaterm of sort $tm$.
\item If $z$ is a metavariable of sort $(p,n)$, $1 \leq m \leq n$, and $a_1, \ldots a_m$ are metaterms of sort $tm$, then $ft^{n-m}(e_p(z))[a_1, \ldots a_m]$ is a metaterm of sort $p$.
\item If $\sigma : (p_1,n_1) \times \ldots \times (p_k,n_k) \to (p,0)$ is a basic function symbol of $T$, $\{ A^j_i \}_{1 \leq i \leq k, 1 \leq j \leq n_i}$ are metaterm of sort $ty$,
$\{ x^j_i \}_{1 \leq i \leq k, 1 \leq j \leq n_i}$ are variables, and $b_1 : p_1, \ldots b_k : p_k$ are metaterms of the specified sorts,
then the following expression is a metaterm of sort $p$:
\[ \sigma((x^1_1 : A^1_1), \ldots (x^{n_1}_1 : A^{n_1}_1).\,b_1, \ldots ((x^1_k : A^1_k), \ldots (x^{n_k}_k : A^{n_k}_k).\,b_k) \]
\end{enumerate}
We will call metaterm of sort $ty$ \emph{types} and metaterms of sort $tm$ \emph{terms}.
We will sometimes omit metaterms in the expression $z[a_1, \ldots a_n]$.
In this case, the omitted metaterms are variables and it should be clear from the context the order of the variables (usually, this is just the order in which they appear in the context).
For example, the expression $\sigma(x y.\,A, z.\,A[x \repl z, y \repl z])$ is a shorthand for $\sigma(x y.\,A[x,y], z.\,A[x,y][x \repl z, y \repl z])$ (which equals to $\sigma(x y.\,A[x,y], z.\,A[z,z])$).
The set $FV(t)$ of free variable of a metaterm $t$ is defined as follows:
\begin{align*}
& FV(x) = \{ x \} \\
& FV(z[a_1, \ldots a_n]) = \bigcup_{1 \leq i \leq n} FV(a_i) \\
& FV(ft^{n-m}(e_p(z))[a_1, \ldots a_m]) = \bigcup_{1 \leq i \leq m} FV(a_i) \\
& FV(\sigma((x^1_1 : A^1_1), \ldots (x^{n_1}_1 : A^{n_1}_1).\,b_1, \ldots ((x^1_k : A^1_k), \ldots (x^{n_k}_k : A^{n_k}_k).\,b_k)) = \\
& \bigcup_{1 \leq i \leq k} (FV(b_i) \setminus \{ x^1_i, \ldots x^{n_i}_i \}) \cup (\bigcup_{1 \leq j \leq i} FV(A^j_i) \setminus \{ x^1_i, \ldots x^{j-1}_i \})
\end{align*}
The relation of $\alpha$-equivalence and the operation of substitution on metaterms are defined as usual.
A \emph{context} is an expression of the form $x_1 : A_1, \ldots x_n : A_n$, where $x_1, \ldots x_n$ are variables and $A_1, \ldots A_n$ are types such that $FV(A_i) \subseteq \{ x_1, \ldots x_{i-1} \}$.
We will say that a metaterm $t$ is \emph{appropriate} for a context $x_1 : A_1, \ldots x_n : A_n$ if $FV(t) \subseteq \{ x_1, \ldots x_n \}$.
A \emph{judgement} is an expression of one of the following forms:
\begin{align*}
\Gamma & \vdash A\ \type \\
\Gamma & \vdash a : A \\
\Gamma & \vdash A \deq A' \\
\Gamma & \vdash a \deq a' : A
\end{align*}
where $\Gamma$ is a context, $A$ and $A'$ are types appropriate for $\Gamma$, and $a$ and $a'$ are terms appropriate for $\Gamma$.
A \emph{formula} is a finite conjunction of judgements.
% 7. Общая лемма про объединения конфлюэнтных теорий.
\begin{lem}
Let $R_0$ and $R_1$ be two confluent term rewriting systems with the same set of terms such that the following conditions hold:
\begin{enumerate}
\item $R_1$ is stongly normalizable, that is every sequence of reductions $t_1 \Rightarrow_{R_1} t_2 \Rightarrow_{R_1} t_3 \Rightarrow_{R_1} \ldots$ is finite.
\item \label{it:red-comm} If $t \Rightarrow_{R_0}^* t_1$ and $t \Rightarrow_{R_1} t_2$, then there is a term $s$ such that $t_1 \Rightarrow_{R_1} s$ and $t_2 \Rightarrow_{R_1}^* \Rightarrow_{R_0}^* \Rightarrow_{R_1}^* s$.
\end{enumerate}
Then the union of $R_0$ and $R_1$ is confluent.
\end{lem}
\begin{proof}
For every triple of terms $t$, $t_1$, and $t_2$ together with two sequences of reductions $s_1$ and $s_2$ of the forms $t \Rightarrow_{R_0 \cup R_1}^* t_1$ and $t \Rightarrow_{R_0 \cup R_1}^* t_2$, respectively,
we define a size of quintuple $(t,t_1,t_2,s_1,s_2)$ as the pair $(l(s_1)+l(s_2),|t|)$, where $l(s)$ is the number of nontrivial blocks of the form $\Rightarrow_{R_0}^*$ separated by nontrivial blocks of the form $\Rightarrow_{R_1}^*$ in the sequence $s$
and $|t|$ is the maximum length of a reduction sequence of the form $\Rightarrow_{R_1}^*$ starting from the term $t$ (this is a well-defined function since $R_1$ is strongly normalizable).
The set of such pairs is ordered lexicographically.
We prove by induction on the size of $(t,t_1,t_2,s_1,s_2)$ that there exists term $t'$ together with sequences $s_1'$ and $s_2'$ of the forms
$t_2 \Rightarrow_{R_0 \cup R_1}^* t'$ and $t_1 \Rightarrow_{R_0 \cup R_1}^* t'$, respectively, such that $l(s_i') \leq l(s_i)$:
\[ \xymatrix@=1em{ & \ar[ld]_{l(s_1)} t \ar[rd]^{l(s_2)} & \\
t_1 \ar@{-->}[rd]_{\leq l(s_2)} & & t_2 \ar@{-->}[ld]^{\leq l(s_1)} \\
& t' &
} \]
If either of the sequences $s_1$ or $s_2$ is empty, then the result is obvious.
If the first nontrivial blocks in $s_1$ and $s_2$ are $\Rightarrow_{R_0}^*$, then we have the following reductions (we write the value of $l$ on the arrows):
\[ \xymatrix@=1em{ & & \ar[ld]_1 t \ar[rd]^1 & & \\
& t_1' \ar[ld]_{l(s_1)-1} \ar@{-->}[rd]_{\leq 1} & & t_2' \ar@{-->}[ld]^{\leq 1} \ar[rd]^{l(s_2)-1} & \\
t_1 \ar@{-->}[rd]_{\leq 1} & & t' \ar@{-->}[ld] \ar@{-->}[rd] & & t_2 \ar@{-->}[ld]^{\leq 1} \\
& t_1'' \ar@{-->}[rd]_{\leq l(s_2)-1} & & t_2'' \ar@{-->}[ld]^{\leq l(s_1)-1} & \\
& & t'' & &
} \]
A term $t'$ exists since $R_0$ is confluent and terms $t_1''$, $t_2''$, and $t''$ exist by the induction hypothesis since the sum of lengths of corresponding reduction sequences is less than $l(s_1) + l(s_2)$.
If the first nontrivial blocks in $s_1$ and $s_2$ are $\Rightarrow_{R_1}^*$, then we have the following reductions:
\[ \xymatrix@=1em{ & & \ar[ld]_0 t \ar[rd]^0 & & \\
& t_1' \ar[ld]_{l(s_1)} \ar@{-->}[rd]_0 & & t_2' \ar@{-->}[ld]^0 \ar[rd]^{l(s_2)} & \\
t_1 \ar@{-->}[rd]_0 & & t' \ar@{-->}[ld] \ar@{-->}[rd] & & t_2 \ar@{-->}[ld]^0 \\
& t_1'' \ar@{-->}[rd]_{\leq l(s_2)} & & t_2'' \ar@{-->}[ld]^{\leq l(s_1)} & \\
& & t'' & &