Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures...

26
Lecture 25 Principles of Functional Programming Summer 2020 Monoids and Mappables The Algebra of Typeclasses

Transcript of Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures...

Page 1: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Lecture 25Principles of Functional Programming

Summer 2020

Monoids and MappablesThe Algebra of Typeclasses

Page 2: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Section 1

Algebra : A Tale of Abstraction

Jacob Neumann Monoids and Mappables 23 June 2020 2 / 26

Page 3: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Origins: Geometry

A geometricconstruction of the

square root

The various components of algebra began toemerge in various civilizations as early as 3800years ago

Especially in Greek mathematics, they wereprimarily concerned with geometricconstructions, and did what we think of asalgebraic operations geometrically.

Jacob Neumann Monoids and Mappables 23 June 2020 3 / 26

Page 4: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Variable abstraction!

Brahmagupta(c.598-668),

inventor(?) ofvariable abstraction

The idea of using and manipulating abstractquantities (variables) took time to develop fully.

Other more sophisticated ideas, like zero,negative numbers, place-value numerals,summation, etc. also developed progressively(and were independently invented by severalpeople)

By the seventh century, mathematicians werebeginning to solve linear, quadratic, and cubicequations, but still only understood these asisolated problems

Jacob Neumann Monoids and Mappables 23 June 2020 4 / 26

Page 5: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

The Birth of Algebra

A page from Al-Jabr

Algebra was first seriously put on its ownindependent footing by MuhammadAl-Khwarizmi, in his work “The CompendiousBook on Calculation by Completion andBalancing”, better known as Al-Jabr.

x2 = 50− x2

2x2 = 50

Algebra emerged as the study of equationsbetween arithmetical combinations of (integer)numbers and abstract variable quantities

Jacob Neumann Monoids and Mappables 23 June 2020 5 / 26

Page 6: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Beyond Numbers

Algebra developed more and more, broadening its areas of study

Algebraic manipulation of rational and irrational numbers

Development of complex numbers

Beginnings of linear algebra: matrices and determinants for solvingsystems of linear equations

Modular arithmetic

Study of polynomials as objects in their own right

Vector spaces

Jacob Neumann Monoids and Mappables 23 June 2020 6 / 26

Page 7: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

The Birth of Abstract Algebra

Galois, Noether

Modern abstract algebra developed followingthe work of Niels Abel and Evariste Galois.Galois coined the term group and was the firstto define a finite field, and establish a deepconnection between them (the study of thisconnection is known as Galois Theory).

Through the latter 19th and early 20th century,abstract algebra developed its modern form,stated in the language of set theory

Much of modern algebra is due to EmmyNoether, who pioneered many central ideas inmodern abstract algebra, such as rings,commutative rings, ideals, modules, andrepresentations

Jacob Neumann Monoids and Mappables 23 June 2020 7 / 26

Page 8: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Still a theory of equations and unknowns

Defn. A group consists of a set G, a function · : G×G→ G, anelement e ∈ G and a function (−)−1 : G→ G satisfying the followingconditions.

Associativity: for all x, y, z ∈ G,

x · (y · z) = (x · y) · z

Identity: for all x ∈ G,

x · e = x = e · x

Inverse: for all x ∈ G,

x · x−1 = e = x−1 · x

Jacob Neumann Monoids and Mappables 23 June 2020 8 / 26

Page 9: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

In SML!

25.0

1 signature GROUP =

2 sig

3 type t

4 (* For all a,b,c:t,

5 * cmb(a,cmb(c,b)) == cmb(cmb(a,b),c) *)

6 val cmb : t * t -> t

7 (* For all x:t,

8 * cmb(x,z) == x == cmb(z,x) *)

9 val z : t

10 (* For all x:t,

11 * cmb(x,inv x) == z == cmb(inv x,x) *)

12 val inv : t -> t

13 end

Jacob Neumann Monoids and Mappables 23 June 2020 9 / 26

Page 10: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

25.1

1 structure IntAddGroup : GROUP =

2 struct

3 type t = int

4 val cmb = op+

5 val z = 0

6 val inv = ∼7 end

Jacob Neumann Monoids and Mappables 23 June 2020 10 / 26

Page 11: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Use equational reasoning

Thm. For all groups G, if z’ : t is such thatG.cmb(x,z’) ∼= x ∼= G.cmb(z’,x) for all x:G.t, then

z’ ∼= G.z

Proof Pick arbitrary G : GROUP and suppose we had such a z’.Then

G.cmb(z,z’) ∼= z (assumption)

G.cmb(z,z’) ∼= z’ (G is a group)

Jacob Neumann Monoids and Mappables 23 June 2020 11 / 26

Page 12: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Jacob Neumann Monoids and Mappables 23 June 2020 12 / 26

Page 13: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Section 2

The Algebra of Functions

Jacob Neumann Monoids and Mappables 23 June 2020 13 / 26

Page 14: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Recall...

25.2

1 signature MONOID =

2 sig

3 type t

4 (* For all a,b,c:t,

5 * cmb(a,cmb(c,b)) == cmb(cmb(a,b),c) *)

6 val cmb : t * t -> t

7 (* For all x:t,

8 * cmb(x,z) == x == cmb(z,x) *)

9 val z : t

10 end

Defn. A monoid consists of a set M , a function · : M ×M →M andan element e ∈M satisfying the following conditions.

Associativity: for all x, y, z ∈M , x · (y · z) = (x · y) · zIdentity: for all x ∈M , x · e = x = e · xJacob Neumann Monoids and Mappables 23 June 2020 14 / 26

Page 15: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Composition Monoid

25.3

1 functor FunMonoid (type t’) : MONOID =

2 struct

3 type t = t’ -> t’

4 val cmb = op o

5 val z = fn (x:t’) => x

6 end

If we restrict ourselves to just bijective functions t’ -> t’, thenwe can perhaps define an inverse function and turn this into a group.But if the functions aren’t bijective, then this is just a monoid.

Must restrict ourselves to functions with same domain andcodomain, because composition isn’t defined between arbitraryfunctions (and thus doesn’t form a monoid).

Jacob Neumann Monoids and Mappables 23 June 2020 15 / 26

Page 16: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Partial Multi-Monoid thing?

Composition is an associative binary operation on functions, withthe identity function serving as an identity.But the composition of two arbitrary functions is not defined: eachfunction comes “tagged” with a domain and a codomain, andcomposition is only defined if the domains and codomains agree inthe proper way. Technically, there’s a different identity for eachpossible domain/codomain.Such an algebraic structure is called a category. The domains andcodomains are called “objects” and the functions are called“morphisms”

Jacob Neumann Monoids and Mappables 23 June 2020 16 / 26

Page 17: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Still an algebraic theory

h o (g o f) ∼= (h o g) o f

id o f ∼= f ∼= f o id

t1 t2 t3 t4f

g o f

g

h o g

h

t1 list t2 list

t3 list

map f

map (g o f)map g

Jacob Neumann Monoids and Mappables 23 June 2020 17 / 26

Page 18: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Algebraic Definitions

Recall how we specified the identity of a group by giving an equationgoverning the role of that element in the “group structure”. We cansimilarly define things by their role in the “categorical structure”.

An object u is called terminal if, for any other object t1, thereexists a unique morphism t1 -> u. unit is terminal.

A morphism f : t1 -> t2 is called an isomorphism if there’sanother g : t2 -> t1 such that

g o f ∼= id and f o g ∼= id

Given objects t1 and t2, an object C is called a coproduct of t1and t2 if there are morphisms inl : t1 -> C andinr : t2 -> C such that, for any object X and any morphismsf : t1 -> X and g : t2 -> X, there exists a uniquemorphism h : C -> X such that

h o inl ∼= f and h o inr ∼= g

Jacob Neumann Monoids and Mappables 23 June 2020 18 / 26

Page 19: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Section 3

The Algebra of Mappables

Jacob Neumann Monoids and Mappables 23 June 2020 19 / 26

Page 20: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Recall: MAPPABLE

25.4

1 signature MAPPABLE =

2 sig

3 type ’a t

4

5 (* for all appropriately -typed f,g

6 * map (g o f) == (map g) o (map f)

7 * map id == id

8 *)

9 val map : (’a -> ’b) -> ’a t -> ’b t

10 end

Jacob Neumann Monoids and Mappables 23 June 2020 20 / 26

Page 21: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

25.5

1 functor Compose( structure A : MAPPABLE

2 structure B : MAPPABLE)

3 : MAPPABLE =

4 struct

5 type ’a t = (’a B.t) A.t

6

7 fun map f = A.map (B.map f)

8 end

Associative (up to representational independence)

Identity:25.6

1 structure IdenMappable : MAPPABLE =

2 struct

3 type ’a t = ’a

4 fun map f = f

5 end

Jacob Neumann Monoids and Mappables 23 June 2020 21 / 26

Page 22: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Monads

The list MAPPABLE has extra features which are not captured by thissimple signature. Indeed, it has extra algebraic properties:

Given any type t1 and any value x : t1, we can get the value[x] which has type t1 ListMappable.t

If have a value of type t1 list list , I can “flatten” that intojust a t1 list .

We’ll specify these “Mappables++” as a SML signature, with algebraicequations as invariants.

Jacob Neumann Monoids and Mappables 23 June 2020 22 / 26

Page 23: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

25.7

1 signature MONAD =

2 sig

3 type ’a t

4

5 val pure : ’a -> ’a t

6

7 val >>= : ’a t * (’a -> ’b t) -> ’b t

8 end

Jacob Neumann Monoids and Mappables 23 June 2020 23 / 26

Page 24: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

25.8

1 infix 1 >>=

2

3 structure OptMonad : MONAD =

4 struct

5 type ’a t = ’a option

6

7 val pure = SOME

8

9 fun (NONE >>= _ ) = NONE

10 | ((SOME x) >>= f) = f x

11 end

Jacob Neumann Monoids and Mappables 23 June 2020 24 / 26

Page 25: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

25.10

1 structure ListMonad : MONAD =

2 struct

3 type ’a t = ’a list

4

5 fun pure x = [x]

6

7 fun (L >>= f) = List.concat(map f L)

8 end

25.11

1 structure L = ListMonad

2 infix 1 >>>=

3 val op >>>= = L.>>=

4

5 fun map f l = l >>>= (L.pure o f)

6 fun concat ll = ll >>>= Fn.id

Jacob Neumann Monoids and Mappables 23 June 2020 25 / 26

Page 26: Lecture 25 Principles of Functional Programming Summer 2020 › ~15150 › resources › lectures › 25 › lecture… · Principles of Functional Programming Summer 2020 Monoids

Thank you!

Jacob Neumann Monoids and Mappables 23 June 2020 26 / 26