% !TEX root = hott_intro.tex

\section{Homotopy pullbacks}

Suppose we are given a map $f:A\to B$, and type families $P$ over $A$, and $Q$ over $B$.
Then any family of maps
\begin{equation*}
g:\prd{x:A}P(x)\to Q(f(x))
\end{equation*}
gives rise to a commuting square
\begin{equation*}
\begin{tikzcd}[column sep=large]
\sm{x:A}P(x) \arrow[r,"{\total[f]{g}}"] \arrow[d,swap,"\proj 1"] & \sm{y:B}Q(y) \arrow[d,"\proj 1"] \\
A \arrow[r,swap,"f"] & B
\end{tikzcd}
\end{equation*}
where $\total[f]{g}$ is defined as $\lam{(x,y)}(f(x),g(x,y))$. In the main theorem of this chapter we show that $g$ is a family of equivalences if and only if this square satisfies a certain universal property: the universal property of \emph{pullback squares}.

Pullback squares are of interest because they appear in many situations. Cartesian products, fibers of maps, and substitutions can all be presented as pullbacks. Moreover, the fact that a family of maps $g:\prd{x:A}P(x)\to Q(f(x))$ is a family of equivalences if and only if it induces a pullback square has the very useful corollary that a square of the form
\begin{equation*}
  \begin{tikzcd}
    C \arrow[d,swap,"p"] \arrow[r] & D \arrow[d,"q"] \\
    A \arrow[r,swap,"f"] & B
  \end{tikzcd}
\end{equation*}
is a pullback square if and only if the induced family of maps between the fibers
\begin{equation*}
  \prd{x:A}\fib{p}{x}\to\fib{q}{f(x)}
\end{equation*}
is a family of equivalences. This connection between pullbacks and \emph{fiberwise equivalences} has an important role in the descent theorem\index{descent} in \cref{chap:descent}.

A second reason for studying pullback squares is that the dual notion of \emph{pushouts} is an important tool to construct new types, including the $n$-spheres for arbitrary $n$. The duality of pullbacks and pushouts makes it possible to obtain proofs of many statements about pushouts from their dual statements about pullbacks.

\subsection{The universal property of pullbacks}

\begin{defn}\label{defn:cospan}
  A \define{cospan}\index{cospan|textbf} consists of three types $A$, $X$, and $B$, and maps $f:A\to X$ and $g:B\to X$.
\end{defn}

\begin{defn}
  Consider a cospan
  \begin{equation*}
    \begin{tikzcd}
      A \arrow[r,"f"] & X & B \arrow[l,swap,"g"]
    \end{tikzcd}
  \end{equation*}
  and a type $C$. A \define{cone}\index{cone!on a cospan|textbf} on the cospan $A \rightarrow X \leftarrow B$ with \define{vertex} $C$\index{vertex!of a cone|textbf} consists of maps $p:C\to A$, $q:C\to B$ and a homotopy $H:f\circ p\htpy g\circ q$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}
      C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
      A \arrow[r,swap,"f"] & X
    \end{tikzcd}
  \end{equation*}
  commutes. We write\index{cone(C)@{$\mathsf{cone}(\blank)$}|textbf}
\begin{equation*}
\mathsf{cone}(C)\defeq \sm{p:C\to A}{q:C\to B}f\circ p\htpy g\circ q
\end{equation*}
for the type of cones with vertex $C$.
\end{defn}

It is good practice to characterize the identity type of any type of importance. In the following lemma we give a characterization of the identity type of the type $\mathsf{cone}(C)$ of cones on $A\rightarrow X\leftarrow B$ with vertex $C$. Such characterizations are entirely routine in homotopy type theory.

\begin{lem}\label{lem:id_cone}%
\index{identity type!of cone@{of $\mathsf{cone}(C)$}|textit}%
Let $(p,q,H)$ and $(p',q',H')$ be cones on a cospan $f:A\rightarrow X \leftarrow B:g$, both with vertex $C$. Then the type $(p,q,H)=(p',q',H')$ is equivalent to the type of triples $(K,L,M)$ consisting of
\begin{align*}
K & : p \htpy p' \\
L & : q \htpy q'
\end{align*}
and a homotopy $M : \ct{H}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H'}$ witnessing that the square
\begin{equation*}
\begin{tikzcd}
f\circ p \arrow[r,"f\cdot K"] \arrow[d,swap,"H"] & f\circ p' \arrow[d,"{H'}"] \\
g\circ q \arrow[r,swap,"g\cdot L"] & g\circ q'
\end{tikzcd}
\end{equation*}
of homotopies commutes.
\end{lem}

\begin{comment}
\begin{rmk}
The homotopy $M$ is a homotopy of homotopies, and for each $z:C$ the identification $M(z)$ witnesses that the square of identifications
\begin{equation*}
\begin{tikzcd}[column sep=huge]
f(p(z)) \arrow[r,equals,"\ap{f}{K(z)}"] \arrow[d,equals,swap,"H(z)"] & f(p'(z)) \arrow[d,equals,"{H'(z)}"] \\
g(q(z)) \arrow[r,equals,swap,"\ap{g}{L(z)}"] & g(q'(z))
\end{tikzcd}
\end{equation*}
commutes.
\end{rmk}
\end{comment}

\begin{proof}
By the fundamental theorem of identity types (\cref{thm:id_fundamental}) it suffices to show that the type
\begin{equation*}
  \sm{(p',q',H'):\sm{p':C\to A}{q':C\to B}f\circ p'\htpy g \circ q'}{K:p\htpy p'}{L:q\htpy q'} \ct{H}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H'}
\end{equation*}
is contractible. Using associativity of $\Sigma$-types and commutativity of cartesian products, it is easy to show that this type is equivalent to the type
\begin{equation*}
  \sm{(p',K):\sm{p':C\to A}p\htpy p'}\sm{(q',L):\sm{q':C\to B}q\htpy q'}\sm{H:f\circ p'\htpy g \circ q'}\ct{H}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H'}
\end{equation*}
Now we observe that the types $\sm{p':C\to A}p\htpy p'$ and $\sm{q':C\to B}q\htpy q'$ are contractible, with centers of contraction
\begin{samepage}
\begin{align*}
(p,\mathsf{htpy\usc{}refl}_p) & : \sm{p':C'\to A} p\htpy p' \\
(q,\mathsf{htpy\usc{}refl}_q) & : \sm{q':C'\to B} q\htpy q'.
\end{align*}%
\end{samepage}%
Thus we apply \cref{ex:contr_in_sigma} to see that the type of tuples $((p',K),(q',L),(H',M))$ is equivalent to the type
\begin{equation*}
\sm{H':f\circ p'\htpy g\circ q'} \ct{H}{\mathsf{htpy\usc{}refl}_{g\circ q}}\htpy \ct{\mathsf{htpy\usc{}refl}_{f\circ p}}{H'}.
\end{equation*}
Of course, the type $\ct{H}{\mathsf{htpy\usc{}refl}_{g\circ q}}\htpy \ct{\mathsf{htpy\usc{}refl}_{f\circ p}}{H'}$ is equivalent to the type $H\htpy H'$, and $\sm{H':f\circ p\htpy g\circ q} H\htpy H'$ is contractible.
\end{proof}

Given a cone with vertex $C$ on a span $A\stackrel{f}{\rightarrow} X \stackrel{g}{\leftarrow} B$ and a map $h:C'\to C$, we construct a new cone with vertex $C'$ in the following definition.

\begin{defn}
For any cone $(p,q,H)$ with vertex $C$ and any type $C'$, we define a map\index{cone map@{$\mathsf{cone\usc{}map}$}|textbf}
\begin{equation*}
\mathsf{cone\usc{}map}(p,q,H):(C'\to C)\to\mathsf{cone}(C')
\end{equation*}
by $h\mapsto (p\circ h,q\circ h,H\circ h)$.
\end{defn}

\begin{defn}
We say that a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p\htpy g\circ q$ is a \define{pullback square}\index{pullback square|textbf}, or that it is \define{cartesian}\index{cartesian square|textbf}, if it satisfies the \define{universal property} of pullbacks\index{universal property!of pullbacks}, which asserts that the map
\begin{equation*}
\mathsf{cone\usc{}map}(p,q,H):(C'\to C)\to\mathsf{cone}(C')
\end{equation*}
is an equivalence for every type $C'$.
\end{defn}

We often indicate the universal property with a diagram as follows:
\begin{equation*}
\begin{tikzcd}
C' \arrow[drr,bend left=15,"{q'}"] \arrow[dr,densely dotted,"h"] \arrow[ddr,bend right=15,swap,"{p'}"] \\
& C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
& A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
since the universal property states that for every cone $(p',q',H')$ with vertex $C'$, the type of pairs $(h,\alpha)$ consisting of $h:C'\to C$ equipped with $\alpha:\mathsf{cone\usc{}map}((p,q,H),h)=(p',q',H')$ is contractible by \cref{thm:contr_equiv}.

As a corollary we obtain the following characterization of the universal property of pullbacks.

\begin{lem}\label{thm:pullback_up}
Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p\htpy g\circ q$
Then the following are equivalent:\index{universal property!of pullbacks (characterization)|textit}
\begin{enumerate}
\item The square is a pullback square.
\item For every type $C'$ and every cone $(p',q',H')$ with vertex $C'$, the type of quadruples $(h,K,L,M)$ consisting of a map $h:C'\to C$, homotopies
\begin{align*}
K & : p\circ h \htpy p' \\
L & : q\circ h \htpy q',
\end{align*}
and a homotopy $M : \ct{(H\cdot h)}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H'}$ witnessing that the square
\begin{equation*}
\begin{tikzcd}
f\circ p\circ h \arrow[r,"f\cdot K"] \arrow[d,swap,"H\cdot h"] & f\circ p' \arrow[d,"{H'}"] \\
g\circ q\circ h \arrow[r,swap,"g\cdot L"] & g\circ q'
\end{tikzcd}
\end{equation*}
commutes, is contractible.
\end{enumerate}
\end{lem}

\begin{proof}
The map $\mathsf{cone\usc{}map}(p,q,H)$ is an equivalence if and only if its fibers are contractible. By \cref{lem:id_cone} it follows that the fibers of $\mathsf{cone\usc{}map}(p,q,H)$ are equivalent the the described type of quadruples ($h,K,L,M)$.
\end{proof}

In the following lemma we establish the uniqueness of pullbacks up to equivalence via a \emph{3-for-2 property} for pullbacks.

\begin{lem}\label{lem:pb_3for2}\index{pullback!3-for-2 property|textit}\index{3-for-2 property!of pullbacks|textit}%
Consider the squares
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] & {C'} \arrow[r,"{q'}"] \arrow[d,swap,"{p'}"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X & A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with homotopies $H:f\circ p \htpy g\circ q$ and $H':f\circ p'\htpy g\circ q'$.
Furthermore, suppose we have a map $h:C'\to C$ equipped with
\begin{align*}
K & : p\circ h \htpy p' \\
L & : q\circ h \htpy q' \\
M & : \ct{(H\cdot h)}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H'}.
\end{align*}
If any two of the following three properties hold, so does the third:
\begin{samepage}%
\begin{enumerate}
\item $C$ is a pullback.
\item $C'$ is a pullback.
\item $h$ is an equivalence.
\end{enumerate}%
\end{samepage}%
\end{lem}

\begin{proof}
By the characterization of the identity type of $\mathsf{cone}(C')$ given in \cref{lem:id_cone} we obtain an identification
\begin{equation*}
\mathsf{cone\usc{}map}((p,q,H),h)=(p',q',H')
\end{equation*}
from the triple $(K,L,M)$.
Let $D$ be a type, and let $k:D\to C'$ be a map. We observe that
\begin{align*}
\mathsf{cone\usc{}map}((p,q,H),(h\circ k)) & \jdeq (p\circ (h\circ k),q\circ (h\circ k),H\circ (h\circ k)) \\
& \jdeq ((p\circ h)\circ k,(q\circ h)\circ k, (H\circ h)\circ k) \\
& \jdeq \mathsf{cone\usc{}map}(\mathsf{cone\usc{}map}((p,q,H),h),k) \\
& = \mathsf{cone\usc{}map}((p',q',H'),k).
\end{align*}
Thus we see that the triangle
\begin{equation*}
\begin{tikzcd}[column sep=-1em]
(D\to C') \arrow[rr,"{h\circ \blank}"] \arrow[dr,swap,"{\mathsf{cone\usc{}map}(p',q',H')}"] & & (D\to C) \arrow[dl,"{\mathsf{cone\usc{}map}(p,q,H)}"] \\
& \mathsf{cone}(D)
\end{tikzcd}
\end{equation*}
commutes. Therefore it follows from the 3-for-2 property of equivalences established in \cref{ex:3_for_2}, that if any two of the maps in this triangle is an equivalence, then so is the third. Now the claim follows from the fact that $h$ is an equivalence if and only if $h\circ\blank : (D\to C')\to (D\to C)$ is an equivalence for any type $D$, which was established in \cref{lem:postcomp_equiv}.
\end{proof}

Pullbacks are not only unique in the sense that any two pullbacks of the same cospan are equivalent, they are \emph{uniquely unique}\index{uniquely uniqueness!of pullbacks} in the sense that the type of quadruples $(h,K,L,M)$ as in \cref{lem:pb_3for2} is contractible.

\begin{cor}
Suppose both commuting squares
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] & {C'} \arrow[r,"{q'}"] \arrow[d,swap,"{p'}"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X & A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with homotopies $H:f\circ p \htpy g\circ q$ and $H':f\circ p'\htpy g\circ q'$ are pullback squares.
Then the type of quadruples $(e,K,L,M)$ consisting of an equivalence $e:\eqv{C'}{C}$ equipped with
\begin{align*}
K & : p\circ e \htpy p' \\
L & : q\circ e \htpy q' \\
M & : \ct{(H\cdot h)}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H'}.
\end{align*}
is contractible.
\end{cor}

\begin{proof}
We have seen that the type of quadruples $(h,K,L,M)$ is equivalent to the fiber of $\mathsf{cone\usc{}map}(p,q,H)$ at $(p',q',H')$. By \cref{lem:pb_3for2} it follows that $h$ is an equivalence. Since $\isequiv(h)$ is a proposition by \cref{ex:isprop_isequiv}, and hence contractible as soon as it is inhabited, it follows that the type of quadruples $(e,K,L,M)$ is contractible.
\end{proof}

\subsection{Canonical pullbacks}

For every cospan we can construct a \emph{canonical pullback}.

\begin{defn}
Let $f:A\to X$ and $g:B\to X$ be maps. Then we define
\begin{align*}
A\times_X B & \defeq \sm{x:A}{y:B}f(x)=g(y) \\
\pi_1 & \defeq \proj 1 & & : A\times_X B\to A \\
\pi_2 & \defeq \proj 1\circ\proj 2 & & : A\times_X B\to B\\
\pi_3 & \defeq \proj 2\circ\proj 2 & & : f\circ \pi_1 \htpy g\circ\pi_2.
\end{align*}
The type $A\times_X B$ is called the \define{canonical pullback}\index{canonical pullback|textbf} of $f$ and $g$.
\end{defn}

Note that $A\times_X B$ depends on $f$ and $g$, although this dependency is not visible in the notation.

\begin{rmk}
  Given $(x,y,p)$ and $(x',y',p')$ in the canonical pullback $A\times_X B$, the identity type $(x,y,p)=(x',y',p')$ is equivalent to the type of triples $(\alpha,\beta,\gamma)$ consisting of $\alpha:x=x'$, $\beta:y=y'$, and an identification $\gamma:\ct{p}{\ap{g}{\beta}}=\ct{\ap{f}{\alpha}}{p'}$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=large]
      f(x) \arrow[r,equal,"\ap{f}{\alpha}"] \arrow[d,swap,equal,"p"] & f(x') \arrow[d,equal,"{p'}"] \\
      g(y) \arrow[r,swap,equal,"\ap{g}{\beta}"] & g(y')
    \end{tikzcd}
  \end{equation*}
  commutes. The proof of this fact is similar to the proof of \cref{lem:id_cone}.
\end{rmk}

\begin{thm}\label{thm:canonical-pullback}
Given maps $f:A\to X$ and $g:B\to X$, the commuting square\index{canonical pullback|textit}
\begin{equation*}
\begin{tikzcd}
A\times_X B \arrow[r,"\pi_2"] \arrow[d,swap,"\pi_1"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X,
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{thm}

\begin{proof}
Let $C$ be a type. Our goal is to show that the map
\begin{equation*}
\mathsf{cone\usc{}map}(\pi_1,\pi_2,\pi_3): (C\to A\times_X B)\to \mathsf{cone}(C)
\end{equation*}
is an equivalence. Note that we have the commuting triangle
\begin{equation*}
  \begin{tikzcd}[column sep=-4em]
    C\to\sm{x:A}{y:B}f(x)=g(y) \arrow[dd,swap,"\mathsf{cone\usc{}map}"] \arrow[dr,"\mathsf{choice}"] \\
    & \sm{p:C\to A}\prd{z:C}\sm{y:B} f(p(z))= g(y) \arrow[dl,"\mathsf{choice}"] \\
    \sm{p:C\to A}{q:C\to B} f\circ p \htpy g\circ q.
  \end{tikzcd}
\end{equation*}
In this triangle the functions $\mathsf{choice}$ are equivalences by \cref{thm:choice}. Therefore, their composite is an equivalence.
\end{proof}

\begin{defn}
Given a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p \htpy g \circ q$, we define the \define{gap map}\index{gap map|textbf}\index{pullback!gap map|textbf}
\begin{equation*}
\mathsf{gap}(p,q,H):C \to A\times_X B
\end{equation*}
by $\lam{z}(p(z),q(z),H(z))$.
\end{defn}

The following theorem provides a useful characterization of pullback squares, because in many situations it is easier to show that the gap map is an equivalence.

\begin{thm}\label{thm:is_pullback}
Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p \htpy g \circ q$. The following are equivalent:
\begin{enumerate}
\item The square is a pullback square
\item There is a term of type
\begin{equation*}
\mathsf{is\usc{}pullback}(p,q,H)\defeq \isequiv(\mathsf{gap}(p,q,H)).
\end{equation*}
\end{enumerate}
\end{thm}

\begin{proof}
  Observe that we are in the situation of \cref{lem:pb_3for2}. Indeed, we have two commuting squares
  \begin{equation*}
    \begin{tikzcd}
      A\times_X B \arrow[r,"\pi_2"] \arrow[d,swap,"\pi_2"] & B \arrow[d,"g"] &[2em] C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
      A \arrow[r,swap,"f"] & X & A \arrow[r,swap,"f"] & X,
    \end{tikzcd}
  \end{equation*}
  and we have the gap map $\mathsf{gap}:C\to A\times_X B$, which comes equipped with the homotopies
\begin{align*}
K & : \pi_1\circ \mathsf{gap} \htpy p & K & \defeq \lam{z}\refl{p(z)} \\
L & : \pi_2\circ \mathsf{gap} \htpy q & L & \defeq \lam{z}\refl{q(z)} \\
M & : \ct{(\pi_3\cdot \mathsf{gap})}{(g\cdot L)} \htpy \ct{(f\cdot K)}{H} & M & \defeq \lam{z}\mathsf{right\usc{}unit}(H(z)).
\end{align*}
Since $A\times_X B$ is shown to be a pullback in \cref{thm:canonical-pullback}, it follows from \cref{lem:pb_3for2} that $C$ is a pullback if and only if the gap map is an equivalence.
\end{proof}

\subsection{Cartesian products and fiberwise products as pullbacks}

An important special case of pullbacks occurs when the cospan is of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[r] & \unit & B. \arrow[l]
\end{tikzcd}
\end{equation*}
In this case, the pullback is just the \emph{cartesian product}.

\begin{lem}\label{lem:prod_pb}
Let $A$ and $B$ be types. Then the square
\begin{equation*}
\begin{tikzcd}
A\times B \arrow[r,"\proj 2"] \arrow[d,swap,"\proj 1"] & B \arrow[d,"\mathsf{const}_{\ttt}"] \\
A \arrow[r,swap,"\mathsf{const}_{\ttt}"] & \unit
\end{tikzcd}
\end{equation*}
which commutes by the homotopy $\mathsf{const}_{\refl{\ttt}}$ is a pullback square.\index{cartesian product!as pullback}
\end{lem}

\begin{proof}
By \cref{thm:is_pullback} it suffices to show that
\begin{equation*}
\mathsf{gap}(\proj 1,\proj2,\lam{(a,b)}\refl{\ttt})
\end{equation*}
is an equivalence. Its inverse is the map $\lam{(a,b,p)}(a,b)$.
\end{proof}

The following generalization of \cref{lem:prod_pb} is the reason why pullbacks are sometimes called \define{fiber products}\index{fiber product|textbf}.

\begin{thm}
Let $P$ and $Q$ be families over a type $X$. Then the square
\begin{equation*}
\begin{tikzcd}[column sep=8em]
\sm{x:X}P(x)\times Q(x) \arrow[r,"{\lam{(x,(p,q))}(x,q)}"] \arrow[d,swap,"{\lam{(x,(p,q))}(x,p)}"] & \sm{x:X}Q(x) \arrow[d,"\proj 1"] \\
\sm{x:X}P(x) \arrow[r,swap,"\proj 1"] & X,
\end{tikzcd}
\end{equation*}
which commutes by the homotopy
\begin{equation*}
H\defeq \lam{(x,(p,q))}\refl{x},
\end{equation*}
is a pullback square.
\end{thm}

\begin{proof}
By \cref{thm:is_pullback} it suffices to show that the gap map is an equivalence. The gap map is homotopic to the function
\begin{equation*}
\lam{(x,(p,q))}((x,p),(x,q),\refl{x}).
\end{equation*}
It is easy to check that this function is an equivalence. I
ts inverse is the map
\begin{equation*}
\lam{((x,p),(y,q),\alpha)}(y,(\mathsf{tr}_P(\alpha,p),q)).\qedhere
\end{equation*}
\end{proof}

\begin{cor}
For any $f:A\to X$ and $g:B\to X$, the square
\begin{equation*}
\begin{tikzcd}[column sep=8em]
\sm{x:X}\fib{f}{x}\times\fib{g}{x} \arrow[r,"{\lam{(x,((a,p),(b,q)))}b}"] \arrow[d,swap,"{\lam{(x,((a,p),(b,q)))}a}"] & B \arrow[d,"g"]  \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{cor}

\subsection{Fibers of maps as pullbacks}

\begin{lem}\label{lem:fib_pb}
For any function $f:A\to B$, and any $b:B$, consider the square
\begin{equation*}
\begin{tikzcd}[column sep=large]
\fib{f}{b} \arrow[r,"\mathsf{const}_\ttt"] \arrow[d,swap,"\proj 1"] & \unit \arrow[d,"\mathsf{const}_b"] \\
A \arrow[r,swap,"f"] & B
\end{tikzcd}
\end{equation*}
which commutes by $\proj 2 : \prd{t:\fib{f}{b}} f(\proj 1(t))=b$. This is a pullback square.\index{fiber!as pullback|textit}
\end{lem}

\begin{proof}
By \cref{thm:is_pullback} it suffices to show that the gap map is an equivalence. The gap map is homotopic to the function
\begin{equation*}
\mathsf{total}(\lam{x}{p}(\ttt,p))
\end{equation*}
The map $\lam{x}{p}(\ttt,p)$ is a family of equivalences by \cref{ex:contr_in_sigma}, so it induces an equivalence on total spaces by \cref{thm:fib_equiv}.
\end{proof}

\begin{cor}
For any type family $B$ over $A$ and any $a:A$ the square
\begin{equation*}
\begin{tikzcd}[column sep=large]
B(a) \arrow[d,swap,"{\lam{y}(a,y)}"] \arrow[r,"\mathsf{const}_\ttt"] & \unit \arrow[d,"\lam{\ttt}a"] \\
\sm{x:A}B(x) \arrow[r,swap,"\proj 1"] & A
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{cor}

\begin{proof}
  To see this, note that the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      B(a) \arrow[rr,"{\lam{b}((a,b),\refl{a})}"] \arrow[dr,swap,"\mathsf{gap}"] & & \fib{\proj 1}{a} \arrow[dl,"\mathsf{gap}"] \\
      & \Big(\sm{x:A}B(x)\Big)\times_A\unit.
    \end{tikzcd}
  \end{equation*}
  Since the top map is an equivalence by \cref{ex:fib_replacement}, and the map on the right is an equivalence by \cref{lem:fib_pb}, it follows that the map on the left is an equivalence. The claim follows.
\end{proof}

\subsection{Families of equivalences}

\begin{lem}\label{lem:pb_subst}
Let $f:A\to B$, and let $Q$ be a type family over $B$. Then the square
\begin{equation*}
\begin{tikzcd}[column sep=6em]
\sm{x:A}Q(f(x)) \arrow[r,"{\lam{(x,q)}(f(x),q)}"] \arrow[d,swap,"\proj 1"] & \sm{y:B}Q(b) \arrow[d,"\proj 1"] \\
A \arrow[r,swap,"f"] & B
\end{tikzcd}
\end{equation*}
commutes by $H\defeq \lam{(x,q)}\refl{f(x)}$. This is a pullback square.\index{substitution!as pullback|textit}
\end{lem}

\begin{proof}
By \cref{thm:is_pullback} it suffices to show that the gap map is an equivalence. The gap map is homotopic to the function
\begin{equation*}
\lam{(x,q)}(x,(f(x),q),\refl{f(x)}).
\end{equation*}
The inverse of this map is given by $\lam{(x,((y,q),p))}(x,\mathsf{tr}_Q(p^{-1},q))$, and it is straightforward to see that these maps are indeed mutual inverses.
\end{proof}

\begin{thm}\label{thm:pb_fibequiv}
Let $f:A\to B$, and let $g:\prd{a:A}P(a)\to Q(f(a))$ be a family of maps\index{family of maps|textit}. The following are equivalent:
\begin{enumerate}
\item The commuting square
\begin{equation*}
\begin{tikzcd}[column sep=large]
\sm{a:A}P(a) \arrow[r,"{\total[f]{g}}"] \arrow[d,->>] & \sm{b:B}Q(b) \arrow[d,->>] \\
A \arrow[r,swap,"f"] & B
\end{tikzcd}
\end{equation*}
is a pullback square.
\item $g$ is a family of equivalences.\index{family of equivalences|textit}
\end{enumerate}
\end{thm}

\begin{proof}
The gap map is homotopic to the composite
\begin{equation*}
\begin{tikzcd}[column sep=large]
\sm{x:A}P(x) \arrow[r,"\total{g}"] & \sm{x:A}Q(f(x)) \arrow[r,"{\mathsf{gap}'}"] & A \times_B \Big(\sm{y:B}Q(y)\Big)
\end{tikzcd}
\end{equation*}
where $\mathsf{gap}'$ is the gap map for the square in \cref{lem:pb_subst}. Since $\mathsf{gap}'$ is an equivalence, it follows by \cref{ex:3_for_2,thm:fib_equiv} that the gap map is an equivalence if and only if $g$ is a family of equivalences.
\end{proof}

Our goal is now to extend \cref{thm:pb_fibequiv} to
arbitrary pullback squares. Note that every commuting
square
\begin{equation*}
\begin{tikzcd}
A \arrow[d,swap,"f"] \arrow[r,"h"] & B \arrow[d,"g"] \\
X \arrow[r,swap,"i"] & Y
\end{tikzcd}
\end{equation*}
with $H: i\circ f ~ g \circ h$ induces a map
\begin{equation*}
\mathsf{fib\usc{}sq} : \prd{x:X} \fib{f}{x} \to \fib{g}{f(x)}
\end{equation*}
on the fibers, by
\begin{equation*}
\mathsf{fib\usc{}sq}(x,(a,p))\defeq (h(a),\ct{H(a)^{-1}}{\ap{i}{p}}).
\end{equation*}

\begin{thm}\label{cor:pb_fibequiv}
Consider a commuting square
\begin{equation*}
\begin{tikzcd}
A \arrow[d,swap,"f"] \arrow[r,"h"] & B \arrow[d,"g"] \\
X \arrow[r,swap,"i"] & Y
\end{tikzcd}
\end{equation*}
with $H: i\circ f ~ g \circ h$. The following are equivalent:
\begin{enumerate}
\item The square is a pullback square.\index{pullback square!characterized by families of equivalences|textit}
\item The induced map on fibers
\begin{equation*}
\mathsf{fib\usc{}sq} : \prd{x:X} \fib{f}{x} \to \fib{g}{f(x)}
\end{equation*}
is a family of equivalences.
\end{enumerate}
\end{thm}

\begin{proof}
First we observe that the square
\begin{equation*}
\begin{tikzcd}[column sep=huge]
\sm{x:X}\fib{f}{x} \arrow[d,swap,"\eqvsym"] \arrow[r,"\total{\mathsf{fib\usc{}sq}}"] &
\sm{x:X}\fib{g}{f(x)} \arrow[d,"\total{\total{\mathsf{inv}}}"] \\
A \arrow[r,swap,"\mathsf{gap}"] & X \times_Y B
\end{tikzcd}
\end{equation*}
commutes. To construct such a homotopy, we need to construct an identification
\begin{equation*}
(f(a),h(a),H(a))=(x,h(a),(\ct{H(a)^{-1}}{\ap{i}{p}})^{-1})
\end{equation*}
for every $x : X$, $a : A$, and $p : f(a) = x$. This is shown by path induction on $p : f(a)=x$. Thus, it suffices to show that
\begin{equation*}
(f(a),h(a),H(a))=(f(a),h(a),(\ct{H(a)^{-1}}{\refl{i(f(a))}})^{-1}),
\end{equation*}
which is a routine exercise.

Now we note that the left and right maps in this square are both equivalences. Therefore it follows that the top map is an equivalence if and only if the bottom map is. The claim now follows by \cref{thm:fib_equiv}.
\end{proof}

\begin{cor}\label{cor:pb_trunc}
Consider a pullback square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X.
\end{tikzcd}
\end{equation*}
If $g$ is a $k$-truncated map, then so is $p$. In particular, if $g$ is an embedding then so is $p$.\index{truncated!map!pullbacks of truncated maps|textit}\index{embedding!pullbacks of embeddings|textit}
\end{cor}

\begin{proof}
Since the square is assumed to be a pullback square, it follows from \cref{cor:pb_fibequiv} that for each $x:A$, the fiber $\fib{p}{x}$ is equivalent to the fiber $\fib{g}{f(x)}$, which is $k$-truncated. Since $k$-truncated types are closed under equivalences by \cref{thm:ktype_eqv}, it follows that $p$ is a $k$-truncated map.
\end{proof}

\begin{cor}\label{cor:pb_equiv}
Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X.
\end{tikzcd}
\end{equation*}
and suppose that $g$ is an equivalence. Then the following are equivalent:
\begin{enumerate}
\item The square is a pullback square.
\item The map $p:C\to A$ is an equivalence.\index{equivalence!pullback of|textit}
\end{enumerate}
\end{cor}

\begin{proof}
If the square is a pullback square, then by \cref{thm:pb_fibequiv} the fibers of $p$ are equivalent to the fibers of $g$, which are contractible by \cref{thm:contr_equiv}. Thus it follows that $p$ is a contractible map, and hence that $p$ is an equivalence.

If $p$ is an equivalence, then by \cref{thm:contr_equiv} both $\fib{p}{x}$ and $\fib{g}{f(x)}$ are contractible for any $x:X$. It follows by \cref{ex:contr_equiv} that the induced map $\fib{p}{x}\to\fib{g}{f(x)}$ is an equivalence. Thus we apply \cref{cor:pb_fibequiv} to conclude that the square is a pullback.
\end{proof}

\begin{thm}\label{thm:pb_fibequiv_complete}
Consider a diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[d,swap,"f"] & B \arrow[d,"g"] \\
X \arrow[r,swap,"h"] & Y.
\end{tikzcd}
\end{equation*}
Then the type of triples $(i,H,p)$ consisting of a map $i:A\to B$, a homotopy $H:h\circ f\htpy g\circ i$, and a term $p$ witnessing that the square
\begin{equation*}
\begin{tikzcd}
A \arrow[d,swap,"f"] \arrow[r,"i"] & B \arrow[d,"g"] \\
X \arrow[r,swap,"h"] & Y.
\end{tikzcd}
\end{equation*}
is a pullback square, is equivalent to the type of families of equivalences
\begin{equation*}
\prd{x:X}\eqv{\fib{f}{x}}{\fib{g}{h(x)}}.
\end{equation*}
\end{thm}

\begin{cor}\label{cor:pb_fibequiv_complete}
Let $h:X\to Y$ be a map, and let $P$ and $Q$ be families over $X$ and $Y$, respectively.
Then the type of triples $(i,H,p)$ consisting of a map
\begin{equation*}
i:\Big(\sm{x:X}P(x)\Big)\to \Big(\sm{y:Y}Q(y)\Big),
\end{equation*}
a homotopy $H:h\circ \proj 1\htpy \proj 1\circ i$, and a term $p$ witnessing that the square
\begin{equation*}
\begin{tikzcd}
\sm{x:X}P(x) \arrow[d,swap,"\proj 1"] \arrow[r,"i"] & \sm{y:Y}Q(y) \arrow[d,"\proj 1"] \\
X \arrow[r,swap,"h"] & Y.
\end{tikzcd}
\end{equation*}
is a pullback square, is equivalent to the type of families of equivalences
\begin{equation*}
\prd{x:X}\eqv{P(x)}{Q(h(x))}.
\end{equation*}
\end{cor}

One useful application of the connection between pullbacks and families of equivalences is the following theorem, which is also called the \define{pasting property} of pullbacks.\index{pasting property!of pullbacks|textit}

\begin{thm}\label{thm:pb_pasting}
Consider a commuting diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[r,"k"] \arrow[d,swap,"f"] & B \arrow[r,"l"] \arrow[d,"g"] & C \arrow[d,"h"] \\
X \arrow[r,swap,"i"] & Y \arrow[r,swap,"j"] & Z
\end{tikzcd}
\end{equation*}
with homotopies $H:i\circ f\htpy g\circ k$ and $K:j\circ g\htpy h\circ l$, and the homotopy
\begin{equation*}
\ct{(j\cdot H)}{(K\cdot k)}:j\circ i\circ f\htpy h\circ l\circ k
\end{equation*}
witnessing that the outer rectangle commutes. Furthermore, suppose that the square on the right is a pullback square. Then the following are equivalent:
\begin{samepage}%
\begin{enumerate}
\item The square on the left is a pullback square.
\item The outer rectangle is a pullback square.
\end{enumerate}%
\end{samepage}%
\end{thm}

\begin{proof}
The commutativity of the two squares and the outer rectangle induces a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
\fib{f}{x} \arrow[rr,"\mathsf{fib\usc{}sq}_{(f,k,H)}(x)"] \arrow[dr,swap,"\mathsf{fib\usc{}sq}_{f,l\circ k,\ct{(j\cdot H)}{(K\cdot k)}}(x)"] & & \fib{g}{i(x)} \arrow[dl,"\mathsf{fib\usc{}sq}_{(g,l,K)}(i(x))"] \\
& \fib{h}{j(i(x))}.
\end{tikzcd}
\end{equation*}
A homotopy witnessing that the triangle commutes is constructed by a routine calculation.

Since the triangle commutes, and since the map $\mathsf{fib\usc{}sq}_{(g,l,K)}(i(x))$ is an equivalence for each $x:X$ by \cref{cor:pb_fibequiv}, it follows
by the 3-for-2 property of equivalences that for each $x:X$ the top map in the triangle is an equivalence if and only if the left map is an equivalence.
The claim now follows by a second application of \cref{cor:pb_fibequiv}.
\end{proof}

\subsection{Descent theorems for coproducts and \texorpdfstring{$\Sigma$}{Σ}-types}

\begin{thm}\label{thm:descent-coprod}
Consider maps $f:A'\to A$ and $g:B'\to B$, a map $h:X'\to X$, and commuting squares of the form
\begin{equation*}
\begin{tikzcd}
A' \arrow[r] \arrow[d,swap,"f"] & X' \arrow[d,"h"] & B' \arrow[r] \arrow[d,swap,"g"] & X' \arrow[d,"h"] \\
A \arrow[r] & X & B \arrow[r] & X.
\end{tikzcd}
\end{equation*}
Then the following are equivalent:
\begin{enumerate}
\item Both squares are pullback squares.
\item The commuting square
\begin{equation*}
\begin{tikzcd}
A'+B' \arrow[d,swap,"f+g"] \arrow[r] & X' \arrow[d,"h"] \\
A+B \arrow[r] & X
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{enumerate}
\end{thm}

\begin{proof}
By \cref{cor:pb_fibequiv} it suffices to show that the following are equivalent:
\begin{enumerate}
\item For each $x:A$ the map
\begin{equation*}
\mathsf{fib\usc{}sq}:\fib{f}{x}\to\fib{h}{\alpha_A(x)}
\end{equation*}
is an equivalence, and for each $y:B$ the map
\begin{equation*}
\mathsf{fib\usc{}sq}:\fib{g}{y}\to\fib{h}{\alpha_B(y)}
\end{equation*}
is an equivalence.
\item For each $t:A+B$ the map
\begin{equation*}
\mathsf{fib\usc{}sq}:\fib{f+g}{t} \to \fib{h}{\alpha(t)}
\end{equation*}
is an equivalence.
\end{enumerate}
By the dependent universal property of coproducts, the second claim is equivalent to the claim that both for each $x:A$ the map
\begin{equation*}
\mathsf{fib\usc{}sq}:\fib{f+g}{\inl(x)}\to \fib{h}{\alpha_A(x)}
\end{equation*}
is an equivalence, and for each $y:B$, the map
\begin{equation*}
\mathsf{fib\usc{}sq}:\fib{f+g}{\inr(y)}\to \fib{h}{\alpha_B(y)}
\end{equation*}
is an equivalence.

We claim that there is a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=-1em]
\fib{f}{x} \arrow[rr] \arrow[dr] & & \fib{f+g}{\inl(x)} \arrow[dl] \\
\phantom{\fib{f+g}{\inl(x)}} & \fib{h}{\alpha_A(x)}
\end{tikzcd}
\end{equation*}
for every $x:A$. To see that the triangle commutes, we need to construct an identification

The top map is given by
\begin{equation*}
(a',p)\mapsto (\inl(a'),\ap{\inl}{p}).
\end{equation*}
The triangle then commutes by the homotopy
\begin{equation*}
(a',p)\mapsto \mathsf{eq\usc{}pair}(\refl,\ap{\mathsf{concat}(H(a')^{-1})}{\mathsf{ap\usc{}comp}_{[\alpha_A,\alpha_B],inl}})
\end{equation*}
We note that the top map is an equivalence, so it follows by the 3-for-2 property of equivalences that the left map is an equivalence if and only if the right map is an equivalence.

Similarly, there is a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=-1em]
\fib{g}{y} \arrow[rr] \arrow[dr] & & \fib{f+g}{\inr(y)} \arrow[dl] \\
\phantom{\fib{f+g}{\inr(y)}} & \fib{h}{\alpha_B(y)}
\end{tikzcd}
\end{equation*}
in which the top map is an equivalence, completing the proof.
\end{proof}

In the following corollary we conclude that coproducts distribute over pullbacks.

\begin{cor}
Consider a cospan of the form
\begin{equation*}
\begin{tikzcd}
& Y \arrow[d] \\
A+B \arrow[r] & X.
\end{tikzcd}
\end{equation*}
Then there is an equivalence
\begin{equation*}
(A+B)\times_X Y \simeq (A\times_X Y)+(B\times_X Y).
\end{equation*}
\end{cor}

\begin{thm}\label{thm:descent-Sigma}
Consider a family of maps $f_i:A'_i\to A_i$ indexed by a type $I$, a map $h:X'\to X$, and a commuting square
\begin{equation*}
\begin{tikzcd}
A'_i \arrow[r] \arrow[d,swap,"f_i"] & X' \arrow[d,"h"] \\
A_i \arrow[r,swap,"\alpha_i"] & X
\end{tikzcd}
\end{equation*}
for each $i:I$. Then the following are equivalent:
\begin{enumerate}
\item For each $i:I$ the square is a pullback square.
\item The commuting square
\begin{equation*}
\begin{tikzcd}
\sm{i:I}A'_i \arrow[d,swap,"\total{f}"] \arrow[r] & X' \arrow[d,"h"] \\
\sm{i:I}A_i \arrow[r,"\alpha"] & X
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{enumerate}
\end{thm}

\begin{proof}
By \cref{cor:pb_fibequiv} it suffices to show that the following are equivalent for each $i:I$ and $a:A_i$:
\begin{enumerate}
\item The map
\begin{equation*}
\mathsf{fib\usc{}sq} : \fib{f_i}{a} \to \fib{g}{\alpha_i(a)}
\end{equation*}
is an equivalence.
\item The map
\begin{equation*}
\mathsf{fib\usc{}sq} : \fib{\total{f}}{i,a}\to \fib{g}{\alpha_i(a)}
\end{equation*}
is an equivalence.
\end{enumerate}
To see this, note that we have a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=-1em]
\fib{f_i}{a} \arrow[rr] \arrow[dr] & & \fib{\total{f}}{i,a} \arrow[dl] \\
\phantom{\fib{\total{f}}{i,a}} & \fib{g}{\alpha_i(a)},
\end{tikzcd}
\end{equation*}
where the top map is an equivalence by \cref{lem:fib_total}. Therefore the claim follows by the 3-for-2 property of equivalences.
\end{proof}

In the following corollary we conclude that $\Sigma$ distributes over coproducts.

\begin{cor}
Consider a cospan of the form
\begin{equation*}
\begin{tikzcd}
& Y \arrow[d] \\
\sm{i:I}A_i \arrow[r] & X.
\end{tikzcd}
\end{equation*}
Then there is an equivalence
\begin{equation*}
\Big(\sm{i:I}A_i\Big)\times_X Y \simeq \sm{i:I} (A_i\times_X Y).
\end{equation*}
\end{cor}

\begin{exercises}
\item \label{ex:id_pb}\index{identity type!as pullback}
\begin{subexenum}
\item Show that the square\index{identity type!as pullback}
\begin{equation*}
\begin{tikzcd}
(x=y) \arrow[r] \arrow[d] & \unit \arrow[d,"\mathsf{const}_y"] \\
\unit \arrow[r,swap,"\mathsf{const}_x"] & A
\end{tikzcd}
\end{equation*}
is a pullback square.
\item Show that the square\index{diagonal!of a type!fibers of}
\begin{equation*}
\begin{tikzcd}[column sep=large]
(x=y) \arrow[r,"\mathsf{const}_{x}"] \arrow[d,swap,"\mathsf{const}_\ttt"] & A \arrow[d,"\delta_A"] \\
\unit \arrow[r,swap,"{\mathsf{const}_{(x,y)}}"] & A\times A
\end{tikzcd}
\end{equation*}
is a pullback square, where $\delta_A:A\to A\times A$ is the diagonal of $A$, defined in \cref{ex:diagonal}.
\end{subexenum}
\item \label{ex:trunc_diagonal_map}In this exercise we give an alternative characterization of the notion of $k$-truncated map, compared to \cref{thm:trunc_ap}. Given a map $f:A\to X$ define the \define{diagonal}\index{diagonal!of a map} of $f$ to be the map $\delta_f:A\to A\times_X A$ given by $x\mapsto (x,x,\refl{f(x)})$.
\begin{subexenum}
\item Construct an equivalence
\begin{equation*}
\eqv{\fib{\delta_f}{(x,y,p)}}{\fib{\apfunc{f}}{p}}
\end{equation*}
to show that the square\index{action on paths!fibers of}\index{diagonal!of a map!fibers of}
\begin{equation*}
\begin{tikzcd}[column sep=large]
\fib{\apfunc{f}}{p} \arrow[r,"\mathsf{const}_x"] \arrow[d,swap,"\mathsf{const}_\ttt"] & A \arrow[d,"\delta_f"] \\
\unit \arrow[r,swap,"{\mathsf{const}_{(x,y,p)}}"] & A\times_X A
\end{tikzcd}
\end{equation*}
is a pullback square, for every $x,y:A$ and $p:f(x)=f(y)$.
\item Show that a map $f:A\to X$ is $(k+1)$-truncated if and only if $\delta_f$ is $k$-truncated.\index{truncated!map!by truncatedness of diagonal}
\end{subexenum}
Conclude that $f$ is an embedding if and only if $\delta_f$ is an equivalence.\index{embedding!diagonal is an equivalence}
\item Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p\htpy g\circ q$. Show that this square is a pullback square if and only if the square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"p"] \arrow[d,swap,"q"] & A \arrow[d,"f"] \\
B \arrow[r,swap,"g"] & X
\end{tikzcd}
\end{equation*}
with $H^{-1}:g\circ q\htpy f\circ p$ is a pullback square.
\item Show that any square of the form
\begin{equation*}
\begin{tikzcd}
C \arrow[r] \arrow[d] & B \arrow[d] \\
\emptyt \arrow[r] & X
\end{tikzcd}
\end{equation*}
commutes and is a pullback square. This is the \emph{descent property} of the empty type.\index{descent!empty type}
\item Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p\htpy g\circ q$. Show that the following are equivalent:
\begin{enumerate}
\item The square is a pullback square.
\item For every type $T$, the commuting square
\begin{equation*}
\begin{tikzcd}
C^T \arrow[r,"q\circ\blank"] \arrow[d,swap,"p\circ\blank"] & B^T \arrow[d,"g\circ\blank"] \\
A^T \arrow[r,swap,"f\circ\blank"] & X^T
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{enumerate}
Note: property (ii) is really just a rephrasing of the universal property of pullbacks.\index{pullback square!universal property}
\item \label{ex:pb_diagonal}Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X
\end{tikzcd}
\end{equation*}
with $H:f\circ p\htpy g\circ q$. Show that the following are equivalent:
\begin{enumerate}
\item The square is a pullback square.
\item The square
\begin{equation*}
\begin{tikzcd}
C \arrow[r,"g\circ q"] \arrow[d,swap,"{\lam{x}(p(x),q(x))}"] & X \arrow[d,"\delta_X"] \\
A\times B \arrow[r,swap,"f\times g"] & X\times X
\end{tikzcd}
\end{equation*}
which commutes by $\lam{z}\mathsf{eq\usc{}pair}(H(z),\refl{g(q(z))})$ is a pullback square.
\end{enumerate}
\item \label{ex:pb_prod}Consider two commuting squares\index{pullback!cartesian products of pullbacks}
\begin{equation*}
\begin{tikzcd}
C_1 \arrow[r] \arrow[d] & B_1 \arrow[d] & C_2 \arrow[r] \arrow[d] & B_2 \arrow[d] \\
A_1 \arrow[r] & X_1 & A_2 \arrow[r] & X_2.
\end{tikzcd}
\end{equation*}
\begin{subexenum}
\item Show that if both squares are pullback squares, then the square
\begin{equation*}
\begin{tikzcd}
C_1\times C_2 \arrow[r] \arrow[d] & B_1\times B_2 \arrow[d] \\
A_1 \times A_2 \arrow[r] & X_1\times X_2.
\end{tikzcd}
\end{equation*}
is also a pullback square.
\item Show that if there are terms $t_1:A_1\times_{X_1}B_1$ and $t_2:A_2\times_{X_2}B_2$, then the converse of (a) also holds.
\end{subexenum}
\item Consider for each $i:I$ a pullback square
\begin{equation*}
\begin{tikzcd}
C_i \arrow[r,"q_i"] \arrow[d,swap,"p_i"] & B_i \arrow[d,"g_i"] \\
A_i \arrow[r,swap,"f_i"] & X_i
\end{tikzcd}
\end{equation*}
with $H_i: f_i\circ p_i\htpy g_i\circ q_i$.\index{pullback!Pi-type of pullbacks@{$\Pi$-type of pullbacks}}\label{ex:pb_pi}
Show that the commuting square
\begin{equation*}
\begin{tikzcd}
\prd{i:I}C_i \arrow[r] \arrow[d] & \prd{i:I}B_i \arrow[d] \\
\prd{i:I}A_i \arrow[r] & \prd{i:I}X_i
\end{tikzcd}
\end{equation*}
is a pullback square.
%\item
%\begin{subexenum}
%\item Show that \index{equivalence!type of equivalences!as pullback}
%\begin{equation*}
%\begin{tikzcd}[column sep=8em]
%\eqv{A}{B} \arrow[r] \arrow[d] & \unit \arrow[d,"{(\idfunc[A],\idfunc[B])}"] \\
%A^B\times B^A \times A^B \arrow[r,swap,"{(h,f,g)\mapsto (h\circ f,f\circ g)}"] & A^A \times B^B
%\end{tikzcd}
%\end{equation*}
%is a pullback square.
%\item Show that \index{contractible!type of contractibility!as pullback}
%\begin{equation*}
%\begin{tikzcd}[column sep=6em]
%\iscontr(A) \arrow[r,"\mathsf{const}_{\ttt}"] \arrow[d,swap,"\proj 1"] & \unit \arrow[d,"{\lam{\ttt}\idfunc[A]}"] \\
%A \arrow[r,swap,"{\lam{x}\mathsf{const}_x}"] & A^A
%\end{tikzcd}
%\end{equation*}
%is a pullback square.
%\end{subexenum}
%\item Consider a commuting square
%\begin{equation*}
%\begin{tikzcd}
%C \arrow[r] \arrow[d] & A \arrow[d] \\
%B \arrow[r] & X.
%\end{tikzcd}
%\end{equation*}
%Show that this square is cartesian if and only if the induced map $C\to A\times_X B$ has a retraction.
%\end{subexenum}
%\item Suppose that the squares
%\begin{equation*}
%\begin{tikzcd}
%C \arrow[r,"q"] \arrow[d,swap,"p"] & B \arrow[d,"g"] & {C'} \arrow[r,"{q'}"] \arrow[d,swap,"{p'}"] & B \arrow[d,"g"] \\
%A \arrow[r,swap,"f"] & X & A \arrow[r,swap,"f"] & X
%\end{tikzcd}
%\end{equation*}
%with homotopies $H:f\circ p \htpy g\circ q$ and $H':f\circ p'\htpy g\circ q'$ are both pullback squares. Show that the type of equivalences $e:\eqv{C'}{C}$ equipped with an identification
%\begin{equation*}
%\mathsf{cone\usc{}map}((p,q,H),e)=(p',q',H')
%\end{equation*}
%is contractible.
\begin{comment}
\item Consider a \define{natural transformation of cospans}\index{cospan!natural transformation of}, i.e., a commuting diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[r,"f"] \arrow[d,swap,"i"] & X \arrow[d,swap,"j"] & B \arrow[l,swap,"g"] \arrow[d,"k"] \\
A' \arrow[r,swap,"{f'}"] & X' & B'. \arrow[l,"{g'}"]
\end{tikzcd}
\end{equation*}
Show that the map
\begin{equation*}
(a,b,p)\mapsto (i(a),j(b),\mathsf{ap}_k(p)): A \times_X B \to A'\times_{X'} B'
\end{equation*}
is $k$-truncated if each of the vertical maps is.
\end{comment}
\begin{comment}
\item \label{ex:pb_fib}Consider a commuting square
\begin{equation*}
\begin{tikzcd}
C \arrow[d,swap,"p"] \arrow[r,"q"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & X.
\end{tikzcd}
\end{equation*}
with $H:f\circ p\htpy g\circ q$, and let $h:C\to A\times_X B$ be the map given by $h(z)\defeq (p(c),q(c),H(c))$.
Show that the square
\begin{equation*}
\begin{tikzcd}[column sep=6.5em]
\fib{\mathsf{gap}(p,q,H)}{(a,b,\alpha)} \arrow[d,swap,"\mathsf{const}_{\ttt}"] \arrow[r,"{\lam{(c,\beta)}(c,\ap{\pi_1}{\beta})}"] & \fib{p}{a} \arrow[d,"{\fibf{(f,g,H)}}"] \\
\unit \arrow[r,swap,"\mathsf{const}_{(b,\alpha^{-1})}"] & \fib{g}{f(a)}
\end{tikzcd}
\end{equation*}
\end{comment}
\item \label{ex:pb_3by3}Consider a commuting diagram of the form
\begin{equation*}
\begin{tikzcd}
A_0 \arrow[r] \arrow[d] & B_0 \arrow[d] & C_0 \arrow[l] \arrow[d] \\
A_1 \arrow[r] & B_1 & C_1 \arrow[l] \\
A_2 \arrow[u] \arrow[r] & B_2 \arrow[u] & C_2 \arrow[u] \arrow[l]
\end{tikzcd}
\end{equation*}
with homotopies filling the (small) squares. Construct an equivalence
\begin{align*}
& (A_0\times_{B_0} C_0) \times_{(A_1\times_{B_1} C_1)} (A_2\times_{B_2} C_2) \\
& \qquad \eqvsym (A_0\times_{A_1} A_2) \times_{(B_0\times_{B_1} B_2)} (C_0\times_{C_1} C_2).
\end{align*}
This is also known as the \define{3-by-3 lemma}\index{3-by-3 lemma!for pullbacks} for pullbacks.
\end{exercises}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 1}

A stack supports two operations: \textsc{Push} and \textsc{Pop}.
Implementing the two operations using a linked-list takes $\mathcal{O}(1)$ time per operation.
Suppose we are given two stacks, $A$ and $B$ with $n$ and $m$ elements, respectively.
We want to implement the following operations:
\begin{description}\itemsep=0pt
\item[\textsc{Push}($A, x$)] Push $x$ elements into $A$.
\item[\textsc{Push}($B, x$)] Push $x$ elements into $B$.
\item[\textsc{MultipopA($k$)}] pop $\text{min}\{k,n\}$ elements from $A$.
\item[\textsc{MultipopB($k$)}] pop $\text{min}\{k,m\}$ elements from $B$.
\item[\textsc{Transfer($k$)}] repeatedly pop an element from $A$ and push it to $B$ until either $k$ elements have been transferred or $A$ is empty.
\end{description}
\begin{enumerate}[label=(\alph*)]
\item What is the worst-case running time of \textsc{MultipopA}, \textsc{MultipopB} and \textsc{Transfer}?
\item Show that the amortized running time per operation is $\mathcal{O}(1)$ for a sequence of \textsc{Push}($A,n$) and \textsc{Transfer}($n$).
You may use any technique shown in class on a sequence on $n$ operations.
\end{enumerate}

\subsection*{Solution}

\begin{enumerate}[label=(\alph*)]
\item Operations \textsc{Push}($A,x$) and \textsc{Push}($B,x$) have a runtime cost of $\mathcal{O}(x)$ as \textsc{Push}() operation with runtime $\mathcal{O}(1)$ should be performed for $x$ times.

Operations \textsc{MultipopA}($k$) and \textsc{MultipopB}($k$) both repeat the \textsc{Pop()} operation for $\text{min}(k,n)$ and $\text{min}(k,m)$ times, respectively.
In the worst-case where $n,m < k$, the operations are limited by the number of elements in the stacks.
Therefore, their worst-case runtime cost are $\mathcal{O}(n)$ and $\mathcal{O}(m)$, respectively.

Operation \textsc{Transfer($k$)} has a worst-case runtime of $\mathcal{O}(k)$, since worst scenario happens when $k < n$ and $k < m$ and there will be $k$ items to \textsc{Pop()} from stack $A$ and $k$ items to \textsc{Push()} to stack $B$.

\item For \textsc{Push}($A,n$), amortized runtime would be constant $C$ where $C$ is the cost to push one element to a stack.
This can be shown easily using the aggregate method as shown in Eq. \ref{eq11}.

\begin{equation}
\sum_{i=1}^{n} c_i = \sum_{i=1}^{n} c = c \sum_{i=1}^{n} 1 = nc \Rightarrow \text{Amortized Cost} = \frac{nc}{n} = c
\label{eq11}
\end{equation}

For \textsc{Transfer($k$)} operation, the worst case happens when there are at least $k$ elements in stack $k$, in which case amortized cost of the operation can be obtained as given in Eq. \ref{eq12}.

\begin{equation}
\sum_{i=1}^{k} c_i = \sum_{i=1}^{k}(c_1 + c_2) = (c_1 + c_2) \sum_{i=1}^{k} 1 = (c_1 + c_2) k
\label{eq12}
\end{equation}

Since cost of \textsc{Pop()} ($c_1$) and \textsc{Push()} ($c_2$) are $\mathcal{O}(1)$, amortized cost of \textsc{Transfer($k$)} is obtained from \ref{eq12}.

\begin{equation}
\text{Amortized Cost} = \frac{k(c_1 + c_2)}{k} = c_1 + c_2 = 2
\end{equation}

\end{enumerate}


\section{Mathematical basics}
This section is going to describe the mathematical concepts of an general computer algebra library which shall be implemented. Although a swath of computer algebra programs is available none of these programs comes with a library the an interested user may simply include in his/her own project.\\
\indent In addition, as anyone can imagine no single program can provide all objecs which are currently investigated. Worse, the user is forced to delve into each programs programming structures and restrictions and potentially has to change the program if desires and needs are not met.
\subsection{Categories and functor}
As we briefly outlined in the first chapter, we discuss the functorial approach. Firstly, we like to explain some the features needed.
\subsubsection{Basic concepts}
We will denote an arbitrary category with $\mathcal{C}$.
\paragraph{Categories} Categories comprise of three concepts or data encoding it:
\begin{description}
\item[Objects] are the instances of a category. They are not necessarily sets in a set theoretic sense rather classes. They are denoted by $\mathrm{Obj}_{\mathcal{C}}$ or $\objc$.
\item[Morphisms] are maps connecting objects in a category sometimes trivially called arrows. These maps are not arbitrary but preserve or respect the structure of each object. The class of morphisms are denoted by $\morc$.
\item[Identity] is a unique map, actually a functor mapping each object $A \in \objc$ to its identity morphism $id_A : A \longrightarrow A$.
\end{description}
The last point is of utmost importance: the identity map is always a morphism in a given category. Furthermore, given a triple $A, B, C \in \objc$ and morhisms $f : A \longrightarrow B, g : \longrightarrow C \in \morc$ the composition yields a new morphism:
$$g \circ f : A \longrightarrow C \in \morc.$$
Some authors require all five properties to be fulfilled to called a category others omit some.
\paragraph{Functors}
Functors can be thought of as maps between categories. To specify given two categories $\mathcal{C}$ and $\mathcal{D}$, a functor $F$ is a pairing $F : \mathcal{C} \longrightarrow \mathcal{D}$ of these two such that:
\begin{description}
\item[Variance] For $A, B \in \objc$ and $f \in \morc$ either of the two diagrams commutes:
$$\begin{array}{cc}
\xymatrix{
A \ar[d]_F\ar[r]^f& B\ar[d]^F\\
F(A) \ar[r]_{(f)} &F(B)\\
} & \xymatrix{
A \ar[d]_F\ar[r]^f& B\ar[d]^F\\
F(A)  &F(B).\ar[l]^{F(f)}\\
}
\end{array}
$$
The left diagram represents a so called covariant functor $F$ - the right one a so called contravariant functor.
\item[Morphism and identity] The map $F(f) : F(A) \longrightarrow F(B)$ in the covariant case and the map $F(f) : F(B) \longrightarrow F(A)$ in the contravariant case are morphisms in $\mor{\mathcal{D}}$. In particular, $F(id_A) = id_{F(A)}$ and $F(id_A) = id_{F(B)}$ are a morphisms.
\end{description}
There are categories consisting of categories or functors. We will leave it to the reader to explore those. In addition, we may get new categories
\subsubsection{Examples}
As any brief discussion, we will a some examples to illustrate these concepts.
\paragraph{The categories $\mathrm{Set}$ and $\mathrm{Top}$}
The most general category we are going to discuss is the category of all sets, $\mathrm{Set}$, with morphisms $\mor{\mathrm{Set}}$ simply as maps
$$f \in \mathrm{map}(A,B) = B^A \subset \mor{\mathrm{Set}},$$
for all $A, B \in \obj{\mathrm{Set}}$.\\
We call a category $\calc$ small if the class of all its objects is a set in a set theoretic sense. The above example is not small, therefore appropiately called large (i.e. a large category).\\
\indent The next example is the category of all topological spaces, $\mathrm{Top}$, with continuous maps as morphisms:
Furthermore, we may define two pairings
$$\begin{array}{rrcl}
\call{O} :& \mathrm{Top} &\longrightarrow& \mathrm{Set},\\
&&&\\
& (A, \tau) &\longmapsto &\left\{U \in \tau :  U\ \mathrm{open}\right\},\\
&&&\\
\call{F} :& \mathrm{Top}&\longrightarrow& \mathrm{Set},\\
&&&\\
&(A, \tau) &\longmapsto& \left\{F \in \tau : F \ \mathrm{closed}\right\}\\
\end{array}\\
$$
then we get a map $f \in C(A,B) \subset \mor{\mathrm{Top}}$ if and only if the following diagrams:
$$\begin{array}{ccc}
\xymatrix{
A \ar[r]^f\ar[d]_{\call{F}} & B\ar[d]^{\call{F}}\\
\call{F}(A) &\call{F}(B)\ar[l]^{\call{F}(f)}\\
}&\mathrm{and}&\xymatrix{
A \ar[r]^f\ar[d]_{\call{O}} & B\ar[d]^{\call{O}}\\
\call{O}(A) &\call{O}(B)\ar[l]^{\call{O}(f)}\\
}\\
\end{array}
$$
commute. Moreover, what we simply called a pairing from topological spaces with their respetive sub topologies of open or closed sets, respectively, are in fact two contravariant functors.
\paragraph{Pointed spaces and partially ordered sets}
Given a set $X$ and an element $x_0 \in X$, we call the pair $(X,x_0)$ a pointed space and $x_0$ its base point. The class of all pointed spaces forms a category $\mathrm{Pt}$ wrt. to maps preserving base points as morphisms. By this defintion, $\mathrm{Pt}$ is a proper subcategory of $\mathrm{Set}$ as the empty set is not a member of its object class. However, the terminal objects are the singletons - objects with exactly one element.\\
\indent We call a category $\calc$ with an object $\cdot$ a category with terminal object $\cdot$ if and only if for any pair of objects $A, B \in \objc$ with arrow $f \in \objc(A,B)$ there exist two unique morphisms $\pi_A : A \longrightarrow \cdot, \pi_B : B \longrightarrow \cdot \in \morc$ such that the following diagram commutes:
$$\xymatrix{
A \ar[r]^f \ar[dr]_{\pi_A} & B\ar[d]^{\pi_B}\\
&\cdot.\\
}$$
Clearly, each pointed space $(X,x_0)$ is an object with terminal object.\\
\indent Reversing arrows, we call a category $\calc$ with object $\ast$ a category with initial object if for all objects $A, B \in \objc$ and arrow $f : B \longrightarrow A$ there are two unique morphisms $\iota_A : \ast \longrightarrow A, \iota_B : \ast \longrightarrow B \in \morc$ such that
$$\xymatrix{
A & B\ar[l]_f\\
&\ast\ar[u]_{\iota_B} \ar[ul]^{\iota_A} .\\
}$$
commutes.\\
\indent We call an object $\ast \in \objc$ a null object if it is initial and terminal. The category of sets, $\mathrm{Set}$, has the empty set as initial and the class of singletons as terminal object. The category of pointed spaces has its singletons as null object.\\
\indent A relation $R \subset X^2$ is called a partial order on a set $X$ if for all $x, y, z \in X$:
\begin{enumerate}
\item $x R x$ (reflexive),
\item $x R y$ and $y R x$ implies $x = y$ (antisymmetry) and
\item $x R y$ and $y R z$ implies $x R z$ (transitivity).
\end{enumerate}
Partially ordered sets are of great importance in conjunction with algebraic categories which we will describe shortly.
\paragraph{Algebraic categories}
In its very beginning, category theory was mainly an algebraic endeavour. As it became apparent that certain theoremes had a wider application then initially anticipated.\\
Since an algebraic library is the aim of this essay we shall spend some time on algebraic categories.
\subparagraph{Semigroups, monoids and groups}
Before we start a short definition. We call a category $\calc$ a category with finite products if for all objects $A, B \in \objc$ the set theoretic product
$$A \times B = \{(a,b) : a \in A, b \in B\} \in \objc.$$
In particular, each canonical projection $\pi_X : A \times B \longrightarrow X$ and their respective embeddings $\iota_X : X \longrightarrow A \times B$, $X = A, B$ are morphisms in $\calc$.\\
\begin{enumerate}
\item Let $S$ be a set and $m : S \times S \longrightarrow S$ be a binary operation. We call a pair $(S, m)$ a semi group if for all $(s, s')$ there is a $s'' \in S$ such that $m(s,s') = s''$ and the following diagram commutes
$$\xymatrix{
S \times S \times S \ar[rr]^{id_S \times m}\ar[d]_{m \times id_S} && S \times S\ar[d]^{m}\\
S \times S \ar[rr]_{m} &&S.\\
}$$
We call the first property closedness (under operation) and the second associativity. Similarily to the category of sets, the category of semi groups has empty set as initial and singletons as terminal object.\\
\item Let $(M, m)$ be a semi group. We call $M$ a monoid if there is a initial object $\ast$ and an embedding $e : \ast \longrightarrow M$ such that the following diagram commutes:
$$\xymatrix{
& M \times M \ar[dd]_{m}& \\
\ast \times M \ar[ur]^{e \times id_M}\ar[dr]_{\simeq} && M \times \ast \ar[ul]_{id_M \times e}\ar[dl]^{\simeq} \\
&M.&\\
}$$
The map $e$ is a morphism in $\mathrm{Mon}$ and called the unit (map). We denote a monoid with triple $(M, m, e)$. Note that $\mathrm{Mon}$ is a category with null object (initial and terminal are isomorphic). Moreover, it is a subcategory of pointed spaces with trivial submonoid as null object.
\item We call a monoid $(G, m, e)$ a group if there is a map $S : G \longrightarrow G$ such that the following diagram commutes:
$$\xymatrix{
&G \ar[ld]_{\Delta} \ar[ddd] \ar[dr]^{\Delta}&\\
G \times G \ar[d]_{S \times id_G} & & \ar[d]^{id_G \times S} G \times G\\
G \times G \ar[dr]_{m} & & \ar[dl]^{m} G \times G\\
&\ast.&\\
}$$
The map $S$ is called the antipode or more popularly, the inverse/inversion. It induces a group (anti-) isomorphism:
$$S : G \stackrel{\sim}{\longrightarrow} G^{\mathrm{op}},$$
$$m^{\mathrm{op}} = m \circ (S \times S) \circ \tau = \left[(g, h) \longmapsto m(h^{-1},g^{-1}) = h^{-1} g^{-1}\right]$$
It is an anti isomorphism if considered as map $G \longrightarrow G$ and an isomorphism $(G,m,e,S) \longrightarrow (G^{\mathrm{op}},m^{\mathrm{op}},e^{\mathrm{op}} = e, S^{op} = S)$ (we exchange the multiplication map $m$ with $m^{\mathrm{op}}$). A group is called abelian if
$$\xymatrix{
G \times G \ar[d]_{m^{\mathrm{op}}} \ar[r]^{m} &G\ar[ld]^{S}\\
G
}$$
commutes. They form a proper subcategory of $\mathrm{Grp}$, denoted $\mathrm{Abel}$. Again, they have a null object (trivial subgroup). Furthermore, this category has finite products (at least two distinct in as far as):
$$\begin{array}{rrcl}
\times : &\mathrm{Grp} \times \mathrm{Grp}& \longrightarrow &\mathrm{Grp},\\ &\left((G, m_G, e_G, S_G), (H, m_H, e_H, S_H)\right)& \longmapsto &(G \times H, m_{G} \times m_H, e_{G} \times e_{H},\\
&&& S_G \times S_H)\\
&&&\\
\times_{\alpha} : &\mathrm{Grp} \times \mathrm{Grp} &\longrightarrow& \mathrm{Grp}\\
&(G,m_G,e_G,S_G),(H,m_H,e_H,S_H))&\longmapsto&(G \times H, m^{\alpha}_{G \times H}, e_G \times e_H, S_{G} \times S_{H})\\
\end{array}$$
with
$$\begin{array}{rcl}
m^\alpha_{G \times H} &=& (m_G \times m_H) (id_G \times \alpha \times id_H \times id_G \times id_H)\\&&(id_G \times id_H \times \tau_{H\times G}\times id_H)(id_G \times \Delta_H \times id_G \times id_H),\\
&&\\
&=& [(g_1,h_1,g_2,h_2) \longmapsto (g_1,h_1,h_1,g_2,h_2) \longmapsto (g_1,h_1,g_2,h_1,h_2) \longmapsto\\&&~ (g_1, \alpha(h_1)(g_2),h_1,h_2) \longmapsto (g_1 \alpha(h_1)(g_2), h_1 h_2)] \ \mathrm{and}\\
&&\\
\alpha &=& [h \longmapsto \alpha(h)] \in \mathrm{Grp}(H,\mathrm{Aut}_{\mathrm{Grp}}(G))\\
\end{array}$$
The former product is called direct (only the neutral element is in both groups and both are normal in $G \times H$), the latter is called the semi direct product for a given group homomorphism $\alpha : H \longrightarrow \mathrm{Aut}(G)$. They coincide if $\alpha$ reduces to the trivial map
$$\alpha = e_{\mathrm{Aut}(G)} \circ \pi_H = [ h \longmapsto e_H \longmapsto id_G].$$
\end{enumerate}


\paragraph{Population} is a set, of which we would like to learn some properties.

\paragraph{Sample} is a subset of a population, on which we calculate the properties and then make
statements about the whole population.

\paragraph{Random sample} is a sample taken in an unpredictable way, i.e. the method of taking the
sample involves an unpredictable component.

\paragraph{Self-weighting sample} is a sample taken in such manner that each element of population has equal
chance of being included in the sample.

\paragraph{Simple random sample} is a kind of self-weighting sample selected so that all samples of
same size have equal chance of being selected.

\subsection*{Remark}
Further on we in most cases assume random sample of size $n$, $X = \sample$.


\section{The conjugate gradient method}

\begin{intro}
  Relying on Hilbert space structure more than Richardson's iteration
  is the \putindex{conjugate gradient method} (cg), since it uses
  orthogonal search directions. Nevertheless, it also relies on
  constructing search directions from residuals, such that a
  \putindex{Riesz isomorphism} enters the same way as before and can
  then be used for preconditioning.

  The beauty of the conjugate gradient method is, that it is parameter
  and tuning free, and it converges considerably faster than a linear
  iteration method.
\end{intro}

\begin{Definition*}{steepest-descent}{Method of steepest descent}
  Let $a(.,.)$ be a symmetric, positive definite bilinear form on
  $V$. Then, the method of \define{steepest descent} for the energy functional
  \begin{gather}
    E(v) = \tfrac12 a(v,v) - f(v),
  \end{gather}
  reads: given an initial vector $u^{(0)}$,
  compute % $p^{(1)} = \nabla E(u^{(0)}) \in V^*$ and
  for $k\ge 0$ iteratively
  \begin{xalignat}2
    \scal(p^{(k)},v) &= \scal({-\nabla E(u^{(k)})},v)_{V^*\times V} &\forall v&\in V \\
    \alpha_k &= \operatorname*{argmin}_{\alpha>0} E\left(u^{(k)} + \alpha p^{(k)}\right)\\
    u^{(k+1)} &= u^{(k)} + \alpha_k p^{(k)}
  \end{xalignat}
\end{Definition*}

\begin{Lemma}{steepest-descent}
  Let $u\in V$ be the unique minimizer of the energy functional $E(.)$. Then, there holds for all $k$
  \begin{gather}
    a\left(u^{(k+1)}-u, p^{(k)}\right) = a(u^{(k+1)}, p^{(k)}) - f(p^{(k)}) = 0.
  \end{gather}
  With $r^{(k)} = f-a(u^{(k)},.)\in V^*$, there holds
  \begin{gather}
    \alpha_k = \frac{\scal(r^{(k)},p^{(k)})_{V^*\times V}}{a\left(p^{(k)},p^{(k)}\right)}.
  \end{gather}
\end{Lemma}

\begin{Definition}{cg-method}
  Let $V$ be a Hilbert space and $V^*$ its dual. The \define{conjugate
  gradient method} for an iteration vector $u^{(k)} \in V$ involves the
  residuals $r^{(k)} \in V^*$ as well as the update direction $p^{(k)}
  \in V$ and the auxiliary vector $w^{(k)} \in V$. It consists of the
  steps
  \begin{enumerate}
  \item Initialization: for $f$ and $u^{(0)}$ given, compute $r^{(0)} = f- a(u^{(0)},.)$ and
    \begin{xalignat*}{2}
      \scal(p^{(0)}, v) = \scal(w^{(0)}, v) &= \scal(r^{(0)},v)_{V^*\times V} & \forall v &\in V.
    \end{xalignat*}
    \item Iteration step: for $u^{(k)}$, $r^{(k)}$, $w^{(k)}$, and
      $p^{(k)}$ given, compute
      \begin{xalignat*}2
        u^{(k+1)} &= u^{(k)} + \alpha_k p^{(k)}
        &
        \alpha_k &= \frac{\scal(r^{(k)},w^{(k)})_{V^*\times V}}{a\left(p^{(k)},p^{(k)}\right)} \\
        r^{(k+1)} &= r^{(k)} - \alpha_k a\left(p^{(k)},.\right) \\
      \scal(w^{(k+1)}, v) &= \scal(r^{(k+1)},v)_{V^*\times V} & \forall v &\in V \\
      p^{(k+1)} &= w^{(k+1)} + \beta_k p^{(k)}
      &
      \beta_k &= \frac{\scal(r^{(k+1)},w^{(k+1)})_{V^*\times V}}{\scal(r^{(k)},w^{(k)})_{V^*\times V}}
      \end{xalignat*}
  \end{enumerate}
\end{Definition}

% \begin{remark}
%   The results on orthogonality and minimization properties of the cg
%   method in~\cite{GrossmannRoosStynes07} or~\cite{Saad00} remain valid in this
%   context. Differences occur in the interpretation of these
%   properties. The conjugate gradient method does not necessarily
%   converge in a finite number of steps, and if the bilinear form is
%   unbounded, no convergence rate is guaranteed.
% \end{remark}

% \begin{lemma}
%   Let $a(.,.)$ be symmetric and elliptic. Then, either $u^{(k)}$ is a
%   solution, or $u^{(k+1)}$ can be computed by a step of the conjugate
%   gradient method. Furthermore, there are the
% \end{lemma}

\begin{Definition}{pcg-method}
  The \define{preconditioned cg method} is obtained from
  above algorithm by reinterpreting the \putindex{Riesz isomorphism}
  in the computation of $w^{(k+1)}$ as a preconditioning operation,
  much alike Definition~\ref{definition:richardson:2} of the
  preconditioned Richardson iteration. Thus, the line defining
  $w^{(k+1)}$ is replaced by
  \begin{xalignat*}2
    b(w^{(k+1)}, v) &= r^{(k+1)}(v) & \forall v &\in V .
  \end{xalignat*}
  Here, like there, the preconditioner enters naturally from the weak
  form of the algorithm.
\end{Definition}

\begin{Definition}{krylov-space}
  The $n$th \define{Krylov space} as subspace of the Hilbert space $V$
  with inner product $b(.,.)$ of the operator $A$ and seed vector
  $w \in V$ is
  \begin{gather}
    \label{eq:cg:2}
    \mathcal K_n = \mathcal K_n(B^{-1}A, w)
    = \operatorname{span}\left\{w, B^{-1}A w, (B^{-1}A)^2 w, \dots, (B^{-1}A w)^{n-1}\right\}.
  \end{gather}
\end{Definition}

\begin{Lemma}{cg-orthogonality}
  The vectors generated by the conjugate gradient iteration have the
  following properties: either $u^{(k)}$ is the solution to the
  linear system or
  \begin{alignat}2
    r^{(k)} &= f - a(u^{(k)},.)\\
    \scal(r^{(k)},w^{(k)})_{V^*\times V} &= \scal(r^{(k)},p^{(k)})_{V^*\times V} \\
    \scal(r^{(k)},p^{(j)}) &= 0 & j&<k\\
    a(p^{(k)},p^{(j)}) &= 0 & j&<k\\
    \scal(r^{(k)},r^{(j)}) &= 0 & j&<k
  \end{alignat}
\end{Lemma}

\begin{Lemma}{cg-minimization}
  The iterates of the cg method have the following minimization
  properties:
  \begin{gather}
    \label{eq:cg:3}
    \begin{split}
      \norm{u^{(k)}-u}_A &= \min_{v\in \mathcal K_k} \norm{u^{(0)} + v -u}_A \\
      &= \min_{\substack{p\in P_{n-1}\\ p(0) = 1}}
      \norm{u^{(0)} + p(B^{-1}A) w -u}_A.
    \end{split}
  \end{gather}
\end{Lemma}

\begin{Theorem}{cg-convergence}
  Let the bilinear form $a(.,.)$ be symmetric, and let the
  \putindex{spectral equivalence} hold. Then,
  the preconditioned cg method converges and we have the estimate
  \begin{gather}
    \label{eq:cg:1}
    \norm{u^{(k)} - u}_A \le 2
    \left(\frac{\sqrt\kappa-1}{\sqrt\kappa+1}\right)^k \norm{u^{(0)} - u}_A.
  \end{gather}
  Here, $\kappa = \Lambda/\lambda$ is the \putindex{spectral condition
    number} of the preconditioned problem.
\end{Theorem}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\section{Uninformed search}

Let:
\begin{itemize}
  \item $b$ is the average branching factor
  \item $d$ is the depth level
  \item $m$ is the maximum depth of the search space
\end{itemize}

Then:
\begin{table}[hb!]
\begin{adjustwidth}{-2.5cm}{-2.5cm}
\resizebox{1.3\columnwidth}{!}{
\begin{tabular}{lllllll}
  Strategy &
  Complete &
  Time &
  Space &
  Optimal &
  Implementation &
  Comment \\
  BFS &
  If $b$ is finite &
  $O(b^d)$ &
  $O(b^d)$ &
  \multicolumn{1}{p{0.11\linewidth}}{Yes, if uniform cost} &
  FIFO &
  \multicolumn{1}{p{0.17\linewidth}}{High memory, exponential time} \\
  DFS &
  \multicolumn{1}{p{0.17\linewidth}}{No, fails in: infinite depth spaces and spaces with loops} &
  $O(b^m)$ &
  $O(bm)$ &
  No &
  LIFO &
  \multicolumn{1}{p{0.18\linewidth}}{Bad if $m$ is much larger than $d$ but if solutions are dense, may be much faster than BFS} \\
  \multicolumn{1}{p{0.13\linewidth}}{Depth limited Search} &
  &
  &
  &
  &
  DFS with limit \\
  & \\
  Iterative Deepening &
  &
  &
  &
  &
  \multicolumn{1}{p{0.20\linewidth}}{Depth deepening with increasing limits} &
  \multicolumn{1}{p{0.20\linewidth}}{Benefits of BFS and DFS} \\
  Uniform cost &
  \multicolumn{1}{p{0.20\linewidth}}{Yes, if solution has finite cost} &
  $O(b^{C^*/\varepsilon})$ &
  \multicolumn{1}{p{0.18\linewidth}}{\# of nodes with $g\leq C^* \rightarrow O(b^{C^*/\varepsilon})$} &
  Yes &
  \multicolumn{1}{p{0.20\linewidth}}{BFS expanding by cost of path (not of last step)}
\end{tabular}}
\caption{Uninformed Search Strategies}
\label{tab:my_label}
\end{adjustwidth}
\end{table}


%!TEX root =  ../main.tex

\section{Analytic Geometry}\label{sec:AG}

\objective{Calculate distances and midpoints on the Cartesian coordinate plane for order pairs from
functions and relations.}

\subsection{Cartesian Plane}
Descartes did many strange things in philosophy, but he was a real boon for mathematics.  The idea
of graphing values of one variable ($x$) to the left and right, while simultaneously graphing another
variable ($y$) on the up and down is credited to him.  This is called the \gls{Cartesian plane}
or ``Rectangular Coordinates''.

\subsubsection{Quadrants}
It is customary to split the coordinate system into four quadrants, numbered with Roman numerals.
Quadrant I is where $x$ and $y$ are both positive, to the upper right.  Quadrant II is where $x$ is negative
but $y$ is positive, to the upper left.  Quadrant III is where both are negative, to the lower left.  Quadrant
IV is where $x$ is positive and $y$ is negative, to the lower right.

\subsection{Triangles}
Just as we might find the middle between two numbers on the number line,
so too we can find a point midway
between two other points, simply by taking the average of their $x$s and the average of their $y$s.

\begin{derivation}{Midpoint}
Given two points $(x_1,y_1)$ and $(x_2,y_2)$ the arithmetic mean of the $x$'s is $x$ of the midpoint,
and the same for $y$.

Midpoint $(x,y)=(\frac{x_1+x_2}{2},\frac{y_1+y_2}{2})$
\end{derivation}

If we draw a right-triangle with its hypotenuse being the shortest path between two points, and its legs going
strictly left-to-right and up-to-down, then calculating their distance is simply the Pythagorean Theorem.

\begin{derivation}{Pythagorean Theorem}
The distance from $(x_1,y_1)$ to $(x_2,y_2)$ is the hypotenuse of a right triangle with legs
$|x_2-x_1|$ and $|y_2-y_1|$.

\begin{equation}
d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}
\end{equation}
\end{derivation}

\subsection{Functions and Relations}

Chapter 1 proper of this textbook is about functions, but even more basic are relations.
For number, it will suffice
to say that any equation with $x$s and $y$s in it is a
relation\footnote{Note that these need not be ``nice''.  For example,
$x^2y^2=4$ produces an infinite, four-pointed star, but must be put into the TI-8* as \texttt{Y1=sqrt(4/x\^2)} and
\texttt{Y2=-sqrt(4/x\^2)}.}.

like
\begin{derivation}{Relation}
A relation between two sets is a collection of ordered pairs containing one object from each set.
If the object $x$ is from the first set and the object $y$ is from the second set, then the objects are said to be related if the ordered pair $(x,y)$ is in the relation.
\end{derivation}

\ExSection[Exercises]
\begin{exercises}{sec:AG}
\index{Absolute Value!of y}
\prob{}Graph the relation $x=|y|$.  How could we put this into our TI-8*?

\prob{}Use the Pythagorean theorem to find 12 lattice points 5 units from the origin.

\prob{}Find the relation describing all the ordered pairs 5 units from the origin.

\prob{}What are the points 5 units away from 4,2 with a y value of 10?

\prob{}What are the points 6 units away from -2,-1 with a x value of -10

\prob{}What are the points root 10 units away from 0,2 and root 10 units away from 2,0?
\end{exercises}


\lab{Algorithms}{The Pseudospectral method for Boundary Value Problems}{The Pseudospectral method for Boundary Value Problems}
\label{lab:pseudospectral1}

Suppose we have a set of grid points $\{x_i\}_{i=0}^N$ on an interval $I$, and function values $u(x_i)$ at those points.
What is the best way to approximate the derivative $u'(x)$?

Here is one idea: We can construct a polynomial $p(x)$ that interpolates the data $(x_i,u(x_i))$.
Then $p'(x)$ could be used as an approximation to $u'(x)$.
However, if we recall Runge's phenomena we can easily see that in general $p(x)$ can be a very poor approximation for $u(x)$, and in this case we could hardly expect $p'(x)$ to approximate $u'(x)$ well.

The key idea we need here is that the grid points $x_i$ must be carefully chosen.
For example, consider the interval $I = [-1,1]$.
Instead of using equally spaced grid points, let the grid $\{x_i\}_{i=0}^N$ consist of the Chebychev points
\[x_i = \cos (i \pi /N), \quad i = 0, 1, \ldots, N.\]
% They've already seen this in several other labs.
% We shouldn't repeat it here.
\begin{comment}
Experiment with the following code to view Runge's phenomena and see the convergence when the Chebychev points are used.

\begin{lstlisting}
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import BarycentricInterpolator

def f(x):
	return 1./(1.+16.*x**2.)

N = 10
# grid = np.cos(np.pi*np.arange(N+1)/N)		# Chebychev grid
grid = np.linspace(-1,1,N+1)
p = BarycentricInterpolator(grid,f(grid) )

x = np.linspace(-1,1,200)
print "The Maximum Error is ", np.max(np.abs((p(x)-f(x))))
plt.plot(x,f(x),'-k')
plt.plot(grid,f(grid),'*k')
plt.plot(x,p(x),'-r')
plt.show()
\end{lstlisting}

As long as the function $u(x)$ is smooth, the interpolating polynomial $p(x)$ will converge to $u(x)$ rapidly.
For example, if $u$ is analytic then $p(x)$ will converge to $u(x)$ at the rate $\mathcal{O}(k^n)$ where $0<k<1$.
This convergence rate is faster than $\mathcal{O}(1/N^k)$ for any $k$. $p'(x)$ converges to $u'(x)$ at a similar rate.

\begin{figure}[ht]
\centering
\includegraphics[width=9cm]{equally_spaced_points.png}
\caption{Runge's Phenomenon: The polynomial interpolant $p(x)$ of  $1/(1+16x^2)$ at 17 equally spaced grid points has an error of 5.88.
This error grows as the number of grid points increases.}
\label{fig:Spectral1_equally_spaced_points}
\end{figure}

\begin{figure}[ht]
\centering
\includegraphics[width=9cm]{chebychev_points.png}
\caption{The polynomial interpolant $p(x)$ of $1/(1+16x^2)$ at 17 chebychev grid points has an error of 0.0175.
This error decreases rapidly as the number of grid points increases.}
\label{fig:Spectral1_chebychev_points}
\end{figure}
\end{comment}

\section*{The Differentation Matrix}
Let $u$ be a function defined on $[-1,1]$.
The polynomial $p(x)$ that interpolates $u$ at the Chebyshev points can be expanded in the form
\[p(x) = \sum_{j=0}^N u(x_j)C_j(x),\]
where each of the basis functions $C_j$ are cardinal functions, defined to be the polynomials of least degree satisfying
\begin{equation*}
C_j(x_i) = \begin{cases} 1 & i=j \\ 0 & i \not = j.
   \end{cases}
\end{equation*}

Then
\[u'(x_i) \approx p'(x_i) = \sum_{j=0}^N u(x_j)C_j'(x_i) \quad \text{ where } i = 0, \ldots N.\]
This can be written in matrix form as
\begin{equation*}
	\left[\begin{array}{c}p'(x_0) \\p'(x_1)\\\vdots \\p'(x_N)\end{array}\right] =
\left[\begin{array}{cccc}C_0'(x_0) & C_1'(x_0) & \ldots & C_N'(x_0) \\C_0'(x_1) & C_1'(x_1) & \ldots & C_N'(x_1) \\\vdots &  &  & \vdots \\C_0'(x_N) & C_1'(x_N) & \ldots & C_N'(x_N)\end{array}\right]
\left[\begin{array}{c}u(x_0) \\u(x_1)\\\vdots \\u(x_N)\end{array}\right],
\end{equation*}
or in shorthand as
\[U' = D U,\]
where $U'$ represents the approximation to $u'$ using the polynomial $p'$.
% Let $U = [u(x_0),\ldots, u(x_N)]^T$ and $U' = [u'(x_0),\ldots, u'(x_N)]^T$.
The matrix $D$ is given by
\begin{equation*}
D_{ij} = C_j'(x_i) = \begin{cases} (1+2N^2)/6 & i=j=0 \\ -(1+2N^2)/6 & i=j=N \\
-x_j/[2(1-x_j^2)] & i=j, \, 0<j<N \\
(-1)^{i+j}\alpha_i/[\alpha_j(x_i-x_j)] & i \not = j
   \end{cases}
\end{equation*}
where $\alpha_0 = \alpha_N = 2,$ and $\alpha_j = 1$ otherwise.

$u'(x)$ can be approximated for values of $x$ not on the grid by interpolating the values of $p'$ at the grid points.
% \begin{equation*}
% u'(x) \approx p'(x) =  \sum_{j=0}^N p'(x_j)C_j(x).
% \end{equation*}
% Thus $p'$ is an analytic function that approximates $u'$, and not just at the grid points.
To evaluate $p'(x),$ use barycentric interpolation on the grid $(x_i,p'(x_i))$.

\begin{problem}
% They've already done this in the Chebyschev lab.
\begin{comment}
Create a function \li{cheb} that takes in a positive integer $N$ and returns the grid $\{x_j\}_{j=0}^N$ of Chebychev points and the differentation matrix $D$ associated with that grid.
\end{comment}

Use the differentiation matrix to find the numerical derivative of $u(x) = e^{x}\cos(6x)$ on a grid of Chebychev points for several values of $N:$ $N=6, 8, 10.$
Then use barycentric interpolation to approximate $u'$ on the grid \li{np.linspace(-1,1,100)}.
Graphically compare those values to the exact derivative.
\end{problem}

To approximate $u''(x)$ on the grid $\{x_i\}$, we use
\[U'' \approx D^2 U.\]
Solving the bvp
\begin{align*}
u'' &= f(x), \\
u(-1) &= 0, \\
u(1) &= 0,
\end{align*}
on the grid $\{x_i\}$ amounts to solving the system
\[D^2 U = F,\]
where $F = [f(x_0),\ldots, f(x_N)]^T$.
Since we have Dirichlet boundary conditions of $0$, we can satisfy the boundary condition by forcing $U[0] = U[N] = 0$.
(We say there are Dirichlet boundary conditions when both boundary conditions are constant values for $u$).
This allows us to ignore the first and last equations in the system, giving us the new system
\[\tilde{D}^2 \tilde{U} = \tilde{F},\]
where $\tilde{D} = D[1:N,1:N]$, $\tilde{U} = U[1:N]$, and $\tilde{F} = F[1:N]$.
Some modifications are necessary to account for nonzero Dirichlet boundary conditions.

\begin{problem}
Use the pseudospectral method to solve the boundary value problem
\begin{align*}
u'' &= e^{2x}, \\
u(-1) &= 0, \\
u(1) &= 0
\end{align*}
Compare your numerical solution with the exact solution.
\end{problem}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{nonzeroDirichlet.pdf}
\caption{The solution of $u'' + u' = e^{3x}$, subject to the boundary conditions
$u(-1) = 2$, $u(1) = -1$.}
\label{fig:nonzeroDirichlet}
\end{figure}

\begin{problem}
Use the pseudospectral method to solve the boundary value problem
\begin{align*}
u'' + u' &= e^{3x}, \\
u(-1) &= 2, \\
u(1) &= -1.
\end{align*}
Check that your numerical solution is converging.
How many subintervals are required to find the solution correct to three decimal places?
See Figure \ref{fig:nonzeroDirichlet}.

Hint: Reduce the problem to one with zero Dirichlet conditions by letting $u = U+G$, where $G$ is a (simple) function satisfying the boundary conditions.
\end{problem}

\begin{comment}
\section*{The Method of Weighted Residuals}
We may write our differential/integral equation in operator notation as
\[(Lu)(x) = f(x), \quad x \in \Omega\]
where $u$ belongs to some infinite-dimensional function space $V.$
We seek for an approximation $u_N$ to the solution $u$.
Our approximation $u_N$ will come from a finite-dimensional function space $S_N$ called the trial space.
Here we use $N$ to denote the dimension of $S_N \subset V$.
The space $S_N$ can be described by a basis $\{\phi_1(x), \ldots, \phi_N(x)\}$.
Then an approximation $u_N \in S_N$ has the form
\[u_N = \sum_{j=1}^N \gamma_j \phi_j\]

We can then define an operator $\mathcal{R}$ on the trial space $S_N$ by
\[\mathcal{R}u_N(x) = Lu_N(x) - f(x)\]
$\mathcal{R}u_N$ is called the residual or the error of the trial function $u_N$.
Note that the residual of the true solution $u$ is zero.
The method of weighted residuals is a family of methods that determine the coefficients $\gamma_j$ of the approximate solution $u_N$ by forcing the residual $\mathcal{R}u_N$ to be zero in some weighted average over $\Omega$.
In other words, given some collection of weight/test functions $\{w_i\}_{i=1}^M$, we require that
\begin{align}
\int_{\Omega}\mathcal{R}u_N(x) w_i(x)\, dx &= 0, \quad \text{ for } i = 1, \ldots M
\label{eqn:Spectral1_weightedaverage}
\end{align}

After doing the integration described by (\ref{eqn:Spectral1_weightedaverage}), we obtain a system of algebraic equations that may be used to determine the coefficients $\gamma_i$.
Different choices of the trial space $S_N$ and the weight/test functions $w_i(x)$ result in different methods.

We obtain the pseudospectral method (or collocation method) by choosing a collection of points $\{x_i\}_{i=1}^M$ in our space $\Omega$ called collocation points.
The weight functions $w_i(x)$ are then given by $w_i(x) = \delta(x-x_i); $ that is, $w_i$ is the Dirac delta function centered at $x_i$.
Then the equations \ref{eqn:Spectral1_weightedaverage} are given by
\begin{align*}
	\int_{\Omega} \mathcal{R}u_N w_i(x) \, dx &= 0,\\
	\int_{\Omega} \mathcal{R}u_N \delta(x-x_i) \, dx &= 0, \\
	\mathcal{R}u_N(x_i) &= 0 \quad \text{ for } i = 1, \ldots, M
\end{align*}
Thus the pseudospectral method requires the residual of the approximate solution to be exactly zero at the collocation points.
Alternatively, the approximate solution satisfies the differential equation exactly at the collocation points.
\end{comment}


\section{Realizability}
\label{sec:realizability}

We briefly motivate the main idea of (typed) realizability. When we
represent a set of mathematical objects~$S$ in a programming
language~$\PL$ there are two natural steps to take: first choose an
\emph{underlying type~$\ut{S}$} of representing values, and second
specify how the values of type~$\ut{S}$ represent, or \emph{realize},
elements of the set~$S$. For example, consider how we might represent
the set~$D$ of simple finite directed graphs (whose vertices are
labeled by integers). As the underlying datatype we might choose
$\ut{D} = \mathtt{int} \; \mathtt{list} *\, (\mathtt{int} *
\mathtt{int}) \mathtt{list}$, and represent a graph~$G \in D$ as a
pair of lists $(v,e)$ where $v = [x_1; \ldots; x_n]$ is the list of
vertices and $e = [e_1; \ldots; e_m]$ is the list of edges. Formally,
we write
%
\begin{equation*}
  (v, e) \rz_D G
\end{equation*}
%
and read it as ``$(v,e)$ realizes~$G \in D$''. Observe that each graph
is realized by at least one pair of lists, and that no pair of lists
represents more than one graph. (As commonly occurs, most graphs are
represented by many different pairs of lists.) This leads us to the definition given
below. We shall abuse notation slightly and write $t \in \ut{S}$ to
mean that $t$ is a closed expression of type $\ut{S}$.

\begin{figure*}
\[
  \hspace{-0.5truecm}
  \parbox[t]{0.28\textwidth}{
\footnotesize
    \begin{align*}
      \ut{\top} &= \mathtt{unit} \\
      \ut{\bot} &= \mathtt{unit} \\
      \ut{x =_S y} &= \mathtt{unit} \\
      \ut{\phi \land \psi} &= \ut{\phi} * \ut{\psi} \\
      \ut{\phi \implies \psi} &= \ut{\phi} \to \ut{\psi} \\
      \ut{\phi \lor \psi} &= \ut{\phi} + \ut{\psi} \\
      &\\
      \ut{\forall x \in A .\, \phi(x)} &= \ut{A} \to \ut{\phi} \\
      \ut{\exists x \in A .\, \phi(x)} &= \ut{A} \times \ut{\phi}
    \end{align*}
  }
\ \vrule\
  \parbox[t]{0.5\textwidth}{
\footnotesize
    \begin{align*}
      () \rz \top &
      \\
      &
      \\
      () \rz x =_S y
        &\quad\text{iff}\quad
      x \per{S} y
      \\
      (t_1,t_2) \rz \phi \land \psi
        &\quad\text{iff}\quad
        \text{$t_1 \rz \phi$ and $t_2 \rz \psi$}
      \\
      t \rz \phi \implies \psi
        &\quad\text{iff}\quad
        \text{for all $u \in \ut{\phi}$, if $u \rz \phi$ then $t\,u
          \rz \psi$}
      \\
      \inl{t} \rz \phi \lor \psi
        &\quad\text{iff}\quad
        \text{$t \rz \phi$}
      \\
      \inr{t} \rz \phi \lor \psi
        &\quad\text{iff}\quad
        \text{$t \rz \psi$}
      \\
      t \rz \forall x \in A . \phi(x)
        &\quad\text{iff}\quad
        \text{for all $u \in \ut{A}$, if $u \rz_A x$ then $t\,u \rz \phi(x)$}
      \\
      (t_1, t_2) \rz \exists x \in A . \phi(x)
        &\quad\text{iff}\quad
        \text{$t_1 \rz_A x$ and $t_2 \rz \phi(x)$}
    \end{align*}
  }
\]
  \caption{Realizability interpretation of logic (outline)}
  \label{fig:rz-logic}
\end{figure*}

\begin{definition}
  A \emph{modest set}\footnote{Modest sets were so named by Dana
    Scott. They are ``modest'' because their size cannot exceed the
    number of expressions of the underlying datatype.} is a triple
  $(S, \ut{S}, {\rz_S})$ where $S$ is a set, $\ut{S}$ is a type and
  $\rz_S$ is a relation between expressions of type~$\ut{S}$ and
  elements of~$S$, satisfying:
  %
  \begin{enumerate}
  \item For every $x \in S$ there is $t \in \ut{S}$ such that $t \rz_S
    x$.
  \item If $t \rz_S x$ and $t \rz_S y$ then $x = y$.
  \end{enumerate}
  %
  A \emph{realized function} $f : (S, \ut{S}, {\rz_S}) \to (T, \ut{T},
  {\rz_T})$ between modest sets is a function $f : S \to T$ for which
  there exists $u \in \ut{S} \to \ut{T}$ such that
  %
  \begin{equation*}
    t \rz_S x \implies u\,t \rz_T f(x) \;.
  \end{equation*}
  %
  We say that $u$ \emph{realizes}~$f$.
\end{definition}

The realizer~$u$ of a realized function~$f$ is more commonly known as
an ``implementation of~$f$'' or an ``algorithm for computing~$f$''.

Modest sets and realized functions form a category of \emph{modest
  sets~$\Mod{\PL}$}. In realizability theory this is a well known
category with good properties. It is regular and locally bi-cartesian
closed, which allows us to interpret first-order logic and a rich type
theory. Here we only outline the main ideas behind the realizability
interpretation of logic. See e.g.~\cite{Bauer:00} for details.

In the realizability interpretation of logic, each formula~$\phi$ is
assigned a set of \emph{realizers} which can be thought of as
computations that witness the validity of~$\phi$. The situation is
somewhat similar (but not equivalent) to the propositions-as-types
translation of logic into type theory, where the proofs of a
proposition correspond to terms of the corresponding type. More
precisely, to each formula~$\phi$ we assign an underlying type
$\ut{\phi}$ of realizers. However, unlike in the propositions-as-types
translation, not all terms of type $\ut{\phi}$ are necessarily valid
realizers for~$\phi$. We write $t \rz \phi$ when $t \in \ut{\phi}$ is
a realizer for~$\phi$. The underlying types and the
realizability relation~$\rz$ are defined inductively on the structure
of~$\phi$; an outline is shown in Figure~\ref{fig:rz-logic}. We say that a
formula~$\phi$ is \emph{valid} in~$\Mod{\PL}$ if it has at least one
realizer.

We shall not dwell any further on the technicalities involving the
category of modest sets, but rather proceed to a concrete description
of our realizability translation. There is one technical point,
though, which we first take care of. A modest set is a triple $(S,
\ut{S}, {\rz_S})$ in which~$S$ is an arbitrary set. For an automated
system it would be convenient if it did not have to refer to arbitrary
sets but rather just to ingredients that are already present in the
programming language, such as types and sets of expressions. Up to
equivalence of categories, modest sets can be constructed as triples
$(\ut{S}, \tot{S}, {\per{S}})$ where $\ut{S}$ is a type, $\tot{S}$ is
a subset of expressions of type~$\ut{S}$, called the \emph{total
  values},\footnote{We do \emph{not} require that a total value must
  be a terminating expression.} and $\per{S}$ is an equivalence
relation on~$\tot{S}$. The relationship between this representation of
a modest set and the original one is as follows:
%
\begin{itemize}
\item $\tot{S}$ is the set of those $t \in \ut{S}$ that
  realize something, i.e., there is $x \in S$ such that $t \rz_S x$.
  These correspond to implementations that satisfy
  the representation invariant, e.g., graphs where the list of edges
  mentions only integers in the list of nodes, a subset of
  all values of type $\mathtt{int} \; \mathtt{list} * (\mathtt{int} *
\mathtt{int}) \; \mathtt{list}$.
\item $t \per{S} u$ if $t$ and $u$ realize the same element, i.e.,
  there is $x \in S$ such that $t \rz_S x$ and $u \rz_S x$.
  This relation equates alternate concrete representations of the same
  abstract value, e.g., equating two concrete graph representations differing
  only in the order of the nodes or the order of the edges.
\end{itemize}
%
The alternative view of a modest set $(\ut{S}, \tot{S}, {\per{S}})$
only refers to objects and concepts from the programming language. It
is better suited for our purposes.

Note that the equivalence relation on~$\tot{S}$ is also a
\emph{partial} equivalence relation on~$\ut{S}$, which shows that
modest sets are in fact equivalent to PER models.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "case"
%%% End:


\subsection{Capsules}

\subsubsection{Primary capsule layers}

Outputs of convolutions are scalars. however we can also create vectors, if we associate some convolutions with each other

eg if we have 6 convolutions, the output of these can be used to create a 6 dimensional vector for each window.

\subsubsection{Normalisation in primary capsule layers (vector squishing)}

We can normalise the length of these vectors to between \(0\) and \(1\).

The output of this repesents the chance of finding the feature they are looking for, and the orientation

If the vector length is low, feature not found. if high, feature found.

We have orientation from vector, and position from window

\subsubsection{Routing capsule layers}

We now have a layer of position and orientation of basic shapes (triangles, rectangles etc)

We want to know which more complex thing they are part of.

So the output of this step is again a matrix with position and orientation, but of more complex features

To determine the activation from each basic shape to the next feature we use routing-by-agreement.

This takes each basic shape and works out what it would look like if the complex feature was present.

If a complex feature has two basic shapes, they will both have the same predicted complex shape. Otherwise the relationship is spurious and they will not

If they agree we have a high weight

This process is complex and computationally expensive.

However we don't need pooling layers now

\subsubsection{caps net}

Does normal conv first, then primary, then secondary.

\subsubsection{caps: reconstruction}

We have vector space of feature position and orietnation. we can recreate output