\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{chtt-notes}[2018/02/02 CHTT Course Notes]

\PassOptionsToPackage{utf8}{inputenc}
\PassOptionsToPackage{centercolon}{mathtools}

\RequirePackage{amsmath}
\RequirePackage{amssymb}
\RequirePackage{amsthm}
\RequirePackage{fancyhdr}
\RequirePackage{inputenc}
\RequirePackage{mathpartir}
\RequirePackage{mathtools}
\RequirePackage{microtype}
\RequirePackage{natbib}
\RequirePackage{proof}
\RequirePackage{todonotes}
\RequirePackage{url}
\RequirePackage{xcolor}
\RequirePackage{xparse}


% Theorems
\newtheorem{theorem}{Theorem}
\newtheorem*{theorem*}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem*{corollary*}{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem*{lemma*}{Lemma}
\newtheorem{remark}[theorem]{Remark}
\newtheorem*{remark*}{Remark}
\newtheorem{claim}[theorem]{Attempt}
\newtheorem*{claim*}{Attempt}
\newtheorem{definition}[theorem]{Definition}
\newtheorem*{definition*}{Definition}
\newtheorem{exercise}[theorem]{Exercise}
\newtheorem*{exercise*}{Exercise}
\newtheorem{example}[theorem]{Example}
\newtheorem*{example*}{Example}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem*{proposition*}{Proposition}
\newtheorem{axiom}[theorem]{Axiom}
\newtheorem*{axiom*}{Axiom}
\newtheorem{fact}[theorem]{Fact}
\newtheorem*{fact*}{Fact}

% \newtheorem{theorem}{Theorem}
% \newtheorem{corollary}[theorem]{Corollary}
% \newtheorem{lemma}[theorem]{Lemma}
% \newtheorem{remark}[theorem]{Remark}
% \newtheorem{claim}[theorem]{Attempt}
% \newtheorem{definition}[theorem]{Definition}
% \newtheorem{exercise}[theorem]{Exercise}
% \newtheorem*{ulemma}{Lemma}

\newenvironment{proofattempt}{\paragraph{\emph{Proof Attempt}.}}{\hfill\color{red}{\textsf{X}}\par}
\newenvironment{prfattemptref}[1]{\paragraph{\emph{Proof Attempt of #1}.}}{\hfill\color{red}{X}\par}

\newcommand{\redprl}{{\color{red}\textbf{Red}}\textbf{PRL}}

\newcommand{\eqdef}{:=}

% Exact equality
\newcommand{\eeq}{\ensuremath{\doteq}}

\NewDocumentCommand{\hasTF}{m g}{\ensuremath{\IfValueTF{#2}{#1 \vdash #2}{#1}\;\mathsf{type}}}
\NewDocumentCommand{\hasEF}{m m g}{\ensuremath{\IfValueTF{#3}{#1 \vdash #2 : #3}{#1 : #2}}}
\NewDocumentCommand{\hasTEF}{m m g}{\ensuremath{\IfValueTF{#3}{#1 \vdash #2 \equiv #3}{#1 \equiv #2}\;\mathsf{type}}}
\NewDocumentCommand{\hasEEF}{m m m g}{\ensuremath{\IfValueTF{#4}{#1 \vdash #2 \equiv #3 : #4}{#1 \equiv #2 : #3}}}
\NewDocumentCommand{\trueF}{m g}{\ensuremath{\IfValueTF{#2}{#1 \vdash #2}{#1}\;\mathsf{true}}}

\NewDocumentCommand{\hasTC}{m g}{\ensuremath{\IfValueTF{#2}{#1 \gg #2}{#1}\;\mathsf{type}}}
\NewDocumentCommand{\hasEC}{m m g}{\ensuremath{\IfValueTF{#3}{#1 \gg #2 \in #3}{#1 \in #2}}}
\NewDocumentCommand{\hasTEC}{m m g}{\ensuremath{\IfValueTF{#3}{#1 \gg #2 \eeq #3}{#1 \eeq #2}\;\mathsf{type}}}
\NewDocumentCommand{\hasEEC}{m m m g}{\ensuremath{\IfValueTF{#4}{#1 \gg #2 \eeq #3 \in #4}{#1 \eeq #2 \in #3}}}

\newcommand{\hasC}[5]{\ensuremath{#1 : (#2 \rhd #3) \leadsto (#4 \rhd #5)}}

\newcommand{\htermop}{\mathsf{HT}}
\newcommand{\hterm}[2]{\ensuremath{\htermop_{#1}(#2)}}
\newcommand{\htermp}[2]{\ensuremath{\htermop^+_{#1}(#2)}}
\newcommand{\bterm}[1]{\ensuremath{{#1} \; \mathsf{term}_{\beta}}}
\newcommand{\hv}[2]{\ensuremath{\mathsf{HV}_{#1}(#2)}}
\newcommand{\hvcbn}[2]{\ensuremath{\mathsf{HV}^{\mathsf{cbn}}_{#1}(#2)}}
\newcommand{\hvcbv}[2]{\ensuremath{\mathsf{HV}^{\mathsf{cbv}}_{#1}(#2)}}

% system F

\newcommand{\htermFF}[5]{\ensuremath{\mathsf{HT}_{#1}(#2)[#3;#4 : #5]}}
\newcommand{\htermF}[4]{\ensuremath{\mathsf{HT}_{#1}(#2)[#3;#4]}}
\newcommand{\erase}[1]{\ensuremath{|#1|}}
\newcommand{\ans}{\ensuremath{\mathsf{ans}}}

\newcommand{\contrb}[2]{#1 \mathrel{\text{contr}_\beta} #2}
\newcommand{\bnf}[1]{#1 \mathrel{\text{nf}_\beta}}
\newcommand{\bnorm}[1]{\ensuremath{#1 \mathrel{\text{norm}_\beta}}}
\newcommand{\stepb}[2]{\ensuremath{#1 \mapsto_{\beta} #2}}
\newcommand{\stepbs}[2]{\ensuremath{#1 \mapsto_{\beta}^* #2}}
\newcommand{\stepbk}[3]{\ensuremath{#1 \mapsto_{\beta}^{(#2)} #3}}

\newcommand{\hnorm}[3]{\ensuremath{\mathsf{HN}^{#1}_{#2}(#3)}}
\newcommand{\id}[1]{\ensuremath{\mathsf{id}(#1)}}

\newcommand{\AND}{\mathrel{\wedge}}
\newcommand{\OR}{\mathrel{\vee}}

\newcommand{\step}[2]{\ensuremath{#1 \mapsto #2}}
\newcommand{\steps}[2]{\ensuremath{#1 \mapsto^* #2}}
\newcommand{\eval}[2]{\ensuremath{#1 \mathrel{\Downarrow} #2}}
\newcommand{\valueJ}[1]{\ensuremath{#1\;\mathsf{value}}}
\newcommand{\fillin}[2]{\ensuremath{#1\{#2\}}}

\newcommand{\fn}[2]{\ensuremath{#1 \to #2}}
\newcommand{\all}[2]{\ensuremath{\forall #1. #2}}
\newcommand{\nat}{\ensuremath{\mathsf{nat}}}
\newcommand{\void}{\ensuremath{\mathsf{void}}}
\newcommand{\bool}{\ensuremath{\mathsf{bool}}}
\newcommand{\vtrue}{\ensuremath{\mathsf{true}}}
\newcommand{\vfalse}{\ensuremath{\mathsf{false}}}
\newcommand{\tp}{\ensuremath{\mathsf{T}}}
\newcommand{\ap}[2]{\ensuremath{#1\,#2}}
\newcommand{\app}[2]{\ensuremath{#1(#2)}}
\newcommand{\tap}[2]{\ensuremath{#1\,[#2]}}
\newcommand{\lam}[3]{\ensuremath{\lambda #1 {:} #2.\, #3}}
\newcommand{\alam}[1]{\ensuremath{\lambda \_.\, #1}}
\newcommand{\tlam}[2]{\ensuremath{\Lambda #1.\, #2}}
\newcommand{\inl}[1]{\ensuremath{1\cdot#1}}
\newcommand{\inr}[1]{\ensuremath{2\cdot#1}}
\newcommand{\outl}[1]{\ensuremath{#1 \cdot 1}}
\newcommand{\outr}[1]{\ensuremath{#1 \cdot 2}}
\newcommand{\scasev}[6]{\ensuremath{\mathsf{case}_{#1}\,#2\,\{\inl{#3} \hookrightarrow #4 \mid \inr{#5} \hookrightarrow #6\}}}
\newcommand{\scase}[5]{\ensuremath{\mathsf{case}_{#1}\,#2\,\{\inl{#3} \hookrightarrow #4 \mid \inr{#3} \hookrightarrow #5\}}}
\newcommand{\vcase}[2]{\ensuremath{\mathsf{case}_{#1}\,#2\,\{\,\}}}

\newcommand{\emptypair}{\ensuremath{\langle \rangle}}
\newcommand{\exprpair}[2]{\ensuremath{\langle #1, #2\rangle}}
\newcommand{\pairtype}[2]{\ensuremath{#1 \times #2}}
\newcommand{\booltype}{\text{bool}}
\newcommand{\unittype}{1}

\newcommand{\true}{\vtrue}
\newcommand{\false}{\vfalse}
\newcommand{\ifexpr}[3]{\ensuremath{\mathrm{if}(#1; #2; #3)}}
\newcommand{\ifexprtyp}[4]{\ensuremath{\mathrm{if}_{#1}(#2; #3; #4)}}
\newcommand{\letbein}[3]{\ensuremath{\mathrm{let}\,\langle#1\rangle\,\mathrm{be}\,#2\,\mathrm{in}\,#3}}

\newcommand{\subst}[3]{[#1/#2]#3}
\newcommand{\update}[3]{[#1 \mid #2 \mapsto #3]}

% System T
\newcommand{\natz}{\ensuremath{\mathsf{z}}}
\newcommand{\natsop}{\mathsf{s}}
\newcommand{\nats}[1]{\natsop(#1)}
\newcommand{\natrecop}{\mathsf{rec}}
\newcommand{\natrec}[5]{\ensuremath{\natrecop_{#1}\{#2; #3.#4\}(#5)}}

% Dependent type theory
\newcommand{\Pitype}[3]{({#1}:{#2}) \to {#3}}
\newcommand{\Sigmatype}[3]{({#1}:{#2}) \times {#3}}
\newcommand{\Id}[3]{\mathsf{Id}_{#1}(#2,#3)}
\newcommand{\Ideq}[3]{{#2} =_{#1} {#3}}
\newcommand{\refl}[2]{\mathsf{refl}_{#1}(#2)}
\newcommand{\erefl}{\underline{\mathsf{refl}}}
\newcommand{\Idelim}[3]{\mathsf{J}_{#1}(#2)(#3)}
\newcommand{\transport}[1]{\mathsf{transport}[{#1}]}

\newcommand{\funext}[5]{\mathsf{funext}_{#1,#2}(#3;#4;#5)}

\newcommand{\Eq}[3]{\mathsf{Eq}_{#1}(#2,#3)}

\DeclareMathOperator{\dom}{dom}

\newcommand{\E}{\mathcal{E}}

% Homotopy type theory
\newcommand{\hap}[2]{\mathsf{ap}_{#1}(#2)}
\newcommand{\hapd}[2]{\mathsf{apd}_{#1}(#2)}
\newcommand{\DIdeq}[4]{{#3} =_{#2}^{#1} {#4}}
\newcommand{\hpitype}[3]{\Pi{#1}:{#2}.\;{#3}}
\newcommand{\hsigtype}[3]{\Sigma{#1}:{#2}.\;{#3}}

% Rule Name
\newcommand{\rn}[1]{\textsc{(#1)}}

% Page layout
\RequirePackage[paper=letterpaper,top=1in,bottom=1in,left=1.5in,right=1.5in,]{geometry}
\setlength{\parindent}{0mm}
\setlength{\parskip}{1.5mm}

% titles
\RequirePackage{titlesec}
\titleformat*{\section}{\large\bfseries}
\titleformat*{\subsection}{\normalsize\bfseries}
\titleformat*{\subsubsection}{\bfseries}
\titleformat*{\paragraph}{\bfseries}

% package to customize three basic list environments: enumerate, itemize and description.
\RequirePackage{enumitem}
%%% Let's not do the following indiscriminately, because it gives you overlapping
%%% subscripts and superscripts when one item has a subscript and the next item
%%% has a superscript.
%%% Besides, spacing makes it easier to read text.
%%% If you really want the following items, add them to the individual instance
%%% of itemize/enumerate/description. —RAK, 2018-02-02
% \setitemize{noitemsep, topsep=0pt, leftmargin=*}
%\setenumerate{noitemsep, topsep=0pt, leftmargin=*}
%\setdescription{noitemsep, topsep=0pt, leftmargin=*}

% title page, modified from article.cls
\makeatletter
\global\let\@week\@empty
\global\let\@scribes\@empty
\def\week#1{\def\@week{#1}}
\def\scribes#1{\def\@scribes{#1}}
\fancyhead[RO,L]{\scshape 15-819: Computational Higher Type Theory}
\fancyhead[LO,R]{\scshape Lecture Notes Week \@week}
\def\@maketitle{%
  \newpage
  \null
  \vskip 2em%
  \begin{center}%
    {\Large \scshape Computational Higher Type Theory (CHTT) \par}%
    \vskip 4pt%
    {\large \scshape Robert Harper \par}%
    \vskip 4pt%
    {\large\bfseries Lecture Notes of Week \@week\space by \@scribes}
  \end{center}%
  \par
  \pagestyle{fancy}
  \vskip 1.5em}
\makeatother
\pagestyle{fancy}

% Courtesy of
% https://www.sharelatex.com/learn/Kb/Cross_referencing_with_the_xr_package_in_ShareLaTeX
\makeatletter
\newcommand*{\addFileDependency}[1]{% argument=file name and extension
  \typeout{(#1)}
  \@addtofilelist{#1}
  \IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother

\newcommand*{\myexternaldocument}[2]{%
  \externaldocument[#2:]{#1}%
  \addFileDependency{#1.tex}%
  \addFileDependency{#1.aux}%
  \message{Added external document #1 with prefix ``#2:''.}
}

\makeatletter
\newcount\weekIncludeXR
\weekIncludeXR=1
\def\doXRs{\loop
  \myexternaldocument{week\the\weekIncludeXR}{W\the\weekIncludeXR}
  \advance\weekIncludeXR by +1
  \ifnum \weekIncludeXR<\@week
  \repeat}

\makeatother

% Reference management (didn't work at the top)

\RequirePackage{xr-hyper}
\RequirePackage{hyperref}
% cleveref is really picky and really really wants to be loaded *last*
\RequirePackage{cleveref}
