Profunctor Optics: Modular Data Accessors

Size: px
Start display at page:

Download "Profunctor Optics: Modular Data Accessors"

Transcription

1 Profunctor Optics: Modular Data Accessors Jeremy Gibbons (joint work with Matthew Pickering and Nicolas Wu) Spring Festival Workshop, Karuizawa, March 2017

2 Profunctor Optics 2 1. Overview lenses for access within product types dually, prisms for access with sum types collectively optics also adapters for conversion, traversals for iteration not closed under heterogeneous composition not convenient even for homogeneous composition alternative representation in terms of profunctors much better compositionality!

3 Profunctor Optics 3 2. Optics Polymorphic lenses, for access into product types: data Lens a b s t = Lens {view :: s a, update :: b s t } For example: pi 1 :: Lens a b (a c) (b c) pi 1 = Lens viewfst updatefst where viewfst (x, y) = x updatefst (x, (x, y)) = (x, y) S T view update A B

4 Profunctor Optics 4 Monomorphic lenses The representation allows the view type to change, but does not require it. For example, sign :: Lens Bool Bool Integer Integer sign = Lens view update where view x = (x 0) update (b, x) = if b then abs x else (abs x)

5 Profunctor Optics 5 Prisms Dually, for access into sum types: data Prism a b s t = Prism {match :: s t + a, build :: b t } For example, the :: Prism a b (Maybe a) (Maybe b) the = Prism down up where down (Just x) = Right x down Nothing = Left Nothing up x = Just x S T match build A B

6 Profunctor Optics 6 Adapters Lenses and prisms are divergent specialisations of a common ancestor, which converts data between two equivalent formats: data Adapter a b s t = Adapter {from :: s a, For example, to :: b t } flatten :: Adapter (a b c) (a b c ) ((a b) c) ((a b ) c ) flatten = Adapter from to where from ((x, y), z) = (x, y, z) to (x, y, z) = ((x, y), z) S T from to A B

7 Profunctor Optics 7 Traversals data Trav a b t = Trav {untrav :: f. Applicative f (a f b) f t } data Traversal a b s t = Traversal {traverse :: s Trav a b t } For example, data Tree a = Leaf Node (Tree a) a (Tree a) inorder :: Traversal a b (Tree a) (Tree b) inorder = Traversal (λt Trav (go t)) where go Leaf h = pure Leaf go (Node t x u) h = pure Node go t h h x go u h cf the Traversable type class, for container types, with method: traverse :: (Traversable t, Applicative f ) (a f b) t a f (t b) S T traverse A n B n

8 Profunctor Optics 8 Homogeneous composite data Consider access to the A in a nested pair (A B) C: pi 11 :: Lens a a ((a b) c) ((a b) c) pi 11 = Lens view update where Lens v u = pi 1 view = v v update (x, xyz) = u (xy, xyz) where xy = v xyz xy = u (x, xy) Clumsy for update, better to resort instead to first principles: update (x, ((x, y), z)) = ((x, y), z) rather than reusing pi 1. The abstraction is inconvenient.

9 Profunctor Optics 9 Heterogeneous composite data Worse, consider access to the A in a composite data structure Maybe (A B) built using both sums and products. It s not a lens, because there is no view :: Maybe (A B) A. And it s not a prism, because there is no build :: A Maybe (A B). The universe of data accessors is not closed under composition.

10 Profunctor Optics Profunctors A generalisation of functions: transformers that consume and produce. A B Captured as a two-parameter type constructor, contravariant the input: class Profunctor p where dimap :: (a a) (b b ) p a b p a b such that dimap id id = id dimap (f f ) (g g ) = dimap f g dimap f g

11 Profunctor Optics 11 Lifting functors Ordinary functions are an instance: instance Profunctor ( ) where dimap f g h = g h f So are functions returning structured results: data UpStar f a b = UpStar {unupstar :: a f b} instance Functor f Profunctor (UpStar f ) where dimap f g (UpStar h) = UpStar (fmap g h f ) (Also functions taking structured arguments; but we don t need this.)

12 Profunctor Optics 12 Cartesian profunctors Subclass class Profunctor p Cartesian p where first :: p a b p (a c) (b c) of profunctors that can act in a product context, satisfying dimap runit runit 1 = first :: P a b P (a 1) (b 1) dimap assoc assoc 1 first first = first :: P a b P (a (c d)) (b (c d)) where runit and assoc witness the monoidal structure of products. For example, the function arrow is cartesian: instance Cartesian ( ) where first h (x, y) = (h x, y)

13 Profunctor Optics 13 Cocartesian profunctors Dually, subclass class Profunctor p Cocartesian p where right :: p a b p (c + a) (c + b) of profunctors that can act in a sum context, satisfying dimap lzero lzero 1 = right :: P a b P (0 + a) (0 + b) dimap coassoc coassoc 1 right right = right :: P a b P ((c + d) + a) ((c + d) + b) where lzero and coassoc witness the monoidal structure of sums. For example, the function arrow is cocartesian: instance Cocartesian ( ) where right h (Left x) = Left x right h (Right y) = Right (h y)

14 Profunctor Optics 14 Monoidal profunctors A third subclass class Profunctor p Parallel p where par :: p a b p c d p (a c) (b d) empty :: p 1 1 of profunctors that can act in parallel. Functions are an instance: instance Parallel ( ) where par f g (x, y) = (f x, g y) empty () = () Not entirely sure what laws we want here; perhaps empty is the unit of par, and coherence with the monoidal structure of products. (Don t seem to need any additional laws for the proofs.)

15 Profunctor Optics Profunctor optics Data accessors represented as mappings between transformers: type Optic p a b s t = p a b p s t lifting a transformer P A B on elements to a transformer P S T on whole data structures. Lenses, prisms, etc arise by imposing constraints on P, starting with it being a Profunctor. Crucially, all optics are now of the same form, and moreover, are functions: so they compose nicely.

16 Profunctor Optics 16 Profunctor adapters data Adapter a b s t = Adapter {from :: s a, to :: b t } type AdapterP a b s t = p. Profunctor p Optic p a b s t with translations adapterc2p :: Adapter a b s t AdapterP a b s t adapterc2p (Adapter o i) = dimap o i adapterp2c :: AdapterP a b s t Adapter a b s t adapterp2c l = l (Adapter id id) For the latter, we need that concrete adapters form a profunctor: instance Profunctor (Adapter a b) where dimap f g (Adapter o i) = Adapter (o f ) (g i)

17 Profunctor Optics 17 Profunctor lenses data Lens a b s t = Lens {view :: s a, update :: b s t } type LensP a b s t = p. Cartesian p Optic p a b s t with translations lensc2p :: Lens a b s t LensP a b s t lensc2p (Lens v u) = dimap (v id) u first lensp2c :: LensP a b s t Lens a b s t lensp2c l = l (Lens id fst) For the latter, we need that concrete lenses form a cartesian profunctor: instance Profunctor (Lens a b) where dimap f g (Lens v u) = Lens (v f ) (g u (id f )) instance Cartesian (Lens a b) where first (Lens v u) = Lens (v fst) ((u (id fst)) (snd snd))

18 Profunctor Optics 18 Profunctor prisms data Prism a b s t = Prism {match :: s t + a, build :: b t } type PrismP a b s t = p. Cocartesian p Optic p a b s t with translations prismc2p :: Prism a b s t PrismP a b s t prismc2p (Prism m b) = dimap m (id b) right prismp2c :: PrismP a b s t Prism a b s t prismp2c l = l (Prism Right id) For the latter, we need that concrete prisms form a cocartesian profunctor: instance Profunctor (Prism a b) where dimap f g (Prism m b) = Prism ((g + id) m f ) (g b) instance Cocartesian (Prism a b) where right (Prism m b) = Prism ((Left Left) ((Right + id) m)) (Right b)

19 Profunctor Optics 19 Profunctor traversals data Traversal a b s t = Traversal {trav :: s Trav a b t } type TraversalP a b s t = p. (Cartesian p, Cocartesian p, Parallel p) with translations Optic p a b s t traversalc2p :: Traversal a b s t TraversalP a b s t traversalc2p (Traversal h) k = dimap h fuse (traverse k) where fuse :: Trav b b t t traverse :: (Choice p, Parallel p) p a b p (Trav a c t) (Trav b c t) traversalp2c :: TraversalP a b s t Traversal a b s t traversalp2c l = l (Traversal (λx Trav (λf f x))) For the latter, we need that concrete traversals form a cartesian, cocartesian, parallel profunctor...

20 Profunctor Optics Composing profunctor optics Recall pi 1 :: Lens a b (a c) (b c) Make a profunctor version: pip 1 :: LensP a b (a c) (b c) pip 1 = lensc2p pi 1 so in fact pip 1 :: Cartesian p p a b p (a c) (b c) pip 1 = dimap (fork viewfst id) updatefst first (not complicated, but also not obvious). Similarly, thep :: PrismP a b (Maybe a) (Maybe b) thep = prismc2p the

21 Profunctor Optics 21 The payoff Now they compose very sweetly: pip 1 pip 1 :: LensP a b ((a c) d) ((b c) d) thep pip 1 :: (Cartesian p, Cocartesian p) Optic p a b (Maybe (a c)) (Maybe (b c)) In the heterogeneous case, the constraints simply conjoin; the result is neither a lens nor a prism. Similarly for traversals. With inorderp :: TraversalP a b (Tree a) (Tree b) inorderp = traversalc2p inorder we have for example inorderp pip 1 :: TraversalP a b (Tree (a c)) (Tree (b c))

22 Profunctor Optics Conclusion Based on many libraries, blog posts, IRC comments, etc, by Edward Kmett, Elliott Hird, Shachaf Ben-Kiki, Russell O Connor, and others. This work is primarily Matthew Pickering s undergraduate thesis. Paper to appear at the Programming conference: 2017.programming-conference.org and available (soon!) from my webpage:

The Art, Science, and Engineering of Programming

The Art, Science, and Engineering of Programming Modular Data Accessors Matthew Pickering a, Jeremy Gibbons b, and Nicolas Wu a a b University of Bristol University of Oxford Abstract Data accessors allow one to read and write components of a data structure,

More information

CSCE 314 Programming Languages Functors, Applicatives, and Monads

CSCE 314 Programming Languages Functors, Applicatives, and Monads CSCE 314 Programming Languages Functors, Applicatives, and Monads Dr. Hyunyoung Lee 1 Motivation Generic Functions A common programming pattern can be abstracted out as a definition. For example: inc ::

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

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

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18) Advanced Programming Handout 7 Monads and Friends (SOE Chapter 18) The Type of a Type In previous chapters we discussed: Monomorphic types such as Int, Bool, etc. Polymorphic types such as [a], Tree a,

More information

Idioms are oblivious, arrows are meticulous, monads are promiscuous

Idioms are oblivious, arrows are meticulous, monads are promiscuous MSFP 2008 Idioms are oblivious, arrows are meticulous, monads are promiscuous Sam Lindley, Philip Wadler and Jeremy Yallop Laboratory for Foundations of Computer Science The University of Edinburgh Abstract

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,

More information

Programming with Math and Logic

Programming with Math and Logic .. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

College Functors, Applicatives

College Functors, Applicatives College 2016-2017 Functors, Applicatives Wouter Swierstra with a bit of Jurriaan Hage Utrecht University Contents So far, we have seen monads define a common abstraction over many programming patterns.

More information

INTRODUCTION TO HASKELL

INTRODUCTION TO HASKELL INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be

More information

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER 2012 2013 MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS Time allowed TWO hours Candidates may complete the front

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

Arrow Basics. Ted Cooper CS510 Winter Here s a literate introduction to arrows and review of the basic Arrow typeclasses.

Arrow Basics. Ted Cooper CS510 Winter Here s a literate introduction to arrows and review of the basic Arrow typeclasses. Arrow Basics Ted Cooper theod@pdx.edu CS510 Winter 2016 1 Introduction Here s a literate introduction to arrows and review of the basic Arrow typeclasses. {-# LANGUAGE Arrows #-} module ArrowBasics where

More information

A Library Writer s Guide to Shortcut Fusion

A Library Writer s Guide to Shortcut Fusion A Library Writer s Guide to Shortcut Fusion Thomas Harper Department of Computer Science, University of Oxford tom.harper@cs.ox.ac.uk Abstract There are now a variety of shortcut fusion techniques in the

More information

CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 21, 2018 Last Class Types terminology Haskell s type system Currying Defining types Value constructors Algebraic data

More information

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

Background Type Classes (1B) Young Won Lim 6/28/18 Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

A general introduction to Functional Programming using Haskell

A general introduction to Functional Programming using Haskell A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell

More information

The Arrow Calculus (Functional Pearl)

The Arrow Calculus (Functional Pearl) The Arrow Calculus (Functional Pearl) Sam Lindley Philip Wadler Jeremy Yallop University of Edinburgh Abstract We introduce the arrow calculus, a metalanguage for manipulating Hughes s arrows with close

More information

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

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences] GADTs Advanced functional programming - Lecture 7 Wouter Swierstra and Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree

More information

Advances in Programming Languages

Advances in Programming Languages T O Y H Advances in Programming Languages APL8: Multiparameter Type Classes, Constructor Classes Ian Stark School of Informatics The University of Edinburgh Thursday 4 February Semester 2 Week 4 E H U

More information

FizzBuzz in Haskell by Embedding a Domain-Specific Language

FizzBuzz in Haskell by Embedding a Domain-Specific Language FizzBuzz in Haskell by Embedding a Domain-Specific Language by Maciej Piróg maciej.adam.pirog@gmail.com April 23, 2014 The FizzBuzz problem is simple but not trivial, which makes it a popular puzzle during

More information

arxiv: v1 [cs.pl] 17 May 2018

arxiv: v1 [cs.pl] 17 May 2018 Generic Deriving of Generic Traversals arxiv:1805.06798v1 [cs.pl] 17 May 2018 CSONGOR KISS, Imperial College London, United Kingdom MATTHEW PICKERING, University of Bristol, United Kingdom NICOLAS WU,

More information

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

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences] GADTs AFP Summer School Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: 3 A datatype data

More information

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

Background Type Classes (1B) Young Won Lim 6/14/18 Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Lecture 4: Higher Order Functions

Lecture 4: Higher Order Functions Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function

More information

Type-indexed functions in Generic Haskell

Type-indexed functions in Generic Haskell Type-indexed functions in Generic Haskell Johan Jeuring September 15, 2004 Introduction Today I will talk about: a Types of polymorphism. b Type-indexed functions. c Dependencies. Read about b, and c in

More information

Category Theory & Functional Data Abstraction

Category Theory & Functional Data Abstraction Category Theory & Functional Data Abstraction Brandon Shapiro Math 100b 1. Introduction Throughout mathematics, particularly algebra, there are numerous commonalities between the studies of various objects

More information

Monads and all that III Applicative Functors. John Hughes Chalmers University/Quviq AB

Monads and all that III Applicative Functors. John Hughes Chalmers University/Quviq AB Monads and all that III Applicative Functors John Hughes Chalmers University/Quviq AB Recall our expression parser expr = do a

More information

Programming Paradigms

Programming Paradigms PP 2017/18 Unit 12 Functions and Data Types in Haskell 1/45 Programming Paradigms Unit 12 Functions and Data Types in Haskell J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE

More information

Explicit Recursion in Generic Haskell

Explicit Recursion in Generic Haskell Explicit Recursion in Generic Haskell Andres Löh Universiteit Utrecht andres@cs.uu.nl 26th March 2003 This is joint work with Dave Clarke and Johan Jeuring. Overview What is Generic Haskell? The difference

More information

An Introduction to Programming and Proving in Agda (incomplete draft)

An Introduction to Programming and Proving in Agda (incomplete draft) An Introduction to Programming and Proving in Agda (incomplete draft) Peter Dybjer January 29, 2018 1 A first Agda module Your first Agda-file is called BoolModule.agda. Its contents are module BoolModule

More information

CSE 505: Concepts of Programming Languages

CSE 505: Concepts of Programming Languages CSE 505: Concepts of Programming Languages Dan Grossman Fall 2003 Lecture 6 Lambda Calculus Dan Grossman CSE505 Fall 2003, Lecture 6 1 Where we are Done: Modeling mutation and local control-flow Proving

More information

Type Systems Winter Semester 2006

Type Systems Winter Semester 2006 Type Systems Winter Semester 2006 Week 4 November 8 November 15, 2006 - version 1.1 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial

More information

All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell

All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell Edward Kmett http://comonad.com/ Categories Categories have objects and arrows Every

More information

Last time: generic programming

Last time: generic programming 1/ 55 Last time: generic programming val (=) : { D: DATA } D. t D. t bool 2/ 55 This time: monads etc., continued >>= effect E 3/ 55 Recap: monads, bind and let An imperative program let id =! counter

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

CIS 500 Software Foundations Fall September 25

CIS 500 Software Foundations Fall September 25 CIS 500 Software Foundations Fall 2006 September 25 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial programming language, then the

More information

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads Monad class EDAN40: Functional Programming Functors and Monads Jacek Malec Dept. of Computer Science, Lund University, Sweden April 23rd, 2018 Motivation: Separation of pure and impure code Properties

More information

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

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences GADTs Advanced functional programming - Lecture 7 Wouter Swierstra 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces:

More information

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 23 Advanced Concepts in Object-Oriented Programming Dan Grossman Spring 2011 So far... The difference between OOP and records of functions with shared private state

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 440: Programming Languages and Translators, Spring 2019 Mon Haskell, Part 4 CS 440: Programming Languages and Translators, Spring 2019 Mon 2019-01-28 More Haskell Review definition by cases Chapter 6: Higher-order functions Revisit currying map, filter Unnamed

More information

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB Monads and all that I. Monads John Hughes Chalmers University/Quviq AB Binary Trees in Haskell data Tree a = Leaf a Branch (Tree a) (Tree a) deriving (Eq,Show) Cf Coq: Inductive tree (A:Set) : Set := leaf

More information

Generalized Iteration and Coiteration for Higher-Order Nested Datatypes

Generalized Iteration and Coiteration for Higher-Order Nested Datatypes Generalized Iteration and Coiteration for Higher-Order Nested Datatypes Mendler rules! Andreas Abel (joint work with Ralph Matthes and Tarmo Uustalu) Slide 1 FoSSaCS 2003 Warsaw, Poland April 8, 2003 Work

More information

Theorem Proving Principles, Techniques, Applications Recursion

Theorem Proving Principles, Techniques, Applications Recursion NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion 1 CONTENT Intro & motivation, getting started with Isabelle Foundations & Principles Lambda Calculus Higher Order Logic,

More information

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 7 Lambda Calculus Dan Grossman Spring 2011 Where we are Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now:

More information

CSci 4223 Principles of Programming Languages

CSci 4223 Principles of Programming Languages CSci 4223 Principles of Programming Languages Lecture 11 Review Features learned: functions, tuples, lists, let expressions, options, records, datatypes, case expressions, type synonyms, pattern matching,

More information

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010 Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010 11 Polymorphism The functional iterator interface we have defined last lecture is nice, but it is not very general.

More information

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source

More information

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32 Contents 1 Declarative Programming 1 1.1 Functional programming.................... 2 1.1.1 Type polymorphism and higher-order functions.... 3 1.1.2 Lazy evaluation..................... 5 1.1.3 Class-based

More information

An axiomatic basis for bidirectional programming

An axiomatic basis for bidirectional programming An axiomatic basis for bidirectional programming Josh Ko and Zhenjiang Hu National Institute of Informatics Third Spring Festival Workshop, Karuizawa 21 March 2017 Asymmetric lenses Source (HTML)

More information

1 Delimited continuations in Haskell

1 Delimited continuations in Haskell 1 Delimited continuations in Haskell This section describes programming with delimited control in Haskell. Delimited control, like its instance, exceptions, is an effect. Therefore, we have to use monads.

More information

Generic Programming with Adjunctions

Generic Programming with Adjunctions Generic Programming with Adjunctions 0.0 Generic Programming with Adjunctions Ralf Hinze Computing Laboratory, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England ralf.hinze@comlab.ox.ac.uk

More information

High-performance defunctionalization in Futhark

High-performance defunctionalization in Futhark 1 High-performance defunctionalization in Futhark Anders Kiel Hovgaard Troels Henriksen Martin Elsman Department of Computer Science University of Copenhagen (DIKU) Trends in Functional Programming, 2018

More information

Boolean networks, local models, and finite polynomial dynamical systems

Boolean networks, local models, and finite polynomial dynamical systems Boolean networks, local models, and finite polynomial dynamical systems Matthew Macauley Department of Mathematical Sciences Clemson University http://www.math.clemson.edu/~macaule/ Math 4500, Spring 2017

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 15: Recursive Types & Subtyping CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

More information

Applicative programming with effects

Applicative programming with effects Under consideration for publication in J. Functional Programming 1 F U N C T I O N A L P E A R L Applicative programming with effects CONOR MCBRIDE University of Nottingham ROSS PATERSON City University,

More information

CIS 500 Software Foundations Midterm I

CIS 500 Software Foundations Midterm I CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking

More information

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

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor. Trees 1. Consider the following data type Tree and consider an example inhabitant tree: data Tree a = Bin (Tree a) a (Tree a) Leaf deriving Show tree :: Tree Int tree = Bin (Bin (Bin Leaf 1 Leaf ) 2 (Bin

More information

Haskell does it with class: Functorial unparsing

Haskell does it with class: Functorial unparsing Under consideration for publication in J. Functional Programming 1 F U N C T I O N A L P E A R L Haskell does it with class: Functorial unparsing RALF HINZE Institute of Information and Computing Sciences,

More information

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our

More information

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements CIS 500 Software Foundations Fall 2004 27 September IS 500, 27 September 1 The Lambda Calculus IS 500, 27 September 3 Announcements Homework 1 is graded. Pick it up from Cheryl Hickey (Levine 502). We

More information

Algebraic Types. Chapter 14 of Thompson

Algebraic Types. Chapter 14 of Thompson Algebraic Types Chapter 14 of Thompson Types so far Base types Int, Integer, Float, Bool, Char Composite types: tuples (t 1,t 2,,t n ) lists [t 1 ] functions (t 1 -> t 2 ) Algebraic types enumerated, product

More information

Starting Boolean Algebra

Starting Boolean Algebra Boolean Algebra March 2, 27 Diagram for FunChip2 Here is a picture of FunChip2 that we created more or less randomly in class on /25 (used in various Activities): Starting Boolean Algebra Boolean algebra

More information

Dependant Type Systems (saying what you are) Data Abstraction (hiding what you are) Review. Dependent Types. Dependent Type Notation

Dependant Type Systems (saying what you are) Data Abstraction (hiding what you are) Review. Dependent Types. Dependent Type Notation Dependant Type Systems (saying what you are) Data Abstraction (hiding what you are) #1 Recount We re now reaching the point where you have all of the tools and background to understand advanced topics.

More information

Monads in Haskell. Nathanael Schilling. December 12, 2014

Monads in Haskell. Nathanael Schilling. December 12, 2014 Monads in Haskell Nathanael Schilling December 12, 2014 Abstract In Haskell, monads provide a mechanism for mapping functions of the type a -> m b to those of the type m a -> m b. This mapping is dependent

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2) Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes Henrik Nilsson University of Nottingham, UK What is the type of (==)? E.g. the following both work: 1 == 2 a == b I.e., (==) can be used to compare

More information

The language of categories

The language of categories The language of categories Mariusz Wodzicki March 15, 2011 1 Universal constructions 1.1 Initial and inal objects 1.1.1 Initial objects An object i of a category C is said to be initial if for any object

More information

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 5: Lazy Evaluation and Infinite Data Structures Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a

More information

An introduction introduction to functional functional programming programming using usin Haskell

An introduction introduction to functional functional programming programming using usin Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program

More information

CSE-321 Programming Languages 2010 Midterm

CSE-321 Programming Languages 2010 Midterm Name: Hemos ID: CSE-321 Programming Languages 2010 Midterm Score Prob 1 Prob 2 Prob 3 Prob 4 Total Max 15 30 35 20 100 1 1 SML Programming [15 pts] Question 1. [5 pts] Give a tail recursive implementation

More information

Round-Tripping Balls. Building A Bi-Directional Printer/Parser

Round-Tripping Balls. Building A Bi-Directional Printer/Parser Round-Tripping Balls Building A Bi-Directional Printer/Parser 1 / 46 Introducing: Enterprise JSON 2 / 46 Introducing: Enterprise JSON datetime "27/6/2013 10:29 pm" 2 / 46 Introducing: Enterprise JSON datetime

More information

User-Defined Algebraic Data Types

User-Defined Algebraic Data Types 72 Static Semantics User-Defined Types User-Defined Algebraic Data Types An algebraic data type declaration has the general form: data cx T α 1... α k = K 1 τ 11... τ 1k1... K n τ n1... τ nkn introduces

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,

More information

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different. Chapter 6 Trees In a hash table, the items are not stored in any particular order in the table. This is fine for implementing Sets and Maps, since for those abstract data types, the only thing that matters

More information

Part III. Chapter 15: Subtyping

Part III. Chapter 15: Subtyping Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule

More information

Homework 1: Functional Programming, Haskell

Homework 1: Functional Programming, Haskell Com S 541 Programming Languages 1 September 25, 2005 Homework 1: Functional Programming, Haskell Due: problems 1 and 3, Thursday, September 1, 2005; problems 4-9, Thursday, September 15, 2005; remaining

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Week 5 Tutorial Structural Induction

Week 5 Tutorial Structural Induction Department of Computer Science, Australian National University COMP2600 / COMP6260 Formal Methods in Software Engineering Semester 2, 2016 Week 5 Tutorial Structural Induction You should hand in attempts

More information

CSC/MAT-220: Lab 6. Due: 11/26/2018

CSC/MAT-220: Lab 6. Due: 11/26/2018 CSC/MAT-220: Lab 6 Due: 11/26/2018 In Lab 2 we discussed value and type bindings. Recall, value bindings bind a value to a variable and are intended to be static for the life of a program. Type bindings

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming

More information

Example: A Compiler Fragment (1) Example: A Compiler Fragment (2) Example: A Compiler Fragment (3)

Example: A Compiler Fragment (1) Example: A Compiler Fragment (2) Example: A Compiler Fragment (3) LiU-FP2016: Lecture 7 Monads Henrik Nilsson University of Nottingham, UK Example: A Compiler Fragment (1) Identification is the task of relating each applied identifier occurrence to its declaration or

More information

Functional Programming in Haskell Part I : Basics

Functional Programming in Haskell Part I : Basics Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian

More information

Reasoning with the HERMIT

Reasoning with the HERMIT Reasoning with the HERMIT Tool Support for Equational Reasoning on GHC Core Programs Andrew Farmer Information and Telecommunication Technology Center University of Kansas, USA afarmer@ittc.ku.edu Neil

More information

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs Lecture 16 Treaps; Augmented BSTs Parallel and Sequential Data Structures and Algorithms, 15-210 (Spring 2012) Lectured by Margaret Reid-Miller 8 March 2012 Today: - More on Treaps - Ordered Sets and Tables

More information

Cuckoo Hashing for Undergraduates

Cuckoo Hashing for Undergraduates Cuckoo Hashing for Undergraduates Rasmus Pagh IT University of Copenhagen March 27, 2006 Abstract This lecture note presents and analyses two simple hashing algorithms: Hashing with Chaining, and Cuckoo

More information

Action graphs and Catalan numbers

Action graphs and Catalan numbers University of California, Riverside March 4, 2015 Catalan numbers The Catalan numbers are a sequence of natural numbers C 0, C 1, C 2,... given by the recursive formula C 0 = 1 C k+1 = k C i C k i. i=0

More information

Subsumption. Principle of safe substitution

Subsumption. Principle of safe substitution Recap on Subtyping Subsumption Some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. Which can be formalized as by introducing:

More information

Where is ML type inference headed?

Where is ML type inference headed? 1 Constraint solving meets local shape inference September 2005 2 Types are good A type is a concise description of the behavior of a program fragment. Typechecking provides safety or security guarantees.

More information

Haskell An Introduction

Haskell An Introduction Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First

More information

Advanced Functional Programming

Advanced Functional Programming Advanced Functional Programming Tim Sheard Portland State University Lecture 2: More about Type Classes Implementing Type Classes Higher Order Types Multi-parameter Type Classes Tim Sheard 1 Implementing

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information