Advertisement

Codata in Action

  • Paul Downen
  • Zachary SullivanEmail author
  • Zena M. Ariola
  • Simon Peyton Jones
Open Access
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 11423)

Abstract

Computer scientists are well-versed in dealing with data structures. The same cannot be said about their dual: codata. Even though codata is pervasive in category theory, universal algebra, and logic, the use of codata for programming has been mainly relegated to representing infinite objects and processes. Our goal is to demonstrate the benefits of codata as a general-purpose programming abstraction independent of any specific language: eager or lazy, statically or dynamically typed, and functional or object-oriented. While codata is not featured in many programming languages today, we show how codata can be easily adopted and implemented by offering simple inter-compilation techniques between data and codata. We believe codata is a common ground between the functional and object-oriented paradigms; ultimately, we hope to utilize the Curry-Howard isomorphism to further bridge the gap.

Keywords

Codata Lambda-calculi Encodings Curry-Howard Function programming Object-oriented programming 

1 Introduction

Functional programming enjoys a beautiful connection to logic, known as the Curry-Howard correspondence, or proofs as programs principle [22]; results and notions about a language are translated to those about proofs, and vice-versa [17]. In addition to expressing computation as proof transformations, this connection is also fruitful for education: everybody would understand that the assumption “an x is zero” does not mean “every x is zero,” which in turn explains the subtle typing rules for polymorphism in programs. The typing rules for modules are even more cryptic, but knowing that they correspond exactly to the rules for existential quantification certainly gives us more confidence that they are correct! While not everything useful must have a Curry-Howard correspondence, we believe finding these delightful coincidences where the same idea is rediscovered many times in both logic and programming can only be beneficial [42].

One such instance involves codata. In contrast with the mystique it has as a programming construct, codata is pervasive in mathematics and logic, where it arises through the lens of duality. The most visual way to view the duality is in the categorical diagrams of sums versus products—the defining arrows go into a sum and come out of a product—and in algebras versus coalgebras [25]. In proof theory, codata has had an impact on theorem proving [5] and on the foundation of computation via polarity [29, 45]. Polarity recognizes which of two dialogic actors speaks first: the proponent (who seeks to verify or prove a fact) or the opponent (who seeks to refute the fact).

The two-sided, interactive view appears all over the study of programming languages, where data is concerned about how values are constructed and codata is concerned about how they are used [15]. Sometimes, this perspective is readily apparent, like with session types [7] which distinguish internal choice (a provider’s decision) versus external choice (a client’s decision). But other occurrences are more obscure, like in the semantics of PCF (i.e. the call-by-name \(\lambda \)-calculus with numbers and general recursion). In PCF, the result of evaluating a program must be of a ground type in order to respect the laws of functions (namely \(\eta \)) [32]. This is not due to differences between ground types versus “higher types,” but to the fact that data types are directly observable, whereas codata types are only indirectly observable via their interface.

Clearly codata has merit in theoretical pursuits; we think it has merit in practical ones as well. The main application of codata so far has been for representing infinite objects and coinductive proofs in proof assistants [1, 39]. However, we believe that codata also makes for an important general-purpose programming feature. Codata is a bridge between the functional and object-oriented paradigms; a common denominator between the two very different approaches to programming. On one hand, functional languages are typically rich in data types—as many as the programmer wants to define via Open image in new window declarations—but has a paucity of codata types (usually just function types). On the other hand, object-oriented languages are rich in codata types—programmer-defined in terms of classes or interfaces—but a paucity of data types (usually just primitives like booleans and numbers). We illustrate this point with a collection of example applications that arise in both styles of programming, including common encodings, demand-driven programming, abstraction, and Hoare-style reasoning.

While codata types can be seen in the shadows behind many examples of programming—often hand-compiled away by the programmer—not many functional languages have native support for them. To this end, we demonstrate a pair of simple compilation techniques between a typical core functional language (with data types) and one with codata. One direction—based on the well-known visitor pattern from object-oriented programming—simultaneously shows how to extend an object-oriented language with data types (as is done by Scala) and how to compile core functional programs to a more object-oriented setting (e.g. targeting a backend like JavaScript or the JVM). The other shows how to add native codata types to functional languages by reducing them to commonly-supported data types and how to compile a “pure” object-oriented style of programming to a functional setting. Both of these techniques are macro-expansions that are not specific to any particular language, as they work with both statically and dynamically typed disciplines, and they preserve the well-typed status of programs without increasing the complexity of the types involved.

Our claim is that codata is a universal programming feature that has been thus-far missing or diminished in today’s functional programming languages. This is too bad, since codata is not just a feature invented for the convenience of programmers, but a persistent idea that has sprung up over and over from the study of mathematics, logic, and computation. We aim to demystify codata, and en route, bridge the wide gulf between the functional and object-oriented paradigms. Fortunately, it is easy for most mainstream languages to add or bring out codata today without a radical change to their implementation. But ultimately, we believe that the languages of the future should incorporate both data and codata outright. To that end, our contributions are to:
  • (Section 2) Illustrate the benefits of codata in both theory and practice: (1) a decomposition of well-known \(\lambda \)-calculus encodings by inverting the priority of construction and destruction; (2) a first-class abstraction mechanism; (3) a method of demand-driven programming; and (4) a static type system for representing Hoare-style invariants on resource use.

  • (Section 3) Provide simple transformations for compiling data to codata, and vice-versa, which are appropriate for languages with different evaluation strategies (eager or lazy) and type discipline (static or dynamic).

  • (Section 4) Demonstrate various implementations of codata for general-purpose programming in two ways: (1) an extension of Haskell with codata; and (2) a prototype language that compiles to several languages of different evaluation strategies, type disciplines, and paradigms.

2 The Many Faces of Codata

Codata can be used to solve other problems in programming besides representing infinite objects and processes like streams and servers [1, 39]. We start by presenting codata as a merger between theory and practice, whereby encodings of data types in an object-oriented style turn out to be a useful intermediate step in the usual encodings of data in the \(\lambda \)-calculus. Demand-driven programming is considered a virtue of lazy languages, but codata is a language-independent tool for capturing this programming idiom. Codata exactly captures the essence of procedural abstraction, as achieved with \(\lambda \)-abstractions and objects, with a logically founded formalism [16]. Specifying pre- and post-conditions of protocols, which is available in some object systems [14], is straightforward with indexed, recursive codata types, i.e. objects with guarded methods [40].

2.1 Church Encodings and Object-Oriented Programming

Crucial information structures, like booleans, numbers, and lists can be encoded in the untyped \(\lambda \)-calculus (a.k.a. Church encodings) or in the typed polymorphic \(\lambda \)-calculus (a.k.a. Böhm-Berarducci [9] encodings). It is quite remarkable that data structures can be simulated with just first-class, higher-order functions. The downside is that these encodings can be obtuse at first blush, and have the effect of obscuring the original program when everything is written with just \(\lambda \)s and application. For example, the \(\lambda \)-representation of the boolean value Open image in new window , the first projection out of a pair, and the constant function K are all expressed as \(\lambda x{.}\lambda y{.}x\), which is not that immediately evocative of its multi-purpose nature.

Object-oriented programmers have also been representing data structures in terms of objects. This is especially visible in the Smalltalk lineage of languages like Scala, wherein an objective is that everything that can be an object is. As it turns out, the object-oriented features needed to perform this representation technique are exactly those of codata. That is because Church-style encodings and object-oriented representations of data all involve switching focus from the way values are built (i.e. introduced) to the way they are used (i.e. eliminated).

Consider the representation of Boolean values as an algebraic data type. There may be many ways to use a Open image in new window value. However, it turns out that there is a most-general eliminator of Open image in new window the expression Open image in new window . This basic construct can be used to define all the other uses for Open image in new window . Instead of focusing on the constructors Open image in new window and Open image in new window let’s then focus on this most-general form of Open image in new window elimination; this is the essence of the encodings of booleans in terms of objects. In other words, booleans can be thought of as objects that implement a single method: Open image in new window . So that the expression Open image in new window would instead be written as ( Open image in new window ). We then define the true and false values in terms of their reaction to Open image in new window :
Or alternatively, we can write the same definition using copatterns, popularized for use in the functional paradigm by Abel et al. [1] by generalizing the usual pattern-based definition of functions by multiple clauses, as:

This works just like equational definitions by pattern-matching in functional languages: the expression to the left of the equals sign is the same as the expression to the right (for any binding of Open image in new window and Open image in new window ). Either way, the net result is that ( Open image in new window ) is Open image in new window , whereas ( Open image in new window ) is Open image in new window .

This covers the object-based presentation of booleans in a dynamically typed language, but how do static types come into play? In order to give a type description of the above boolean objects, we can use the following interface, analogous to a Java interface:

This declaration is dual to a data declaration in a functional language: data declarations define the types of constructors (which produce values of the data type) and codata declarations define the types of destructors (which consume values of the codata type) like Open image in new window . The reason that the Open image in new window observation introduces its own polymorphic type Open image in new window is because an if-then-else might return any type of result (as long as both branches agree on the type). That way, both the two objects Open image in new window and Open image in new window above are values of the codata type Open image in new window .

At this point, the representation of booleans as codata looks remarkably close to the encodings of booleans in the \(\lambda \)-calculus! Indeed, the only difference is that in the \(\lambda \)-calculus we “anonymize” booleans. Since they reply to only one request, that request name can be dropped. We then arrive at the familiar encodings in the polymorphic \(\lambda \)-calculus:In addition, the invocation of the Open image in new window method just becomes ordinary function application; Open image in new window of type a is written as b a x y. Otherwise, the definition and behavior of booleans as either codata types or as polymorphic functions are the same.
This style of inverting the definition of data types—either into specific codata types or into polymorphic functions—is also related to another concept in object-oriented programming. First, consider how a functional programmer would represent a binary Open image in new window (with integer-labeled leaves) and a Open image in new window function that traverses a tree by converting the labels on all leaves and combining the results of sub-trees:
The above code relies on pattern-matching on values of the Open image in new window data type and higher-order functions Open image in new window and Open image in new window for accumulating the result. Now, how might an object-oriented programmer tackle the problem of traversing a tree-like structure? The visitor pattern! With this pattern, the programmer specifies a “visitor” object which contains knowledge of what to do at every node of the tree, and tree objects must be able to accept a visitor with a method that will recursively walk down each subcomponent of the tree. In a pure style—which returns an accumulated result directly instead of using mutable state as a side channel for results—the visitor pattern for a simple binary tree interface will look like:
And again, we can write this same code more elegantly, without the need to break apart the two arguments across the equal sign with a manual abstraction, using copatterns as:
Notice how the above code is just an object-oriented presentation of the following encoding of binary trees into the polymorphic \(\lambda \)-calculus:
$$\begin{aligned}&\begin{aligned} Tree&= \forall a. TreeVisitor ~a \rightarrow a&\qquad TreeVisitor ~a&= ( Int \rightarrow a) \times (a \rightarrow a \rightarrow a) \end{aligned} \\&leaf : Int \rightarrow Tree \\&leaf ~(x{:} Int ) = \varLambda a{.}\lambda v{:} TreeVisitor ~a.~ ( fst ~ v) ~ x \\&branch : \forall a. Tree \rightarrow Tree \rightarrow Tree \\&branch ~(l{:} Tree )~(r{:} Tree ) = \varLambda a{.}\lambda v{:} TreeVisitor ~a.~ ( snd ~ v) ~ (l~a~v) ~ (r~a~v) \end{aligned}$$
The only essential difference between this \(\lambda \)-encoding of trees versus the \(\lambda \)-encoding of booleans above is currying: the representation of the data type \( Tree \) takes a single product \( TreeVisitor ~a\) of the necessary arguments, whereas the data type \( Bool \) takes the two necessary arguments separately. Besides this easily-converted difference of currying, the usual Böhm-Berarducci encodings shown here correspond to a pure version of the visitor pattern.

2.2 Demand-Driven Programming

In “Why functional programming matters” [23], Hughes motivates the utility of practical functional programming through its excellence in compositionality. When designing programs, one of the goals is to decompose a large problem into several manageable sub-problems, solve each sub-problem in isolation, and then compose the individual parts together into a complete solution. Unfortunately, Hughes identifies some examples of programs which resist this kind of approach.

In particular, numeric algorithms—for computing square roots, derivatives integrals—rely on an infinite sequence of approximations which converge on the true answer only in the limit of the sequence. For these numeric algorithms, the decision on when a particular approximation in the sequence is “close enough” to the real answer lies solely in the eyes of the beholder: only the observer of the answer can say when to stop improving the approximation. As such, standard imperative implementations of these numeric algorithms are expressed as a single, complex loop, which interleaves both the concerns of producing better approximations with the termination decision on when to stop. Even more complex is the branching structure of the classic minimax algorithm from artificial intelligence for searching for reasonable moves in two-player games like chess, which can have an unreasonably large (if not infinite) search space. Here, too, there is difficulty separating generation from selection, and worse there is the intermediate step of pruning out uninteresting sub-trees of the search space (known as alpha-beta pruning). As a result, a standard imperative implementation of minimax is a single, recursive function that combines all the tasks—generation, pruning, estimation, and selection—at once.

Hughes shows how both instances of failed decomposition can be addressed in functional languages through the technique of demand-driven programming. In each case, the main obstacle is that the control of how to drive the next step of the algorithm—whether to continue or not—lies with the consumer. The producer of potential approximations and game states, in contrast, should only take over when demanded by the consumer. By giving primary control to the consumer, each of these problems can be decomposed into sensible sub-tasks, and recomposed back together. Hughes uses lazy evaluation, as found in languages like Miranda and Haskell, in order to implement the demand-driven algorithms. However, the downside of relying on lazy evaluation is that it is a whole-language decision: a language is either lazy by default, like Haskell, or not, like OCaml. When working in a strict language, expressing these demand-driven algorithms with manual laziness loses much of their original elegance [33].

In contrast, a language should directly support the capability of yielding control to the consumer independently of the language being strict or lazy; analogously to what happens with lambda abstractions. An abstraction computes on-demand, why is this property relegated to this predefined type only? In fact, the concept of codata also has this property. As such, it allows us to describe demand-driven programs in an agnostic way which works just as well in Haskell as in OCaml without any additional modification. For example, we can implement Hughes’ demand-driven AI game in terms of codata instead of laziness. To represent the current game state, and all of its potential developments, we can use an arbitrarily-branching tree codata type.
The task of generating all potential future boards from the current board state produces one of these tree objects, described as follows (where Open image in new window of type Open image in new window generates a list of possible moves):
Notice that the tree might be finite, such as in the game of Tic-Tac-Toe. However, it would still be inappropriate to waste resources fully generating all moves before determining which are even worth considering. Fortunately, the fact that the responses of a codata object are only computed when demanded means that the consumer is in full control over how much of the tree is generated, just as in Hughes’ algorithm. This fact lets us write the following simplistic Open image in new window function which cuts off sub-trees at a fixed depth.
The more complex alpha-beta pruning algorithm can be written as its own pass, similar to Open image in new window above. Just like Hughes’ original presentation, the evaluation of the best move for the opponent is the composition of a few smaller functions:

What is the difference between this codata version of minimax and the one presented by Hughes that makes use of laziness? They both compute on-demand which makes the game efficient. However, demand-driven code written with codata can be easily ported between strict and lazy languages with only syntactic changes. In other words, codata is a general, portable, programming feature which is the key for compositionality in program design.1

2.3 Abstraction Mechanism

In the pursuit of scalable and maintainable program design, the typical followup to composability is abstraction. The basic purpose of abstraction is to hide certain implementation details so that different parts of the code base need not be concerned with them. For example, a large program will usually be organized into several different parts or “modules,” some of which may hold general-purpose “library” code and others may be application-specific “clients” of those libraries. Successful abstractions will leverage tools of the programming language in question so that there is a clear interface between libraries and their clients, codifying which details are exposed to the client and which are kept hidden inside the library. A common such detail to hide is the concrete representation of some data type, like strings and collections. Clear abstraction barriers give freedom to both the library implementor (to change hidden details without disrupting any clients) as well as the client (to ignore details not exposed by the interface).

Reynolds [35] identified, and Cook [12] later elaborated on, two different mechanisms to achieve this abstraction: abstract data types and procedural abstraction. Abstract data types are crisply expressed by the Standard ML module system, based on existential types, which serves as a concrete practical touchstone for the notion. Procedural abstraction is pervasively used in object-oriented languages. However, due to the inherent differences among the many languages and the way they express procedural abstraction, it may not be completely clear of what the “essence” is, the way existential types are the essence of modules. What is the language-agnostic representation of procedural abstraction? Codata! The combination of observation-based interfaces, message-passing, and dynamic dispatch are exactly the tools needed for procedural abstraction. Other common object-oriented features—like inheritance, subtyping, encapsulation, and mutable state—are orthogonal to this particular abstraction goal. While they may be useful extensions to codata for accomplishing programming tasks, only pure codata itself is needed to represent abstraction.

Specifying a codata type is giving an interface—between an implementation and a client—so that instances of the type (implementations) can respond to requests (clients). In fact, method calls are the only way to interact with our objects. As usual, there is no way to “open up” a higher-order function—one example of a codata type—and inspect the way it was implemented. The same intuition applies to all other codata types. For example, Cook’s [12] procedural “set” interface can be expressed as a codata type with the following observations:
Every single object of type Open image in new window will respond to these observations, which is the only way to interact with it. This abstraction barrier gives us the freedom of defining several different instances of Open image in new window objects that can all be freely composed with one another. One such instance of Open image in new window uses a list to keep track of a hidden state of the contained elements (where Open image in new window checks if a particular number is an element of the given list, and the operation Open image in new window is the standard functional fold):
But of course, many other instances of Open image in new window can also be given. For example, this codata type interface also makes it possible to represent infinite sets like the set Open image in new window of all even numbers which is defined in terms of the more general Open image in new window that unions all even numbers with some other set (where the function Open image in new window checks if a number is even):

Because of the natural abstraction mechanism provided by codata, different Open image in new window implementations can interact with each other. For example, we can union a finite set and Open image in new window together because both definitions of Open image in new window know nothing of the internal structure of the other Open image in new window . Therefore, all we can do is apply the observations provided by the Open image in new window codata type.

While sets of numbers are fairly simplistic, there are many more practical real-world instances of the procedural abstraction provided by codata to be found in object-oriented languages. For example, databases are a good use of abstraction, where basic database queries can be represented as the observations on table objects. A simplified interface to a database table (containing rows of type Open image in new window ) with selection, deletion, and insertion, is given as follows:
On one hand, specific implementations can be given for connecting to and communicating with a variety of different databases—like Postgres, MySQL, or just a simple file system—which are hidden behind this interface. On the other hand, clients can write generic operations independently of any specific database, such as copying rows from one table to another or inserting a row into a list of compatible tables:

In addition to abstracting away the details of specific databases, both Open image in new window and Open image in new window can communicate between completely different databases by just passing in the appropriate object instances, which all have the same generic type. Another use of this generality is for testing. Besides the normal instances of Open image in new window which perform permanent operations on actual tables, one can also implement a fictitious simulation which records changes only in temporary memory. That way, client code can be seamlessly tested by running and checking the results of simulated database operations that have no external side effects by just passing pure codata objects.

2.4 Representing Pre- and Post-Conditions

The extension of data types with indexes (a.k.a. generalized algebraic data types) has proven useful to statically verify a data structure’s invariant, like for red-black trees [43]. With indexed data types, the programmer can inform the static type system that a particular value of a data type satisfies some additional conditions by constraining the way in which it was constructed. Unsurprisingly, indexed codata types are dual and allow the creator of an object to constrain the way it is going to be used, thereby adding pre- and post-conditions to the observations of the object. In other words, in a language with type indexes, codata enables the programmer to express more information in its interface.

This additional expressiveness simplifies applications that rely on a type index to guard observations. Thibodeau et al. [40] give examples of such programs, including an automaton specification where its transitions correspond to an observation that changes a pre- and post-condition in its index, and a fair resource scheduler where the observation of several resources is controlled by an index tracking the number of times they have been accessed. For concreteness, let’s use an indexed codata type to specify safe protocols as in the following example from an object-oriented language with guarded methods:

This example comes from DeLine and Fähndrich [14], where they present an extension to C\(^\sharp \) constraining the pre- and post-conditions for method calls. If we have an instance of this Open image in new window interface, then observing it through the above methods can return new socket objects with a different index. The index thereby governs the order in which clients are allowed to apply these methods. A socket will start with the index Open image in new window . The only way to use a Open image in new window is to Open image in new window it, and the only way to use a Open image in new window is to Open image in new window it. This forces us to follow a protocol when initializing a Open image in new window .

Intermezzo 1

This declaration puts one aspect in the hands of the programmer, though. A client can open a socket and never close it, hogging the resource. We can remedy this problem with linear types, which force us to address any loose ends before finishing the program. With linear types, it would be a type error to have a lingering Open image in new window socket laying around at the end of the program, and a call to Open image in new window would use it up. Furthermore, linear types would ensure that outdated copies of Open image in new window objects cannot be used again, which is especially appropriate for actions like Open image in new window which is meant to transform a Open image in new window socket into a Open image in new window one, and likewise for Open image in new window which transforms a Open image in new window socket into a Open image in new window one. Even better, enhancing linear types with a more sophisticated notion of ownership—like in the Rust programming language which differentiates a permanent transfer of ownership from temporarily borrowing it—makes this resource-sensitive interface especially pleasant. Observations like Open image in new window , Open image in new window , and Open image in new window which are meant to fully consume the observed object would involve full ownership of the object itself to the method call and effectively replace the old object with the returned one. In contrast, observations like Open image in new window and Open image in new window which are meant to be repeated on the same object would merely borrow the object for the duration of the action so that it could be used again.

3 Inter-compilation of Core Calculi

We saw previously examples of using codata types to replicate well-known encodings of data types into the \(\lambda \)-calculus. Now, let’s dive in and show how data and codata types formally relate to one another. In order to demonstrate the relationship, we will consider two small languages that extend the common polymorphic \(\lambda \)-calculus: Open image in new window extends \(\lambda \) with user-defined algebraic data types, and Open image in new window extends \(\lambda \) with user-defined codata types. In the end, we will find that both of these foundational languages can be inter-compiled into one another. Data can be represented by codata via the visitor pattern ( Open image in new window ). Codata can be represented by data by tabulating the possible answers of objects ( Open image in new window ).

In essence, this demonstrates how to compile programs between the functional and object-oriented paradigms. The Open image in new window direction shows how to extend existing functional languages (like OCaml, Haskell, or Racket) with codata objects without changing their underlying representation. Dually, the Open image in new window direction shows how to compile functional programs with data types into an object-oriented target language (like JavaScript).

Each of the encodings are macro expansions, in the sense that they leave the underlying base \(\lambda \)-calculus constructs of functions, applications, and variables unchanged (as opposed to, for example, continuation-passing style translations). They are defined to operate on untyped terms, but they also preserve typability when given well-typed terms. The naïve encodings preserve the operational semantics of the original term, according to a call-by-name semantics. We also illustrate how the encodings can be modified slightly to correctly simulate the call-by-value operational semantics of the source program. To conclude, we show how the languages and encodings can be generalized to more expressive type systems, which include features like existential types and indexed types (a.k.a. generalized algebraic data types and guarded methods).

Notation. We use both an overline \(\overline{t}\) and dots \(t_1 \dots \) to indicate a sequence of terms t (and likewise for types, variables, etc.). The arrow type Open image in new window means Open image in new window ; when n is 0, it is not a function type, i.e. just the codomain Open image in new window . The application Open image in new window means Open image in new window ; when n is 0, it is not a function application, but the constant Open image in new window . We write a single step of an operational semantics with the arrow \(\mapsto \), and many steps (i.e. its reflexive-transitive closure) as Open image in new window . Operational steps may occur within an evaluation context E, i.e. \(t \mapsto t'\) implies that \(E[t] \mapsto E[t']\).

3.1 Syntax and Semantics

We present the syntax and semantics of the base language and the two extensions Open image in new window and Open image in new window . For the sake of simplicity, we keep the languages as minimal as possible to illustrate the main inter-compilations. Therefore, Open image in new window and Open image in new window do not contain recursion, nested (co)patterns, or indexed types. The extension with recursion is standard, and an explanation of compiling (co)patterns can be found in [11, 38, 39]. Indexed types are later discussed informally in Sect. 3.6.
Fig. 1.

Polymorphic \(\lambda \)-calculus: the base language

The Base Language. We will base both our core languages of interest on a common starting point: the polymorphic \(\lambda \)-calculus as shown in Fig. 1.2 This is the standard simply typed \(\lambda \)-calculus extended with impredicative polymorphism (a.k.a. generics). There are only three forms of terms (variables x, applications t u, and function abstractions \(\lambda x{.}e\)) and three forms of types (type variables a, function types \(\tau \rightarrow \rho \), and polymorphic types \(\forall a{.}\tau \)). We keep the type abstraction and instantiation implicit in programs—as opposed to explicit as in System F—for two reasons. First, this more accurately resembles the functional languages in which types are inferred, as opposed to mandatory annotations explicit within the syntax of programs. Second, it more clearly shows how the translations that follow do not rely on first knowing the type of terms, but apply to any untyped term. In other words, the compilation techniques are also appropriate for dynamically typed languages like Scheme and Racket.

Figure 1 reviews both the standard call-by-name and call-by-value operational semantics for the \(\lambda \)-calculus. As usual, the difference between the two is that in call-by-value, the argument of a function call is evaluated prior to substitution, whereas in call-by-name the argument is substituted first. This is implied by the different set of evaluation contexts (E) and the fact that the operational rule uses a more restricted notion of value (V) for substitutable arguments in call-by-value. Note that, there is an interplay between evaluation and typing. In a more general setting where effects are allowed, the typing rule for introducing polymorphism (i.e. the rule with \(S : \forall a{.}\tau \) in the conclusion) is only safe for substitutable terms, which imposes the well-known the value restriction for call-by-value (limiting S to values), but requires no such restriction in call-by-name where every term is a substitutable value (letting S be any term).
Fig. 2.

Open image in new window : Extending polymorphic \(\lambda \)-calculus with data types

A Language with Data. The first extension of the \(\lambda \)-calculus is with user-defined data types, as shown in Fig. 2; it corresponds to a standard core language for statically typed functional languages. Data declarations introduce a new type constructor ( Open image in new window ) as well as some number of associated constructors ( Open image in new window ) that build values of that data type. For simplicity, the list of branches in a case expression are considered unordered and non-overlapping (i.e. no two branches for the same constructor within a single case expression). The types of constructors are given alongside free variables in \(\varGamma \), and the typing rule for constructors requires they be fully applied. We also assume an additional side condition to the typing rule for case expressions that the branches are exhaustive (i.e. every constructor of the data type in question is covered as a premise).

Figure 2 presents the extension to the operational semantics from Fig. 1, which is also standard. The new evaluation rule for data types reduces a case expression matched with an applied constructor. Note that since the branches are unordered, the one matching the constructor is chosen out of the possibilities and the parameters of the constructor are substituted in the branch’s pattern. There is also an additional form of constructed values: in call-by-name any constructor application is a value, whereas in call-by-value only constructors parameterized by other values is a value. As such, call-by-value goes on to evaluate constructor parameters in advance, as shown by the extra evaluation context. In both evaluation strategies, there is a new form of evaluation context that points out the discriminant of a case expression, since it is mandatory to determine which constructor was used before deciding the appropriate branch to take.
Fig. 3.

Open image in new window : Extending polymorphic \(\lambda \)-calculus with codata types

A Language with Codata. The second extension of the \(\lambda \)-calculus is with user-defined codata types, as shown in Fig. 3. Codata declarations in Open image in new window define a new type constructor ( Open image in new window ) along with some number of associated destructors ( Open image in new window ) for projecting responses out of values of a codata type. The type level of Open image in new window corresponds directly to Open image in new window . However, at the term level, we have codata observations of the form Open image in new window using “dot notation”, which can be thought of as sending the message Open image in new window to the object t or as a method invocation from object-oriented languages. Values of codata types are introduced in the form Open image in new window , which lists each response this value gives to all the possible destructors of the type. As with case expressions, we take the branches to be unordered and non-overlapping for simplicity.

Interestingly, the extension of the operational semantics with codata—the values, evaluation contexts, and reduction rules—are identical for both call-by-name and call-by-value evaluation. In either evaluation strategy, a codata object Open image in new window is considered a value and the codata observation Open image in new window must evaluate t no matter what to continue, leading to the same form of evaluation context Open image in new window . The additional evaluation rule selects and invokes the matching branch of a codata object and is the same regardless of the evaluation strategy.

Note that the reason that values of codata types are the same in any evaluation strategy is due to the fact that the branches of the object are only ever evaluated on-demand, i.e. when they are observed by a destructor, similar to the fact that the body of a function is only ever evaluated when the function is called. This is the semantic difference that separates codata types from records found in many programming languages. Records typically map a collection of labels to a collection of values, which are evaluated in advance in a call-by-value language similar to the constructed values of data types. Whereas with codata objects, labels map to behavior which is only invoked when observed.

The additional typing rules for Open image in new window are also given in Fig. 3. The rule for typing Open image in new window is analogous to a combination of type instantiation and application, when viewing Open image in new window as a function of the given type. The rule for typing a codata object, in contrast, is similar to the rule for typing a case expression of a data type. However, in this comparison, the rule for objects is partially “upside down” in the sense that the primary type in question ( Open image in new window ) appears in the conclusion rather than as a premise. This is the reason why there is one less premise for typing codata objects than there is for typing data case expressions. As with that rule, we assume that the branches are exhaustive, so that every destructor of the codata type appears in the premise.

3.2 Compiling Data to Codata: The Visitor Pattern

In Sect. 2.1, we illustrated how to convert a data type representing trees into a codata type. This encoding corresponds to a rephrasing of the object-oriented visitor pattern to avoid unnecessary side-effects. Now lets look more generally at the pattern, to see how any algebraic data type in Open image in new window can be encoded in terms of codata in Open image in new window .

The visitor pattern has the net effect of inverting the orientation of a data declaration (wherein construction comes first) into codata declarations (wherein destruction comes first). This reorientation can be used for compiling user-defined data types in Open image in new window to codata types in Open image in new window as shown in Fig. 4. As with all of the translations we will consider, this is a macro expansion since the syntactic forms from the base \(\lambda \)-calculus are treated homomorphically (i.e. Open image in new window , Open image in new window , and Open image in new window ). Furthermore, this translation also perfectly preserves types, since the types of terms are exactly the same after translation (i.e. Open image in new window ).
Fig. 4.

Open image in new window mapping data to codata via the visitor pattern

Notice how each data type ( Open image in new window ) gets represented by two codata types: the “visitor” ( Open image in new window ) which says what to do with values made with each constructor, and the type itself ( Open image in new window ) which has one method which accepts a visitor and returns a value of type b. An object of the codata type, then, must be capable of accepting any visitor, no matter what type of result it returns. Also notice that we include no other methods in the codata type representation of Open image in new window .

At the level of terms, first consider how the case expression of the data type is encoded. The branches of the case (contained within the curly braces) are represented as a first-class object of the visitor type: each constructor is mapped to the corresponding destructor of the same name and the variables bound in the pattern are mapped to parameters of the function returned by the object in each case. The whole case expression itself is then implemented by calling the sole method ( Open image in new window ) of the codata object and passing the branches of the case as the corresponding visitor object. Shifting focus to the constructors, we can now see that they are compiled as objects that invoke the corresponding destructor on any given visitor, and the terms which were parameters to the constructor are now parameters to a given visitor’s destructor. Of course, other uses of the visitor pattern might involve a codata type ( Open image in new window ) with more methods implementing additional functionality besides case analysis. However, we only need the one method to represent data types in Open image in new window because case expressions are the primitive destructor for values of data types in the language.

For example, consider applying the above visitor pattern to a binary tree data type as follows:Note how this encoding differs from the one that was given in Sect. 2.1 since the Open image in new window method is non-recursive whereas the Open image in new window method was recursive, in order to model a depth-first search traversal of the tree.
Of course, other operations, like the Open image in new window function, could be written in terms of case expressions and recursion as usual by an encoding with above method calls. However, it is possible to go one step further and include other primitive destructors—like recursors or iterators in the style of Gödel’s system T—by embedding them as other methods of the encoded codata type. For example, we can represent Open image in new window as a primitive destructor as it was in Sect. 2.1 in addition to non-recursive case analysis by adding an alternative visitor Open image in new window and one more destructor to the generated Open image in new window codata type like so:where the definition of Open image in new window and the encoding of case expressions is the same. In other words, this compilation technique can generalize to as many primitive observations and recursion schemes as desired.

3.3 Compiling Codata to Data: Tabulation

Having seen how to compile data to codata, how can we go the other way? The reverse compilation would be useful for extending functional languages with user-defined codata types, since many functional languages are compiled to a core representation based on the \(\lambda \)-calculus with data types.

Intuitively, the declared data types in Open image in new window can be thought of as “sums of products.” In contrast, the declared codata types in Open image in new window can be thought of as “products of functions.” Since both core languages are based on the \(\lambda \)-calculus, which has higher-order functions, the main challenge is to relate the two notions of “products.” The codata sense of products are based on projections out of abstract objects, where the different parts are viewed individually and only when demanded. The data sense of products, instead, are based on tuples, in which all components are laid out in advance in a single concrete structure.

One way to convert codata to data is to tabulate an object’s potential answers ahead of time into a data structure. This is analogous to the fact that a function of type Open image in new window can be alternatively represented by a tuple of type Open image in new window , where the first and second components are the responses of the original function to Open image in new window and Open image in new window , respectively. This idea can be applied to Open image in new window in general as shown in the compilation in Fig. 5.
Fig. 5.

Open image in new window tabulating codata responses with data tuples

A codata declaration of Open image in new window becomes a data declaration with a single constructor ( Open image in new window ) representing a tuple containing the response for each of the original destructors of Open image in new window . At the term level, a codata abstraction is compiled by concretely tabulating each of its responses into a tuple using the Open image in new window constructor. A destructor application returns the specific component of the constructed tuple which corresponds to that projection. Note that, since we assume that each object is exhaustive, the tabulation transformation is relatively straightforward; filling in “missing” method definitions with some error value that can be stored in the tuple at the appropriate index would be done in advance as a separate pre-processing step.

Also notice that there is a special case for non-observable “empty” codata types, which are all collapsed into a single pre-designated Open image in new window data type. The reason for this collapse is to ensure that this compilation preserves typability: if applied to a well-typed term, the result is also well-typed. The complication arises from the fact that when faced with an empty object Open image in new window , we have no idea which constructor to use without being given further typing information. So rather than force type checking or annotation in advance for this one degenerate case, we instead collapse them all into a single data type so that there is no need to differentiate based on the type. In contrast, the translation of non-empty objects is straightforward, since we can use the name of any one of the destructors to determine the codata type it is associated with, which then informs us of the correct constructor to use.

3.4 Correctness

For the inter-compilations between Open image in new window into Open image in new window to be useful in practice, they should preserve the semantics of programs. For now, we focus only on the call-by-name semantics for each of the languages. With the static aspect of the semantics, this means they should preserve the typing of terms.

Proposition 1

(Type Preservation). For each of the Open image in new window and Open image in new window translations: if \(\varGamma \vdash t : \tau \) then Open image in new window (in the call-by-name type system).

Proof

(Sketch). By induction on the typing derivation of \(\varGamma \vdash t : \tau \).

With the dynamic aspect of the semantics, the translations should preserve the outcome of evaluation (either converging to some value, diverging into an infinite loop, or getting stuck) for both typed and untyped terms. This works because each translation preserves the reduction steps, values, and evaluation contexts of the source calculus’ call-by-name operational semantics.

Proposition 2

(Evaluation Preservation). For each of the Open image in new window and Open image in new window translations: Open image in new window if and only if Open image in new window (in the call-by-name semantics).

Proof

(Sketch). The forward (“only if”) implication is a result of the following facts that hold for each translation in the call-by-name semantics:

The reverse (“if”) implication then follows from the fact that the call-by-name operational semantics of both source and target languages is deterministic.

3.5 Call-by-Value: Correcting the Evaluation Order

The presented inter-compilation techniques are correct for the call-by-name semantics of the calculi. But what about the call-by-value semantics? It turns out that the simple translations seen so far do not correctly preserve the call-by-value semantics of programs, but they can be easily fixed by being more careful about how they treat the values of the source and target calculi. In other words, we need to make sure that values are translated to values, and evaluation contexts to evaluation contexts. For instance, the following translation (up to renaming) does not preserve the call-by-value semantics of the source program:The object Open image in new window is a value in call-by-value, and the erroneous response to the Open image in new window will only be evaluated when observed. However, the structure Open image in new window is not a value in call-by-value, because the field \( error \) must be evaluated in advance which causes an error immediately. In the other direction, we could also haveHere, the immediate error in Open image in new window has become incorrectly delayed inside the value Open image in new window .
The solution to this problem is straightforward: we must manually delay computations that are lifted out of (object or \(\lambda \)) abstractions, and manually force computations before their results are hidden underneath abstractions. For the visitor pattern, the correction is to only introduce the codata object on constructed values. We can handle other constructed terms by naming their non-value components in the style of administrative-normalization like so:Conversely, the tabulating translation Open image in new window will cause the on-demand observations of the object to be converted to preemptive components of a tuple structure. To counter this change in evaluation order, a thunking technique can be employed as follows:The two operations can be implemented as Open image in new window and Open image in new window as usual, but can also be implemented as more efficient memoizing operations. With all these corrections, Propositions 1 and 2 also hold for the call-by-value type system and operational semantics.

3.6 Indexed Data and Codata Types: Type Equalities

In the world of types, we have so far only formally addressed inter-compilation between languages with simple and polymorphic types. What about the compilation of indexed data and codata types? It turns out some of the compilation techniques we have discussed so far extend to type indexes without further effort, whereas others need some extra help. In particular, the visitor-pattern-based translation Open image in new window can just be applied straightforwardly to indexed data types:

In this case, the notion of an indexed visitor codata type exactly corresponds to the mechanics of case expressions for GADTs. In contrast, the tabulation translation Open image in new window does not correctly capture the semantics of indexed codata types, if applied naïvely.

Thankfully, there is a straightforward way of “simplifying” indexed data types to more conventional data types using some built-in support for type equalities. The idea is that a constructor with a more specific return type can be replaced with a conventional constructor that is parameterized by type equalities that prove that the normal return type must be the more specific one. The same idea can be applied to indexed codata types as well. A destructor that can only act on a more specific instance of the codata type can instead be replaced by one which works on any instance, but then immediately asks for proof that the object’s type is the more specific one before completing the observation. These two translations, of replacing type indexes with type equalities, are defined as:
This formalizes the intuition that indexed data types can be thought of as enriching constructors to carry around additional constraints that were available at their time of construction, whereas indexed codata types can be thought of as guarding methods with additional constraints that must be satisfied before an observation can be made. Two of the most basic examples of this simplification are for the type declarations which capture the notion of type equality as an indexed data or indexed codata type, which are defined and simplified like so:
With the above ability to simplify away type indexes, all of the presented compilation techniques are easily generalized to indexed data and codata types by composing them with Open image in new window . For practical programming example, consider the following safe stack codata type indexed by its number of elements.
This stack type is safe in the sense that the Open image in new window operation can only be applied to non-empty Open image in new window s. We cannot compile this to a data type via Open image in new window directly, because that translation does not apply to indexed codata types. However, if we first simplify the Open image in new window type via Open image in new window , we learn that we can replace the type of the Open image in new window destructor with Open image in new window , whereas the Open image in new window destructor is already simple, so it can be left alone. That way, for any object Open image in new window , even though a client can initiate the observation Open image in new window , it will never be completed since there is no way to choose a b and prove that Open image in new window equals Open image in new window . Therefore, the net result of the combined Open image in new window translation turns Open image in new window into the following data type, after some further simplification:

Notice how the constructor of this type has two fields; one for Open image in new window and one for Open image in new window , respectively. However, the Open image in new window operation is guarded by a proof obligation: the client can only receive the top integer and remaining stack if he/she proves that the original stack contains a non-zero number of elements.

4 Compilation in Practice

We have shown how data and codata are related through the use of two different core calculi. To explore how these ideas manifest in practice, we have implemented codata in a couple of settings. First, we extended Haskell with codata in order to compare the lazy and codata approaches to demand-driven programming described in Sect. 2.2.3 Second, we have created a prototype language with indexed (co)data types to further explore the interaction between the compilation and target languages. The prototype language does not commit to a particular evaluation strategy, typing discipline, or paradigm; instead this decision is made when compiling a program to one of several backends. The supported backends include functional ones—Haskell (call-by-need, static types), OCaml (call-by-value, static types), and Racket (call-by-value, dynamic types)—as well as the object-oriented JavaScript.4 The following issues of complex copattern matching and sharing applies to both implementations; the performance results on efficiency of memoized codata objects are tested with the Haskell extension for the comparison with conventional Haskell code.

Complex Copattern Matching. Our implementations support nested copatterns so that objects can respond to chains of multiple observations, even though Open image in new window only provides flat copatterns. This extension does not enhance the language expressivity but allows more succinct programs [2]. A flattening step is needed to compile nested copatterns down to a core calculus, which has been explored in previous work by Setzer et al. [37] and Thibodeau [39] and implemented in OCaml by Regis-Gianas and Laforgue [33]. Their flattening algorithm requires copatterns to completely cover the object’s possible observations because the coverage information is used to drive flattening. This approach was refined and incorporated in a dependently typed setting by Cockx and Abel [11]. With our goal of supporting codata independently of typing discipline and coverage analysis, we have implemented the purely syntax driven approach to flattening found in [38]. For example, the Open image in new window function from Sect. 2.2 expands to:
Table 1.

Fibonacci scaling tests for the GHC implementation

n

Time(s) codata

Time(s) data

Allocs(bytes) codata

Allocs(bytes) data

10000

0.02

0.01

10,143,608

6,877,048

100000

0.39

0.27

495,593,464

463,025,832

1000000

19.64

18.54

44,430,524,144

44,104,487,488

Sharing. If codata is to be used instead of laziness for demand-driven programming, then it must have the same performance characteristics, which relies on sharing the results of computations [6]. To test this, we compare the performance of calculating streams of Fibonacci numbers—the poster child for sharing—implemented with both lazy list data types and a stream codata type in Haskell extended with codata. These tests, presented in Table 1, show the speed of the codata version is always slower in terms of run time and allocations than the lazy list version, but the difference is small and the two versions scale at the same rate. These performance tests are evidence that codata shares the same information when compiled to a call-by-need language; this we get for free because call-by-need data constructors—which codata is compiled into via Open image in new window —memoize their fields. In an eager setting, it is enough to use memoized versions of Open image in new window and Open image in new window , which are introduced by the call-by-value compilation described in Sect. 3.5. This sharing is confirmed by the OCaml and Racket backends of the prototype language which find the 100th Fibonacci in less than a second (a task that takes hours without sharing).

As the object-oriented representative, the JavaScript backend is a compilation from data to codata using the visitor pattern presented in Sect. 3.2. Because codata remains codata (i.e. JavaScript objects), an optimization must be performed to ensure the same amount of sharing of codata as the other backends. The solution is to lift out the branches of a codata object, as shown in Fig. 6, where the call-by-need let-bindings can be implemented by Open image in new window and Open image in new window in strict languages as usual. It turns out that this transformation is also needed in an alternative compilation technique presented by Regis-Gianas and Laforgue [33] where codata is compiled to functions, i.e. another form of codata.
Fig. 6.

Memoization of Open image in new window

5 Related Work

Our work follows in spirit of Amin et al.’s [3] desire to provide a minimal theory that can model type parameterization, modules, objects and classes. Another approach to combine type parameterization and modules is also offered by 1ML [36], which is mapped to System F. Amin et al.’s work goes one step further by translating System F to a calculus that directly supports objects and classes. Our approach differs in methodology: instead of searching for a logical foundation of a pre-determined notion of objects, we let the logic guide us while exploring what objects are. Even though there is no unanimous consensus that functional and object-oriented paradigms should be combined, there have been several hybrid languages for combining both styles of programming, including Scala, the Common Lisp Object System [8], Objective ML [34], and a proposed but unimplemented object system for Haskell [30].

Arising out of the correspondence between programming languages, category theory, and universal algebras, Hagino [20] first proposed codata as an extension to ML to remedy the asymmetry created by data types. In the same way that data types represent initial F-algebras, codata types represent final F-coalgebras. These structures were implemented in the categorical programming language Charity [10]. On the logical side of the correspondence, codata arises naturally in the sequent calculus [15, 28, 44] since it provides the right setting to talk about construction of either the provider (i.e. the term) or the client (i.e. the context) side of a computation, and has roots in classical [13, 41] and linear logic [18, 19].

In session-typed languages, which also have a foundation in linear logic, external choice can be seen as a codata (product) type dual to the way internal choice corresponds to a data (sum) type. It is interesting that similar problems arise in both settings. Balzer and Pfenning [7] discuss an issue that shows up in choosing between internal and external choice; this corresponds to choosing between data and codata, known as the expression problem. They [7] also suggest using the visitor pattern to remedy having external choice (codata) without internal choice (data) as we do in Sect. 3.2. Of course, session types go beyond codata by adding a notion of temporality (via linearity) and multiple processes that communicate over channels.

To explore programming with coinductive types, Ancona and Zucca [4] and Jeannin et al. [26] extended Java and OCaml with regular cyclic structures; these have a finite representation that can be eagerly evaluated and fully stored in memory. A less restricted method of programming these structures was introduced by Abel et al. [1, 2] who popularized the idea of programming by observations, i.e. using copatterns. This line of work further developed the functionality of codata types in dependently typed languages by adding indexed codata types [40] and dependent copattern matching [11], which enabled the specification of bisimulation proofs and encodings of productive infinite objects in Agda. We build on these foundations by developing codata in practical languages.

Focusing on implementation, Regis-Gianas and Laforgue [33] added codata with a macro transformation in OCaml. As it turns out, this macro definition corresponds to one of the popular encodings of objects in the \(\lambda \)-calculus [27], where codata/objects are compiled to functions from tagged messages to method bodies. This compilation scheme requires the use of GADTs for static type checking, and is therefore only applicable to dynamically typed languages or the few statically typed languages with expressive enough type systems like Haskell, OCaml, and dependently typed languages. Another popular technique for encoding codata/objects is presented in [31], corresponding to a class-based organization of dynamic dispatch [21], and is presented in this paper. This technique compiles codata/objects to products of methods, which has the advantage of being applicable in a simply-typed setting.

6 Conclusion

We have shown here how codata can be put to use to capture several practical programming idioms and applications, besides just modeling infinite structures. In order to help incorporate codata in today’s programming languages, we have shown how to compile between two core languages: one based on the familiar notion of data types from functional languages such as Haskell and OCaml, and the other one, based on the notion of a structure defined by reactions to observations [1]. This paper works toward the goal of providing common ground between the functional and object-oriented paradigms; as future work, we would like to extend the core with other features of full-fledged functional and object-oriented languages. A better understanding of codata clarifies both the theory and practice of programming languages. Indeed, this work is guiding us in the use of fully-extensional functions for the compilation of Haskell programs. The design is motivated by the desire to improve optimizations, in particular the ones relying on the “arity” of functions, to be more compositional and work between higher-order abstractions. It is interesting that the deepening of our understanding of objects is helping us in better compiling functional languages!

Footnotes

  1. 1.

    To see the full code for all the examples of [24] implemented in terms of codata, visit https://github.com/zachsully/codata_examples.

  2. 2.

    The judgement \(\varGamma \vdash \rho \) should be read as: all free type variables in \(\rho \) occur in \(\varGamma \). As usual \(\varGamma , a\) means that a does not occur free in \(\varGamma \).

  3. 3.
  4. 4.

    The prototype compiler is at https://github.com/zachsully/dl/tree/esop2019.

References

  1. 1.
    Abel, A., Pientka, B., Thibodeau, D., Setzer, A.: Copatterns: programming infinite structures by observations. In: Proceedings of the 40th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2013, pp. 27–38 (2013)Google Scholar
  2. 2.
    Abel, A.M., Pientka, B.: Wellfounded recursion with copatterns: a unified approach to termination and productivity. In: Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming, ICFP 2013, pp. 185–196 (2013)Google Scholar
  3. 3.
    Amin, N., Rompf, T., Odersky, M.: Foundations of path-dependent types. In: Proceedings of the 2014 ACM International Conference on Object Oriented Programming Systems Languages & Applications, pp. 233–249 (2014)Google Scholar
  4. 4.
    Ancona, D., Zucca, E.: Corecursive featherweight Java. In: Proceedings of the 14th Workshop on Formal Techniques for Java-Like Programs, FTfJP 2012, Beijing, China, 12 June 2012, pp. 3–10 (2012)Google Scholar
  5. 5.
    Andreoli, J.M.: Logic programming with focusing proofs in linear logic. J. Logic Comput. 2, 297–347 (1992)MathSciNetCrossRefGoogle Scholar
  6. 6.
    Ariola, Z.M., Felleisen, M.: The call-by-need lambda calculus. J. Funct. Program. 7(3), 265–301 (1997)MathSciNetCrossRefGoogle Scholar
  7. 7.
    Balzer, S., Pfenning, F.: Objects as session-typed processes. In: Proceedings of the 5th International Workshop on Programming Based on Actors, Agents, and Decentralized Control, AGERE! 2015, pp. 13–24. ACM, New York (2015)Google Scholar
  8. 8.
    Bobrow, D.G., Kahn, K.M., Kiczales, G., Masinter, L., Stefik, M., Zdybel, F.: Commonloops: merging Lisp and object-oriented programming. In: Conference on Object-Oriented Programming Systems, Languages, and Applications (OOPSLA 1986), Portland, Oregon, USA, Proceedings, pp. 17–29 (1986)Google Scholar
  9. 9.
    Böhm, C., Berarducci, A.: Automatic synthesis of typed lambda-programs on term algebras. Theor. Comput. Sci. 39, 135–154 (1985)CrossRefGoogle Scholar
  10. 10.
    Cockett, R., Fukushima, T.: About charity. Technical report, University of Calgary (1992)Google Scholar
  11. 11.
    Cockx, J., Abel, A.: Elaborating dependent (co)pattern matching. In: Proceedings of the 23rd ACM SIGPLAN International Conference on Functional Programming, ICFP 2018, pp. 75:1–75:30 (2018)Google Scholar
  12. 12.
    Cook, W.R.: On understanding data abstraction, revisited. In: Proceedings of the 24th ACM SIGPLAN Conference on Object Oriented Programming Systems Languages and Applications, pp. 557–572 (2009)Google Scholar
  13. 13.
    Curien, P.L., Herbelin, H.: The duality of computation. In: Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming, ICFP 2000, pp. 233–243. ACM, New York (2000)Google Scholar
  14. 14.
    DeLine, R., Fähndrich, M.: Typestates for objects. In: Odersky, M. (ed.) ECOOP 2004. LNCS, vol. 3086, pp. 465–490. Springer, Heidelberg (2004).  https://doi.org/10.1007/978-3-540-24851-4_21CrossRefGoogle Scholar
  15. 15.
    Downen, P., Ariola, Z.M.: The duality of construction. In: Shao, Z. (ed.) ESOP 2014. LNCS, vol. 8410, pp. 249–269. Springer, Heidelberg (2014).  https://doi.org/10.1007/978-3-642-54833-8_14CrossRefzbMATHGoogle Scholar
  16. 16.
    Dummett, M.: The Logical Basis of Methaphysics: The William James Lectures, 1976. Harvard University Press, Cambridge (1991)Google Scholar
  17. 17.
    Gallier, J.: Constructive logics. Part I: a tutorial on proof systems and typed lambda-calculi. Theor. Comput. Sci. 110(2), 249–339 (1993)MathSciNetCrossRefGoogle Scholar
  18. 18.
    Girard, J.Y.: Linear logic. Theor. Comput. Sci. 50(1), 1–101 (1987)MathSciNetCrossRefGoogle Scholar
  19. 19.
    Girard, J.Y.: On the unity of logic. Ann. Pure Appl. Logic 59(3), 201–217 (1993)MathSciNetCrossRefGoogle Scholar
  20. 20.
    Hagino, T.: Codatatypes in ML. J. Symbolic Comput. 8, 629–650 (1989)MathSciNetCrossRefGoogle Scholar
  21. 21.
    Harper, R.: Practical Foundations for Programming Languages, 2nd edn. Cambridge University Press, New York (2016)CrossRefGoogle Scholar
  22. 22.
    Howard, W.A.: The formulae-as-types notion of construction. In: Curry, H.B., Hindley, J.R., Seldin, J.P. (eds.) To H.B. Curry Essays on Combinatory Logic, Lambda Calculus and Formalism, pp. 479–490. Academic Press, London (1980). unpublished manuscript of 1969Google Scholar
  23. 23.
    Hughes, J.: Why functional programming matters. Comput. J. 32(2), 98–107 (1989)CrossRefGoogle Scholar
  24. 24.
    Hughes, R.J.M.: Super-combinators: a new implementation method for applicative languages. In: Proceedings of the ACM Symposium on Lisp and Functional Programming, pp. 1–10 (1982)Google Scholar
  25. 25.
    Jacobs, B., Rutten, J.: A tutorial on (co)algebras and (co)induction. EATCS Bull. 62, 222–259 (1997)zbMATHGoogle Scholar
  26. 26.
    Jeannin, J., Kozen, D., Silva, A.: CoCaml: functional programming with regular coinductive types. Fundam. Inform. 150(3–4), 347–377 (2017)MathSciNetCrossRefGoogle Scholar
  27. 27.
    Krishnamurthi, S.: Programming Languages: Application and Interpretation (2007)Google Scholar
  28. 28.
    Munch-Maccagnoni, G.: Focalisation and classical realisability. In: Grädel, E., Kahle, R. (eds.) CSL 2009. LNCS, vol. 5771, pp. 409–423. Springer, Heidelberg (2009).  https://doi.org/10.1007/978-3-642-04027-6_30CrossRefGoogle Scholar
  29. 29.
    Munch-Maccagnoni, G.: Syntax and models of a non-associative composition of programs and proofs. Ph.D. thesis, Université Paris Diderot (2013)Google Scholar
  30. 30.
    Nordlander, J.: Polymorphic subtyping in O’Haskell. Sci. Comput. Program. 43(2–3), 93–127 (2002)MathSciNetCrossRefGoogle Scholar
  31. 31.
    Pierce, B.C.: Types and Programming Languages. The MIT Press, Cambridge (2002)zbMATHGoogle Scholar
  32. 32.
    Plotkin, G.: LCF considered as a programming language. Theor. Comput. Sci. 5(3), 223–255 (1977)MathSciNetCrossRefGoogle Scholar
  33. 33.
    Regis-Gianas, Y., Laforgue, P.: Copattern-matchings and first-class observations in OCaml, with a macro. In: Proceedings of the 19th International Symposium on Principles and Practice of Declarative Programming, PPDP 2017 (2017)Google Scholar
  34. 34.
    Rémy, D., Vouillon, J.: Objective ML: a simple object-oriented extension of ML. In: Proceedings of the 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 1997, pp. 40–53. ACM, New York (1997)Google Scholar
  35. 35.
    Reynolds, J.C.: User-defined types and procedural data structures as complementary approaches to data abstraction. In: Gries, D. (ed.) Programming Methodology. MCS, pp. 309–317. Springer, New York (1978).  https://doi.org/10.1007/978-1-4612-6315-9_22CrossRefGoogle Scholar
  36. 36.
    Rossberg, A.: 1ML - core and modules united (F-ing first-class modules). In: Proceedings of the 20th ACM SIGPLAN International Conference on Functional Programming, ICFP 2015, pp. 35–47. ACM, New York (2015)Google Scholar
  37. 37.
    Setzer, A., Abel, A., Pientka, B., Thibodeau, D.: Unnesting of copatterns. In: Dowek, G. (ed.) RTA 2014. LNCS, vol. 8560, pp. 31–45. Springer, Cham (2014).  https://doi.org/10.1007/978-3-319-08918-8_3CrossRefGoogle Scholar
  38. 38.
    Sullivan, Z.: The essence of codata and its implementation. Master’s thesis, University of Oregon (2018)Google Scholar
  39. 39.
    Thibodeau, D.: Programming infinite structures using copatterns. Master’s thesis, McGill University (2015)Google Scholar
  40. 40.
    Thibodeau, D., Cave, A., Pientka, B.: Indexed codata types. In: Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming, pp. 351–363 (2016)Google Scholar
  41. 41.
    Wadler, P.: Call-by-value is dual to call-by-name. In: Proceedings of the Eighth ACM SIGPLAN International Conference on Functional Programming, pp. 189–201 (2003)Google Scholar
  42. 42.
    Wadler, P.: Propositions as types. Commun. ACM 58(12), 75–84 (2015)CrossRefGoogle Scholar
  43. 43.
    Weirich, S.: Depending on types. In: Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming, ICFP 2014 (2014)Google Scholar
  44. 44.
    Zeilberger, N.: On the unity of duality. Ann. Pure Appl. Logic 153, 66–96 (2008)MathSciNetCrossRefGoogle Scholar
  45. 45.
    Zeilberger, N.: The logical basis of evaluation order and pattern-matching. Ph.D. thesis, Carnegie Mellon University (2009)Google Scholar

Copyright information

© The Author(s) 2019

Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license and indicate if changes were made.

The images or other third party material in this chapter are included in the chapter's Creative Commons license, unless indicated otherwise in a credit line to the material. If material is not included in the chapter's Creative Commons license and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder.

Authors and Affiliations

  • Paul Downen
    • 1
  • Zachary Sullivan
    • 1
    Email author
  • Zena M. Ariola
    • 1
  • Simon Peyton Jones
    • 2
  1. 1.University of OregonEugeneUSA
  2. 2.Microsoft ResearchCambridgeUK

Personalised recommendations