Applicative, traversable, foldable

Similar documents
Applicative, traversable, foldable

College Functors, Applicatives

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

Applicative programming with effects

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

Lecture 4: Higher Order Functions

Data types à la carte

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

CS 457/557: Functional Languages

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

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

CSCE 314 Programming Languages Functors, Applicatives, and Monads

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

INTRODUCTION TO HASKELL

Type families and data kinds

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

Monads in Haskell. Nathanael Schilling. December 12, 2014

Monad Overview (3B) Young Won Lim 1/16/18

Programming Languages

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

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

Programming with Math and Logic

A general introduction to Functional Programming using Haskell

Introduction to Functional Programming in Haskell 1 / 56

Monads. COS 441 Slides 16

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

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

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

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

Functional Programming

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

Idioms are oblivious, arrows are meticulous, monads are promiscuous

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

Monads seen so far: IO vs Gen

Accurate Step Counting

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

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

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

An introduction introduction to functional functional programming programming using usin Haskell

Advanced Functional Programming

The Haskell HOP: Higher-order Programming

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

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

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

CS 457/557: Functional Languages

Week 5 Tutorial Structural Induction

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

CSCE 314 Programming Languages

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Applicatives Comparisons (2C) Young Won Lim 3/6/18

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

Shell CSCE 314 TAMU. Higher Order Functions

Monads. Bonus lecture 2017 David Sands

Monads. Functional Programming (CS4011) Monads

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

Software System Design and Implementation

CIS 194: Homework 6. Due Friday, October 17, Preface. Setup. Generics. No template file is provided for this homework.

Last time: generic programming

CS 11 Haskell track: lecture 4. n This week: Monads!

Programming Languages 3. Definition and Proof by Induction

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Programming Paradigms, Fall 06

Maybe Monad (3B) Young Won Lim 1/3/18

FUNCTIONAL PEARL Parsing Permutation Phrases

LECTURE 17. Expressions and Assignment

Polymorphism Overview (1A) Young Won Lim 2/20/18

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

Course year Typeclasses and their instances

Introduction. Wouter Swierstra. Applied functional programming. Faculty of Science Information and Computing Sciences

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

6.001 Notes: Section 6.1

Haskell Scripts. Yan Huang

INDEX. Symbols & Numbers. Learn You a Haskell for Great Good! 2011 by Miran Lipovača

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

CMSC 330: Organization of Programming Languages

Accurate Step Counting

Modular Reifiable Matching

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Principles of Programming Languages (H)

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

A more elegant specification for FRP

1 The smallest free number

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

Semantics of programming languages

301AA - Advanced Programming

Monad (3A) Young Won Lim 8/9/17

Background Operators (1E) Young Won Lim 7/7/18

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Functional Programming - 2. Higher Order Functions

Monad Background (3A) Young Won Lim 10/5/17

CSCE 314 Programming Languages

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

DerivingVia or, How to Turn Hand-Written Instances into an Anti-Pattern

CS 11 Haskell track: lecture 5. This week: State monads

Transcription:

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. This kind of abstraction occurs more often in Haskell s libraries. In this lecture we will cover: applicative functors foldable traversable arrows 2 We ll motivate the need for applicative functors starting with examples.

Sequencing IO operations sequenceio :: [IO a] -> IO [a] sequenceio [] = return [] sequenceio (c : cs) = do x <- c xs <- sequenceio cs return (x : xs) There is nothing wrong with this code but using do notation may seem like overkill. The variable x isn t used in the rest of the computation! We would like to apply one monadic computation to another. 3

Using ap The ap function defined as follows: ap : Monad m => m (a -> b) -> m a -> m b ap mf mx = do f <- mf x <- mx return (f x) Using ap we can write: sequenceio :: [IO a] -> IO [a] sequenceio [] = return [] sequenceio (c:cs) = return (:) ap c ap sequenceio cs 4 This even works for any monad, not just the IO monad.

Evaluating expressions Another example: data Expr v = Var v Val Int Add (Expr v) (Expr v) type Env v = Map v Int eval : Expr v -> Env v -> Int eval (Var v) env = lookup v env eval (Val i) env = i eval (Add l r) env = (eval l env) + (eval r env) Once again, we are passing around an environment that is only really used in the Var branch. 5

An applicative alternative const : a -> (env -> a) const x = \env -> a s : (env -> a -> b) -> (env -> a) -> (env -> b) s ef es env = (ef env) (es env) eval : Expr v -> Env v -> Int eval (Var v) = lookup v eval (Val i) = const i eval (Add l r) env = const (+) s (eval l) s (eval r) 6 The s combinator lets us apply one computation expecting an environment to another.

Transposing matrices transpose :: [[a]] -> [[a]] transpose [] = repeat [] transpose (xs : xss) = zipwith (:) xs (transpose xss) Can we play the same trick and find a combinator that will apply a list of functions to a list of arguments? zapp : [a -> b] -> [a] -> [b] zapp (f : fs) (x : xs) = (f x) : (zapp fs xs) transpose (xs : xss) = repeat (:) zapp xs zapp transpose xss 7

What is the pattern? What do these functions have in common? ap : IO (a -> b) -> IO a -> IO b s : (env -> a -> b) -> (env -> a) -> (env -> b) zapp : [a -> b] -> [a] -> [b] 8

Applicative (applicative functors) class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b Note that Functor is a superclass of Applicative. We can also define map in terms of the applicative operations: (<$>) :: Functor f => (a -> b) -> f a -> f b 9

Applicative (applicative functors) class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b Note that Functor is a superclass of Applicative. We can also define map in terms of the applicative operations: (<$>) :: Functor f => (a -> b) -> f a -> f b (<$>) f fx = pure f <*> fx 9

Relating Applicative functors and Monads Every monad can be given an applicative functor interface. instance Monad m => Applicative m where pure :: a -> m a pure = return mf <*> mx = do f <- mf; x <- mx; return (f x) But this may not always be the right choice. For example, we have seen the zippy applicative instance for lists; using the instance arising from the list monad gives very different behaviour! But not every applicative functor is a monad 10

Monads vs. applicative functors - I (<*>) :: (Applicative f) => f (a -> b) -> f a -> f b (>>=) :: (Monad m) => m a -> (a -> m b) -> m b The arguments to <*> are (typically) first-order structures (that may contain higher-order data). Monadic bind is inherently higher order. With monads, subsequent actions can depend on the results of effects: depending on the character the user enters, respond differently. 11

Monads vs applicative functors - II There are more Applicative functors than there are monads; but monads are more powerful! If you have an Applicative functor, that s good; having a monad is better. If you need a monad, that s good; only needing an Applicative functor is better. With applicative functors, the structure is statically determined (and can be analyzed or optimized). Consider the following example: miffy : Monad m => m Bool -> m a -> m a -> ma 12

Composing monads Given two monads m1 and m2, is m1. m2 a monad? 13

Composing monads Given two monads m1 and m2, is m1. m2 a monad? data Compose m1 m2 a = Compose (m1 (m2 a)) instance (Monad m1, Monad m2) => Monad (Compose m1 m2) where return :: a -> m1 (m2 a) (>>=) :: m1(m2 a) -> (a -> m1(m2 b)) -> m1(m2 b) 13 Unfortunately, there is no guarantee that such an instance can be defined. As a result, there has been a great deal of work on monad transformers, that allow complex monads to be assembled from smaller pieces. For applicative functors however

Composing applicative functors For any pair of applicative functors f and g: data Compose f g a = Compose (f (g a)) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure :: a -> f (g a) pure x =... (<*>) :: f (g (a -> b)) -> (f (g a)) -> f (g b) fgf <*> fgx =... We can define the desired pure and <*> operations! This is a guarantee of compositionality. 14

Composing applicative functors For any pair of applicative functors f and g: data Compose f g a = Compose (f (g a)) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure :: a -> f (g a) pure x = pure (pure x) (<*>) :: f (g (a -> b)) -> (f (g a)) -> f (g b) fgf <*> fgx = (pure <*>) <*> fgf <*> fgx We can define the desired pure and <*> operations! This is a guarantee of compositionality. 15

Imprecise but catchy slogans Monads are programmable semi-colons! Applicatives are programmable function application! 16

Applicative functor laws identity pure id <*> u = u composition pure (.) <*> u <*> v <*> w = u <*> (v <*> w) homomorphism pure f <*> pure x = pure (f x) interchange u <*> pure x = pure ( f -> f x) <*> u 17

Spot the pattern sequenceio :: [IO a] -> IO [a] sequenceio [] = pure [] sequenceio (c:cs) = pure (:) <*> c <*> sequenceio cs transpose :: [[a]] -> [[a]] transpose [] = pure [] transpose (xs : xss) = pure (:) <*> xs <*> transpose xss Both these functions take a list of applicative actions as argument. 18 They traverse this list, performing the actions one by one, collecting the results in a list.

Traversing lists We can define a new function to capture this pattern: traverse :: Applicative f => [f a] -> f [a] traverse [] = pure [] traverse (x:xs) = pure (:) <*> x <*> traverse xs Clearly we can traverse lists in this fashion but what other data types support such an operation? 19

Traversable The Traversable class captures those types that can be traversed in this fashion: class Traversable t where traverse :: Applicative f => (a -> f b) -> t a -> f (t b) sequencea :: Applicative f => t (f a) -> f (t a) sequencea = traverse id It requires a slightly more general traverse than the one we have seen so far. 20

Traversable: example data Expr v = Var v Val Int Add (Expr v) (Expr v) instance Traversable Expr where traverse :: Applicative f => (a -> f b) -> Expr a -> f (Expr b) traverse f (Var v) = pure Var <*> f v traverse f (Val x) = pure (Val x) traverse f (Add l r) = pure Add <*> traverse f l <*> traverse f r In general, traverse is just like fmap only in applicative style. 21

fmap via traverse newtype Id a = Id {getid :: a} myfmap :: Traversable f => (a -> b) -> f a -> f b myfmap f = getid. traverse (Id. f) 22

Introducing Foldable In the Haskell libraries, Traversable is defined slightly differently. class (Functor t, Foldable t) => Traversable t where What is the Foldable class? 23

Folding a list The foldr function on lists captures a common pattern think of it as a functional for-loop. foldr :: (a -> b -> b) -> b -> [a] -> b foldr f y [] = y foldr f y (x:xs) = f x (foldr f y xs) We can use it to define all kinds of simple list traversals: sum = foldr (+) 0 max = foldr max 0 xs (++) = \ys -> foldr (:) ys xs concat = foldr (++) [] map = \f -> foldr (xs -> f x : xs) [] 24

Folding: beyond lists There are many other data types that support some form of fold operator. data Tree a = Leaf a Node (Tree a) (Tree a) Empty foldtree :: (a -> b -> b) -> b -> Tree a -> b foldtree f y Empty = y foldtree f y (Leaf x) = f x y foldtree f y (Node l r) = foldtree f (foldtree f y r) l Note that generic programming gives a slightly more precise account. 25

Foldable class Foldable f where foldr :: (a -> b -> b) -> b -> f a -> b foldmap :: Monoid m => (a -> m) -> t a -> m Sometimes it can be easier to define the foldmap function but what is a Monoid? 26

Intermezzo: monoids class Monoid a where mempty :: a mappend :: a -> a -> a Here mempty should be the unit of the associative operator mappend. Examples? 27

Monoids everywhere Bool using && and True; Bool using or and False; Int using + and 0; Int using * and 1; Int using max and minbound; List a using ++ and []; Imperative programs using ; and skip; a -> a using. and id; Monoids pop up everywhere! 28

Defining foldmap Instead of defining fold, sometimes it can be easier to define foldmap: foldmap :: Monoid m => (a -> m) -> Tree a -> m foldmap f Empty = mempty foldmap f (Leaf x) = f x foldmap f (Node l r) = foldmap f l mappend foldmap f r You need to apply f all a values in the tree and combine subtrees using mappend. 29

Why? What is the point of all this abstraction? We all agree (I hope!) that foldr is useful for lists. sum = foldr (+) 0 max = foldr max 0 xs (++) ys = foldr (:) ys xs concat = foldr (++) [] map f = foldr (xs -> f x : xs) [] 30

Why? What is the point of all this abstraction? We all agree (I hope!) that foldr is useful for lists. sum :: Foldable f => f Int -> Int sum = foldr (+) 0 max :: Foldable f => f Int -> Int max = foldr max 0 flatten :: Foldable f => f a -> [a] flatten = foldmap (-> [x])... but we can now give definitions that work for any foldable structure. 31

Generalizing any As a slightly less trivial example, consider the any function: any :: (a -> Bool) -> [a] -> Bool any p [] = False any p (x:xs) = p x any p xs How can we generalize this to work on any traversable type? 32

Mighty Booleans Let s start by finding the right monoidal structure. Instead of defining an instance for Bool, introducing a new type can help clarify the monoidal structure we are using. newtype Might = Might {might : Bool} instance Monoid Might where mempty = Might False (Might b) mappend (Might b ) = Might (b b ) 33

Generic any any :: Foldable f => (a -> Bool) -> f a -> Bool any p = might. foldmap (Might. p) Many other functions can be generalized similarly. 34

The Foldable-Traversible Proposal As of GHC 7.10, many Prelude functions have been generalized to work over any traversable structure and not just lists. Suppose we have a data type for binary trees, with the obvious traversable/foldable instances: data Tree a where Leaf :: a -> Tree a Node :: Tree a -> Tree a -> Tree a We can use the prelude functions we are used to over this data structure too. 35

Example folds over trees > let t = (Node (Leaf 1) (Node (Leaf 2) (Leaf 3)) > any iseven t True > length t 3 > elem 3 t True We no longer need to define specialized functions for trees. 36

Exercise What is the foldable instance for Maybe? What about the foldable instance for pairs? 37

Drawbacks But there are also quite surprising examples: minimum (1,1000) length (lookup 4 [(2, Hello ), (4, World ), (5,! )] null (lookup 3 []) 38

Drawbacks But there are also quite surprising examples: minimum (1,1000) length (lookup 4 [(2, Hello ), (4, World ), (5,! )] null (lookup 3 []) Sometimes code may type check, where you would have liked to see a type error. 38

Introducing arrows If applicative functors generalize the notion of application, can we find a similar abstraction over functions and function composition? Yes! There is more than a decade of work investigating functional programming using Arrows. 39

Arrows class Arrow a where arr :: (b -> c) -> a b c (>>>) :: a b c -> a c d -> a b d first :: a b c -> a (b,d) (c,d) Just like applicative functors and monads, arrows have several associated laws. Many programs using arrows require additional operations similar to classes such as MonadPlus. GHC supports special syntax for programming with Arrows, similar to the do notation for monads. 40

Historical context 41 Monads were originally studied in the context of program language semantics. Only later, was their importance for structuring programs discovered (and subsequently, modelling IO in Haskell) Arrows (Hughes 2000) were proposed as a weaker alternative to monads, but they have not been widely adopted. More recently, applicative functors have gained a lot of traction in the Haskell community (McBride and Paterson 2008), generalising the interface by Duponcheel and Swierstra (1996). Applicative functors, together with the associated Traversable and Foldable classes, are now part of the Haskell standard.

Why care? Functional programmers are adicted to abstraction: as soon as they spot a pattern, they typically want to abstract over it. The type classes we have seen today, such as monads, applicative functors, foldable, and traversable, all capture some common pattern. Using these patterns can save you some boilerplate code. But understanding these patterns can help guide your design. Is my type a monad? Or is it just applicative? Can I find a Traversable instance? Why not? 42

Further reading Applicative programming with effects, McBride and Paterson Monoids: Theme and Variations, Brent Yorgey Programming with arrows, John Hughes Idioms are oblivious, arrows are meticulous, monads are promiscuous, Lindley, Wadler and Yallop and much much more! 43