→ A 1A : identity for ⊕ (x ⊕ y) ⊕ z = x ⊕ (y ⊕ z) x = x ⊕ 1A = 1A ⊕ x Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 2 / 67
× F(B) → F((A, B)) η : A → F(A) (fa ⊗ fb) ⊗ fc ∼ = fa ⊗ (fb ⊗ fc) fa ∼ = fa ⊗ ηUnit ∼ = ηUnit ⊗ fa Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 23 / 67
B)] = la match { case Nil => Nil case h :: t => lb.map((h, _)) ++ zipList(t, lb) } Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 25 / 67
B)] = la match { case Nil => Nil case h :: t => lb.map((h, _)) ++ zipList(t, lb) } def pureList[A](a: A): List[A] = List(a) Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 25 / 67
g: X => B): X => (A, B) = (x: X) => (f(x), g(x)) def pureFunction[A, X](a: A): X => A = (_: X) => a Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 26 / 67
(F(A) × A → F(B)) → F(B) η : A → F(A) 1> = >: (A → F(B) × B → F(C)) → A → F(C) Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 33 / 67
(F(A) × A → F(B)) → F(B) η : A → F(A) (fa > >= f ) > >= g = fa > >= (f >=> g)1 1> = >: (A → F(B) × B → F(C)) → A → F(C) Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 33 / 67
(F(A) × A → F(B)) → F(B) η : A → F(A) (fa > >= f ) > >= g = fa > >= (f >=> g)1 f (x) = η(x) > >= f 1> = >: (A → F(B) × B → F(C)) → A → F(C) Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 33 / 67
(F(A) × A → F(B)) → F(B) η : A → F(A) (fa > >= f ) > >= g = fa > >= (f >=> g)1 f (x) = η(x) > >= f fa = fa > >= η 1> = >: (A → F(B) × B → F(C)) → A → F(C) Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 33 / 67
=> Option[B]): Option[B] = oa match { case Some(a) => f(a) case None => None } Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 34 / 67
=> List[B]): List[B] = la match { case Nil => Nil case h :: t => f(h) ++ flatMapList(t, f) } Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 35 / 67
B → F(C) h : C → F(D) (f >=> g) >=> h = f >=> (g >=> h) f = f >=> η = η >=> f Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 40 / 67
A → B, g : B → C ... 1A : A → A (f ◦ g) ◦ h = f ◦ (g ◦ h) f = f ◦ 1A = 1A ◦ f Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 43 / 67
(A, B) = { val a = x._1 combine y._1 val b = x._2 combine y._2 (a, b) } 0Assuming Monoid[A] and Monoid[B] Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 54 / 67
(A, B) = { val a = x._1 combine y._1 val b = x._2 combine y._2 (a, b) } def empty: (A, B) = (empty[A], empty[B]) 0Assuming Monoid[A] and Monoid[B] Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 54 / 67
y: (F[B], G[B])): (F[(A, B)], G[(A, B)]) = { val f = x._1 zip y._1 val g = x._2 zip y._2 (f, g) } 0Assuming Monoidal[F] and Monoidal[G] Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 57 / 67
F[G[B]]): F[G[(A, B)]] = { val fp: F[(G[A], G[B])] = fga zip fgb fp.map { case (ga, gb) => ga zip gb } } 0Assuming Monoidal[F] and Monoidal[G] Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 62 / 67
compose A’s, lax monoidal/applicative functors compose independent programs, monads compose dependent programs Category theory studies composition, and each of the above can be framed as a category Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 66 / 67
compose A’s, lax monoidal/applicative functors compose independent programs, monads compose dependent programs Category theory studies composition, and each of the above can be framed as a category Monoids compose Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 66 / 67
compose A’s, lax monoidal/applicative functors compose independent programs, monads compose dependent programs Category theory studies composition, and each of the above can be framed as a category Monoids compose Lax monoidal/applicative functors compose Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 66 / 67
compose A’s, lax monoidal/applicative functors compose independent programs, monads compose dependent programs Category theory studies composition, and each of the above can be framed as a category Monoids compose Lax monoidal/applicative functors compose Monads in general do not compose See: monad transformers, extensible (algebraic) effects Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 66 / 67
Invariant Shadows - Michael Pilquist http://mpilquist.github.io/blog/2015/06/22/ invariant-shadows-part-2/ All About Applicative - Me! https://www.youtube.com/watch?v=Mn7BtPALFys A categorial view of computational effects - Emily Riehl https://www.youtube.com/watch?v=6t6bsWVOIzs Conceptual Mathematics - F. William Lawvere and Stephen H. Schanuel Adelbert Chang (@adelbertchang) Simplicity in composition Scala World 2017 67 / 67