ladder-calculus/beamer/lc-beamer/example.tex
2024-09-19 14:17:48 +02:00

454 lines
12 KiB
TeX
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{formal-grammar}
\usepackage[dvipsnames]{xcolor}
\usepackage{mathpartir}
\usepackage{stmaryrd}
\newcommand{\metavariable}[1]{\textcolor{teal}{#1}}
\newcommand{\typeterminal}[1]{\textcolor{brown}{#1}}
\newcommand{\exprterminal}[1]{\textcolor{Sepia}{#1}}
\title{A functional core calculus with ladder-types}
%\date[ISPN 80]{27th International Symposium of Prime Numbers}
\author[Euclid]{Michael Sippel \texttt{michael.sippel@mailbox.tu-dresden.de}\inst{1}}
\institute{\inst{1} Technische Universität Dresden}
\usetheme{ccc}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Type Systems}
\framesubtitle{Tension between Safety and Flexibility}
\begin{itemize}
\item<1-> Goal: eliminate undefined behaviour,
facilitate abstract code
\item<2-> simple types are overly restrictive,
loss of low level control
\item<3-|alert@3> polymorphism
(regain flexibility in type-safe way)
\begin{itemize}
\item<4-> parametric polymorphism (generics)
\item<4-> subtype polymorphism (inheritance / coercions)
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{How to get fast code?}
\framesubtitle{Requirements for a type system}
\begin{itemize}
\item<1-> algorithmic optimization
\begin{itemize}
\item see which methods are used most frequently
\item switch structures to reduce complexity there
\item identify special cases
\end{itemize}
\item<2-> optimize implementation detail:
\begin{itemize}
\item remove indirection
\item bottleneck? memory!\\
(plenty of Main-memory, but FAST memory is scarce\\
\(\rightarrow\) improve cache \& bus efficiency with compact representations)
\begin{itemize}
\item<3-> Balance De-/Encode Overhead
vs. Fetch-Overhead
\item<4-> Synchronize locality in access order and memory layout.
-> elements accessed in succession shall be close in memory.
(e.g. AoS vs SoA)
\item<5-> accounting for machine specific cache-sizes
\item<6-> accounting for SIMD instructions
\item<7-> interfacing with kernels running on accelerator devices
\end{itemize}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Abstract Concept - Multiple Representations}
\framesubtitle{Requirements for a type system}
\begin{itemize}
\item
\end{itemize}
\end{frame}
\begin{frame}{Coercions: (In-)Coherence with Transitivity}
\begin{itemize}
\item<1-> Common example: coerce \texttt{Int -> Real}
(subsumption \(\mathbb{N} \subset \mathbb{R}\))
\begin{itemize}
\item \texttt{Int -> Real}
\item \texttt{Int -> String}
\item \texttt{Real -> String}
\end{itemize}
\item<2-> transitivity creates two extensionally \emph{different} coercion paths:
\begin{itemize}
\item \texttt{Int -> String} (3 -> "3")
\item \texttt{Int -> Real -> String} (3 -> "3.0")
\end{itemize}
\item<3-> Subsumptive interpretation is misleading!
\item<3-> \texttt{Int -> Real} is not as unambiguous as it might seem.
\begin{itemize}
\item value ranges (e.g. map int to unit interval [0,1])
\item multiple quantization functions (linear, exponential, \dots)
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Sea of Types}
\includegraphics[width=\textwidth]{sot-int.png}
\end{frame}
\begin{frame}{Sea of Types}
\includegraphics[width=\textwidth]{sot-json.png}
\end{frame}
\begin{frame}{Ladder Types}
\begin{block}{Intuition}
Encode "represented-as" relation into type terms
\end{block}
\begin{block}{}
\( \tau_1 \sim \tau_2 \) reads "\(\tau_1\) represented as \(\tau_2\)"
\end{block}
\begin{example}\(
Color \sim sRGB
\sim HSV \sim \langle \text{Vec3} \quad \mathbb{R}_{[0,1]}
\sim \langle \text{QuantizeLinear} \quad 256 \rangle
\sim \mathbb{Z}_{256} \sim \text{machine.UInt8} \sim \text{Byte}
\rangle
\sim \text{[Byte; 3]}
\)\end{example}
\end{frame}
\begin{frame}{Extending System F}
\small
\begin{grammar}
\firstcase{ T }{
\metavariable{\sigma}
}{Base Type}
\otherform{
\metavariable{\alpha}
}{Type Variable}
\otherform{
\typeterminal{\forall} \metavariable{\alpha} \typeterminal{.} \nonterm{T}
}{Universal Type}
\otherform{
\typeterminal{<} \nonterm{T} \quad \nonterm{T} \typeterminal{>}
}{Specialized Type}
\otherform{
\nonterm{T} \quad \typeterminal{\rightarrow} \quad \nonterm{T}
}{Function Type}
\otherform{
\nonterm{T} \quad \typeterminal{\rightarrow_\text{morph}} \quad \nonterm{T}
}{Morphism Type}
\otherform{
\nonterm{T} \quad \typeterminal{\sim} \quad \nonterm{T}
}{Ladder Type}
$$\\$$
\firstcase{ E
% T_\selexpr
}
{ \metavariable{x}
} {Variable}
\otherform{
\exprterminal{let} \quad \metavariable{x} \quad \exprterminal{=} \quad
\nonterm{ E }
\quad \exprterminal{in} \quad
\nonterm{ E }
}{Variable Binding}
\otherform{
$$ \exprterminal{\Lambda} \metavariable{\alpha}
\quad \exprterminal{\mapsto} \quad $$
\nonterm{ E }
}{Type Abstraction}
\otherform{
$$ \exprterminal{\lambda} \metavariable{x} $$
\exprterminal{:} \nonterm{ T }
\quad $$\exprterminal{\mapsto}$$ \quad
\nonterm{ E }
}{Value Abstraction}
\otherform{
$$ \exprterminal{\lambda} \metavariable{x} $$
\exprterminal{:} \nonterm{ T }
\quad $$\exprterminal{\mapsto_\text{morph}}$$ \quad
\nonterm{ E }
}{Value Morphism}
\otherform{
\nonterm{ E }
\quad
\nonterm{ T }
}{Type Application}
\otherform{
\nonterm{ E }
\quad
\nonterm{ E }
}{Value Application}
\otherform{
\nonterm{ E }
\quad
\exprterminal{as}
\quad
\nonterm{ T }
}{Ascension}
\otherform{
\nonterm{ E }
\quad
\exprterminal{des}
\quad
\nonterm{ T }
}{Descension}
\end{grammar}
\end{frame}
\begin{frame}{Morphism Graph}
\begin{definition}[Morphism Paths]
\label{def:pathrules}
\begin{mathpar}
\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}
\end{definition}
\end{frame}
\begin{frame}{Morphism Graph}
\begin{example}[Morphism Graph]
\begin{mathpar}
\small
\text{Assume }
\Gamma := \{\\
\exprterminal{\text{degrees-to-turns}} : \typeterminal{\text{Angle}\sim\text{Degrees}\sim\mathbb{R} \quad \rightarrow_\text{morph} \quad \text{Angle}\sim\text{Turns}\sim\mathbb{R}},\\
\exprterminal{\text{turns-to-radians}} : \typeterminal{\text{Angle}\sim\text{Turns}\sim\mathbb{R} \quad \rightarrow_\text{morph} \quad \text{Angle}\sim\text{Radians}\sim\mathbb{R}},\\
\}.
\end{mathpar}
\end{example}
\end{frame}
\begin{frame}{Morphism Graph}
\begin{example}[Morphism Graph]
Then ..
\small
\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}
\end{frame}
\begin{frame}{Typing}
\begin{definition}[Typing Relation]
\begin{mathpar}
\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}
}
\end{mathpar}
\end{definition}
\end{frame}
\begin{frame}{Translation}
\begin{definition}[Morphism Translation]
\begin{mathpar}
\small
\Big{\llbracket}
\inferrule[M-Sub]{
\metavariable{\tau} \leq \metavariable{\tau'}
}{
\Gamma \vdash \metavariable{\tau} \leadsto \metavariable{\tau'}
}
\Big{\rrbracket} = \exprterminal{\lambda x:\metavariable{\tau} \mapsto x}
\and
\Big{\llbracket}
\inferrule[M-Single]{
(\metavariable{h} : \metavariable{\tau} \typeterminal{\rightarrow_\text{morph}} \metavariable{\tau'}) \in \Gamma
}{
\Gamma \vdash \metavariable{\tau} \leadsto \metavariable{\tau'}
}
\Big{\rrbracket} = \metavariable{h}
\and
\end{mathpar}
\end{definition}
\end{frame}
\begin{frame}{Translation}
\begin{definition}[Morphism Translation (2)]
\begin{mathpar}
\small
\Big{\llbracket}
\inferrule[M-Chain]{
C_1 :: \Gamma \vdash \metavariable{\tau} \leadsto \metavariable{\tau'}\\
C_2 :: \Gamma \vdash \metavariable{\tau'} \leadsto \metavariable{\tau''}
}{
\Gamma \vdash \metavariable{\tau} \leadsto \metavariable{\tau''}
}
\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
\Big{\llbracket}
\inferrule[M-MapSeq]{
C_1 :: \Gamma \vdash \metavariable{\tau} \leadsto \metavariable{\tau'}
}{
\Gamma \vdash
\typeterminal{\langle\text{Seq } \metavariable{\tau}\rangle} \leadsto
\typeterminal{\langle\text{Seq } \metavariable{\tau'}\rangle}
}
\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})}
\end{mathpar}
\end{definition}
\end{frame}
\begin{frame}{Translation}
\begin{definition}[Expression Translation]
Translates a type-derivation tree into a fully expanded expression
\begin{mathpar}
\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{)}
\end{mathpar}
\end{definition}
\end{frame}
\begin{frame}{Soundness}
\begin{lemma}[Morphism generation has correct type]
\begin{mathpar}
C :: \Gamma \vdash \metavariable{\tau} \leadsto \metavariable{\tau'}\\
\Rightarrow \Gamma \vdash \llbracket C \rrbracket : \metavariable{\tau} \typeterminal{\rightarrow_\text{morph}} \metavariable{\tau'}
\end{mathpar}
\end{lemma}
\begin{lemma}[Expression Translation preserves typing]
\begin{mathpar}
D :: \Gamma \vdash \metavariable{e} : \metavariable{\tau}\\
\Rightarrow \Gamma \vdash \llbracket D \rrbracket : \metavariable{\tau}
\end{mathpar}
\end{lemma}
\end{frame}
\begin{frame}{Soundness}
\begin{theorem}[Preservation]
\begin{mathpar} \forall \Gamma, \metavariable{e}, \metavariable{\tau},\\
D :: \Gamma \vdash \metavariable{e} : \metavariable{\tau}\\
\Rightarrow \llbracket D \rrbracket \rightarrow_{eval} \metavariable{e'} \\
\Rightarrow \Gamma \vdash \metavariable{e'} : \metavariable{\tau}
\end{mathpar}
\end{theorem}
\end{frame}
\begin{frame}{Soundness}
\begin{theorem}[Progress]
\begin{mathpar} \forall \Gamma, \metavariable{e}, \metavariable{\tau},\\
D :: \Gamma \vdash \metavariable{e} : \metavariable{\tau}\\
\Rightarrow \llbracket D \rrbracket \text{ is a value} \\ \vee \\
\exists \metavariable{e'} . \llbracket D \rrbracket \rightarrow_{eval} \metavariable{e'} \\
\end{mathpar}
\end{theorem}
\end{frame}
\begin{frame}{Summary}
todo
\end{frame}
\end{document}