% !TEX root = hott_intro.tex

\section{Contractible types and contractible maps}
\sectionmark{Contractible types and maps}

In this lecture we introduce the notions of contractible types and maps. A contractible type is a type which has, up to identification, only one term. In other words, a contractible type is a type that comes equipped with a point, and an identification of this point with any point.

We may think of contractible types as singletons up to homotopy, and indeed we show that the unit type is an example of a contractible type. Moreover, we show that contractible types satisfy an induction principle that is very similar to the induction principle of the unit type, provided that we formulate the computation rule using the identity type rather than postulating a judgmental computation rule. Another example of a contractible type is the total space of the family of identifications with a fixed starting point.

We then introduce the notion of fiber of a map, which is the type theoretic analogue of the pre-image of a map, and we say that a map in contractible if all its fibers are contractible. Thus, a map is contractible if the pre-image at any point in the codomain is a singleton. This condition is of course analogous to the set theoretic notion of bijective map, which suggests that on the type theoretical side of things a map should be contractible if and only if it is an equivalence.

The forward direction of this claim is straightforward, and we prove this direction immediately in \cref{thm:equiv_contr}. The converse direction can be done directly, but it is certainly more involved. Therefore we prepare the proof of the converse direction by first characterizing the identity type of a fiber of a map. Then we show that any equivalence $e$ can be given the structure of a invertible map with an additional coherence relating the homotopies
\begin{equation*}
  e\circ e^{-1}\htpy \idfunc,\qquad\text{and}\qquad e^{-1}\circ e\htpy\idfunc,
\end{equation*}
and finally we use these observations in \cref{thm:contr_equiv} to conclude that the fibers of any equivalence must be contractible.

\subsection{Contractible types}

\begin{defn}
  We say that a type $A$ is \define{contractible}\index{contractible!type|textbf} if it comes equipped with a term of type
  \begin{equation*}
    \iscontr(A) \defeq \sm{c:A}\prd{x:A}c=x.
  \end{equation*}
  Given a term $(c,C):\iscontr(A)$, we call $c:A$ the \define{center of contraction}\index{center of contraction|textbf} of $A$, and we call $C:\prd{x:A}c=x$ the \define{contraction}\index{contraction} of $A$.
\end{defn}

\begin{rmk}
Suppose $A$ is a contractible type with center of contraction $c$ and contraction $C$. Then the type of $C$ is (judgmentally) equal to the type
\begin{equation*}
\mathsf{const}_c\htpy\idfunc[A].
\end{equation*}
In other words, the contraction $C$ is a \emph{homotopy} from the constant function to the identity function.
\end{rmk}

\begin{eg}
  The unit type is easily seen to be contractible. For the center of contraction we take $\ttt:\unit$. Then we define a contraction $\prd{x:\unit}\ttt=x$ by the induction principle of $\unit$. Applying the induction principle, it suffices to construct a term of type $\ttt = \ttt$, for which we just take $\refl{\ttt}$.
\end{eg}

\begin{defn}
  For any $a:A$ and any type family $B$ over $A$, we define
  \begin{equation*}
    \mathsf{ev\usc{}pt} : \Big(\prd{x:A}B(x)\Big)\to B(a)
  \end{equation*}
  to be the function $\lam{f}f(a)$.
\end{defn}

\begin{defn}
  Suppose $A$ comes equipped with a term $a:A$. Then we say that $A$ satisfies \define{singleton induction}\index{singleton induction|textbf} if for every type family $B$ over $A$, the map
  \begin{equation*}
    \mathsf{ev\usc{}pt}:\Big(\prd{x:A}B(x)\Big)\to B(a)
  \end{equation*}
  has a section. In other words, if $A$ satisfies singleton induction we have a function and a homotopy
  \begin{align*}
    \mathsf{sing\usc{}ind}_{a} & : B(a)\to \prd{x:A}B(x) \\
    \mathsf{sing\usc{}comp}_{a} & : \mathsf{ev\usc{}pt}\circ \mathsf{sing\usc{}ind}_{a} \htpy \idfunc
  \end{align*}
  for any type family $B$ over $A$.
\end{defn}

\begin{eg}
  Note that the singleton induction principle is almost the same as the induction principle for the unit type, the difference being that the "computation rule" in the singleton induction for $A$ is stated using an \emph{identification} rather than as a judgmental equality. The unit type\index{unit type!contractibility} $\unit$ comes equipped with a function
  \begin{equation*}
    \indunit:B(\ttt)\to \prd{x:\unit}B(x)
  \end{equation*}
  for every type family $B$ over $\unit$, satisfying the judgmental equality $\indunit(b,\ttt)\jdeq b$ for every $b:B(\ttt)$ by the computation rule. Thus we easily obtain the homotopy
  \begin{equation*}
    \lam{b}\refl{b}:\mathsf{ev\usc{}pt}\circ\indunit \htpy\idfunc,
  \end{equation*}
  and we conclude that the unit type satisfies singleton induction.
\end{eg}

\begin{thm}\label{thm:contractible}
Let $A$ be a type. The following are equivalent:
\begin{enumerate}
\item The type $A$ is contractible.
\item The type $A$ comes equipped with a term $a:A$, and satisfies singleton induction.
\end{enumerate}
\end{thm}

\begin{proof}
Suppose $A$ is contractible with center of contraction $c$ and contraction $C$.
First we observe that, without loss of generality, we may assume that $C$ comes equipped with an identification $p:C(c)=\refl{c}$.
To see this, note that we can always define a new contraction $C'$ by
\begin{equation*}
C'(x)\defeq\ct{C(c)^{-1}}{C(x)},
\end{equation*}
which satisfies the requirement by the left inverse law, constructed in \cref{defn:id_invlaw}.

To show that $A$ satisfies singleton induction let $B$ be a type family over $A$ equipped with $b:B(a)$. To define $\mathsf{sing\usc{}ind}_a(b):\prd{x:A}B(x)$, let $x:A$. We have an identification $C(x):a=x$, and $b$ is in $B(a)$. Therefore we can transport $b$ along the path $C(x)$ to obtain
\begin{equation*}
  \mathsf{sing\usc{}ind}_a(b)\defeq \mathsf{tr}_B(C(x),b):B(x).
\end{equation*}
To see that $\mathsf{sing\usc{}ind}(c)=b$ note that we have
\begin{equation*}
\begin{tikzcd}
\mathsf{tr}_B(C(c),b) \arrow[r,equals,"\ap{\lam{\omega}\mathsf{tr}_B(\omega,b)}{p}"] &[4em] \mathsf{tr}_B(\refl{c},b) \arrow[r,equals,"\refl{b}"] & b.
\end{tikzcd}
\end{equation*}
This completes the proof that $A$ satisfies singleton induction.

For the converse, suppose that $a:A$ and that $A$ satisfies singleton induction. Our goal is to show that $A$ is contractible. For the center of contraction we take the term $a:A$. By singleton induction applied to $B(x)\defeq a=x$ we have the map
\begin{equation*}
\mathsf{sing\usc{}ind}_{a} : a=a \to \prd{x:A}a=x.
\end{equation*}
Therefore $\mathsf{sing\usc{}ind}_{A,a}(\refl{a})$ is a contraction.
\end{proof}

\begin{thm}\label{thm:total_path}
For any $a:A$, the type
\begin{equation*}
\sm{x:A}a=x
\end{equation*}
is contractible.\index{identity type!contractibility of total space|textit}
\end{thm}

\begin{proof}
  We will prove the statement by showing that $\sm{y:A}x=y$ satisfies singleton induction, and then use \cref{thm:contractible} to conclude that $\sm{x:A}a=x$ is contractible. We will use the term $(a,\refl{a}):\sm{x:A}a=x$ as the center of contraction.

  Now let $P$ be a type family over $\sm{x:A}a=x$. Note that we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      \prd{t:\sm{x:A}a=x}P(t) \arrow[dr,swap,"\mathsf{ev\usc{}pt}"] \arrow[rr,"\mathsf{ev\usc{}pair}"] & & \prd{x:A}{p:a=x}P(x,p) \arrow[dl,"\mathsf{ev\usc{}refl}"] \\
      \phantom{\prd{x:A}{p:a=x}P(x,p)} & P(a,\refl{a})
    \end{tikzcd}
  \end{equation*}
  where the maps $\mathsf{ev\usc{}pair}$ and $\mathsf{ev\usc{}refl}$ are defined as
  \begin{align*}
    f & \mapsto \lam{x}{p}f(x,p) \\
    g & \mapsto g(a,\refl{a}),
  \end{align*}
  respectively. By the induction principle for $\Sigma$-types it follows that $\mathsf{ev\usc{}pair}$ has a section, and by path induction it follows that $\mathsf{ev\usc{}refl}$ has a section. Therefore it follows from \cref{ex:3_for_2} that the composite $\mathsf{ev\usc{}pt}$ has a section.
\end{proof}

\subsection{Contractible maps}
\begin{defn}
Let $f:A\to B$ be a function, and let $b:B$. The \define{fiber}\index{fiber|textbf}\index{homotopy fiber|see {fiber}} of $f$ at $b$ is defined to be the type
\begin{equation*}
\fib{f}{b}\defeq\sm{a:A}f(a)=b.
\end{equation*}
\end{defn}

In other words, the fiber of $f$ at $b$ is the type of $a:A$ that get mapped by $f$ to $b$.
One may think of the fiber as a type theoretic version of the pre-image\index{pre-image|see {fiber}} of a point.

It will be useful to have a characterization of the identity type of a fiber, so we will make such a characterization immediately.

  \begin{defn}
    Let $f:A \to B$ be a map, and let $(x,p),(x',p'):\fib{f}{y}$ for some $y:B$.
    Then we define
    \begin{equation*}
      \mathsf{Eq\usc{}fib}_f((x,p),(x',p'))\defeq \sm{\alpha:x=x'}p=\ct{\ap{f}{\alpha}}{p'}
    \end{equation*}
    The relation $\mathsf{Eq\usc{}fib}_f:\fib{f}{y}\to\fib{f}{y}\to\UU$ is a reflexive relation, since we have
    \begin{equation*}
      \lam{(x,p)}(\refl{x},\refl{p}):\prd{(x,p):\fib{f}{y}}\mathsf{Eq\usc{}fib}_f((x,p),(x,p)).
    \end{equation*}
  \end{defn}

  \begin{lem}
    Consider a map $f:A\to B$ and let $y:B$. The canonical map
    \begin{equation*}
      ((x,p)=(x',p'))\to\mathsf{Eq\usc{}fib}_f((x,p),(x',p'))
    \end{equation*}
    induced by the reflexivity of $\mathsf{Eq\usc{}fib}_f$ is an equivalence for any $(x,p),(x',p'):\fib{f}{y}$.
  \end{lem}

  \begin{proof}
    The converse map
    \begin{equation*}
      \mathsf{Eq\usc{}fib}_f((x,p),(x',p'))\to ((x,p)=(x',p'))
    \end{equation*}
    is easily defined by $\Sigma$-induction, and then path induction twice. The homotopies witnessing that this converse map is indeed a right inverse as well as a left inverse is similarly constructed by induction.
  \end{proof}

  Now we arrive at the notion of contractible map.

\begin{defn}
We say that a function $f:A\to B$ is \define{contractible}\index{contractible!map|textbf} if there is a term of type
\begin{equation*}
\iscontr(f)\defeq\prd{b:B}\iscontr(\fib{f}{b}).
\end{equation*}
\end{defn}

\begin{thm}\label{thm:equiv_contr}
Any contractible map is an equivalence.\index{contractible!map!is an equivalence|textit}
\end{thm}

\begin{proof}
Let $f:A\to B$ be a contractible map. Using the center of contraction of each $\fib{f}{y}$, we obtain a term of type
\begin{align*}
\lam{y}\pairr{g(y),G(y)}:\prd{y:B}\fib{f}{y}.
\end{align*}
Thus, we get map $g:B\to A$, and a homotopy $G:\prd{y:B} f(g(y))=y$. In other words, we get a section of $f$.

It remains to construct a retraction of $f$. Taking $g$ as our retraction, we have to show that $\prd{x:A} g(f(x))=x$. Note that we get an identification $p:f(g(f(x)))=f(x)$ since $g$ is a section of $f$. It follows that $(g(f(x)),p):\fib{f}{f(x)}$. Moreover, since $\fib{f}{f(x)}$ is contractible we get an identification $q:\pairr{g(f(x)),p}=\pairr{x,\refl{f(x)}}$. The base path $\ap{\proj 1}{q}$ of this identification is an identification of type $g(f(x))=x$, as desired.
\end{proof}

\subsection{Equivalences are contractible maps}

In \cref{thm:contr_equiv} we will show the converse to \cref{thm:equiv_contr}, i.i., we will show that any equivalence is a contractible map. We will do this in two steps.

First we introduce a new notion of \emph{coherently invertible map}, for which we can easily show that such maps have contractible fibers. Then we show that any equivalence is a coherently invertible map.

  Recall that an invertible map is a map $f:A\to B$ equipped with $g:B\to A$ and homotopies
  \begin{equation*}
    G : f\circ g \htpy \idfunc\qquad\text{and}\qquad H:g\circ f\htpy \idfunc.
  \end{equation*}
  Then we observe that both $G \cdot f$ and $f \cdot H$ are homotopies of the same type
  \begin{equation*}
    f\circ g\circ f \htpy f.
  \end{equation*}
  A coherently invertible map is an invertible map for which there is a further homotopy $G \cdot f\htpy f\cdot H$.

  \begin{defn}
    Consider a map $f:A\to B$. We say that $f$ is \define{coherently invertible} if it comes equipped with
    \begin{align*}
      g & : B \to A \\
      G & : f \circ g \htpy \idfunc \\
      H & : g \circ f \htpy \idfunc \\
      K & : G \cdot f \htpy f \cdot H.
    \end{align*}
    We will write $\mathsf{is\usc{}coh\usc{}invertible}(f)$ for the type of quadruples $(g,G,H,K)$.
  \end{defn}

  Although we will encounter the notion of coherently invertible map on some further occasions, the following lemma is our main motivation for considering it.

  \begin{lem}\label{lem:contr-inv}
    Any coherently invertible map has contractible fibers.
  \end{lem}

  \begin{proof}
    Consider a map $f:A\to B$ equipped with
    \begin{align*}
      g & : B \to A \\
      G & : f \circ g \htpy \idfunc \\
      H & : g \circ f \htpy \idfunc \\
      K & : G \cdot f \htpy f \cdot H,
    \end{align*}
    and let $y:B$. Our goal is to show that $\fib{f}{y}$ is contractible. For the center of contraction we take $(g(y),G(y))$. In order to construct a contraction, it suffices to construct a term of type
    \begin{equation*}
      \prd{x:A}{p:f(x)=y}\mathsf{Eq\usc{}fib}_f((g(y),G(y)),(x,p)).
    \end{equation*}
    By path induction on $p:f(x)=y$ it suffices to construct a term of type
    \begin{equation*}
      \prd{x:A}\mathsf{Eq\usc{}fib}_f((g(f(x)),G(f(x))),(x,\refl{f(x)})).
    \end{equation*}
    By definition of $\mathsf{Eq\usc{}fib}_f$, we have to construct a term of type
    \begin{equation*}
      \prd{x:A}\sm{\alpha:g(f(x))=x}G(f(x))=\ct{\ap{f}{\alpha}}{\refl{f(x)}}.
    \end{equation*}
    Such a term is constructed as $\lam{x}(H(x),K'(x))$, where the homotopy $H:g\circ f\htpy \idfunc$ is given by assumption, and the homotopy
    \begin{align*}
      K' & : \prd{x:A}G(f(x))=\ct{\ap{f}{H(x)}}{\refl{f(x)}}
    \end{align*}
    is defined as
    \begin{equation*}
      K'\defeq \ct{K}{\mathsf{htpy\usc{}right\usc{}unit}(f\cdot H)^{-1}}.\qedhere
    \end{equation*}
  \end{proof}

  Our next goal is to show that for any map $f:A\to B$ equipped with
  \begin{equation*}
    g:B\to A,\qquad G:f\circ g \htpy \idfunc,\qquad\text{and}\qquad H:g\circ f\htpy \idfunc,
  \end{equation*}
  we can improve the homotopy $G$ to a new homotopy $G':f\circ g\htpy \idfunc$ for which there is a further homotopy
  \begin{equation*}
    f\cdot H\htpy G'\cdot f.
  \end{equation*}
  Note that this situation is analogous to the situation in the proof of \cref{thm:contractible}, where we improved the contraction $C$ so that it satisfied $C(c)=\refl{}$. The extra coherence $f\cdot H\htpy G'\cdot f$ is then used in the proof that the fibers of an equivalence are contractible.

\begin{defn}\label{defn:htpy_nat}\index{homotopy!naturality|textbf}
Let $f,g:A\to B$ be functions, and consider $H:f\htpy g$ and $p:x=y$ in $A$. We define identification
\begin{equation*}
\mathsf{htpy\usc{}nat}(H,p) \defeq  :\ct{\ap{f}{p}}{H(y)}=\ct{H(x)}{\ap{g}{p}}
\end{equation*}
witnessing that the square
\begin{equation*}
\begin{tikzcd}
f(x) \arrow[r,equals,"H(x)"] \arrow[d,equals,swap,"\ap{f}{p}"] & g(x) \arrow[d,equals,"\ap{g}{p}"] \\
f(y) \arrow[r,equals,swap,"H(y)"] & g(y)
\end{tikzcd}
\end{equation*}
commutes. This square is also called the \define{naturality square} of the homotopy $H$ at $p$.
\end{defn}

\begin{constr}
  By path induction on $p$ it suffices to construct an identification
  \begin{equation*}
    \ct{\ap{f}{\refl{x}}}{H(x)}=\ct{H(x)}{\ap{g}{\refl{x}}}
  \end{equation*}
  since $\ap{f}{\refl{x}}\jdeq \refl{f(x)}$ and $\ap{g}{\refl{x}}\jdeq\refl{g(x)}$, and since $\ct{\refl{f(x)}}{H(x)}\jdeq H(x)$, we see that the path $\mathsf{right\usc{}unit}(H(x))^{-1}$ is of the asserted type.
\end{constr}

\begin{defn}\label{defn:retraction_swap}
Consider $f:A\to A$ and $H: f\htpy \idfunc[A]$. We construct an identification $H(f(x))=\ap{f}{H(x)}$, for any $x:A$.
\end{defn}

\begin{constr}
By the naturality of homotopies with respect to identifications the square
\begin{equation*}
\begin{tikzcd}[column sep=large]
ff(x) \arrow[d,swap,equals,"\ap{f}{H(x)}"] \arrow[r,equals,"H(f(x))"] & f(x) \arrow[d,equals,"H(x)"] \\
f(x) \arrow[r,swap,equals,"H(x)"] & x
\end{tikzcd}
\end{equation*}
commutes. This gives the desired identification $H(f(x))=\ap{f}{H(x)}$.
\end{constr}

\begin{lem}\label{lem:coherently-invertible}
  Let $f:A\to B$ be a map, and consider $(g,G,H):\mathsf{has\usc{}inverse}(f)$. Then there is a homotopy $G':f\circ g\htpy \idfunc$ equipped with a further homotopy
  \begin{equation*}
    K : G'\cdot f \htpy f\cdot H.
  \end{equation*}
  Thus we obtain a map $\mathsf{has\usc{}inverse}(f)\to\mathsf{is\usc{}coh\usc{}invertible}(f)$.
\end{lem}

\begin{proof}
  For each $y:B$, we construct the identification $G'(y)$ as the concatenation
  \begin{equation*}
    \begin{tikzcd}
      fg(y) \arrow[r,equals,"{G(fg(y))}^{-1}"] &[2.5em] fgfg(y) \arrow[r,equals,"\ap{f}{H(g(y))}"] &[2.5em] fg(y) \arrow[r,equals,"G(y)"] & y.
\end{tikzcd}
  \end{equation*}
  In order to construct a homotopy $G'\cdot f\htpy f\cdot H$, it suffices to show that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=8em]
      fgfgf(x) \arrow[r,equals,"{G(fgf(x))}"] \arrow[d,equals,swap,"\ap{f}{H(gf(x))}"] & fgf(x) \arrow[d,equals,"\ap{f}{H(x)}"] \\
      fgf(x) \arrow[r,equals,swap,"G(f(x))"] & f(x)
    \end{tikzcd}
  \end{equation*}
  commutes for every $x:A$.
  Recall from \cref{defn:retraction_swap} that we have $H(gf(x))=\ap{gf}{H(x)}$. Using this identification, we see that it suffices to show that the square
  \begin{equation*}
    \begin{tikzcd}[column sep=8em]
      fgfgf(x) \arrow[r,equals,"G(fgf(x))"] \arrow[d,equals,swap,"\ap{fgf}{H(x)}"] & fgf(x) \arrow[d,equals,"\ap{f}{H(x)}"] \\
      fgf(x) \arrow[r,equals,swap,"G(f(x))"] & f(x)
    \end{tikzcd}
  \end{equation*}
  commutes. Now we observe that this is just a naturality square the homotopy $Gf:fgf\htpy f$, which commutes by \cref{defn:htpy_nat}.
\end{proof}

Now we put the pieces together to conclude that any equivalence has contractible fibers.

\begin{thm}\label{thm:contr_equiv}
Any equivalence is a contractible map.\index{equivalence!is a contractible map|textit}
\end{thm}

\begin{proof}
  We have seen in \cref{lem:contr-inv} that any coherently invertible map is a contractible map. Moreover, any equivalence has the structure of an invertible map by \cref{thm:inv_equiv}, and any invertible map is coherently invertible by \cref{lem:coherently-invertible}.
\end{proof}

\begin{cor}\label{cor:contr_path}
Let $A$ be a type, and let $a:A$. Then the type
\begin{equation*}
\sm{x:A}x=a
\end{equation*}
is contractible.
\end{cor}

\begin{proof}
By \cref{thm:id_equiv}, the identity function is an equivalence. Therefore, the fibers of the identity function are contractible by \cref{thm:contr_equiv}. Note that $\sm{x:A}x=a$ is exactly the fiber of $\idfunc[A]$ at $a:A$.
\end{proof}

\begin{comment}
\begin{proof}
We have the term $(a,\refl{a}):\sm{x:A}a=x$, which we take for the center of contraction. To construct the contraction, we have to show that
\begin{equation*}
\prd{p:\sm{x:A}a=x} (a,\refl{a})=p.
\end{equation*}
By the induction principle for dependent pair types it suffices to construct a term of type
\begin{equation*}
\prd{x:A}{p:a=x} (a,\refl{a})=(x,p)
\end{equation*}
Note that we may proceed here by path induction on $p$. That is, it suffices to consider the case $p\jdeq\refl{a}$, and show that $(a,\refl{a})=(a,\refl{a})$. Here we choose $\refl{(a,\refl{a})}$.
\end{proof}
\end{comment}

\begin{exercises}
\item \label{ex:prop_contr}Show that if $A$ is contractible, then for any $x,y:A$ the identity type $x=y$ is also contractible.\index{contractible!type!identity types of}
\item \label{ex:contr_retr}Suppose that $A$ is a retract of $B$. Show that\index{contractible!type!retracts of}
  \begin{equation*}
    \iscontr(B)\to\iscontr(A).
  \end{equation*}
\item \label{ex:contr_equiv}
  \begin{subexenum}
  \item Show that for any type $A$, the map $\mathsf{const}_\ttt : A\to \unit$ is an equivalence if and only if $A$ is contractible.\index{contractible!type!equivalence with $\unit$}
  \item Apply \cref{ex:3_for_2} to show that for any map $f:A\to B$, if any two of the three assertions\index{contractible!type!three@{3-for-2}}
    \begin{enumerate}
    \item $A$ is contractible
    \item $B$ is contractible
    \item $f$ is an equivalence
    \end{enumerate}
    hold, then so does the third.
  \end{subexenum}
\item Show that for any two types $A$ and $B$, the following are equivalent:
  \begin{enumerate}
  \item Both $A$ and $B$ are contractible.
  \item The type $A\times B$ is contractible.
  \end{enumerate}
\item \label{ex:contr_in_sigma} Let $C$ be a contractible type with center of contraction $c:C$. Furthermore, let $B$ be a type family over $C$. Show that the map $b\mapsto\pairr{c,b}:B(c)\to\sm{x:C}B(x)$ is an equivalence.
\item \label{ex:proj_fiber}Let $B$ be a family of types over $A$, and consider the projection map
  \begin{equation*}
    \proj 1 : \big(\sm{x:A}B(x)\big)\to A.
  \end{equation*}
  Show that for any $a:A$, the map
  \begin{equation*}
    \lam{((x,y),p)} \mathsf{tr}_B(p,y) : \fib{\proj 1}{a} \to B(a),
  \end{equation*}
  is an equivalence. Conclude that $\proj 1$ is an equivalence if and only if each $B(a)$ is contractible.
\item \label{ex:fib_replacement}Construct for any map $f:A\to B$ an equivalence $e:\eqv{A}{\sm{y:B}\fib{f}{y}}$ and a homotopy $H:f\htpy \proj 1\circ e$ witnessing that the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0em]
      A \arrow[rr,"e"] \arrow[dr,swap,"f"] & & \sm{y:B}\fib{f}{y} \arrow[dl,"\proj 1"] \\
      \phantom{\sm{y:B}\fib{f}{y}} & B
    \end{tikzcd}
  \end{equation*}
  commutes. The projection $\proj 1 : (\sm{y:B}\fib{f}{y})\to B$ is sometimes also called the \define{fibrant replacement}\index{fibrant replacement} of $f$.
\end{exercises}


% !TEX root = index.tex

\section{The Topology behind the Algebra}
\epigraph{It is my experience that proofs involving matrices can be shortened by 50\% if one throws the matrices out.}{E. Artin}

 Most problems in this section are (*) or harder and hence optional.

\begin{ques}
  Show that for a space $X$ if some good cover has dimension $n$ then $\check H^i(X) = 0$ for $i > n$.
\end{ques}

\begin{definition}
  The {\bf Euler characteristic} of a space $X$ is defined as
  \begin{align*}
    \chi(M) := \sum \limits_{i \in \Z} (-1)^i \dim \check H^i(X)  = \dim \check H^0(X) - \dim \check H^1(X) + \dim \check H^0(X) \pm \dots
  \end{align*}
\end{definition}

\begin{ques}
  Let $M$ be a $g$ holed torus.
  Triangulate $M$ and suppose there are $V$ number of vertices, $E$ number of edges, and $F$ number of faces in the triangulation. Find a good cover of $M$ using this triangulation and prove that
  \begin{align*}
    \chi(M) = V - E + F
  \end{align*} (Recall that we don't need to find the maps $d^i$ to find the Euler characteristic.)
\end{ques}

\begin{ques}
Let $G$ be a connected graph. Let $T$ be a maximal tree in $G$ i.e. $T$ is a subgraph of $G$ which is a tree such that adding any edge of $G$ to it creates a cycle. Find a cover $\U$ of $G$ containing $T$ as an element (i.e. $T \in \U$). Use this cover to find a topological interpretation of $\dim \check H^1(G)$.
\end{ques}

% \begin{ques}
%   By gluing the sides of a square in funky ways we can create the Klein Bottle and the Real Projective Plane. Find their Cech cohomologies. (We can find open covers for torus, Klein bottle, and projective plane containing exactly 3 sets.)
%   \begin{figure}[H]
%   	\centering
%     \begin{subfigure}[t]{0.3\textwidth}
%   		\centering
%   		\includegraphics[height=2.5cm]{Torus}
%       \caption{Torus}
%   	\end{subfigure}
%     \begin{subfigure}[t]{0.3\textwidth}
%   		\centering
%   		\includegraphics[height=2.5cm]{KleinBottle}
%       \caption{Klein Bottle}
%   	\end{subfigure}
%   	\begin{subfigure}[t]{0.3\textwidth}
%   		\centering
%   		\includegraphics[height=2.5cm]{ProjectivePlane}
%       \caption{Projective Plane}
%   	\end{subfigure}
%   \end{figure}
% \end{ques}

We'll now find a topological interpretation for $\check H^0$. Let $X$ be a connected space with a good cover $\U = \{ U_1, U_2, \dots, U_n\}$. Assume that all the non-empty $U_i$ and $U_{\{i,j\}}$ are connected (this is simplify to our arguments, the proof works without this simplification). We have
\begin{align*}
  \L^0 &= \L(U_1) \oplus \L(U_2) \oplus \dots \oplus \L(U_n) \\
  \L^1 &= \L(U_{\{1,2\}}) \oplus \L(U_{\{1,3\}}) \oplus \dots \oplus \L(U_{\{n-1,n\}})
\end{align*}
and $\check H^0(X)$ is the kernel of the restriction maps $d^0 : \L^0 \rightarrow \L^1$. \begin{center}
  \begin{tabular}{ l | c c  }
        & $U_l$ & $\cdots$ \\\hline
    $U_{\{i,j\}}$ &   0 \mbox{ or } 1    &      \\
    \vdots &       &
  \end{tabular} $ = d^0$
\end{center}

\begin{ques} $ $
  \begin{enumerate}
    \item As $U_{\{i,j\}} = U_i \cap U_j$, argue that every row of the matrix $d^0$ (in the canonical bases) has exactly 2 non-zero entries.
    \item Argue that because $X$ is connected every open set in the cover $U_i$ must intersect some other $U_j$. What does this imply for the matrix $d^0$?
    \item Show that
    \begin{align*}
        \ker d^0 = \{ [0, 0, \dots, 0]^T, [1, 1, \dots, 1]^T\}
    \end{align*}
    and hence $\dim \check H^0(X) = 1$.
    \item Suppose $Y$ is another topological space.
      Find the Cech cohomologies of $X \sqcup Y$ (the disjoint union of $X$ and $Y$) in terms of  $X$ and $Y$. In particular, what is $\check H^0(X \sqcup Y)$?
    \item Find a topological interpretation of $\dim \check H^0(Z)$ for a general topological space $Z$ (not necessarily connected).
  \end{enumerate}
\end{ques}

\begin{ques} Find the Cech cohomologies of $X \vee Y$ in terms of  $X$ and $Y$. (Assume we have appropriate good covers.)
\end{ques}

\newpage
We'll next prove that $\R^n \not \cong \R^m$ (non-homeomorphic) if $n \not = m$. Let
\begin{align*}
  S^n &= \{ (x_0, x_1, \dots, x_n) \in \R^{n+1}: x_0^2 + x_1^2 + \dots + x_n^2 = 1 \}
\end{align*}
\begin{ques} $ $
  \begin{enumerate}
  \item Let $X, Y$ be topological spaces such that $Y$ is contractible. If $\U$ is a good cover for $X$, find a good cover for $X \times Y$. Show that $\check H^i(X) \cong \check H^i(X \times Y)$ for all $i \in \Z$. In particular, this is true when $Y = (0,1)$.
  \item Show that $\R^{n} \setminus \{ 0 \} \cong S^{n-1} \times (0,1)$.
  \item Let $X, Y$ be topological spaces. Show that if $X \cong Y$ then $\check H^i(X) \cong \check H^i(Y)$.
  \item Conclude that if $\R^n \cong \R^m$ then  $\check H^i (S^{n-1}) \cong \check H^i (S^{m-1})$ for all $i \in \Z$.
\end{enumerate}
\end{ques}
\noindent Thus we've reduced the problem to a cohomology computation, which we can do by finding an appropriate good cover of $S^{n}$. Let \begin{align*}
  S^n_+ &= \{ (x_0, x_1, \dots, x_n) \in \R^{n+1}: x_n \ge 0, x_0^2 + x_1^2 + \dots + x_n^2 = 1 \}\\
  S^n_- &= \{ (x_0, x_1, \dots, x_n) \in \R^{n+1}:  x_n \le 0, x_0^2 + x_1^2 + \dots + x_n^2 = 1 \}
\end{align*}

\begin{ques} $ $
\begin{enumerate}
  \item What is $S^0$?
  \item What are the dimensions of the Cech cohomologies of $S^0$, $S^1$, and $S^2$? Based on these make a guess as to what the dimensions of the Cech cohomologies of $S^n$ are.
\end{enumerate}
\end{ques}
\noindent We'll find the cohomologies of $S^n$ using induction.
For a non-empty subset $X \subseteq S^{n-1}$ define the (positive) cone over $X$, denoted $C_+ X$, to be a subspace of $S^{n}_+$ defined as
\begin{align*}
  C_+ X = \{ (tx_0, tx_1, \dots, tx_{n-1}, x_n) \in S^{n}_+ : t \in \R_{\ge 0}, (x_0, x_1, \dots, x_{n-1}) \in X \}
\end{align*}
Define the cone over the empty set $C_+(\phi)$ to be the single point $(0,\dots,0,1)$.

\begin{ques} Let $X,Y \subseteq S^{n-1}$.
\begin{enumerate}
  \item Let $X$ be some subset of $S^1$. Draw the cone $C_+X \subseteq S^2$.
  \item Prove that $C_+ X$ is connected and contractible.
  \item Prove that $C_+X \cap C_+ Y = C_+(X \cap Y)$ (this is why we need $C_+ \phi = (0,\dots,0,1)$).
  \item Prove that $C_+X \cap S^{n}_- \cong X$.
\end{enumerate}
\end{ques}
\begin{ques} By induction, suppose $S^{n-1}$ has a good cover $\U = \{ U_1, \dots, U_n\}$ with $n$ elements. (Check the base case.)
\begin{enumerate}
  \item  Prove that $\U' = \{ C_+ U_1, C_+ U_2, \dots, C_+ U_n, S^n_-\}$ is a good cover of $S^n$ (this proves the induction step).
  \item What is the dimension of this cover? Conclude that $\check H^{i}(S^n) = 0$ for $i > n$.
\end{enumerate}
\end{ques}
\noindent There are two types of intersections $\U'_I$:\\
$\qquad$ (type I) ones obtained by intersecting the sets $\{ C_+ U_1, C_+ U_2, \dots, C_+ U_n\}$  and \\$\qquad$ (type II) ones obtained by intersecting $S^n_-$ with the some of the sets from $\{ C_+ U_1, C_+ U_2, \dots, C_+ U_n\}$.
\begin{ques} $ $
  \begin{enumerate}
  \item Show that the intersections of type I are all contractible and the intersections of type II are the ones that show up in the Cech complex for $S^{n-1}$ and hence the Cech complex for $S^n$ contains a copy of the Cech complex of $S^{n-1}$ \emph{right} shifted by one.
  \item Use this to inductively prove that for $n \ge 1$
  \begin{align*}
    \check H^i(S^{n}) \cong \begin{cases}
      \F & \mbox{ for } i=0,n \\
      0 & \mbox{ otherwise }
  \end{cases}
  \end{align*}
  \item Conclude that $S^n \not \cong S^m$ and $\R^n \not \cong \R^m$ if $n \neq m$.
\end{enumerate}
\end{ques}


\section{Introduction}
\subsection{Problem Description}
The main goal of the activity described in this report is the following: realizing a network implementing a \textbf{perceptron} with a \textbf{sigmoid activation function}.\\
Before describing the whole design and implementation process a very little introduction about the architecture must be done.\\

\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{img/perceptron.png}
	\caption{Perceptron Architecture}
\end{figure}

A \textbf{Perceptron} is a \textit{binary classifier that maps his inputs to a specific output y = f(z), where f() is the \textbf{activation function} of the perceptron.} The inputs are real numbers and the input z of the activation function is obtained as:\\
\begin{equation}
	z = b + \sum_{i = 0}^{N_{L}-1}w_{i}x_{i}
\end{equation}

Every input $x_{i}$, every weight $w_{i}$ and the bias $b$ are real numbers in the range of $[-1, 1]$. $N_{L}$ represents the number of inputs $x_{i}$ of the perceptron.\\
The \textbf{activation function}, in our case, will be a \textbf{sigmoid function}, described as follows:
\begin{figure}[h]
	\centering
	\caption{Sigmoid Function Plot}
	\includegraphics[width=8cm]{img/sigmoid.png}
\end{figure}
\begin{equation}
	y = \dfrac{1}{1+e^{-z}}
\end{equation}
Where $z$ is the result of the equation (1.1).
\subsection{Applications}
A single perceptron is the building block of \textit{artificial neural networks}, in which different layers of perceptrons are connected. The output of the neural network is a real number and could be use to classify \textit{complex objects}: patterns, human faces (Apple's FaceID), handwritings (Google Keyboard), medical diagnosis, e-mail spams (Gmail) and so on and so forth.\\
\begin{figure}[H]
	\centering
	\caption{Neural network example}
	\includegraphics[width=8cm]{img/neural_network.png}
\end{figure}
In the image above there is a simple schema of a neural network, in which the circles represent the perceptrons.
\subsection{Possible Architectures}

The main architecture will be made up by three main logical parts, from an higher-lever point of view:
\begin{itemize}
	\item \textbf{Multiplication Circuit}: implementation of the multiplication operation between each input $x_{i}$ and each weight $w_{i}$.
	\item \textbf{Adder Circuit}: implementation of the addition between the results of the former phase and the bias $b$.
	\item \textbf{Activation Function Circuit}: implementation of the computation of the sigmoid function.
\end{itemize}

In the next chapter the architecture will be documented with more precision.
Different project choices could be made for each logical part of the architecture:

\begin{itemize}
	\item{\textbf{Multiplication Circuit}}: could be implemented through a \textbf{ROM-based solution} in which every possible result is stored and the two inputs represent the addresses for getting the result. This solution is good only with \textbf{a very low number of bits}, which is not our case: in fact the the ROM will be composed by $2^{(n_{w_{i}} + n_{b_{i}})}$ memory cells ($n_{w_{i}}$ represent the number of bits of $w_{i}$ and the same for $n_{b_{i}}$ and $b_{i}$). In order to implement the multiplication circuit will be implemented through a  \textbf{Paraller Multiplier}, with some additional logic to handle the signed inputs.
	\item{\textbf{Adder Circuit}}: different choices could be made to implement the adder circuit. Some possible solutions, starting from the simplest to the more complex solution, that we can exploit are the \textbf{Serial Adder}, the \textbf{Parallel Adder} or the \textbf{Parallel Adder with Pipeline }. The first one needs less logic but requires $n$ clock cycles for computing an $n$ bits result. The second solution improves the first one by computing one result in \textbf{one clock cycle}, on the other hand it could add some problems due to long logic chains between two register. The third solution is the best from the perspective of the number of clock cycles required and the \textbf{critical path}, in fact by adding some registers in between the computation of the bits will reduce the logic chains (and increasing the number of clock cycles though).
	\item{\textbf{Activation Function Circuit}}: As seen during the laboratory class, this part will be implemented by exploiting a \textbf{Look-Up-Table}. In order to do so, could be necessary a \textbf{truncation} of the result of the former computation in order to limit the size of the LUT. With $12$ bits are necessary $2^{12} = 4096$ entries, which could be even reduced by performing some optimization by exploiting the sigmoid function symmetry. For further details see next Chapter.
\end{itemize}


% \chapauthor{J. P. Balthasar Mueller}
\chapter{Linear Regression}
\label{chap:linearregression}

\begin{multicols}{2}[\subsubsection*{Contents of this chapter}]
   \printcontents{}{1}{\setcounter{tocdepth}{2}}
\end{multicols}

There is no escaping from linear regression but hardly anyone seems to agree on how to do it properly. Even worse, people seem to expect you to know things like the normal equations, which you'll never, ever need outside of a job interview.

\section{Least Squares Regression ($L^2$)}

\subsection{The Normal Equations, Analytical Least Squares Estimator}
Section \ref{sec:linearequations} discussed the case of the linear system of equations:

\begin{equation}
\mathbf{A}\mathbf{x} = \mathbf{b}
\end{equation}

When $\mathbf{A}^{m\times n}$ with $m>n$, so that the system is overdetermined. This is, of course, the starting point for least squares regression, only that the convention is to use different letters:

\begin{equation}
\mathbf{X}\mathbf{\beta} = \mathbf{y}
\end{equation}

And that, seeing that the system is overdetermined, one looks for an approximate solution $\mathbf{\hat{\beta}}$, so that

\begin{equation}
\mathbf{X}\mathbf{\beta} + \mathbf{\epsilon} = \mathbf{y}
\end{equation}

A natural approach for picking an approximate solution $\mathbf{\hat{\beta}}$ is to look for the projection of $\mathbf{y}$ in the column space of $\mathbf{X}$. That is, since the column rank $\leq n$ of $\mathbf{X}$ is insufficient to express $m$-dimensional $\mathbf{y}$ exactly in terms of only $m$ coefficients $\mathbf{\beta}$, we look for the $n$-dimensional shadow $\mathbf{\hat{\beta}}$ of some hypothetical higher dimensional exact solution.

The projection has the property that it maximizes the dot product $(\mathbf{X}\mathbf{\hat{\beta}})\cdot \mathbf{y}$, and hence minimizes the length of the difference vector $\epsilon$. In turn, the length of the difference vector $\epsilon$ is $\sqrt{\epsilon\cdot\epsilon}$, which is monotonic to $\epsilon\cdot\epsilon = \sum^m_i \epsilon_i^2$. That means that finding the projection of $\mathbf{y}$ in the column space of $\mathbf{X}$ minimizes the  $L_2$ norm of $\epsilon$, also known as \textit{least squares error}.

There are two ways to go about finding $\mathbf{\hat{\beta}}$.

\subsection{The Quick Way to $\mathbf{\hat{\beta}}$}

By construction, the vector $\epsilon$ is orthogonal to the column space of $\mathbf{X}$. Which means:

\begin{equation}
\begin{array}{rl}
\mathbf{X}^T\epsilon &= 0\\
\mathbf{X}^T\left(\mathbf{X}\mathbf{\hat{\beta}}-\mathbf{y}\right) &= 0\\
\mathbf{X}^T\mathbf{X}\mathbf{\hat{\beta}} &= \mathbf{X}^T\mathbf{y}\\
\mathbf{\hat{\beta}} &= \left(\mathbf{X}^T\mathbf{X}\right)^{-1}\mathbf{X}^T\mathbf{y}
\end{array}
\end{equation}

Making use of the fact that $\mathbf{X}^T\mathbf{X}$ is square and therefore hopefully invertible.

\subsection{The Long Way to $\mathbf{\hat{\beta}}$}

Loss functions play a central role in computational statistics (for example when regularization is introduced), and therefore it is of interest to approach finding $\mathbf{\hat{\beta}}$ by instead minimizing the least square error. This requires:

\begin{equation}
\frac{d}{d\mathbf{\hat{\beta}}}L_2(\epsilon) = 0
\end{equation}

where

\begin{equation}
\begin{array}{rl}
L_2(\epsilon) &= \left(\mathbf{X}\mathbf{\hat{\beta}}-\mathbf{y}\right)^T\left(\mathbf{X}\mathbf{\hat{\beta}}-\mathbf{y}\right)\\
&= \mathbf{\hat{\beta}}^T\mathbf{X}^T\mathbf{X}\mathbf{\hat{\beta}} - \mathbf{\hat{\beta}}^T\mathbf{X}^T\mathbf{y} - \mathbf{y}^T\mathbf{X}\mathbf{\hat{\beta}} + \mathbf{y}^T\mathbf{y}
\end{array}
\end{equation}

Taking derivatives with respect to a vector is covered in section \ref{sec:derivatives}.

It follows:

\begin{equation}
\begin{array}{l}
\frac{d}{d\mathbf{\hat{\beta}}}\left(x^T\mathbf{X}^T\underbrace{\mathbf{X}\mathbf{\hat{\beta}}}_{u(\mathbf{\hat{\beta}})}\right) = \frac{d}{du}\left(u^Tu\right)\frac{d}{d\mathbf{\hat{\beta}}}u = 2u^T\frac{d}{d\mathbf{\hat{\beta}}}u = 2\mathbf{\hat{\beta}}^T\mathbf{X}^T\mathbf{X}\\
\\
\frac{d}{d\mathbf{\hat{\beta}}}\mathbf{\hat{\beta}}^T\mathbf{X}^T\mathbf{y} = \mathbf{y}^T\mathbf{X}\\
\\
\frac{d}{d\mathbf{\hat{\beta}}}\mathbf{y}^T\mathbf{X}\mathbf{\hat{\beta}} = \mathbf{y}^T\mathbf{X}\\
\\
\frac{d}{d\mathbf{\hat{\beta}}}\mathbf{y}^T\mathbf{y} = 0
\end{array}
\end{equation}

So that

\begin{equation}
\begin{array}{rl}
\frac{d}{dx}L_2(\epsilon) = 0 &= 2\mathbf{\hat{\beta}}^T\mathbf{X}^T\mathbf{X} - 2\mathbf{y}^T\mathbf{X}\\
\mathbf{\hat{\beta}}^T\mathbf{X}^T\mathbf{X} &= \mathbf{y}^T\mathbf{X}\\
\mathbf{X}^T\mathbf{X}\mathbf{\hat{\beta}} &= \mathbf{X}^T\mathbf{y}\\
\mathbf{\hat{\beta}} &= \left(\mathbf{X}^T\mathbf{X}\right)^{-1}\mathbf{X}^T\mathbf{y}
\end{array}
\end{equation}

\subsection{Projection Matrix}

If $\mathbf{\hat{y}}=\mathbf{X}\mathbf{\hat{\beta}}$ is the projection of $\mathbf{y}$ in the column space of $\mathbf{X}$, then, based on the result for $\hat{\beta}$, the projection matrix is $\mathbf{P} = \mathbf{X}\left(\mathbf{X}^T\mathbf{X}\right)^{-1}\mathbf{X}^T$. In a fully determined system, $\mathbf{P}=\mathbf{I}$. Projection matrices have eigenvalues that are either $1$ or $0$, corresponding to dimensions that are kept or discarded during the projection operation.

\subsection{Bayesian Perspective on Least Squares Regression}

\subsection{Q-plots}
\subsection{Variance Inflation Factor}

\section{Total Least Squares}
While least squares regression only allows for errors in the dependent variable, total least squares regression allows for measurement errors on both variables.

\section{Ridge Regression (Tikhonov Regularization, $\lambda ||\mathbf{\beta}||^2$)}
Ridge Regression ads the $L^1$ norm of the weight vector to

\subsection{Analytical Ridge Estimator}
\subsection{Bayesian Perspective on Ridge Regression}

\section{Least Absolute Shrinkage and Selection Operator Regression (LASSO)}

\section{Least Absolute Deviation Regression (LAD, $L^1$)}

\section{Generalized Linear Models}

\input{./chapters/sections/reg_countregressions.tex}

\input{./chapters/sections/reg_logisticregression.tex}

\chapauthor{}


\chapter{Computation}

\section{Unary algebra}

\index{unary algebra}%
A \emph{unary algebra} is \((S,f)\) where \(f : S \to S\).

The natural graph of a unary algebra \((V,f)\) is \((V,E)\)
where \((a,b) \in E\) iff \(f(a) = b\).

\section{String}

\index{language}%
A \emph{language} is a set of strings.

Two
\index{string}%
\emph{strings}
\(x\) and \(y\) can be
\index{string concatenation}%
\index{concatenation!string}%
\emph{concatenated} to \(xy\).

Two languages \(A\) and \(B\) can be
\index{language concatenation}%
\index{concatenation!of languages}%
\emph{concatenated} to
\(AB = \{ ab ~|~ a \in A, ~ b \in B \}\).

The
\index{empty string}%
\index{string!empty}%
\emph{empty string} \(\emptystr\) satisfies \(\emptystr a = a \emptystr = a\).

\index{alphabet}%
An \emph{alphabet} is a finite set where each element is a string of length one.

\paragraph{Power}
Let \(A\) be a language and \(n\) be a natural number.
Define \(A^{n+1} = A A^n\).
Corollary: \(A^0 = \{\emptystr\}\).

\paragraph{Kleene closure}
Let \(A\) be a language.
The
\index{Kleene closure}%
\index{Kleene operator}%
\index{Kleene star}%
\emph{Kleene closure}
of \(A\) is \(A^* = A^0 \cup AA^*\).
Another nonrecursive definition is
\( A^* = \bigcup_{n \in \Nat} A^n \).

\(A^*\) is the smallest superset of \(A\)
closed under string concatenation.

A
\index{formal system}%
\emph{formal system} \(F\) is a relation \(F \subseteq A^* \times A^*\)
made from finitely many rules where each rule has the form \(L \vdash R\)
where \(A\) is the alphabet.
What limits the form of \(L\) and \(R\)?

A
\index{problem}%
\emph{problem} \(P\) is a relation \(P \subseteq A^* \times A^*\) where \(A\) is the alphabet.
An \emph{instance} of the problem is an element of the problem's relation.

A
\index{problem!decision}%
\index{decision problem}%
\emph{decision problem} is isomorphic to a language
containing the strings representing the yes-instances.

\(F\)-unspecifiable problem:
Proof by diagonalization?

\section{Machine as formal system}

\paragraph{Example}
Let \(M\) be the language.
Syntax:
Literals are \(\{\emptystr, 1\}\).
Empty string \(\emptystr \in M\).
\(1 \in M\).
\(A \in M, B \in M \vdash AB \in M\).
Semantics: \(S(\emptystr) = 0\) and \(S(1A) = 1 + S(A)\).
The syntax of that formal system can also be described using turnstiles:
\begin{align*}
    &\vdash \emptystr
    \\
    &\vdash 1
    \\
    A,B &\vdash AB
\end{align*}

The expression \(\vdash_M A\) means that \(A \in M\).
The expression \(A \vdash_M B\) means that if \(A \in M\), then \(B \in M\).
If \(M\) is clear from context, we write \(\vdash\) instead of \(\vdash_M\).

\paragraph{Example}
Syntax:
Literals are \(\{z,s,p\}\):
\begin{align*}
    &\vdash z
    \\
    A &\vdash s(A)
    \\
    A,B &\vdash p(A,B)
\end{align*}
Semantics/interpretation \(I : M \to \Nat\):
\begin{align*}
    I(z) &= 0
    \\
    I(s(A)) &= 1 + I(A)
    \\
    I(p(A,B)) &= I(A) + I(B)
\end{align*}

A state \(A\) is \emph{terminal} iff \(A \vdash\).

\section{Computability with respect to primitives}

Let the \emph{primitive set} \(P\) be a finite set of relations.
The set of all \emph{\(P\)-computable} functions is \(P^*\).
A relation \(f\) is \(P\)-computable iff it is equal to
a constant finite composition of relations in \(P^*\).

\paragraph{Deterministic}
Iff every primitive is a function,
then the system is \emph{deterministic}.
Iff every primitive is a relation,
then the system is \emph{nondeterministic}.
Every function is a relation,
so every deterministic system is also a nondeterministic system,
but not the other way around.

\paragraph{Example}
Let \(P = \{S\}\) where \(S(n) = n+1\).
Then \(g(n) = n + 3\) is \(P\)-computable because \(g(n) = S(S(S(n)))\).
For every constant \(k\), the addition-by-constant function \(h_k(n) = n+k\) is \(P\)-computable because \(h_k = S^k\).
However, \(n \to 2n\) is not \(P\)-computable
because there is no \(k\) such that \(S^k(n) = 2n\) for all \(n\).

\paragraph{Example}
Let \(P = \{f,l\}\) where \(f(a,b) = (a+b,b)\) and \(l(a,b) = a\).
Then \(g(x,y) = x+y\) is \(P\)-computable because \(g(x,y) = l(f(x,y))\).

\paragraph{Example}
Let \(P = \{S,r\}\) where \(S(n)=n+1\), \(r(0,x) = x\), and \(r(S(n),x) = r(n,f(x))\).
Then \(g(x,y) = x+y\) is \(P\)-computable because \(g(x,y) = r(x,y)\).

Lemma:
If \(r(0,x) = x\) and \(r(n+1,x) = f(r(n,x))\) then \(r(n+1,x) = r(n,f(x))\).

Lemma:
If \(r(0,x) = x\) and \(r(n+1,x) = r(n,f(x))\) then \(r(n+1,x) = f(r(n,x))\).

\section{Complexity}

The \(P\)-complexity of a \(P\)-computable relation \(f\)
is the number of \(P\)-relations used to make \(f\).

\section{Circuit size}

The circuit-size complexity of a zeroth-order logic formula:
\begin{align*}
    C(a) &= 1 \text{ if \(a\) is an axiom}
    \\
    C(\neg p) &= 1 + C(p)
    \\
    C(p \wedge q) &= 1 + C(p) + C(q)
    \\
    C(p \vee q) &= 1 + C(p) + C(q)
\end{align*}

The circuit-depth complexity of a zeroth-order logic formula:
\begin{align*}
    D(a) &= 1 \text{ if \(a\) is an axiom}
    \\
    D(\neg p) &= 1 + D(p)
    \\
    D(p \wedge q) &= 1 + \max(D(p),D(q))
    \\
    D(p \vee q) &= 1 + \max(D(p),D(q))
\end{align*}

What prevents us from ``cheating'' by encoding the input as the solution?

\section{Primitive recursive functions}

\section{Program}

Let \(AB = \{ ab ~|~ a \in A, ~ b \in B \}\)
where \(ab\) is string concatenation.

Let the \emph{instruction set} \(I\) be a finite set of atoms.

The set of all \(I\)-programs is \(I^*\).

A program is a finite element of \(I^*\).

Let the \emph{execution function} \(f : I^* \to P^*\)
where
\(f(\emptystr) = id\),
\(f(ab) = f(b) \circ f(a)\),
and that \(x \in I\) implies \(f(x) \in P\).
The set of all \emph{\(I\)-describable} functions are \(\{ f(x) ~|~ x \in I^* \}\).

\section{Fixed point}

Iff \(x = f(x)\) then \(x\) is a \emph{fixed point} of \(f\).

Let \(F\) be a set of some functions.
Let \(G(F)\) be the set of all functions that can be made by combining the functions in \(F\).

A recursive equation has the form \(f(x) = g(f,x)\).
For example, iff \(x^2 = x^4 + 1\) and \(f(x) = x^2\), then \(g(f,x) = x^4 + 1 = [f(x)]^2 + 1 = f(x^2) + 1 = x^4 + f(1)\)
and many more.

The \(y\)-combinator satisfies \(y(f) = f(y(f))\).

\section{Machine}

A \emph{machine} is \((S,f)\)
where \(S\) is the \emph{state set},
and \(f \subseteq S \times S\) is the \emph{transition relation}.
Iff \(f\) is a function, then the machine is \emph{deterministic}.
\(I = \{ a ~|~ a \in S, ~ (a,b) \not\in f \}\) is the \emph{initial state set}.

\paragraph{Graph}
Such machine is also a \emph{graph} \((V,E)\) where \(V=S\), \(E = f\),
and \(I\) is the set of all \emph{source} vertices.
A source vertex is a vertex with zero indegree.
A sink vertex is a vertex with zero outdegree.

The \emph{time complexity} of an instance is the path length from its initial state to its terminal state.

\(G\) \emph{accepts} \(v\) iff \(F^\infty(\{v\}) = \emptyset\) where \(F\) is the graph's fringe function.
The \emph{language} recognized by \(G\) is the largest \(L \subseteq V\) such that \(F^\infty(L) = \emptyset\).

A Turing machine is \((C,I,f)\)
where \(C\) is countable
and \(f\) is recursive.

Example: a state of a Turing machine is \((c,l,h,r)\)
where \(c\) is a configuration,
\(l\) is the tape content to the left of the head,
\(h\) is the tape content at the head,
and \(r\) is the tape content to the right of the head.

\section{Primitive by distance}

Iff \(d\) is a distance function and \(S\) is the state type,
then \(f\) is \emph{\(d\)-primitive} iff \(\forall x \in S : d(x,f(x)) \le 1\).

Let \(P\) be a set of some \(d\)-primitive functions.
Let \(P^*\) be the set of all finite compositions of the functions in \(P\).

\paragraph{Example}
Let \(S\) be the set of all infinite \(\Nat\)-indexed \(E\)-arrays.
Let \(x,y \in S\).
The \emph{Hamming distance between \(x\) and \(y\)} is
\(d(x,y) = \sum_{k\in\Nat} [x_k \neq y_k]\).
The function \(f(x_0,x_1,\ldots) = f(0,x_1,\ldots)\) is \(d\)-primitive,
which in English means that this \(f\) does not change more than one element.

\section{Example stack machine primitive}

Let \(S = \Nat^\infty\).
Example:
Infinite-stack machine:
\begin{align*}
    swap(a,b,c,\ldots) = (b,a,c,\ldots)
    \\
    dup(a,b,\ldots) = (a,a,b,\ldots)
    \\
    push(a,b,\ldots) = (0,a,b,\ldots)
    \\
    pop(a,b,c,\ldots) = (b,c,\ldots)
    \\
    read(k,x_0,x_1,\ldots) = (x_k,x_0,x_1,\ldots)
    \\
    write(k,v,x_0,x_1,\ldots) = (k,v,x_0,x_1,\ldots,x_{k-1},v,x_{k+1},\ldots)
    \\
    if(0,f,t,a,\ldots) = (f,a,\ldots)
    \\
    if(1,f,t,a,\ldots) = (t,a,\ldots)
    \\
    inc(a,b,\ldots) = (a+1,b,\ldots)
    \\
    dec(a,b,\ldots) = (a-1,b,\ldots)
    \\
    add(a,b,c,\ldots) = (a+b,c,\ldots)
\end{align*}

\section{Machine (old)}

A machine that can only change one cell at a time.
A machine is
\((P, f)\)
where \(P = (S,A^\infty)\)
and
\(d(x,f(x)) \le 1\)
for all \(x : P\).
The state set \(S\) is finite.
The alphabet \(A\) is finite.
Define the distance between two phases as
\begin{align}
    d~(s,x)~(t,y) = \sum_{k : \Nat} ~ [x_k \neq y_k].
\end{align}
It ignores the state.
It only cares about the memory.

But unlike Turing machines, this machine cannot approach an infinite number of 1s...

The machine is free to change its state as long as it only changes at most one cell at a time.
If we remove the restriction, we get a nondeterministic machine,
or even an oracle.

If \(S\) has exactly \(n\) elements,
we can encode \(f : S \to A\) as \(g : A^n\)
where \(f~s_k = g_k\) where \(g = (f~s_1, \ldots, f~s_n)\).

\section{Machine (older)}

The transition function depends on the set of primitive operations of the machine.
Seen the other way around, this transition function
determines the set of primitive operations of the machine.

A machine is an embodiment of an algorithm.

A machine performs computation by repeatedly
making a transition from its current configuration
according to its transition function
until it reaches a terminal configuration.

A \emph{primitive operation} maps a configuration to a configuration.
Every primitive operation represents a computation that the machine can do in one unit time.
An \emph{architecture} is a set of primitive operations
and a set of rules for evaluating expressions built using those primitive operations.
A \emph{machine} is an architecture and a configuration representing its current state.

\section{Finite automaton}

\emph{Automata} is the plural of automaton.

% https://en.wikipedia.org/wiki/Automata_theory#Formal_definition

A
\index{finite automaton}%
\emph{finite automaton} is \(M = (Q,A,T,i,F)\) where
\(A\) is the alphabet,
\(T \subseteq Q \times A \times Q\) is the transition relation,
\(i \in Q\) is the initial state,
and \(F \subseteq Q\) is the set of accepting states.
Another name of finite automaton is
\index{state machine}%
\emph{state machine}.

A finite automaton is a formal system.

\paragraph{Example}
Let \(Q = \{q_0,q_1\}, A = \{0,1\}, i = q_0, F = \{q_1\}\), and:
\begin{align*}
    &\vdash T(q,0,q)
    \\
    &\vdash T(q,1,q_1)
\end{align*}
Then \(M\) recognizes \(\{ x ~|~ x \in \{0,1\}^*, x \text{ contains 1}\}\).

\paragraph{Final-state relation}
The \emph{final-state relation} of \(M\) is \(Z \subseteq A^* \times Q\) where:
\begin{align*}
    &\vdash Z(\emptystr,i)
    \\
    Z(p,a), T(a,c,b) &\vdash Z(pc,b)
\end{align*}
The \emph{final states} of \(M\) for input \(w\) is \(Y(w) = \{ q ~|~ Z(w,q) \}\).

\(M\) \emph{accepts} \(w\) iff \(F \subseteq Y(w)\).

The \emph{language recognized by \(M\)} is \(\{ w ~|~ F \subseteq Y(w) \}\).

There is a bijection between finite automata and their final-state relations.

\section{Wang B-machine}

\(I = \{ l,r,e,m,n \} \cup \{ c_k ~|~ k \in \Nat \}\) is \emph{control alphabet}
where \(l\) is left, \(r\) is right, \(e\) is erase,
\(m\) is mark, \(n\) is no-operation,
\(c\) is conditional-absolute-jump-if-marked.

\(T = \{0,1\}\) is \emph{data alphabet} where \(0\) is blank and \(1\) is mark.

\paragraph{Execution}
A state is \(S(a,b,c,d)\) where \((a,b)\) is control tape and \((c,d)\) is data tape.
The string \(b\) ends with infinitely many no-operation symbols (\(n\)).
The string \(c\) begin with infinitely many blanks.
The string \(d\) ends with infinitely many blanks (\(0\)).
The leftmost symbol in \(b\) is the current instruction.
The leftmost symbol in \(d\) is the current scanned data tape cell.
\begin{align*}
    \alpha \in T, ~ S(A,l B,C \alpha,D) &\vdash S(A l,B,C,\alpha D)
    \\
    \alpha \in T, ~ S(A,r B,C,\alpha D) &\vdash S(A r,B,C \alpha,D)
    \\
    \alpha \in T, ~ S(A,e B,C,\alpha D) &\vdash S(A e,B,C,0 D)
    \\
    \alpha \in T, ~ S(A,m B,C,\alpha D) &\vdash S(A m,B,C,1 D)
    \\
    S(A,c_n B,C,0 D) &\vdash S(A c_n,B,C,0 D)
    \\
    |A| = n, ~ S(A \alpha, c_n B, C, 1 D) &\vdash S(A, \alpha c_n B, C, 1 D)
\end{align*}

\paragraph{Example}
The program \(e r c_0\) replaces all marks with blanks.

\section{Turing machine}

A \emph{Turing machine} is a finite-state machine with read-write memory.
How do we model finite-state machine?
How do we model read-write memory?

Let \(m\) be a Turing machine.

Let \(A\) be the alphabet.
It is finite.

Let \(S\) be the state type.
It is finite.

Let \(P = (S,T)\) be the phase type.

Let \(T = (A^\infty,A,A^\infty)\) be the tape type.

Define the \emph{phase} of \(m\) as \((s,t)\)
where \(s\) is the \emph{state} and \(t\) is the \emph{tape}.

A tape is \((L,h,R) : (A^\infty,A,A^\infty)\).

Define the transition function \(\after : P \to P\).

The pluggable parts of the machine are
the alphabet type \(A\),
the state type \(S\),
the next-state function \(\jump : (S,A) \to S\),
the output-symbol function \(\fwrite : (S,A) \to A\),
and the direction function \(\dir : (S,A) \to \{0,1\}\).
The \(\jump\) computes the next state.
The \(\fwrite\) function computes the symbol to write.
The \(\dir\) function computes where the head should move after writing that symbol.

The tape is \((L,h,R)\).
The string \(L\) is the \emph{reverse} of the string on the left of the head.
The symbol \(h\) is the symbol at the head.
The string \(R\) is the string on the right of the head.

Here comes an equational description of a Turing machine.
For readability, we don't use the minimum number of equations.

\section{The fixed parts}

A Turing machine must satisfy all of these.
These are the parts we cannot change.

The types.
\begin{align}
    P &= (S,T)
    \\
    T &= (\InfList~A, ~ A, ~ \InfList~A)
    \\
    \after &: P \to P
\end{align}

The logical constraints.
\begin{align}
    S &\text{ is finite}
    \\
    A &\text{ is finite}
\end{align}

The value definitions:
\begin{align}
    \after~(s,t) &= (s',t')
    \\
    t &= (L,h,R)
    \\
    s' &= \jump~i
    \\
    t' &= \dir~\stay~\fleft~\fright~i~(L,w,R)
    \\
    i &= (s,h)
    \\
    w &= \foutput~i
    \\
    \stay~(L,h,R) &= (L,h,R)
    \\
    \fleft~(cL,h,R) &= (L,c,hR)
    \\
    \fright~(L,h,cR) &= (hL,c,R)
\end{align}

We try to avoid pattern matching:
\begin{align}
    t' &= (L',h',R')
    \\
    w &= \foutput~i
    \\
    L' &= \dir~\stay_0~\fleft_0~\fright_0~L
    \\
    h' &= \dir~\stay_1~\fleft_1~\fright_1~L~w~R
    \\
    R' &= \dir~\stay_2~\fleft_2~\fright_2~R
    \\
    \stay~(L,h,R) &= (L,h,R)
    \\
    \fleft~(L,h,R) &= (\tail~L, \head~L, \cons~h~R)
    \\
    \fright~(L,h,R) &= (\cons~h~L, \head~R, \tail~R)
    \\
    \stay_0~L &= L
    \\
    \fleft_0~L &= \tail~L
    \\
    \fright_0~L &= \cons~L
    \\
    \stay_1~h &= h
    \\
    \fleft_1~L &= \head~L
    \\
    \fright_1~R &= \head~R
    \\
    \stay_2~R &= R
    \\
    \fleft_2~R &= \cons~R
    \\
    \fright_2~R &= \tail~R
    \\
    \stay_0 &= \stay_2
    \\
    \fleft_0 &= \fright_2
    \\
    \fright_0 &= \fleft_2
    \\
    \fleft_1 &= \fright_1
\end{align}

Some note about the notations:
We write \(A^\infty\) for the set of all infinite strings of \(A\).

\section{The pluggable parts}

There are the parts we can change.
\begin{align}
    s_0 &= \text{the initial state}
    \\
    S &= \text{the set of states}
    \\
    A &= \text{the set of symbols}
    \\
    \jump &= \text{a function of type \((S,A) \to S\)}
    \\
    \foutput &= \text{a function of type \((S,A) \to A\)}
    \\
    \dir &= \text{a function of type \(\forall a ~.~ a \to a \to a \to (S,A) \to a\)}
\end{align}

If you have the pluggable parts,
the equations will give you a Turing machine.

\section{Some definitions}

The input is the initial phase.

Let \(x = (s,t)\).
We say that \(x\) is an \emph{initial} phase iff \(s = s_0\).
We say that \(x\) is a \emph{halting} phase iff \(\after~x = x\).

The \emph{phase graph} is defined as follows.
Its vertex set is the set of all phases.
An edge \((x,y)\) is in the graph iff \(\after~x = y\).

We say that \(x\) \emph{eventually leads} to \(y\) iff
there is a path from \(x\) to \(y\) in the phase graph.

We say that the machine \emph{halts for input \(t\)} iff
\((s_0,t)\) eventually leads to a halting phase.

Let the alphabet be \(A = \{b,0,1\}\) where \(b\) is the blank symbol.
Let \(B = \ldots bbb \ldots\) be an infinite string of blank symbols.
We say that the machine \emph{accepts the input \(t\)} iff
the phase \((s_0,t)\) eventually leads to the phase \((s, (B,1,B))\)
where \(s\) can be any state.

\section{Turing machine as constrained unary algebra}

A Turing machine is a unary algebra with a limited transition function.
It can only read and write at the head.

We can encode an infinite stream of \(a\)
as the infinite type \(S~a = (a \to b) \to (S~a \to b) \to b\).

\emph{A Turing machine is a finite state machine with memory.}

A finite state machine has input, but no memory.

We can encode memory as infinite feedforward.

A finite state machine step function is a function \((S,I) \to (S,O)\).

\section{Logical formulation of Turing machine}

We can encode a function \(f : A \to B\) as a predicate \(f' : A \to B \to Bool\) such that \(f'~a~b\) iff \(f~a = b\).

If you have the predicates \(\jump\), \(\fwrite\), \(\stay\), \(\fleft\), and \(\fright\),
and an initial state and initial tape,
then the Turing machine generator will give you the following inference rules.
You can rewrite the rules in Prolog.
Prolog will then simulate that Turing machine.
\begin{align}
    \inferrule
    {\fwrite~s~h~w \\ \stay~s~h}
    {\tape~s~(L,h,R)~(L,w,R)}
    \\
    \inferrule
    {\cons~h'~L'~L \\ \cons~w~R~R' \\ \fwrite~s~h~w \\ \fleft~s~h}
    {\tape~s~(L,h,R)~(L',h',R')}
    \\
    \inferrule
    {\cons~w~L~L' \\ \cons~h'~R'~R \\ \fwrite~s~h~w \\ \fright~s~h}
    {\tape~s~(L,h,R)~(L',h',R')}
    \\
    \inferrule
    {\jump~s~h~s' \\ \tape~s~(L,h,R)~(L',h',R')}
    {\phase~(s,(L,h,R))~(s',(L',h',R'))}
\end{align}

The computation path beginning from \(p\).
\begin{align}
    \inferrule
    {\phase~p~q \\ p \neq q \\ \fpath~q~r \\ \cons~p~r~r'}
    {\fpath~p~r'}
    \\
    \inferrule
    {\halting~p}
    {\fpath~p~[p]}
\end{align}

\begin{align}
    \halting~x &\equiv \phase~x~x
\end{align}

\begin{align}
    \inferrule
    {\forall s ~ \forall h ~ | \{ w ~|~ \fwrite~s~h~w \} | \le 1 \\ \forall s ~ \forall h ~ |[\stay~s~h] + [\fleft~s~h] + [\fright~s~h]| \le 1}
    {\text{the machine is deterministic}}
\end{align}

\section{What}

Turing machine as mono-unary algebra of states (configuration-tape pairs)

Universal Turing machine

Lambda calculus

Recursive functions

\section{Regex, cfg, Brzozowsky}

If \(a\) is a type, then \(\Regex~a\) is a type.
Usually we call the \(a\) in \(\Regex~a\) the \emph{alphabet}.

\begin{align}
    &\vdash \fnull : \Regex~a
    \\
    &\vdash \fempty : \Regex~a
    \\
    x : a &\vdash x : \Regex~a
    \\
    x : \Regex~a, ~ y : \Regex~a &\vdash xy : \Regex~a
    \\
    x : \Regex~a, ~ y : \Regex~a &\vdash x|y : \Regex~a
    \\
    x : \Regex~a &\vdash x^* : \Regex~a
\end{align}

Sometimes we parenthesize. The expression \(x|y^*\) means \(x|(y^*)\),
which is different from \((x|y)^*\).

Let there be a function \(\match : \Regex~a \to \List~a \to \Bool\).
\begin{align}
    \match~\fnull~x &\vdash
    \\
    &\vdash \match~\fempty~[]
    \\
    x : a, ~ y = [x] &\vdash \match~x~y
    \\
    \match~r~x, ~ \match~s~y &\vdash \match~(rs)~(xy)
    \\
    \match~r~x \vee \match~s~x &\vdash \match~(r|s)~x
    \\
    &\vdash \match~r^*~[]
    \\
    \match~r~x, \match~(r^*)~y &\vdash \match~(r^*)~(xy)
\end{align}

Recall that a language is a \emph{set} of strings.
We define multiplication slightly differently.
We replace ordered pair with string concatenation:
\begin{align}
    Q \times D &= \{ qd ~|~ q : Q, ~ d : D \}.
\end{align}

\section{Laziness}

Complete laziness, full laziness, interaction sets

Compile Java to lambda calculus

High level intermediate language

\section{Descriptive complexity}

% https://en.wikipedia.org/wiki/Fagin%27s_theorem
Fagin's theorem:
There is a bijection between second-order logic predicates and NP problems. (?)

See Neil Immerman's book. (?)


\section{Gasses And Thermodynamics}
	\subsection{Temperature}
	The measure of hot and cold. More scientifically this is the measure of microscopic kinetic energy
	\newline
	The temperature scales are Kelvin (K) which has a zero point of absolute 0, Celsius (C), which has a zero point of water freezing and has 100 at the boiling point of water, and fahrenheit (F).
	\newline
	\begin{align*}
	T_C &= T_k - 273.15\\
	T_F &= \frac{9}{5}T_C+32
	\end{align*}
	\subsection{Thermal Equilibrium}
	Two objects are in thermal equilibrium when, while in direct contact, they have the same temperature. When 2 objects have different temperatures, $T_A$ and $T_B$ are brought into thermal contact and they will exchange energy via heat transfer until they reach thermal equilibrium.
	\newline
	\newline
	Heat transfer is the spontaneous exchange of microscopic energy due to molecular or atomic collisions.
	\subsection{Thermal Expansion}
	Thermal expansion is when a solid object expands when it receives a raise in temperature
	\begin{align*}
	\frac{dL}{dT} = \alpha L\\
	\frac{dA}{dT} = 2\alpha A\\
	\frac{dV}{dT} = 3\alpha V
	\end{align*}
	Where $\alpha$ is the coefficient of linear expansion
	\subsection{Calorimetry}
	Calorimetry is the science of measuring heat transfer. Internal/Thermal energy is the amoung of energy stored in an object as described by its temperature.
	Heat Transfer = Q
	\subsection{First Law of Thermodynamics}
	\begin{align*}
	\Delta E_{int} &= Q-W\\
	dE_{int} &= dQ-dQ\\
	Q &=c \Delta T\\
	c &= \frac{C}{M}\\
	Q &= mc\Delta T
	\end{align*}
	Where c is the specific heat capacity, m is the mass of the substance, and $\Delta T$ is the change in temperature.
	\subsection{Phases of Matter}
	The phases of matter (from hottest to coldest) are Plasma, Gas (condenses to, or evaporates from), Liquid (freezes to, or melts from), Solid, and Condensates. During a phase change $Q=mL_v$ or $Q=mL_v$
	\subsubsection{Example 1}
	The U-district bridge has a span of 450ft. How much will it expand in spokane. The bridge is made out of concrete and steel which both have $\alpha = 12*10^{-6}\frac{1}{C\si{\degree}}$. $T_{high}=110\si{\degree}F \to 120\si{\degree}F$, $T_{low}=-30\si{\degree}F \to -40\si{\degree}F$
	\begin{align*}
	t_{high}&=489\si{\degree}C\\
	T_{low}&=-40\si{\degree}C
	L&=137m
	\end{align*}
	\newline
	\newline
	\begin{align*}
	\frac{\Delta L}{\Delta T}&=\alpha L\\
	\Delta L&=\alpha L\Delta T\\
	&=\left(12*10^{-6}\frac{1}{C\si{\degree}}\right)(137m)(48.9\si{\degree}C--40\si{\degree}C)\\
	&=0.146m
	\end{align*}
	\subsubsection{Example 2}
	Your freezer is set to $23\si{\degree}F$, you remove a 26g ice cube and place it in an empty glass. The next day the ice has melted and come to $72\si{\degree}F$. Find the change in energy of the ice.
	\begin{align*}
	\Delta E_{int}&=M_{ice}C_{ice} \Delta T_{01}+M_{ice}L_{f_{ice}}+M_{water}C_{water} \Delta T_{12}\\
	&=M_{ice}(C_{ice}\Delta T_{01}+L_{f_{ice}}+C_{water} \Delta T_{12})
	\end{align*}

	\subsection{Molecular Model of Gasses}
	We will describe gasses using state variables. State variables are macroscopic quantities used to describe the state of matter. Relative state variables can be used together to make quantitative predictions. This can be done using equations of state. Pressure $P=\frac{F}{A}$, Volume V, temperature T, the number of gas particles N, or the number of moles $n=\frac{N}{N_A}$ where $N_A$ is Avogadros number ($6.022*10^{23}$)
	\subsection{Simplifying Assumptions of Gaseous Behavior}
	These are assumptions for an ideal gas that will make math easier:
	\begin{itemize}
	\item Distance between the gas molecules is much larger than the diameter of the molecules themselves, thus forces between molecules can be ignored.
	\item When gas molecules colide we will assume they are perfectly elastic collisions, thus $P$ can be easily derived.
	\item Due to the large distance between molecules, they are very compressible.
	\item The temperature of the gas is well above boiling point for given $P$ and V.
	\end{itemize}
	When a gas can be accurately described by these rules, the gas is considered an ideal gas. Most gasses experienced everyday are ideal gasses. The equation of state for an ideal gas is the ideal gas law:
	\begin{equation*}
	PV=NK_BT \to PV=n(N_AK_B)T \to PV=nRT
	\end{equation*}
	Boltons Constant $\to K_B=1.38*10^{-23}\frac{J}{K}$
	\newline Gas Constant $\to R=8.314\frac{J}{mol*K}$
	\subsection{Finding the Pressure of an Ideal Gas}
	Assuming a gas is within a rigid walled cube of length $l$, and when a gas molecule hits a wall it will change direction but conserve kinetic energy:
	\begin{align*}
	\Delta P \to \Delta P_x = -2mv_x
	\vec{F}=m\vec{a}
	\vec{F}=\frac{d}{dt}\vec{P} = \frac{\Delta \vec{P}}{\Delta t}
	\end{align*}
	Average time between colisions is $2L=v_x \Delta t$
	\begin{align*}
	\left|\frac{\Delta P_x}{\Delta t}\right| = \frac{2mv_x}{\frac{2L}{V_x}}=\frac{mv^2}{L}=F_x\\
	P_i=\frac{F}{A}=\frac{F_x}{L^2}=\frac{m_iv_{xi}^2}{L^3}
	\end{align*}
	Total Pressure: $P=\sum_{i}{P_i}=\frac{m}{L^3}(\sum_{i}V_x^2)$
	\begin{align*}
	\shortintertext{We know: } \vec{v}&=v_x\vec{i}+v_y\vec{j}+v_z\vec{k}\\
	v^2 &= v_x^2+v_y^2+v_z^2\\
	v^3 &= v_x^3+v_y^3+v_z^3
	\end{align*}
	Therefore we can determine that:
	\begin{align*}
	P &= \frac{m}{L^3}N(v_x^2)_{avg}\\
	P &= \frac{m}{3L^3}N(v^2)_{avg}\\
	PV &= \frac{m}{3}N(v^2)_{avg}
	\end{align*}
	Remember that for ideal gasses, $PV=NK_BT$, therefore  $K_BT=\frac{1}{3}m(v^2)_{avg}$
	\subsection{Root Mean Squared}
	Root mean squared is the average velocity for any given particle. It is defined by the equation $v_{rms}=\sqrt{v_{avg}^2}=\sqrt{\frac{3K_BT}{m}}$, where M is the mass of the object (particle) and T is the temperature of the particle.
	\subsection{Kinetic Average}
	\begin{equation*}
	K_{avg}=\frac{1}{2}m(v^2)_{avg}=\frac{3}{2}K_BT
	\end{equation*}
	\newline
	\begin{align*}
	E_{int}&=K_{tot}=\sum{i}K_i=NK_{avg_i}\\
	E_{int} &= \frac{3}{2}NK_BT\\
	\shortintertext{Or }E_{int}&=\frac{3}{2}nRT
	\end{align*}
	\subsection{Mean Free Path}
	The mean free path of a gas is the average distance traveled by a gas molecule between collisions. Considering a room of volume V, that is filled with gas with molecules with diameter d.
	\begin{align*}
	\lambda &= \frac{\text{Length of path in time}\Delta T}{\text{Number of collisions within}\Delta T}\\
	\lambda &= \frac{v \Delta T}{N\frac{V_{Cylinder}}{V}}\\
	\lambda &= \frac{V}{\sqrt{2}N\pi d^2}\\
	\text{Number Density } \eta &= \frac{N}{V}\\
	\lambda &=\frac{1}{\sqrt{2}\eta \pi d^2}\\
	PV &= nRT = NK_BT \text{, } P=\eta K_BT
	\end{align*}
	\subsubsection{Example 1}
	Find the RMS speed($v_{rms}$) of a $N_2$ molecule in a room at 20$\si{\degree}C$. DiNitrogen has a molecular mass of $14\frac{g}{mol}$, the R constant = $8.314\frac{J}{mol*K}$, M = molar mass and m = mass and T=293K.
	Recall that $M=mN_A$.
	\begin{align*}
	v_{rms}&=\sqrt{(v^2)_{avg}}\\
	&=\sqrt{\frac{3K_BT}{m}}\\
	&=\sqrt{\frac{3K_BT}{\frac{M}{N_A}}}\\
	&=\sqrt{\frac{3N_AK_BT}{M}}=\sqrt{\frac{3RT}{M}}\\
	v_{rms}&=\sqrt{\frac{3\left(8.314\frac{J}{mol*K}\right)(295K)}{0.028\frac{Kg}{mol}}}
	\end{align*}

	\subsection{Work}
	How much work will a gas do on its environment? Recall, $P=\frac{F}{A}$.
	\begin{align*}
	dW&=\vec{F}d\vec{x}\\
	&=Fdx-PAdx\\
	&=PdV
	\end{align*}
	\subsection{How The First law of Thermodynamics Relates to Work}
	\begin{align*}
	\Delta E_{int} = Q-W \to dE_{int} &=Q-PdV\\
	d\left(\frac{3}{2}NK_BT\right) &= Q-PdV\\
	\left(\frac{3}{2}NK_BT\right) &=Q-QdV
	\end{align*}
	\begin{itemize}
	\item Presure thermal equilibrium (quasi-static)
	\begin{align*}
		\int{dw}&=\int{PdV}\\
		W_{1\to2}&=\int_{v_1}^{v^2}{PdV}
	\end{align*}
	\item Isoconic case ($\Delta V = 0$)
	\begin{align*}
		w=0
	\end{align*}
	\item isoboric ($\Delta P=0$)
	\begin{align*}
		w_{1\to2}&=P\int_{v_1}^{v_2}{dV}\\
		&=P(V_2-V_1)
	\end{align*}
	\item isothermal ($\Delta T=0$)
	\begin{align*}
		w_{1\to2} &= \int_{V_1}^{V_2}{PdV}\\
		P=\frac{NK_BT}{V}\\
		w_{1\to2} &=\int_{V_1}^{V_2}{\frac{NK_BT}{V}dV}\\
		&=NK_BT\int_{V_1}^{V_2}\frac{dV}{V}\\
		w_{1\to2} &= NK_BTln \left( \frac{V_2}{V_1} \right)
	\end{align*}
	\end{itemize}
	\subsection{Pressure / Volume Diagrams}
	This is an easy way to interperate the changes due to temperature, pressure, and volume changes.
	\begin{itemize}
	\item Adiabatic $(Q=0)\to E_{int}=W$
	\item isothermal $(\Delta T = 0)$
	\begin{align*}
		\Delta E_{int} &= Q-W\\
		\frac{3}{2}NK_BdT&=Q-PdV \text{, } PdV=Q
	\end{align*}
	\item Isocloric $(\Delta V = 0) \to W=0 \to \Delta E_{int}=0$
	\item isoboric $(\Delta P = 0) \to W=P\Delta V \to \Delta E_{int}=Q-P\Delta V$
	\end{itemize}
	\subsubsection{Example 1}
	A sealed ideal gas is in a rigid container of $0.6m^3$ initially at room temperature $(T_1=20\si{\degree}C\to 293K)$ and pressure $(P_1=1atm\cong 10^5pa)$. If the temp ``doubles'' to $T_2=40\si{\degree}C\to313K$,
	\newline A.) what is the new pressure$(P_2)$?
	\begin{align*}
	\newline
	PV&=nRT\\
	\text{Or, }P_1V_1&=n_1RT_1 \text{, }P_2V_2=n_2RT_1\\
	\frac{P_1}{T_1}&=\frac{n_1R}{V_1} = const\\
	\frac{P_2}{T_2}&=\frac{n_1R}{V_2} = \frac{n_1R}{V_1}\\
	\text{therefore:}\\
	\frac{P_1}{T_1}&=\frac{P_2}{T_2}\\
	P_2&=P_1\frac{T_2}{T_1}\\
	&=(10^5pa)\frac{315K}{295K}=1.07*10^5pa
	\end{align*}
	B.) How much work did the gas do?
	\begin{align*}
	dW&=PdW\\
	w_{1\to2}&=\int_{V_1}^{V^2}{PdV}\\
	w_{1\to2}&=0
	\text{There was no change in volume}
	\end{align*}
	C.) How much heat was transfered (Not using $mc\Delta T$)?
	\begin{align*}
	\Delta E_{int}&=Q-W (W=\int{PdV}=0)\\
	\Delta E_{int}&=Q\\
	Q&=\frac{3}{2}nR(T_2-T_1)=\frac{3}{2}NK_B(T_2-T_1)\\
	&=\frac{3}{2}(P_2V_2-P_1V_1)\\
	Q&= \frac{3}{2}V_1(P_2,P_1)\\
	&=6,140J
	\end{align*}
	\subsubsection{Example 2}
	A sealed ideal gas undergoes an isobaric $(\Delta P=0)$ expansion during which it triples in volume. Then it is isothermically $(\Delta T=0)$ compressed to original volume, after which it cools to its original temperature. Find Q and W in terms of initial pressure and volume for each stage of cucle and the full cycle. Draw a PV diagram as well.
	\begin{align*}
	PV&=nRT\\
	P&=\frac{nRT}{V}\to T=\frac{PV}{nR}\\
	\Delta E_{int_{cyc}}&=0=Q_{cyc}-W{cyc}\\
	Q_{cyc}&=W_{cyc}\\
	\text{A.) Find the work from 1 to 2:}\\
	W_{1\to2}&=\int_{V_1}^{V_2}{PdV}\\
	&=\int_{V_1}^{3V_1}{dV}\\
	&=P_1(3V_1-V_1)\\
	w_{1\to2}&=2P_1V_1\\
	\text{B.) Find the work from 2 to 3:}\\
	W_{2\to3}&=\int_{v_2}^{v_3}PdV \to PV=nRT \to P=\frac{nRT}{V}\\
	&=\int_{V_2}^{V_1}{\frac{nRT}{V}dV}\\
	&=n_2RT_2\int_{V_2=3V}^{V_1}{\frac{dv}{V}}\\
	&=n_2RT_2ln\left(\frac{V_1}{3V_1}\right)=n_2RT_2ln\left(\frac{1}{3}\right)\\
	w_{2\to3}&=-n_2RT_2ln(3) \to PV=nRT\\
	&=-P_2V_2ln(3)\\
	w_{2\to3}&=-3ln(3)P_1V_1=-3.29P_1V_1\\
	\text{C.) Find the work from 3 to 1:}\\
	w_{3\to1} &= \int_{v_3}^{v_1}{PdV}=0\\
	w_{3\to1} &= 0\\
	\text{D.) Find the heat of the cycle}\\
	w_{cyc}&=w_{1\to2}+w_{2\to3}+w{2\to3}=P_1V_1(2-3ln3)\\
	&= -1.30P_1V_1\\
	\text{Since, } \Delta E_{int_cyc}=0, \text{ then } 0&=Q_{cyc}-W_{cyc}\\
	Q_{cyc}&=W_{cyc}=-1.30P_1V_1\\
	\text{And since }2\to3\text{ is an isotherm, we know } Q&=W\\
	Q_{2\to3}&=W_{2\to3}=-3ln(3)P_1V_1\\
	\text{For }1\to2 \text{: }\\
	\Delta E_int_{1\to2} &= Q_{1\to2}-W_{1\to2}\\
	Q_{1\to2}&=\frac{3}{2}nR(T_2-T_1)+2P_1V_1\\
	&=\frac{3}{2}n_2RT_2-\frac{3}{2}n_1RT_1+2P_1V_1\\
	&=\frac{3}{2}P_2V_2-\frac{3}{2}P_1V_1+2P_1V_1\\
	&=\frac{3}{2}P_13v_2-\frac{3}{2}P_1V_1+2P_1V_1\\
	&= \left(\frac{9}{2}-\frac{3}{2}+2\right)P_1V_1\\
	Q_{1\to2}&=5P_1V_1\\
	Q_{cyc}&=Q_{1\to2}+Q_{2\to3}+Q{3\to1}\\
	Q{3\to1}&=Q_{cyc}-Q_{1\to2}-Q_{2\to3}\\
	&=((2-3ln3)-5+3ln3)P_1V_1\\
	Q_{3\to1}&=-3P_1V_1
	\end{align*}

	\subsection{Using $C_V$ and $C_P$}
	While considering the first law, $dE_{int}=Q-PdV$, consider a constant volume: $dE_{int}=Q$, also: $E_{int}=\frac{3}{2}nRT$.
	\begin{align*}
	\frac{3}{2}nRdT&=Q\\
	nC_vdT &=Q\\
	\frac{3}{2}R=C_v \text{ if volume is constant}\\
	dE_{int}&=nC_vdT\\
	\end{align*}
	when $\Delta V=0$, then $Q=nC_V\Delta T$, but in all cases, $\Delta E_{int}= nC_V\Delta T$.
	\newline
	\begin{align*}
	\text{We have introduced } C_V&=\frac{3}{2}R\\
	\text{But recall: } (v)_{avg}^2&=V_{x_{avg}}^2+V_{y_{avg}}^2+V_{z_{avg}}^2\\
	\text{which gives us three degrees of translation, which is also why:}\\
	k_{avg}&=\frac{1}{2}m(v^2)_{avg}\\
	\text{this leads us to } E_{int}&=\frac{3}{2}nRT\\
	\end{align*}
	We can generalize $C_V=\frac{3}{2}R$ to be $C_V=\frac{d}{2}R$ for degrees if freedom. Other degrees of freedom come from both roation and vigration. Most monatomic atoms have 3 degrees of translation, and zero degrees of rotation and vibration. Diatomic atoms on the other hand have 3 degrees of translation, 2 degrees of rotation, and 1 degree of vibration.
	\newline
	\newline
	Consider the case when $(\Delta P=0)$. Let's preserve the form $Q=nC_PdT$
	\begin{align*}
	dE_{int}&=Q-Pdv\\
	\Delta E_{int}&=Q-P\Delta V\\
	nC_V\Delta T&=Q-P\Delta V\\
	nC_V\Delta T &= nC_P\Delta T-P\Delta V\\
	\text{Note that } PV=nRT\\
	dPV=d(nRT) \text{ therefore }P\Delta V=nR\Delta T\\
	nC_v\Delta T&=nC_P \Delta T-nR\Delta T\\
	C_V&=C_P-R\\
	C_P&=C_V+R\\
	\end{align*}
	Now for $(Q=0)$,
	\begin{align*}
	dE_{int}&=Q-PdV\\
	nC_VdT&=-PdV\\
	ndT&=-\frac{P}{C_V}dT\\
	\text{because }PV=nRT \text{, we can conclude } ndT&=\frac{V}{R}+\frac{P}{R}dV\\
	-\frac{P}{C_V}dT&=\frac{V}{R}dP+\frac{P}{R}dV\\
	\text{divide everything by PV: } -\frac{1}{C_V}\frac{D_V}{V} &=\frac{1}{R}\frac{dP}{P}+\frac{1}{R}\frac{dV}{V}\\
	\text{Multiply by }C_VR \text{ } -R\frac{dV}{V}&=C_V\frac{dP}{P}+C_V{dV}{V}\\
	0&=C_V\frac{dP}{P}+(C_V+R)\frac{dV}{V}\\
	&=C_V\frac{dP}{P}+C_P\frac{dV}{V}\\
	&=\int{\frac{dP}{P}}+\frac{C_P}{C_V}\int{\frac{dV}{V}}\\
	\text{const}&=ln\frac{P}{P_o}+\frac{C_P}{C_V}ln\frac{V}{V_o}\\
	PV^{\left(\frac{C_P}{C_V}\right)}&=\text{const}\\
	PV^{\gamma}&=\text{const for } \gamma=\frac{C_P}{C_V}\\
	\end{align*}
	\subsubsection{Example 1}
	2.2 moles of Ar gas are in a sealed metal container at room temp $(20\si{\degree}C=68\si{\degree}F)$. We then put the container on the sidewalk on a hot $(35\si{\degree}C=95\si{\degree}C)$ day. Let the gas come to temp $(95\si{\degree}F)$.
	\newline
	A.) Was any work done by or on the gas? There was little displacement, so almost 0 work done on the container.
	\newline
	B.) was any heat transferred to or from the gas? Heat was transfered to the gas.
	\newline
	C.) Find the gass' internal energy. We can always write:
	\newline
	\begin{align*}
	E_{int}&=\frac{3}{2}NK_BT=\frac{3}{2}nRT\\
	dE_{int}&=\frac{3}{2}K_B(TdV+NdT)\\
	dE_{int}&=\frac{3}{2}K_BdT\\
	\Delta E_{int}&=\frac{3}{2}NK_B\Delta T\\
	\Delta E_{int}&=nR\Delta T\\
	&=\frac{3}{2}(2.2mol)(8.314\frac{J}{molK})(35\si{\degree}C-20\si{\degree}C)\\
	&=411J\\
	\text{More generally } E_{int}=\frac{d}{2}nRT \text{Where d=degrees of translation}\\
	&=nC_VT \text{for } C_V=\frac{d}{2}R\\
	\end{align*}
	\newline
	Around room temperature for monotimic atoms, $C_V=\frac{3}{2}R$. For diatomic molecules it is $C_V=\frac{5}{2}R$.
	\subsubsection{Example 2}
	Solve the same problem where the only difference is the gas is a 2.2 moles of $H_2$. Given the temperatures, we know that $C_V=\frac{5}{2}R$.
	\begin{align*}
	\Delta E_{int}&=nC_V\Delta T\\
	&=n\frac{5}{2}R\Delta T\\
	&=\frac{5}{2}nR\Delta T\\
	&=\frac{5}{2}(2.2mol)(8.214\frac{J}{molK})(15K)\\
	&=687J
	\end{align*}
	\subsubsection{Example 3}
	Calculate work done by a gas during adiabatic expansion from $V_1$ to $V_2=4V_1$ in terms of $V_1$ and $V_2$. Remeber that adiabats result in no heat transfer $(Q=0)$ and $PV^\gamma{}=constant$, which means $\gamma{}=\frac{C_P}{C_V}=const$. Let $PV^\gamma{}=const$.
	\begin{align*}
	\text{Always: } w&=\int_{V_1}^{V^2}{PdV}\\
	&=\int_{V_1}{V_2}\frac{b}{V^\gamma}dv\\
	&=b\int_{V_1}^{V_2}{V^{-\gamma}}dv\\
	&=\frac{b}{-\gamma+1}V^{\gamma+1}|_{V_1}{V_2=4V_1}\\
	&=\frac{b}{1-\gamma}\left[(4V_1)^{1-\gamma}-V_1^{1-\gamma}\right]\\
	&=\frac{b}{1-\gamma}\left[4^{1-\gamma}V_1^{1-\gamma}-V_1^{1-\gamma}\right]\\
	&=\frac{b}{1-\gamma}V_1^{1-\gamma}\left[4^{1-\gamma}-1\right]\\
	&=\frac{4^{1-\gamma}-1}{1-\gamma}bV_1^{1-\gamma}\\
	&=\frac{4^{1-\gamma}-1}{1-\gamma}P_1V_1^{\gamma}V_1^{1-\gamma}\\
	W&=\frac{4^{1-\gamma}-1}{1-\gamma}P_1V_1
	\end{align*}
	Work done by a gas during an adiabatic expansion from $V_1to4V_1$. Let's find a more simplified answer for monatomic and diatomic gasses. The adiabatic ratio is $\gamma=\frac{C_P}{C_V}$, but it also means $C_P=C_V+R$. At room temperature, $C_V=\frac{3}{2}R$ for monotomic and $C_V=\frac{5}{2}R$.
	\begin{align*}
	\gamma_{mon}&=\frac{\frac{3}{2}R+R}{\frac{3}{2}R}=\frac{\frac{5}{2}R}{\frac{3}{2}R}=\frac{5}{3}\\
	\gamma_{dia}&=\frac{\frac{5}{2}R+R}{\frac{5}{2}R}=\frac{\frac{9}{2}R}{\frac{5}{2}R}=\frac{7}{5}\\
	\text{We found that } W&=\frac{4^{1-\gamma}-1}{1-\gamma}P_1V_1\\
	W_{dia}&=\frac{4^{1-\gamma}-1}{1-\gamma}P_1V_1 \text{ for } \gamma_{dia}=\frac{7}{5}\\
	w_{dia}&=1.06P_1V_1\\
	W_{mon}&=\frac{4^{\frac{3}{3}-\frac{5}{3}}-1}{\frac{3}{3}\frac{5}{3}}P_1V_1\\
	&=\frac{-4^{\frac{-2}{3}}-1}{\frac{-2}{3}}P_1V_1\\
	&=\frac{3}{2}\left(1-4^{\frac{-2}{3}}\right)P_1V_1\\
	&=.905P_1V_1\\
	\Delta{}E_{int}&=-1\\
	\end{align*}
	\subsection{Entropy}
	Reversible processes can have their processes reversed in time. When played inr evers, the behavior still looks physical (like a video). Any real system will have disipative forces and thus will not be perfectly reversible. Considering heat transfer, recording an ice cube melt and playing that video in reverse would look nonsensical. According to the second law of thermodynamics, heat always flows spontaneously from a hotter object to a colder object and vice versa. Irreversible process can happen within a closed system.
	\newline
	\newline
	For a reversible system, entropy is given by $dS=\frac{dQ}{T}$. For isothermal processes:
	\begin{align*}
	\int{dS}&=\int{\frac{dQ}{T}}\\
	\int{dS}&=\frac{1}{T}\int{dQ}\\
	\Delta S &= \frac{Q}{T}
	\end{align*}
	Imagine an ice cube melting at $0\si{\degree}C$. Then $Q=mL_f$,
	\begin{equation*}
	\Delta S =\frac{mLf}{T}
	\end{equation*}
	In general for a reversible process, we know:
	\begin{align*}
	dE_{int}&=dQ-dW\\
	dQ&=dE_{int}+dW\\
	\text{then entropy is: } ds&=\frac{dQ}{T}=\frac{dE_{int}+dW}{T}\\
	&=\frac{nC_VdT+PdV}{T}=nC_V\frac{dT}{T}+P\frac{dV}{T}\\
	\text{from the ideal gas law, } PV&=nRT \text{ or } \frac{P}{T}=\frac{nR}{V}\\
	\text{then, } \int{dS}&=\int{nC_V\frac{dT}{T}}+\int{\frac{nRdV}{V}}\\
	\Delta S_{0\to1}&=nC_Vln\frac{T_1}{T_2}+nRln\frac{V_1}{V_2}\\
	\text{For a reversible complete cycle:}\\
	\oint dS&=0
	\end{align*}
\newpage


\section{Homotopy groups of types}

\subsection{Pointed types}

\begin{defn}
\begin{enumerate}
\item A pointed type consists of a type $X$ equipped with a base point $x:X$. We will write $\UU_\ast$ for the type $\sm{X:\UU}X$ of all pointed types.
\item Let $(X,\ast_X)$ be a pointed type. A \define{pointed family} over $(X,\ast_X)$ consists of a type family $P:X\to \UU$ equipped with a base point $\ast_P:P(\ast_X)$.
\item Let $(P,\ast_P)$ be a pointed family over $(X,\ast_X)$. A \define{pointed section} of $(P,\ast_P)$ consists of a dependent function $f:\prd{x:X}P(x)$ and an identification $p:f(\ast_X)=\ast_P$. We define the \define{pointed $\Pi$-type} to be the type of pointed sections:
\begin{equation*}
\Pi^\ast_{(x:X)}P(x) \defeq \sm{f:\prd{x:X}P(x)}f(\ast_X)=\ast_P
\end{equation*}
In the case of two pointed types $X$ and $Y$, we may also view $Y$ as a pointed family over $X$. In this case we write $X\to_\ast Y$ for the type of pointed functions.
\item Given any two pointed sections $f$ and $g$ of a pointed family $P$ over $X$, we define the type of pointed homotopies
\begin{equation*}
f\htpy_\ast g \defeq \Pi^\ast_{(x:X)} f(x)=g(x),
\end{equation*}
where the family $x\mapsto f(x)=g(x)$ is equipped with the base point $\ct{p}{q^{-1}}$.
\end{enumerate}
\end{defn}

\begin{eg}
The circle $\sphere{1}$ is a pointed type with base point $\base:\sphere{1}$.
\end{eg}

\begin{eg}
If $X$ is a pointed type, then in the suspension of $X$ we have the canonical identification $\merid(\ast_X):\north=\south$. Therefore we do not have to worry about whether to choose $\north$ or $\south$ as the base point of $\susp{X}$.
\end{eg}

\begin{rmk}
Since pointed homotopies are defined as certain pointed sections, we can use the same definition of pointed homotopies again to consider pointed homotopies between pointed homotopies, and so on.
\end{rmk}

\begin{defn}
\begin{enumerate}
\item For any pointed type $X$, we define the \define{pointed identity function} $\mathsf{id}^\ast_X\defeq (\idfunc[X],\refl{\ast})$.
\item For any two pointed maps $f:X\to_\ast Y$ and $g:Y\to_\ast Z$, we define the \define{pointed composite}
\begin{equation*}
g\mathbin{\circ_\ast} f \defeq (g\circ f,\ct{\ap{g}{p_f}}{p_g}).
\end{equation*}
\end{enumerate}
\end{defn}

\subsection{Loop spaces}
\begin{defn}
Let $X$ be a pointed type with base point $x$. We define the \define{loop space} $\loopspace{X,x}$ of $X$ at $x$ to be the pointed type $x=x$ with base point $\refl{x}$.
\end{defn}

\begin{defn}
The loop space operation $\loopspacesym$ is \emph{functorial} in the sense that
\begin{enumerate}
\item For every pointed map $f:X\to_\ast Y$ there is a pointed map
\begin{equation*}
\loopspace{f}:\loopspace{X}\to_\ast \loopspace{Y},
\end{equation*}
defined by $\loopspace{f}(\omega)\defeq \ct{p_f}{\ap{f}{\omega}}{p_f^{-1}}$, which is base point preserving by $\mathsf{right\usc{}inv}(p_f)$.
\item For every pointed type $X$ there is a pointed homotopy
\begin{equation*}
\loopspace{\mathsf{id}_X^\ast}\htpy_\ast \mathsf{id}^\ast_{\loopspace{X}}.
\end{equation*}
\item For any two pointed maps $f:X\to_\ast Y$ and $g:Y\to_\ast X$, there is a pointed homotopy witnessing that the triangle
\begin{equation*}
\begin{tikzcd}
& \loopspace{Y} \arrow[dr,"\loopspace{g}"] \\
\loopspace{X} \arrow[rr,swap,"\loopspace{g\circ_\ast f}"] \arrow[ur,"\loopspace{f}"] & & \loopspace{Z}
\end{tikzcd}
\end{equation*}
of pointed types commutes.
\end{enumerate}
\end{defn}

\begin{thm}
Consider two pointed types $(X,x_0)$ and $(Y,y_0)$. Then there is an equivalence
\begin{equation*}
\eqv{(\susp X \to_\ast Y)}{(X \to_\ast \loopspace Y)}
\end{equation*}
\end{thm}

\begin{proof}
Computing with the universal property of the suspension
\begin{align*}
\susp X \to_\ast Y & \eqvsym \sm{y,y':Y} (X \to (y=y'))\times (y'=y_0) \\
& \eqvsym \sm{y:Y} X\to (y=y_0) \\
& \eqvsym \sm{f:X\to (y=y_0)}f(x_0)=\refl{y_0}.
\end{align*}
In the last equivalence we used \cref{ex:coh_intro}.
\end{proof}

\subsection{Homotopy groups}
In homotopy type theory we use $0$-types to define groups.
\begin{defn}
A \define{group} $\mathcal{G}$ consists of a set $G$ with a unit $e:G$, a multiplication $x,y\mapsto x\cdot y$, and an inverse operation $x\mapsto x^{-1}$ satisfying the \define{group laws}:
\begin{align*}
(x\cdot y)\cdot z & =x\cdot(y\cdot z) & x^{-1}\cdot x & = e \\
e\cdot x & = x & x\cdot x^{-1} & = e. \\
x\cdot e & =x
\end{align*}
\end{defn}

\begin{defn}
For $n\geq 1$, the \define{$n$-th homotopy group} of a type $X$ at a base point $x:X$ consists of the type
\begin{equation*}
|\pi_n(X,x)| \defeq \trunc{0}{\loopspace[n]{X,x}}
\end{equation*}
equipped with the group operations inherited from the path operations on $\loopspace[n]{X,x}$.
Often we will simply write $\pi_n(X)$ when it is clear from the context what the base point of $X$ is.

For $n\jdeq 0$ we define $\pi_0(X,x)\defeq \trunc{0}{X}$.
\end{defn}

\begin{eg}
In \cref{cor:circle_loopspace} we established that $\eqv{\loopspace{\sphere{1}}}{\Z}$. It follows that
\begin{equation*}
\pi_1(\sphere{1})=\Z \qquad\text{ and }\qquad\pi_n(\sphere{1})=0\qquad\text{for $n\geq 2$.}
\end{equation*}
Furthermore, we have seen in \cref{circle_conn} that $\trunc{0}{\sphere{1}}$ is contractible.
Therefore we also have $\pi_0(\sphere{1})=0$.
\end{eg}

\subsection{The Eckmann-Hilton argument}

Given a diagram of identifications
\begin{equation*}
\begin{tikzcd}[column sep=7em]
x \arrow[r,equals,bend left=60,"p",""{name=A,below}] \arrow[r,equals,""{name=B},""{name=E,below},"{p'}"{near end}] \arrow[r,equals,bend right=60,"{p''}"{below},""{name=F,above}] \arrow[from=A,to=B,phantom,"r\Downarrow"] \arrow[from=E,to=F,phantom,"{r'\Downarrow}"]
& y
\end{tikzcd}
\end{equation*}
in a type $A$, where $r:p=p'$ and $r':p'=p''$,
we obtain by concatenation an identification $\ct{r}{r'}:p=p''$. This operation on identifications of identifications is sometimes called the \define{vertical concatenation}, because there is also a \emph{horizontal} concatenation operation.

\begin{defn}
Consider identifications of identifications $r:p=p'$ and $s:q=q'$, where $p,p':x=y$, and $q,q':y=z$ are identifications in a type $A$, as indicated in the diagram
\begin{equation*}
\begin{tikzcd}[column sep=huge]
x \arrow[r,equals,bend left=30,"p",""{name=A,below}] \arrow[r,equals,bend right=30,""{name=B,above},"{p'}"{below}] \arrow[from=A,to=B,phantom,"r\Downarrow"] & y \arrow[r,equals,bend left=30,"q",""{name=C,below}] \arrow[r,equals,bend right=30,""{name=D,above},"{q'}"{below}] \arrow[from=C,to=D,phantom,"s\Downarrow"] & z.
\end{tikzcd}
\end{equation*}
We define the \define{horizontal concatenation} $\ct[h]{r}{s}:\ct{p}{q}=\ct{p'}{q'}$ of $r$ and $s$.
\end{defn}

\begin{proof}
First we induct on $r$, so it suffices to define $\ct[h]{\refl{p}}{s}:\ct{p}{q}=\ct{p}{q'}$.
Next, we induct on $p$, so it suffices to define $\ct[h]{\refl{\refl{y}}}{s}:\ct{\refl{y}}{q}=\ct{\refl{y}}{q'}$.
Since $\ct{\refl{y}}{q}\jdeq q$ and $\ct{\refl{y}}{q'}\jdeq q'$, we take $\ct[h]{\refl{\refl{y}}}{s}\defeq s$.
\end{proof}

\begin{lem}
Horizontal concatenation satisfies the left and right unit laws.
\end{lem}

In the following lemma we establish the \define{interchange law} for horizontal and vertical concatenation.

\begin{lem}
Consider a diagram of the form
\begin{equation*}
\begin{tikzcd}[column sep=7em]
x \arrow[r,equals,bend left=60,"p",""{name=A,below}] \arrow[r,equals,""{name=B},""{name=E,below}] \arrow[r,equals,bend right=60,"{p''}"{below},""{name=F,above}] \arrow[from=A,to=B,phantom,"r\Downarrow"] \arrow[from=E,to=F,phantom,"{r'\Downarrow}"]
& y \arrow[r,equals,bend left=60,"q",""{name=C,below}] \arrow[r,equals,""{name=G,above},""{name=H,below}] \arrow[r,equals,bend right=60,""{name=D,above},"{q''}"{below}] \arrow[from=C,to=G,phantom,"s\Downarrow"] \arrow[from=H,to=D,phantom,"{s'\Downarrow}"] & z.
\end{tikzcd}
\end{equation*}
Then there is an identification
\begin{equation*}
\ct[h]{(\ct{r}{r'})}{(\ct{s}{s'})}=\ct{(\ct[h]{r}{s})}{(\ct[h]{r'}{s'})}.
\end{equation*}
\end{lem}

\begin{proof}
We use path induction on both $r$ and $r'$, followed by path induction on $p$. Then it suffices to show that
\begin{equation*}
\ct[h]{(\ct{\refl{\refl{y}}}{\refl{\refl{y}}})}{(\ct{s}{s'})}=\ct{(\ct[h]{\refl{\refl{y}}}{s})}{(\ct[h]{\refl{\refl{y}}}{s'})}.
\end{equation*}
Using the computation rules, we see that this reduces to
\begin{equation*}
\ct{s}{s'}=\ct{s}{s'},
\end{equation*}
which we have by reflexivity.
\end{proof}

\begin{thm}
For $n\geq 2$, the $n$-th homotopy group is abelian.
\end{thm}

\begin{proof}
Our goal is to show that
\begin{equation*}
\prd{r,s:\pi_2(X)} r\cdot s=s\cdot r.
\end{equation*}
Since we are constructing an identification in a set, we can use the universal property of $0$-truncation on both $r$ and $s$. Therefore it suffices to show that
\begin{equation*}
\prd{r,s:\refl{x_0}=\refl{x_0}} \tproj{0}r\cdot \tproj{0}s=\tproj{0}s\cdot \tproj{0}r.
\end{equation*}
Now we use that $\tproj{0}{r}\cdot\tproj{0}{s}\jdeq \tproj{0}{\ct{r}{s}}$ and $\tproj{0}{s}\cdot\tproj{0}{r}\jdeq \tproj{0}{\ct{s}{r}}$, to see that it suffices to show that $\ct{r}{s}=\ct{s}{r}$, for every $r,s:\refl{x}=\refl{x}$. Using the unit laws and the interchange law, this is a simple computation:
\begin{align*}
\ct{r}{s} & = \ct{(\ct[h]{r}{\refl{x}})}{(\ct[h]{\refl{x}}{s})} \\
& = \ct[h]{(\ct{r}{\refl{x}})}{(\ct{\refl{x}}{s})} \\
& = \ct[h]{(\ct{\refl{x}}{r})}{(\ct{s}{\refl{x}})} \\
& = \ct{(\ct[h]{\refl{x}}{s})}{(\ct[h]{r}{\refl{x}})} \\
& = \ct{s}{r}.\qedhere
\end{align*}
\end{proof}

\subsection{Simply connectedness of the $2$-sphere}

\begin{defn}
A pointed type $X$ is said to be \define{$n$-connected} if its homotopy groups $\pi_i(X)$ are trivial for $i\leq n$. A $0$-connected type is also just called \define{connected}, and a $1$-connected type is also called \define{simply connected}.
\end{defn}

We write $\ast$ for the base point of the sphere $\sphere{n}$.

\begin{thm}
For any $n:\N$ and any family $P$ of $n$-types over the $(n+2)$-sphere $\sphere{n+2}$, the function
\begin{equation*}
\Big(\prd{x:\sphere{n+2}}P(x)\Big)\to P(\ast)
\end{equation*}
given by $f\mapsto f(\ast)$, is an equivalence.
\end{thm}

\begin{cor}
The $2$-sphere is simply connected.
\end{cor}

\begin{proof}
Our goal is to show that $\pi_1(\sphere{2})$ is contractible. In other words, we have to show that $\trunc{0}{\loopspace{\sphere{2}}}$ is contractible. We do this by constructing a term of type
\begin{equation*}
\prd{t:\sphere{2}}\iscontr(\trunc{0}{\ast=t}).
\end{equation*}
First we note that
\begin{equation*}
\prd{t:\sphere{2}}\trunc{0}{\ast=t}
\end{equation*}
is equivalent to the type $\trunc{0}{\ast=\ast}$, of which we have the term $\tproj{0}{\refl{\ast}}$. Thus we obtain a dependent function $\alpha:\prd{t:\sphere{2}}\trunc{0}{\ast=t}$ equipped with $\alpha(\ast)=\tproj{0}{\refl{\ast}}$. Now we proceed to show that
\begin{equation*}
\prd{t:\sphere{2}}{p:\trunc{0}{\ast=t}} \alpha(t)=p
\end{equation*}
by the dependent universal property of $0$-truncation. Therefore it suffices to construct a term of type
\begin{equation*}
\prd{t:\sphere{2}}{p:\ast=t}\alpha(t)=\tproj{0}{p}.
\end{equation*}
This is immediate by path induction and the fact that $\alpha(\ast)=\tproj{0}{\refl{\ast}}$.
\end{proof}

\begin{exercises}
\item Show that the type of pointed families over a pointed type $(X,x)$ is equivalent to the type
\begin{equation*}
\sm{Y:\UU_\ast} Y\to_\ast X.
\end{equation*}
\item Given two pointed types $A$ and $X$, we say that $A$ is a (pointed) retract of $X$ if we have $i:A\to_\ast X$, a retraction $r:X\to_\ast A$, and a pointed homotopy $H:r\circ_\ast i\htpy_\ast \idfunc^\ast$.
\begin{subexenum}
\item Show that if $A$ is a pointed retract of $X$, then $\loopspace{A}$ is a pointed retract of $\loopspace{X}$.
\item Show that if $A$ is a pointed retract of $X$ and $\pi_n(X)$ is a trivial group, then $\pi_n(A)$ is a trivial group.
\end{subexenum}
\item Construct by path induction a family of maps
\begin{equation*}
\prd{A,B:\UU}{a:A}{b:B} (\id{\pairr{A,a}}{\pairr{B,b}})\to \sm{e:\eqv{A}{B}}e(a)=b,
\end{equation*}
and show that this map is an equivalence. In other words, an \emph{identification of pointed types} is a base point preserving equivalence.
\item Let $\pairr{A,a}$ and $\pairr{B,b}$ be two pointed types. Construct by path induction a family of maps
\begin{equation*}
\prd{f,g:A\to B}{p:f(a)=b}{q:g(a)=b} (\id{\pairr{f,p}}{\pairr{g,q}})\to \sm{H:f\htpy g} p = \ct{H(a)}{q},
\end{equation*}
and show that this map is an equivalence. In other words, an \emph{identification of pointed maps} is a base point preserving homotopy.
\item Show that if $A\leftarrow S\rightarrow B$ is a span of pointed types, then for any pointed type $X$ the square
\begin{equation*}
\begin{tikzcd}
(A\sqcup^S B \to_\ast X) \arrow[r] \arrow[d] & (B \to_\ast X) \arrow[d] \\
(A\to_\ast X) \arrow[r] & (S\to_\ast X)
\end{tikzcd}
\end{equation*}
is a pullback square.
\item \label{ex:yoneda_ptd_types}Let $f:A\to_\ast B$ be a pointed map. Show that the following are equivalent:
\begin{enumerate}
\item $f$ is an equivalence.
\item For any pointed type $X$, the precomposition map
\begin{equation*}
\blank\mathbin{\circ_\ast}f:(B\to_\ast X)\to_\ast (A\to_\ast X)
\end{equation*}
is an equivalence.
\end{enumerate}
\item In this exercise we prove the suspension-loopspace adjunction.
\begin{subexenum}
\item Construct a pointed equivalence
\begin{equation*}
\tau_{X,Y}:(\susp(X)\to_\ast Y) \eqvsym_\ast (X\to \loopspace{Y})
\end{equation*}
for any two pointed spaces $X$ and $Y$.
\item Show that for any $f:X\to_\ast X'$ and $g:Y'\to_\ast Y$, there is a pointed homotopy witnessing that the square
\begin{equation*}
\begin{tikzcd}[column sep=large]
(\susp(X')\to_\ast Y') \arrow[r,"\tau_{X',Y'}"] \arrow[d,swap,"h\mapsto g\circ h\circ \susp(f)"] & (X'\to_\ast \loopspace{Y'}) \arrow[d,"h\mapsto\loopspace{g}\circ h\circ f"] \\
(\susp(X)\to_\ast Y) \arrow[r,swap,"\tau_{X,Y}"] & (X\to_\ast \loopspace{Y})
\end{tikzcd}
\end{equation*}
\end{subexenum}
\item Show that if
\begin{equation*}
\begin{tikzcd}
C \arrow[r] \arrow[d] & B \arrow[d] \\
A \arrow[r] & X
\end{tikzcd}
\end{equation*}
is a pullback square of pointed types, then so is
\begin{equation*}
\begin{tikzcd}
\loopspace{C} \arrow[r] \arrow[d] & \loopspace{B} \arrow[d] \\
\loopspace{A} \arrow[r] & \loopspace{X}.
\end{tikzcd}
\end{equation*}
\item
\begin{subexenum}
\item Show that if $X$ is $k$-truncated, then its $n$-th homotopy group $\pi_n(X)$ is trivial for each choice of base point, and each $n> k$.
\item Show that if $X$ is $(k+l)$-truncated, and for each $0< i\leq l$ the $(k+i)$-th homotopy groups $\pi_{k+i}(X)$ are trivial for each choice of base point, then $X$ is $k$-truncated.
\end{subexenum}
It is consistent to assume that there are types for which all homotopy groups are trivial, but which aren't contractible nonetheless. Such types are called \define{$\infty$-connected}.
\end{exercises}