![Page 1: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/1.jpg)
Lists and Other Monoids Tom Schrijvers
Leuven Haskell User Group
![Page 2: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/2.jpg)
RecursionSchemes
GADTs
Type Families
Monads
Effect Handlers
Data Genericity
Expression Problem
Free Theorems
Lists and Other
Monoids
DSLs
Type Classes
…
Data Genericity
![Page 3: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/3.jpg)
RecursionSchemes
GADTs
Type Families
Monads
Effect Handlers
Data Genericity
Expression Problem
Free Theorems
Lists and Other
Monoids
DSLs
Type Classes
…
Data Genericity
Lists and Other
Monoids
![Page 4: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/4.jpg)
Introduction to Monoids
![Page 5: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/5.jpg)
Abstract Patterns
![Page 6: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/6.jpg)
Abstract Patterns
Math
![Page 7: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/7.jpg)
Haskell’s Math Inspiration
![Page 8: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/8.jpg)
Haskell’s Math Inspiration
Algebra
![Page 9: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/9.jpg)
Haskell’s Math Inspiration
Algebra
Abstract
![Page 10: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/10.jpg)
Haskell’s Math Inspiration
Algebra
Abstract Universal
![Page 11: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/11.jpg)
Haskell’s Math Inspiration
Algebra CategoryTheory
Abstract Universal
![Page 12: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/12.jpg)
Algebra CategoryTheory
![Page 13: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/13.jpg)
Algebra CategoryTheory
regular Haskell monoids
![Page 14: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/14.jpg)
Algebra CategoryTheory
regular Haskell monoids
monoids in the category of
endofunctors
![Page 15: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/15.jpg)
Algebra CategoryTheory
regular Haskell monoids
monoids in the category of
endofunctors
Haskell monads and
applicative functors
![Page 16: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/16.jpg)
The Monoid Structure
4 ingredients
![Page 17: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/17.jpg)
The Monoid Structure
4 ingredients
A set M
![Page 18: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/18.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
A set M
![Page 19: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/19.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
![Page 20: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/20.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
![Page 21: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/21.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
ℤ = {…,-1,0,1,…}
![Page 22: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/22.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
ℤ = {…,-1,0,1,…} + : ℤ × ℤ → ℤ
![Page 23: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/23.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
ℤ = {…,-1,0,1,…} + : ℤ × ℤ → ℤ
0 ∈ ℤ
![Page 24: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/24.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
ℤ = {…,-1,0,1,…} + : ℤ × ℤ → ℤ
0 ∈ ℤ
Left Unit xε ⨂ x =
![Page 25: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/25.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
ℤ = {…,-1,0,1,…} + : ℤ × ℤ → ℤ
0 ∈ ℤ Right Unit x ⨂ ε x=
Left Unit xε ⨂ x =
![Page 26: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/26.jpg)
The Monoid Structure
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
ℤ = {…,-1,0,1,…} + : ℤ × ℤ → ℤ
0 ∈ ℤAssociativity x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
Right Unit x ⨂ ε x=
Left Unit xε ⨂ x =
![Page 27: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/27.jpg)
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
![Page 28: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/28.jpg)
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
![Page 29: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/29.jpg)
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
Int
![Page 30: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/30.jpg)
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
A function ⨂ :: M → M → M
Int
![Page 31: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/31.jpg)
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
A function ⨂ :: M → M → M
Int (+) :: Int -> Int -> Int
![Page 32: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/32.jpg)
An element ε :: M
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
A function ⨂ :: M → M → M
Int (+) :: Int -> Int -> Int
![Page 33: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/33.jpg)
An element ε :: M
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
A function ⨂ :: M → M → M
Int (+) :: Int -> Int -> Int
0 :: Int
![Page 34: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/34.jpg)
An element ε :: M
Haskell Monoids
4 ingredients
A binary operator ⨂ : M × M → M
An element ε ∈ M
A set M
3 Properties
Left Unit
Right Unit
Associativity
x
x ⨂ ε
x ⨂ (y ⨂ z) (x ⨂ y) ⨂ z=
x
ε ⨂ x
=
=
A type M
A function ⨂ :: M → M → M
3 Properties
Int (+) :: Int -> Int -> Int
0 :: Int
![Page 35: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/35.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
![Page 36: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/36.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
A type M
![Page 37: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/37.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
A type M
![Page 38: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/38.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
![Page 39: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/39.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
![Page 40: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/40.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
A function ⨂ :: M → M → M
![Page 41: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/41.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
A function ⨂ :: M → M → M
![Page 42: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/42.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
A function ⨂ :: M → M → M
3 Properties
![Page 43: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/43.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
A function ⨂ :: M → M → M
3 Propertiesnot in the Haskell
language
😕
![Page 44: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/44.jpg)
Monoid Type Class
class Monoid m where mempty :: m mappend :: m -> m -> m
(<>) = mappend
Data.Monoid
An element ε :: M
A type M
A function ⨂ :: M → M → M
3 Properties
convenient binary operator
![Page 45: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/45.jpg)
Monoid Instanceclass Monoid m where mempty :: m mappend :: m -> m -> m
instance Monoid Int where mempty = 0 mappend = (+)
![Page 46: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/46.jpg)
Monoid for Bool?
class Monoid m where mempty :: m mappend :: m -> m -> m
instance Monoid Bool where mempty = ??? mappend = ???
![Page 47: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/47.jpg)
Two Possible Instances
instance Monoid Bool where mempty = True mappend = (&&) instance Monoid Bool where mempty = False mappend = (||)
![Page 48: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/48.jpg)
Two Possible Instances
instance Monoid Bool where mempty = True mappend = (&&) instance Monoid Bool where mempty = False mappend = (||)
![Page 49: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/49.jpg)
Two Possible Instances
instance Monoid Bool where mempty = True mappend = (&&) instance Monoid Bool where mempty = False mappend = (||)
![Page 50: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/50.jpg)
Two Possible Instances
instance Monoid Bool where mempty = True mappend = (&&) instance Monoid Bool where mempty = False mappend = (||)
Can’t have two instances for the
same type!!!
![Page 51: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/51.jpg)
Data.Monoid
What's in a name? that which we call a BoolBy any other name would smell as sweet;
![Page 52: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/52.jpg)
Newtype to the Rescue!Data.Monoid
What's in a name? that which we call a BoolBy any other name would smell as sweet;
![Page 53: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/53.jpg)
Newtype to the Rescue!newtype All = All { getAll :: Bool }
instance Monoid All where mempty = All True All x `mappend` All y = All (x && y) newtype Any = Any { getAny :: Bool } instance Monoid Any where mempty = Any False Any x `mappend` Any y = Any (x || y)
Data.Monoid
![Page 54: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/54.jpg)
Newtype to the Rescue!newtype All = All { getAll :: Bool }
instance Monoid All where mempty = All True All x `mappend` All y = All (x && y) newtype Any = Any { getAny :: Bool } instance Monoid Any where mempty = Any False Any x `mappend` Any y = Any (x || y)
Data.Monoid
Both isomorphic to Bool
![Page 55: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/55.jpg)
Newtype to the Rescue!newtype All = All { getAll :: Bool }
instance Monoid All where mempty = All True All x `mappend` All y = All (x && y) newtype Any = Any { getAny :: Bool } instance Monoid Any where mempty = Any False Any x `mappend` Any y = Any (x || y)
Data.Monoid
![Page 56: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/56.jpg)
Newtype to the Rescue!newtype All = All { getAll :: Bool }
instance Monoid All where mempty = All True All x `mappend` All y = All (x && y) newtype Any = Any { getAny :: Bool } instance Monoid Any where mempty = Any False Any x `mappend` Any y = Any (x || y)
Data.Monoid
![Page 57: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/57.jpg)
Newtype to the Rescue!newtype All = All { getAll :: Bool }
instance Monoid All where mempty = All True All x `mappend` All y = All (x && y) newtype Any = Any { getAny :: Bool } instance Monoid Any where mempty = Any False Any x `mappend` Any y = Any (x || y)
Data.Monoid
Two non-conflicting instances
![Page 58: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/58.jpg)
Same Problem for NumSum a
Product a
Data.Monoid
Num a => a
![Page 59: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/59.jpg)
Same Problem for NumSum a
Product a
HomeworkInvent 10 more monoid
structures for Int
Data.Monoid
Num a => a
![Page 60: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/60.jpg)
Applications
![Page 61: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/61.jpg)
The diagrams Package
Brent Yorgey
1
![Page 62: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/62.jpg)
The diagrams Package
Brent Yorgey
=<>
1
![Page 63: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/63.jpg)
The diagrams Package
Brent Yorgey
=<>
`atop`
1
![Page 64: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/64.jpg)
--enable-foo --disable-foo --enable-foo --baz=help
e.g., Command-Line Options:
Duncan Coutts
Compositional Settings2
![Page 65: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/65.jpg)
--enable-foo --disable-foo --enable-foo --baz=help
e.g., Command-Line Options:
Duncan Coutts
Compositional Settings
<>default setting
setting update <><> setting
update…
2
![Page 66: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/66.jpg)
Compositional Settingsdata ConfigFlags = ConfigFlags { foo :: Flag Bool, bar :: Flag PackageDB, baz :: [String] }
2
![Page 67: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/67.jpg)
Compositional Settingsdata ConfigFlags = ConfigFlags { foo :: Flag Bool, bar :: Flag PackageDB, baz :: [String] }
data Flag a = Flag a | Default
instance Monoid (Flag a) where mempty = Default _ `mappend` f@(Flag _) = f f `mappend` Default = f
right biased
2
![Page 68: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/68.jpg)
Compositional Settingsinstance Monoid ConfigFlags where mempty = ConfigFlags { foo = mempty , bar = mempty , baz = mempty } c1 `mappend` c2 = ConfigFlags { foo = foo c1 <> foo c2 , bar = bar c1 <> bar c2 , baz = baz c1 <> baz c2 }
2
![Page 69: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/69.jpg)
Data Aggregation3
Data.Foldable
class Foldable t where foldMap :: Monoid m => (a -> m) -> (t a -> m)
![Page 70: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/70.jpg)
Data Aggregation3
Data.Foldable
class Foldable t where foldMap :: Monoid m => (a -> m) -> (t a -> m)
polymorphic collection
![Page 71: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/71.jpg)
Data Aggregation3
Data.Foldable
toList :: Foldable t => t a -> [a]and :: Foldable t => t Bool -> Boolor :: Foldable t => t Bool -> Boolany :: Foldable t => (a -> Bool) -> t a -> Boolall :: Foldable t => (a -> Bool) -> t a -> Boolsum :: (Foldable t, Num a) => t a -> aproduct :: (Foldable t, Num a) => t a -> amaximum :: (Foldable t, Ord a) => t a -> aminimum :: (Foldable t, Ord a) => t a -> aelem :: (Foldable t, Eq a) => a -> t a -> Bool…
class Foldable t where foldMap :: Monoid m => (a -> m) -> (t a -> m)
polymorphic collection
![Page 72: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/72.jpg)
Data Aggregation3
data Tree a = Empty | Fork (Tree a) a (Tree a)
instance Foldable Tree where foldMap gen Empty = mempty foldMap gen (Fork l x r) = foldMap gen l <> gen x <> foldMap gen r
![Page 73: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/73.jpg)
Data Aggregation3
data Tree a = Empty | Fork (Tree a) a (Tree a)
instance Foldable Tree where foldMap gen Empty = mempty foldMap gen (Fork l x r) = foldMap gen l <> gen x <> foldMap gen r
> sum (Fork (Fork Empty 5 Empty) 3 Empty) 8 > maximum (Fork (Fork Empty 5 Empty) 3 Empty) 5
![Page 74: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/74.jpg)
Data.Foldable
Foldable/Traversable Proposal
3
toList :: Foldable t => t a -> [a]and :: Foldable t => t Bool -> Boolor :: Foldable t => t Bool -> Boolany :: Foldable t => (a -> Bool) -> t a -> Boolall :: Foldable t => (a -> Bool) -> t a -> Boolsum :: (Foldable t, Num a) => t a -> aproduct :: (Foldable t, Num a) => t a -> amaximum :: (Foldable t, Ord a) => t a -> aminimum :: (Foldable t, Ord a) => t a -> aelem :: (Foldable t, Eq a) => a -> t a -> Bool…
![Page 75: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/75.jpg)
Data.Foldable
Foldable/Traversable Proposal
3
toList :: Foldable t => t a -> [a]and :: Foldable t => t Bool -> Boolor :: Foldable t => t Bool -> Boolany :: Foldable t => (a -> Bool) -> t a -> Boolall :: Foldable t => (a -> Bool) -> t a -> Boolsum :: (Foldable t, Num a) => t a -> aproduct :: (Foldable t, Num a) => t a -> amaximum :: (Foldable t, Ord a) => t a -> aminimum :: (Foldable t, Ord a) => t a -> aelem :: (Foldable t, Eq a) => a -> t a -> Bool…
aka Burning Bridges
Proposal
![Page 76: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/76.jpg)
Data.Foldable
Foldable/Traversable Proposal
3
Prelude
toList :: Foldable t => t a -> [a]and :: Foldable t => t Bool -> Boolor :: Foldable t => t Bool -> Boolany :: Foldable t => (a -> Bool) -> t a -> Boolall :: Foldable t => (a -> Bool) -> t a -> Boolsum :: (Foldable t, Num a) => t a -> aproduct :: (Foldable t, Num a) => t a -> amaximum :: (Foldable t, Ord a) => t a -> aminimum :: (Foldable t, Ord a) => t a -> aelem :: (Foldable t, Eq a) => a -> t a -> Bool…
In Prelude as of GHC 7.10
aka Burning Bridges
Proposal
![Page 77: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/77.jpg)
Divide & Conquer
![Page 78: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/78.jpg)
Linear Processing
x1
x2
x3
x4
+
+
+
![Page 79: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/79.jpg)
Linear Processingx1 <> (x2 <> (x3 <> x4))
x1
x2
x3
x4
+
+
+
![Page 80: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/80.jpg)
Linear Processingx1 <> (x2 <> (x3 <> x4))
x1 <> (x2 <> x34)x1
x2
x3
x4
+
+
+
![Page 81: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/81.jpg)
Linear Processingx1 <> (x2 <> (x3 <> x4))
x1 <> (x2 <> x34)
x1 <> (x234)
x1
x2
x3
x4
+
+
+
![Page 82: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/82.jpg)
Linear Processingx1 <> (x2 <> (x3 <> x4))
x1 <> (x2 <> x34)
x1 <> (x234)
x1234
x1
x2
x3
x4
+
+
+
![Page 83: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/83.jpg)
Linear Processingx1 <> (x2 <> (x3 <> x4))
x1 <> (x2 <> x34)
x1 <> (x234)
x1234
x1
x2
x3
x4
+
+
+
n-1 gate delays
![Page 84: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/84.jpg)
Linear Strategy
mconcat :: Monoid m => [m] -> mmconcat = foldr mappend mempty
Data.Monoid
![Page 85: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/85.jpg)
Parallel Processing
x1
x2
x3
x4+
+
+
![Page 86: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/86.jpg)
Parallel Processing(x1 <> x2) <> (x3 <> x4)
x1
x2
x3
x4+
+
+
![Page 87: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/87.jpg)
Parallel Processing(x1 <> x2) <> (x3 <> x4)
x12 <> x34x1
x2
x3
x4+
+
+
![Page 88: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/88.jpg)
Parallel Processing(x1 <> x2) <> (x3 <> x4)
x12 <> x34
x1234
x1
x2
x3
x4+
+
+
![Page 89: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/89.jpg)
Parallel Processing(x1 <> x2) <> (x3 <> x4)
x12 <> x34
x1234
x1
x2
x3
x4+
+
+
log n gate delays
![Page 90: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/90.jpg)
Parallel Strategypconcat :: Monoid m => [m] -> mpconcat [] = memptypconcat [x] = xpconcat xs = (ys `par` zs) `pseq` (ys <> zs) where len = length xs (ys', zs') = splitAt (len `div` 2) xs ys = pconcat ys' zs = pconcat zs'
![Page 91: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/91.jpg)
The List Monoid
![Page 92: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/92.jpg)
The List Monoid
class Monoid m where mempty :: m mappend :: m -> m -> m
instance Monoid [a] where mempty = [] mappend = (++)
Data.Monoid
![Page 93: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/93.jpg)
Equational Reasoning
![Page 94: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/94.jpg)
Left Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
mempty `mappend` ys = {- def. of mempty -} [] `mappend` ys = {- def. of mappend -} [] ++ ys = {- def. of (++) -} ys
Proof Style:EquationalReasoning
![Page 95: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/95.jpg)
Left Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
mempty `mappend` ys = {- def. of mempty -} [] `mappend` ys = {- def. of mappend -} [] ++ ys = {- def. of (++) -} ys
Proof Style:EquationalReasoning
![Page 96: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/96.jpg)
Left Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
mempty `mappend` ys = {- def. of mempty -} [] `mappend` ys = {- def. of mappend -} [] ++ ys = {- def. of (++) -} ys
Proof Style:EquationalReasoning
![Page 97: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/97.jpg)
Left Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
mempty `mappend` ys = {- def. of mempty -} [] `mappend` ys = {- def. of mappend -} [] ++ ys = {- def. of (++) -} ys
Proof Style:EquationalReasoning
![Page 98: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/98.jpg)
Left Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
mempty `mappend` ys = {- def. of mempty -} [] `mappend` ys = {- def. of mappend -} [] ++ ys = {- def. of (++) -} ys
Proof Style:EquationalReasoning
![Page 99: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/99.jpg)
Left Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
mempty `mappend` ys = {- def. of mempty -} [] `mappend` ys = {- def. of mappend -} [] ++ ys = {- def. of (++) -} ys
Proof Style:EquationalReasoning
![Page 100: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/100.jpg)
Right Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
l ++ [] = l
![Page 101: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/101.jpg)
Right Unit Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
l ++ [] = l
Proof Style:Structural Induction
+EquationalReasoning
![Page 102: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/102.jpg)
Base Case: l = []
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
[] ++ [] = {- def. of (++) -} []
Proof Style:Structural Induction
+EquationalReasoning
![Page 103: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/103.jpg)
Base Case: l = []
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
[] ++ [] = {- def. of (++) -} []
Proof Style:Structural Induction
+EquationalReasoning
![Page 104: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/104.jpg)
Inductive Case: l = x:xs
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
(x:xs) ++ [] = {- def. of (++) -} x : xs ++ [] = {- ind. hypot. -} x:xs
Proof Style:Structural Induction
+EquationalReasoning
![Page 105: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/105.jpg)
Inductive Case: l = x:xs
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
(x:xs) ++ [] = {- def. of (++) -} x : xs ++ [] = {- ind. hypot. -} x:xs
Proof Style:Structural Induction
+EquationalReasoning
xs ++ [] = xsInduction Hypothesis
![Page 106: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/106.jpg)
Inductive Case: l = x:xs
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
(x:xs) ++ [] = {- def. of (++) -} x : xs ++ [] = {- ind. hypot. -} x:xs
Proof Style:Structural Induction
+EquationalReasoning
xs ++ [] = xsInduction Hypothesis
![Page 107: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/107.jpg)
Inductive Case: l = x:xs
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
(x:xs) ++ [] = {- def. of (++) -} x : xs ++ [] = {- ind. hypot. -} x:xs
Proof Style:Structural Induction
+EquationalReasoning
xs ++ [] = xsInduction Hypothesis
![Page 108: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/108.jpg)
Inductive Case: l = x:xs
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
(x:xs) ++ [] = {- def. of (++) -} x : xs ++ [] = {- ind. hypot. -} x:xs
Proof Style:Structural Induction
+EquationalReasoning
xs ++ [] = xsInduction Hypothesis
![Page 109: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/109.jpg)
Associativity Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
xs ++ (ys ++ zs) = (xs ++ ys) ++ zs
Proof Style:Structural Induction
+EquationalReasoning
![Page 110: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/110.jpg)
Associativity Proof
[] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys
xs ++ (ys ++ zs) = (xs ++ ys) ++ zs
Proof Style:Structural Induction
+EquationalReasoning
Homework
![Page 111: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/111.jpg)
The Free Monoid
![Page 112: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/112.jpg)
Monoid (Homo)morphisma function between monoids
f :: M1 -> M2
f mempty = mempty f (x <> y) = f x <> f y
such that:
and:
![Page 113: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/113.jpg)
length (x ++ y) = length x + length y
length [] = 0
length :: [a] -> Int
Monoid (Homo)morphisma function between monoids
such that:
and:
![Page 114: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/114.jpg)
Free Monoid
A
M
gen
![Page 115: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/115.jpg)
Free Monoid
A
M
gen FreeMonoid
![Page 116: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/116.jpg)
Free Monoid
A
M
gen FreeMonoid
homomorphism
![Page 117: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/117.jpg)
Free Monoid
A
M
gen FreeMonoid
inj
homomorphism
![Page 118: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/118.jpg)
Free Monoid
A
M
gen
inj
homomorphism
[A]
![Page 119: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/119.jpg)
Free Monoid
A
M
gen
homomorphism
[A]
\x -> [x]
![Page 120: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/120.jpg)
Free Monoid
A
M
gen [A]
\x -> [x]
mconcat . map gen
![Page 121: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/121.jpg)
What is a Data.Foldable?
T a
![Page 122: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/122.jpg)
What is a Data.Foldable?
T a
m
foldMap gen
Data structures that support some
abstract nonsense?
![Page 123: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/123.jpg)
What is a Data.Foldable?
T a
[a]
toList
Data structures that
support a list view!
m
foldMap gen
Data structures that support some
abstract nonsense?
![Page 124: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/124.jpg)
What is a Data.Foldable?
T a
mconcat . map gen
[a]
toList
Data structures that
support a list view!
m
foldMap gen
Data structures that support some
abstract nonsense?
![Page 125: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/125.jpg)
What is a Data.Foldable?
T a
mconcat . map gen
[a]
toList
Data structures that
support a list view!
m
foldMap gen
Data structures that support some
abstract nonsense?
(Potentially)more efficient
![Page 126: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/126.jpg)
Summary
![Page 127: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/127.jpg)
Monoids
★ Simple concept from Algebra
★ Ubiquitous in Haskell
★ Cool Applications
★ List is the Free Monoid
![Page 128: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/128.jpg)
Next time: 5/5/2015
![Page 129: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/129.jpg)
Recursion Schemes
GADTs
Type Families
Monads
Effect Handlers
Data Genericity
Expression Problem
Free Theorems
DSLs
Type Classes
…
Data Genericity
RecursionSchemes
Lists and other
Monoids
![Page 130: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/130.jpg)
Recursion Schemes
GADTs
Type Families
Monads
Effect Handlers
Data Genericity
Expression Problem
Free Theorems
DSLs
Type Classes
…
Data Genericity
RecursionSchemes
Lists and other
Monoids
GADTs
Type Families
Type Classes
![Page 131: Lists and Other Monoids - KU Leuventom.schrijvers/Research/...Monoids DSLs Type Classes … Recursion Schemes GADTs Type Families Monads Effect Handlers Data Genericity Expression](https://reader033.vdocuments.us/reader033/viewer/2022042920/5f673f14558dde63fb230179/html5/thumbnails/131.jpg)
Join the Google Group
Leuven Haskell User Group