SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
THE ORIGINS OF FREE
Adam Warski, SoftwareMill
27 May 2017, LambdaConf
@adamwarski, SoftwareMill, LambdaConf 2017
THE PLAN
➤ Why bother?
➤ Universal algebra
➤ Free algebras
➤ The meaning of life free
➤ Monads & free monads
➤ Free monads in Haskell, Cats and Scalaz
➤ It’s simpler than it sounds
@adamwarski, SoftwareMill, LambdaConf 2017
WHY BOTHER WITH FREE?
➤ Free monads:
➤ programs as values
➤ separate definition from interpretation
➤ testing
➤ composability
➤ use a high-level language
➤ hide low-level concerns
@adamwarski, SoftwareMill, LambdaConf 2017
WHAT IS AN ALGEBRA?
“algebra is the study of mathematical symbols and the rules for manipulating these symbols"

Wikipedia, 2017
“the part of mathematics in which letters and other general symbols are used to represent numbers
and quantities in formulae and equations."

Google Search, 2017
y = ax + b
E = mc2
f(10◊x) = K(▸9)
def sum(l: List[L]) = l.fold(_ + _)
main = getCurrentTime >>= print
@adamwarski, SoftwareMill, LambdaConf 2017
UNIVERSAL ALGEBRA: SIGNATURE
➤ Goal: Model programs as algebras
➤ Let’s generalise!
➤ Studies algebraic structures, rather than concrete models
algebraic signature ⌃ = (S, ⌦)
set S
family ⌦ of sets indexed by S⇤
⇥ S
➤ Syntax:
➤ type names:
➤ operation names:
@adamwarski, SoftwareMill, LambdaConf 2017
UNIVERSAL ALGEBRA: SIGNATURE EXAMPLE
toString(succ(0) + succ(succ(0)))
S = {int, str}
⌦✏,int = {0}
⌦int,int = {succ}
⌦(int,int),int = {+}
⌦(str,str),str = {++}
⌦int,str = {toString}
@adamwarski, SoftwareMill, LambdaConf 2017
UNIVERSAL ALGEBRA: SIGNATURE EXAMPLE
S = {v}
⌦✏,v = {1}
⌦(v,v),v = {⇤}
(1 ⇤ 1) ⇤ (1 ⇤ (1 ⇤ 1))
@adamwarski, SoftwareMill, LambdaConf 2017
UNIVERSAL ALGEBRA: ALGEBRA
➤ A specific interpretation of the signature
➤ for each type, a set
➤ for each operation, a function between appropriate sets
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
We can define a ⌃-algebra A:
|A|int = {0, 1, 2, ...} = N
|A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...}
succA = x.x + 1
+A = xy.x + y
. . .
@adamwarski, SoftwareMill, LambdaConf 2017
UNIVERSAL ALGEBRA: ALGEBRA
➤ As a side-note, F-algebras:











are a generalisation of single-sorted algebras presented here
type Algebra[F[_], A] = F[A] => A
// or
type Algebra f a = f a -> a
@adamwarski, SoftwareMill, LambdaConf 2017
TERM ALGEBRA
➤ Can we build an algebra out of pure syntax?
➤ Expressions (terms) that can be built from the signature
➤ Rather boring, no interpretation at all
|T⌃|int = {0, succ(0), succ(succ(0)), ..., 0 + 0, 0 + succ(0), ...}
|T⌃|str = {toString(0), toString(succ(0)), ..., toString(0)++toString(0), ...}
succT⌃ (t) = succ(t), e.g. succT⌃ (succ(0)) = succ(succ(0))
+T⌃ (t1, t2) = t1 + t2
...
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
We define the term algebra T⌃:
@adamwarski, SoftwareMill, LambdaConf 2017
TERM ALGEBRA
➤ Defined inductively
➤ base: all constants are terms
➤ step: any functions we can apply on previous terms
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
{0}
{0, 0 + 0, succ(0), toString(0)}
{0, 0 + 0, succ(0), 0 + succ(0), succ(0) + 0, succ(0) + succ(0),
toString(0), toString(succ(0)), toString(0) + +toString(0)}
@adamwarski, SoftwareMill, LambdaConf 2017
TERM ALGEBRA
⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤}
1, 1 ⇤ 1, (1 ⇤ 1) ⇤ 1, 1 ⇤ (1 ⇤ 1), 1 ⇤ ((1 ⇤ 1) ⇤ 1), ...
@adamwarski, SoftwareMill, LambdaConf 2017
HOMOMORPHISM
➤ Homomorphism is a function between algebras
➤ For each type, functions between type interpretations
➤ Such that operations are preserved
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
When A and B are ⌃-algebras, f : A ! B is a homomorphism when:
fint : |A|int ! |B|int
fstr : |A|str ! |B|str
8x2|A|int
fint(succA(x)) = succB(fint(x))
8xy2|A|int
f(x +A y) = f(x) +B f(y)
8x2|A|int
fstr(toStringA(x)) = toStringB(fint(x))
@adamwarski, SoftwareMill, LambdaConf 2017
INITIAL ALGEBRA
Theorem 1 T⌃ is initial
⌃-algebra I is initial when for any other
⌃-algebra A there is exactly one
homomorphism from I to A.
@adamwarski, SoftwareMill, LambdaConf 2017
INITIAL ALGEBRA
Theorem 1 T⌃ is initial
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
|A|int = {0, 1, 2, ...} = N
|A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...}
succA = x.x + 1
+A = xy.x + y
. . .
We can define a ⌃-algebra A:
f : T⌃ ! A
f(0T⌃ ) = 0A
f(succT⌃ (t)) = succA(f(t))
...
@adamwarski, SoftwareMill, LambdaConf 2017
INITIAL ALGEBRA
➤ Only one way to interpret a term
➤ no junk: term algebra contains only what’s absolutely necessary
➤ no confusion: no two values are combined if they don’t need to be
➤ There’s only one initial algebra (up to isomorphism)
⌃-algebra I is initial when for any other
⌃-algebra A there is exactly one
homomorphism between them.
Theorem 1 T⌃ is initial
@adamwarski, SoftwareMill, LambdaConf 2017
INITIAL ALGEBRA
➤ This algebra is definitely not initial:
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
➤ Junk: strings “a”, “b”, …
➤ Confusion: 0+succ(0) is same as succ(0)+0
We can define a ⌃-algebra A:
|A|int = {0, 1, 2, ...} = N
|A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...}
@adamwarski, SoftwareMill, LambdaConf 2017
TERMS WITH VARIABLES
For any set X, T⌃(X) is the term algebra with X added as ”constants”
(but called variables)
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
Xint = {i, j, k}
Xstr = {s1, s2}
succ(i) + j + succ(succ(k))
s1 + +toString(0)
toString(succ(0) + k) + +s2
@adamwarski, SoftwareMill, LambdaConf 2017
TERMS WITH VARIABLES
For any set X, T⌃(X) is the term algebra with X added as ”constants”
(but called variables)
⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤}
Xv = {x, y, z, ...}
1 ⇤ x
x ⇤ (y ⇤ z)
(x ⇤ y) ⇤ z)
(x ⇤ 1) ⇤ (y ⇤ (1 ⇤ 1))
@adamwarski, SoftwareMill, LambdaConf 2017
FREE ALGEBRA
➤ An interpretation of the variables determines an interpretation of any term
Theorem 1 For any variable set X, T⌃(X) is free
For any set X, T⌃(X) is the term algebra with X added as ”constants”
(but called variables)
⌃-algebra I is free over X (X ⇢ I) when for any other
⌃-algebra A, any function f : X ! |A| extends uniquely
to a homomorphism f#
: I ! A between them.
@adamwarski, SoftwareMill, LambdaConf 2017
FREE ALGEBRA EXAMPLE
⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString}
Xint = {i, j, k}
Xstr = {s1, s2}
|A|int = N, |A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...}
succA = x.x + 1
+A = xy.x + y
. . .
f : X ! |A|
f(i) = 10, f(j) = 5, f(k) = 42
f(s1) = ”lambda”, f(s2) = ”conf”
f#
: T⌃(X) ! A
f#
(toString(succ(j) + succ(0)) + +s1) = ”7lambda”
f#
(s2 + +toString(k) + +s2) = ”lambda42conf”
@adamwarski, SoftwareMill, LambdaConf 2017
MEANING OF FREE
➤ Free to interpret in any way
➤ no constraints
➤ Free of additional structure
➤ only what’s absolutely necessary
➤ No junk, no confusion
@adamwarski, SoftwareMill, LambdaConf 2017
ALGEBRAS WITH EQUATIONS
➤ Let’s add constraints on interpretations of a signature
➤ that is, on the algebras
➤ And let’s focus only algebras satisfying a set of equations
⌃-equation: 8X.t = t0
X: variables, with sorts
t, t0
2 T⌃(X): terms with these variables
➤ Equations:
@adamwarski, SoftwareMill, LambdaConf 2017
ALGEBRAS WITH EQUATIONS
⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤}
8{x, y, z}x ⇤ (y ⇤ z) = (x ⇤ y) ⇤ z
8{x}x ⇤ 1 = x
8{x}1 ⇤ x = x
:
@adamwarski, SoftwareMill, LambdaConf 2017
TERM ALGEBRAS WITH EQUATIONS
➤ usually doesn’t satisfy the equations
➤ Some terms need to be “combined”
➤ Equivalence relation generated by
➤ Instead of terms, sets of terms
➤ equivalent wrt equations from
T⌃(X)
: ⌘
@adamwarski, SoftwareMill, LambdaConf 2017
TERM ALGEBRAS WITH EQUATIONS
⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤}
8{x, y, z}x ⇤ (y ⇤ z) = (x ⇤ y) ⇤ z
8{x}x ⇤ 1 = x
8{x}1 ⇤ x = x
:
T⌃(X)/ ⌘ : {1}
{x, x ⇤ 1, 1 ⇤ x}
{x ⇤ (y ⇤ z), (x ⇤ y) ⇤ z, (1 ⇤ x) ⇤ (y ⇤ z), 1 ⇤ (x ⇤ (y ⇤ z)), ...}
@adamwarski, SoftwareMill, LambdaConf 2017
FREE ALGEBRAS WITH EQUATIONS
Theorem 1 For any variable set X and equations , T⌃(X)/ ⌘ is free in the
class of algebras satisfying
➤ Homomorphism to any other algebra
➤ how to interpret a set of terms?
➤ interpret any of them
@adamwarski, SoftwareMill, LambdaConf 2017
FREE ALGEBRAS WITH EQUATIONS
⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤}
8{x, y, z}x ⇤ (y ⇤ z) = (x ⇤ y) ⇤ z
8{x}x ⇤ 1 = x
8{x}1 ⇤ x = x
:
T⌃(X)/ ⌘ : {1}
{x, x ⇤ 1, 1 ⇤ x}
{x ⇤ (y ⇤ z), (x ⇤ y) ⇤ z, (1 ⇤ x) ⇤ (y ⇤ z), 1 ⇤ (x ⇤ (y ⇤ z)), ...}
➤ Is there a simpler representation?
[]
[x]
[x, y, z]
@adamwarski, SoftwareMill, LambdaConf 2017
FREE RECAP
➤ Algebraic signature:
➤ All possible interpretations: algebras
➤ For any variable set
➤ The term algebra is free
➤ any interpretation of the variables
➤ determines an interpretation of any term
➤ A general construction
⌃ = (S, ⌦)
X
T⌃(X)
f : X ! |A|
f#
: T⌃(X) ! A
@adamwarski, SoftwareMill, LambdaConf 2017
MODELLING SEQUENTIAL PROGRAMS: MONADS
➤ A sequential program can:
➤ return a value (pure)
➤ compute what to do next basing on previous result (flatMap)
➤ People decided to call an object with such operations a Monad
➤ Hence, we’ll use Monads to represent programs as data
➤ + sanity laws
@adamwarski, SoftwareMill, LambdaConf 2017
FREE MONAD
➤ Signature ~ pure + flatMap
➤ Variables ~ operations (our DSL)
➤ Free Monad ~ terms built out of pure, flatMap, our DSL
➤ modulo monad laws!
➤ e.g. flatMap(pure(x), f) = f(x)
Interpretation of the DSL determines the interpretation of the whole program
@adamwarski, SoftwareMill, LambdaConf 2017
FREE IN CATS/SCALAZ
trait Free[F[_], A]



object Free {

case class Pure[F[_], A](a: A) extends Free[F, A]

case class Suspend[F[_], A](a: F[A]) extends Free[F, A]

case class FlatMapped[F[_], B, C](

c: Free[F, C], f: C => Free[F, B]) extends Free[F, B]

}
@adamwarski, SoftwareMill, LambdaConf 2017
FREE IN HASKELL
data Free f r = Free (f (Free f r)) | Pure r
trait Free[F[_], A]



object Free {

case class Pure[F[_], A](a: A) extends Free[F, A]

case class Join[F[_], A](f: F[Free[F, A]]) extends Free[S, A]

}
f/F[_] must be a functor!
@adamwarski, SoftwareMill, LambdaConf 2017
SUMMING UP
➤ Direct construction of free algebras
➤ Hand-wavy construction of free monad
➤ Free
➤ free to interpret in any way
➤ free of constraints
➤ no junk, no confusion
➤ Free in Haskell is the same free as in Scala
@adamwarski, SoftwareMill, LambdaConf 2017
ABOUT ME
➤ Software Engineer, co-founder @
➤ Custom software development; Scala/Java/Kafka/Cassandra/…
➤ Open-source: Quicklens, MacWire, ElasticMQ, ScalaClippy, …
➤ Long time ago: student of Category Theory
@adamwarski, SoftwareMill, LambdaConf 2017
FURTHER READING
➤ “Foundations of Algebraic Specification and Formal
Software Development” by Donald Sannella and
Andrzej Tarlecki
➤ The Internet
THANK YOU!

Weitere ähnliche Inhalte

Was ist angesagt?

Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Abhimanyu Mishra
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicosKmilo Bolaños
 
Prestation_ClydeShen
Prestation_ClydeShenPrestation_ClydeShen
Prestation_ClydeShenClyde Shen
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
Metodos jacobi y gauss seidel
Metodos jacobi y gauss seidelMetodos jacobi y gauss seidel
Metodos jacobi y gauss seidelCesar Mendoza
 
Left and Right Folds - Comparison of a mathematical definition and a programm...
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...Philip Schwarz
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2SHAKOOR AB
 
Formal methods 2 - languages and machines
Formal methods   2 - languages and machinesFormal methods   2 - languages and machines
Formal methods 2 - languages and machinesVlad Patryshev
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebraRonald Teo
 

Was ist angesagt? (17)

Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicos
 
Lecture 4 f17
Lecture 4 f17Lecture 4 f17
Lecture 4 f17
 
Lecture 11 f17
Lecture 11 f17Lecture 11 f17
Lecture 11 f17
 
Prestation_ClydeShen
Prestation_ClydeShenPrestation_ClydeShen
Prestation_ClydeShen
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
Metodos jacobi y gauss seidel
Metodos jacobi y gauss seidelMetodos jacobi y gauss seidel
Metodos jacobi y gauss seidel
 
Left and Right Folds - Comparison of a mathematical definition and a programm...
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
Calculus Lecture 1
Calculus Lecture 1Calculus Lecture 1
Calculus Lecture 1
 
Quantifier
QuantifierQuantifier
Quantifier
 
Formal methods 2 - languages and machines
Formal methods   2 - languages and machinesFormal methods   2 - languages and machines
Formal methods 2 - languages and machines
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebra
 
Basic calculus (i)
Basic calculus (i)Basic calculus (i)
Basic calculus (i)
 
Wg qcolorable
Wg qcolorableWg qcolorable
Wg qcolorable
 

Ähnlich wie Origins of Free

Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"LogeekNightUkraine
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Parallel Bayesian Optimization
Parallel Bayesian OptimizationParallel Bayesian Optimization
Parallel Bayesian OptimizationSri Ambati
 
Dataflow Analysis
Dataflow AnalysisDataflow Analysis
Dataflow AnalysisMiller Lee
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
Truth, deduction, computation lecture f
Truth, deduction, computation   lecture fTruth, deduction, computation   lecture f
Truth, deduction, computation lecture fVlad Patryshev
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Hakka Labs
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Philip Schwarz
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevJavaDayUA
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"Sergei Winitzki
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
A Coq Library for the Theory of Relational Calculus
A Coq Library for the Theory of Relational CalculusA Coq Library for the Theory of Relational Calculus
A Coq Library for the Theory of Relational CalculusYoshihiro Mizoguchi
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
February 11 2016
February 11 2016February 11 2016
February 11 2016khyps13
 
Defining Functions on Equivalence Classes
Defining Functions on Equivalence ClassesDefining Functions on Equivalence Classes
Defining Functions on Equivalence ClassesLawrence Paulson
 
Statistics - ArgMax Equation
Statistics - ArgMax EquationStatistics - ArgMax Equation
Statistics - ArgMax EquationAndrew Ferlitsch
 

Ähnlich wie Origins of Free (20)

Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Parallel Bayesian Optimization
Parallel Bayesian OptimizationParallel Bayesian Optimization
Parallel Bayesian Optimization
 
Dataflow Analysis
Dataflow AnalysisDataflow Analysis
Dataflow Analysis
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
Truth, deduction, computation lecture f
Truth, deduction, computation   lecture fTruth, deduction, computation   lecture f
Truth, deduction, computation lecture f
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
A Coq Library for the Theory of Relational Calculus
A Coq Library for the Theory of Relational CalculusA Coq Library for the Theory of Relational Calculus
A Coq Library for the Theory of Relational Calculus
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
lecture_lex.pdf
lecture_lex.pdflecture_lex.pdf
lecture_lex.pdf
 
February 11 2016
February 11 2016February 11 2016
February 11 2016
 
Defining Functions on Equivalence Classes
Defining Functions on Equivalence ClassesDefining Functions on Equivalence Classes
Defining Functions on Equivalence Classes
 
Statistics - ArgMax Equation
Statistics - ArgMax EquationStatistics - ArgMax Equation
Statistics - ArgMax Equation
 

Mehr von SoftwareMill

Growing Oxen: channel operators and retries
Growing Oxen: channel operators and retriesGrowing Oxen: channel operators and retries
Growing Oxen: channel operators and retriesSoftwareMill
 
How To Survive a Live-Coding Session
How To Survive a Live-Coding SessionHow To Survive a Live-Coding Session
How To Survive a Live-Coding SessionSoftwareMill
 
Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...
Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...
Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...SoftwareMill
 
Have you ever wondered about code review?
Have you ever wondered about code review?Have you ever wondered about code review?
Have you ever wondered about code review?SoftwareMill
 
Reactive Integration with Akka Streams and Alpakka
Reactive Integration with Akka Streams and AlpakkaReactive Integration with Akka Streams and Alpakka
Reactive Integration with Akka Streams and AlpakkaSoftwareMill
 
W świecie botów czyli po co nam SI
W świecie botów czyli po co nam SIW świecie botów czyli po co nam SI
W świecie botów czyli po co nam SISoftwareMill
 
Small intro to Big Data
Small intro to Big DataSmall intro to Big Data
Small intro to Big DataSoftwareMill
 
Out-of-the-box Reactive Streams with Java 9
Out-of-the-box Reactive Streams with Java 9Out-of-the-box Reactive Streams with Java 9
Out-of-the-box Reactive Streams with Java 9SoftwareMill
 
Hiring, Bots and Beer. (Hiring in the IT industry)
Hiring, Bots and Beer. (Hiring in the IT industry) Hiring, Bots and Beer. (Hiring in the IT industry)
Hiring, Bots and Beer. (Hiring in the IT industry) SoftwareMill
 
Teal Is The New Black
Teal Is The New BlackTeal Is The New Black
Teal Is The New BlackSoftwareMill
 
Windowing data in big data streams
Windowing data in big data streamsWindowing data in big data streams
Windowing data in big data streamsSoftwareMill
 
Kafka as a message queue
Kafka as a message queueKafka as a message queue
Kafka as a message queueSoftwareMill
 
Introduction to Cassandra
Introduction to CassandraIntroduction to Cassandra
Introduction to CassandraSoftwareMill
 
Cassandra - how to fail?
Cassandra - how to fail?Cassandra - how to fail?
Cassandra - how to fail?SoftwareMill
 
How to manage in a flat organized, remote and transparent company
How to manage in a flat organized, remote and transparent companyHow to manage in a flat organized, remote and transparent company
How to manage in a flat organized, remote and transparent companySoftwareMill
 
Performance tests with gatling
Performance tests with gatlingPerformance tests with gatling
Performance tests with gatlingSoftwareMill
 
Projekt z punktu widzenia UX designera
Projekt z punktu widzenia UX designeraProjekt z punktu widzenia UX designera
Projekt z punktu widzenia UX designeraSoftwareMill
 
Machine learning by example
Machine learning by exampleMachine learning by example
Machine learning by exampleSoftwareMill
 
Open source big data landscape and possible ITS applications
Open source big data landscape and possible ITS applicationsOpen source big data landscape and possible ITS applications
Open source big data landscape and possible ITS applicationsSoftwareMill
 
Jednorożce to kobiety a nie firmy. O √kobiecym w STEM
Jednorożce to kobiety a nie firmy. O √kobiecym w STEMJednorożce to kobiety a nie firmy. O √kobiecym w STEM
Jednorożce to kobiety a nie firmy. O √kobiecym w STEMSoftwareMill
 

Mehr von SoftwareMill (20)

Growing Oxen: channel operators and retries
Growing Oxen: channel operators and retriesGrowing Oxen: channel operators and retries
Growing Oxen: channel operators and retries
 
How To Survive a Live-Coding Session
How To Survive a Live-Coding SessionHow To Survive a Live-Coding Session
How To Survive a Live-Coding Session
 
Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...
Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...
Goryle i ser szwajcarski. Czego medycyna ratunkowa może Cię nauczyć o tworzen...
 
Have you ever wondered about code review?
Have you ever wondered about code review?Have you ever wondered about code review?
Have you ever wondered about code review?
 
Reactive Integration with Akka Streams and Alpakka
Reactive Integration with Akka Streams and AlpakkaReactive Integration with Akka Streams and Alpakka
Reactive Integration with Akka Streams and Alpakka
 
W świecie botów czyli po co nam SI
W świecie botów czyli po co nam SIW świecie botów czyli po co nam SI
W świecie botów czyli po co nam SI
 
Small intro to Big Data
Small intro to Big DataSmall intro to Big Data
Small intro to Big Data
 
Out-of-the-box Reactive Streams with Java 9
Out-of-the-box Reactive Streams with Java 9Out-of-the-box Reactive Streams with Java 9
Out-of-the-box Reactive Streams with Java 9
 
Hiring, Bots and Beer. (Hiring in the IT industry)
Hiring, Bots and Beer. (Hiring in the IT industry) Hiring, Bots and Beer. (Hiring in the IT industry)
Hiring, Bots and Beer. (Hiring in the IT industry)
 
Teal Is The New Black
Teal Is The New BlackTeal Is The New Black
Teal Is The New Black
 
Windowing data in big data streams
Windowing data in big data streamsWindowing data in big data streams
Windowing data in big data streams
 
Kafka as a message queue
Kafka as a message queueKafka as a message queue
Kafka as a message queue
 
Introduction to Cassandra
Introduction to CassandraIntroduction to Cassandra
Introduction to Cassandra
 
Cassandra - how to fail?
Cassandra - how to fail?Cassandra - how to fail?
Cassandra - how to fail?
 
How to manage in a flat organized, remote and transparent company
How to manage in a flat organized, remote and transparent companyHow to manage in a flat organized, remote and transparent company
How to manage in a flat organized, remote and transparent company
 
Performance tests with gatling
Performance tests with gatlingPerformance tests with gatling
Performance tests with gatling
 
Projekt z punktu widzenia UX designera
Projekt z punktu widzenia UX designeraProjekt z punktu widzenia UX designera
Projekt z punktu widzenia UX designera
 
Machine learning by example
Machine learning by exampleMachine learning by example
Machine learning by example
 
Open source big data landscape and possible ITS applications
Open source big data landscape and possible ITS applicationsOpen source big data landscape and possible ITS applications
Open source big data landscape and possible ITS applications
 
Jednorożce to kobiety a nie firmy. O √kobiecym w STEM
Jednorożce to kobiety a nie firmy. O √kobiecym w STEMJednorożce to kobiety a nie firmy. O √kobiecym w STEM
Jednorożce to kobiety a nie firmy. O √kobiecym w STEM
 

Kürzlich hochgeladen

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 

Kürzlich hochgeladen (20)

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 

Origins of Free

  • 1. THE ORIGINS OF FREE Adam Warski, SoftwareMill 27 May 2017, LambdaConf
  • 2. @adamwarski, SoftwareMill, LambdaConf 2017 THE PLAN ➤ Why bother? ➤ Universal algebra ➤ Free algebras ➤ The meaning of life free ➤ Monads & free monads ➤ Free monads in Haskell, Cats and Scalaz ➤ It’s simpler than it sounds
  • 3. @adamwarski, SoftwareMill, LambdaConf 2017 WHY BOTHER WITH FREE? ➤ Free monads: ➤ programs as values ➤ separate definition from interpretation ➤ testing ➤ composability ➤ use a high-level language ➤ hide low-level concerns
  • 4. @adamwarski, SoftwareMill, LambdaConf 2017 WHAT IS AN ALGEBRA? “algebra is the study of mathematical symbols and the rules for manipulating these symbols"
 Wikipedia, 2017 “the part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formulae and equations."
 Google Search, 2017 y = ax + b E = mc2 f(10◊x) = K(▸9) def sum(l: List[L]) = l.fold(_ + _) main = getCurrentTime >>= print
  • 5. @adamwarski, SoftwareMill, LambdaConf 2017 UNIVERSAL ALGEBRA: SIGNATURE ➤ Goal: Model programs as algebras ➤ Let’s generalise! ➤ Studies algebraic structures, rather than concrete models algebraic signature ⌃ = (S, ⌦) set S family ⌦ of sets indexed by S⇤ ⇥ S ➤ Syntax: ➤ type names: ➤ operation names:
  • 6. @adamwarski, SoftwareMill, LambdaConf 2017 UNIVERSAL ALGEBRA: SIGNATURE EXAMPLE toString(succ(0) + succ(succ(0))) S = {int, str} ⌦✏,int = {0} ⌦int,int = {succ} ⌦(int,int),int = {+} ⌦(str,str),str = {++} ⌦int,str = {toString}
  • 7. @adamwarski, SoftwareMill, LambdaConf 2017 UNIVERSAL ALGEBRA: SIGNATURE EXAMPLE S = {v} ⌦✏,v = {1} ⌦(v,v),v = {⇤} (1 ⇤ 1) ⇤ (1 ⇤ (1 ⇤ 1))
  • 8. @adamwarski, SoftwareMill, LambdaConf 2017 UNIVERSAL ALGEBRA: ALGEBRA ➤ A specific interpretation of the signature ➤ for each type, a set ➤ for each operation, a function between appropriate sets ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} We can define a ⌃-algebra A: |A|int = {0, 1, 2, ...} = N |A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...} succA = x.x + 1 +A = xy.x + y . . .
  • 9. @adamwarski, SoftwareMill, LambdaConf 2017 UNIVERSAL ALGEBRA: ALGEBRA ➤ As a side-note, F-algebras:
 
 
 
 
 
 are a generalisation of single-sorted algebras presented here type Algebra[F[_], A] = F[A] => A // or type Algebra f a = f a -> a
  • 10. @adamwarski, SoftwareMill, LambdaConf 2017 TERM ALGEBRA ➤ Can we build an algebra out of pure syntax? ➤ Expressions (terms) that can be built from the signature ➤ Rather boring, no interpretation at all |T⌃|int = {0, succ(0), succ(succ(0)), ..., 0 + 0, 0 + succ(0), ...} |T⌃|str = {toString(0), toString(succ(0)), ..., toString(0)++toString(0), ...} succT⌃ (t) = succ(t), e.g. succT⌃ (succ(0)) = succ(succ(0)) +T⌃ (t1, t2) = t1 + t2 ... ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} We define the term algebra T⌃:
  • 11. @adamwarski, SoftwareMill, LambdaConf 2017 TERM ALGEBRA ➤ Defined inductively ➤ base: all constants are terms ➤ step: any functions we can apply on previous terms ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} {0} {0, 0 + 0, succ(0), toString(0)} {0, 0 + 0, succ(0), 0 + succ(0), succ(0) + 0, succ(0) + succ(0), toString(0), toString(succ(0)), toString(0) + +toString(0)}
  • 12. @adamwarski, SoftwareMill, LambdaConf 2017 TERM ALGEBRA ⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤} 1, 1 ⇤ 1, (1 ⇤ 1) ⇤ 1, 1 ⇤ (1 ⇤ 1), 1 ⇤ ((1 ⇤ 1) ⇤ 1), ...
  • 13. @adamwarski, SoftwareMill, LambdaConf 2017 HOMOMORPHISM ➤ Homomorphism is a function between algebras ➤ For each type, functions between type interpretations ➤ Such that operations are preserved ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} When A and B are ⌃-algebras, f : A ! B is a homomorphism when: fint : |A|int ! |B|int fstr : |A|str ! |B|str 8x2|A|int fint(succA(x)) = succB(fint(x)) 8xy2|A|int f(x +A y) = f(x) +B f(y) 8x2|A|int fstr(toStringA(x)) = toStringB(fint(x))
  • 14. @adamwarski, SoftwareMill, LambdaConf 2017 INITIAL ALGEBRA Theorem 1 T⌃ is initial ⌃-algebra I is initial when for any other ⌃-algebra A there is exactly one homomorphism from I to A.
  • 15. @adamwarski, SoftwareMill, LambdaConf 2017 INITIAL ALGEBRA Theorem 1 T⌃ is initial ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} |A|int = {0, 1, 2, ...} = N |A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...} succA = x.x + 1 +A = xy.x + y . . . We can define a ⌃-algebra A: f : T⌃ ! A f(0T⌃ ) = 0A f(succT⌃ (t)) = succA(f(t)) ...
  • 16. @adamwarski, SoftwareMill, LambdaConf 2017 INITIAL ALGEBRA ➤ Only one way to interpret a term ➤ no junk: term algebra contains only what’s absolutely necessary ➤ no confusion: no two values are combined if they don’t need to be ➤ There’s only one initial algebra (up to isomorphism) ⌃-algebra I is initial when for any other ⌃-algebra A there is exactly one homomorphism between them. Theorem 1 T⌃ is initial
  • 17. @adamwarski, SoftwareMill, LambdaConf 2017 INITIAL ALGEBRA ➤ This algebra is definitely not initial: ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} ➤ Junk: strings “a”, “b”, … ➤ Confusion: 0+succ(0) is same as succ(0)+0 We can define a ⌃-algebra A: |A|int = {0, 1, 2, ...} = N |A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...}
  • 18. @adamwarski, SoftwareMill, LambdaConf 2017 TERMS WITH VARIABLES For any set X, T⌃(X) is the term algebra with X added as ”constants” (but called variables) ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} Xint = {i, j, k} Xstr = {s1, s2} succ(i) + j + succ(succ(k)) s1 + +toString(0) toString(succ(0) + k) + +s2
  • 19. @adamwarski, SoftwareMill, LambdaConf 2017 TERMS WITH VARIABLES For any set X, T⌃(X) is the term algebra with X added as ”constants” (but called variables) ⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤} Xv = {x, y, z, ...} 1 ⇤ x x ⇤ (y ⇤ z) (x ⇤ y) ⇤ z) (x ⇤ 1) ⇤ (y ⇤ (1 ⇤ 1))
  • 20. @adamwarski, SoftwareMill, LambdaConf 2017 FREE ALGEBRA ➤ An interpretation of the variables determines an interpretation of any term Theorem 1 For any variable set X, T⌃(X) is free For any set X, T⌃(X) is the term algebra with X added as ”constants” (but called variables) ⌃-algebra I is free over X (X ⇢ I) when for any other ⌃-algebra A, any function f : X ! |A| extends uniquely to a homomorphism f# : I ! A between them.
  • 21. @adamwarski, SoftwareMill, LambdaConf 2017 FREE ALGEBRA EXAMPLE ⌃ = (S, ⌦), S = {int, str} and ⌦ = {0, succ, +, ++, toString} Xint = {i, j, k} Xstr = {s1, s2} |A|int = N, |A|str = {”a”, ”aa”, ..., ”b”, ”ab”, ...} succA = x.x + 1 +A = xy.x + y . . . f : X ! |A| f(i) = 10, f(j) = 5, f(k) = 42 f(s1) = ”lambda”, f(s2) = ”conf” f# : T⌃(X) ! A f# (toString(succ(j) + succ(0)) + +s1) = ”7lambda” f# (s2 + +toString(k) + +s2) = ”lambda42conf”
  • 22. @adamwarski, SoftwareMill, LambdaConf 2017 MEANING OF FREE ➤ Free to interpret in any way ➤ no constraints ➤ Free of additional structure ➤ only what’s absolutely necessary ➤ No junk, no confusion
  • 23. @adamwarski, SoftwareMill, LambdaConf 2017 ALGEBRAS WITH EQUATIONS ➤ Let’s add constraints on interpretations of a signature ➤ that is, on the algebras ➤ And let’s focus only algebras satisfying a set of equations ⌃-equation: 8X.t = t0 X: variables, with sorts t, t0 2 T⌃(X): terms with these variables ➤ Equations:
  • 24. @adamwarski, SoftwareMill, LambdaConf 2017 ALGEBRAS WITH EQUATIONS ⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤} 8{x, y, z}x ⇤ (y ⇤ z) = (x ⇤ y) ⇤ z 8{x}x ⇤ 1 = x 8{x}1 ⇤ x = x :
  • 25. @adamwarski, SoftwareMill, LambdaConf 2017 TERM ALGEBRAS WITH EQUATIONS ➤ usually doesn’t satisfy the equations ➤ Some terms need to be “combined” ➤ Equivalence relation generated by ➤ Instead of terms, sets of terms ➤ equivalent wrt equations from T⌃(X) : ⌘
  • 26. @adamwarski, SoftwareMill, LambdaConf 2017 TERM ALGEBRAS WITH EQUATIONS ⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤} 8{x, y, z}x ⇤ (y ⇤ z) = (x ⇤ y) ⇤ z 8{x}x ⇤ 1 = x 8{x}1 ⇤ x = x : T⌃(X)/ ⌘ : {1} {x, x ⇤ 1, 1 ⇤ x} {x ⇤ (y ⇤ z), (x ⇤ y) ⇤ z, (1 ⇤ x) ⇤ (y ⇤ z), 1 ⇤ (x ⇤ (y ⇤ z)), ...}
  • 27. @adamwarski, SoftwareMill, LambdaConf 2017 FREE ALGEBRAS WITH EQUATIONS Theorem 1 For any variable set X and equations , T⌃(X)/ ⌘ is free in the class of algebras satisfying ➤ Homomorphism to any other algebra ➤ how to interpret a set of terms? ➤ interpret any of them
  • 28. @adamwarski, SoftwareMill, LambdaConf 2017 FREE ALGEBRAS WITH EQUATIONS ⌃ = (S, ⌦), S = {v} and ⌦ = {1, ⇤} 8{x, y, z}x ⇤ (y ⇤ z) = (x ⇤ y) ⇤ z 8{x}x ⇤ 1 = x 8{x}1 ⇤ x = x : T⌃(X)/ ⌘ : {1} {x, x ⇤ 1, 1 ⇤ x} {x ⇤ (y ⇤ z), (x ⇤ y) ⇤ z, (1 ⇤ x) ⇤ (y ⇤ z), 1 ⇤ (x ⇤ (y ⇤ z)), ...} ➤ Is there a simpler representation? [] [x] [x, y, z]
  • 29. @adamwarski, SoftwareMill, LambdaConf 2017 FREE RECAP ➤ Algebraic signature: ➤ All possible interpretations: algebras ➤ For any variable set ➤ The term algebra is free ➤ any interpretation of the variables ➤ determines an interpretation of any term ➤ A general construction ⌃ = (S, ⌦) X T⌃(X) f : X ! |A| f# : T⌃(X) ! A
  • 30. @adamwarski, SoftwareMill, LambdaConf 2017 MODELLING SEQUENTIAL PROGRAMS: MONADS ➤ A sequential program can: ➤ return a value (pure) ➤ compute what to do next basing on previous result (flatMap) ➤ People decided to call an object with such operations a Monad ➤ Hence, we’ll use Monads to represent programs as data ➤ + sanity laws
  • 31. @adamwarski, SoftwareMill, LambdaConf 2017 FREE MONAD ➤ Signature ~ pure + flatMap ➤ Variables ~ operations (our DSL) ➤ Free Monad ~ terms built out of pure, flatMap, our DSL ➤ modulo monad laws! ➤ e.g. flatMap(pure(x), f) = f(x) Interpretation of the DSL determines the interpretation of the whole program
  • 32. @adamwarski, SoftwareMill, LambdaConf 2017 FREE IN CATS/SCALAZ trait Free[F[_], A]
 
 object Free {
 case class Pure[F[_], A](a: A) extends Free[F, A]
 case class Suspend[F[_], A](a: F[A]) extends Free[F, A]
 case class FlatMapped[F[_], B, C](
 c: Free[F, C], f: C => Free[F, B]) extends Free[F, B]
 }
  • 33. @adamwarski, SoftwareMill, LambdaConf 2017 FREE IN HASKELL data Free f r = Free (f (Free f r)) | Pure r trait Free[F[_], A]
 
 object Free {
 case class Pure[F[_], A](a: A) extends Free[F, A]
 case class Join[F[_], A](f: F[Free[F, A]]) extends Free[S, A]
 } f/F[_] must be a functor!
  • 34. @adamwarski, SoftwareMill, LambdaConf 2017 SUMMING UP ➤ Direct construction of free algebras ➤ Hand-wavy construction of free monad ➤ Free ➤ free to interpret in any way ➤ free of constraints ➤ no junk, no confusion ➤ Free in Haskell is the same free as in Scala
  • 35. @adamwarski, SoftwareMill, LambdaConf 2017 ABOUT ME ➤ Software Engineer, co-founder @ ➤ Custom software development; Scala/Java/Kafka/Cassandra/… ➤ Open-source: Quicklens, MacWire, ElasticMQ, ScalaClippy, … ➤ Long time ago: student of Category Theory
  • 36. @adamwarski, SoftwareMill, LambdaConf 2017 FURTHER READING ➤ “Foundations of Algebraic Specification and Formal Software Development” by Donald Sannella and Andrzej Tarlecki ➤ The Internet