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

Post on 11-Aug-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Algebras in ProgrammingWhat do we mean by algerbra

Harry Laoulakos@harrylaou

• Senior Scala Consultant

• Functional Programming

• Check Cost vs Benefit

Algebras in Programming 2

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

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

Algebras in Programming 4

Areas of Mathematics• Elementary algebra

• Abstract algebra

• Linear algebra

• Boolean algebra

• Commutative algebra

• Homological algebra

Algebras in Programming 5

more areas• Universal algebra

• Algebraic number theory

• Algebraic geometry

• Algebraic combinatorics

• Relational algebra

Algebras in Programming 6

Algebras can also be mathematical structuresFor example in category theory

• F-algebra

• F-coalgebra

• T-algebra

Algebras in Programming 7

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

Algebras in Programming 8

In Programming

• ADTs: algebraic data types

• GADTs: Generalized algebraic data types

• Tagless Final Algebra

• Free Μonads Algebra

• Recursion Schemes Algebra

Algebras in Programming 9

ADTAlgebraic data type

Algebras in Programming 10

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

Algebras in Programming 11

Product Type

• case class

• tuple

• HList

Algebras in Programming 12

Product Type

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

Algebras in Programming 13

Product Type

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

Think AND

Algebras in Programming 14

Sum Type

• Either

• sealed trait

• Coproduct

Algebras in Programming 15

Sum Type

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

Algebras in Programming 16

Sum Type

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

Think OR

Algebras in Programming 17

Note

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

Algebras in Programming 18

Product TypeRust

struct User { id: u64, name: String}

Algebras in Programming 19

Sum TypeRust

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

Algebras in Programming 20

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

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

Very common ADTs

• Option

• Either

Algebras in Programming 23

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

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

ADT• Parametrized : They can have type parameters

• Product Types: Think AND

• Sum Types: Think OR

Algebras in Programming 26

GADTGeneralized Algebraic Data Type

• A generalization of parametric algebraic data types.

Algebras in Programming 27

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

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

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

GADT• Type-safe business logic

• Type-safe DSLs

Algebras in Programming 31

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

Algebras in Programming 32

Sum Type

sealed trait VehicleTypecase object Car extends VehicleTypecase object Moto extends VehicleType

Algebras in Programming 33

Sum Type

sealed trait VehicleTypecase object Car extends VehicleTypecase object Moto extends VehicleType

How many VehicleTypes?

Algebras in Programming 34

Sum Type

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

Algebras in Programming 35

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

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

Product Type

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

Algebras in Programming 38

Product Type

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

How many vehicles? If 2 VehicleTypes and 3 Colours?

Algebras in Programming 39

Product Type

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

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

Algebras in Programming 40

Product Type

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

If 2 VehicleTypes, 3 Colours and 2 Boolean types?

Algebras in Programming 41

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

ADTAs in elementary algebra

• Sum is the result of addition

• Product is the result of multiplication.

Algebras in Programming 43

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

Function Type

type Like = Colour -> Boolean

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

How many implementations?

Algebras in Programming 45

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

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

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

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

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

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

Exponentials

In simpler terms

A Function Type val f : A => B

corresponds to the exponential

Algebras in Programming 52

Practical example

From the book: Functional Programming for Mortals

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

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

Algebras in Programming 53

More Algebras

• Tagless Final Algebra

• Free Μonads Algebra

Algebras in Programming 54

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

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

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

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

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

Algebras in Category Theory• F-algebra

• F-coalgebra

• Free Algebra

• Cofree Algebra

Algebras in Programming 60

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

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

Co-

• Co-Algebra

• Co-Free

Algebras in Programming 63

Co-

• Co-Algebra

• Co-Free

What does co- mean?

Algebras in Programming 64

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

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

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

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

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

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

Harry Laoulakos : @harrylaou

Algebras in Programming 70

top related