String Diagrams for Free Monads og , University of Wrocaw, Poland - - PowerPoint PPT Presentation

string diagrams for free monads
SMART_READER_LITE
LIVE PREVIEW

String Diagrams for Free Monads og , University of Wrocaw, Poland - - PowerPoint PPT Presentation

String Diagrams for Free Monads og , University of Wrocaw, Poland Maciej Pir (joint work with Nicolas Wu , University of Bristol, UK) Institute of Cybernetics Tallinn, 8 Dec 2016 Practical programming Abstract nonsense Practical


slide-1
SLIDE 1

String Diagrams for Free Monads

Maciej Pir´

  • g, University of Wrocław, Poland

(joint work with Nicolas Wu, University of Bristol, UK) Institute of Cybernetics Tallinn, 8 Dec 2016

slide-2
SLIDE 2

Abstract nonsense Practical programming

slide-3
SLIDE 3

Abstract nonsense Practical programming We want our reasoning principles to be:

  • equational: proofs are by chains of equalities

between programs

  • expressive: leveraging high-level properties of

particular constructs

slide-4
SLIDE 4

free monads

A type of trees with nodes shaped by the functor f and leaves given by variables of the type a.

data Free f a = Var a | Op (f (Free f a))

Monadic bind given by substitution in leaves. Sometimes denoted F∗.

slide-5
SLIDE 5

the problem

  • Inductive data types (initial algebra) come with a low-level

resoning principle (structural induction).

  • However, structural induction can be a bit awkward.
slide-6
SLIDE 6

the problem

  • Inductive data types (initial algebra) come with a low-level

resoning principle (structural induction).

  • However, structural induction can be a bit awkward.

Consider the free monad transformer:

newtype FreeT f m a = FreeT (m (Free (f . m) a)

Would you like to prove the monad laws using structural induction by hand?

slide-7
SLIDE 7

(From an unpublished appendix to M.Pir´

  • g & J.Gibbons. Monads for behaviour. MFPS 2013)
slide-8
SLIDE 8

the problem

  • Let’s invent more abstract reasoning principles that

respect the underlying structure!

slide-9
SLIDE 9

string diagrams

  • A 2-dimensional notation for expressions
  • Useful for presentation of more complicated expressions

that involve a number of functors

  • Some administrative equalities are built-in
slide-10
SLIDE 10

lookup

Consider the function

lookup k :: ∀a.List (Key, a) → Maybe a

It is polymorphic in a. So, alternatively, we write lookup k : List ◦ (Key, -) → Maybe

slide-11
SLIDE 11

lookup

Consider the function

lookup k :: ∀a.List (Key, a) → Maybe a

It is polymorphic in a. So, alternatively, we write lookup k : List ◦ (Key, -) → Maybe

lookup k

List (Key, -) Maybe

slide-12
SLIDE 12

composition

concat

List List List

lookup k

(Key, -) List Maybe

slide-13
SLIDE 13

composition

concat

List List List

lookup k

(Key, -) List Maybe

slide-14
SLIDE 14

composition

List

concat

List List List

lookup k

(Key, -) Maybe

slide-15
SLIDE 15

composition

List

concat

List List List

lookup k

(Key, -) Maybe

slide-16
SLIDE 16

composition

List

concat

List List List

lookup k

(Key, -) Maybe

slide-17
SLIDE 17

composition

List

concat

List List List

lookup k

(Key, -) Maybe

slide-18
SLIDE 18

composition

List

concat

List List

lookup k

(Key, -) Maybe

slide-19
SLIDE 19

composition

lookup k . concat :: ∀a.List (Key, a) → Maybe a

List

concat

List List

lookup k

(Key, -) Maybe

slide-20
SLIDE 20

composition

lookup k . concat :: ∀a.List (List (Key, a)) → Maybe a

List

concat

List List

lookup k

(Key, -) Maybe

slide-21
SLIDE 21

composition

lookup k . concat . fmap reverse :: ∀a.List (List (Key, a)) → Maybe a

lookup k concat reverse

List List (Key, -) Maybe

slide-22
SLIDE 22

free monads, more abstractly

This definition is only one of many possible implementations:

data Free f a = Var a | Op (f (Free f a))

Shouldn’t this be an interface?

class FreeMonad f a where ???

What are the operations and laws that characterise free monads?

slide-23
SLIDE 23

freeness, mathematically

emb :: ∀a.f a → Free f a

emb : F → F∗ F F∗ Generic operation that takes a single operation to a term.

slide-24
SLIDE 24

freeness, mathematically

interp :: (Functor f, Monad m) ⇒ (∀x.f x -> m x) -> Free f a → m a

Given a monad M and f : F → M, there is a monad morphism

⌊f⌋ : F∗ → M

F∗ M

F M

f

slide-25
SLIDE 25

cancellation

interp f . emb = f ⌊f⌋ · emb = f

F M

F∗ F

f

=

F M f Interpreting a term that consists of one operation is the same as interpreting that operation.

slide-26
SLIDE 26

reflection

interp emb = id ⌊emb⌋ = id

F∗ F∗

F

=

F∗ F∗ Interpreting operations as syntax... preserves syntax.

slide-27
SLIDE 27

fusion

m . interp f = interp (m . f)

m · ⌊f⌋ = ⌊m · f⌋, where m is a monad morphism f m F∗ T

F M

=

f m F∗ T

F M

Interpreting a term and then transforming semantics is the same as interpreting and transforming the term in one go.

slide-28
SLIDE 28

example: renaming is functorial

Given functors F and G, and a natural transformation f : F → G, we can define a natural transformation (hoist) F∗ → G∗ as follows:

⌊emb · f⌋

F∗ G∗

F G

f

slide-29
SLIDE 29

Given three functors F, G, H, and two natural transformations f : F → G and g : G → H, is the composition of hoists a hoist of composition? That is:

⌊emb · g⌋ · ⌊emb · f⌋ = ⌊emb · g · f⌋

F∗ H∗

F G G∗ G H

f g

=

F∗ H∗

F G H

f g

slide-30
SLIDE 30

example: renaming is functorial

F∗ H∗

F G G∗ G H

f g

slide-31
SLIDE 31

example: renaming is functorial

F∗ H∗

F G G∗ G H

f g

(fusion)

=

F∗ H∗

F G G∗ G H

f g

slide-32
SLIDE 32

example: renaming is functorial

F∗ H∗

F G G∗ G H

f g

(fusion)

=

F∗ H∗

F G G∗ G H

f g

(cancellation)

=

F∗ H∗

F G H

f g

slide-33
SLIDE 33

going back to FreeT

Sadly, what we have seen is not enough to show that FreeT is a monad. Luckily, free monads in Haskell have other kinds of similar properties.

slide-34
SLIDE 34

distributable free monads

Given f : F ◦ G → G ◦ F F G G F f

slide-35
SLIDE 35

distributable free monads

Given f : F ◦ G → G ◦ F F G G F f we get

f : F∗ ◦ G → G ◦ F∗

F∗ G G F∗ f

F F

slide-36
SLIDE 36

cancellation

f · emb = G emb · f

F G G F∗ f

F F∗ F

=

F G G F∗ f

F

slide-37
SLIDE 37

reflection

id = id

F∗ F∗

F

=

F∗ F∗

slide-38
SLIDE 38

fusion

Jf′ · fK = Jf′ · fK F∗ J K J K F∗ f f′

F F F F

=

F∗ J K J K F∗ f f′

F G F

slide-39
SLIDE 39

uniformity

fF∗ · g · F∗f = fF · g · F∗f F∗ J F∗ K g f f

K F F J

=

F∗ J F∗ K g f f

K F F J

slide-40
SLIDE 40

uniformity

Kind of

(fg)∗f = f(gf)∗

in Kleene algebra

slide-41
SLIDE 41

(From an unpublished appendix to M.Pir´

  • g & J.Gibbons. Monads for behaviour. MFPS 2013)
slide-42
SLIDE 42

S∗ M M M S∗

µ δ η

(unit)

=

S∗ M M M S∗

µ δ µ η η

(uniformity)

=

S∗ M M M S∗

µ δ µ η η

(associativity)

=

S∗ M M M S∗

δ δ µ η η

(fusion)

=

S∗ M M M S∗

δ δ µ η η

slide-43
SLIDE 43

more in the ICFP 2016 paper

  • one more universal property (generalised fold)
  • relationship between different universal properties
  • a lot of further examples and an equational proof of

a real-life theorem

  • explanation where all these properties come from