Index: doc/rob_thesis/intro.tex
===================================================================
--- doc/rob_thesis/intro.tex	(revision 9c14ae94c6baf3aa3a91be49b0076852ea86e5ef)
+++ doc/rob_thesis/intro.tex	(revision 9c14ae94c6baf3aa3a91be49b0076852ea86e5ef)
@@ -0,0 +1,673 @@
+%======================================================================
+\chapter{Introduction}
+%======================================================================
+
+\section{\CFA Background}
+\label{s:background}
+\CFA is a modern extension to the C programming language.
+As it is an extension of C, there is already a wealth of existing C code and principles that govern the design of the language.
+Among the goals set out in the original design of \CFA, four points stand out \cite{Bilson03}.
+\begin{enumerate}
+\item The behaviour of standard C code must remain the same when translated by a \CFA compiler as when translated by a C compiler.
+\item Standard C code must be as fast and as small when translated by a \CFA compiler as when translated by a C compiler.
+\item \CFA code must be at least as portable as standard C code.
+\item Extensions introduced by \CFA must be translated in the most efficient way possible.
+\end{enumerate}
+Therefore, these design principles must be kept in mind throughout the design and development of new language features.
+In order to appeal to existing C programmers, great care must be taken to ensure that new features naturally feel like C.
+The remainder of this section describes some of the important new features that currently exist in \CFA, to give the reader the necessary context in which the new features presented in this thesis must dovetail. % TODO: harmonize with?
+
+\subsection{C Background}
+\label{sub:c_background}
+One of the lesser-known features of standard C is \emph{designations}.
+Designations are similar to named parameters in languages such as Python and Scala, except that they only apply to aggregate initializers.
+\begin{cfacode}
+struct A {
+  int w, x, y, z;
+};
+A a0 = { .x:4 .z:1, .x:8 };
+A a1 = { 1, .y:7, 6 };
+A a2[4] = { [2]:a0, [0]:a1, { .z:3 } };
+// equvialent to
+// A a0 = { 0, 8, 0, 1 };
+// A a1 = { 1, 0, 7, 6 };
+// A a2[4] = { a1, { 0, 0, 0, 3 }, a0, { 0, 0, 0, 0 } };
+\end{cfacode}
+Designations allow specifying the field to initialize by name, rather than by position.
+Any field not explicitly initialized is initialized as if it had static storage duration \cite[p.~141]{C11}.
+A designator specifies the current object for initialization, and as such any undesignated subobjects pick up where the last initialization left off.
+For example, in the initialization of @a1@, the initializer of @y@ is @7@, and the unnamed initializer @6@ initializes the next subobject, @z@.
+Later initializers override earlier initializers, so a subobject for which there is more than one initializer is only initailized by its last initializer.
+This can be seen in the initialization of @a0@, where @x@ is designated twice, and thus initialized to @8@.
+Note that in \CFA, designations use a colon separator, rather than an equals sign as in C.
+
+C also provides \emph{compound literal} expressions, which provide a first-class mechanism for creating unnamed objects.
+\begin{cfacode}
+struct A { int x, y; };
+int f(A, int);
+int g(int *);
+
+f((A){ 3, 4 }, (int){ 5 } = 10);
+g((int[]){ 1, 2, 3 });
+g(&(int){ 0 });
+\end{cfacode}
+Compound literals create an unnamed object, and result in an lvalue, so it is legal to assign a value into a compound literal or to take its address \cite[p.~86]{C11}.
+Syntactically, compound literals look like a cast operator followed by a brace-enclosed initializer, but semantically are different from a C cast, which only applies basic conversions and is never an lvalue.
+
+\subsection{Overloading}
+\label{sub:overloading}
+Overloading is the ability to specify multiple entities with the same name.
+The most common form of overloading is function overloading, wherein multiple functions can be defined with the same name, but with different signatures.
+Like in \CC, \CFA allows overloading based both on the number of parameters and on the types of parameters.
+  \begin{cfacode}
+  void f(void);  // (1)
+  void f(int);   // (2)
+  void f(char);  // (3)
+
+  f('A');        // selects (3)
+  \end{cfacode}
+In this case, there are three @f@ procedures, where @f@ takes either 0 or 1 arguments, and if an argument is provided then it may be of type @int@ or of type @char@.
+Exactly which procedure is executed depends on the number and types of arguments passed.
+If there is no exact match available, \CFA attempts to find a suitable match by examining the C built-in conversion heuristics.
+  \begin{cfacode}
+  void g(long long);
+
+  g(12345);
+  \end{cfacode}
+In the above example, there is only one instance of @g@, which expects a single parameter of type @long long@.
+Here, the argument provided has type @int@, but since all possible values of type @int@ can be represented by a value of type @long long@, there is a safe conversion from @int@ to @long long@, and so \CFA calls the provided @g@ routine.
+
+In addition to this form of overloading, \CFA also allows overloading based on the number and types of \emph{return} values.
+This extension is a feature that is not available in \CC, but is available in other programming languages such as Ada \cite{Ada95}.
+  \begin{cfacode}
+  int g();         // (1)
+  double g();      // (2)
+
+  int x = g();     // selects (1)
+  \end{cfacode}
+Here, the only difference between the signatures of the different versions of @g@ is in the return values.
+The result context is used to select an appropriate routine definition.
+In this case, the result of @g@ is assigned into a variable of type @int@, so \CFA prefers the routine that returns a single @int@, because it is an exact match.
+
+There are times when a function should logically return multiple values.
+Since a function in standard C can only return a single value, a programmer must either take in additional return values by address, or the function's designer must create a wrapper structure t0 package multiple return-values.
+\begin{cfacode}
+int f(int * ret) {        // returns a value through parameter ret
+  *ret = 37;
+  return 123;
+}
+
+int res1, res2;           // allocate return value
+int res1 = g(&res2);      // explicitly pass storage
+\end{cfacode}
+The former solution is awkward because it requires the caller to explicitly allocate memory for $n$ result variables, even if they are only temporary values used as a subexpression, or even not used at all.
+\begin{cfacode}
+struct A {
+  int x, y;
+};
+struct A g() {            // returns values through a structure
+  return (struct A) { 123, 37 };
+}
+struct A res3 = g();
+... res3.x ... res3.y ... // use result values
+\end{cfacode}
+The latter approach requires the caller to either learn the field names of the structure or learn the names of helper routines to access the individual return values.
+Both solutions are syntactically unnatural.
+
+In \CFA, it is possible to directly declare a function returning mutliple values.
+This provides important semantic information to the caller, since return values are only for output.
+\begin{cfacode}
+[int, int] f() {       // don't need to create a new type
+  return [123, 37];
+}
+\end{cfacode}
+However, the ability to return multiple values requires a syntax for accepting the results from a function.
+In standard C, return values are most commonly assigned directly into local variables, or are used as the arguments to another function call.
+\CFA allows both of these contexts to accept multiple return values.
+\begin{cfacode}
+int res1, res2;
+[res1, res2] = f();    // assign return values into local variables
+
+void g(int, int);
+g(f());                // pass both return values of f to g
+\end{cfacode}
+As seen in the example, it is possible to assign the results from a return value directly into local variables.
+These local variables can be referenced naturally, without requiring any unpacking as in structured return values.
+Perhaps more interesting is the fact that multiple return values can be passed to multiple parameters seamlessly, as in the call @g(f())@.
+In this call, the return values from @f@ are linked to the parameters of @g@ so that each of the return values is passed directly to the corresponding parameter of @g@, without any explicit storing, unpacking, or additional naming.
+
+An extra quirk introduced by multiple return values is in the resolution of function calls.
+  \begin{cfacode}
+  int f();            // (1)
+  [int, int] f();     // (2)
+
+  void g(int, int);
+
+  int x, y;
+  [x, y] = f();       // selects (2)
+  g(f());             // selects (2)
+  \end{cfacode}
+In this example, the only possible call to @f@ that can produce the two @int@s required by @g@ is the second option.
+A similar reasoning holds for assigning into multiple variables.
+
+In \CFA, overloading also applies to operator names, known as \emph{operator overloading}.
+Similar to function overloading, a single operator is given multiple meanings by defining new versions of the operator with different signatures.
+In \CC, this can be done as follows
+  \begin{cppcode}
+  struct A { int i; };
+  int operator+(A x, A y);
+  bool operator<(A x, A y);
+  \end{cppcode}
+
+In \CFA, the same example can be written as follows.
+  \begin{cfacode}
+  struct A { int i; };
+  int ?+?(A x, A y);
+  bool ?<?(A x, A y);
+  \end{cfacode}
+Notably, the only difference in this example is syntax.
+Most of the operators supported by \CC for operator overloading are also supported in \CFA.
+Of notable exception are the logical operators (e.g. @||@), the sequence operator (i.e. @,@), and the member-access operators (e.g. @.@ and \lstinline{->}).
+
+Finally, \CFA also permits overloading variable identifiers.
+This feature is not available in \CC.
+  \begin{cfacode} % TODO: pick something better than x? max, zero, one?
+  struct Rational { int numer, denom; };
+  int x = 3;               // (1)
+  double x = 1.27;         // (2)
+  Rational x = { 4, 11 };  // (3)
+
+  void g(double);
+
+  x += 1;                  // chooses (1)
+  g(x);                    // chooses (2)
+  Rational y = x;          // chooses (3)
+  \end{cfacode}
+In this example, there are three definitions of the variable @x@.
+Based on the context, \CFA attempts to choose the variable whose type best matches the expression context.
+
+Finally, the values @0@ and @1@ have special status in standard C.
+In particular, the value @0@ is both an integer and a pointer literal, and thus its meaning depends on the context.
+In addition, several operations can be redefined in terms of other operations and the values @0@ and @1@.
+For example,
+\begin{cfacode}
+int x;
+if (x) {  // if (x != 0)
+  x++;    //   x += 1;
+}
+\end{cfacode}
+Every if statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result.
+Due to these rewrite rules, the values @0@ and @1@ have the types \zero and \one in \CFA, which allow for overloading various operations that connect to @0@ and @1@ \footnote{In the original design of \CFA, @0@ and @1@ were overloadable names \cite[p.~7]{cforall}.}.
+The types \zero and \one have special built in implicit conversions to the various integral types, and a conversion to pointer types for @0@, which allows standard C code involving @0@ and @1@ to work as normal.
+  \begin{cfacode}
+  // lvalue is similar to returning a reference in C++
+  lvalue Rational ?+=?(Rational *a, Rational b);
+  Rational ?=?(Rational * dst, zero_t) {
+    return *dst = (Rational){ 0, 1 };
+  }
+
+  Rational sum(Rational *arr, int n) {
+    Rational r;
+    r = 0;     // use rational-zero_t assignment
+    for (; n > 0; n--) {
+      r += arr[n-1];
+    }
+    return r;
+  }
+  \end{cfacode}
+This function takes an array of @Rational@ objects and produces the @Rational@ representing the sum of the array.
+Note the use of an overloaded assignment operator to set an object of type @Rational@ to an appropriate @0@ value.
+
+\subsection{Polymorphism}
+\label{sub:polymorphism}
+In its most basic form, polymorphism grants the ability to write a single block of code that accepts different types.
+In particular, \CFA supports the notion of parametric polymorphism.
+Parametric polymorphism allows a function to be written generically, for all values of all types, without regard to the specifics of a particular type.
+For example, in \CC, the simple identity function for all types can be written as
+  \begin{cppcode}
+  template<typename T>
+  T identity(T x) { return x; }
+  \end{cppcode}
+\CC uses the template mechanism to support parametric polymorphism. In \CFA, an equivalent function can be written as
+  \begin{cfacode}
+  forall(otype T)
+  T identity(T x) { return x; }
+  \end{cfacode}
+Once again, the only visible difference in this example is syntactic.
+Fundamental differences can be seen by examining more interesting examples.
+In \CC, a generic sum function is written as follows
+  \begin{cppcode}
+  template<typename T>
+  T sum(T *arr, int n) {
+    T t;
+    for (; n > 0; n--) t += arr[n-1];
+    return t;
+  }
+  \end{cppcode}
+Here, the code assumes the existence of a default constructor, assignment operator, and an addition operator over the provided type @T@.
+If any of these required operators are not available, the \CC compiler produces an error message stating which operators could not be found.
+
+A similar sum function can be written in \CFA as follows
+  \begin{cfacode}
+  forall(otype T | **R**{ T ?=?(T *, zero_t); T ?+=?(T *, T); }**R**)
+  T sum(T *arr, int n) {
+    T t = 0;
+    for (; n > 0; n--) t = t += arr[n-1];
+    return t;
+  }
+  \end{cfacode}
+The first thing to note here is that immediately following the declaration of @otype T@ is a list of \emph{type assertions} that specify restrictions on acceptable choices of @T@.
+In particular, the assertions above specify that there must be a an assignment from \zero to @T@ and an addition assignment operator from @T@ to @T@.
+The existence of an assignment operator from @T@ to @T@ and the ability to create an object of type @T@ are assumed implicitly by declaring @T@ with the @otype@ type-class.
+In addition to @otype@, there are currently two other type-classes.
+The three type parameter kinds are summarized in \autoref{table:types}
+
+\begin{table}[h!]
+  \begin{center}
+    \begin{tabular}{|c||c|c|c||c|c|c|}
+                                                                                                    \hline
+    name    & object type & incomplete type & function type & can assign value & can create & has size \\ \hline
+    @otype@ & X           &                 &               & X                & X          & X        \\ \hline
+    @dtype@ & X           & X               &               &                  &            &          \\ \hline
+    @ftype@ &             &                 & X             &                  &            &          \\ \hline
+    \end{tabular}
+  \end{center}
+  \caption{\label{table:types} The different kinds of type parameters in \CFA}
+\end{table}
+
+A major difference between the approaches of \CC and \CFA to polymorphism is that the set of assumed properties for a type is \emph{explicit} in \CFA.
+One of the major limiting factors of \CC's approach is that templates cannot be separately compiled.
+In contrast, the explicit nature of assertions allows \CFA's polymorphic functions to be separately compiled.
+
+In \CFA, a set of assertions can be factored into a \emph{trait}.
+\begin{cfacode}
+  trait Addable(otype T) {
+    T ?+?(T, T);
+    T ++?(T);
+    T ?++(T);
+  }
+  forall(otype T | Addable(T)) void f(T);
+  forall(otype T | Addable(T) | { T --?(T); }) T g(T);
+  forall(otype T, U | Addable(T) | { T ?/?(T, U); }) U h(T, U);
+\end{cfacode}
+This capability allows specifying the same set of assertions in multiple locations, without the repetition and likelihood of mistakes that come with manually writing them out for each function declaration.
+
+\section{Invariants}
+% TODO: discuss software engineering benefits of ctor/dtors: {pre/post} conditions, invariants
+% an important invariant is the state of the environment (memory, resources)
+% some objects pass their contract to the object user
+An \emph{invariant} is a logical assertion that true for some duration of a program's execution.
+Invariants help a programmer to reason about code correctness and prove properties of programs.
+
+In object-oriented programming languages, type invariants are typically established in a constructor and maintained throughout the object's lifetime.
+This is typically achieved through a combination of access control modifiers and a restricted interface.
+Typically, data which requires the maintenance of an invariant is hidden from external sources using the \emph{private} modifier, which restricts reads and writes to a select set of trusted routines, including member functions.
+It is these trusted routines that perform all modifications to internal data in a way that is consistent with the invariant, by ensuring that the invariant holds true at the end of the routine call.
+
+In C, the @assert@ macro is often used to ensure invariants are true.
+Using @assert@, the programmer can check a condition and abort execution if the condition is not true.
+This is a powerful tool that forces the programmer to deal with logical inconsistencies as they occur.
+For production, assertions can be removed by simply defining the preprocessor macro @NDEBUG@, making it simple to ensure that assertions are 0-cost for a performance intensive application.
+\begin{cfacode}
+struct Rational {
+  int n, d;
+};
+struct Rational create_rational(int n, int d) {
+  assert(d != 0);  // precondition
+  if (d < 0) {
+    n *= -1;
+    d *= -1;
+  }
+  assert(d > 0);  // postcondition
+  // rational invariant: d > 0
+  return (struct Rational) { n, d };
+}
+struct Rational rat_abs(struct Rational r) {
+  assert(r.d > 0); // check invariant, since no access control
+  r.n = abs(r.n);
+  assert(r.d > 0); // ensure function preserves invariant on return value
+  return r;
+}
+\end{cfacode}
+
+Some languages, such as D, provide language-level support for specifying program invariants.
+In addition to providing a C-like @assert@ expression, D allows specifying type invariants that are automatically checked at the end of a constructor, beginning of a destructor, and at the beginning and end of every public member function.
+\begin{dcode}
+import std.math;
+struct Rational {
+  invariant {
+    assert(d > 0, "d <= 0");
+  }
+  int n, d;
+  this(int n, int d) {  // constructor
+    assert(d != 0);
+    this.n = n;
+    this.d = d;
+    // implicitly check invariant
+  }
+  Rational abs() {
+    // implicitly check invariant
+    return Rational(std.math.abs(n), d);
+    // implicitly check invariant
+  }
+}
+\end{dcode}
+The D compiler is able to assume that assertions and invariants hold true and perform optimizations based on those assumptions.
+
+An important invariant is the state of the execution environment, including the heap, the open file table, the state of global variables, etc.
+Since resources are finite, it is important to ensure that objects clean up properly when they are finished, restoring the execution environment to a stable state so that new objects can reuse resources.
+
+\section{Resource Management}
+\label{s:ResMgmt}
+
+Resource management is a problem that pervades every programming language.
+
+In standard C, resource management is largely a manual effort on the part of the programmer, with a notable exception to this rule being the program stack.
+The program stack grows and shrinks automatically with each function call, as needed for local variables.
+However, whenever a program needs a variable to outlive the block it is created in, the storage must be allocated dynamically with @malloc@ and later released with @free@.
+This pattern is extended to more complex objects, such as files and sockets, which also outlive the block where they are created, but at their core is resource management.
+Once allocated storage escapes a block, the responsibility for deallocating the storage is not specified in a function's type, that is, that the return value is owned by the caller.
+This implicit convention is provided only through documentation about the expectations of functions.
+
+In other languages, a hybrid situation exists where resources escape the allocation block, but ownership is precisely controlled by the language.
+This pattern requires a strict interface and protocol for a data structure, where the protocol consists of a pre-initialization and a post-termination call, and all intervening access is done via interface routines.
+This kind of encapsulation is popular in object-oriented programming languages, and like the stack, it contains a significant portion of resource management cases.
+
+For example, \CC directly supports this pattern through class types and an idiom known as RAII \footnote{Resource Acquisition is Initialization} by means of constructors and destructors.
+Constructors and destructors are special routines that are automatically inserted into the appropriate locations to bookend the lifetime of an object.
+Constructors allow the designer of a type to establish invariants for objects of that type, since it is guaranteed that every object must be initialized through a constructor.
+In particular, constructors allow a programmer to ensure that all objects are initially set to a valid state.
+On the other hand, destructors provide a simple mechanism for tearing down an object and resetting the environment in which the object lived.
+RAII ensures that if all resources are acquired in a constructor and released in a destructor, there are no resource leaks, even in exceptional circumstances.
+A type with at least one non-trivial constructor or destructor will henceforth be referred to as a \emph{managed type}.
+In the context of \CFA, a non-trivial constructor is either a user defined constructor or an auto generated constructor that calls a non-trivial constructor.
+
+For the remaining resource ownership cases, programmer must follow a brittle, explicit protocol for freeing resources or an implicit porotocol implemented via the programming language.
+
+In garbage collected languages, such as Java, resources are largely managed by the garbage collector.
+Still, garbage collectors are typically focus only on memory management.
+There are many kinds of resources that the garbage collector does not understand, such as sockets, open files, and database connections.
+In particular, Java supports \emph{finalizers}, which are similar to destructors.
+Sadly, finalizers come with far fewer guarantees, to the point where a completely conforming JVM may never call a single finalizer. % TODO: citation JVM spec; http://stackoverflow.com/a/2506514/2386739
+Due to operating system resource limits, this is unacceptable for many long running tasks. % TODO: citation?
+Instead, the paradigm in Java requires programmers manually keep track of all resource \emph{except} memory, leading many novices and experts alike to forget to close files, etc.
+Complicating the picture, uncaught exceptions can cause control flow to change dramatically, leaking a resource which appears on first glance to be closed.
+\begin{javacode}
+void write(String filename, String msg) throws Exception {
+  FileOutputStream out = new FileOutputStream(filename);
+  FileOutputStream log = new FileOutputStream(filename);
+  out.write(msg.getBytes());
+  log.write(msg.getBytes());
+  log.close();
+  out.close();
+}
+\end{javacode}
+Any line in this program can throw an exception.
+This leads to a profusion of finally blocks around many function bodies, since it isn't always clear when an exception may be thrown.
+\begin{javacode}
+public void write(String filename, String msg) throws Exception {
+  FileOutputStream out = new FileOutputStream(filename);
+  try {
+    FileOutputStream log = new FileOutputStream("log.txt");
+    try {
+      out.write(msg.getBytes());
+      log.write(msg.getBytes());
+    } finally {
+      log.close();
+    }
+  } finally {
+    out.close();
+  }
+}
+\end{javacode}
+In Java 7, a new \emph{try-with-resources} construct was added to alleviate most of the pain of working with resources, but ultimately it still places the burden squarely on the user rather than on the library designer.
+Furthermore, for complete safety this pattern requires nested objects to be declared separately, otherwise resources which can throw an exception on close can leak nested resources. % TODO: cite oracle article http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html?
+\begin{javacode}
+public void write(String filename, String msg) throws Exception {
+  try (
+    FileOutputStream out = new FileOutputStream(filename);
+    FileOutputStream log = new FileOutputStream("log.txt");
+  ) {
+    out.write(msg.getBytes());
+    log.write(msg.getBytes());
+  } // automatically closes out and log in every exceptional situation
+}
+\end{javacode}
+On the other hand, the Java compiler generates more code if more resources are declared, meaning that users must be more familiar with each type and library designers must provide better documentation.
+
+% D has constructors and destructors that are worth a mention (under classes) https://dlang.org/spec/spec.html
+%  also https://dlang.org/spec/struct.html#struct-constructor
+% these are declared in the struct, so they're closer to C++ than to CFA, at least syntactically. Also do not allow for default constructors
+% D has a GC, which already makes the situation quite different from C/C++
+The programming language, D, also manages resources with constructors and destructors \cite{D}.
+In D, @struct@s are stack allocated and managed via scoping like in \CC, whereas @class@es are managed automatically by the garbage collector.
+Like Java, using the garbage collector means that destructors may never be called, requiring the use of finally statements to ensure dynamically allocated resources that are not managed by the garbage collector, such as open files, are cleaned up.
+Since D supports RAII, it is possible to use the same techniques as in \CC to ensure that resources are released in a timely manner.
+Finally, D provides a scope guard statement, which allows an arbitrary statement to be executed at normal scope exit with \emph{success}, at exceptional scope exit with \emph{failure}, or at normal and exceptional scope exit with \emph{exit}. % cite? https://dlang.org/spec/statement.html#ScopeGuardStatement
+It has been shown that the \emph{exit} form of the scope guard statement can be implemented in a library in \CC. % cite: http://www.drdobbs.com/184403758
+
+% TODO: discussion of lexical scope vs. dynamic
+% see Peter's suggestions
+% RAII works in both cases. Guaranteed to work in stack case, works in heap case if root is deleted (but it's dangerous to rely on this, because of exceptions)
+
+\section{Tuples}
+\label{s:Tuples}
+In mathematics, tuples are finite-length sequences which, unlike sets, allow duplicate elements.
+In programming languages, tuples are a construct that provide fixed-sized heterogeneous lists of elements.
+Many programming languages have tuple constructs, such as SETL, \KWC, ML, and Scala.
+
+\KWC, a predecessor of \CFA, introduced tuples to C as an extension of the C syntax, rather than as a full-blown data type \cite{Till89}.
+In particular, Till noted that C already contains a tuple context in the form of function parameter lists.
+The main contributions of that work were in the form of adding tuple contexts to assignment in the form of multiple assignment and mass assignment (discussed in detail in section \ref{s:TupleAssignment}), function return values (see section \ref{s:MRV_Functions}), and record field access (see section \ref{s:MemberAccessTuple}).
+Adding tuples to \CFA has previously been explored by Esteves \cite{Esteves04}.
+
+The design of tuples in \KWC took much of its inspiration from SETL.
+SETL is a high-level mathematical programming language, with tuples being one of the primary data types.
+Tuples in SETL allow a number of operations, including subscripting, dynamic expansion, and multiple assignment.
+
+\CCeleven introduced @std::tuple@ as a library variadic template struct.
+Tuples are a generalization of @std::pair@, in that they allow for arbitrary length, fixed-size aggregation of heterogeneous values.
+\begin{cppcode}
+tuple<int, int, int> triple(10, 20, 30);
+get<1>(triple); // access component 1 => 30
+
+tuple<int, double> f();
+int i;
+double d;
+tie(i, d) = f(); // assign fields of return value into local variables
+
+tuple<int, int, int> greater(11, 0, 0);
+triple < greater; // true
+\end{cppcode}
+Tuples are simple data structures with few specific operations.
+In particular, it is possible to access a component of a tuple using @std::get<N>@.
+Another interesting feature is @std::tie@, which creates a tuple of references, which allows assigning the results of a tuple-returning function into separate local variables, without requiring a temporary variable.
+Tuples also support lexicographic comparisons, making it simple to write aggregate comparators using @std::tie@.
+
+There is a proposal for \CCseventeen called \emph{structured bindings}, that introduces new syntax to eliminate the need to pre-declare variables and use @std::tie@ for binding the results from a function call. % TODO: cite http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0144r0.pdf
+\begin{cppcode}
+tuple<int, double> f();
+auto [i, d] = f(); // unpacks into new variables i, d
+
+tuple<int, int, int> triple(10, 20, 30);
+auto & [t1, t2, t3] = triple;
+t2 = 0; // changes triple
+
+struct S { int x; double y; };
+S s = { 10, 22.5 };
+auto [x, y] = s; // unpack s
+\end{cppcode}
+Structured bindings allow unpacking any struct with all public non-static data members into fresh local variables.
+The use of @&@ allows declaring new variables as references, which is something that cannot be done with @std::tie@, since \CC references do not support rebinding.
+This extension requires the use of @auto@ to infer the types of the new variables, so complicated expressions with a non-obvious type must documented with some other mechanism.
+Furthermore, structured bindings are not a full replacement for @std::tie@, as it always declares new variables.
+
+Like \CC, D provides tuples through a library variadic template struct.
+In D, it is possible to name the fields of a tuple type, which creates a distinct type.
+\begin{dcode} % TODO: cite http://dlang.org/phobos/std_typecons.html
+Tuple!(float, "x", float, "y") point2D;
+Tuple!(float, float) float2;  // different types
+
+point2D[0]; // access first element
+point2D.x;  // access first element
+
+float f(float x, float y) {
+  return x+y;
+}
+
+f(point2D.expand);
+\end{dcode}
+Tuples are 0-indexed and can be subscripted using an integer or field name, if applicable.
+The @expand@ method produces the components of the tuple as a list of separate values, making it possible to call a function that takes $N$ arguments using a tuple with $N$ components.
+
+Tuples are a fundamental abstraction in most functional programming languages, such as Standard ML.
+A function in SML always accepts exactly one argument.
+There are two ways to mimic multiple argument functions: the first through currying and the second by accepting tuple arguments.
+\begin{smlcode}
+fun fact (n : int) =
+  if (n = 0) then 1
+  else n*fact(n-1)
+
+fun binco (n: int, k: int) =
+  real (fact n) / real (fact k * fact (n-k))
+\end{smlcode}
+Here, the function @binco@ appears to take 2 arguments, but it actually takes a single argument which is implicitly decomposed via pattern matching.
+Tuples are a foundational tool in SML, allowing the creation of arbitrarily complex structured data types.
+
+Scala, like \CC, provides tuple types through the standard library.
+Scala provides tuples of size 1 through 22 inclusive through generic data structures.
+Tuples support named access and subscript access, among a few other operations.
+\begin{scalacode}
+val a = new Tuple3[Int, String, Double](0, "Text", 2.1)  // explicit creation
+val b = (6, 'a', 1.1f)       // syntactic sugar for Tuple3[Int, Char, Float]
+val (i, _, d) = triple       // extractor syntax, ignore middle element
+
+println(a._2)                // named access => print "Text"
+println(b.productElement(0)) // subscript access => print 6
+\end{scalacode}
+In Scala, tuples are primarily used as simple data structures for carrying around multiple values or for returning multiple values from a function.
+The 22-element restriction is an odd and arbitrary choice, but in practice it doesn't cause problems since large tuples are uncommon.
+Subscript access is provided through the @productElement@ method, which returns a value of the top-type @Any@, since it is impossible to receive a more precise type from a general subscripting method due to type erasure.
+The disparity between named access beginning at @_1@ and subscript access starting at @0@ is likewise an oddity, but subscript access is typically avoided since it discards type information.
+Due to the language's pattern matching facilities, it is possible to extract the values from a tuple into named variables, which is a more idiomatic way of accessing the components of a tuple.
+
+
+\Csharp has similarly strange limitations, allowing tuples of size up to 7 components. % TODO: cite https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx
+The officially supported workaround for this shortcoming is to nest tuples in the 8th component.
+\Csharp allows accessing a component of a tuple by using the field @Item$N$@ for components 1 through 7, and @Rest@ for the nested tuple.
+
+
+% TODO: cite 5.3 https://docs.python.org/3/tutorial/datastructures.html
+In Python, tuples are immutable sequences that provide packing and unpacking operations.
+While the tuple itself is immutable, and thus does not allow the assignment of components, there is nothing preventing a component from being internally mutable.
+The components of a tuple can be accessed by unpacking into multiple variables, indexing, or via field name, like D.
+Tuples support multiple assignment through a combination of packing and unpacking, in addition to the common sequence operations.
+
+% TODO: cite https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-ID448
+Swift, like D, provides named tuples, with components accessed by name, index, or via extractors.
+Tuples are primarily used for returning multiple values from a function.
+In Swift, @Void@ is an alias for the empty tuple, and there are no single element tuples.
+
+\section{Variadic Functions}
+\label{sec:variadic_functions}
+In statically-typed programming languages, functions are typically defined to receive a fixed number of arguments of specified types.
+Variadic argument functions provide the ability to define a function that can receive a theoretically unbounded number of arguments.
+
+C provides a simple implementation of variadic functions.
+A function whose parameter list ends with @, ...@ is a variadic function.
+Among the most common variadic functions is @printf@.
+\begin{cfacode}
+int printf(const char * fmt, ...);
+printf("%d %g %c %s", 10, 3.5, 'X', "a string");
+\end{cfacode}
+Through the use of a format string, @printf@ allows C programmers to print any of the standard C data types.
+Still, @printf@ is extremely limited, since the format codes are specified by the C standard, meaning users cannot define their own format codes to extend @printf@ for new data types or new formatting rules.
+
+C provides manipulation of variadic arguments through the @va_list@ data type, which abstracts details of the manipulation of variadic arguments.
+Since the variadic arguments are untyped, it is up to the function to interpret any data that is passed in.
+Additionally, the interface to manipulate @va_list@ objects is essentially limited to advancing to the next argument, without any built-in facility to determine when the last argument is read.
+This requires the use of an \emph{argument descriptor} to pass information to the function about the structure of the argument list, including the number of arguments and their types.
+The format string in @printf@ is one such example of an argument descriptor.
+\begin{cfacode}
+int f(const char * fmt, ...) {
+  va_list args;
+  va_start(args, fmt);  // initialize va_list
+  for (const char * c = fmt; *c != '\0'; ++c) {
+    if (*c == '%') {
+      ++c;
+      switch (*c) {
+        case 'd': {
+          int i = va_arg(args, int);  // have to specify type
+          // ...
+          break;
+        }
+        case 'g': {
+          double d = va_arg(args, double);
+          // ...
+          break;
+        }
+        ...
+      }
+    }
+  }
+  va_end(args);
+  return ...;
+}
+\end{cfacode}
+Every case must be handled explicitly, since the @va_arg@ macro requires a type argument to determine how the next set of bytes is to be interpreted.
+Furthermore, if the user makes a mistake, compile-time checking is typically restricted to standard format codes and their corresponding types.
+In general, this means that C's variadic functions are not type-safe, making them difficult to use properly.
+
+% When arguments are passed to a variadic function, they undergo \emph{default argument promotions}.
+% Specifically, this means that
+
+\CCeleven added support for \emph{variadic templates}, which add much needed type-safety to C's variadic landscape.
+It is possible to use variadic templates to define variadic functions and variadic data types.
+\begin{cppcode}
+void print(int);
+void print(char);
+void print(double);
+...
+
+void f() {}    // base case
+
+template<typename T, typename... Args>
+void f(const T & arg, const Args &... rest) {
+  print(arg);  // print the current element
+  f(rest...);  // handle remaining arguments recursively
+}
+\end{cppcode}
+Variadic templates work largely through recursion on the \emph{parameter pack}, which is the argument with @...@ following its type.
+A parameter pack matches 0 or more elements, which can be types or expressions depending on the context.
+Like other templates, variadic template functions rely on an implicit set of constraints on a type, in this example a @print@ routine.
+That is, it is possible to use the @f@ routine any any type provided there is a corresponding @print@ routine, making variadic templates fully open to extension, unlike variadic functions in C.
+
+Recent \CC standards (\CCfourteen, \CCseventeen) expand on the basic premise by allowing variadic template variables and providing convenient expansion syntax to remove the need for recursion in some cases, amongst other things.
+
+% D has variadic templates that deserve a mention http://dlang.org/ctarguments.html
+
+In Java, a variadic function appears similar to a C variadic function in syntax.
+\begin{javacode}
+int sum(int... args) {
+  int s = 0;
+  for (int x : args) {
+    s += x;
+  }
+  return s;
+}
+
+void print(Object... objs) {
+  for (Object obj : objs) {
+    System.out.print(obj);
+  }
+}
+
+print("The sum from 1 to 10 is ", sum(1,2,3,4,5,6,7,8,9,10), ".\n");
+\end{javacode}
+The key difference is that Java variadic functions are type-safe, because they specify the type of the argument immediately prior to the ellipsis.
+In Java, variadic arguments are syntactic sugar for arrays, allowing access to length, subscripting operations, and for-each iteration on the variadic arguments, among other things.
+Since the argument type is specified explicitly, the top-type @Object@ can be used to accept arguments of any type, but to do anything interesting on the argument requires a down-cast to a more specific type, landing Java in a similar situation to C in that writing a function open to extension is difficult.
+
+The other option is to restrict the number of types that can be passed to the function by using a more specific type.
+Unfortunately, Java's use of nominal inheritance means that types must explicitly inherit from classes or interfaces in order to be considered a subclass.
+The combination of these two issues greatly restricts the usefulness of variadic functions in Java.
