1 | \documentclass[smallextended]{svjour3} |
---|
2 | |
---|
3 | \smartqed |
---|
4 | |
---|
5 | \usepackage[english]{babel} |
---|
6 | \usepackage[colorlinks]{hyperref} |
---|
7 | \usepackage{listings} |
---|
8 | \usepackage{microtype} |
---|
9 | |
---|
10 | \lstset{basicstyle=\footnotesize\tt,columns=flexible,breaklines=false, |
---|
11 | keywordstyle=\color{red}\bfseries, |
---|
12 | keywordstyle=[2]\color{blue}, |
---|
13 | commentstyle=\color{green}, |
---|
14 | stringstyle=\color{blue}, |
---|
15 | showspaces=false,showstringspaces=false, |
---|
16 | xleftmargin=1em} |
---|
17 | |
---|
18 | \bibliographystyle{spphys} |
---|
19 | |
---|
20 | \author{Dominic P. Mulligan \and Claudio Sacerdoti Coen} |
---|
21 | \title{Polymorphic variants in dependent type theory\thanks{The project CerCo acknowledges the financial support of the Future and Emerging Technologies (FET) programme within the Seventh Framework Programme for Research of the European Commission, under FET-Open grant number: 243881.}} |
---|
22 | |
---|
23 | \institute{ |
---|
24 | Dominic P. Mulligan \at |
---|
25 | Computer Laboratory,\\ |
---|
26 | University of Cambridge. |
---|
27 | \email{dominic.p.mulligan@gmail.com} \and |
---|
28 | Claudio Sacerdoti Coen \at |
---|
29 | Dipartimento di Scienze dell'Informazione,\\ |
---|
30 | Universit\`a di Bologna. |
---|
31 | \email{sacerdot@cs.unibo.it} |
---|
32 | } |
---|
33 | |
---|
34 | \begin{document} |
---|
35 | |
---|
36 | \maketitle |
---|
37 | |
---|
38 | \begin{abstract} |
---|
39 | |
---|
40 | Big long abstract introducing the work |
---|
41 | |
---|
42 | \keywords{Polymorphic variants \and dependent type theory \and Matita theorem prover} |
---|
43 | |
---|
44 | \end{abstract} |
---|
45 | |
---|
46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
47 | % Section |
---|
48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
49 | \section{Introduction} |
---|
50 | \label{sect.introduction} |
---|
51 | |
---|
52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
53 | % Section |
---|
54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
55 | \section{Polymorphic variants} |
---|
56 | \label{sect.polymorphic.variants} |
---|
57 | |
---|
58 | In this section we will attempt to provide a self-contained \emph{pr\'ecis} of polymorphic variants for the unfamiliar reader. |
---|
59 | Those readers who wish to survey a more complete introduction to the subject are referred to Garrigue's original publications on the subject matter~\cite{garrigue:programming:1998,garrigue:code:2000}. |
---|
60 | |
---|
61 | Most mainstream functional programming languages, such as OCaml and Haskell, have mechanisms for defining new inductive types from old through the use of algebraic data type definitions. |
---|
62 | Each algebraic data type may be described as a sum-of-products. |
---|
63 | The programmer provides a fixed number of distinct \emph{constructors} with each constructor expects a (potentially empty) product of arguments. |
---|
64 | Values of a given inductive data type are built using a data type's constructors. |
---|
65 | Quotidian data structures---such as lists, trees, heaps, zippers, and so forth---can all be introduced using this now familiar mechanism. |
---|
66 | |
---|
67 | Having built data from various combinations of constructors, to complete the picture we now need some facility for picking said data apart, in order to build functions that operate over that data. |
---|
68 | Functional languages almost uniformly employ \emph{pattern matching} for this task. |
---|
69 | Given any inhabitant of an algebraic data type, by the aforementioned sum-of-products property, we know that it must consist of some constructor applied to various arguments. |
---|
70 | Using pattern matching we can therefore deconstruct algebraic data by performing a case analysis on the constructors of a given type. |
---|
71 | |
---|
72 | The combination of algebraic data types and pattern matching is powerful, and is arguably the main branching mechanism for most functional programming languages. |
---|
73 | Furthermore, using pattern matching it is easy to define new functions that consume algebraic data---the set of operations that can be defined for any given algebraic type is practically without bound. |
---|
74 | |
---|
75 | Unfortunately, in practical programming, we often want to expand a previously defined algebraic data type, adding more constructors. |
---|
76 | When it comes to extending algebraic data types with new constructors in this way, we hit a problem: these types are `closed'. |
---|
77 | In order to circumvent this restriction, we must introduce a new algebraic type with the additional constructor, lifting the old type---and any functions defined over it---into this type. |
---|
78 | |
---|
79 | \begin{figure}[ht] |
---|
80 | \begin{minipage}[b]{0.45\linewidth} |
---|
81 | \begin{lstlisting} |
---|
82 | data Term |
---|
83 | = Lit Int |
---|
84 | | Add Term Term |
---|
85 | | Mul Term Term |
---|
86 | |
---|
87 | evaluate :: Term -> Int |
---|
88 | evaluate (Lit i) = i |
---|
89 | evaluate (Add l r) = evaluate l + evaluate r |
---|
90 | evaluate (Mul l r) = evaluate l * evaluate r |
---|
91 | \end{lstlisting} |
---|
92 | \end{minipage} |
---|
93 | \hspace{0.5cm} |
---|
94 | \begin{minipage}[b]{0.45\linewidth} |
---|
95 | \begin{lstlisting} |
---|
96 | |
---|
97 | \end{lstlisting} |
---|
98 | \end{minipage} |
---|
99 | \label{fig.pattern-matching.vs.oop} |
---|
100 | \caption{A simple language of integer arithmetic embedded as an algebraic data type and as a class hierarchy.} |
---|
101 | \end{figure} |
---|
102 | |
---|
103 | We can compare and contrast functional programming languages' use of algebraic data and pattern matching with the approach taken by object-oriented languages (see Figure~\ref{fig.pattern-matching.vs.oop} for a concrete example). |
---|
104 | In mainstream object-oriented languages such as Java algebraic data types correspond to interfaces, or some base object. |
---|
105 | Constructors correspond to classes implementing this interface; branching by pattern matching is emulated using the language's dynamic dispatch mechanism. |
---|
106 | The base interface of the object hierarchy specifies the permitted operations defined for the type. |
---|
107 | |
---|
108 | As all operations |
---|
109 | it is hard to enlarge the set of operations defined over a given type without altering the entire class hierarchy. |
---|
110 | If the interface changes so must every class implementing it. |
---|
111 | However, note it is easy to extend the hierarchy to new cases, corresponding to the introduction of a new constructor in the functional world, by merely adding another class corresponding to that constructor implementing the interface. |
---|
112 | |
---|
113 | [dpm: reword the above --- hard to phrase precisely ] |
---|
114 | |
---|
115 | |
---|
116 | \begin{itemize} |
---|
117 | \item General introduction, motivations |
---|
118 | \item Bounded vs not-bounded. |
---|
119 | \end{itemize} |
---|
120 | |
---|
121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
122 | % Section |
---|
123 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
124 | \subsection{Matita} |
---|
125 | \label{subsect.matita} |
---|
126 | |
---|
127 | Matita~\cite{asperti:user:2007} is a dependently-typed proof assistant developed in Bologna, implementing the Calculus of (Co)inductive Constructions, a type theory similar to that of Coq. |
---|
128 | Throughout this paper, all running examples are provided in Matita's proof script vernacular. |
---|
129 | This vernacular, similar to the syntax of OCaml or Coq, should be mostly straightforward. |
---|
130 | |
---|
131 | One possible source of confusion is our use of $?$ and $\ldots$, which correspond to a term, or terms, to be inferred automatically by unification, respectively. |
---|
132 | Any terms that cannot be inferred by unification from the surrounding context are left for the user to provide as proof obligations. |
---|
133 | |
---|
134 | Matita |
---|
135 | |
---|
136 | \subsection{Subtyping as instantiation vs subtyping as safe static cast} |
---|
137 | |
---|
138 | \subsection{Syntax \& type checking rules} |
---|
139 | The ones of Guarrigue + casts, but also for the bounded case? |
---|
140 | Casts support both styles of subtyping. |
---|
141 | |
---|
142 | \subsection{Examples} |
---|
143 | The weird function types that only work in subtyping as instantiation |
---|
144 | |
---|
145 | \subsection{Solution to the expression problem} |
---|
146 | Our running example in pseudo-OCaml syntax |
---|
147 | |
---|
148 | \section{Bounded polymorphic variants via dependent types} |
---|
149 | Requirements (i.e. O(1) pattern-matching, natural extracted code, etc.) |
---|
150 | \subsection{Simulation (reduction + type checking)} |
---|
151 | \subsection{Examples} |
---|
152 | The weird function types redone |
---|
153 | \subsection{Subtyping as instantiation vs subtyping as safe static cast} |
---|
154 | Here we show/discuss how our approach supports both styles at once. |
---|
155 | \subsection{Solution to the expression problem, I} |
---|
156 | Using subtyping as cast, the file I have produced |
---|
157 | \subsection{Solution to the expression problem, II} |
---|
158 | Using subtyping as instantiation, comparisons, pros vs cons |
---|
159 | \subsection{Negative encoding (??)} |
---|
160 | The negative encoding and application to the expression problem |
---|
161 | \subsection{Other encodings (??)} |
---|
162 | Hints to other possible encodings |
---|
163 | |
---|
164 | \section{Extensible records (??)} |
---|
165 | |
---|
166 | \section{Comparison to related work and alternatives} |
---|
167 | \begin{itemize} |
---|
168 | \item Disjoint unions: drawbacks |
---|
169 | \item Encoding the unbounded case: drawbacks |
---|
170 | \end{itemize} |
---|
171 | |
---|
172 | \section{Appendix: interface of library functions used to implement everything} |
---|
173 | |
---|
174 | \bibliography{polymorphic-variants} |
---|
175 | |
---|
176 | \end{document} |
---|