algebras in programming - harrylaou · •a calculus for lazy functional programming based on...

70
Algebras in Programming What do we mean by algerbra

Upload: others

Post on 11-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Algebras in ProgrammingWhat do we mean by algerbra

Page 2: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Harry Laoulakos@harrylaou

• Senior Scala Consultant

• Functional Programming

• Check Cost vs Benefit

Algebras in Programming 2

Page 3: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

For absolute beginners

• not necessary to know scala

• stay focussed on semantics

• build a vocabulary to communicate with fellow programmers

• "touches" mathematical concepts, but doesn't go deep

Algebras in Programming 3

Page 4: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Slideshttps://www.harrylaou.com/slides/AlgebrasInProgramming.pdf

Algebras in Programming 4

Page 5: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Areas of Mathematics• Elementary algebra

• Abstract algebra

• Linear algebra

• Boolean algebra

• Commutative algebra

• Homological algebra

Algebras in Programming 5

Page 6: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

more areas• Universal algebra

• Algebraic number theory

• Algebraic geometry

• Algebraic combinatorics

• Relational algebra

Algebras in Programming 6

Page 7: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Algebras can also be mathematical structuresFor example in category theory

• F-algebra

• F-coalgebra

• T-algebra

Algebras in Programming 7

Page 8: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

When we are talking about algebra, it is not necessarily one or even similar things.

Algebras in Programming 8

Page 9: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

In Programming

• ADTs: algebraic data types

• GADTs: Generalized algebraic data types

• Tagless Final Algebra

• Free Μonads Algebra

• Recursion Schemes Algebra

Algebras in Programming 9

Page 10: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

ADTAlgebraic data type

Algebras in Programming 10

Page 11: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

ADTis a composite type of - Product Types (or Tagged Unions)- Sum Types (or Co-Product or Unions)

Algebras in Programming 11

Page 12: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

• case class

• tuple

• HList

Algebras in Programming 12

Page 13: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class User(id:Long, username:String, ...)

Algebras in Programming 13

Page 14: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class User(id:Long, username:String, ...)

Think AND

Algebras in Programming 14

Page 15: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

• Either

• sealed trait

• Coproduct

Algebras in Programming 15

Page 16: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait Animalcase class Cat(name:String) extends Animalcase class Dog(name: String) extends Animal

Algebras in Programming 16

Page 17: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait Animalcase class Cat(name:String) extends Animalcase class Dog(name: String) extends Animal

Think OR

Algebras in Programming 17

Page 18: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Note

It is a different than inheritance.There are GADTs in Rust, which doesn't support inheritance.

Algebras in Programming 18

Page 19: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product TypeRust

struct User { id: u64, name: String}

Algebras in Programming 19

Page 20: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum TypeRust

enum Animal { Cat(String), Dog(String)}

Algebras in Programming 20

Page 21: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Union TypeScala 3

case class Cat(name:String) case class Dog(name: String)type Animal = Cat | Dog

or even

def func(a: Cat | Dog ) : String = ???

Algebras in Programming 21

Page 22: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Enums as ADTScala 3

enum Color(val rgb: Int) { case Red extends Color(0xFF0000) case Green extends Color(0x00FF00) case Blue extends Color(0x0000FF) case Mix(mix: Int) extends Color(mix)}

Algebras in Programming 22

Page 23: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Very common ADTs

• Option

• Either

Algebras in Programming 23

Page 24: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Option

(more or less)

sealed trait Option[+A] final case class Some[+A](value: A) extends Option[A]case object None extends Option[Nothing]

Algebras in Programming 24

Page 25: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Either

(more or less)

sealed trait Either[+A, +B] extendsfinal case class Left[+A, +B] (value: A) extends Either[A, B] final case class Right[+A, +B]( value: B) extends Either[A, B]

Algebras in Programming 25

Page 26: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

ADT• Parametrized : They can have type parameters

• Product Types: Think AND

• Sum Types: Think OR

Algebras in Programming 26

Page 27: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

GADTGeneralized Algebraic Data Type

• A generalization of parametric algebraic data types.

Algebras in Programming 27

Page 28: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

GADTExample

sealed trait Expr[T]case class Num(i: Int) extends Expr[Int]case class Bool(b: Boolean) extends Expr[Boolean]case class Add(x: Expr[Int], y: Expr[Int]) extends Expr[Int]case class Mult(x: Expr[Int], y: Expr[Int]) extends Expr[Int]case class Eq[A](x: Expr[A], y: Expr[A]) extends Expr[Boolean]

Algebras in Programming 28

Page 29: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

GADTExample

def eval[T](e: Expr[T]): T = e match { case Num(i) => i case Bool(b) => b case Add(x,y) => eval(x)+eval(y) case Mult(x,y) => eval(x)*eval(y) case Eq(x,y) => eval(x) == eval(y) }

Algebras in Programming 29

Page 30: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

GADTExample

val expr1 = Mult(Add(Num(1),Num(2)),Num(4))val expr2 = Num(12)val expr3 = Eq(expr1,expr2)

eval(expr3) true: Boolean

Algebras in Programming 30

Page 31: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

GADT• Type-safe business logic

• Type-safe DSLs

Algebras in Programming 31

Page 32: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Coool, but...where the heck is the Algebra?

Algebras in Programming 32

Page 33: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait VehicleTypecase object Car extends VehicleTypecase object Moto extends VehicleType

Algebras in Programming 33

Page 34: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait VehicleTypecase object Car extends VehicleTypecase object Moto extends VehicleType

How many VehicleTypes?

Algebras in Programming 34

Page 35: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait Colour case object Red extends Colourcase object Yellow extends Colourcase object Blue extends Colour

Algebras in Programming 35

Page 36: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait Colour case object Red extends Colourcase object Yellow extends Colourcase object Blue extends Colour

How many Colours?

Algebras in Programming 36

Page 37: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Sum Type

sealed trait VehicleTypecase object Car extends VehicleTypecase object Moto extends VehicleType

2 VehicleTypes

sealed trait Colour case object Red extends Colourcase object Yellow extends Colourcase object Blue extends Colour

3 Colours

Algebras in Programming 37

Page 38: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class Vehicle (vehicleType:VehicleType, colour:Colour)

Algebras in Programming 38

Page 39: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class Vehicle (vehicleType:VehicleType, colour:Colour)

How many vehicles? If 2 VehicleTypes and 3 Colours?

Algebras in Programming 39

Page 40: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class Vehicle (vehicleType:VehicleType, colour:Colour)

If 2 VehicleTypes and 3 Colours?2 * 3 = 6 Vehicles

Algebras in Programming 40

Page 41: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class Vehicle (vehicleType:VehicleType, colour:Colour, isUsed:Boolean)

If 2 VehicleTypes, 3 Colours and 2 Boolean types?

Algebras in Programming 41

Page 42: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Product Type

case class Vehicle (vehicleType:VehicleType, colour:Colour, isSecondHand:Boolean)

If 2 VehicleTypes, 3 Colours and 2 Boolean types?2 * 3 * 2 = 12 Vehicles

Algebras in Programming 42

Page 43: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

ADTAs in elementary algebra

• Sum is the result of addition

• Product is the result of multiplication.

Algebras in Programming 43

Page 44: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function TypeFunction is a type in Scala

def like(colour:Colour) :Boolean

or

val like : Colour => Boolean

also

type Like = Colour => Boolean

Algebras in Programming 44

Page 45: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function Type

type Like = Colour -> Boolean

Red -> {t,f}Yellow -> {t,f}Blue -> {t,f}

How many implementations?

Algebras in Programming 45

Page 46: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function Type

type Like = Colour -> Boolean

Red -> {t,f}Yellow -> {t,f}Blue -> {t,f}

There can be 2 * 2 * 2 = 23 implementations

Algebras in Programming 46

Page 47: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function Type

sealed trait Countrycase object France extends Country case object UK extends Countrycase object Germany extends Country case object Japan extends Country case object USA extends Country

5 countries

Algebras in Programming 47

Page 48: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function Type

sealed trait Brand case object Brand1 extends Brand case object Brand2 extends Brand...case object Brand20 extends Brand

2O brands

Algebras in Programming 48

Page 49: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function Type

def isProduced(brand:Brand) : Country

Brand1 -> {France, UK, Germany, Japan, USA}Brand2 -> {France, UK, Germany, Japan, USA} ...Brand20 -> {France, UK, Germany, Japan, USA}

How many implementations?

Algebras in Programming 49

Page 50: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Function Type

def isProduced(brand:Brand) : Country

Brand1 -> {France, UK, Germany, Japan, USA}Brand2 -> {France, UK, Germany, Japan, USA} ...Brand20 -> {France, UK, Germany, Japan, USA}

There can be 5 * 5 * ... * 5 (20 times) = 520 implementations

Algebras in Programming 50

Page 51: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

ExponentialsIn Category Theory

In mathematical literature, the function object, or the internal hom-object between two objects a and b, is often called the exponential and denoted by ba.

Algebras in Programming 51

Page 52: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Exponentials

In simpler terms

A Function Type val f : A => B

corresponds to the exponential

Algebras in Programming 52

Page 53: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Practical example

From the book: Functional Programming for Mortals

• (A => C) => ((B => C) => C)

• (Either[A, B] => C) => C

Algebras in Programming 53

Page 54: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

More Algebras

• Tagless Final Algebra

• Free Μonads Algebra

Algebras in Programming 54

Page 55: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Tagless Final vs Free Μonads

• The comparison is out of scope for this talk

• But let's try to find out what Algebra means

• A random blogpost: Free Monad vs Tagless Final

Algebras in Programming 55

Page 56: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Tagless Final Algebra

trait DatabaseAlgebra[F[_], T] { def create(t: T): F[Boolean] def read(id: Long): F[Either[DatabaseError, T]] def delete(id: Long): F[Either[DatabaseError, Unit]]}

... tagless final encoded algebras.

Algebras in Programming 56

Page 57: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Free Monad Algebra

sealed trait DBFreeAlgebraT[T]case class Create[T](t: T) extends DBFreeAlgebraT[Boolean]case class Read[T](id: Long) extends DBFreeAlgebraT[Either[DatabaseError, T]]case class Delete[T](id: Long) extends DBFreeAlgebraT[Either[DatabaseError, Unit]]

• ... We need to create some interpreters to interpret our algebra into actions'

• ... For this section we’ll create interpreters to execute...

Algebras in Programming 57

Page 58: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Free Monad and Tagless Final

• in essence the Interpreter pattern

• A grammar for a simple language should be defined

• in Free Monad and Tagless Final : the grammar is called "Algebra"

• the grammar/algebra is implemented by concrete implementations.

Algebras in Programming 58

Page 59: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Category Theory

• formalizes mathematical structure and its concepts in terms of a labeled directed graph called a category, whose nodes are called objects, and whose labelled directed edges are called arrows (or morphisms)

• the mathematics of mathematics

• the science of composition and abstraction

• the mathematics behind functional programming

Algebras in Programming 59

Page 60: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Algebras in Category Theory• F-algebra

• F-coalgebra

• Free Algebra

• Cofree Algebra

Algebras in Programming 60

Page 61: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Algebra

• Set of A

• Some operator

• unary: A => A

• binary (A, A) => A

• n-ary (A,A,...,A ) => A

• What about Seq[A] => A ?

• or Option[A]=> A

• F[A] => A

Algebras in Programming 61

Page 62: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

F-Algebra

• F[A] => A

• If the F is also a functor

• this is called F-algebra

• F-algebra generalization of normal algebra

Algebras in Programming 62

Page 63: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Co-

• Co-Algebra

• Co-Free

Algebras in Programming 63

Page 64: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Co-

• Co-Algebra

• Co-Free

What does co- mean?

Algebras in Programming 64

Page 65: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Duality in Category Theory

Duality is a correspondence between the properties of a category C and the dual properties of the opposite category Cop

Algebras in Programming 65

Page 66: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

F-coalgebra

• if F[A] => A is an algebra

• If we reverse the arrows

• type Coalgebra[F[_], A] = A => F[A]

Algebras in Programming 66

Page 67: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Recursion Schemes

• a calculus for lazy functional programming based on recursion operators associated with data type definitions.

• in simple terms: is an advanced functional programming technique that moves the the recursion in the type level

• using F-Algebras and F- Coalgebras

Algebras in Programming 67

Page 68: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Recap

• (G)ADT analogy to elementary algebra

• Sum Type => arithmetic addition

• Product Type => arithmetic multiplication

• Function Type => exponential operation

• Free Monads Algebra - Tagless Final Algebra => Grammar in Interpreter Pattern

• F-algebra

• F-coalgebra

Algebras in Programming 68

Page 69: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

References / Read more• https://en.wikipedia.org/wiki/Algebra

• What the F is a GADT?

• GADT support in Scala

• Function Types

• Functional Programming for Mortals

• Free Monad vs Tagless Final

• Recursion Schemes for Higher Algebras

• The Science Behind Functional Programming

• AST playground: recursion schemes and recursive data

Algebras in Programming 69

Page 70: Algebras in Programming - Harrylaou · •a calculus for lazy functional programming based on recursion operators associated with data type definitions. •in simple terms: is an

Questions?https://www.harrylaou.com/slides/AlgebrasInProgramming.pdf

Harry Laoulakos : @harrylaou

Algebras in Programming 70