Slide 1

Slide 1 text

Free Functors & Monads Lars Hupel March 16th, 2015

Slide 2

Slide 2 text

A Simple Calculator data Expr = Literal Int | Var String | Sum Expr Expr evaluate :: Map String Int -> Expr -> Maybe Int 2 / 22

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Calculator Revisited data Expr a t = Literal a | Var t | Sum (Expr a t) (Expr a t) 4 / 22

Slide 5

Slide 5 text

Calculator Revisited data Expr a t = Literal a | Var t | Sum (Expr a t) (Expr a t) > :t Sum Sum :: Expr a t -> Expr a t -> Expr a t 4 / 22

Slide 6

Slide 6 text

Calculator Revisited data Expr a t where Literal :: a -> Expr a t Var :: t -> Expr a t Sum :: Expr a t -> Expr a t -> Expr a t 4 / 22

Slide 7

Slide 7 text

Calculator Revisited data Expr a t where Literal :: a -> Expr a t Var :: t -> Expr a t Sum :: Expr a t -> Expr a t -> Expr a t ▶ So far: Expr a t contains only a literals ▶ type a is constant in the whole expression ▶ What if we want heterogeneous operations? > :t even even :: Integral a => a -> Bool 4 / 22

Slide 8

Slide 8 text

Calculator Revisited data Expr a where Literal :: a -> Expr a Sum :: Expr a -> Expr a -> Expr a 4 / 22

Slide 9

Slide 9 text

Calculator Revisited data Expr a where Literal :: a -> Expr a Sum :: Num a => Expr a -> Expr a -> Expr a Even :: Integral a => Expr a -> Expr Bool 4 / 22

Slide 10

Slide 10 text

Calculator Revisited data Expr a where Literal :: a -> Expr a Sum :: Num a => Expr a -> Expr a -> Expr a Even :: Integral a => Expr a -> Expr Bool Cast :: (a -> b) -> Expr a -> Expr b 4 / 22

Slide 11

Slide 11 text

A Fancy Calculator ▶ we now have a datatype which represents (some) arithmetic operations ▶ Apart from evaluating, what can we do with it? 5 / 22

Slide 12

Slide 12 text

A Fancy Calculator ▶ we now have a datatype which represents (some) arithmetic operations ▶ Apart from evaluating, what can we do with it? ▶ print ▶ count operations ▶ optimize 5 / 22

Slide 13

Slide 13 text

A Fancy Calculator ▶ we now have a datatype which represents (some) arithmetic operations ▶ Apart from evaluating, what can we do with it? ▶ print ▶ count operations ▶ optimize 5 / 22

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

7 / 22

Slide 16

Slide 16 text

The Essence of IO ▶ a representation of a computation ▶ ... which interacts with the world 8 / 22

Slide 17

Slide 17 text

The Essence of IO ▶ a representation of a computation ▶ ... which interacts with the world 8 / 22

Slide 18

Slide 18 text

The Essence of IO ▶ a representation of a computation ▶ ... which interacts with the world ▶ in Haskell: may contain all sorts of effects ▶ in GHC: opaque, non-inspectable 8 / 22

Slide 19

Slide 19 text

The Essence of IO ▶ a representation of a computation ▶ ... which interacts with the world ▶ in Haskell: may contain all sorts of effects ▶ in GHC: opaque, non-inspectable ▶ but: a better world is possible 8 / 22

Slide 20

Slide 20 text

IO as a DSL ▶ calculator: datatype with one constructor per operation ▶ terminal application: datatype with one constructor per operation? ▶ read from standard input ▶ write to standard output 9 / 22

Slide 21

Slide 21 text

IO as a DSL ▶ calculator: datatype with one constructor per operation ▶ terminal application: datatype with one constructor per operation? ▶ read from standard input ▶ write to standard output ▶ open file ▶ read from file ▶ ... 9 / 22

Slide 22

Slide 22 text

IO as a DSL ▶ calculator: datatype with one constructor per operation ▶ terminal application: datatype with one constructor per operation? ▶ read from standard input ▶ write to standard output ▶ open file ▶ read from file ▶ ... 9 / 22

Slide 23

Slide 23 text

A Datatype for Terminal IO data Terminal a where ReadLine :: Terminal String WriteLine :: String -> Terminal () 10 / 22

Slide 24

Slide 24 text

A Datatype for Terminal IO data Terminal a where ReadLine :: Terminal String WriteLine :: String -> Terminal () ▶ Terminal is an ordinary GADT ▶ nicely represents what we want ▶ But what to do with it? 10 / 22

Slide 25

Slide 25 text

Free Structures “ Informally, a free object over a set A can be thought as being a “generic” algebraic structure over A: the only equations that hold between elements of the free object are those that follow from the defining axioms of the algebraic structure. Wikipedia: Free object ” 11 / 22

Slide 26

Slide 26 text

Warning 12 / 22

Slide 27

Slide 27 text

Free Monoids Monoid (M, ⊕, e) ▶ a set M ▶ an operation ⊕ : M × M → M ▶ a neutral element e ∈ M ▶ associativity: m1 ⊕ (m2 ⊕ m3) = (m1 ⊕ m2) ⊕ m3 ▶ neutrality: m ⊕ e = e ⊕ m = m 13 / 22

Slide 28

Slide 28 text

Free Monoids Monoid (M, ⊕, e) ▶ a set M ▶ an operation ⊕ : M × M → M ▶ a neutral element e ∈ M ▶ associativity: m1 ⊕ (m2 ⊕ m3) = (m1 ⊕ m2) ⊕ m3 ▶ neutrality: m ⊕ e = e ⊕ m = m Question What is the simplest monoid over a given type t? 13 / 22

Slide 29

Slide 29 text

Free Monoids Monoid (M, ⊕, e) ▶ a set M ▶ an operation ⊕ : M × M → M ▶ a neutral element e ∈ M ▶ associativity: m1 ⊕ (m2 ⊕ m3) = (m1 ⊕ m2) ⊕ m3 ▶ neutrality: m ⊕ e = e ⊕ m = m Answer List t 13 / 22

Slide 30

Slide 30 text

Free Monads Monad m class Monad m where -- return a >>= k == k a -- m >>= return == m -- m >>= (\x -> k x >>= h) == (m >>= k) >>= h (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a 14 / 22

Slide 31

Slide 31 text

Free Monads Monad m class Monad m where -- return a >>= k == k a -- m >>= return == m -- m >>= (\x -> k x >>= h) == (m >>= k) >>= h (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a Question What is the simplest monad over a given type constructor f? 14 / 22

Slide 32

Slide 32 text

Free Monads Monad m class Monad m where -- return a >>= k == k a -- m >>= return == m -- m >>= (\x -> k x >>= h) == (m >>= k) >>= h (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a Answer Well ... 14 / 22

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Putting It All Together ▶ Recall our Terminal data type ▶ Stick it into FreeM and call it a day! data Terminal a where ReadLine :: Terminal String WriteLine :: String -> Terminal () 16 / 22

Slide 35

Slide 35 text

Putting It All Together ▶ Recall our Terminal data type ▶ Stick it into FreeM and call it a day! ▶ ... except, no. data Terminal a where ReadLine :: Terminal String WriteLine :: String -> Terminal () 16 / 22

Slide 36

Slide 36 text

Putting It All Together ▶ Recall our Terminal data type ▶ Stick it into FreeM and call it a day! ▶ ... except, no. ▶ It’s not even a Functor data Terminal a where ReadLine :: Terminal String WriteLine :: String -> Terminal () 16 / 22

Slide 37

Slide 37 text

Free Functors Functor f class Functor f where -- fmap id == id -- fmap (f . g) == fmap f . fmap g fmap :: (a -> b) -> f a -> f b 17 / 22

Slide 38

Slide 38 text

Free Functors Functor f class Functor f where -- fmap id == id -- fmap (f . g) == fmap f . fmap g fmap :: (a -> b) -> f a -> f b 17 / 22

Slide 39

Slide 39 text

Free Functors Functor f class Functor f where -- fmap id == id -- fmap (f . g) == fmap f . fmap g fmap :: (a -> b) -> f a -> f b Question What is the simplest functor over a given type constructor f? 17 / 22

Slide 40

Slide 40 text

Free Functors Functor f class Functor f where -- fmap id == id -- fmap (f . g) == fmap f . fmap g fmap :: (a -> b) -> f a -> f b Answer Coyoneda f 17 / 22

Slide 41

Slide 41 text

Coyoneda? “ What is sometimes called the co-Yoneda lemma is a basic fact about presheaves (a basic fact of topos theory): it says that every presheaf is a colimit of representables and more precisely that it is the “colimit over itself of all the representables contained in it”. nLab ” 18 / 22

Slide 42

Slide 42 text

Coyoneda? “ What is sometimes called the co-Yoneda lemma is a basic fact about presheaves (a basic fact of topos theory): it says that every presheaf is a colimit of representables and more precisely that it is the “colimit over itself of all the representables contained in it”. nLab ” 18 / 22

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Q & A  larsr h  larsrh

Slide 46

Slide 46 text

Image Credits ▶ Randall Munroe, https://xkcd.com/1312/ ▶ Thomas Kluyver, Kyle Kelley, Brian E. Granger, https://github.com/ipython/xkcd-font ▶ Gerd Laures, https://golem.ph.utexas.edu/category/ 2012/01/vorsicht_funktor.html 22 / 22