The Under-Performing Unfold a New Approach to Optimising Corecursive Programs

The Under-Performing Unfold a New Approach to Optimising Corecursive Programs

The Under-Performing Unfold A new approach to optimising corecursive programs Jennifer Hackett Graham Hutton Mauro Jaskelioff University of Nottingham Universidad Nacional de Rosario, Argentina fjph,[email protected] CIFASIS–CONICET, Argentina jaskelioff@cifasis-conicet.gov.ar Abstract pattern of corecursive programming. By exploiting the categorical This paper presents a new approach to optimising corecursive pro- principle of duality, we adapt the theory of worker-wrapper factori- grams by factorisation. In particular, we focus on programs written sation for folds, which was developed initially by Hutton, Jaskelioff using the corecursion operator unfold. We use and expand upon the and Gill [15] and subsequently extended by Sculthorpe and Hut- proof techniques of guarded coinduction and unfold fusion, captur- ton [24]. We discuss the practical considerations of the dual theory, ing a pattern of generalising coinductive hypotheses by means of which differ from those of the original theory and avoid the need abstraction and representation functions. The pattern we observe is for strictness side conditions. In addition, we revise the theory to simple, has not been observed before, and is widely applicable. We further improve its utility. As we shall see, the resulting theory for develop a general program factorisation theorem from this pattern, unfolds captures a common pattern of coinductive reasoning that demonstrating its utility with a range of practical examples. is simple, has not been observed before, and is widely applicable. We demonstrate the application of the new theory with a range of Categories and Subject Descriptors D.1.1 [Programming Tech- practical examples of varying complexity. niques]: Applicative (Functional) Programming The development of our new theory is first motivated using a General Terms Theory, Proof Methods, Optimisation small programming example, which we then generalise using cat- egory theory. We use only simple concepts for this generalisation, Keywords fusion, factorisation, coinduction, unfolds and only a minimal amount of categorical knowledge is required. Readers without a background in category theory should not be de- 1. Introduction terred. The primary application area for our theory is functional When writing programs that produce data structures it is often nat- languages such as Haskell, however the use of categorical concepts ural to use the technique of corecursion [8, 20], in which subterms means that our theory is more widely applicable. of the result are produced by recursive calls. This is particularly useful in lazy languages such as Haskell, as it allows us to process 2. Coinductive Types and Proofs parts of the result without producing the rest of it. In this way, we Haskell programmers will be familiar with recursive type defini- can write programs that deal with large or infinite data structures tions, where a type is defined in terms of itself. For example, the and trust that the memory requirements remain reasonable. type of natural numbers can be defined as follows: However, while this technique may allow us to save on the number of recursive calls by producing data lazily, the time cost of data N = Zero j Succ N each call or the space cost of the arguments can still be a problem. This definition states that an element of the type N is either Zero For this reason, it is necessary to examine various approaches or Succ n for some n that is also an element of the type N. More to reducing these costs. A commonly-used approach is program formally, recursive type definitions can be given meaning as fixed fusion [4, 11, 31], where separate stages of a computation are points of type-level equations. For example, we can read the above combined into one to avoid producing intermediate data. For this as defining N to be a fixed point of the equation X = 1 + X , paper, our focus will be on the opposite technique of program where 1 is the unit type and + is the disjoint sum of types. factorisation [7], where a computation is split into separate parts. Assuming that such a fixed point equation has at least one so- In particular, we consider the problem of splitting a computation lution, there are two we will typically be interested in. First of all, into the combination of a more efficient worker program that uses a there is the least fixed point, which is given by the smallest type different representation of data and a wrapper program that effects that is closed under the constructors. This is known as an inductive the necessary change of representation [10]. type. In a set-theoretic context, the inductive interpretation of our The primary contribution of this paper is a general factorisa- definition of N is simply the set of natural numbers, with construc- tion theorem for programs in the form of an unfold [9], a common tors Zero and Succ having the usual behaviour. Alternatively, there is the greatest fixed point, which is given by the largest type that supports deconstruction of values by pattern matching. This is known as a coinductive type [12]. In a set- theoretic context, the coinductive interpretation of our definition of N is the set of naturals augmented with an infinite value 1 that is the solution to the equation 1 = Succ 1. In general, coinductively-defined sets have infinite elements, while inductively-defined sets do not. In the setting of Haskell, [Copyright notice will appear here once ’preprint’ option is removed.] however, types correspond to (pointed) complete partial orders 1 2014/1/13 (CPOs) rather than sets, where there is no distinction between in- from the results. We would like to define a function that performs ductive and coinductive type definitions as the two notions coin- this task, specified informally as follows: cide [6]. For the purposes of this article we will use Haskell syntax as a metalanguage for programming in both set-theoretic and CPO tabulate :: (N ! a) ! Stream a contexts. It will therefore be necessary to distinguish between in- tabulate f = [f 0; f 1; f 2; f 3;:::] ductive and coinductive definitions, which we do so by using the The following definition satisfies this specification: keywords data and codata respectively. For example, we could define an inductive type of lists and a tabulate f = f 0 : tabulate (f ◦ (+1)) coinductive type of streams as follows, using the constructor (:) in However, this definition is inefficient, as with each recursive call both definitions for consistency with Haskell usage: the function argument becomes more costly to apply, as shown in data [a ] = a :[a ] j [] the following expansion of the definition: codata Stream a = a : Stream a tabulate f = [f 0; (f ◦ (+1)) 0; (f ◦ (+1) ◦ (+1)) 0;:::] If types are sets, the first definition gives finite lists while the second The problem is that the natural number is recomputed from scratch gives infinite streams. If types are CPOs, the first definition gives each time by repeated application of (+1) to 0. If we were to save both finite and infinite lists, while the second gives infinite streams. the result and re-use it in future steps, we could avoid repeating Also note that in the context of sets, if streams were defined using work. The idea can be implemented by defining a new function data rather than codata the resulting type would be empty. that takes the current value as an additional argument: 0 2.1 Coinduction tabulate :: (N ! a; N) ! Stream a 0 0 To reason about elements of inductive types one can use the tech- tabulate (f ; n) = f n : tabulate (f ; n + 1) nique of induction. Likewise, to reason about elements of coinduc- The correctness of the more efficient implementation for tabu- tive types one can use the dual technique of coinduction. To for- lation can be captured by the following equation malise this precisely involves the notion of bisimulation [8, 12]. In this section we shall give an informal presentation of guarded coin- tabulate f = tabulate0 (f ; 0) duction [3, 28], a special case that avoids the need for such machin- ery. This form of coinduction is closely related to the unique fixed which can written in point-free form as point principle developed by Hinze [14]. tabulate = tabulate0 ◦ (λf ! (f ; 0)) To prove an equality lhs = rhs between expressions of the same coinductive type using guarded coinduction, we simply at- This equation can be viewed as a program factorisation, in which tempt the proof in the usual way using equational reasoning. How- tabulate is factored into the composition of tabulate0 and the ever, we may also make use of the coinductive hypothesis, which function λf ! (f ; 0). This latter function effects a change of allows us to substitute lhs for rhs (or vice-versa) provided that we data representation from the old argument type N ! a to the only do so immediately underneath a constructor of the coinduc- new argument type (N ! a; N). In order to try to prove the above tive type. We say that such a use of the coinductive hypothesis is equation, we proceed by guarded coinduction: guarded. For example, if we define the following functions that tabulate f produce values of type Stream N = f definition of tabulate g from n = n : from (n + 1) f 0 : tabulate (f ◦ (+1)) skips n = n : skips (n + 2) = f coinduction hypothesis g 0 double (n : ns) = n ∗ 2 : double ns f 0 : tabulate (f ◦ (+1); 0) = f assumption g then we can show that skips (n ∗ 2) = double (from n) for any f 0 : tabulate0 (f ; 1) natural number n using guarded coinduction: = f definition of tabulate0 g 0 skips (n ∗ 2) tabulate (f ; 0) = f definition of skips g To complete the proof, the assumption used in the third step n ∗ 2 : skips ((n ∗ 2) + 2) tabulate0 (f ◦ (+1); 0) = tabulate0 (f ; 1) must be verified.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us