Changeset 2409
 Timestamp:
 Oct 19, 2012, 4:48:06 PM (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Papers/polymorphicvariants2012/polymorphicvariants.tex
r2408 r2409 40 40 \label{sect.introduction} 41 41 42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 43 % Section 44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 42 45 \section{Polymorphic variants} 46 \label{sect.polymorphic.variants} 47 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}. 50 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. 54 Quotidian data structuressuch as lists, trees, heaps, zippers, and so forthcan all be introduced using this now familiar mechanism. 55 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 type applied to various arguments. 59 Using pattern matching we can therefore deconstruct algebraic data by performing a case analysis on the constructors of a given type. 60 61 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. 66 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. 68 In mainstream objectoriented languages such as Java algebraic data types correspond to class hierarchies, each constructor corresponding to a subclass in this hierarchy. 69 Pattern matching is emulated using a dynamic dispatch mechanism. 70 Using the objectoriented approach it is easy to add a new `case' to a given hierarchy: just introduce a new subclass. 71 72 [dpm: reword the above  hard to phrase precisely ] 73 74 43 75 \begin{itemize} 44 76 \item General introduction, motivations
Note: See TracChangeset
for help on using the changeset viewer.