Changeset 2414
 Timestamp:
 Oct 23, 2012, 2:43:58 PM (9 years ago)
 Location:
 Papers/polymorphicvariants2012
 Files:

 1 added
 1 edited
Legend:
 Unmodified
 Added
 Removed

Papers/polymorphicvariants2012/polymorphicvariants.tex
r2410 r2414 5 5 \usepackage[english]{babel} 6 6 \usepackage[colorlinks]{hyperref} 7 \usepackage{listings} 7 8 \usepackage{microtype} 8 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} 9 19 10 20 \author{Dominic P. Mulligan \and Claudio Sacerdoti Coen} … … 46 56 \label{sect.polymorphic.variants} 47 57 48 In this section we provide a selfcontained \emph{pr\'ecis} of polymorphic variants.49 For a more complete summary, we refer the reader to Garrigue's publications on the subject~\cite{dpm: todo}.58 In this section we will attempt to provide a selfcontained \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}. 50 60 51 Most mainstream functional programming languages, such as OCaml and Haskell, have mechanisms for inductively defining types through the use of \emph{algebraic data types}. 52 Each algebraic data type can be described as a sumofproducts, wherein we associate a fixed number of distinct \emph{constructors} to the type being introduced, all of whom expect a product of arguments. 53 Inductive data, modelled as an algebraic data type, is built incrementally from the groundup using the constructors of that type. 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 sumofproducts. 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. 54 65 Quotidian data structuressuch as lists, trees, heaps, zippers, and so forthcan all be introduced using this now familiar mechanism. 55 66 56 Having built data using constructors, to complete the picture we now need some facility for picking said data apart.57 Functional languages employ \emph{pattern matching} for this task.58 Given any inhabitant of an inductive type, by the aforementioned sumofproducts property, we know that it must consist of some constructor of that typeapplied to various arguments.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 sumofproducts property, we know that it must consist of some constructor applied to various arguments. 59 70 Using pattern matching we can therefore deconstruct algebraic data by performing a case analysis on the constructors of a given type. 60 71 61 72 The combination of algebraic data types and pattern matching is powerful, and is arguably the main branching mechanism for most functional programming languages. 62 Further, using pattern matching it is easy to define new functions that consume algebraic datathe set of operations that can be defined for any given algebraic type is unbounded. 63 Unfortunately, when it comes to extending algebraic data types with new constructors these types are essentially `closed'. 64 We cannot simply extend an algebraic type with a new constructor. 65 We must introduce a new algebraic type with the additional constructor, lifting the old typeand any functions defined over itinto this type. 73 Furthermore, using pattern matching it is easy to define new functions that consume algebraic datathe set of operations that can be defined for any given algebraic type is practically without bound. 66 74 67 Moving sideways, we can compare and contrast functional programming languages' use of algebraic data paired with pattern matching with the approach taken by objectoriented languages, or extending the root object. 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 typeand any functions defined over itinto 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 \end{lstlisting} 97 \end{minipage} 98 \label{fig.patternmatching.vs.oop} 99 \caption{A simple language of integer arithmetic embedded as an algebraic data type and as a class hierarchy.} 100 \end{figure} 101 102 We can compare and contrast functional programming languages' use of algebraic data and pattern matching with the approach taken by objectoriented languages (see Figure~\ref{fig.patternmatching.vs.oop} for a concrete example). 68 103 In mainstream objectoriented languages such as Java algebraic data types correspond to interfaces, or some base object. 69 Constructors correspond to classes implementing this interface; pattern matching is emulated using the language's dynamic dispatch mechanism. 70 The interface specifies the permitted operations defined for the type. 71 In contrast to the functional approach, it is hard to enlarge the set of operations defined over a given type without altering the entire class hierarchy. 104 Constructors correspond to classes implementing this interface; branching by pattern matching is emulated using the language's dynamic dispatch mechanism. 105 The base interface of the object hierarchy specifies the permitted operations defined for the type. 106 107 As all operations 108 it is hard to enlarge the set of operations defined over a given type without altering the entire class hierarchy. 72 109 If the interface changes so must every class implementing it. 73 110 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. … … 80 117 \item Bounded vs notbounded. 81 118 \end{itemize} 119 120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 121 % Section 122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 \subsection{Matita} 124 \label{subsect.matita} 82 125 83 126 \subsection{Subtyping as instantiation vs subtyping as safe static cast} … … 119 162 \section{Appendix: interface of library functions used to implement everything} 120 163 164 \bibliography{polymorphicvariants} 165 121 166 \end{document}
Note: See TracChangeset
for help on using the changeset viewer.