SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
Truth, Deduction,
Computation
Lecture 4
Monoids
Vlad Patryshev
SCU
2013
See common pattern?
●

2+3==5; 0+n==n+0==n

●

6*7==42; 1*n==n*1==n

●

max(10, 100) == 100; max(Int.MinValue, x) == x

●

"ab"+"cd"=="abcd"; ""+"ef"=="ef"; "gh"+""=="gh"

●

List(1,2)+List(3,4)==List(1,2,3,4); Nil+List(5,6)==List
(5,6); List(7,8)+Nil==List(7,8)

●

Set(1,2,3)+Set(2,4)==Set(1,2,3,4); Set.Empty+Set(5,6)
==Set(5,6)
You see Monoids!
●

2+3==5; 0+n==n+0==n

●

6*7==42; 1*n==n*1==n

●

max(10, 100) == 100; max(Int.MinValue, x) == x

●

"ab"+"cd"=="abcd"; ""+"ef"=="ef"; "gh"+""=="gh"

●

List(1,2)+List(3,4)==List(1,2,3,4); Nil+List(5,6)==List
(5,6); List(7,8)++Nil==List(7,8)

●

Set(1,2,3)+Set(2,4)==Set(1,2,3,4); Set.Empty+Set(5,6)
==Set(5,6)
Monoid Rules
1)

a Op b = c

2) Zero Op a = a Op Zero = a
signatures:
Op: (T, T) => T
Zero: () => T // or rather just T

but wait, there's more!
Monoid Rules - associativity
(a Op b) Op c == a Op (b Op c)
Not everything is associative
5-(3-2) != (5-3)-2
avg(10, avg(30, 50)) != avg(avg(10, 30), 50)
Magma - like monoid, but no associativity and no unit
●

Tuples: (a, (b, c)) is not the same as ((a,b), c)

●

Binary trees:
Monoid Rules - associativity
(a1 Op (a2 Op (a3 Op (a4...))))
aka fold
(Zero /: a) (x Op y)
Can regroup and run in parallel: thanks to associativity!

This is what empowers Map/Reduce
Mappings between Monoids
f: A → B
f(ZeroA) = ZeroB
f(x OpA y) = f(x) OpB f(y)
e.g.
●
●
●
●
●
●

log(1)=0; log(a*b)=log(a)+log(b)
twice(n)=2*n; twice(0)=0, twice(n+m)=twice(n)+twice(m)
length("")=0; length(s1+s2)=length(s1)+length(s2)
sum(Nil)=0; sum(list1+list2)=sum(list1)+sum(list2)
prod(Nil)=1; prod(list1+list2)=prod(list1)*prod(list2)
maxOf(Nil)=Int.MinValue; maxOf(list1+list2)=max(maxOf
(list1)),maxOf(list2))
Free Monoid
any type

a monoid

take any function, f: A → B
this is equivalent to specifying...
f': List[A] → B
where
f'(Nil) = ZeroB
f'(list1 + list2) = f'(list1) OpB f'(list2)
List[A] is a Free Monoid on A
Free Monoid - example
Suppose we have...
object WeekDay extends Enumeration {
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
and a function
WeekDay=>Int = (Mon->1, Tue->1, Wed->1, Thu->1, Fri->1,
Sat->0, Sun->0)
If we have a monoid (Int, 0, +), we can automatically extend this
mapping to List[WeekDay]->Int
which counts the number of working days.
Reduce
reduce: List[A] → A
with properties:

●
●

reduce(Nil) = ZeroA
reduce(list1+list2) = reduce(list1) OpA reduce(list2)

(This defines an algebra over the functor List[_].)
This is exactly what we did on the previous page, reduce.
That's almost it
Monoids are simple, are not they?
Let's throw in more properties...
Commutative Monoids
a Op b == b Op a
e.g.

●
●

2+3==3+2
max(2.72, 3.14) == max(3.14, 2.72)

but not these:

●
●

"hello" + "world" != "world" + "hello"
List(1,2,3)+List(4,5,6) != List(4,5,6)+List(1,2,3)
Commutative Monoids
How can we create one?
e.g.
"abracadabra" -> "aaaaabbcdrr"
Order does not matter; numbers matter.
Free commutative monoid on A: a collection of possibly duplicates, order
does not matter.
It is called Bag[A]
Commutative Monoids
Bag[A]
<script language=”javascript”>
var bag =
{
"partridges in a pear tree": 1,
"turtle doves": 2,
"french hens": 3,
"calling birds": 4,
"golden rings": 5
//etc
}
</script>
Commutative Monoids
How about sets?!
Commutative Monoids...
How about sets?!
x Op x == x

x is idempotent

Set('a','b','c') + Set('b','c','d') = Set('a','b','c','d')
Commutative Monoids...
How about sets?!
x Op x == x : x is idempotent
Set('a','b','c') + Set('b','c','d') = Set('a','b','c','d')

another example

●
●

max(x, x) = x
min(x, x) = x
Big Picture

The Whole Picture

(source: Alexander Bunkenburg, "The Boom Hierarchy")
Questions?

http://vpatryshev.blogspot.com/2009/06/why-monoids.html (with Java examples)
http://blog.safaribooksonline.com/2013/05/15/monoids-for-programmers-a-scala-example/

Mais conteúdo relacionado

Mais procurados

Integral table
Integral tableIntegral table
Integral tableAnkitcos0
 
Tugas matematika menemukan konsep persamaan kuadrat
Tugas matematika menemukan konsep persamaan kuadratTugas matematika menemukan konsep persamaan kuadrat
Tugas matematika menemukan konsep persamaan kuadrattrisnasariasih
 
Legendre's eqaution
Legendre's eqautionLegendre's eqaution
Legendre's eqautionSachin Patel
 
Etude Des Proprietes Physicochimiques Et Caracterisation Dune Argile Locale
Etude Des Proprietes Physicochimiques Et Caracterisation Dune Argile LocaleEtude Des Proprietes Physicochimiques Et Caracterisation Dune Argile Locale
Etude Des Proprietes Physicochimiques Et Caracterisation Dune Argile LocaleRaouf Alsaytara
 
Zadaci iz matisha
Zadaci iz matisha Zadaci iz matisha
Zadaci iz matisha titoyebac
 
Bsc maths derivative_formula
Bsc maths derivative_formulaBsc maths derivative_formula
Bsc maths derivative_formulaShani Qasmi
 
Jordan's solution
Jordan's solutionJordan's solution
Jordan's solutionsabsma
 

Mais procurados (14)

Ch13 20
Ch13 20Ch13 20
Ch13 20
 
Integral table
Integral tableIntegral table
Integral table
 
Ch03 12
Ch03 12Ch03 12
Ch03 12
 
Tugas matematika menemukan konsep persamaan kuadrat
Tugas matematika menemukan konsep persamaan kuadratTugas matematika menemukan konsep persamaan kuadrat
Tugas matematika menemukan konsep persamaan kuadrat
 
Legendre's eqaution
Legendre's eqautionLegendre's eqaution
Legendre's eqaution
 
MS2 POwer Rules
MS2 POwer RulesMS2 POwer Rules
MS2 POwer Rules
 
Integral table
Integral tableIntegral table
Integral table
 
Etude Des Proprietes Physicochimiques Et Caracterisation Dune Argile Locale
Etude Des Proprietes Physicochimiques Et Caracterisation Dune Argile LocaleEtude Des Proprietes Physicochimiques Et Caracterisation Dune Argile Locale
Etude Des Proprietes Physicochimiques Et Caracterisation Dune Argile Locale
 
Integrales
Integrales Integrales
Integrales
 
Intro To Agda
Intro To AgdaIntro To Agda
Intro To Agda
 
Zadaci iz matisha
Zadaci iz matisha Zadaci iz matisha
Zadaci iz matisha
 
Divisor de 7
Divisor de 7Divisor de 7
Divisor de 7
 
Bsc maths derivative_formula
Bsc maths derivative_formulaBsc maths derivative_formula
Bsc maths derivative_formula
 
Jordan's solution
Jordan's solutionJordan's solution
Jordan's solution
 

Destaque

Ka rin carol wedemeyer
Ka rin   carol wedemeyerKa rin   carol wedemeyer
Ka rin carol wedemeyerVlad Patryshev
 
Truth, deduction, computation; lecture 3
Truth, deduction, computation;  lecture 3Truth, deduction, computation;  lecture 3
Truth, deduction, computation; lecture 3Vlad Patryshev
 
Truth, deduction, computation lecture i (last one)
Truth, deduction, computation   lecture i (last one)Truth, deduction, computation   lecture i (last one)
Truth, deduction, computation lecture i (last one)Vlad Patryshev
 
Josi's ceramics thingy
Josi's ceramics thingyJosi's ceramics thingy
Josi's ceramics thingyVlad Patryshev
 

Destaque (7)

Gao
GaoGao
Gao
 
Ka rin carol wedemeyer
Ka rin   carol wedemeyerKa rin   carol wedemeyer
Ka rin carol wedemeyer
 
Truth, deduction, computation; lecture 3
Truth, deduction, computation;  lecture 3Truth, deduction, computation;  lecture 3
Truth, deduction, computation; lecture 3
 
Truth, deduction, computation lecture i (last one)
Truth, deduction, computation   lecture i (last one)Truth, deduction, computation   lecture i (last one)
Truth, deduction, computation lecture i (last one)
 
Bill & june vaughn
Bill & june vaughnBill & june vaughn
Bill & june vaughn
 
Bains
BainsBains
Bains
 
Josi's ceramics thingy
Josi's ceramics thingyJosi's ceramics thingy
Josi's ceramics thingy
 

Semelhante a Truth, deduction, computation; lecture 4

Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-stdPaul Phillips
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh Singh
 
Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...
Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...
Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...Facultad de Informática UCM
 
Brief intro to clojure
Brief intro to clojureBrief intro to clojure
Brief intro to clojureRoy Rutto
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in ScalaTim Dalton
 
Top school in delhi ncr
Top school in delhi ncrTop school in delhi ncr
Top school in delhi ncrEdhole.com
 
Bai tap-prolog-da-tap-hop-9889
Bai tap-prolog-da-tap-hop-9889Bai tap-prolog-da-tap-hop-9889
Bai tap-prolog-da-tap-hop-9889anhsaobang1289
 
Lp presentations fnctions
Lp presentations fnctionsLp presentations fnctions
Lp presentations fnctionsrodylie
 
Functions ppt Dr Frost Maths Mixed questions
Functions ppt Dr Frost Maths Mixed questionsFunctions ppt Dr Frost Maths Mixed questions
Functions ppt Dr Frost Maths Mixed questionsgcutbill
 
Send + More = Money – Let’s mash 2 monads to solve a simple CSP
Send + More = Money – Let’s mash 2 monads to solve a simple CSPSend + More = Money – Let’s mash 2 monads to solve a simple CSP
Send + More = Money – Let’s mash 2 monads to solve a simple CSPFilippo Vitale
 
Real number system full
Real  number  system fullReal  number  system full
Real number system fullAon Narinchoti
 

Semelhante a Truth, deduction, computation; lecture 4 (20)

Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Number theory lecture (part 1)
Number theory lecture (part 1)Number theory lecture (part 1)
Number theory lecture (part 1)
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-std
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Hw5sols
Hw5solsHw5sols
Hw5sols
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...
Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...
Mi ordenador, de mayor, quiere ser cuántico (curso acelerado de simulación ...
 
Brief intro to clojure
Brief intro to clojureBrief intro to clojure
Brief intro to clojure
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in Scala
 
Python overview
Python overviewPython overview
Python overview
 
Top school in delhi ncr
Top school in delhi ncrTop school in delhi ncr
Top school in delhi ncr
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
Bai tap-prolog-da-tap-hop-9889
Bai tap-prolog-da-tap-hop-9889Bai tap-prolog-da-tap-hop-9889
Bai tap-prolog-da-tap-hop-9889
 
Lp presentations fnctions
Lp presentations fnctionsLp presentations fnctions
Lp presentations fnctions
 
Functions ppt Dr Frost Maths Mixed questions
Functions ppt Dr Frost Maths Mixed questionsFunctions ppt Dr Frost Maths Mixed questions
Functions ppt Dr Frost Maths Mixed questions
 
Send + More = Money – Let’s mash 2 monads to solve a simple CSP
Send + More = Money – Let’s mash 2 monads to solve a simple CSPSend + More = Money – Let’s mash 2 monads to solve a simple CSP
Send + More = Money – Let’s mash 2 monads to solve a simple CSP
 
FUNCTIONS L.1.pdf
FUNCTIONS L.1.pdfFUNCTIONS L.1.pdf
FUNCTIONS L.1.pdf
 
C programs
C programsC programs
C programs
 
Real number system full
Real  number  system fullReal  number  system full
Real number system full
 

Mais de Vlad Patryshev

Formal methods 8 - category theory (last one)
Formal methods   8 - category theory (last one)Formal methods   8 - category theory (last one)
Formal methods 8 - category theory (last one)Vlad Patryshev
 
Formal methods 6 - elements of algebra
Formal methods   6 - elements of algebraFormal methods   6 - elements of algebra
Formal methods 6 - elements of algebraVlad Patryshev
 
Formal methods 5 - Pi calculus
Formal methods   5 - Pi calculusFormal methods   5 - Pi calculus
Formal methods 5 - Pi calculusVlad Patryshev
 
Formal methods 4 - Z notation
Formal methods   4 - Z notationFormal methods   4 - Z notation
Formal methods 4 - Z notationVlad Patryshev
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machinesVlad Patryshev
 
Formal methods 2 - languages and machines
Formal methods   2 - languages and machinesFormal methods   2 - languages and machines
Formal methods 2 - languages and machinesVlad Patryshev
 
Formal methods 1 - introduction
Formal methods   1 - introductionFormal methods   1 - introduction
Formal methods 1 - introductionVlad Patryshev
 
Formal methods 7 - category theory
Formal methods   7 - category theoryFormal methods   7 - category theory
Formal methods 7 - category theoryVlad Patryshev
 
Truth, deduction, computation lecture h
Truth, deduction, computation   lecture hTruth, deduction, computation   lecture h
Truth, deduction, computation lecture hVlad Patryshev
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture gVlad Patryshev
 
Truth, deduction, computation lecture f
Truth, deduction, computation   lecture fTruth, deduction, computation   lecture f
Truth, deduction, computation lecture fVlad Patryshev
 
Truth, deduction, computation lecture e
Truth, deduction, computation   lecture eTruth, deduction, computation   lecture e
Truth, deduction, computation lecture eVlad Patryshev
 
Truth, deduction, computation lecture d
Truth, deduction, computation   lecture dTruth, deduction, computation   lecture d
Truth, deduction, computation lecture dVlad Patryshev
 
Truth, deduction, computation lecture c
Truth, deduction, computation   lecture cTruth, deduction, computation   lecture c
Truth, deduction, computation lecture cVlad Patryshev
 
Truth, deduction, computation lecture b
Truth, deduction, computation   lecture bTruth, deduction, computation   lecture b
Truth, deduction, computation lecture bVlad Patryshev
 
Truth, deduction, computation lecture a
Truth, deduction, computation   lecture aTruth, deduction, computation   lecture a
Truth, deduction, computation lecture aVlad Patryshev
 
Truth, deduction, computation lecture 9
Truth, deduction, computation   lecture 9Truth, deduction, computation   lecture 9
Truth, deduction, computation lecture 9Vlad Patryshev
 
Truth, deduction, computation lecture 8
Truth, deduction, computation   lecture 8Truth, deduction, computation   lecture 8
Truth, deduction, computation lecture 8Vlad Patryshev
 
Truth, deduction, computation lecture 7
Truth, deduction, computation   lecture 7Truth, deduction, computation   lecture 7
Truth, deduction, computation lecture 7Vlad Patryshev
 
Truth, deduction, computation lecture 6
Truth, deduction, computation   lecture 6Truth, deduction, computation   lecture 6
Truth, deduction, computation lecture 6Vlad Patryshev
 

Mais de Vlad Patryshev (20)

Formal methods 8 - category theory (last one)
Formal methods   8 - category theory (last one)Formal methods   8 - category theory (last one)
Formal methods 8 - category theory (last one)
 
Formal methods 6 - elements of algebra
Formal methods   6 - elements of algebraFormal methods   6 - elements of algebra
Formal methods 6 - elements of algebra
 
Formal methods 5 - Pi calculus
Formal methods   5 - Pi calculusFormal methods   5 - Pi calculus
Formal methods 5 - Pi calculus
 
Formal methods 4 - Z notation
Formal methods   4 - Z notationFormal methods   4 - Z notation
Formal methods 4 - Z notation
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
Formal methods 2 - languages and machines
Formal methods   2 - languages and machinesFormal methods   2 - languages and machines
Formal methods 2 - languages and machines
 
Formal methods 1 - introduction
Formal methods   1 - introductionFormal methods   1 - introduction
Formal methods 1 - introduction
 
Formal methods 7 - category theory
Formal methods   7 - category theoryFormal methods   7 - category theory
Formal methods 7 - category theory
 
Truth, deduction, computation lecture h
Truth, deduction, computation   lecture hTruth, deduction, computation   lecture h
Truth, deduction, computation lecture h
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
Truth, deduction, computation lecture f
Truth, deduction, computation   lecture fTruth, deduction, computation   lecture f
Truth, deduction, computation lecture f
 
Truth, deduction, computation lecture e
Truth, deduction, computation   lecture eTruth, deduction, computation   lecture e
Truth, deduction, computation lecture e
 
Truth, deduction, computation lecture d
Truth, deduction, computation   lecture dTruth, deduction, computation   lecture d
Truth, deduction, computation lecture d
 
Truth, deduction, computation lecture c
Truth, deduction, computation   lecture cTruth, deduction, computation   lecture c
Truth, deduction, computation lecture c
 
Truth, deduction, computation lecture b
Truth, deduction, computation   lecture bTruth, deduction, computation   lecture b
Truth, deduction, computation lecture b
 
Truth, deduction, computation lecture a
Truth, deduction, computation   lecture aTruth, deduction, computation   lecture a
Truth, deduction, computation lecture a
 
Truth, deduction, computation lecture 9
Truth, deduction, computation   lecture 9Truth, deduction, computation   lecture 9
Truth, deduction, computation lecture 9
 
Truth, deduction, computation lecture 8
Truth, deduction, computation   lecture 8Truth, deduction, computation   lecture 8
Truth, deduction, computation lecture 8
 
Truth, deduction, computation lecture 7
Truth, deduction, computation   lecture 7Truth, deduction, computation   lecture 7
Truth, deduction, computation lecture 7
 
Truth, deduction, computation lecture 6
Truth, deduction, computation   lecture 6Truth, deduction, computation   lecture 6
Truth, deduction, computation lecture 6
 

Último

ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 

Último (20)

ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 

Truth, deduction, computation; lecture 4

  • 2. See common pattern? ● 2+3==5; 0+n==n+0==n ● 6*7==42; 1*n==n*1==n ● max(10, 100) == 100; max(Int.MinValue, x) == x ● "ab"+"cd"=="abcd"; ""+"ef"=="ef"; "gh"+""=="gh" ● List(1,2)+List(3,4)==List(1,2,3,4); Nil+List(5,6)==List (5,6); List(7,8)+Nil==List(7,8) ● Set(1,2,3)+Set(2,4)==Set(1,2,3,4); Set.Empty+Set(5,6) ==Set(5,6)
  • 3. You see Monoids! ● 2+3==5; 0+n==n+0==n ● 6*7==42; 1*n==n*1==n ● max(10, 100) == 100; max(Int.MinValue, x) == x ● "ab"+"cd"=="abcd"; ""+"ef"=="ef"; "gh"+""=="gh" ● List(1,2)+List(3,4)==List(1,2,3,4); Nil+List(5,6)==List (5,6); List(7,8)++Nil==List(7,8) ● Set(1,2,3)+Set(2,4)==Set(1,2,3,4); Set.Empty+Set(5,6) ==Set(5,6)
  • 4. Monoid Rules 1) a Op b = c 2) Zero Op a = a Op Zero = a signatures: Op: (T, T) => T Zero: () => T // or rather just T but wait, there's more!
  • 5. Monoid Rules - associativity (a Op b) Op c == a Op (b Op c) Not everything is associative 5-(3-2) != (5-3)-2 avg(10, avg(30, 50)) != avg(avg(10, 30), 50) Magma - like monoid, but no associativity and no unit ● Tuples: (a, (b, c)) is not the same as ((a,b), c) ● Binary trees:
  • 6. Monoid Rules - associativity (a1 Op (a2 Op (a3 Op (a4...)))) aka fold (Zero /: a) (x Op y) Can regroup and run in parallel: thanks to associativity! This is what empowers Map/Reduce
  • 7. Mappings between Monoids f: A → B f(ZeroA) = ZeroB f(x OpA y) = f(x) OpB f(y) e.g. ● ● ● ● ● ● log(1)=0; log(a*b)=log(a)+log(b) twice(n)=2*n; twice(0)=0, twice(n+m)=twice(n)+twice(m) length("")=0; length(s1+s2)=length(s1)+length(s2) sum(Nil)=0; sum(list1+list2)=sum(list1)+sum(list2) prod(Nil)=1; prod(list1+list2)=prod(list1)*prod(list2) maxOf(Nil)=Int.MinValue; maxOf(list1+list2)=max(maxOf (list1)),maxOf(list2))
  • 8. Free Monoid any type a monoid take any function, f: A → B this is equivalent to specifying... f': List[A] → B where f'(Nil) = ZeroB f'(list1 + list2) = f'(list1) OpB f'(list2) List[A] is a Free Monoid on A
  • 9. Free Monoid - example Suppose we have... object WeekDay extends Enumeration { val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value } and a function WeekDay=>Int = (Mon->1, Tue->1, Wed->1, Thu->1, Fri->1, Sat->0, Sun->0) If we have a monoid (Int, 0, +), we can automatically extend this mapping to List[WeekDay]->Int which counts the number of working days.
  • 10. Reduce reduce: List[A] → A with properties: ● ● reduce(Nil) = ZeroA reduce(list1+list2) = reduce(list1) OpA reduce(list2) (This defines an algebra over the functor List[_].) This is exactly what we did on the previous page, reduce.
  • 11. That's almost it Monoids are simple, are not they? Let's throw in more properties...
  • 12. Commutative Monoids a Op b == b Op a e.g. ● ● 2+3==3+2 max(2.72, 3.14) == max(3.14, 2.72) but not these: ● ● "hello" + "world" != "world" + "hello" List(1,2,3)+List(4,5,6) != List(4,5,6)+List(1,2,3)
  • 13. Commutative Monoids How can we create one? e.g. "abracadabra" -> "aaaaabbcdrr" Order does not matter; numbers matter. Free commutative monoid on A: a collection of possibly duplicates, order does not matter. It is called Bag[A]
  • 14. Commutative Monoids Bag[A] <script language=”javascript”> var bag = { "partridges in a pear tree": 1, "turtle doves": 2, "french hens": 3, "calling birds": 4, "golden rings": 5 //etc } </script>
  • 16. Commutative Monoids... How about sets?! x Op x == x x is idempotent Set('a','b','c') + Set('b','c','d') = Set('a','b','c','d')
  • 17. Commutative Monoids... How about sets?! x Op x == x : x is idempotent Set('a','b','c') + Set('b','c','d') = Set('a','b','c','d') another example ● ● max(x, x) = x min(x, x) = x
  • 18. Big Picture The Whole Picture (source: Alexander Bunkenburg, "The Boom Hierarchy")
  • 19. Questions? http://vpatryshev.blogspot.com/2009/06/why-monoids.html (with Java examples) http://blog.safaribooksonline.com/2013/05/15/monoids-for-programmers-a-scala-example/