A Generic Approach to Datatype Persistency in Haskell
Total Page:16
File Type:pdf, Size:1020Kb
Load more
Recommended publications
-
NICOLAS WU Department of Computer Science, University of Bristol (E-Mail: [email protected], [email protected])
Hinze, R., & Wu, N. (2016). Unifying Structured Recursion Schemes. Journal of Functional Programming, 26, [e1]. https://doi.org/10.1017/S0956796815000258 Peer reviewed version Link to published version (if available): 10.1017/S0956796815000258 Link to publication record in Explore Bristol Research PDF-document University of Bristol - Explore Bristol Research General rights This document is made available in accordance with publisher policies. Please cite only the published version using the reference above. Full terms of use are available: http://www.bristol.ac.uk/red/research-policy/pure/user-guides/ebr-terms/ ZU064-05-FPR URS 15 September 2015 9:20 Under consideration for publication in J. Functional Programming 1 Unifying Structured Recursion Schemes An Extended Study RALF HINZE Department of Computer Science, University of Oxford NICOLAS WU Department of Computer Science, University of Bristol (e-mail: [email protected], [email protected]) Abstract Folds and unfolds have been understood as fundamental building blocks for total programming, and have been extended to form an entire zoo of specialised structured recursion schemes. A great number of these schemes were unified by the introduction of adjoint folds, but more exotic beasts such as recursion schemes from comonads proved to be elusive. In this paper, we show how the two canonical derivations of adjunctions from (co)monads yield recursion schemes of significant computational importance: monadic catamorphisms come from the Kleisli construction, and more astonishingly, the elusive recursion schemes from comonads come from the Eilenberg-Moore construction. Thus we demonstrate that adjoint folds are more unifying than previously believed. 1 Introduction Functional programmers have long realised that the full expressive power of recursion is untamable, and so intensive research has been carried out into the identification of an entire zoo of structured recursion schemes that are well-behaved and more amenable to program comprehension and analysis (Meijer et al., 1991). -
Categorical Programming with Inductive and Coinductive Types
DISSERTATIONES MATHEMATICAE UNIVERSITATIS TARTUENSIS 23 CATEGORICAL PROGRAMMING WITH INDUCTIVE AND COINDUCTIVE TYPES VARMO VENE TARTU 2000 DISSERTATIONES MATHEMATICAE UNIVERSITATIS TARTUENSIS 23 DISSERTATIONES MATHEMATICAE UNIVERSITATIS TARTUENSIS 23 CATEGORICAL PROGRAMMING WITH INDUCTIVE AND COINDUCTIVE TYPES VARMO VENE TARTU 2000 Faculty of Mathematics, University of Tartu, Estonia Dissertation accepted for public defense of the degree of Doctor of Philosophy (PhD) on May 26, 2000 by the Council of the Faculty of Mathematics, University of Tartu. Opponent: PhD, University Lecturer Jeremy Gibbons Oxford University Computing Laboratory Oxford, England The public defense will take place on Sept. 3, 2000. The publication of this dissertation was financed by Institute of Computer Science, University of Tartu. c Varmo Vene, 2000 Tartu Ulikooli¨ Kirjastuse trukikoda¨ Tiigi 78, 50410 Tartu Tellimus nr. 365 CONTENTS 1 Introduction 9 1.1 Motivation . 9 1.2 Overview of the thesis . 12 1.3 Notation . 14 2 Inductive and coinductive types 15 2.1 Initial algebras and catamorphisms . 15 2.2 Terminal coalgebras and anamorphisms . 22 2.3 Implementation in Haskell . 26 2.4 Related work . 31 3 Primitive (co)recursion 33 3.1 Primitive recursion via tupling . 33 3.2 Paramorphisms . 35 3.3 Apomorphisms . 40 3.4 Para- and apomorphisms in Haskell . 43 3.5 Related work . 45 4 Course-of-value (co)iteration 47 4.1 Course-of-value iteration via memoization . 47 4.2 Histomorphisms . 50 4.3 Futumorphisms . 55 4.4 Histo- and futumorphisms in Haskell . 58 4.5 Related work . 61 5 Mendler-style inductive types 63 5.1 Mendler-style inductive types: covariant case . 63 5.2 Conventional inductive types reduced to Mendler-style inductive types . -
Sorting with Bialgebras and Distributive Laws
Sorting with Bialgebras and Distributive Laws Ralf Hinze Daniel W. H. James Thomas Harper Nicolas Wu José Pedro Magalhães Department of Computer Science, University of Oxford {ralf.hinze;daniel.james;tom.harper;nicolas.wu;jose.pedro.magalhaes}@cs.ox.ac.uk Abstract This is an entirely routine and naïve definition, which makes use Sorting algorithms are an intrinsic part of functional programming of the span function from the list utilities section of the Haskell folklore as they exemplify algorithm design using folds and un- Report. When the input list ys is ordered, insert y ys adds y to the folds. This has given rise to an informal notion of duality among list ys and maintains the invariant that the ensuing list is ordered. sorting algorithms: insertion sorts are dual to selection sorts. Using Thus, we are able to fold an unordered list into an ordered one when bialgebras and distributive laws, we formalise this notion within a we start with an empty list as the initial value of the fold. categorical setting. We use types as a guiding force in exposing the Perhaps less well known is that an alternative sorting algorithm, recursive structure of bubble, insertion, selection, quick, tree, and selection sort, can be written in terms of an unfold. An unfold can heap sorts. Moreover, we show how to distill the computational be thought of as the dual of a fold: a fold consumes a list, whereas essence of these algorithms down to one-step operations that are unfold produces a list, as is evident in the type of unfoldr: expressed as natural transformations. -
Recursion for the Masses
Recursion for the Masses Recursion for the Masses TCS Seminar WS19/20 Christoph Rauch Dec 10, 2019 Recursion for the Masses Introduction of Recursion to Students Early Stage (FAU) students are more or less expected to "know" what recursion is no definition at all in maths classes "Java can do it" plus a definition of what a recursive definition looks like in Java in GDA (now AuD) tail recursion, mutual recursion, divide & conquer similarly "Haskell can do it" in PFP literally recommended to use iteration whenever possible nowadays, students are blessed with ThProg! Recursion for the Masses First Real Contact Theory of Programming We learn that . (inductive) data types are initial algebras to functors initiality provides unique solutions to recursive equations of a certain form; the "recursion scheme" of folds (and, in fact, the more general primitive recursion) there’s a dual concept (namely final coalgebras) for coinductive data first examples of structured recursion not the end of the line! Recursion for the Masses Recursion Schemes Recursion Schemes folds (tear down a structure) unfolds (build up a structure) algebra f a → Fix f → a ↔ coalgebra f a → a → Fix f catamorphism anamorphism f a → a a → f a generalized prepromorphism* postpromorphism* (m f ↝ f m) → (α → f (m β)) … after applying a NatTrans … before applying a NatTrans generalized (f a → a) → (f ↝ f) (a → f a) → (f ↝ f) (f w ↝ w f) → (f (w α) → β) paramorphism* apomorphism* … with primitive recursion … returning a branch or single level f (Fix f ⨯ a) → a a → f (Fix f ∨ a) zygomorphism* g apomorphism … with a helper function (f b → b) → (f (b ⨯ a) → a) (b → f b) → (a → f (b ∨ a)) g histomorphism histomorphism futumorphism g futumorphism … with prev. -
Functional Programming with Apomorphisms (Corecursion)
Proc. Estonian Acad. Sci. Phys. Math., 1998, 47, 3, 147—16 1 FUNCTIONAL PROGRAMMING WITH APOMORPHISMS (CORECURSION) Varmo VENE and Tarmo UUSTALUb a Institute of Computer Science, University of Tartu, J. Liivi 2, EE-2484 Tartu, Estonia; e-mail: [email protected] b Department of Teleinformatics, Royal Institute of Technology, Electrum 204, SE-164 40 Kista, Sweden; e-mail: [email protected] Received 19 January 1998, in revised form 23 February 1998 Abstract. In the mainstream categorical approach to typed (total) functional programming, functions with inductive source types defined by primitive recursion are called paramorphisms; the utility of primitive recursion as a scheme for defining functions in programming is well known. We draw attention to the dual notion of apomorphisms — functions with coinductive target types defined by primitive corecursion and show on examples that primitive corecursion is useful in programming. Key words: typed (total) functional programming, categorical program calculation, (co)datatypes, (co)recursion forms. 1. INTRODUCTION This paper is about the categorical approach to typed (total) functional programming where datatypes and codatatypes are modelled as initial algebras and terminal coalgebras. Our object of study is the notion of apomorphisms, which are functions with coinductive target types defined by primitive corecursion. Apomorphisms are the dual of paramorphisms, functions with inductive source types defined by primitive corecursion. The widely used term paramorphism was [1] introduced by Meertens (irapa — Greek preposition meaning “near to”, “at the side of”, “towards”), the term apomorphism is a novel invention of ours (airo — Greek preposition meaning “apart from”, “far from”, “away from”). Our aim is to show that apomorphisms are a convenient tool for a declaratively thinking programmer, often more handy than anamorphisms, i.e. -
Classical (Co) Recursion: Programming
arXiv, 34 pages, 2021. © 2021 1 Classical (Co)Recursion: Programming PAUL DOWNEN and ZENA M. ARIOLA University of Oregon (e-mail: {pdownen,ariola}@cs.uoregon.edu) Abstract Structural recursion is a widespread technique. It is suitable to be used by programmers in all mod- ern programming languages, and even taught to beginning computer science students. What, then, of its dual: structural corecursion? For years, structural corecursion has proved to be an elegant programming technique made possible by languages like Haskell. There, its benefits are to enable compositional algorithm design by decoupling the generation and consumption of (potentially) infi- nite or large collections of data. However, it is usually thought of as a more advanced topic than structural recursion not suitable for beginners, and not easily applicable outside of the relatively narrow context of lazy, pure functional programming. Our aim here is to illustrate how the benefits of structural corecursion can be found in a broader swath of the programming landscape than previously thought. Beginning from a tutorial on structural corecursion in the total, pure functional language Agda, we show how these same ideas are mapped to familiar concepts in a variety of different languages. We show how corecursion can be done in strict functional languages like Scheme, and even escapes the functional paradigm entirely, showing up in the natural expression of common object-oriented features found in languages like Python and Java. Opening up structural corecursion to a much wider selection of languages and paradigms— and therefore, also to a much larger audience of programmers—lets us also ask how corecursion interacts with computational effects. -
Unifying Structured Recursion Schemes
Unifying Structured Recursion Schemes Ralf Hinze Nicolas Wu Jeremy Gibbons Department of Computer Science, University of Oxford, Wolfson Building, Parks Road, Oxford, OX1 3QD, England {ralf.hinze,nicolas.wu,jeremy.gibbons}@cs.ox.ac.uk Abstract paramorphisms [21], in which the body of structural recursion has Folds over inductive datatypes are well understood and widely access to immediate subterms as well as to their images under the used. In their plain form, they are quite restricted; but many dis- recursion; histomorphisms [26], in which the body has access to the parate generalisations have been proposed that enjoy similar cal- recursive images of all subterms, not just the immediate ones; and culational benefits. There have also been attempts to unify the var- so-called generalised folds [4], which use polymorphic recursion ious generalisations: two prominent such unifications are the ‘re- to handle nested datatypes. cursion schemes from comonads’ of Uustalu, Vene and Pardo, and The many divergent generalisations of catamorphisms can be our own ‘adjoint folds’. Until now, these two unified schemes have bewildering to the uninitiated, and there have been attempts to unify appeared incompatible. We show that this appearance is illusory: them. One approach is the identification of recursion schemes from in fact, adjoint folds subsume recursion schemes from comonads. comonads [30] (which we call ‘rsfcs’ for short). Comonads capture The proof of this claim involves standard constructions in category the general idea of ‘evaluation in context’ [27], and rsfcs make theory that are nevertheless not well known in functional program- contextual information available to the body of the recursion.