2024-08-07 16:00:01 +02:00
\documentclass [10pt, sigplan, nonacm] { acmart}
2024-07-24 11:23:51 +02:00
\usepackage [utf8] { inputenc}
\usepackage { formal-grammar}
\usepackage [dvipsnames] { xcolor}
\usepackage { mathpartir}
2024-08-07 15:59:03 +02:00
\usepackage { hyperref}
\usepackage { url}
2024-09-05 11:20:21 +02:00
\usepackage { stmaryrd}
2024-08-07 15:59:03 +02:00
\usepackage { minted}
\usemintedstyle { tango}
\DeclareUnicodeCharacter { 2200} { $ \forall $ }
\DeclareUnicodeCharacter { 03C3} { $ \sigma $ }
\DeclareUnicodeCharacter { 03C4} { $ \tau $ }
\DeclareUnicodeCharacter { 03BB} { $ \lambda $ }
\DeclareUnicodeCharacter { 21A6} { $ \mapsto $ }
\DeclareUnicodeCharacter { 039B} { $ \Lambda $ }
\DeclareUnicodeCharacter { 03B1} { $ \alpha $ }
\DeclareUnicodeCharacter { 03B2} { $ \beta $ }
\DeclareUnicodeCharacter { 03B3} { $ \gamma $ }
\DeclareUnicodeCharacter { 03B4} { $ \delta $ }
\DeclareUnicodeCharacter { 0393} { $ \Gamma $ }
2024-09-05 11:20:21 +02:00
\DeclareUnicodeCharacter { 211D} { $ \mathbb { R } $ }
2024-07-24 11:23:51 +02:00
\newcommand { \metavariable } [1]{ \textcolor { teal} { #1} }
\newcommand { \typeterminal } [1]{ \textcolor { brown} { #1} }
\newcommand { \exprterminal } [1]{ \textcolor { Sepia} { #1} }
\newcommand { \seltype } [0]{ { \textsc { \footnotesize { Typ} } } }
\newcommand { \selexpr } [0]{ { \textsc { \footnotesize { Exp} } } }
\newcommand { \selval } [0]{ { \textsc { \footnotesize { Val} } } }
\newcommand { \exprvars } [0]{ V }
\newcommand { \typevars } [0]{ \Upsilon }
\newcommand { \typenames } [0]{ \Sigma }
\newcommand { \typenonterm } [1]{ \nonterm { T_ \seltype \footnotesize { \textsf { $ ( \typenames , # 1 ) $ } } } }
\newcommand { \exprnonterm } [2]{ \nonterm { T_ \selexpr \footnotesize { \textsf { $ ( \typenames , # 1 , # 2 ) $ } } } }
\newcommand { \valnonterm } [2]{ \nonterm { T_ \selval \footnotesize { \textsf { $ ( \typenames , # 1 , # 2 ) $ } } } }
\newcommand { \todo } [1]{ { \textcolor { red} { \textbf { TODO:} #1} } }
\title { A functional core calculus with ladder-types}
\author { Michael Sippel}
\email { michael.sippel@mailbox.tu-dresden.de}
\makeatletter
\let \@ authorsaddresses\@ empty
\makeatother
\begin { document}
\begin { abstract}
This paper presents a minimal core calculus extending the \( \lambda \) -calculus by a polymorphic type-system similar to SystemF, but in addition it introduces a new type-constructor called the \emph { ladder-type} .
Using ladder-types, multi-layered embeddings of higher-level data-types into lower-level data-types can be described by a type-level structure.
By facilitating automatic transformations between semantically compatible datatypes, ladder-typing opens up a new paradigm of abstraction.
We formally define the syntax \& semantics of this calculus and prove its \emph { type soundness} .
Further we show how the Boehm-Berarducci encoding can be used to implement algebraic datatypes on the basis of the introduced core calculus.
\end { abstract}
\maketitle
\tableofcontents
%\newpage
\section { Core Calculus}
\subsection { Syntax}
The calculus is made up of a \emph { type-level} language, and an \emph { expression-level} language. The formal grammar for both is given in \ref { gr:core} .
In addition to \emph { function types} \( \tau _ 1 \rightarrow \tau _ 2 \) , \emph { universal types} \( \forall \alpha . \tau \) and type specialization \( < \tau _ 1 \tau _ 2 > \) ,
which are already known from SystemF,
types can be of the form \( \tau _ 1 \sim \tau _ 2 \) to denote a \emph { ladder type} to formalizes the notion of a type \( \tau _ 1 \) being represented in terms of type \( \tau _ 2 \) .
Similar to SystemF, expressions can be \emph { variables} , \emph { type-abstractions} , \emph { }
2024-08-07 16:00:01 +02:00
Coq definitions of the abstract syntax can be found in \hyperref [coq:terms] { \texttt { terms.v} } .
2024-07-24 11:23:51 +02:00
\begin { figure} [h]
\label { gr:core}
\begin { grammar}
2024-08-07 16:00:01 +02:00
\firstcase { T } {
2024-07-24 11:23:51 +02:00
\metavariable { \sigma }
2024-08-07 16:00:01 +02:00
} { Base Type}
2024-07-24 11:23:51 +02:00
\otherform {
2024-08-07 16:00:01 +02:00
\metavariable { \alpha }
} { Type Variable}
2024-07-24 11:23:51 +02:00
\otherform {
2024-08-07 16:00:01 +02:00
\typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \nonterm { T}
2024-07-24 11:23:51 +02:00
} { Universal Type}
\otherform {
2024-08-07 16:00:01 +02:00
\typeterminal { <} \nonterm { T} \quad \nonterm { T} \typeterminal { >}
} { Specialized Type}
2024-07-24 11:23:51 +02:00
\otherform {
2024-08-07 16:00:01 +02:00
\nonterm { T} \quad \typeterminal { \rightarrow } \quad \nonterm { T}
2024-07-24 11:23:51 +02:00
} { Function Type}
\otherform {
2024-08-07 16:00:01 +02:00
\nonterm { T} \quad \typeterminal { \rightarrow _ \text { morph} } \quad \nonterm { T}
2024-07-24 11:23:51 +02:00
} { Morphism Type}
\otherform {
2024-08-07 16:00:01 +02:00
\nonterm { T} \quad \typeterminal { \sim } \quad \nonterm { T}
2024-07-24 11:23:51 +02:00
} { Ladder Type}
$$ \\ $$
2024-08-07 16:00:01 +02:00
\firstcase { E
% T_\selexpr
}
2024-07-24 11:23:51 +02:00
{ \metavariable { x}
2024-08-07 16:00:01 +02:00
} { Variable}
2024-07-24 11:23:51 +02:00
2024-08-08 13:17:34 +02:00
\otherform {
\exprterminal { let} \quad \metavariable { x} \quad \exprterminal { =} \quad
\nonterm { E }
\quad \exprterminal { in} \quad
\nonterm { E }
} { Variable Binding}
2024-07-24 11:23:51 +02:00
\otherform {
$$ \exprterminal { \Lambda } \metavariable { \alpha }
\quad \exprterminal { \mapsto } \quad $$
2024-08-07 16:00:01 +02:00
\nonterm { E }
2024-07-24 11:23:51 +02:00
} { Type Abstraction}
\otherform {
$$ \exprterminal { \lambda } \metavariable { x } $$
2024-08-07 16:00:01 +02:00
\exprterminal { :} \nonterm { T }
2024-07-24 11:23:51 +02:00
\quad $$ \exprterminal { \mapsto } $$ \quad
2024-08-07 16:00:01 +02:00
\nonterm { E }
2024-07-24 11:23:51 +02:00
} { Value Abstraction}
\otherform {
$$ \exprterminal { \lambda } \metavariable { x } $$
2024-08-07 16:00:01 +02:00
\exprterminal { :} \nonterm { T }
\quad $$ \exprterminal { \mapsto _ \text { morph } } $$ \quad
\nonterm { E }
2024-07-24 11:23:51 +02:00
} { Value Morphism}
\otherform {
2024-08-07 16:00:01 +02:00
\nonterm { E }
2024-07-24 11:23:51 +02:00
\quad
2024-08-07 16:00:01 +02:00
\nonterm { T }
2024-07-24 11:23:51 +02:00
} { Type Application}
\otherform {
2024-08-07 16:00:01 +02:00
\nonterm { E }
2024-07-24 11:23:51 +02:00
\quad
2024-08-07 16:00:01 +02:00
\nonterm { E }
2024-07-24 11:23:51 +02:00
} { Value Application}
\otherform {
2024-08-07 16:00:01 +02:00
\nonterm { E }
2024-07-24 11:23:51 +02:00
\quad
\exprterminal { as}
\quad
2024-08-07 16:00:01 +02:00
\nonterm { T }
2024-08-08 13:17:34 +02:00
} { Ascription}
2024-07-24 11:23:51 +02:00
2024-08-08 13:17:34 +02:00
%\otherform{
% \nonterm{ E }
% \quad
% \exprterminal{to}
% \quad
% \nonterm{ T }
%}{Transformation}
%\otherform{\exprterminal{(} \quad \nonterm{E} \quad \exprterminal{)}}{Parenthesis}
2024-07-24 11:23:51 +02:00
$$ \\ $$
2024-08-07 16:00:01 +02:00
\firstcase { V} {
2024-08-08 13:17:34 +02:00
\nonterm { V } \quad
\exprterminal { as} \quad
\nonterm { T }
} { Ascribed Value}
2024-07-24 11:23:51 +02:00
2024-07-27 13:30:34 +02:00
\otherform {
\exprterminal { \Lambda } \metavariable { \alpha } \quad
\exprterminal { \mapsto } \quad
2024-08-07 16:00:01 +02:00
\nonterm { V }
} { Type-Abstraction Value}
2024-07-24 11:23:51 +02:00
\otherform {
2024-08-07 16:00:01 +02:00
\exprterminal { \lambda } \metavariable { x}
\exprterminal { :}
\nonterm { T } \quad
2024-07-24 11:23:51 +02:00
\exprterminal { \mapsto } \quad
2024-08-07 16:00:01 +02:00
\nonterm { E }
} { Abstraction Value}
2024-07-24 11:23:51 +02:00
\end { grammar}
\caption { Syntax of the core calculus with colors for \metavariable { metavariables} , \typeterminal { type-level terminal symbols} , \exprterminal { expression-level terminal symbols}
2024-08-07 16:00:01 +02:00
where $ \typenames , \typevars , \exprvars $ are mutually disjoint, countable sets of symbols to denote atomic type identifiers (\( \typenames \) ), typevariables (\( \typevars \) ), and expression variables (\( \exprvars \) ).
2024-08-08 13:17:34 +02:00
By default, assume \( \metavariable { \sigma } \in \typenames \) , \( \metavariable { \alpha } \in \typevars \) and \( \metavariable { x } \in \exprvars \) .
For simplicity, we write \( \metavariable { e } \in \nonterm { E } \) to say that the term \metavariable { e} is contained in the language generated by the nonterminal \( \nonterm { E } \) .
2024-07-24 11:23:51 +02:00
$$ \\ $$ }
\end { figure}
\begin { example}
\label { ex:terms}
Let \( \Sigma = \{ \text { Digit } , \text { Char } , \text { Seq } , \text { UTF - 8 } , \mathbb { N } , PosInt \} \cup \{ 0 , 1 , 2 , ... \} \) .
The following terms are valid types over \( \Sigma \) :
\begin { enumerate}
2024-08-07 16:00:01 +02:00
\item \typeterminal { <Seq Char>} \\ %\( \in \typenonterm{\emptyset}\)\\
2024-07-24 11:23:51 +02:00
"sequence of characters"
2024-08-07 16:00:01 +02:00
\item \typeterminal { <Seq <Digit 10> \( \sim \) Char>} \\ %\( \in \typenonterm{\emptyset}\)\\
2024-07-24 11:23:51 +02:00
"sequence of decimal digits, where each digit is represented as character"
2024-08-07 16:00:01 +02:00
\item \typeterminal { <Seq Item> \( \rightarrow \mathbb { N } \rightarrow \) Item} \\ %\( \in \typenonterm{\{Item\}}\)\\
2024-07-24 11:23:51 +02:00
"function that maps a sequence of items and a natural number to an item"\\
2024-08-07 16:00:01 +02:00
%Note: this type contains the free variable \typeterminal{Item}
\item \typeterminal { <Seq Char> \( \sim \) UTF-8 \( \rightarrow \mathbb { N } \rightarrow \) Char } \\ %\( \in \typenonterm{\emptyset}\)\\
2024-07-24 11:23:51 +02:00
"function that takes a sequence of chars, represented as UTF-8 string, and a natural number to return a character"
2024-08-07 16:00:01 +02:00
\item \typeterminal { \( \forall \) Radix . <Seq <Digit Radix> \( \sim \) Char>} \\ %\(\in \typenonterm{\emptyset} \)\\
"given the parameter \typeterminal { Radix} , a sequence of digits where each digit is represented as character"
%Note: this type-term is \emph{ground} (i.e. \(\typevars = \emptyset\)), since \typeterminal{Radix} is bound by \(\typeterminal{\forall}\)
2024-07-24 11:23:51 +02:00
\item \typeterminal {
\( \forall \) SrcRadix.\\
\( \forall \) DstRadix.\\
\( \mathbb { N } \sim \) <PosInt SrcRadix> \( \sim \) <Seq <Digit SrcRadix> \( \sim \) Char>\\
\( \rightarrow _ { morph } \) \\
\( \mathbb { N } \sim \) <PosInt DstRadix> \( \sim \) <Seq <Digit DstRadix> \( \sim \) Char>\\
2024-08-07 16:00:01 +02:00
} \\ %\(\in \typenonterm{\emptyset} \)\\
2024-07-24 11:23:51 +02:00
"morphism function that maps the \typeterminal { PosInt} representation of \( \typeterminal { \mathbb { N } } \) with radix \typeterminal { SrcRadix} to the \typeterminal { PosInt} representation of radix \typeterminal { DstRadix} "
\end { enumerate}
\end { example}
\begin { definition} [Substitution in Types]
Given a type-variable assignment \( \psi _ t = \{ \metavariable { \alpha _ 1 } \mapsto \metavariable { \tau _ 1 } , \quad \metavariable { \alpha _ 2 } \mapsto \metavariable { \tau _ 2 } , \quad \dots \} \) ,
2024-08-08 13:17:34 +02:00
the thereby induced substitution \( \overline { \psi _ t } \) replaces all \emph { free} occurences of the variables \( \metavariable { \alpha _ i } \) in a type-term \( \metavariable { \xi } \in \nonterm { T } \) recursively with the type-term given by \( \psi _ t ( \metavariable { \alpha _ i } ) \)
, while occurences of bound variables are left untouched.
Further, we assume that for all \( \tau _ i \) , all variable names are disjunct with the free variables of the term to which the substitution is applied.
2024-08-07 16:00:01 +02:00
Coq definition is at \hyperref [coq:subst-type] { subst.v:\ref { coq:subst-type} } .
2024-07-24 11:23:51 +02:00
\[ \overline { \psi _ t } \metavariable { \xi } = \begin { cases }
\metavariable { \xi } \quad \text { if } \metavariable { \xi } \in \typenames \\
\metavariable { \xi } \quad \text { if } \metavariable { \xi } \in \{ \metavariable { \alpha _ 1} , \metavariable { \alpha _ 2} , \dots \} \text { and } \metavariable { \xi } \notin \text { Dom} (\psi _ t)\\
\metavariable { \tau } \quad \text { if } \metavariable { \xi } \in \{ \metavariable { \alpha _ 1} , \metavariable { \alpha _ 2} , \dots \} \text { and } (\metavariable { \xi } \mapsto \metavariable { \tau } ) \in \psi _ t\\
\typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \overline { \psi _ t} \metavariable { \xi '} \quad \text { if } \metavariable { \xi } \text { is of the form } \typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \metavariable { \xi '} \text { and } \metavariable { \alpha } \notin \text { Dom} (\psi _ t)\\
\typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \overline { \psi _ t \setminus \{ \metavariable { \alpha } \mapsto \metavariable { \tau } \} } \metavariable { \xi '} \quad \text { if } \metavariable { \xi } \text { is of the form } \typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \metavariable { \xi '} \text { and } \{ \metavariable { \alpha } \mapsto \metavariable { \tau } \} \in \psi _ t\\
\typeterminal { <} (\overline { \psi _ t} \metavariable { \xi _ 1} ) \quad (\overline { \psi _ t} \metavariable { \xi _ 2} ) \typeterminal { >} \quad \text { if } \metavariable { \xi } \text { is of the form } \typeterminal { <} \metavariable { \xi _ 1} \quad \metavariable { \xi _ 2} \typeterminal { >} \\
(\overline { \psi _ t} \metavariable { \xi _ 1} ) \typeterminal { \rightarrow } (\overline { \psi _ t} \metavariable { \xi _ 2} ) \quad \text { if } \metavariable { \xi } \text { is of the form } \metavariable { \xi _ 1} \typeterminal { \rightarrow } \metavariable { \xi _ 2} \\
(\overline { \psi _ t} \metavariable { \xi _ 1} ) \typeterminal { \rightarrow _ { morph} } (\overline { \psi _ t} \metavariable { \xi _ 2} ) \quad \text { if } \metavariable { \xi } \text { is of the form } \metavariable { \xi _ 1} \typeterminal { \rightarrow _ { morph} } \metavariable { \xi _ 2} \\
(\overline { \psi _ t} \metavariable { \xi _ 1} ) \typeterminal { \sim } (\overline { \psi _ t} \metavariable { \xi _ 2} ) \quad \text { if } \metavariable { \xi } \text { is of the form } \metavariable { \xi _ 1} \typeterminal { \sim } \metavariable { \xi _ 2} \\
\end { cases} \]
\[ \overline { \psi _ t } \metavariable { e } = \begin { cases }
\metavariable { e} \quad \text { if } \metavariable { e} \text { is a variable}
2024-08-08 13:17:34 +02:00
\\
\exprterminal { \text { let } \metavariable { x} = } \overline { \psi _ t} \metavariable { a} \exprterminal { \text { in } } \overline { \psi _ t} \metavariable { e'}
\quad \text { if \metavariable { e} is of the form }
\exprterminal { \text { let } \metavariable { x} = \metavariable { a} \text { in } \metavariable { e'} }
\\
\exprterminal { \Lambda \metavariable { \alpha } \mapsto } \overline { \psi _ t} \metavariable { e'}
\quad \text { if \metavariable { e} is of the form } \exprterminal { \Lambda \metavariable { \alpha } \mapsto \metavariable { e'} }
\\
\exprterminal { \lambda \metavariable { x} : } \overline { \psi _ t} \metavariable { \tau } \exprterminal { \mapsto } \overline { \psi _ t} \metavariable { e'}
\quad \text { if \metavariable { e} is of the form } \exprterminal { \lambda \metavariable { x} : \metavariable { \tau } \mapsto \metavariable { e'} }
\\
\exprterminal { \lambda \metavariable { x} : } \overline { \psi _ t} \metavariable { \tau } \exprterminal { \mapsto _ \text { morph} } \overline { \psi _ t} \metavariable { e'}
\quad \text { if \metavariable { e} is of the form } \exprterminal { \lambda \metavariable { x} : \metavariable { \tau } \mapsto _ \text { morph} \metavariable { e'} }
\\
\overline { \psi _ t} \metavariable { e'} \overline { \psi _ t} \metavariable { \tau }
\quad \text { if \metavariable { e} is of the form }
\exprterminal { ( \metavariable { e'} \metavariable { \tau } )}
\\
\overline { \psi _ t} \metavariable { e_ 1} \overline { \psi _ t} \metavariable { e_ 2}
\quad \text { if \metavariable { e} is of the form }
\exprterminal { (\metavariable { e_ 1} \metavariable { e_ 2} )}
\\
\overline { \psi _ t} \metavariable { e'} \exprterminal { \text { as } } \overline { \psi _ t} \metavariable { \tau }
\quad \text { if \metavariable { e} is of the form }
\exprterminal { \metavariable { e'} \text { as } \metavariable { \tau } }
2024-07-24 11:23:51 +02:00
\end { cases} \]
\end { definition}
\begin { definition} [Substitution in Expressions]
\todo { complete}
Given an expression-variable assignment \( \psi _ e = \{ \metavariable { x _ 1 } \mapsto \metavariable { t _ 1 } , \quad \metavariable { x _ 2 } \mapsto \metavariable { t _ 2 } , \quad \dots \} \) ,
2024-08-08 13:17:34 +02:00
the thereby induced substitution \( \overline { \psi _ e } \) replaces all \emph { free} occurences of the expression variables \( \metavariable { x _ i } \)
in an expression \( e \in \nonterm { E } \) with the \( \psi _ e ( \metavariable { x _ i } ) \)
\[ \overline { \psi _ e } \metavariable { e } = \begin { cases }
\metavariable { e} \quad \text { if } \metavariable { e} \in \exprvars \text { and } \metavariable { e} \notin \text { Dom} (\psi _ e)\\
\metavariable { t} \quad \text { if } \metavariable { e} \in \exprvars \text { and } (\metavariable { e} \mapsto \metavariable { t} ) \in \psi _ e\\
\exprterminal { \text { let } \metavariable { x} \text { = } } \overline { \psi _ e} \metavariable { a} \exprterminal { \text { in } } \overline { \psi _ e} \metavariable { e'}
\quad \text { if }
\\
\exprterminal { \lambda \metavariable { x} :\metavariable { \tau } \mapsto } \overline { \psi _ e} \metavariable { e'}
\quad \text { if \metavariable { e} is of the form }
\exprterminal { \lambda \metavariable { x} :\metavariable { \tau } \mapsto \metavariable { e'} }
\text { and } \metavariable { x} \notin \text { Dom} (\psi _ e)
\\
\exprterminal { \lambda \metavariable { x} :\metavariable { \tau } \mapsto } \overline { \psi _ e \setminus \{ \metavariable { x} \mapsto \metavariable { t} \} } \metavariable { e'}
\quad \text { if \metavariable { e} is of the form }
\exprterminal { \lambda \metavariable { x} :\metavariable { \tau } \mapsto \metavariable { e'} }
\text { and } (\metavariable { x} \mapsto \metavariable { t} ) \in \psi _ e
\\
2024-07-24 11:23:51 +02:00
\end { cases} \]
\end { definition}
\subsection { Typing}
2024-08-08 13:18:36 +02:00
2024-07-24 11:23:51 +02:00
\subsubsection { Equivalence of Type Terms}
2024-08-08 13:18:36 +02:00
%We want distributivity of ladders over type-specialization as well as over function/morphism types.
\begin { definition} [Distributivity in Types]
\begin { mathpar}
\typeterminal { < \metavariable { \sigma } \sim \metavariable { \sigma '} \quad \metavariable { \tau } >}
\rightarrow _ \text { distribute}
\typeterminal { < \metavariable { \sigma } \quad \metavariable { \tau } > \sim < \metavariable { \sigma '} \quad \metavariable { \tau } > }
\typeterminal { < \metavariable { \sigma } \quad \metavariable { \tau } \sim \metavariable { \tau '} >}
\rightarrow _ \text { distribute}
\typeterminal { < \metavariable { \sigma } \quad \metavariable { \tau } > \sim < \metavariable { \sigma } \quad \metavariable { \tau '} > }
\typeterminal { \metavariable { \sigma } \sim \metavariable { \sigma '} \rightarrow \metavariable { \tau } }
\rightarrow _ \text { distribute}
\typeterminal { (\metavariable { \sigma } \rightarrow \metavariable { \tau } ) \sim ( \metavariable { \sigma '} \rightarrow \metavariable { \tau } ) }
\typeterminal { \metavariable { \sigma } \rightarrow \metavariable { \tau } \sim \metavariable { \tau '} }
\rightarrow _ \text { distribute}
\typeterminal { (\metavariable { \sigma } \rightarrow \metavariable { \tau } ) \sim ( \metavariable { \sigma } \rightarrow \metavariable { \tau '} ) }
\typeterminal { \metavariable { \sigma } \sim \metavariable { \sigma '} \rightarrow _ \text { morph} \metavariable { \tau } }
\rightarrow _ \text { distribute}
\typeterminal { (\metavariable { \sigma } \rightarrow _ \text { morph} \metavariable { \tau } ) \sim ( \metavariable { \sigma '} \rightarrow _ \text { morph} \metavariable { \tau } ) }
\typeterminal { \metavariable { \sigma } \rightarrow _ \text { morph} \metavariable { \tau } \sim \metavariable { \tau '} }
\rightarrow _ \text { distribute}
\typeterminal { (\metavariable { \sigma } \rightarrow _ \text { morph} \metavariable { \tau } ) \sim ( \metavariable { \sigma } \rightarrow _ \text { morph} \metavariable { \tau '} ) }
2024-08-07 15:59:03 +02:00
2024-08-08 13:18:36 +02:00
\end { mathpar}
Let \( \rightarrow _ \text { condense } \) be the inverse to \( \rightarrow _ \text { distribute } \) .
2024-08-07 15:59:03 +02:00
See \hyperref [coq:type-dist] { equiv.v:\ref { coq:type-dist} } .
2024-07-24 11:23:51 +02:00
\end { definition}
2024-08-08 13:18:36 +02:00
\begin { definition} [Alpha Conversion in Types]
\begin { mathpar}
\typeterminal { \forall \metavariable { \alpha } . \metavariable { \tau } }
\rightarrow _ { \alpha }
\typeterminal { \forall \metavariable { \alpha '} . } \{ \metavariable { \alpha } \mapsto \metavariable { \alpha '} \} \metavariable { \tau }
\end { mathpar}
\end { definition}
2024-07-24 11:23:51 +02:00
\begin { definition} [Equivalence Relation]
2024-08-08 13:18:36 +02:00
Transitive closure over \( \rightarrow _ \text { distribute } \) , \( \rightarrow _ \text { condense } \) and \( \rightarrow _ \alpha \) .
2024-08-07 15:59:03 +02:00
2024-08-08 13:18:36 +02:00
\begin { mathpar}
2024-08-22 09:05:20 +02:00
\inferrule [T-Eq-Refl] {
2024-08-08 13:18:36 +02:00
\metavariable { \tau } \in \nonterm { T}
} {
\metavariable { \tau } \equiv \metavariable { \tau }
} \and
2024-08-22 09:05:20 +02:00
\inferrule [T-Eq-Trans] {
2024-08-08 13:18:36 +02:00
\metavariable { \tau _ 1} \equiv \metavariable { \tau _ 2} \\
\metavariable { \tau _ 2} \equiv \metavariable { \tau _ 3}
} {
\metavariable { \tau _ 1} \equiv \metavariable { \tau _ 3}
}
2024-08-22 09:05:20 +02:00
\inferrule [T-Eq-Alpha] {
2024-08-08 13:18:36 +02:00
\metavariable { \tau _ 1} \rightarrow _ \alpha \metavariable { \tau _ 2}
} {
\metavariable { \tau _ 1} \equiv \metavariable { \tau _ 2}
}
2024-08-22 09:05:20 +02:00
\inferrule [T-Eq-Distribute] {
2024-08-08 13:18:36 +02:00
\metavariable { \tau _ 1} \rightarrow _ \text { distribute} \metavariable { \tau _ 2}
} {
\metavariable { \tau _ 1} \equiv \metavariable { \tau _ 2}
} \and
2024-08-22 09:05:20 +02:00
\inferrule [T-Eq-Condense] {
2024-08-08 13:18:36 +02:00
\metavariable { \tau _ 1} \rightarrow _ \text { condense} \metavariable { \tau _ 2}
} {
\metavariable { \tau _ 1} \equiv \metavariable { \tau _ 2}
}
\end { mathpar}
2024-08-07 15:59:03 +02:00
See \hyperref [coq:type-equiv] { equiv.v:\ref { coq:type-equiv} } .
2024-07-24 11:23:51 +02:00
\end { definition}
2024-08-08 13:18:36 +02:00
\begin { lemma} [Symmetry of \( \equiv \) ]
\begin { mathpar}
2024-08-22 09:05:20 +02:00
\inferrule [T-Eq-Symm] {
2024-08-08 13:18:36 +02:00
\metavariable { \tau _ 1} \equiv \metavariable { \tau _ 2}
} {
\metavariable { \tau _ 2} \equiv \metavariable { \tau _ 1}
}
\end { mathpar}
\begin { proof}
\( \rightarrow _ { distribute } \) is the inverse of \( \rightarrow _ { condense } \) and \( \rightarrow _ { \alpha } \) is symmetric by itself.
\end { proof}
\end { lemma}
2024-07-24 11:23:51 +02:00
\subsubsection { Normal Forms}
\begin { definition} [Ladder Normal Form]
2024-08-08 13:18:36 +02:00
LNF is reached by exhaustive application of \( \rightarrow _ \text { distribute } \) .
2024-07-24 11:23:51 +02:00
\end { definition}
2024-08-08 13:18:36 +02:00
\begin { definition} [Parameter Normal Form]
PNF reached by exhaustive application of \( \rightarrow _ \text { condense } \) .
\end { definition}
2024-07-24 11:23:51 +02:00
2024-08-08 13:19:17 +02:00
\subsubsection { Subtype Relations}
2024-08-22 09:05:20 +02:00
We define two relations: first the representation subtype relation \( \leq \) and second the transformation subtype relation \( \precsim \) .
2024-08-08 13:19:17 +02:00
2024-08-22 09:05:20 +02:00
\begin { definition} [Representation Subtype (\( \tau _ 1 \leq \tau _ 2 \) )]
2024-08-08 13:19:17 +02:00
\begin { mathpar}
2024-08-22 09:05:20 +02:00
\inferrule [TSubRepr-Refl] {
2024-08-08 13:19:17 +02:00
\metavariable { \tau } \equiv \metavariable { \tau '}
} {
\metavariable { \tau } \leq \metavariable { \tau '}
}
2024-08-22 09:05:20 +02:00
\inferrule [TSubRepr-Trans] {
\metavariable { \sigma } \leq \metavariable { \tau } \\
\metavariable { \tau } \leq \metavariable { \nu }
} {
\metavariable { \sigma } \leq \metavariable { \nu }
}
\inferrule [TSubRepr-Ladder] {
2024-08-08 13:19:17 +02:00
\metavariable { \sigma } \leq \metavariable { \tau }
} {
\metavariable { \sigma '} \typeterminal { \sim } \metavariable { \sigma } \leq \metavariable { \tau }
}
\end { mathpar}
2024-07-24 11:23:51 +02:00
\end { definition}
2024-08-22 09:05:20 +02:00
\begin { definition} [Transformation Subtype (\( \tau _ 1 \precsim \tau _ 2 \) )]
2024-08-08 13:19:17 +02:00
\begin { mathpar}
2024-08-22 09:05:20 +02:00
\inferrule [TSub-Refl] {
2024-08-08 13:19:17 +02:00
\metavariable { \tau } \equiv \metavariable { \tau '}
} {
\metavariable { \tau } \precsim \metavariable { \tau '}
2024-08-22 09:05:20 +02:00
} \and
\inferrule [TSub-Trans] {
\metavariable { \sigma } \precsim \metavariable { \tau } \\
\metavariable { \tau } \precsim \metavariable { \nu }
} {
\metavariable { \sigma } \precsim \metavariable { \nu }
2024-08-08 13:19:17 +02:00
}
2024-08-22 09:05:20 +02:00
\\
\inferrule [TSub-Ladder] {
2024-08-08 13:19:17 +02:00
\metavariable { \sigma } \precsim \metavariable { \tau }
} {
\metavariable { \sigma '} \typeterminal { \sim } \metavariable { \sigma } \precsim \metavariable { \tau }
2024-08-22 09:05:20 +02:00
} \and
\inferrule [TSub-Morph] {
2024-08-08 13:19:17 +02:00
\metavariable { \sigma } \equiv \metavariable { \tau }
} {
\metavariable { \sigma } \typeterminal { \sim } \metavariable { \sigma '} \precsim \metavariable { \tau } \typeterminal { \sim } \metavariable { \tau '}
}
\end { mathpar}
2024-07-24 11:23:51 +02:00
\end { definition}
2024-08-08 13:19:17 +02:00
2024-08-22 09:05:20 +02:00
\begin { example} [Representation \& Transformation Subtypes]$ \\ $
2024-08-08 13:19:17 +02:00
\begin { enumerate}
\item \typeterminal { <Digit 10> \( \sim \) Char \( \quad \leq \quad \) Char } \\
2024-08-22 09:05:20 +02:00
.. is a \emph { representation subtype} , because the representation of \typeterminal { <Digit 10>} is \emph { embedded} into \typeterminal { Char} .\\
2024-08-08 13:19:17 +02:00
\item \typeterminal { <Digit 10> \( \sim \) Char \( \quad \precsim \quad \) <Digit 10> \( \sim \) machine.UInt64} \\
2024-08-22 09:05:20 +02:00
.. is a \emph { transformation subtype} , because the \typeterminal { Char} based representation can be transformed into a representation based on \typeterminal { machine.UInt64} ,
2024-08-08 13:19:17 +02:00
while preserving its semantics.
\end { enumerate}
\end { example}
2024-09-05 12:46:24 +02:00
\subsubsection { Typing Context}
2024-07-24 11:23:51 +02:00
2024-08-08 13:20:18 +02:00
As usual, the typing-context \( \Gamma = \{ \metavariable { x _ 1 } : \metavariable { \tau _ 1 } , \quad \metavariable { x _ 2 } : \metavariable { \tau _ 2 } , \quad \ldots \} \)
is a finite mapping which assigns variables \( \metavariable { x _ i } \in \exprvars \) to types \( \metavariable { \tau _ i } \in \nonterm { T } \) .
2024-09-05 12:46:24 +02:00
%Using the inference rules given in \ref{def:pathrules} \ref{def:typerules}, further typing-judgements
%of the form \(\Gamma \vdash \metavariable{e} : \metavariable{\tau} \quad\) read as "expression \(\metavariable{e}\) is of type \(\metavariable{\tau}\)"
%can be derived from the context \(\Gamma\) where \(\metavariable{e} \in \nonterm{E}\) and \(\metavariable{\tau} \in \nonterm{T}\).
\subsubsection { Morphism Graph}
Every typing context \( \Gamma \) implies a \emph { Morphism Graph} , a directed graph whose vertices are types
and the edges represent a type-transformations, as defined by morphisms.
A type \( \metavariable { \tau } \) can be implicitly coerced into a type \( \metavariable { \tau ' } \) ,
provided there is a path from \( \metavariable { \tau } \) to \( \metavariable { \tau ' } \) in the \emph { Morphism-Graph} of \( \Gamma \) ,
written as \( \Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau ' } \) .
2024-08-08 13:20:18 +02:00
2024-09-05 12:46:24 +02:00
\begin { definition} [Morphism Paths]
2024-09-05 11:20:21 +02:00
\label { def:pathrules}
\begin { mathpar}
2024-07-24 11:23:51 +02:00
2024-09-05 11:20:21 +02:00
\inferrule [M-Sub] {
\metavariable { \tau } \leq \metavariable { \tau '}
} {
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '}
}
\inferrule [M-Single] {
(\metavariable { h} : \metavariable { \tau } \typeterminal { \rightarrow _ \text { morph} } \metavariable { \tau '} ) \in \Gamma
} {
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '}
}
\inferrule [M-Chain] {
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '} \\
\Gamma \vdash \metavariable { \tau '} \leadsto \metavariable { \tau ''}
} {
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau ''}
}
\inferrule [M-MapSeq] {
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '}
} {
\Gamma \vdash
\typeterminal { \langle \text { Seq } \metavariable { \tau } \rangle } \leadsto
\typeterminal { \langle \text { Seq } \metavariable { \tau '} \rangle }
}
\end { mathpar}
2024-07-24 11:23:51 +02:00
\end { definition}
2024-09-05 11:20:21 +02:00
\begin { example} [Morphism Graph]
Assume \( \Gamma : = \{ \\
\exprterminal { \text { degrees-to-turns} } : \typeterminal { \text { Angle} \sim \text { Degrees} \sim \mathbb { R} \rightarrow _ \text { morph} \text { Angle} \sim \text { Turns} \sim \mathbb { R} } ,\\
\exprterminal { \text { turns-to-radians} } : \typeterminal { \text { Angle} \sim \text { Turns} \sim \mathbb { R} \rightarrow _ \text { morph} \text { Angle} \sim \text { Radians} \sim \mathbb { R} } ,\\
\} \) .
Then
\begin { itemize}
\item \( \Gamma \vdash \typeterminal { \text { Angle } \sim \text { Degrees } \sim \mathbb { R } } \leadsto \typeterminal { \mathbb { R } } \) (by \textsc { M-Sub} )
\item \( \Gamma \vdash \typeterminal { \text { Angle } \sim \text { Degrees } \sim \mathbb { R } } \leadsto \typeterminal { \text { Angle } \sim \text { Radians } \sim \mathbb { R } } \) (by \textsc { M-Chain} )
\item \( \Gamma \vdash \typeterminal { \langle \text { Seq } \text { Angle } \sim \text { Degrees } \sim \mathbb { R } \rangle } \leadsto \typeterminal { \langle \text { Seq } \text { Angle } \sim \text { Radians } \sim \mathbb { R } \rangle } \) (by \textsc { M-MapSeq} )
\end { itemize}
\end { example}
2024-07-24 11:23:51 +02:00
2024-09-05 11:20:21 +02:00
\begin { definition} [Typing Relation]
2024-08-08 13:20:18 +02:00
\label { def:typerules}
2024-07-24 11:23:51 +02:00
\begin { mathpar}
\inferrule [T-Variable] {
\metavariable { x} :\metavariable { \tau } \in \Gamma \\
} {
\Gamma \vdash \metavariable { x} :\metavariable { \tau }
} \and
\inferrule [T-LetBinding] {
\Gamma \vdash \metavariable { e} : \metavariable { \sigma } \\
\Gamma , \metavariable { x} :\metavariable { \sigma } \vdash \metavariable { t} : \metavariable { \tau }
} {
\Gamma \vdash (\exprterminal { \text { let } } \metavariable { x} \exprterminal { \text { = } } \metavariable { e} \exprterminal { \text { in } } \metavariable { t} ) : \metavariable { \tau }
}
\inferrule [T-TypeAbs] {
\Gamma \vdash \metavariable { e} : \metavariable { \tau } \\
} {
\Gamma \vdash (\exprterminal { \Lambda } \metavariable { \alpha } \exprterminal { \mapsto } \metavariable { e} ) : \typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \metavariable { \tau }
}
\inferrule [T-TypeApp] {
2024-07-27 13:30:34 +02:00
\Gamma \vdash \metavariable { e} : \typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \metavariable { \tau } \\
2024-08-07 16:00:01 +02:00
\metavariable { \sigma } \in \nonterm { T}
2024-07-24 11:23:51 +02:00
} {
\Gamma \vdash ( \metavariable { e} \quad \metavariable { \sigma } ) : \{ \metavariable { \alpha } \mapsto \metavariable { \sigma } \} \metavariable { \tau }
}
2024-08-08 13:20:18 +02:00
\inferrule [T-Abs] {
\Gamma ,\metavariable { x} :\metavariable { \sigma } \vdash \metavariable { e} : \metavariable { \tau } \\
2024-07-24 11:23:51 +02:00
} {
\Gamma \vdash (\exprterminal { \lambda } \metavariable { x} \exprterminal { :} \metavariable { \sigma } \exprterminal { \mapsto } \metavariable { e} ) : \metavariable { \sigma } \typeterminal { \rightarrow } \metavariable { \tau }
}
\inferrule [T-MorphAbs] {
2024-08-08 13:20:18 +02:00
\Gamma ,\metavariable { x} :\metavariable { \tau } \vdash \metavariable { e} : \metavariable { \tau '} \\
\metavariable { \tau } \precsim \metavariable { \tau '}
2024-07-24 11:23:51 +02:00
} {
2024-08-08 13:20:18 +02:00
\Gamma \vdash (\exprterminal { \lambda } \metavariable { x} \exprterminal { :} \metavariable { \tau } \exprterminal { \mapsto _ { morph} } \metavariable { e} ) : \metavariable { \tau } \typeterminal { \rightarrow _ { morph} } \metavariable { \tau '}
2024-07-24 11:23:51 +02:00
} \and
2024-09-05 11:20:21 +02:00
\inferrule [T-App] {
\Gamma \vdash \metavariable { f} : \metavariable { \sigma } \typeterminal { \rightarrow } \metavariable { \tau } \\
\Gamma \vdash \metavariable { a} : \metavariable { \sigma '} \\
\Gamma \vdash \metavariable { \sigma '} \leadsto \metavariable { \sigma }
} {
\Gamma \vdash (\metavariable { f} \quad \metavariable { a} ) : \metavariable { \tau }
} \and
2024-08-08 13:20:18 +02:00
\inferrule [T-MorphFun] {
\Gamma \vdash \metavariable { f} : \metavariable { \sigma } \typeterminal { \rightarrow _ { morph} } \metavariable { \tau }
2024-07-24 11:23:51 +02:00
} {
2024-08-08 13:20:18 +02:00
\Gamma \vdash \metavariable { f} : \metavariable { \sigma } \typeterminal { \rightarrow } \metavariable { \tau }
2024-07-24 11:23:51 +02:00
} \and
\inferrule [T-Ascension] {
\Gamma \vdash \metavariable { e} : \metavariable { \tau } \\
\metavariable { \tau '} \leq \metavariable { \tau }
} {
\Gamma \vdash (\metavariable { e} \exprterminal { \text { as } } \metavariable { \tau '} ) : \metavariable { \tau '}
} \and
\inferrule [T-Descension] {
\Gamma \vdash \metavariable { e} : \metavariable { \tau } \\
\metavariable { \tau } \leq \metavariable { \tau '}
} {
\Gamma \vdash \metavariable { e} : \metavariable { \tau '}
2024-08-08 13:20:18 +02:00
}
\end { mathpar}
\end { definition}
2024-09-05 11:20:21 +02:00
\begin { definition} [Well-Typedness]
An expression \( \metavariable { e } \in \nonterm { E } \) is \textbf { well-typed} if there exist \( \Gamma \) and \( \metavariable { \tau } \)
such that \( \Gamma \vdash \metavariable { e } : \metavariable { \tau } \) by \ref { def:typerules} .
\end { definition}
\subsection { Coercion Semantics}
%We define the translation function \(\llbracket . \rrbracket\) which translates morphism-paths into
%expressions that define a transformation function, and also translates type-derivations into expressions with expanded type coercions.
%which completes a \emph{semantically well-typed} expression
%by inserting all required coercions based on the typing derivation of the expression.
%The result shall be a \emph{syntactically well-typed} expression.
We write \( C :: \tau \leadsto \tau ' \) to mean "C is a morphism-path derivation tree whose conclusion is \( \tau \leadsto \tau ' \) ".
Similarly, we write \( D :: \Gamma \vdash e : \tau \) to mean "D is a typing derivation whose conclusion is \( \Gamma \vdash e : \tau \) "
\begin { definition} [Morphism Translation]
%Translates a morphism-path derivation into an expression that defines a coercion function
2024-08-08 13:20:18 +02:00
\begin { mathpar}
2024-09-05 11:20:21 +02:00
\Big { \llbracket }
\inferrule [M-Sub] {
\metavariable { \tau } \leq \metavariable { \tau '}
2024-08-08 13:20:18 +02:00
} {
2024-09-05 11:20:21 +02:00
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '}
2024-08-08 13:20:18 +02:00
}
2024-09-05 11:20:21 +02:00
\Big { \rrbracket } = \exprterminal { \lambda x:\metavariable { \tau } \mapsto x}
\and
2024-08-08 13:20:18 +02:00
2024-09-05 11:20:21 +02:00
\Big { \llbracket }
\inferrule [M-Single] {
(\metavariable { h} : \metavariable { \tau } \typeterminal { \rightarrow _ \text { morph} } \metavariable { \tau '} ) \in \Gamma
2024-09-04 12:45:33 +02:00
} {
2024-09-05 11:20:21 +02:00
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '}
2024-09-04 12:45:33 +02:00
}
2024-09-05 11:20:21 +02:00
\Big { \rrbracket } = \metavariable { h}
\and
2024-09-04 12:45:33 +02:00
2024-09-05 11:20:21 +02:00
\Big { \llbracket }
\inferrule [M-Chain] {
C_ 1 :: \Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '} \\
C_ 2 :: \Gamma \vdash \metavariable { \tau '} \leadsto \metavariable { \tau ''}
2024-08-08 13:20:18 +02:00
} {
2024-09-05 11:20:21 +02:00
\Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau ''}
2024-08-08 13:20:18 +02:00
}
2024-09-05 11:20:21 +02:00
\Big { \rrbracket } = \exprterminal { \lambda \text { x} :\metavariable { \tau } \mapsto }
\Big { \llbracket } C_ 2 \Big { \rrbracket }
\exprterminal { (} \Big { \llbracket } C_ 1 \Big { \rrbracket } \exprterminal { \text { x} )}
\and
2024-08-08 13:20:18 +02:00
2024-09-05 11:20:21 +02:00
\Big { \llbracket }
\inferrule [M-MapSeq] {
C_ 1 :: \Gamma \vdash \metavariable { \tau } \leadsto \metavariable { \tau '}
2024-08-08 13:20:18 +02:00
} {
2024-09-05 11:20:21 +02:00
\Gamma \vdash
\typeterminal { \langle \text { Seq } \metavariable { \tau } \rangle } \leadsto
\typeterminal { \langle \text { Seq } \metavariable { \tau '} \rangle }
2024-08-08 13:20:18 +02:00
}
2024-09-05 11:20:21 +02:00
\Big { \rrbracket } = \exprterminal { \lambda \text { xs} :\typeterminal { \langle \text { Seq } \metavariable { \tau } \rangle } \mapsto }
\exprterminal { ( \text { map} } \Big { \llbracket } C_ 1 \Big { \rrbracket } \exprterminal { \text { xs} )}
2024-08-08 13:20:18 +02:00
\end { mathpar}
\end { definition}
2024-09-05 11:20:21 +02:00
\begin { definition} [Expression Translation]
%Translates a type-derivation tree into a fully expanded expression
2024-08-08 13:20:18 +02:00
\begin { mathpar}
2024-09-05 11:20:21 +02:00
\Big { \llbracket } \inferrule [T-Variable] {
\metavariable { x} :\metavariable { \tau } \in \Gamma
2024-08-08 13:20:18 +02:00
} {
2024-09-05 11:20:21 +02:00
\Gamma \vdash \metavariable { x} :\metavariable { \tau }
} \Big { \rrbracket } = \metavariable { x}
\and
2024-08-08 13:20:18 +02:00
2024-09-05 11:20:21 +02:00
\Big { \llbracket } \inferrule [T-LetBinding] {
D_ 1 ::\Gamma \vdash \metavariable { e} : \metavariable { \sigma } \\
D_ 2 :: \Gamma , \metavariable { x} :\metavariable { \sigma } \vdash \metavariable { t} : \metavariable { \tau }
2024-08-08 13:20:18 +02:00
} {
2024-09-05 11:20:21 +02:00
\Gamma \vdash (\exprterminal { \text { let } } \metavariable { x} \exprterminal { \text { = } } \metavariable { e} \exprterminal { \text { in } } \metavariable { t} ) : \metavariable { \tau }
} \Big { \rrbracket } = \exprterminal { \text { let } \metavariable { x} = }
\Big { \llbracket } D_ 1 \Big { \rrbracket }
\exprterminal { \text { in } }
\Big { \llbracket } D_ 2 \Big { \rrbracket }
\and
\Big { \llbracket }
\inferrule [T-TypeAbs] {
D_ 1 :: \Gamma \vdash \metavariable { e} : \metavariable { \tau } \\
} {
\Gamma \vdash (\exprterminal { \Lambda } \metavariable { \alpha } \exprterminal { \mapsto } \metavariable { e} ) : \typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \metavariable { \tau }
}
\Big { \rrbracket } = \exprterminal { \Lambda \metavariable { \alpha } \mapsto } \Big { \llbracket } D_ 1 \Big { \rrbracket }
\and
\Big { \llbracket }
\inferrule [T-TypeApp] {
D_ 1 :: \Gamma \vdash \metavariable { e} : \typeterminal { \forall } \metavariable { \alpha } \typeterminal { .} \metavariable { \tau } \\
\metavariable { \sigma } \in \nonterm { T}
} {
\Gamma \vdash ( \metavariable { e} \quad \metavariable { \sigma } ) : \{ \metavariable { \alpha } \mapsto \metavariable { \sigma } \} \metavariable { \tau }
}
\Big { \rrbracket } =
\exprterminal { (}
\Big { \llbracket }
D_ 1
\Big { \rrbracket }
\metavariable { \sigma }
2024-08-08 13:20:18 +02:00
\exprterminal { )}
2024-09-05 11:20:21 +02:00
\and
2024-08-08 13:20:18 +02:00
\Big { \llbracket }
2024-09-05 11:20:21 +02:00
\inferrule [T-Abs] {
D_ 1 :: \Gamma ,\metavariable { x} :\metavariable { \sigma } \vdash \metavariable { e} : \metavariable { \tau } \\
} {
\Gamma \vdash (\exprterminal { \lambda } \metavariable { x} \exprterminal { :} \metavariable { \sigma } \exprterminal { \mapsto } \metavariable { e} ) : \metavariable { \sigma } \typeterminal { \rightarrow } \metavariable { \tau }
}
\Big { \rrbracket } =
\exprterminal { \lambda } \metavariable { x} \exprterminal { :} \metavariable { \sigma }
\exprterminal { \mapsto } \Big { \llbracket } D_ 1\Big { \rrbracket }
\and
2024-07-24 11:23:51 +02:00
2024-09-05 12:46:24 +02:00
\Big { \llbracket }
\inferrule [T-MorphAbs] {
D_ 1 :: \Gamma ,\metavariable { x} :\metavariable { \tau } \vdash \metavariable { e} : \metavariable { \tau '} \\
\metavariable { \tau } \precsim \metavariable { \tau '}
} {
\Gamma \vdash (\exprterminal { \lambda } \metavariable { x} \exprterminal { :} \metavariable { \tau } \exprterminal { \mapsto _ { morph} } \metavariable { e} ) : \metavariable { \tau } \typeterminal { \rightarrow _ \text { morph} } \metavariable { \tau '}
}
\Big { \rrbracket } =
\exprterminal { \lambda } \metavariable { x} \exprterminal { :} \metavariable { \tau }
\exprterminal { \mapsto _ \text { morph} } \Big { \llbracket } D_ 1\Big { \rrbracket }
\and
2024-09-05 11:20:21 +02:00
\Big { \llbracket }
\inferrule [T-App] {
D_ 1 :: \Gamma \vdash \metavariable { f} : \metavariable { \sigma } \typeterminal { \rightarrow } \metavariable { \tau } \\
D_ 2 :: \Gamma \vdash \metavariable { a} : \metavariable { \sigma '} \\ \\
C :: \Gamma \vdash \metavariable { \sigma '} \leadsto \metavariable { \sigma }
} {
\Gamma \vdash (\metavariable { f} \quad \metavariable { a} ) : \metavariable { \tau }
}
\Big { \rrbracket } =
\Big { \llbracket } D_ 1\Big { \rrbracket }
\exprterminal { (}
\Big { \llbracket } C\Big { \rrbracket }
\Big { \llbracket } D_ 2\Big { \rrbracket }
\exprterminal { )}
\and
2024-08-08 13:20:18 +02:00
2024-09-05 11:20:21 +02:00
\Big { \llbracket }
\inferrule [T-MorphFun] {
D_ 1 :: \Gamma \vdash \metavariable { f} : \metavariable { \sigma } \typeterminal { \rightarrow _ { morph} } \metavariable { \tau }
} {
\Gamma \vdash \metavariable { f} : \metavariable { \sigma } \typeterminal { \rightarrow } \metavariable { \tau }
}
\Big { \rrbracket } = \Big { \llbracket } D_ 1 \Big { \rrbracket }
\and
\Big { \llbracket }
\inferrule [T-Ascension] {
D_ 1 :: \Gamma \vdash \metavariable { e} : \metavariable { \tau } \\
\metavariable { \tau '} \leq \metavariable { \tau }
} {
\Gamma \vdash (\metavariable { e} \exprterminal { \text { as } } \metavariable { \tau '} ) : \metavariable { \tau '}
}
\Big { \rrbracket } =
\Big { \llbracket } D_ 1\Big { \rrbracket } \exprterminal { \text { as } } \metavariable { \tau '}
\and
\Big { \llbracket }
\inferrule [T-Descension] {
D_ 1 :: \Gamma \vdash \metavariable { e} : \metavariable { \tau } \\
\metavariable { \tau } \leq \metavariable { \tau '}
} {
\Gamma \vdash \metavariable { e} : \metavariable { \tau '}
}
\Big { \rrbracket } =
\Big { \llbracket }
D_ 1
\Big { \rrbracket }
\end { mathpar}
\end { definition}
2024-08-08 13:20:18 +02:00
2024-07-24 11:23:51 +02:00
2024-08-07 16:00:01 +02:00
\subsection { Evaluation}
2024-07-24 11:23:51 +02:00
2024-08-08 13:20:18 +02:00
Evaluation of an expression \( \metavariable { e } \in \nonterm { E } \) is defined by exhaustive application of the rewrite rule \( \rightarrow _ \beta \) as in \ref { def:evalrules} .
2024-07-24 11:23:51 +02:00
\begin { definition} [Inference Rules for Evaluation]
\label { def:evalrules}
\begin { mathpar}
\inferrule [E-App1] {
\metavariable { e_ 1} \rightarrow _ \beta \metavariable { e_ 1'}
} {
\metavariable { e_ 1} \metavariable { e_ 2}
\rightarrow _ \beta
\metavariable { e_ 1'} \metavariable { e_ 2}
} \and
\inferrule [E-App2] {
\metavariable { e_ 2} \rightarrow _ \beta \metavariable { e_ 2'}
} {
2024-08-08 13:20:18 +02:00
\metavariable { v_ 1} \metavariable { e_ 2}
2024-07-24 11:23:51 +02:00
\rightarrow _ \beta
2024-08-08 13:20:18 +02:00
\metavariable { v_ 1} \metavariable { e_ 2'}
} \and
2024-07-24 11:23:51 +02:00
\inferrule [E-TypApp] {
2024-08-08 13:20:18 +02:00
% \metavariable{\tau} \in \typenonterm{\emptyset}\\
2024-07-24 11:23:51 +02:00
\metavariable { e} \rightarrow _ \beta \metavariable { e'}
} {
\metavariable { e}
\metavariable { \tau }
\rightarrow _ \beta
\metavariable { e'}
\metavariable { \tau }
}
\newline
\inferrule [E-TypAppLam] {
} {
\exprterminal { (\Lambda } \metavariable { \alpha }
\exprterminal { \mapsto } \metavariable { e}
\exprterminal { )}
\metavariable { \tau }
\rightarrow _ \beta
\{ \metavariable { \alpha } \mapsto \metavariable { \tau } \} \metavariable { e}
} \and
\inferrule [E-AppLam] {
} {
\exprterminal { (\lambda } \metavariable { x}
\exprterminal { :} \metavariable { \sigma }
\exprterminal { \mapsto } \metavariable { e}
\exprterminal { )}
\metavariable { a}
\rightarrow _ \beta
\{ \metavariable { x} \mapsto \metavariable { a} \} \metavariable { e}
} \and
\inferrule [E-AppLet] {
} {
\exprterminal { \text { let } } \metavariable { x}
\exprterminal { \text { = } } \metavariable { a}
\exprterminal { \text { in } } \metavariable { e}
\rightarrow _ \beta
\{ \metavariable { x} \mapsto \metavariable { a} \} \metavariable { e}
2024-09-04 12:45:33 +02:00
}
2024-09-05 12:46:24 +02:00
2024-09-04 12:45:33 +02:00
\inferrule [E-AppLamAscribe] {
2024-07-24 11:23:51 +02:00
} {
2024-09-05 12:46:24 +02:00
\exprterminal { (( \lambda \metavariable { x} :\metavariable { \sigma } \mapsto \metavariable { e} )}
2024-08-08 13:20:18 +02:00
\exprterminal { \text { as } }
2024-09-04 12:45:33 +02:00
\typeterminal { \metavariable { \tau } }
2024-09-05 12:46:24 +02:00
\exprterminal { )}
\metavariable { a}
2024-08-08 13:20:18 +02:00
\rightarrow _ \beta
2024-09-05 12:46:24 +02:00
\exprterminal { ( \lambda \metavariable { x} :\metavariable { \sigma } \mapsto \metavariable { e} )}
\metavariable { a}
2024-07-24 11:23:51 +02:00
}
\end { mathpar}
\end { definition}
2024-08-07 16:00:01 +02:00
\subsection { Soundness}
2024-07-27 13:30:34 +02:00
2024-07-24 11:23:51 +02:00
\begin { lemma} [Preservation]
\label { lemma:preservation}
2024-09-05 12:46:24 +02:00
Assume the expression \( \metavariable { e } \) is well typed,
i.e. there is a type-derivation tree
\( D :: \Gamma \vdash \metavariable { e } : \metavariable { \tau } \)
for some type \( \metavariable { \tau } \) and context \( \Gamma \) .
Then forall \( \metavariable { e' } \) with \( \llbracket D \rrbracket \rightarrow _ { \beta } \metavariable { e' } \)
it holds that \( \Gamma \vdash \metavariable { e' } : \metavariable { \tau } \) as well.
2024-07-24 11:23:51 +02:00
\begin { proof}
\todo { }
\end { proof}
\end { lemma}
2024-07-27 13:30:34 +02:00
\begin { lemma} [Progress]
\label { lemma:progress}
2024-09-05 12:46:24 +02:00
Assume the expression \( \metavariable { e } \) is well typed,
i.e. there is a type-derivation tree
\( D :: \Gamma \vdash \metavariable { e } : \metavariable { \tau } \)
for some type \( \metavariable { \tau } \) and context \( \Gamma \) .
Then either \( \metavariable { e } \) is a value
or there exists some \( \metavariable { e' } \) such that \( \llbracket D \rrbracket \rightarrow _ { \beta } \metavariable { e' } \)
2024-07-24 11:23:51 +02:00
2024-07-27 13:30:34 +02:00
\begin { proof}
\todo { }
\end { proof}
\end { lemma}
2024-07-24 11:23:51 +02:00
2024-09-05 12:46:24 +02:00
\begin { theorem} [Soundness]
2024-08-08 13:20:18 +02:00
\label { theorem:semantic-soundness}
2024-09-05 12:46:24 +02:00
No well-typed expression is stuck.
2024-08-08 13:20:18 +02:00
2024-09-05 12:46:24 +02:00
Assume the typing derivation \( D :: \Gamma \vdash \metavariable { e } : \metavariable { \tau } \) .
2024-08-08 13:20:18 +02:00
Then it never occurs that \( \llbracket D \rrbracket \rightarrow _ { \beta } ^ { * } \metavariable { e' } \) where \metavariable { e'} is in normal form but not a value.
\begin { proof}
2024-09-05 12:46:24 +02:00
\todo { }
%Assume the typing derivation \(D :: \Gamma \vdash \metavariable{e}:\approx\metavariable{\tau}\).
%By \ref{lemma:translation}, \(\Gamma \vdash \llbracket D \rrbracket : \metavariable{\tau}\)
%and thus it follows by \ref{theorem:syntactic-soundness} that \metavariable{e} is not stuck.
2024-08-08 13:20:18 +02:00
\end { proof}
\end { theorem}
2024-07-24 11:23:51 +02:00
2024-08-08 13:20:18 +02:00
\newpage
2024-07-24 11:23:51 +02:00
\section { Boehm-Berarducci Encoding}
\newcommand { \typvar } [1]{ \textcolor { Emerald} { #1} }
\newcommand { \expvar } [1]{ \textcolor { MidnightBlue} { #1} }
\subsection { Booleans}
\begin { definition}
Let \( \Sigma = \{ \text { Bool } , \text { BB - Bool } \} \) .
\begin { itemize}
\item let { \textcolor { magenta} { bb-true} } = \exprterminal { \( ( \Lambda \typvar { \alpha } \mapsto \lambda \expvar { t } : \typvar { \alpha } \mapsto \lambda \expvar { f } : \typvar { \alpha } \mapsto \expvar { t } ) \) as} \typeterminal { Bool \( \sim \) BB-Bool}
\item let { \textcolor { magenta} { bb-false} } = \exprterminal { \( ( \Lambda \typvar { \alpha } \mapsto \lambda \expvar { t } : \typvar { \alpha } \mapsto \lambda \expvar { f } : \typvar { \alpha } \mapsto \expvar { f } ) \) as} \typeterminal { Bool \( \sim \) BB-Bool}
\end { itemize}
\end { definition}
\begin { lemma}
{ \textcolor { magenta} { bb-true} } and { \textcolor { magenta} { bb-false} } are \emph { values} .
\begin { proof}
\todo { }
\end { proof}
\end { lemma}
\subsection { Conditionals}
\begin { itemize}
\item let { \textcolor { magenta} { bb-if} } = \exprterminal {
\( \Lambda \typvar { \alpha } \) \\
\( \mapsto \lambda \) \expvar { cond} : \typeterminal { Bool \( \sim \) BB-Bool \( \sim \forall \typvar { \beta } . ( \typvar { \beta } \rightarrow \typvar { \beta } \rightarrow \typvar { \beta } ) \) } \\
\( \mapsto \lambda \) \expvar { then} : \( \typvar { \alpha } \) \\
\( \mapsto \lambda \) \expvar { else} : \( \typvar { \alpha } \) \\
\( \mapsto \) \expvar { cond} \typvar { \( \alpha \) } \expvar { then} \expvar { else}
}
\end { itemize}
\subsection { Natural Numbers}
\begin { definition}
Let \( \Sigma = \{ \mathbb { N } , \text { BB - Nat } \} \) .
\begin { itemize}
\item let { \textcolor { magenta} { bb-zero} } = \( \exprterminal { ( \Lambda \typvar { \alpha } \mapsto \lambda \expvar { s } : \typeterminal { \typvar { \alpha } \rightarrow \typvar { \alpha } } \mapsto \lambda \expvar { z } : \typvar { \alpha } \mapsto \expvar { z } ) \text { as } } \)
\typeterminal { \( \mathbb { N } \sim \text { BB - Nat } \sim \forall \typvar { \alpha } . ( \typvar { \alpha } \rightarrow \typvar { \alpha } ) \rightarrow \typvar { \alpha } \rightarrow \typvar { \alpha } \) }
\item let { \textcolor { magenta} { bb-one} } = \( \exprterminal { ( \Lambda \typvar { \alpha } \mapsto \lambda \expvar { s } : \typeterminal { \typvar { \alpha } \rightarrow \typvar { \alpha } } \mapsto \lambda \expvar { z } : \typvar { \alpha } \mapsto \expvar { s } \expvar { z } ) \text { as } } \)
\typeterminal { \( \mathbb { N } \sim \text { BB - Nat } \sim \forall \typvar { \alpha } . ( \typvar { \alpha } \rightarrow \typvar { \alpha } ) \rightarrow \typvar { \alpha } \rightarrow \typvar { \alpha } \) }
\item let { \textcolor { magenta} { bb-two} } = \( \exprterminal { ( \Lambda \typvar { \alpha } \mapsto \lambda \expvar { s } : \typeterminal { \typvar { \alpha } \rightarrow \typvar { \alpha } } \mapsto \lambda \expvar { z } : \typvar { \alpha } \mapsto \expvar { s } ( \expvar { s } \expvar { z } ) ) \text { as } } \)
\typeterminal { \( \mathbb { N } \sim \text { BB - Nat } \sim \forall \typvar { \alpha } . ( \typvar { \alpha } \rightarrow \typvar { \alpha } ) \rightarrow \typvar { \alpha } \rightarrow \typvar { \alpha } \) }
\end { itemize}
\end { definition}
\begin { definition}
The successor function is given by:
let { \textcolor { magenta} { bb-succ} } =
\newline \( \exprterminal { \quad \lambda \expvar { n } :
\typeterminal { \mathbb { N}
\sim \text { BB-Nat}
\sim \forall \typvar { \alpha } . ((\typvar { \alpha } \rightarrow \typvar { \alpha } ) \rightarrow \typvar { \alpha } \rightarrow \typvar { \alpha } )} \\
} \)
\newline
\( \exprterminal { \quad \quad \mapsto ( \Lambda \typvar { \alpha } \mapsto \lambda \expvar { s } : \typeterminal { ( \typvar { \alpha } \rightarrow \typvar { \alpha } ) } \mapsto \lambda \expvar { z } : \typvar { \alpha } } \)
\( \exprterminal { \mapsto \expvar { s } ( \expvar { n } \typvar { \alpha } \expvar { s } \expvar { z } ) ) \text { as } } \)
\typeterminal {
\( \mathbb { N } \sim \text { BB - Nat } \sim \forall \typvar { \alpha } . ( \typvar { \alpha } \rightarrow \typvar { \alpha } ) \rightarrow \typvar { \alpha } \rightarrow \typvar { \alpha } \)
}
\end { definition}
\begin { example}
\( \emptyset \vdash ( \textcolor { magenta } { \text { bb - succ } \quad \text { bb - one } } ) : \typeterminal { \mathbb { N } \sim \text { BB - Nat } \sim \forall \typvar { \alpha } . ( \typvar { \alpha } \rightarrow \typvar { \alpha } ) \rightarrow \typvar { \alpha } \rightarrow \typvar { \alpha } ) } \)
\end { example}
\begin { example}
\textcolor { magenta} { bb-succ bb-one} \( \rightarrow _ \beta ^ { * } \) \textcolor { magenta} { bb-two} .
\end { example}
\begin { example}
\exprterminal { (} \textcolor { magenta} { bb-succ bb-false} \exprterminal { )} is stuck. \( \nexists \metavariable { v } . \) \exprterminal { (} \textcolor { magenta} { bb-succ bb-false} \exprterminal { )} \( \rightarrow _ \text { eval } \metavariable { v } \) where \( \metavariable { v } \) is a \emph { value} .
\end { example}
\begin { example}
\exprterminal { (} \textcolor { magenta} { bb-succ bb-false} \exprterminal { )} is \emph { not} well-typed. \( \nexists \metavariable { \tau } . \emptyset \vdash \) \textcolor { magenta} { bb-succ bb-false} : \( \metavariable { \tau } \) .
\end { example}
\subsection { Pairs}
\begin { definition}
pairs
\begin { itemize}
\item let { \textcolor { magenta} { bb-pair} } = \( \exprterminal { \Lambda \typvar { \alpha } \mapsto \Lambda \typvar { \beta } \mapsto \lambda \expvar { a } : \typvar { \alpha } \mapsto \lambda \expvar { b } : \typvar { \beta } } \) \\
\( \exprterminal {
\mapsto (\Lambda \typvar { \gamma } \mapsto \lambda \expvar { s} : \typeterminal { \typvar { \alpha } \rightarrow \typvar { \beta } \rightarrow \typvar { \gamma } } \mapsto \expvar { s} \expvar { a} \expvar { b} )} \)
\exprterminal { as } \typeterminal { \( ( \typvar { \alpha } \times \typvar { \beta } ) \) \( \sim \) <BB-Pair \( \typvar { \alpha } \text { } \typvar { \beta } \) > \( \sim \) \( \forall \typvar { \gamma } . ( ( \typvar { \alpha } \rightarrow \typvar { \beta } \rightarrow \typvar { \gamma } ) \rightarrow \typvar { \gamma } ) \) } \\
\item let { \textcolor { magenta} { bb-pair-first} } = \( \exprterminal {
\Lambda \typvar { \alpha } \mapsto \Lambda \typvar { \beta } \\
\mapsto \lambda \expvar { p} : \typeterminal {
(\typvar { \alpha } \times \typvar { \beta } ) \sim \text { <BB-Pair \( \typvar { \alpha } \text { } \typvar { \beta } \) > } \sim \forall \typvar { \gamma } . ((\typvar { \alpha } \rightarrow \typvar { \beta } \rightarrow \typvar { \gamma } ) \rightarrow \typvar { \gamma } )
} } \) \\
\( \exprterminal {
\mapsto \expvar { p} \typvar { \alpha } (\lambda \expvar { a} :\typvar { \alpha } \mapsto \lambda \expvar { b} :\typvar { \beta } \mapsto \expvar { a} )
} \) \\
\item let { \textcolor { magenta} { bb-pair-second} } = \( \exprterminal {
\Lambda \typvar { \alpha } \mapsto \Lambda \typvar { \beta }
\mapsto \lambda \expvar { p} : \typeterminal {
(\typvar { \alpha } \times \typvar { \beta } ) \sim \text { <BB-Pair \( \typvar { \alpha } \text { } \typvar { \beta } \) > } \sim \forall \typvar { \gamma } . ((\typvar { \alpha } \rightarrow \typvar { \beta } \rightarrow \typvar { \gamma } ) \rightarrow \typvar { \gamma } )
} } \) \\
\( \exprterminal {
\mapsto \expvar { p} \typvar { \beta } (\lambda \expvar { a} :\typvar { \alpha } \mapsto \lambda \expvar { b} :\typvar { \beta } \mapsto \expvar { b} )
} \) \\
\end { itemize}
\end { definition}
\subsection { Lists}
\subsection { Algebraic Datatypes}
\todo { }
2024-08-07 15:59:03 +02:00
\input { appendix}
2024-07-24 11:23:51 +02:00
\end { document}