The Algebra of Programming in Haskell

Similar documents
Lecture 4: Higher Order Functions

Introduction to Recursion schemes

Bringing Functions into the Fold Tom Schrijvers. Leuven Haskell User Group

Logic - CM0845 Introduction to Haskell

Data types à la carte

Programming with Math and Logic

CSC324 Principles of Programming Languages

CS 457/557: Functional Languages

Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Haskell Overview II (2A) Young Won Lim 8/9/16

INTRODUCTION TO HASKELL

Structural polymorphism in Generic Haskell

Programming Language Concepts, CS2104 Lecture 7

Introduction to Functional Programming in Haskell 1 / 56

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

IA014: Advanced Functional Programming

Haskell Overview II (2A) Young Won Lim 9/26/16

Lists. Michael P. Fourman. February 2, 2010

CS 440: Programming Languages and Translators, Spring 2019 Mon

Recursive Types. Chapter 7

Haskell Overview II (2A) Young Won Lim 8/23/16

Lecture 8: Summary of Haskell course + Type Level Programming

Less Is More. Generic Programming Theory and Practice. José Pedro Magalhães

College Functors, Applicatives

Lecture 19: Functions, Types and Data Structures in Haskell

A Shortcut Fusion Rule for Circular Program Calculation

Representing Cyclic Structures as Nested Datatypes. Makoto Hamana

Prototyping Generic Programming in Template Haskell

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Programming with Universes, Generically

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Shared Subtypes. Subtyping Recursive Parameterized Algebraic Data Types

Implementing the Opetopes:

Functional Programming Mid-term exam Tuesday 3/10/2017

Program Calculus Calculational Programming

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Advanced features of Functional Programming (Haskell)

Structurally Recursive Descent Parsing. Nils Anders Danielsson (Nottingham) Joint work with Ulf Norell (Chalmers)

Recursively defined cpo algebras

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Data types à la carte. Wouter Swierstra Dutch HUG 25/8/10

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

JVM ByteCode Interpreter

Generic Programming with Adjunctions

CSCE 314 Programming Languages

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Shell CSCE 314 TAMU. Higher Order Functions

Exercise 1 (2+2+2 points)

CSCI-GA Scripting Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Programming in Omega Part 1. Tim Sheard Portland State University

Typed Scheme: Scheme with Static Types

Monadic Augment and Generalised Short Cut Fusion

Applicative, traversable, foldable

CSCE 314 Programming Languages

Applicative, traversable, foldable

Higher-Order Functions

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

Advances in Programming Languages

Type-indexed functions in Generic Haskell

Programming Paradigms

Course year Typeclasses and their instances

Monad Background (3A) Young Won Lim 11/8/17

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Algebraic Types. Chapter 14 of Thompson

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Generic programming for the masses

CSE 3302 Programming Languages Lecture 8: Functional Programming

CS 457/557: Functional Languages

CSCE 314 Programming Languages

Generic Views on Data Types

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

CSC324 Principles of Programming Languages

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)

Monad Background (3A) Young Won Lim 11/18/17

Background Type Classes (1B) Young Won Lim 6/28/18

Strongly Typed Programs and Proofs

A New Foundation for Generic Programming

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Shell CSCE 314 TAMU. Haskell Functions

301AA - Advanced Programming

Generic Constructors and Eliminators from Descriptions

Inductive Types for Free

An introduction introduction to functional functional programming programming using usin Haskell

INTRODUCTION TO FUNCTIONAL PROGRAMMING

A general introduction to Functional Programming using Haskell

LECTURE 16. Functional Programming

Stream Fusion. Wouter Swierstra 30/11/2010

CS 11 Haskell track: lecture 1

Haskell Programming with Nested Types: A Principled Approach

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Transcription:

The Algebra of Programming in Haskell Bruno Oliveira The Algebra of Programming in Haskell p.1/21

Datatype Generic Programming - Motivation/Goals The project is to develop a novel mechanism for parameterizing programs, namely parametrization by a datatype or type constructor. We aim to develop a calculus for constructing datatype-generic programs. Ultimate goal of improving the state of the art in generic object-oriented programming, as occurs for example in the C++ Standard Template Library. The Algebra of Programming in Haskell p.2/21

Introduction - Algebra of Programming In the excellent book Algebra of Programming, Bird and de Moor show us how to calculate programs in a very elegant way. Further, the problems that they solve are datatype-generic. As they note: "... The problems are abstract in the sense that they are parameterized by one or more datatypes.... " The Algebra of Programming provides us: A mathematical framework based in a categorical calculus of relations The categorical calculus allow us to formulate algorithmic strategies without reference to specific datatypes. An important subset of generic functions. The Algebra of Programming in Haskell p.3/21

Notation f g id k f f i 1 i 2 π 1 π 2 function composition identity function constant function curry function uncurry function left injection to sum right injection to sum left component of product right component of product 1 unit type and value f g fork over product f g either function f + g sum mapping f g product mapping The Algebra of Programming in Haskell p.4/21

A Theory of Lists Consider the Haskell [A] (we use capitals instead of lower case to denote types) datatype. A possible definition for it, could be: data [A] = [ ] A : [A] You can view this data definition as the following isomorphism: out [ ] [A] = 1 + A [A] in [ ] The Algebra of Programming in Haskell p.5/21

A Theory of lists A well known function on lists is the foldr function: foldr f k [ ] = k foldr f k (x : xs) = f x (foldr f k xs) [A] foldr f k B foldr and its dual unfoldr are the basis for many definitions on lists. "Uncurried" versions of this functions, are the basis for much of the theory presented in the book. The Algebra of Programming in Haskell p.6/21

A Theory of Lists - Morphisms [A] out [ ] 1 + A [A] ( f ) [ ] rec [ ] ( f ) [ ] B 1 + A B f We call catamorphism to the "uncurried" version of foldr and we denote it as ( f ) [ ]. ( f ) [ ] = f rec [ ] ( f ) [ ] out [ ] where out [ ] = (1 + head tail) ( [ ])? rec [ ] g = id + id g The Algebra of Programming in Haskell p.7/21

Functors - Generalizing the Theory By using functors, we can generalize the theory. For instance, we could abstract the expansion of [A] to: F X = 1 + A X Parameterizing F with [A], we would obtain 1 + A [A]. A catamorphism could be expressed generically by: T out F T ( f ) F ( f ) X F X f The Algebra of Programming in Haskell p.8/21

Functional Dependencies Allow programmers to specify multiple parameter classes more precisely. For instance: class C a b class D a b a b class E a b a b, b a From these definitions we can tell that: Class C is a binary relation. Class D is not only a relation, but actually a (partial) function. Class E represents a (partial) one-one mapping. The Algebra of Programming in Haskell p.9/21

Related Work - PolyP PolyP The original PolyP system allows us to write generic definitions for regular datatypes of kind. The system works by using a type based translation from PolyP to Haskell at compile time. PolyP 2 More recently, PolyP 2 introduces a novel translation mechanism allowing PolyP code to be translated to Haskell classes and instances. The structure of a regular datatype is described by its pattern functor. For instance: data List a = Nil Cons a (List a) type ListF = Empty + Par Rec The Algebra of Programming in Haskell p.10/21

Related Work - PolyP 2 All pattern functors (except ) are instances of the class P_fmap2 : class P_fmap2 f where fmap2 :: (a c) (b d) (f a b f c d) To convert between a datatype and its pattern functor, the multi-parameter type class FunctorOf is used: class FunctorOf f d d f where inn :: f a (d a) d a out :: d a f a (d a) Having these, we could define, for instance: ( f ) = f fmap2 id ( f ) out [( f )] = inn fmap2 id [( f )] f The Algebra of Programming in Haskell p.11/21

APLib In the Algebra of Programming Library (APLib) we show a similar framework working for regular datatypes of all kinds. The class Iso acts like an "weak" isomorphism by establishing an one-one mapping between A and B class Iso a b a b, b a where out :: a b inn :: b a out A = F A inn Class MorphArrows contains more information than Functor. class Iso a b MorphArrows a b c d a b d c, a b c d where down :: (a d) b c up :: (d a) c b f A g D down f B up g C The Algebra of Programming in Haskell p.12/21

APLib - Morphisms By using MorphArrows we can define catamorphisms as: A out B ( f ) = f down ( f ) out ( f ) down ( f ) D C f Defining anamorphisms and hylomorphisms is easy: [( f )] = inn up [( f )] f f, g = ( f ) [( g )] The Algebra of Programming in Haskell p.13/21

APLib - Example data Expr op a = Leaf a Binary op (Expr op a) (Expr op a) data Op = Sum Sub instance MorphArrows (Expr op a) (a + op Expr op a Expr op a) (a + op b b) b where down f = id + id f f up f = id + id f f Calculating the value of an expression: eval :: Expr Op Int Int eval = ( id evalop ) where evalop = ((+) π 2 ( ) π 2 ) (issum π 1 )? The Algebra of Programming in Haskell p.14/21

APLib - Defining Generic Map We can define a generic map by having a class MapArrows which transforms the Functor that we are working with. The parameters f and g are similar to kind-indexed types. T class Iso a b MapArrows a b c f g a b c f g where ( h ) left :: f c b right :: g b c A inn out h X down ( h ) C gmap f = ( h ) where h = inn left f B left f The Algebra of Programming in Haskell p.15/21

Specializations Two possible approaches to specializations: 1. By Type - define a new function with a more restrictive type. Usefull for having less generic functions. cata :: MorphArrows (f a) u c b (c b) f a b cata g = ( g ) 2. By Definition - define a new function based on the definition of the most generic one, but specific to a type. Useful for optimization. out [ ] = (1 + head tail) ( [ ])? down [ ] g = id + id g ( f ) [ ] = f down [ ] ( f ) [ ] out [ ] The Algebra of Programming in Haskell p.16/21

Abstract Data Types data Ord a BTree a = Empty Branch a (BTree a) (BTree a) class OrdList f where isnil :: f a Bool nil :: f a add :: Ord a a f a f a getnext :: Ord a f a Maybe (a, f a) The instance for BTree a could be: instance (OrdList f, Ord a) Iso (f a) (1 + a f a) where out = (1 + fromjust getnext) isnil? inn = (nil add ) Given that, we could define a sorting function: sort :: [Int ] [Int ] sort = ( inn ) ([( out )] :: [Int ] BTree Int) The Algebra of Programming in Haskell p.17/21

Future Research Generate the instance for MorphArrows and MapArrows automatically. Template Haskell seems to fit well. A mechanism like Derivable type classes might be another possibility. Try to minimize the number of classes/instances. Consider a larger range of datatypes: Ian Bailey and Paul Blampied work. Consider using the framework in a dependent type system. The Algebra of Programming in Haskell p.18/21

Future and Related Work - Type Transformers Type transformers allow us define types and definitions based on types. For instance, for out we could have: The type is given by: θ <Type > ::Type The definition is given by: out < T > ::T θ <T > This would fit nicely into classes with functional dependencies. class Iso T θ T θ, θ T where out < T > ::T θ inn < θ, T > :: θ T When defining out, we will be interested in matching the recursive pattern of T in F T. out < T > ::T θ out < Data T > = out < T, Data T > The Algebra of Programming in Haskell p.19/21

Future and related work - Type Transformers out < T, Rec > ::T θ out < Rec, Rec> = id out < 1, > = () out < Prim, > = id out < Data t, Rec> = t out < t, Rec> out < a + b, Rec> = out < a, Rec > +out < a, Rec> out < a b, Rec> = out < a, Rec > out < b, Rec> out < Con c, Rec> = out < a, Rec > isc? θ <T, Rec > ::Type θ <Rec, Rec> = Rec θ <1, > = () θ <Prim, > = Prim θ <Data t, Rec> = t θ <t, Rec> θ <a + b, Rec> = θ <a, Rec > + θ <a, Rec> θ <a b, Rec> = θ <a, Rec > θ <b, Rec> θ <Con c a, Rec> = θ <a, Rec > isc? The Algebra of Programming in Haskell p.20/21

Conclusions Theory based on categorical calculus of relations allows us to reason about the programs. Integrates nicely with other features of Haskell (ex. type classes) Possible application for optimization. Support for regular datatypes with no restriction on the kind. Restricted support for generic functions. Still not "quite" right: no explicit Functor concept, need for dual definitions. The Algebra of Programming in Haskell p.21/21