\documentclass{report}

\usepackage[a4paper, margin=2cm]{geometry}
\usepackage{textgreek}
\usepackage{mathpartir}
\usepackage{mathtools}
\usepackage{amssymb}

\allowdisplaybreaks[1]
\newtagform{nowidth}{\llap\bgroup(}{)\egroup}

\newcommand{\theLang}{myml}
\newcommand{\code}{\mathtt}
\newcommand{\backtick}{{}^{\backprime}}
\newcommand{\ruleTag}[1]{\label{#1}\tag{\textsc{#1}}}
\DeclareMathOperator{\domain}{dom}
\DeclareMathOperator{\union}{union}
\DeclareMathOperator{\find}{find}
\DeclareMathOperator{\describeContextScheme}{descCtxScm}
\DeclareMathOperator{\describeScheme}{descScm}
\DeclareMathOperator{\describeType}{descType}
\DeclareMathOperator{\describeRow}{descRow}
\DeclareMathOperator{\describePresence}{descPre}
\DeclareMathOperator{\describePresenceWithType}{descPT}
\DeclareMathOperator{\instantiate}{inst}
\DeclareMathOperator{\instantiateType}{instType}
\DeclareMathOperator{\instantiateRow}{instRow}
\DeclareMathOperator{\instantiatePresence}{instPre}
\DeclareMathOperator{\generalizeValue}{genVal}
\DeclareMathOperator{\generalize}{gen}
\DeclareMathOperator{\freeVariable}{fv}
\DeclareMathOperator{\freeVariableWithKind}{fvKind}
\DeclareMathOperator{\dangerousVariable}{dv}
\DeclareMathOperator{\dangerousVariableRecordRow}{dvRcdRow}
\DeclareMathOperator{\dangerousVariableRecordPresence}{dvRcdPre}
\DeclareMathOperator{\dangerousVariableVariantRow}{dvVariantRow}
\DeclareMathOperator{\dangerousVariableVariantPresence}{dvVariantPre}
\DeclareMathOperator{\unify}{unify}
\DeclareMathOperator{\unifyRow}{unifyRow}
\DeclareMathOperator{\unifyPresence}{unifyPre}
\DeclareMathOperator{\unifyPresenceWithType}{unifyPT}
\newcommand{\newVariable}{newvar}
\newcommand{\newVariables}{newvars}
\newcommand{\entails}{\vdash}
\newcommand{\typingRelation}[5]{#1, #2 \entails #3 \;:\; #4 ,\; #5}
\newcommand{\composite}{\circ}
\newcommand{\definedAs}{\stackrel{def}{=}}
\newcommand{\sequencing}{;\;}

\title{The Definition of \theLang}
\author{Lin Yinfeng}

\begin{document}

\maketitle

\tableofcontents

\chapter{\theLang{} the Calculus}

\section{Syntax}

\subsection{Term}

\begin{align*}
\code{t} \Coloneqq \quad & & \text{term} \\
& \code{\lambda\ x.\ t} & \text{abstraction} \\
| \quad & \code{t\ t} & \text{application} \\
| \quad & \code{x} & \text{variable} \\
| \quad & \code{let\ x = t\ in\ t} & \text{let-in} \\
| \quad & \code{\{\ \}} & \text{empty record} \\
| \quad & \code{extend(l)} & \text{record extend} \\
| \quad & \code{update(l)} & \text{record update} \\
| \quad & \code{access(l)} & \text{record access} \\
| \quad & \code{[\ ]} & \text{empty match} \\
| \quad & \code{extend(\backtick l)} & \text{match extend} \\
| \quad & \code{update(\backtick l)} & \text{match update} \\
| \quad & \code{\backtick l} & \text{variant creation} \\
| \quad & \code{ref} & \text{reference} \\
| \quad & \code{!} & \text{dereference} \\
| \quad & \code{\coloneqq} & \text{assignment} \\
| \quad & \code{l} & \text{location} \\
| \quad & \code{new} & \text{new}
\end{align*}

\subsection{Derived Form}

\begin{align*}
\lambda\ \_ .\ \code{t} \definedAs & \lambda\ \code{x}.\ \code{t} \quad \text{where \(\code{x} \not\in \freeVariable(\code{t})\)} \\
\code{t}_1\sequencing\code{t}_2 \definedAs & (\lambda\ \_ .\ \code{t}_2)\ \code{t}_1
\end{align*}

\subsection{Value}

\begin{align*}
\code{v} \Coloneqq \quad & & \text{value} \\
& \code{\lambda\ x.\ t} & \text{abstraction} \\
| \quad & \code{rv} & \text{record value} \\
| \quad & \code{mv} & \text{match value} \\
| \quad & \code{\backtick l\ v} & \text{variant} \\
| \quad & \code{extend(l)} & \text{record extend 1} \\
| \quad & \code{extend(l)\ v} & \text{record extend 2} \\
| \quad & \code{update(l)} & \text{record update 1} \\
| \quad & \code{update(l)\ v} & \text{record update 2} \\
| \quad & \code{access(l)} & \text{record access} \\
| \quad & \code{extend(\backtick l)} & \text{match extend 1} \\
| \quad & \code{extend(\backtick l)\ v} & \text{match extend 2} \\
| \quad & \code{update(\backtick l)} & \text{match update 1} \\
| \quad & \code{update(\backtick l)\ v} & \text{match update 2} \\
| \quad & \code{\backtick l} & \text{variant creation} \\
| \quad & \code{ref} & \text{reference} \\
| \quad & \code{!} & \text{dereference} \\
| \quad & \code{\coloneqq} & \text{assignment 1} \\
| \quad & \code{\coloneqq\ v} & \text{assignment 2} \\
| \quad & \code{l} & \text{location}
\end{align*}

\begin{align*}
\code{rv} \Coloneqq \quad & & \text{record value} \\
& \code{\{\ \}} & \text{empty record} \\
| \quad & \code{extend(l)\ v\ rv} & \text{record extend}
\end{align*}

\begin{align*}
\code{mv} \Coloneqq \quad & & \text{match value} \\
& \code{[\ ]} & \text{empty match} \\
| \quad & \code{extend(\backtick l)\ v\ mv} & \text{match extend}
\end{align*}

\subsection{Type}

\begin{align*}
\code{T, MT} \Coloneqq \quad & & \text{monomorphic type} \\
& \code{X} & \text{type variable} \\
& \code{MT \rightarrow MT} & \text{arrow} \\
& \code{\{\ R\ \}} & \text{record} \\
& \code{[\ R\ ]} & \text{variant} \\
& \code{\mu X.\ MT} & \text{recursive} \\
& \code{Ref\ MT} & \text{reference}
\end{align*}

\begin{align*}
\code{R} \Coloneqq \quad & & \text{row} \\
& \code{\cdot} & \text{empty row} \\
& \code{X} & \text{row variable} \\
& \code{l : P, R} & \text{presence} \\
& \code{\mu X.\ R} & \text{recursive}
\end{align*}

\begin{align*}
\code{P} \Coloneqq \quad & & \text{presence} \\
& \code{Absent} & \text{absent} \\
& \code{Present\ MT} & \text{present} \\
& \code{X} & \text{variable} \\
& \code{X\ MT} & \text{variable with type}
\end{align*}

\begin{align*}
\code{PT} \Coloneqq \quad & & \text{presence with type} \\
& \code{PTAbsent} & \text{absent} \\
& \code{PTPresent} & \text{present} \\
& \code{X} & \text{variable}
\end{align*}

\begin{align*}
\code{PT} \Coloneqq\quad & & \text{polymorphic type} \\
& \code{MT} & \text{monomorphic type} \\
& \code{\forall\ X :: K.\ PT} & \text{universal qualified}
\end{align*}

\subsection{Kind}

\begin{align*}
\code{K} \Coloneqq \quad & & \text{kind} \\
& \code{*} & \text{proper} \\
& \code{Presence} & \text{presence} \\
& \code{Row} & \text{row} \\
& \code{K \Rightarrow K} & \text{arrow}
\end{align*}

\subsection{Typing Context}

\begin{align*}
\Gamma \Coloneqq \quad & & \text{typing context} \\
& \code{\emptyset} & \text{empty} \\
& \code{\Gamma, \code{x}:\code{PT}} & \text{binding}
\end{align*}

\section{Evaluation}

Evaluation of \theLang{} is defined in small step operational semantic. The relation \(\code{t}\mid\mu \longrightarrow \code{t}\mid\mu\) is the smallest relation satisfying all instances of the following rules.

\usetagform{nowidth}
\begin{gather}
\inferrule
{}
{(\lambda\ \code{x}.\ \code{t}_{12})\ \code{v}_2\mid\mu \longrightarrow [\code{x}\mapsto\code{v}_2]\code{t}_{12} \mid\mu}
\ruleTag{E-AppAbs}
\\
\inferrule
{\code{t}_1\mid\mu \longrightarrow \code{t}'_1\mid\mu'}
{\code{t}_1\ \code{t}_2\mid\mu \longrightarrow \code{t}'_1\ \code{t}_2\mid\mu'}
\ruleTag{E-App1}
\\
\inferrule
{\code{t}_2\mid\mu \longrightarrow \code{t}'_2\mid\mu'}
{\code{v}_1\ \code{t}_2\mid\mu \longrightarrow \code{v}_1\ \code{t}'_2\mid\mu'}
\ruleTag{E-App2}
\\
\inferrule
{}
{\code{let}\ \code{x} = \code{v}_1\ \code{in}\ \code{t}_2\mid\mu \longrightarrow [\code{x}\mapsto\code{v}_1]\code{t}_2\mid\mu}
\ruleTag{E-LetV}
\\
\inferrule
{\code{t}_1\mid\mu \longrightarrow \code{t}'_1\mid\mu'}
{\code{let}\ \code{x} = \code{t}_1\ \code{in}\ \code{t}_2\mid\mu \longrightarrow \code{let}\ \code{x} = \code{t}'_1\ \code{in}\ \code{t}_2\mid\mu'}
\ruleTag{E-Let}
\\
\inferrule
{}
{(\code{extend}(\backtick \code{l}_1)\ \code{v}_1\ \code{mv}_1)\ (\backtick \code{l}_2\ \code{v}_2)\mid\mu\longrightarrow
\code{mv}_1\ (\backtick \code{l}_2\ \code{v}_2)\mid\mu}
\ruleTag{E-Match1}
\\
\inferrule
{}
{(\code{extend}(\backtick \code{l})\ \code{v}_1\ \code{mv}_1)\ (\backtick \code{l}\ \code{v}_2)\mid\mu\longrightarrow
\code{v}_1\ \code{v}_2\mid\mu}
\ruleTag{E-Match2}
\\
\inferrule
{}
{\code{update}(\backtick \code{l}_1)\ \code{v}_1\ (\code{extend}(\backtick \code{l}_2)\ \code{v}_2\ \code{mv}_2)\mid\mu\longrightarrow \\\\
\code{extend}(\backtick \code{l}_2)\ \code{v}_2\ (\code{update}(\backtick \code{l}_1)\ \code{v}_1\ \code{mv}_2)\mid\mu}
\ruleTag{E-MatUpdate1}
\\
\inferrule
{}
{\code{update}(\backtick \code{l})\ \code{v}_1\ (\code{extend}(\backtick \code{l})\ \code{v}_2\ \code{mv}_2)\mid\mu\longrightarrow
(\code{extend}(\backtick \code{l})\ \code{v}_1\ \code{mv}_2)\mid\mu}
\ruleTag{E-MatUpdate2}
\\
\inferrule
{}
{\code{access}(\code{l}_1)\ (\code{extend}(\code{l}_2)\ \code{v}_2\ \code{mv}_2)\mid\mu\longrightarrow
\code{access}(\code{l}_1)\ \code{mv}_2\mid\mu}
\ruleTag{E-RcdAccess1}
\\
\inferrule
{}
{\code{access}(\code{l})\ (\code{extend}(\code{l})\ \code{v}_2\ \code{mv}_2)\mid\mu\longrightarrow
\code{v}_2\mid\mu}
\ruleTag{E-RcdAccess2}
\\
\inferrule
{}
{\code{update}(\code{l}_1)\ \code{v}_1\ (\code{extend}(\code{l}_2)\ \code{v}_2\ \code{mv}_2)\mid\mu\longrightarrow \\\\
\code{extend}(\code{l}_2)\ \code{v}_2\ (\code{update}(\code{l}_1)\ \code{v}_1\ \code{mv}_2)\mid\mu}
\ruleTag{E-RcdUpdate1}
\\
\inferrule
{}
{\code{update}(\code{l})\ \code{v}_1\ (\code{extend}(\code{l})\ \code{v}_2\ \code{mv}_2)\mid\mu\longrightarrow
(\code{extend}(\code{l})\ \code{v}_1\ \code{mv}_2)\mid\mu}
\ruleTag{E-RcdUpdate2}
\\
\inferrule
{\code{l}\notin\domain(\mu)}
{\code{ref}\ \code{v}\mid\mu\longrightarrow\code{l}\mid(\mu, \code{l}\mapsto\code{v})}
\ruleTag{E-Ref}
\\
\inferrule
{\mu(\code{l}) = \code{v}}
{\code{!}\ \code{l}\mid\mu\longrightarrow v\mid\mu}
\ruleTag{E-Deref}
\\
\inferrule
{\code{l}\in\domain(\mu)}
{\code{l}\coloneqq\code{v}\mid\mu\longrightarrow \code{unit}\mid[\code{l}\mapsto\code{v}]\mu}
\ruleTag{E-Assign}
\end{gather}

\section{Substitution}

A substitution \([\code{x}\mapsto\code{t}]\) is a mapping from term or type variables to terms or types. Apply substition \([\code{x}\mapsto\code{t}_1]\) to \(\code{t}_3\), is written \([\code{x}\mapsto\code{t}_1]\code{t}_2 = \code{t}_3\), which replace all occurrence of variable \(\code{x}\) in \(\code{t}_2\) to \(\code{t}_1\). Free variables in \(\code{t}_1\) should not be bound in \(\code{t}_3\). Use \textalpha-conversion to prevent free variables in \(\code{t}_1\) being bound.

\section{Typing}

\subsection{Equivalence Relation}

Maintain an equivalence relation \(E\) with Union-Find algorithm.

Every equivalence class has a descriptor, which is an element in the equivalence class.

The operation \(\find(E, e) = e'\) return the descriptor of the element \(e\) in the equivalence relation \(E\).

The operation \(\union(E, a, b) = E'\) equate the equivalence class of element \(a\) and \(b\), make the descriptor of the equivalence class of element \(b\) as the descriptor of the unioned equivalence class. Return the new equivalence relation \(E'\).

\subsection{Type Description from Equivalence Relation}

Equivalence relation of type, row and presence forms tree representations of monomorphic type.

operation \(\describeType(E, V, \code{T})\) to convert a monomorphic type \(\code{T}\) into a complete type by continuous replacing variables with its descriptor in equivalence relation \(E\).\(V\) is a set of variable, variable in the set will not be converted.

\(\describeRow(E, V, \code{R})\), \(\describePresence(E, V, \code{P})\), \(\describePresenceWithType(E, V, \code{PT})\) do the same thing as \(\describeType\) for other kind of types.

\(\describeScheme(E, V, \code{PT})\) only convert non qualified variables.

\begin{gather}
\inferrule
{\describeScheme(E, V \cup \{\code{X}\}, \code{PT}) = \code{PT}'}
{\describeScheme(E, V, \forall\ \code{X} :: \code{K} .\ \code{PT}) = \forall\ \code{X} :: \code{K} .\ \code{PT}'}
\ruleTag{DescScm-PT}
\\
\inferrule
{}
{\describeScheme(E, V, \code{MT}) = \describeType(E, V, \code{MT})}
\ruleTag{DescScm-MT}
\end{gather}

\begin{gather}
\inferrule
{\code{X} \in V}
{\describeType(E, V, \code{X}) = \code{X}}
\ruleTag{DescType-Var1}
\\
\inferrule
{\code{X} \not\in V \and
 \find(E, \code{X}) = \code{X}}
{\describeType(E, V, \code{X}) = \code{X}}
\ruleTag{DescType-Var2}
\\
\inferrule
{\code{X} \not\in V \and
 \find(E, \code{X}) = \code{MT} \and
 \code{MT} \not= \code{X} \\\\
 \describeType(E, V \cup \{\code{X}\}, \code{MT}) = \code{MT}' \and
 \code{X} \not\in \freeVariable(\code{MT}')}
{\describeType(E, V, \code{X}) = \code{MT}'}
\ruleTag{DescType-Var3}
\\
\inferrule
{\code{X} \not\in V \and
 \find(E, \code{X}) = \code{MT} \and
 \code{MT} \not= \code{X} \\\\
 \describeType(E, V \cup \{\code{X}\}, \code{MT}) = \code{MT}' \and
 \code{X} \in \freeVariable(\code{MT}')}
{\describeType(E, V, \code{X}) = \mu\code{X}.\code{MT}'}
\ruleTag{DescType-VarRec}
\\
\inferrule
{\describeType(E, V, \code{MT}_1) = \code{MT}'_1 \\\\
 \describeType(E, V, \code{MT}_2) = \code{MT}'_2}
{\describeType(E, V, \code{MT}_1\rightarrow\code{MT}_2) = \code{MT}'_1\rightarrow\code{MT}'_2}
\ruleTag{DescType-Arr}
\\
\inferrule
{\describeRow(E, V, \code{R}) = \code{R}'}
{\describeType(E, V, \{\ \code{R}\ \}) = \{\ \code{R}'\ \}}
\ruleTag{DescType-Rcd}
\\
\inferrule
{\describeRow(E, V, \code{R}) = \code{R}'}
{\describeType(E, V, [\ \code{R}\ ]) = [\ \code{R}'\ ]}
\ruleTag{DescType-Variant}
\\
\inferrule
{\describeType(E, V, \code{MT}) = \code{MT}'}
{\describeType(E, V, \code{Ref}\ \code{MT}) = \code{Ref}\ \code{MT}'}
\ruleTag{DescType-Ref}
\\
\inferrule
{\describeType(E, V \cup \{\code{X}\}, \code{MT}) = \code{MT}'}
{\describeType(E, V, \mu\code{X}.\code{MT}) = \mu\code{X}.\code{MT}'}
\ruleTag{DescType-Rec}
\end{gather}

\begin{gather}
\inferrule
{}
{\describeRow(E, V, \cdot) = \cdot}
\ruleTag{DescRow-Empty}
\\
\inferrule
{\code{X} \in V}
{\describeRow(E, V, \code{X}) = \code{X}}
\ruleTag{DescRow-Var1}
\\
\inferrule
{\code{X} \not\in V \and
 \find(E, \code{X}) = \code{X}}
{\describeRow(E, V, \code{X}) = \code{X}}
\ruleTag{DescRow-Var2}
\\
\inferrule
{\code{X} \not\in V \and
 \find(E, \code{X}) = \code{R} \and
 \code{R} \not= \code{X} \\\\
 \describeRow(E, V \cup \{\code{X}\}, \code{R}) = \code{R}' \and
 \code{X} \not\in \freeVariable(\code{R}')}
{\describeRow(E, V, \code{X}) = \code{R}'}
\ruleTag{DescRow-Var3}
\\
\inferrule
{\code{X} \not\in V \and
 \find(E, \code{X}) = \code{R} \and
 \code{R} \not= \code{X} \\\\
 \describeRow(E, V \cup \{\code{X}\}, \code{R}) = \code{R}' \and
 \code{X} \in \freeVariable(\code{R}')}
{\describeRow(E, V, \code{X}) = \mu\code{X}.\code{R}'}
\ruleTag{DescRow-VarRec}
\\
\inferrule
{\describePresence(E, V, \code{P}) = \code{P}' \and
 \describeRow(E, \code{R}) = \code{R}'}
{\describeRow(E, V, (\code{l}:\code{P},\code{R})) = \code{l}:\code{P}',\code{R}'}
\ruleTag{DescRow-Pre}
\\
\inferrule
{\describeRow(E, V \cup \{\code{X}\}, \code{R}) = \code{R}'}
{\describeRow(E, V, \mu\code{X}.\code{R}) = \mu\code{X}.\code{R}'}
\ruleTag{DescRow-Rec}
\end{gather}

\begin{gather}
\inferrule
{}
{\describePresence(E, V, \code{Absent}) = \code{Absent}}
\ruleTag{DescPre-Abs}
\\
\inferrule
{\describeType(E, V, \code{MT}) = \code{MT}'}
{\describePresence(E, V, \code{Present}\ \code{MT}) = \code{Present}\ \code{MT}'}
\ruleTag{DescPre-Pre}
\\
\inferrule
{\find(E, \code{X}) = \code{X}}
{\describePresence(E, V, \code{X}) = \code{X}}
\ruleTag{DescPre-Var1}
\\
\inferrule
{\find(E, \code{X}) = \code{P} \and
 \code{MT} \not= \code{X} \and
 \describePresence(E, V, \code{P}) = \code{P}'}
{\describePresence(E, V, \code{X}) = \code{P}'}
\ruleTag{DescPre-Var2}
\\
\inferrule
{\describePresenceWithType(E, V, \code{X}) = \code{X} \\\\
 \describeType(E, V, \code{MT}) = \code{MT}'}
{\describePresence(E, V, \code{X}\ \code{MT}) = \code{X}\ \code{MT}')}
\ruleTag{DescPre-PTVar1}
\\
\inferrule
{\describePresenceWithType(E, V, \code{X}) = \code{PTAbsent}}
{\describePresence(E, V, \code{X}\ \code{MT}) = \code{Absent}}
\ruleTag{DescPre-PTVar2}
\\
\inferrule
{\describePresenceWithType(E, V, \code{X}) = \code{PTPresent} \\\\
 \describeType(E, V, \code{MT}) = \code{MT}'}
{\describePresence(E, V, \code{X}\ \code{MT}) = \code{Present}\ \code{MT}'}
\ruleTag{DescPre-PTVar3}
\end{gather}

\begin{gather}
\inferrule
{}
{\describePresenceWithType(E, V, \code{PTAbsent}) = \code{PTAbsent}}
\ruleTag{DescPT-Abs}
\\
\inferrule
{}
{\describePresenceWithType(E, V, \code{PTPREsent}) = \code{PTPREsent}}
\ruleTag{DescPT-Pre}
\\
\inferrule
{\find(E, \code{X}) = \code{X}}
{\describePresenceWithType(E, V, \code{X}) = \code{X}}
\ruleTag{DescPT-Var1}
\\
\inferrule
{\find(E, \code{X}) = \code{PT} \and
 \code{PT} \not= \code{X}}
{\describePresenceWithType(E, V, \code{X}) = \describePresenceWithType(E, V, \code{PT})}
\ruleTag{DescPT-Var2}
\end{gather}

\begin{gather}
\inferrule
{}
{\describeContextScheme(E, V, \code{\emptyset}) = \code{\emptyset}}
\ruleTag{DescCtxScm-Empty}
\\
\inferrule
{}
{\describeContextScheme(E, V, (\code{x}:\code{PT}, \Gamma)) = \code{x}:\describeScheme(E, V, \code{PT}), \Gamma}
\ruleTag{DescCtxScm-Bind}
\end{gather}

\subsection{Instantiation}

Instantiation first replace every qualified type variable with fresh variables, then encode the type as a tree in equivalence relation.

\begin{gather}
\inferrule
{\instantiate(E, \code{PT}) = E', \code{PT'}}
{\instantiate(E, \code{\forall\ X.\ PT}) = E' , [\code{X}\mapsto\newVariable]\code{PT'}}
\ruleTag{Inst-PT}
\\
\instantiate(E, \code{MT}) = \instantiateType(E, \code{MT})
\ruleTag{Inst-MT}
\end{gather}

\begin{gather}
\inferrule
{}
{\instantiateType(E, \code{X}) = E , \code{X}}
\ruleTag{InstType-Var}
\\
\inferrule
{\instantiateType(E, \code{T}_1) = E_1, \code{T}'_1 \and
\instantiateType(E_1, \code{T}_2) = E_2, \code{T}'_2}
{\instantiateType(E, \code{T}_1\rightarrow\code{T}_2) = E_2, \code{T}'_1\rightarrow\code{T}'_2}
\ruleTag{InstType-Arr}
\\
\inferrule
{\instantiateRow(E, \code{R}) = E', \code{R}'}
{\instantiateType(E, \{\ \code{R}\ \}) = E', \{\ \code{R}'\ \}}
\ruleTag{InstType-Rcd}
\\
\inferrule
{\instantiateRow(E, \code{R}) = E', \code{R}'}
{\instantiateType(E, [\ \code{R}\ ]) = E', [\ \code{R}'\ ]}
\ruleTag{InstType-Variant}
\\
\inferrule
{\instantiateType(E, \code{MT}) = E', \code{MT}' \and
 \union(E', \code{X}, \code{MT}') = E''}
{\instantiateType(E, \mu \code{X}.\code{MT}) = E'', \code{X}}
\ruleTag{InstType-Rec}
\\
\inferrule
{\instantiateType(E, \code{MT}) = E', \code{MT}'}
{\instantiateType(E, \code{Ref}\ \code{MT}) = E', \code{Ref}\ \code{MT}'}
\ruleTag{InstType-Ref}
\end{gather}

\begin{gather}
\inferrule
{}
{\instantiateRow(E, \cdot) = E, \cdot}
\ruleTag{InstRow-Empty}
\\
\inferrule
{}
{\instantiateRow(E, \code{X}) = E, \code{X}}
\ruleTag{InstRow-Var}
\\
\inferrule
{\instantiatePresence(E, \code{P}) = E', \code{P}' \and
 \instantiateRow(E', \code{R}) = E'', \code{R}'}
{\instantiateRow(E, (\code{l}:\code{P}, \code{R})) = E'', (\code{l}:\code{P}', \code{R}')}
\ruleTag{InstRow-Pre}
\\
\inferrule
{\instantiateRow(E, \code{R}) = E', \code{R}' \and
 \union(E', \code{X}, \code{R}') = E''}
{\instantiateRow(E, \mu\code{X}.\code{R}) = E'', \code{X}}
\ruleTag{InstRow-Rec}
\end{gather}

\begin{gather}
\inferrule
{}
{\instantiatePresence(E, \code{Absent}) = E, \code{Absent}}
\ruleTag{InstPre-Abs}
\\
\inferrule
{\instantiateType(E, \code{MT}) = E', \code{MT}'}
{\instantiatePresence(E, \code{Present}\ \code{MT}) = E', \code{Present}\ \code{MT}'}
\ruleTag{InstPre-Pre}
\\
\inferrule
{}
{\instantiatePresence(E, \code{X}) = E, \code{X}}
\ruleTag{InstPre-Var}
\\
\inferrule
{\instantiateType(E, \code{MT}) = E', \code{MT}'}
{\instantiatePresence(E, \code{X}\ \code{MT}) = E', \code{X}\ \code{MT}'}
\ruleTag{InstPre-VarWithType}
\end{gather}

\subsection{Generalization}

\begin{equation}
\begin{split}
\generalizeValue(\Gamma, E, \code{MT}) & = \forall\ \overline{\freeVariable(\code{MT}') \setminus \freeVariable(\Gamma')}.\ \code{MT}' \\
\Gamma' & = \describeContextScheme(\Gamma) \\
\code{MT}' & = \describeType(E, \emptyset, \code{MT})
\end{split}
\ruleTag{Gen-Value}
\end{equation}

\begin{equation}
\begin{split}
\generalize(\Gamma, E, \code{MT}) & = \forall\ \overline{\freeVariable(\code{MT}') \setminus \freeVariable(\Gamma') \setminus \dangerousVariable(\code{MT}')}.\ \code{MT}' \\
\Gamma' & = \describeContextScheme(\Gamma) \\
\code{MT}' & = \describeType(E, \emptyset, \code{MT})
\end{split}
\ruleTag{Gen-NonValue}
\end{equation}

\subsubsection{Dangerous Variable}

Working in progress.

\subsubsection{Replace Covariant Only Present}

Working in progress.

\subsection{Typing Relation}

\begin{gather}
\inferrule
{\code{X}:\code{PT} \in \Gamma \and
 \instantiate(\code{PT}) = \code{MT}}
{\typingRelation{\Gamma}{E}{\code{X}}{\code{MT}}{E}}
\ruleTag{T-Var}
\\
\inferrule
{\typingRelation{\Gamma}{E}{\code{t}_1}{\code{MT}_1}{E_1} \and
 \typingRelation{\Gamma}{E_1}{\code{t}_2}{\code{MT}_2}{E_2} \\\\
 \code{X} = \newVariable \and
 \unify(E_2, \code{MT}_1, \code{MT}_2\rightarrow\code{X}) = E_3}
{\typingRelation{\Gamma}{E}{\code{t}_1\ \code{t}_2}{\code{X}}{E_3}}
\ruleTag{T-App}
\\
\inferrule
{\code{X} = \newVariable \and
 \typingRelation{(\Gamma, \code{x}:\code{X})}{E}{\code{t}}{\code{MT}}{E'}}
{\typingRelation{\Gamma}{E}{\lambda\ \code{x}.\ \code{t}}{\code{X}\rightarrow\code{MT}}{E'}}
\ruleTag{T-Abs}
\\
\inferrule
{\typingRelation{\Gamma}{E}{\code{t}_1}{\code{MT}_1}{E_1} \and
 \generalize(\Gamma, E_1, \code{MT}_1) = \code{PT}_1 \\\\
 \typingRelation{(\Gamma, \code{x}:\code{PT}_1)}{E_1}{\code{t}_2}{\code{MT}_2}{E_2}}
{\typingRelation{\Gamma}{E}{\code{let}\ \code{x} = \code{t}_1\ \code{in}\ \code{t}_2}{\code{MT}_2}{E_2}}
\ruleTag{T-Let}
\\
\inferrule
{\typingRelation{\Gamma}{E}{\code{t}_1}{\code{MT}_1}{E_1} \and
 \generalizeValue(\Gamma, E_1, \code{MT}_1) = \code{PT}_1 \\\\
 \typingRelation{(\Gamma, \code{x}:\code{PT}_1)}{E_1}{\code{t}_2}{\code{MT}_2}{E_2}}
{\typingRelation{\Gamma}{E}{\code{let}\ \code{x} = \code{v}_1\ \code{in}\ \code{t}_2}{\code{MT}_2}{E_2}}
\ruleTag{T-LetVal}
\end{gather}

\begin{gather}
\inferrule
{}
{\typingRelation{\Gamma}{E}{\{\ \}}{\{\ \cdot\ \}}{E}}
\ruleTag{T-EmptyRcd}
\\
\inferrule
{\code{P}, \code{R}, \code{PT}, \code{MT} = \newVariables}
{\typingRelation{\Gamma}{E}{\code{extend(l)}}
 {\code{MT}\rightarrow\{\ \code{l}:\code{P}, \code{R}\ \}\rightarrow\{\ \code{l}:\code{PT}\ \code{MT}, \code{R}\ \}}
 {E}}
\ruleTag{T-RcdExtend}
\\
\inferrule
{\code{R}, \code{PT}, \code{MT}_1, \code{MT}_2 = \newVariables}
{\typingRelation{\Gamma}{E}{\code{update(l)}}
 {\code{MT}_1\rightarrow\{\ \code{l}:\code{Present}\ \code{MT}_2, \code{R}\ \}\rightarrow\{\ \code{l}:\code{PT}\ \code{MT}_1, \code{R}\ \}}
 {E}}
\ruleTag{T-RcdUpdate}
\\
\inferrule
{\code{R}, \code{MT} = \newVariables}
{\typingRelation{\Gamma}{E}{\code{access(l)}}
 {\{\ \code{l}:\code{Present}\ \code{MT}, \code{R}\ \}\rightarrow\code{MT}}
 {E}}
\ruleTag{T-RcdAccess}
\\
\inferrule
{\code{MT} = \newVariable}
{\typingRelation{\Gamma}{E}{[\ ]}{[\ \cdot\ ]\rightarrow\code{MT}}{E}}
\ruleTag{T-EmptyMatch}
\\
\inferrule
{\code{MT}_1, \code{MT}_2, \code{R}, \code{P}, \code{PT} = \newVariables \\\\
 \code{MT} = (\code{MT}_1\rightarrow\code{MT}_2)\rightarrow([\ \code{\backtick l}:\code{P}, \code{R}\ ]\rightarrow\code{MT}_2)\rightarrow[\ \code{\backtick l}:\code{PT}\ \code{MT}_1, \code{R}\ ]\rightarrow\code{MT}_2}
{\typingRelation{\Gamma}{E}{\code{extend(\backtick l)}}{\code{MT}}{E}}
\ruleTag{T-MatchExtend}
\\
\inferrule
{\code{MT}_1, \code{MT}_2, \code{MT}_3, \code{R}, \code{P}, \code{PT} = \newVariables \\\\
 \code{MT} = (\code{MT}_1\rightarrow\code{MT}_3)\rightarrow([\ \code{\backtick l}:\code{Present}\ \code{MT}_2, \code{R}\ ] \rightarrow\code{MT}_3) \\\\
 \rightarrow[\ \code{\backtick l}:\code{PT}\ \code{MT}_1, \code{R}\ ]\rightarrow\code{MT}_3}
{\typingRelation{\Gamma}{E}{\code{extend(\backtick l)}}{\code{MT}}{E}}
\ruleTag{T-MatchUpdate}
\\
\inferrule
{\code{MT}, \code{R} = \newVariable}
{\typingRelation{\Gamma}{E}{\code{\backtick l}}{\code{MT}\rightarrow[\ \code{\backtick l}:\code{Present}\ \code{MT}, \code{R}\ ]}{E}}
\ruleTag{T-Variant}
\\
\inferrule
{\code{MT} = \newVariable}
{\typingRelation{\Gamma}{E}{\code{ref}}{\code{MT}\rightarrow\code{Ref}\ \code{MT}}{E}}
\ruleTag{T-Ref}
\\
\inferrule
{\code{MT} = \newVariable}
{\typingRelation{\Gamma}{E}{\code{!}}{\code{Ref}\ \code{MT}\rightarrow\code{MT}}{E}}
\ruleTag{T-Deref}
\\
\inferrule
{\code{MT} = \newVariable}
{\typingRelation{\Gamma}{E}{\code{:=}}{\code{Ref}\ \code{MT}\rightarrow\code{MT}\rightarrow\{\ \cdot\ \}}{E}}
\ruleTag{T-Assign}
\\
\inferrule
{\code{MT} = \newVariable}
{\typingRelation{\Gamma}{E}{\code{new}}{(\code{Ref} \code{MT}\rightarrow\code{MT})\rightarrow\code{MT}}{E}}
\ruleTag{T-New}
\end{gather}

\subsection{Unification}

\begin{align*}
\unify(E, \code{MT}_1, \code{MT}_2) = \;
& \text{let}\ \code{S} = \find(E, \code{MT}_1)\ \text{in} \\
& \text{let}\ \code{T} = \find(E, \code{MT}_2)\ \text{in} \\
& \text{if}\ \code{S} = \code{T} \\
& \quad \text{then}\ E \\
& \text{else}\ \text{if}\ \code{S} = \code{X} \\
& \quad \text{then}\ \union(E, \code{X}, \code{T}) \\
& \text{else}\ \text{if}\ \code{T} = \code{X} \\
& \quad \text{then}\ \union(E, \code{X}, \code{S}) \\
& \text{else} \\
& \quad \text{let}\ E' = \union(E, \code{S}, \code{T})\ \text{in} \\
& \quad \text{if}\ \code{S} = \code{S}_{1}\rightarrow\code{S}_{2}\ \text{and}\ \code{T} = \code{T}_{1}\rightarrow\code{T}_{2} \\
& \quad \quad \text{then}\ \text{let}\ E'' = \unify(E', \code{S}_{1}, \code{T}_{1})\ \text{in}\ \unify(E'', \code{S}_{2}, \code{T}_{2}) \\
& \quad \text{else}\ \text{if}\ \code{S} = \{\ \code{R}_1\ \}\ \text{and}\ \code{T} = \{\ \code{R}_2\ \} \\
& \quad \quad \text{then}\ \unifyRow(E', \code{R}_1, \code{R}_2) \\
& \quad \text{else}\ \text{if}\ \code{S} = [\ \code{R}_1\ ]\ \text{and}\ \code{T} = [\ \code{R}_2\ ] \\
& \quad \quad \text{then}\ \unifyRow(E', \code{R}_1, \code{R}_2) \\
& \quad \text{else}\ \text{if}\ \code{S} = \code{Ref}\ \code{S}'\ \text{and}\ \code{T} = \code{Ref}\ \code{T}' \\
& \quad \quad\text{then}\ \unify(E', \code{S}', \code{T}') \\
& \quad \text{else} \\
& \quad \quad \text{fail}
\end{align*}

\begin{align*}
\unify(E, \code{R}_1, \code{R}_2) = \;
& \text{let}\ \code{R}'_1 = \find(E, \code{R}_1)\ \text{in} \\
& \text{let}\ \code{R}'_2 = \find(E, \code{R}_2)\ \text{in} \\
& \text{if}\ \code{R}'_1 = \code{R}'_2 \\
& \quad \text{then}\ E \\
& \text{else}\ \text{if}\ \code{R}'_1 = \code{X} \\
& \quad \text{then}\ \union(E, \code{X}, \code{R}'_2) \\
& \text{else}\ \text{if}\ \code{R}'_2 = \code{X} \\
& \quad \text{then}\ \union(E, \code{X}, \code{R}'_1) \\
& \text{else}\ \text{if}\ \code{R}'_1 = \cdot\ \text{and}\ \code{R}'_2 = \code{l}:\code{P},\code{R}''_2 \\
& \quad \text{then}\ \text{let}\ E' = \unifyPresence(E, \code{P}, \code{Absent})\ \text{in}\ \unifyRow(E', \code{R}''_2, \cdot) \\
& \text{else}\ \text{if}\ \code{R}'_1 = \code{l}:\code{P},\code{R}''_1\ \text{and}\ \code{R}'_2 = \cdot \\
& \quad \text{then}\ \text{let}\ E' = \unifyPresence(E, \code{P}, \code{Absent})\ \text{in}\ \unifyRow(E', \code{R}''_1, \cdot) \\
& \text{else}\ \text{if}\ \code{R}'_1 = \code{l}:\code{P}_1,\code{R}''_1\ \text{and}\ \code{R}'_2 = \code{l}:\code{P}_2,\code{R}''_2 \\
& \quad \text{then}\ \text{let}\ E' = \unifyPresence(E, \code{P}_1, \code{P}_2)\ \text{in}\ \unifyRow(E', \code{R}''_1, \code{R}''_2) \\
& \text{else}\ \text{if}\ \code{R}'_1 = \code{l}_1:\code{P}_1,\code{R}''_1\ \text{and}\ \code{R}'_2 = \code{l}_2:\code{P}_2,\code{R}''_2 \\
& \quad \text{then}\ \text{let}\ \code{R}_3 = \newVariable\ \code{in} \\
& \qquad \text{let}\ E' = \unifyRow(E, \code{R}''_1, (\code{l}_2:\code{P}_2,\code{R}_3))\ \text{in}\ \unifyRow(E', \code{R}''_2, (\code{l}_1:\code{P}_1,\code{R}_3)) \\
& \text{else} \\
& \quad \text{fail}
\end{align*}

\begin{align*}
\unifyPresence(E, \code{P}_1, \code{P}_2) = \;
& \text{let}\ \code{P}'_1 = \find(E, \code{P}_1)\ \text{in} \\
& \text{let}\ \code{P}'_2 = \find(E, \code{P}_2)\ \text{in} \\
& \text{if}\ \code{P}'_1 = \code{P}'_2 \\
& \quad \text{then}\ E \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{X}\ \text{then} \\
& \quad \text{then}\ \union(E, \code{X}, \code{P}'_2) \\
& \text{else}\ \text{if}\ \code{P}'_2 = \code{X}\ \text{then} \\
& \quad \text{then}\ \union(E, \code{X}, \code{P}'_1) \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{Present}\ \code{MT}_1\ \text{and}\ \code{P}'_2 = \code{Present}\ \code{MT}_2\ \text{then} \\
& \quad \text{then}\ \unify(E, \code{MT}_1, \code{MT}_2) \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{X}_1\ \code{MT}_1\ \text{and}\ \code{P}'_2 = \code{Absent}\ \text{then} \\
& \quad \text{then}\ \unifyPresenceWithType(E, \code{X}_1, \code{PTAbsent}) \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{Absent}\ \text{and}\ \code{P}'_2 = \code{X}_2\ \code{MT}_2\ \text{then} \\
& \quad \text{then}\ \unifyPresenceWithType(E, \code{X}_2, \code{PTAbsent}) \\
& \quad \text{then}\ \unify(E, \code{MT}_1, \code{MT}_2) \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{X}_1\ \code{MT}_1\ \text{and}\ \code{P}'_2 = \code{Present}\ \code{MT}_2\ \text{then} \\
& \quad \text{then}\ \text{let}\ E' = \unifyPresenceWithType(E, \code{X}_1, \code{PTPresent})\ \text{in}\ \unify(E', \code{MT}_1, \code{MT}_2) \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{Present}\ \code{MT}_1\ \text{and}\ \code{P}'_2 = \code{X}_2\ \code{MT}_2\ \text{then} \\
& \quad \text{then}\ \text{let}\ E' = \unifyPresenceWithType(E, \code{X}_2, \code{PTPresent})\ \text{in}\ \unify(E', \code{MT}_1, \code{MT}_2) \\
& \text{else}\ \text{if}\ \code{P}'_1 = \code{X}_1\ \code{MT}_1\ \text{and}\ \code{P}'_2 = \code{X}_2\ \code{MT}_2\ \text{then} \\
& \quad \text{then}\ \text{let}\ E' = \unifyPresenceWithType(E, \code{X}_1, \code{X}_2)\ \text{in}\ \unify(E', \code{MT}_1, \code{MT}_2) \\
& \text{else} \\
& \quad \text{fail}
\end{align*}

\begin{align*}
\unifyPresenceWithType(E, \code{PT}_1, \code{PT}_2) = \;
& \text{let}\ \code{PT}'_1 = \find(E, \code{PT}_1)\ \text{in} \\
& \text{let}\ \code{PT}'_2 = \find(E, \code{PT}_2)\ \text{in} \\
& \text{if}\ \code{PT}'_1 = \code{PT}'_2 \\
& \quad \text{then}\ E \\
& \text{else}\ \text{if}\ \code{PT}'_1 = \code{X}\ \text{then} \\
& \quad \text{then}\ \union(E, \code{X}, \code{PT}'_2) \\
& \text{else}\ \text{if}\ \code{PT}'_2 = \code{X}\ \text{then} \\
& \quad \text{then}\ \union(E, \code{X}, \code{PT}'_1) \\
& \text{else} \\
& \quad \text{fail}
\end{align*}

\chapter{\theLang{} the Language}

Working in progress.

\end{document}