SlideShare a Scribd company logo
1 of 75
Download to read offline
Big picture of Category Theory in Scala
with deep dive into:
- Contravariant
- Profunctors
Piotr Paradziński
ScalaC27.03.2019
1
● PR’s to Scalaz 7
○ github.com/scalaz/scalaz/pull/2020 Day convolution (0,5 year fight translate from Haskell)
○ github.com/scalaz/scalaz/pull/2028 Strong Profunctor laws - they were none
○ github.com/scalaz/scalaz/pull/2029 Density comonad (abstraction - no practical applications yet)
● PR’s to Cats
○ github.com/typelevel/cats/pull/2640 Strong Profunctor laws (based on CT replace ad hoc ones)
● PR’s to Haskell Profunctors
○ github.com/ekmett/profunctors/pull/65 broken link (this is how you start!)
○ github.com/ekmett/profunctors/pull/66 small fix in docs for Strong Profunctor laws
● type_classopedia - wiki about Category Theory abstraction in Scala
○ github.com/lemastero/scala_typeclassopedia
● other OSS work: resources about effects, programming language theory, streaming benchmarks
○ Sclaz ZIO github.com/scalaz/scalaz-zio/pulls?utf8=%E2%9C%93&q=+author%3Alemastero SclaC Hackaton
○ yallop/effects-bibliography github.com/yallop/effects-bibliography/pulls?utf8=✓&q=+author%3Alemastero add
Idris Effects, Scala Eff Monad) wiki about effects by Jeremy Yallop (University of Cambridge)
○ steshaw.org/plt/ (github.com/steshaw/plt/pulls?utf8=✓&q=+author%3Alemastero) add 7Sketches, TAC Journal
○ monix/streaming-benchmarks github.com/monix/streaming-benchmarks/pull/1 updated benchmarks
○ cohomolo-gy/haskell-resources github.com/cohomolo-gy/haskell-resources/pull/3 add Haskell papers
○ lauris/awesome-scala github.com/lauris/awesome-scala/pull/425 add ZIO, update Monix, RxScala
○ passy/awesome-recurion-schemas github.com/passy/awesome-recursion-schemes/pull/22 add droste
Big Picture
3
Category Theory abstractions in Scala
- Functor, Apply, Applicative
4
Category Theory abstractions there is more
- Functor, Apply, Applicative
- Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector
5
Functor - Signature
def map[A,B](fa: F[A])(f: A => B): F[B] Functor
6
Apply - Signature
def map[A,B](fa: F[A])(f: A => B): F[B] Functor
def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply
7
Applicative - Signature
def map[A,B](fa: F[A])(f: A => B): F[B] Functor
def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply
def pure[A](value: A): F[A] Applicative
8
Monad - Signature
def map[A,B](fa: F[A])(f: A => B): F[B] Functor
def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply
def pure[A](value: A): F[A] Applicative
def flatMap[A,B](fa: F[A])(f: A => F[B]): F[B] Monad
9
Signatures - similar?
def map[A,B](fa: F[A])(f: A => B): F[B] Functor
def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply
def pure[A](value: A): F[A] Applicative
def flatMap[A,B](fa: F[A])(f: A => F[B]): F[B] Monad
10
Covariant Functors Signatures - Pattern
def map A => B => F[B] Functor
def ap F[A => B] => F[B] Apply
def pure () => B => F[B] Applicative*
def flatMap A => F[B] => F[B] Monad
http://blog.tmorris.net/posts/scala-type-class-hierarchy/index.html
11
Category Theory abstractions in Scala
- Functor, Apply, Applicative
- Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector, Free
- Comonads: NonEmptyList, Stream, Store, Context, Cofree
12
Comonads - signatures
def map[A,B](fa: F[A])(f: A => B): F[B] Functor
def coflatMap[A, B](fa: F[A])(f: F[A] => B): F[B] CoflatMap
def coflatten[A](fa: F[A]): F[F[A]] CoflatMap
def extract[A](x: F[A]): A Comonad
13
Comonads - Signatures - Pattern
//def flatMap (F[A], A => F[B]): F[B] FlatMap
def coflatMap (F[A], F[A] => B): F[B] CoflatMap
//def flatten F[F[A]]: F[A] Monad
def coflatten F[A]: F[F[A]] CoflatMap
//def pure A : F[A] Applicative
def extract F[A]: A Comonad
14
Category Theory abstractions in Scala
- Functor, Apply, Applicative
- Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector
- Comonads: NonEmptyList, Stream, Store, Context
- Contravariant, Divide, Divisible
15
Big Picture
Contravariant Functors
16
Contravariant - signature
trait Contravariant[F[_]] {
def contramap[A, B] (fa: F[A])(f: B => A): F[B]
}
17
Contravariant - like a Functor but flip!
trait Functor[F[_]] {
def map[A, B] (fa: F[A]) (f: A => B): F[B]
}
trait Contravariant[F[_]] {
def contramap[A, B] (fa: F[A]) (f: B => A): F[B]
}
18
Contravariant Functor - input + ability to prepend
Functor is “full of” A’s (container A, function producing A)
F[A] we can map A => B and we get F[B]
F[A] we can contramap B => A and we get F[B]
Contravariant “needs” A (index of container, function
consuming A)
19
case class Predicate[A](fun: A => Boolean)
Example in GIthub:
https://github.com/lemastero/scala_typeclassopedia/blob/master/src/test/scala/contravariant/Contravaria
ntSpec.scala
Contravariant - example Predicate (1)
20
case class Predicate[A](fun: A => Boolean)
val predicateContravariantFunctor = new Contravariant[Predicate] {
def contramap[A, B](pred: Predicate[A])(fba: B => A): Predicate[B] =
Predicate[B](fba andThen pred.fun)
}
Contravariant - example Predicate (2)
21
val pl = Predicate[String](_.length > 5)
pl.fun("Contravariant") // true
val pr1 = Predicate[Int](_ > 5)
pr1.fun(42) // true
Contravariant - example Predicate (3)
22
val pl = Predicate[String](_.length > 5)
pl.fun("Contravariant") // true
val pr1 = Predicate[Int](_ > 5)
pr1.fun(42) // true
val len: String => Int = _.length
val pr = Contravariant[Predicate].contramap(pr1)(len)
pr.fun("Contravariant") // true
Contravariant - example Predicate (4)
23
Contravariant - example Show
trait Show[F] {
def show(f: F): String
}
implicit val showContravariant = new Contravariant[Show] {
def contramap[A, B](r: Show[A])(f: B => A): Show[B] = new Show[B] {
def show(b: B): String = r.show(f(b))
}
}
Scalaz github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/Show.scala#L51-L53
24
Contravariant - example Op (Haskell like)
case class Op[R,A](getOp: A => R)
def opContravariant [R] = new Contravariant[Op[ R, ?]] {
def contramap[A, B](fa: Op[R, A])(f: B => A): Op[R, B] =
Op(f andThen fa.getOp)
}
25
Contravariant - example Function1
def function1Contravariant[R]: Contravariant[? => R] =
new Contravariant[? => R] {
def contramap[A, B](r: A => R)(f: B => A) = r compose f
}
26
Contravariant - FunctionN parameters all but last
trait Function2[-T1, -T2, +R]{
def apply(v1: T1, v2: T2): R
}
trait Function3[-T1, -T2, -T3, +R]{
def apply(v1: T1, v2: T2, v3: T3): R
}
//...
All parameters are in negative position, co we could define Contravariant
instance for it.
(Or even BiContravariant, TriContravariant, ... if they existed :)
27
Contravariant - example Reader (1)
case class Reader[C, V](run: C => V)
28
Contravariant - example Reader - Functor
case class Reader[C, V](run: C => V)
def readerFunctor[C] = new Functor[Reader[C,?]] {
def map[A, B](x: Reader[C, A])(f: A => B): Reader[C, B] =
Reader(x.run andThen f)
}
29
Contravariant - example Reader - Monad
case class Reader[C, V](run: C => V)
def readerMonad[C] = new Monad[Reader[C, ?]] {
def map[A, B](x: Reader[C, A])(f: A => B): Reader[C, B] =
Reader(x.run andThen f)
def pure[A](a: A): Reader[C, A] =
new Reader(_ => a)
def flatMap[A, B](ma: Reader[C, A])(f: A => Reader[C, B]) = ???
}
30
Contravariant - example Reader - Contravariant
case class Reader[C, V](run: C => V)
def readerContra[V] = new Contravariant[Reader[?, V]] {
def contramap[A, B](fa: Reader[A, V])(f: B => A):
Reader[B, V] = Reader(f andThen fa.run)
}
31
Contravariant - example Encoder
Encoder in scodec:
github.com/scodec/scodec/blob/series/1.11.x/shared/src/main/scala/scodec/Encoder.scala#L4
0-L47
has Contravariant instance
github.com/scodec/scodec-cats/blob/master/shared/src/main/scala/scodec/interop/cats/CatsI
nstances.scala#L121-L123
32
Contravariant - Resources Haskell
George Wilson: Contravariant Functors: The Other Side of the Coin :
https://www.youtube.com/watch?v=IJ_bVVsQhvc
Michael noyman - Covariance and Contravariance
fpcomplete.com/blog/2016/11/covariance-contravariance
Tom Ellis - 24 Days of Hackage: contravariant
https://ocharles.org.uk/blog/guest-posts/2013-12-21-24-days-of-hackage-contravariant.html
(nice example with actors producing Behaviour a)
https://packdeps.haskellers.com/reverse/contravariant (100+ Haskell libs using
Contravariant package)
33
Contravariant - discrimination sort
youtube.com/watch?v=cB8DapKQz-I
Sorting in linear time
Edward Kmett in Haskell
In Scala?
34
Functor laws
fmap id = id
fmap f . fmap g = fmap (f . g)
Contravariant laws
contramap id = id
contramap f . contramap g = contramap (g . f)
Contravariant - laws in Haskell
35
Cats:
github.com/typelevel/cats/blob/master/laws/src/main/scala/cats/laws/ContravariantLaws.scala
Scalaz 7:
github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/Contravariant.scala#L59-L68
scala_typeclassopedia:
github.com/lemastero/scala_typeclassopedia#contravariant-contravariant-functor
Contravariant - laws in Scala
36
Contravariant - laws in Scala
37
Divide
(Contravariant Semigroupal - in Cats)
38
case class Serializer[A](run: A => Array[Byte])
val strSerial = Serializer[String](_.getBytes)
val intSerial = Serializer[Int](_.toString.getBytes)
Github:
https://github.com/lemastero/scala_typeclassopedia/blob/master/src/test/scala/contravaria
nt/DivideSpec.scala
Divide (1) - Serialization
39
val fragmentSerial = Serializer[Fragment] { frag =>
val a1 = strSerial.run(frag.name)
val a2 = intSerial.run(frag.size)
a1 ++ a2
}
val serialized = fragmentSerial.run(Fragment("Area", 52))
new String(serialized ) mustBe "Area52"
Divide (2) - How to combine serializers?
40
trait Divide[F[_]] extends Contravariant[F] {
def divide[A,B,C](f: A => (B,C), fb: F[B], fc: F[C]): F[A]
}
Divide (3) - abstraction
41
val fragmentDivide: Divide[Serializer] = new Divide[Serializer] {
def divide2[A1, A2, Z](s1: => Serializer[ A1], s2: => Serializer[ A2])(f: Z
=> (A1, A2)): Serializer[ Z] = Serializer{ frag =>
val (a1,a2) = f(frag)
s1.run(a1) ++ s2.run(a2)
}
}
Divide (4)
42
val fragAsTuple: Fragment => (String, Int) =
frag => (frag.name, frag.size)
val fragSerial: Serializer[Fragment] =
Divide[Serializer].divide(strSerial, intSerial)(fragAsTuple)
Divide (5)
43
val fragAsTuple: Fragment => (String, Int) =
frag => (frag.name, frag.size)
val fragSerial: Serializer[Fragment] =
Divide[Serializer].divide(strSerial, intSerial)(fragAsTuple)
val serialized = fragSerial.run(Fragment("Area", 52))
new String(serialized ) mustBe "Area52"
Divide - Run
44
https://github.com/lemastero/scala_typeclassopedia#divide-co
ntravariant-apply
Divide - Laws & derived methods
45
Define full laws for Divide as in Haskell
hackage.haskell.org/package/contravariant/docs/Data-Functor-Contravariant-Divisible.html#g:4
Not simplified as in Scalaz and Cats!
Contravariant - Divide - Exercise
46
Contravariant Functors (1)
def contramap(fa: F[A],f: B => A): F[B] Contravariant
def divide(f: A => (B,C), fb: F[B],fc: F[C]): F[A] Divide
def conquer: F[A] Divisible
There is no Contravariant Monad
There is Contravariant Traversable, Alternative:
https://www.youtube.com/watch?v=cB8DapKQz-I Edward Kmett, Discrimination is
Wrong, 2015
47
Contravariant Functors (2) - arrows reversed
//def map (F[A], A => B): F[B] Functor
def contramap (F[A], A <= B): F[B] Contravariant (Contravariant Functor)
//def map2 ((A,B) => Z, F[A], F[B]): F[Z] Apply (not ap!)
def divide (Z => (A,B), F[A], F[B]): F[Z] Divide (Contravariant Apply)
//def pure: ( () => A ) => F[A] Applicative
def conquer: ( A <= () ) => F[A] Divisible (Contravariant Applicative)
48
Bigger Picture
Profunctors
49
Category Theory abstractions in Scala
- Functor, Apply, Applicative
- Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector
- Comonads: NonEmptyList, Stream, Store, Context
- Contravariant, Divide, Divisible
- Profunctor, Profunctor Strong, Profunctor Choice, Arrows, Kleisli
50
trait Profunctor[P[_, _]] {
def dimap[X,Y,Z,W](ab: X => Y, cd: Z => W): P[Y, Z] => P[X, W]
}
Profunctor
51
trait Profunctor[P[_, _]] {
def dimap[X,Y,Z,W](ab: X => Y, cd: Z => W): P[Y, Z] => P[X, W]
}
● contramap on first type ( P[Y, _] is Contravariant )
● map on second type ( P[_, Z] is Functor )
Profunctor = Contravariant + Functor
52
trait Profunctor[P[_, _]] {
def dimap[X,Y,Z,W](ab: X => Y, cd: Z => W): P[Y, Z] => P[X, W]
// derived methods
def lmap[A,B,C](f: A => B): P[B,C] => P[A,C] = dimap[A,B,C,C](f,identity[C])
def rmap[A,B,C](f: B => C): P[A,B] => P[A,C] = dimap[A,A,B,C](identity[A],
f)
}
Profunctor - derived methods
53
Haskell
● dimap id id == id
● dimap (f . g) (h . i) == dimap g h . dimap f i
Scala a bit more verbose
Profunctor - Laws
54
val function1: Profunctor[Function1] = new Profunctor[Function1] {
def dimap[X, Y, Z, W](f: X => Y, g: Z => W): (Y => Z) => (X => W) =
h => f andThen (g compose h)
def lmap[A,B,C](f: A => B): (B => C) => (A => C) = f andThen
def rmap[A,B,C](f: B => C): (A => B) => (A => C) = f compose
}
Profunctor - Example Function1
55
val f: String => Int = _.length
case class Person(name: String, age: Int)
val preF: Person => String = _.name
val postF: Int => Boolean = _ > 5
Profunctor - Example Function1 - setup
56
val f: String => Int = _.length
case class Person(name: String, age: Int)
val preF: Person => String = _.name
val postF: Int => Boolean = _ > 5
Profunctor[Function1].dimap(f)(preF)(postF)(
Person("Foo", 100)
)
Profunctor - Example Function1 - true ?
57
val f: String => Int = _.length
case class Person(name: String, age: Int)
val preF: Person => String = _.name
val postF: Int => Boolean = _ > 5
Profunctor[Function1].dimap(f)(preF)(postF)(
Person("Foo", 100)
)
// false
Profunctor - Example Function1
58
● Haskell opaleye:
https://github.com/tomjaguarpaw/haskell-opaleye/search?q=dimap&unscoped_q=dimap
● Monadic profunctors for bidirectional programming - blog post
https://blog.poisson.chat/posts/2017-01-01-monadic-profunctors.html
● Kleisli Arrow
Profunctor - Usage
59
Strong Profunctor
60
trait Strong[P[_, _]] extends Profunctor[P] {
def first[X,Y,Z](pab: P[X, Y]): P[(X, Z), (Y, Z)]
}
Profunctor - Strong
61
trait Strong[P[_, _]] extends Profunctor[P] {
def first[X,Y,Z](pab: P[X, Y]): P[(X, Z), (Y, Z)]
// derived methods
def second[X,Y,Z](pab: P[X, Y]): P[(Z, X), (Z, Y)]
}
Profunctor - Strong - Derived methods
62
val functionStrong: Strong[Function1] = new Strong[Function1]
with Function1Profunctor {
def first[X, Y, Z](pab: X => Y): Function1[( X, Z), (Y, Z)] =
(xz: (X, Z)) => (pab(xz._1) , xz._2)
}
Profunctor - Strong - Function1
63
val functionStrong: Strong[Function1] = new Strong[Function1]
with Function1Profunctor {
def first[X, Y, Z](pab: X => Y): Function1[( X, Z), (Y, Z)] =
(xz: (X, Z)) => (pab(xz._1) , xz._2)
}
Profunctor[Function1].first(len)(( "foo", 42)) == (3,42)
Profunctor - Strong - Function1
64
PR #2640 Strong Profunctor Laws: https://github.com/typelevel/cats/pull/2640
Profunctor - Strong Laws
65
Description:
https://github.com/lemastero/scala_typeclassopedia#profunctor
Translation to Scala early (very early pre-alpha):
https://github.com/lemastero/scala_typeclassopedia/tree/master/src/main/scala/profunctor
Profunctor hierarchy - big, not in Scala yet
66
Strong Profunctor == Arrow
Model IO using Profunctors/Arrows?
67
Bigger Picture
Everything else
68
Category Theory abstractions in Scala
- Functor, Apply, Applicative
- Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector
- Comonads: NonEmptyList, Stream, Store, Context
- Contravariant, Divide, Divisible
- Profunctor, Profunctor Strong, Profunctor Choice, Arrows, Kleisli
- Traversable, Foldable (+ Monoid, Semigroup)
- Kan extensions, Yoneda, Coyoneda, Density, Codensity
- Free Monads, Free Applicative, Cofree, Free Alternative, Free Arrows
- ... Profunctor optics, Cartesian Closed Categories, Day Convolution
69
Big Picture of Category Theory in Scala
70
Nice papers
- Different encodings of CT like Sjoerd Visscher data-category:
hackage.haskell.org/package/data-category-0.7/docs/Data-Category.html
- Bifunctors: Joker, Clown, Biff, … - nice papers
- Arrows … - nice papers
- Distributive (Comonad Applicative) - nice papers
- Adjunctions between Monads, Applicative, Arrows - nice papers
- Monad, Applicative, Arrow as Monoid in Monoidal Category with given tensor
(Functor Composition, Sum, Product, Day convolution) - nice papers
- Recursion schemas (Fold, Unfolds, Fix point) - people talk about 3, exists 20+
71
Alternative encoding
- Different encodings of CT like Sjoerd Visscher data-category:
hackage.haskell.org/package/data-category-0.7/docs/Data-Category.html
- Proofs of Category Theory in Coq
- Proofs of Category Theory using Univalent Foundations (related to HoTT) in
Coq (UniMath/UniMath) https://github.com/UniMath/UniMath/tree/master/UniMath/CategoryTheory
72
How to learn Category Theory
1) Translate to Scala, Kata?
wiki.haskell.org/Typeclassopedia, Edward Kmett libs
2) Describe github.com/lemastero/scala_typeclassopedia
3) In Scala 3
4) Talk using new vocabulary!
73
Good general theory does not search for the maximum
generality, but for the right generality.
Saunders Mac Lane
Choose the right level of abstraction, to see the
problem more clearly
Eugenia Cheng, Category in Life
https://www.youtube.com/watch?v=ho7oagHeqNc
Thank you :)
74
Thank you :)
75

More Related Content

What's hot

Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalMarian Wamsiedel
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfJaroslavRegec1
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Philip Schwarz
 
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021Natan Silnitsky
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access PrinciplePhilip Schwarz
 
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Ruslan Shevchenko
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldJorge Vásquez
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them AllJohn De Goes
 
Microservices Architecture & Testing Strategies
Microservices Architecture & Testing StrategiesMicroservices Architecture & Testing Strategies
Microservices Architecture & Testing StrategiesAraf Karsh Hamid
 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022Alexander Ioffe
 
The Terror-Free Guide to Introducing Functional Scala at Work
The Terror-Free Guide to Introducing Functional Scala at WorkThe Terror-Free Guide to Introducing Functional Scala at Work
The Terror-Free Guide to Introducing Functional Scala at WorkJorge Vásquez
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureJosé Paumard
 
Handling eventual consistency in a transactional world with Matteo Cimini and...
Handling eventual consistency in a transactional world with Matteo Cimini and...Handling eventual consistency in a transactional world with Matteo Cimini and...
Handling eventual consistency in a transactional world with Matteo Cimini and...HostedbyConfluent
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use casesFabio Biondi
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayDebasish Ghosh
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Philip Schwarz
 

What's hot (20)

Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdf
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1
 
Completable future
Completable futureCompletable future
Completable future
 
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
 
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them All
 
Microservices Architecture & Testing Strategies
Microservices Architecture & Testing StrategiesMicroservices Architecture & Testing Strategies
Microservices Architecture & Testing Strategies
 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022
 
The Terror-Free Guide to Introducing Functional Scala at Work
The Terror-Free Guide to Introducing Functional Scala at WorkThe Terror-Free Guide to Introducing Functional Scala at Work
The Terror-Free Guide to Introducing Functional Scala at Work
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Handling eventual consistency in a transactional world with Matteo Cimini and...
Handling eventual consistency in a transactional world with Matteo Cimini and...Handling eventual consistency in a transactional world with Matteo Cimini and...
Handling eventual consistency in a transactional world with Matteo Cimini and...
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2
 
Es6 to es5
Es6 to es5Es6 to es5
Es6 to es5
 

Similar to Big picture of category theory in scala with deep dive into contravariant and profunctors

Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr Paradziński
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Peng Cheng
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
 
Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...Scalac
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsFrançois Garillot
 
Advance Scala - Oleg Mürk
Advance Scala - Oleg MürkAdvance Scala - Oleg Mürk
Advance Scala - Oleg MürkPlanet OS
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2Hang Zhao
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type ClassesTapio Rautonen
 
From Function1#apply to Kleisli
From Function1#apply to KleisliFrom Function1#apply to Kleisli
From Function1#apply to KleisliHermann Hueck
 
Mining Functional Patterns
Mining Functional PatternsMining Functional Patterns
Mining Functional PatternsDebasish Ghosh
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
Kyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfFlavio W. Brasil
 
Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)Ittay Dror
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsDebasish Ghosh
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad Fabernovel
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaDaniel Sebban
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
MATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdf
MATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdfMATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdf
MATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdfCentral university of Haryana
 

Similar to Big picture of category theory in scala with deep dive into contravariant and profunctors (20)

Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystified
 
Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Advance Scala - Oleg Mürk
Advance Scala - Oleg MürkAdvance Scala - Oleg Mürk
Advance Scala - Oleg Mürk
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type Classes
 
From Function1#apply to Kleisli
From Function1#apply to KleisliFrom Function1#apply to Kleisli
From Function1#apply to Kleisli
 
Mining Functional Patterns
Mining Functional PatternsMining Functional Patterns
Mining Functional Patterns
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Kyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdf
 
Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain Models
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
MATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdf
MATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdfMATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdf
MATLAB-Cheat-Sheet-for-Data-Science_LondonSchoolofEconomics (1).pdf
 

Recently uploaded

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

Big picture of category theory in scala with deep dive into contravariant and profunctors

  • 1. Big picture of Category Theory in Scala with deep dive into: - Contravariant - Profunctors Piotr Paradziński ScalaC27.03.2019 1
  • 2. ● PR’s to Scalaz 7 ○ github.com/scalaz/scalaz/pull/2020 Day convolution (0,5 year fight translate from Haskell) ○ github.com/scalaz/scalaz/pull/2028 Strong Profunctor laws - they were none ○ github.com/scalaz/scalaz/pull/2029 Density comonad (abstraction - no practical applications yet) ● PR’s to Cats ○ github.com/typelevel/cats/pull/2640 Strong Profunctor laws (based on CT replace ad hoc ones) ● PR’s to Haskell Profunctors ○ github.com/ekmett/profunctors/pull/65 broken link (this is how you start!) ○ github.com/ekmett/profunctors/pull/66 small fix in docs for Strong Profunctor laws ● type_classopedia - wiki about Category Theory abstraction in Scala ○ github.com/lemastero/scala_typeclassopedia ● other OSS work: resources about effects, programming language theory, streaming benchmarks ○ Sclaz ZIO github.com/scalaz/scalaz-zio/pulls?utf8=%E2%9C%93&q=+author%3Alemastero SclaC Hackaton ○ yallop/effects-bibliography github.com/yallop/effects-bibliography/pulls?utf8=✓&q=+author%3Alemastero add Idris Effects, Scala Eff Monad) wiki about effects by Jeremy Yallop (University of Cambridge) ○ steshaw.org/plt/ (github.com/steshaw/plt/pulls?utf8=✓&q=+author%3Alemastero) add 7Sketches, TAC Journal ○ monix/streaming-benchmarks github.com/monix/streaming-benchmarks/pull/1 updated benchmarks ○ cohomolo-gy/haskell-resources github.com/cohomolo-gy/haskell-resources/pull/3 add Haskell papers ○ lauris/awesome-scala github.com/lauris/awesome-scala/pull/425 add ZIO, update Monix, RxScala ○ passy/awesome-recurion-schemas github.com/passy/awesome-recursion-schemes/pull/22 add droste
  • 4. Category Theory abstractions in Scala - Functor, Apply, Applicative 4
  • 5. Category Theory abstractions there is more - Functor, Apply, Applicative - Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector 5
  • 6. Functor - Signature def map[A,B](fa: F[A])(f: A => B): F[B] Functor 6
  • 7. Apply - Signature def map[A,B](fa: F[A])(f: A => B): F[B] Functor def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply 7
  • 8. Applicative - Signature def map[A,B](fa: F[A])(f: A => B): F[B] Functor def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply def pure[A](value: A): F[A] Applicative 8
  • 9. Monad - Signature def map[A,B](fa: F[A])(f: A => B): F[B] Functor def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply def pure[A](value: A): F[A] Applicative def flatMap[A,B](fa: F[A])(f: A => F[B]): F[B] Monad 9
  • 10. Signatures - similar? def map[A,B](fa: F[A])(f: A => B): F[B] Functor def ap[A,B](ff: F[A => B])(fa: F[A]): F[B] Apply def pure[A](value: A): F[A] Applicative def flatMap[A,B](fa: F[A])(f: A => F[B]): F[B] Monad 10
  • 11. Covariant Functors Signatures - Pattern def map A => B => F[B] Functor def ap F[A => B] => F[B] Apply def pure () => B => F[B] Applicative* def flatMap A => F[B] => F[B] Monad http://blog.tmorris.net/posts/scala-type-class-hierarchy/index.html 11
  • 12. Category Theory abstractions in Scala - Functor, Apply, Applicative - Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector, Free - Comonads: NonEmptyList, Stream, Store, Context, Cofree 12
  • 13. Comonads - signatures def map[A,B](fa: F[A])(f: A => B): F[B] Functor def coflatMap[A, B](fa: F[A])(f: F[A] => B): F[B] CoflatMap def coflatten[A](fa: F[A]): F[F[A]] CoflatMap def extract[A](x: F[A]): A Comonad 13
  • 14. Comonads - Signatures - Pattern //def flatMap (F[A], A => F[B]): F[B] FlatMap def coflatMap (F[A], F[A] => B): F[B] CoflatMap //def flatten F[F[A]]: F[A] Monad def coflatten F[A]: F[F[A]] CoflatMap //def pure A : F[A] Applicative def extract F[A]: A Comonad 14
  • 15. Category Theory abstractions in Scala - Functor, Apply, Applicative - Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector - Comonads: NonEmptyList, Stream, Store, Context - Contravariant, Divide, Divisible 15
  • 17. Contravariant - signature trait Contravariant[F[_]] { def contramap[A, B] (fa: F[A])(f: B => A): F[B] } 17
  • 18. Contravariant - like a Functor but flip! trait Functor[F[_]] { def map[A, B] (fa: F[A]) (f: A => B): F[B] } trait Contravariant[F[_]] { def contramap[A, B] (fa: F[A]) (f: B => A): F[B] } 18
  • 19. Contravariant Functor - input + ability to prepend Functor is “full of” A’s (container A, function producing A) F[A] we can map A => B and we get F[B] F[A] we can contramap B => A and we get F[B] Contravariant “needs” A (index of container, function consuming A) 19
  • 20. case class Predicate[A](fun: A => Boolean) Example in GIthub: https://github.com/lemastero/scala_typeclassopedia/blob/master/src/test/scala/contravariant/Contravaria ntSpec.scala Contravariant - example Predicate (1) 20
  • 21. case class Predicate[A](fun: A => Boolean) val predicateContravariantFunctor = new Contravariant[Predicate] { def contramap[A, B](pred: Predicate[A])(fba: B => A): Predicate[B] = Predicate[B](fba andThen pred.fun) } Contravariant - example Predicate (2) 21
  • 22. val pl = Predicate[String](_.length > 5) pl.fun("Contravariant") // true val pr1 = Predicate[Int](_ > 5) pr1.fun(42) // true Contravariant - example Predicate (3) 22
  • 23. val pl = Predicate[String](_.length > 5) pl.fun("Contravariant") // true val pr1 = Predicate[Int](_ > 5) pr1.fun(42) // true val len: String => Int = _.length val pr = Contravariant[Predicate].contramap(pr1)(len) pr.fun("Contravariant") // true Contravariant - example Predicate (4) 23
  • 24. Contravariant - example Show trait Show[F] { def show(f: F): String } implicit val showContravariant = new Contravariant[Show] { def contramap[A, B](r: Show[A])(f: B => A): Show[B] = new Show[B] { def show(b: B): String = r.show(f(b)) } } Scalaz github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/Show.scala#L51-L53 24
  • 25. Contravariant - example Op (Haskell like) case class Op[R,A](getOp: A => R) def opContravariant [R] = new Contravariant[Op[ R, ?]] { def contramap[A, B](fa: Op[R, A])(f: B => A): Op[R, B] = Op(f andThen fa.getOp) } 25
  • 26. Contravariant - example Function1 def function1Contravariant[R]: Contravariant[? => R] = new Contravariant[? => R] { def contramap[A, B](r: A => R)(f: B => A) = r compose f } 26
  • 27. Contravariant - FunctionN parameters all but last trait Function2[-T1, -T2, +R]{ def apply(v1: T1, v2: T2): R } trait Function3[-T1, -T2, -T3, +R]{ def apply(v1: T1, v2: T2, v3: T3): R } //... All parameters are in negative position, co we could define Contravariant instance for it. (Or even BiContravariant, TriContravariant, ... if they existed :) 27
  • 28. Contravariant - example Reader (1) case class Reader[C, V](run: C => V) 28
  • 29. Contravariant - example Reader - Functor case class Reader[C, V](run: C => V) def readerFunctor[C] = new Functor[Reader[C,?]] { def map[A, B](x: Reader[C, A])(f: A => B): Reader[C, B] = Reader(x.run andThen f) } 29
  • 30. Contravariant - example Reader - Monad case class Reader[C, V](run: C => V) def readerMonad[C] = new Monad[Reader[C, ?]] { def map[A, B](x: Reader[C, A])(f: A => B): Reader[C, B] = Reader(x.run andThen f) def pure[A](a: A): Reader[C, A] = new Reader(_ => a) def flatMap[A, B](ma: Reader[C, A])(f: A => Reader[C, B]) = ??? } 30
  • 31. Contravariant - example Reader - Contravariant case class Reader[C, V](run: C => V) def readerContra[V] = new Contravariant[Reader[?, V]] { def contramap[A, B](fa: Reader[A, V])(f: B => A): Reader[B, V] = Reader(f andThen fa.run) } 31
  • 32. Contravariant - example Encoder Encoder in scodec: github.com/scodec/scodec/blob/series/1.11.x/shared/src/main/scala/scodec/Encoder.scala#L4 0-L47 has Contravariant instance github.com/scodec/scodec-cats/blob/master/shared/src/main/scala/scodec/interop/cats/CatsI nstances.scala#L121-L123 32
  • 33. Contravariant - Resources Haskell George Wilson: Contravariant Functors: The Other Side of the Coin : https://www.youtube.com/watch?v=IJ_bVVsQhvc Michael noyman - Covariance and Contravariance fpcomplete.com/blog/2016/11/covariance-contravariance Tom Ellis - 24 Days of Hackage: contravariant https://ocharles.org.uk/blog/guest-posts/2013-12-21-24-days-of-hackage-contravariant.html (nice example with actors producing Behaviour a) https://packdeps.haskellers.com/reverse/contravariant (100+ Haskell libs using Contravariant package) 33
  • 34. Contravariant - discrimination sort youtube.com/watch?v=cB8DapKQz-I Sorting in linear time Edward Kmett in Haskell In Scala? 34
  • 35. Functor laws fmap id = id fmap f . fmap g = fmap (f . g) Contravariant laws contramap id = id contramap f . contramap g = contramap (g . f) Contravariant - laws in Haskell 35
  • 37. Contravariant - laws in Scala 37
  • 39. case class Serializer[A](run: A => Array[Byte]) val strSerial = Serializer[String](_.getBytes) val intSerial = Serializer[Int](_.toString.getBytes) Github: https://github.com/lemastero/scala_typeclassopedia/blob/master/src/test/scala/contravaria nt/DivideSpec.scala Divide (1) - Serialization 39
  • 40. val fragmentSerial = Serializer[Fragment] { frag => val a1 = strSerial.run(frag.name) val a2 = intSerial.run(frag.size) a1 ++ a2 } val serialized = fragmentSerial.run(Fragment("Area", 52)) new String(serialized ) mustBe "Area52" Divide (2) - How to combine serializers? 40
  • 41. trait Divide[F[_]] extends Contravariant[F] { def divide[A,B,C](f: A => (B,C), fb: F[B], fc: F[C]): F[A] } Divide (3) - abstraction 41
  • 42. val fragmentDivide: Divide[Serializer] = new Divide[Serializer] { def divide2[A1, A2, Z](s1: => Serializer[ A1], s2: => Serializer[ A2])(f: Z => (A1, A2)): Serializer[ Z] = Serializer{ frag => val (a1,a2) = f(frag) s1.run(a1) ++ s2.run(a2) } } Divide (4) 42
  • 43. val fragAsTuple: Fragment => (String, Int) = frag => (frag.name, frag.size) val fragSerial: Serializer[Fragment] = Divide[Serializer].divide(strSerial, intSerial)(fragAsTuple) Divide (5) 43
  • 44. val fragAsTuple: Fragment => (String, Int) = frag => (frag.name, frag.size) val fragSerial: Serializer[Fragment] = Divide[Serializer].divide(strSerial, intSerial)(fragAsTuple) val serialized = fragSerial.run(Fragment("Area", 52)) new String(serialized ) mustBe "Area52" Divide - Run 44
  • 46. Define full laws for Divide as in Haskell hackage.haskell.org/package/contravariant/docs/Data-Functor-Contravariant-Divisible.html#g:4 Not simplified as in Scalaz and Cats! Contravariant - Divide - Exercise 46
  • 47. Contravariant Functors (1) def contramap(fa: F[A],f: B => A): F[B] Contravariant def divide(f: A => (B,C), fb: F[B],fc: F[C]): F[A] Divide def conquer: F[A] Divisible There is no Contravariant Monad There is Contravariant Traversable, Alternative: https://www.youtube.com/watch?v=cB8DapKQz-I Edward Kmett, Discrimination is Wrong, 2015 47
  • 48. Contravariant Functors (2) - arrows reversed //def map (F[A], A => B): F[B] Functor def contramap (F[A], A <= B): F[B] Contravariant (Contravariant Functor) //def map2 ((A,B) => Z, F[A], F[B]): F[Z] Apply (not ap!) def divide (Z => (A,B), F[A], F[B]): F[Z] Divide (Contravariant Apply) //def pure: ( () => A ) => F[A] Applicative def conquer: ( A <= () ) => F[A] Divisible (Contravariant Applicative) 48
  • 50. Category Theory abstractions in Scala - Functor, Apply, Applicative - Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector - Comonads: NonEmptyList, Stream, Store, Context - Contravariant, Divide, Divisible - Profunctor, Profunctor Strong, Profunctor Choice, Arrows, Kleisli 50
  • 51. trait Profunctor[P[_, _]] { def dimap[X,Y,Z,W](ab: X => Y, cd: Z => W): P[Y, Z] => P[X, W] } Profunctor 51
  • 52. trait Profunctor[P[_, _]] { def dimap[X,Y,Z,W](ab: X => Y, cd: Z => W): P[Y, Z] => P[X, W] } ● contramap on first type ( P[Y, _] is Contravariant ) ● map on second type ( P[_, Z] is Functor ) Profunctor = Contravariant + Functor 52
  • 53. trait Profunctor[P[_, _]] { def dimap[X,Y,Z,W](ab: X => Y, cd: Z => W): P[Y, Z] => P[X, W] // derived methods def lmap[A,B,C](f: A => B): P[B,C] => P[A,C] = dimap[A,B,C,C](f,identity[C]) def rmap[A,B,C](f: B => C): P[A,B] => P[A,C] = dimap[A,A,B,C](identity[A], f) } Profunctor - derived methods 53
  • 54. Haskell ● dimap id id == id ● dimap (f . g) (h . i) == dimap g h . dimap f i Scala a bit more verbose Profunctor - Laws 54
  • 55. val function1: Profunctor[Function1] = new Profunctor[Function1] { def dimap[X, Y, Z, W](f: X => Y, g: Z => W): (Y => Z) => (X => W) = h => f andThen (g compose h) def lmap[A,B,C](f: A => B): (B => C) => (A => C) = f andThen def rmap[A,B,C](f: B => C): (A => B) => (A => C) = f compose } Profunctor - Example Function1 55
  • 56. val f: String => Int = _.length case class Person(name: String, age: Int) val preF: Person => String = _.name val postF: Int => Boolean = _ > 5 Profunctor - Example Function1 - setup 56
  • 57. val f: String => Int = _.length case class Person(name: String, age: Int) val preF: Person => String = _.name val postF: Int => Boolean = _ > 5 Profunctor[Function1].dimap(f)(preF)(postF)( Person("Foo", 100) ) Profunctor - Example Function1 - true ? 57
  • 58. val f: String => Int = _.length case class Person(name: String, age: Int) val preF: Person => String = _.name val postF: Int => Boolean = _ > 5 Profunctor[Function1].dimap(f)(preF)(postF)( Person("Foo", 100) ) // false Profunctor - Example Function1 58
  • 59. ● Haskell opaleye: https://github.com/tomjaguarpaw/haskell-opaleye/search?q=dimap&unscoped_q=dimap ● Monadic profunctors for bidirectional programming - blog post https://blog.poisson.chat/posts/2017-01-01-monadic-profunctors.html ● Kleisli Arrow Profunctor - Usage 59
  • 61. trait Strong[P[_, _]] extends Profunctor[P] { def first[X,Y,Z](pab: P[X, Y]): P[(X, Z), (Y, Z)] } Profunctor - Strong 61
  • 62. trait Strong[P[_, _]] extends Profunctor[P] { def first[X,Y,Z](pab: P[X, Y]): P[(X, Z), (Y, Z)] // derived methods def second[X,Y,Z](pab: P[X, Y]): P[(Z, X), (Z, Y)] } Profunctor - Strong - Derived methods 62
  • 63. val functionStrong: Strong[Function1] = new Strong[Function1] with Function1Profunctor { def first[X, Y, Z](pab: X => Y): Function1[( X, Z), (Y, Z)] = (xz: (X, Z)) => (pab(xz._1) , xz._2) } Profunctor - Strong - Function1 63
  • 64. val functionStrong: Strong[Function1] = new Strong[Function1] with Function1Profunctor { def first[X, Y, Z](pab: X => Y): Function1[( X, Z), (Y, Z)] = (xz: (X, Z)) => (pab(xz._1) , xz._2) } Profunctor[Function1].first(len)(( "foo", 42)) == (3,42) Profunctor - Strong - Function1 64
  • 65. PR #2640 Strong Profunctor Laws: https://github.com/typelevel/cats/pull/2640 Profunctor - Strong Laws 65
  • 66. Description: https://github.com/lemastero/scala_typeclassopedia#profunctor Translation to Scala early (very early pre-alpha): https://github.com/lemastero/scala_typeclassopedia/tree/master/src/main/scala/profunctor Profunctor hierarchy - big, not in Scala yet 66
  • 67. Strong Profunctor == Arrow Model IO using Profunctors/Arrows? 67
  • 69. Category Theory abstractions in Scala - Functor, Apply, Applicative - Monads: State, Writer, Reader, IO, Option, Eiter, Validated, List/Vector - Comonads: NonEmptyList, Stream, Store, Context - Contravariant, Divide, Divisible - Profunctor, Profunctor Strong, Profunctor Choice, Arrows, Kleisli - Traversable, Foldable (+ Monoid, Semigroup) - Kan extensions, Yoneda, Coyoneda, Density, Codensity - Free Monads, Free Applicative, Cofree, Free Alternative, Free Arrows - ... Profunctor optics, Cartesian Closed Categories, Day Convolution 69
  • 70. Big Picture of Category Theory in Scala 70
  • 71. Nice papers - Different encodings of CT like Sjoerd Visscher data-category: hackage.haskell.org/package/data-category-0.7/docs/Data-Category.html - Bifunctors: Joker, Clown, Biff, … - nice papers - Arrows … - nice papers - Distributive (Comonad Applicative) - nice papers - Adjunctions between Monads, Applicative, Arrows - nice papers - Monad, Applicative, Arrow as Monoid in Monoidal Category with given tensor (Functor Composition, Sum, Product, Day convolution) - nice papers - Recursion schemas (Fold, Unfolds, Fix point) - people talk about 3, exists 20+ 71
  • 72. Alternative encoding - Different encodings of CT like Sjoerd Visscher data-category: hackage.haskell.org/package/data-category-0.7/docs/Data-Category.html - Proofs of Category Theory in Coq - Proofs of Category Theory using Univalent Foundations (related to HoTT) in Coq (UniMath/UniMath) https://github.com/UniMath/UniMath/tree/master/UniMath/CategoryTheory 72
  • 73. How to learn Category Theory 1) Translate to Scala, Kata? wiki.haskell.org/Typeclassopedia, Edward Kmett libs 2) Describe github.com/lemastero/scala_typeclassopedia 3) In Scala 3 4) Talk using new vocabulary! 73
  • 74. Good general theory does not search for the maximum generality, but for the right generality. Saunders Mac Lane Choose the right level of abstraction, to see the problem more clearly Eugenia Cheng, Category in Life https://www.youtube.com/watch?v=ho7oagHeqNc Thank you :) 74