Tagless Final Encoding - Algebras and Interpreters and also Programs

Philip Schwarz
Philip SchwarzSoftware development is what I am passionate about
Tagless Final Encoding
Algebras and Interpreters
and also Programs
an introduction, through the work of Gabriel Volpe
Algebras
Tagless Final
Encoding
@philip_schwarz
slides by http://fpilluminated.com/
This slide deck is a quick, introductory look, at Tagless Final, as explained (in rather more depth
than is possible or appropriate here) by Gabriel in his great book: Practical FP in Scala, a Hands-
on Approach.
In the first six slides, we are going to see Gabriel introduce the key elements of the technique.
Tagless final is all about algebras and interpreters. Yet,
something is missing when it comes to writing applications…
Tagless final is a great technique used to
structure purely functional applications.
Yes, as you’ll see, to the two official pillars of Tagless Final, i.e. algebras and interpreters, Gabriel
adds a third one: programs.
Algebras
Tagless Final
Encoding
@philip_schwarz
Algebras
An algebra describes a new language (DSL) within a host language, in this case, Scala.
This is a tagless final encoded algebra; tagless algebra, or algebra for short: a simple interface that
abstracts over the effect type using a type constructor F[_].
Do not confuse algebras with typeclasses, which in Scala, happen to share the same encoding.
The difference is that typeclasses should have coherent instances, whereas tagless algebras could have
many implementations, or more commonly called interpreters.
…
Overall, tagless algebras seem a perfect fit for encoding business concepts. For example, an algebra
responsible for managing items could be encoded as follows.
Nothing new, right? This tagless final encoded algebra is merely an interface that abstracts over the effect
type. Notice that neither the algebra nor its functions have any typeclass constraint.
If you find yourself needing to add a typeclass constraint, such as Monad, to your algebra, what you probably
need is a program.
The reason being that typeclass constraints define capabilities, which belong in programs and interpreters.
Algebras should remain completely abstract.
trait Items[F[_]] {
def getAll: F[List[Item]]
def add(item: Item): F[Unit]
}
trait Counter[F[_]] {
def increment: F[Unit]
def get: F[Int]
}
Tagless algebras should not have typeclass constraints
Tips
Interpreters
We would normally have two interpreters per algebra: one for testing and one for doing real things. For
instance, we could have two different implementations of our Counter.
A default interpreter using Redis.
And a test interpreter using an in-memory data structure.
Interpreters help us encapsulate state and allow separation of concerns: the interface knows nothing about
the implementation details. Moreover, interpreters can be written either using a concrete datatype such as
IO or going polymorphic all the way, as we did in this case.
object Counter {
@newtype case class RedisKey(value: String)
def make[F[_]: Functor](
key: RedisKey,
cmd: RedisCommands[F, String, Int]
): Counter[F] =
new Counter[F] {
def increment: F[Unit] =
cmd.increment(key.value).void
def get: F[Int] =
cmd.get(key.value).map(_.getOrElse(0))
}
}
def testCounter[F[_]](
ref: Ref[F, Int]
): Counter[F] = new Counter[F] {
def increment: F[Unit] = ref.update(_ + 1)
def get: F[Int] = ref.get
}
We are currently working our way through slides containing excerpts from Chapter 2: Tagless Final Encoding.
The next slide is an exception in that it contains an excerpt from Chapter1: Best Practices, which has already
introduced (in a less formal way), the concept of an interpreter for the Counter trait (without yet referring to
the latter as an algebra).
In-memory counter
Let’s say we need an in-memory counter that needs to be accessed and modified by other components. Here
is what our interface could look like.
It has a higher-kinded type F[_], representing an abstract effect, which most of the time ends up being IO,
but it could really be any other concrete type that fits the shape.
Next, we need to define an interpreter in the companion object of our interface, in this case using a Ref. We
will talk more about it in the next section.
…
Moving on, it’s worth highlighting that other programs will interact with this counter solely via its interface. E.g.
// prints out 0,1,6 when executed
def program(c: Counter[IO]): IO[Unit] =
for {
_ <- c.get.flatMap(IO.println)
_ <- c.increment
_ <- c.get.flatMap(IO.println)
_ <- c.increment.replicateA(5).void
_ <- c.get.flatMap(IO.println)
} yield ()
In the next chapter, we will discuss whether it is best to pass the dependency implicitly or explicitly.
object Counter {
def make[F[_]: Functor: Ref.Make]: F[Counter[F]] =
Ref.of[F, Int](0).map { ref =>
new Counter[F] {
def increment: F[Unit] = ref.update(_ + 1)
def get: F[Int] = ref.get
}
}
}
trait Counter[F[_]] {
def increment: F[Unit]
def get: F[Int]
}
import cats.Functor
import cats.effect.kernel.Ref
import cats.syntax.functor._
Programs
Tagless final is all about algebras and interpreters. Yet, something is missing when it comes to writing applications: we
need to use these algebras to describe business logic, and this logic belongs in what I like to call programs.
Although it is not an official name – and it is not mentioned in the original tagless final paper – it is how we will be
referring to such interfaces in this book.
Say we need to increase a counter every time there is a new item added. We could encode it as follows.
Observe the characteristics of this program. It is pure business logic, and it holds no state at all, which in any case,
must be encapsulated in the interpreters. Notice the typeclass constraints as well; it is a good practice to have them
in programs instead of tagless algebras.
…
Moreover, we can discuss typeclass constraints. In this case, we only need Apply to use *> (alias for productR).
However, it would also work with Applicative or Monad. The rule of thumb is to limit ourselves to adopt the least
powerful typeclass that gets the job done.
It is worth mentioning that Apply itself doesn’t specify the semantics of composition solely with this constraint, *>
might combine its arguments sequentially or parallelly, depending on the underlying typeclass instance. To ensure
our composition is sequential, we could use FlatMap instead of Apply.
class ItemsCounter[F[_]: Apply](
counter: Counter[F],
items: Items[F]
){
def addItem(item: Item): F[Unit] =
items.add(item) *>
counter.increment
}
Programs can make use of algebras and other programs
Notes
When adding a typeclass constraint, remember about the principle of least power
Tips
Other kinds of programs might be directly encoded as functions.
Furthermore, we could have programs composed of other programs.
Whether we encode programs in one way or another, they should describe pure business logic and nothing else.
The question is: what is pure business logic? We could try and define a set of rules to abide by. It is allowed to:
• Combine pure computations in terms of tagless algebras and programs.
– Only doing what our effect constraints allows us to do.
• Perform logging (or console stuff) only via a tagless algebra.
– In Chapter 8, we will see how to ignore logging or console stuff in tests, which are most of the time irrelevant in
such context.
You can use this as a reference. However, the answer should come up as a collective agreement within your team.
def program[F[_]: Console: Monad]: F[Unit] =
for {
_ <- Console[F].println("Enter your name: ")
n <- Console[F].readLine
_ <- Console[F].println(s"Hello $n!")
} yield ()
class MasterMind[F[_]: Console: Monad](
itemsCounter: ItemsCounter[F],
counter: Counter[F]
){
def logic(item: Item): F[Unit] =
for {
_ <- itemsCounter.addItem(item)
c <- counter.get
_ <- Console[F].println(s"Number of items: $c")
} yield ()
}
def program(counter: Counter[IO]): IO[Unit] =
for {
_ <- display(counter)
_ <- counter.increment
_ <- display(counter)
_ <- repeat(5){ counter.increment }
_ <- display(counter)
} yield ()
def display(counter: Counter[IO]): IO[Unit] =
counter.get.flatMap(IO.println)
def repeat(n: Int)(action: IO[Unit]): IO[Unit] =
action.replicateA(n).void
def program(c: Counter[IO]): IO[Unit] =
for {
_ <- c.get.flatMap(IO.println)
_ <- c.increment
_ <- c.get.flatMap(IO.println)
_ <- c.increment.replicateA(5).void
_ <- c.get.flatMap(IO.println)
} yield ()
REFACTOR
Let’s refactor a little bit the first program that we came across.
On the next slide, we are going to take that program and wrap it in a tiny application that can be executed.
We are also going to use a little bit of UML, to show how the program uses an algebra (i.e. an interface) implemented by an interpreter.
To do this, we are going to slightly abuse UML, along the lines described by Martin Fowler in https://martinfowler.com/bliki/BallAndSocket.html.
Program
Algebra
Interface Algebra is used (required) by Program.
Interface Algebra is realized (implemented) by Interpreter.
Interpreter
Algebra
Program Interpreter
Algebra
Interface Algebra, which is realized (implemented)
by Interpreter, is used (required) by Program.
Algebra
ball
Algebra interface.
In a further twist, I’ll also be putting Algebra inside the ball.
socket
mated socket and ball
valid UML notation
invalid
UML
notation
@philip_schwarz
import cats.effect.IO
import cats.effect.IOApp.Simple
object Application extends Simple {
// prints out 0, 1, 6 when executed
override def run: IO[Unit] =
Counter.make[IO].flatMap(program(_))
private def program(counter: Counter[IO]): IO[Unit] =
for {
_ <- display(counter)
_ <- counter.increment
_ <- display(counter)
_ <- repeat(5){ counter.increment }
_ <- display(counter)
} yield ()
private def display(counter: Counter[IO]): IO[Unit] =
counter.get.flatMap(IO.println)
private def repeat(n: Int)(action: IO[Unit]): IO[Unit] =
action.replicateA(n).void
}
trait Counter[F[_]] {
def increment: F[Unit]
def get: F[Int]
}
Algebra
Program
object Counter {
def make[F[_]: Functor: Ref.Make]: F[Counter[F]] =
Ref.of[F, Int](0).map { ref =>
new Counter[F] {
def increment: F[Unit] =
ref.update(_ + 1)
def get: F[Int] =
ref.get
}
}
}
Interpreter
Next, we take the mastermind program that we saw earlier, and
use it in a tiny application that tests it a bit.
Because we are going to need it in the next slide, here is a much
pared down version of the Item referenced by the program.
import io.estatico.newtype.macros.newtype
import java.util.UUID
object item {
@newtype case class ItemId(value: UUID)
@newtype case class ItemName(value: String)
@newtype case class ItemDescription(value: String)
case class Item(uuid: ItemId, name: ItemName, description: ItemDescription)
}
class ItemsCounter[F[_]: Apply](
counter: Counter[F],
items: Items[F]
){
def addItem(item: Item): F[Unit] =
items.add(item) *>
counter.increment
}
class MasterMind[F[_]: Console: Monad](
itemsCounter: ItemsCounter[F],
counter: Counter[F]
){
def logic(item: Item): F[Unit] =
for {
_ <- itemsCounter.addItem(item)
c <- counter.get
_ <- Console[F].println(s"Number of items: $c")
} yield ()
}
trait Counter[F[_]] {
def increment: F[Unit]
def get: F[Int]
}
trait Items[F[_]] {
def getAll: F[List[Item]]
def add(item: Item): F[Unit]
}
object TestCounter {
def make[F[_]: Functor: Ref.Make]: F[Counter[F]] =
Ref.of[F, Int[(0).map { ref =>
new Counter[F] {
def increment: F[Unit] =
ref.update(_ + 1)
def get: F[Int] =
ref.get
}
}
object TestItems {
def make[F[_]: Functor : Ref.Make]: F[Items[F]] =
Ref.of[F, Map[ItemId, Item]](Map.empty).map { ref =>
new Items[F] {
def getAll: F[List[Item]] =
ref.get.map(_.values.toList)
def add(item: Item): F[Unit] =
ref.update(_ + (item.uuid -> item))
}
}
}
import cats.effect.IO
import cats.effect.IOApp.Simple
import item.{Item, ItemDescription, ItemId, ItemName}
import java.util.UUID
object TestMasterMindProgram extends Simple {
val item = Item(
ItemId(UUID.fromString("0c69d914-6ff6-11ee-b962-0242ac120002")),
ItemName("Practical FP in Scala"),
ItemDescription("A great book")
)
override def run: IO[Unit] = {
for {
counter <- TestCounter.make[IO]
items <- TestItems.make[IO]
itemsCounter = new ItemsCounter[IO](counter, items)
masterMind = new MasterMind(itemsCounter, counter)
itemsBefore <- items.getAll
countBefore <- counter.get
_ = assert(itemsBefore.isEmpty)
_ = assert(countBefore == 0)
_ <- masterMind.logic(item)
itemsAfter <- items.getAll
countAfter <- counter.get
_ = assert(itemsAfter.sameElements(List(item)))
_ = assert(countAfter == 1)
} yield ()
}
}
Algebra
Interpreter
Program
Algebra
Interpreter
Program
Program
Of course this was just a quick introduction to the Tagless Final technique.
See the book for much more depth, and many other important aspects of using the technique.
The next slide contains just a taster.
Some might question the decision to invest in this technique for a business application, claiming it entails great complexity.
This is a fair concern but let’s ask ourselves, what’s the alternative? Using IO directly in the entire application? By all means,
this could work, but at what cost? At the very least, we would be giving up on parametricity† and the principle of least power.
There is a huge mix of concerns. How can we possibly
reason about this function? How can we even test it? We
got ourselves into a very uncomfortable situation.
Now let’s compare it against the abstract equivalent of
it. Instead of performing side-effects, we have now
typeclass constraints and capabilities.
Teams making use of this technique will immediately understand that all we can do in the body of the constrained function is to compose
Counter, Log, and Time actions sequentially as well as to use any property made available by the Monad constraint. It is true, however, that
the Scala compiler does not enforce it so this is up to the discipline of the team.
Since Scala is a hybrid language, the only thing stopping us from running wild side-effects in this function is self-discipline and peer reviews.
However, good practices are required in any team for multiple purposes, so I would argue it is not necessarily a bad thing, as we can do the same
thing in programs encoded directly in IO.
† https://en.wikipedia.org/wiki/Parametricity
That’s all. I hope you found it useful.
1 of 16

Recommended

Java modular extension for operator overloading by
Java modular extension for operator overloadingJava modular extension for operator overloading
Java modular extension for operator overloadingijpla
394 views10 slides
Functional Effects - Part 1 by
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1Philip Schwarz
2K views8 slides
Function by
FunctionFunction
FunctionMuhammadBakri13
581 views27 slides
Chapter 1.ppt by
Chapter 1.pptChapter 1.ppt
Chapter 1.pptethiouniverse
5 views65 slides
Presentation 2.pptx by
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptxziyadaslanbey
3 views11 slides
Algorithm and c language by
Algorithm and c languageAlgorithm and c language
Algorithm and c languagekamalbeydoun
1.3K views43 slides

More Related Content

Similar to Tagless Final Encoding - Algebras and Interpreters and also Programs

Tutorial basic of c ++lesson 1 eng ver by
Tutorial basic of c ++lesson 1 eng verTutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verQrembiezs Intruder
289 views7 slides
Java 8-revealed by
Java 8-revealedJava 8-revealed
Java 8-revealedHamed Hatami
674 views27 slides
Functions in c++ by
Functions in c++Functions in c++
Functions in c++Asaye Dilbo
351 views15 slides
Function by
FunctionFunction
FunctionRajat Patel
585 views20 slides
Presentation 2 (1).pdf by
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdfziyadaslanbey
10 views16 slides

Similar to Tagless Final Encoding - Algebras and Interpreters and also Programs(20)

Functions in c++ by Asaye Dilbo
Functions in c++Functions in c++
Functions in c++
Asaye Dilbo351 views
Modeling Aspects with AP&P Components by mukhtarhudaya
Modeling Aspects with AP&P ComponentsModeling Aspects with AP&P Components
Modeling Aspects with AP&P Components
mukhtarhudaya232 views
Introduction to c programming by AMAN ANAND
Introduction to c programmingIntroduction to c programming
Introduction to c programming
AMAN ANAND98 views
InstructionsYou introduce functions with a program that call by hildredzr1di
InstructionsYou introduce functions with a program that callInstructionsYou introduce functions with a program that call
InstructionsYou introduce functions with a program that call
hildredzr1di3 views
C programming languag for cse students by Abdur Rahim
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse students
Abdur Rahim389 views
Mastering Python lesson 4_functions_parameters_arguments by Ruth Marvin
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin438 views
The Expression Problem - Part 2 by Philip Schwarz
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
Philip Schwarz650 views
Dive into Python Functions Fundamental Concepts.pdf by SudhanshiBakre1
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre15 views
Functions-Computer programming by nmahi96
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi9680 views
265 ge8151 problem solving and python programming - 2 marks with answers by vithyanila
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answers
vithyanila457 views
Functional Programming in Scala: Notes by Roberto Casadei
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
Roberto Casadei1.6K views

More from Philip Schwarz

Scala Left Fold Parallelisation - Three Approaches by
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three ApproachesPhilip Schwarz
39 views44 slides
Fusing Transformations of Strict Scala Collections with Views by
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsPhilip Schwarz
20 views28 slides
A sighting of sequence function in Practical FP in Scala by
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaPhilip Schwarz
27 views4 slides
N-Queens Combinatorial Puzzle meets Cats by
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsPhilip Schwarz
33 views386 slides
Kleisli composition, flatMap, join, map, unit - implementation and interrelat... by
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Philip Schwarz
10 views16 slides
The aggregate function - from sequential and parallel folds to parallel aggre... by
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...Philip Schwarz
262 views31 slides

More from Philip Schwarz(20)

Scala Left Fold Parallelisation - Three Approaches by Philip Schwarz
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three Approaches
Philip Schwarz39 views
Fusing Transformations of Strict Scala Collections with Views by Philip Schwarz
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
Philip Schwarz20 views
A sighting of sequence function in Practical FP in Scala by Philip Schwarz
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in Scala
Philip Schwarz27 views
N-Queens Combinatorial Puzzle meets Cats by Philip Schwarz
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets Cats
Philip Schwarz33 views
Kleisli composition, flatMap, join, map, unit - implementation and interrelat... by Philip Schwarz
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Philip Schwarz10 views
The aggregate function - from sequential and parallel folds to parallel aggre... by Philip Schwarz
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
Philip Schwarz262 views
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske... by Philip Schwarz
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
Philip Schwarz249 views
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha... by Philip Schwarz
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Philip Schwarz826 views
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t... by Philip Schwarz
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Philip Schwarz1.2K views
Jordan Peterson - The pursuit of meaning and related ethical axioms by Philip Schwarz
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axioms
Philip Schwarz141 views
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c... by Philip Schwarz
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Philip Schwarz100 views
Defining filter using (a) recursion (b) folding with S, B and I combinators (... by Philip Schwarz
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Philip Schwarz78 views
The Sieve of Eratosthenes - Part 1 - with minor corrections by Philip Schwarz
The Sieve of Eratosthenes - Part 1 - with minor correctionsThe Sieve of Eratosthenes - Part 1 - with minor corrections
The Sieve of Eratosthenes - Part 1 - with minor corrections
Philip Schwarz110 views
The Sieve of Eratosthenes - Part 1 by Philip Schwarz
The Sieve of Eratosthenes - Part 1The Sieve of Eratosthenes - Part 1
The Sieve of Eratosthenes - Part 1
Philip Schwarz1K views
The Uniform Access Principle by Philip Schwarz
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz542 views
Computer Graphics in Java and Scala - Part 1b by Philip Schwarz
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
Philip Schwarz261 views
Computer Graphics in Java and Scala - Part 1 by Philip Schwarz
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
Philip Schwarz403 views
The Expression Problem - Part 1 by Philip Schwarz
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
Philip Schwarz1.2K views
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac... by Philip Schwarz
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz268 views
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ... by Philip Schwarz
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Philip Schwarz312 views

Recently uploaded

DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...Deltares
16 views12 slides
MariaDB stored procedures and why they should be improved by
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improvedFederico Razzoli
8 views32 slides
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)... by
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...Deltares
9 views34 slides
Applying Platform Engineering Thinking to Observability.pdf by
Applying Platform Engineering Thinking to Observability.pdfApplying Platform Engineering Thinking to Observability.pdf
Applying Platform Engineering Thinking to Observability.pdfNatan Yellin
12 views16 slides
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...Deltares
7 views24 slides
El Arte de lo Possible by
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo PossibleNeo4j
34 views35 slides

Recently uploaded(20)

DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares16 views
MariaDB stored procedures and why they should be improved by Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)... by Deltares
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
Deltares9 views
Applying Platform Engineering Thinking to Observability.pdf by Natan Yellin
Applying Platform Engineering Thinking to Observability.pdfApplying Platform Engineering Thinking to Observability.pdf
Applying Platform Engineering Thinking to Observability.pdf
Natan Yellin12 views
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares7 views
El Arte de lo Possible by Neo4j
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo Possible
Neo4j34 views
Neo4j : Graphes de Connaissance, IA et LLMs by Neo4j
Neo4j : Graphes de Connaissance, IA et LLMsNeo4j : Graphes de Connaissance, IA et LLMs
Neo4j : Graphes de Connaissance, IA et LLMs
Neo4j46 views
Advanced API Mocking Techniques by Dimpy Adhikary
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking Techniques
Dimpy Adhikary18 views
Mark Simpson - UKOUG23 - Refactoring Monolithic Oracle Database Applications ... by marksimpsongw
Mark Simpson - UKOUG23 - Refactoring Monolithic Oracle Database Applications ...Mark Simpson - UKOUG23 - Refactoring Monolithic Oracle Database Applications ...
Mark Simpson - UKOUG23 - Refactoring Monolithic Oracle Database Applications ...
marksimpsongw74 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Cycleops - Automate deployments on top of bare metal.pptx by Thanassis Parathyras
Cycleops - Automate deployments on top of bare metal.pptxCycleops - Automate deployments on top of bare metal.pptx
Cycleops - Automate deployments on top of bare metal.pptx
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker by Deltares
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - ParkerDSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
Deltares8 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares12 views
DSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - Prida by Deltares
DSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - PridaDSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - Prida
DSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - Prida
Deltares17 views
Les nouveautés produit Neo4j by Neo4j
 Les nouveautés produit Neo4j Les nouveautés produit Neo4j
Les nouveautés produit Neo4j
Neo4j27 views
How to Make the Most of Regression and Unit Testing.pdf by Abhay Kumar
How to Make the Most of Regression and Unit Testing.pdfHow to Make the Most of Regression and Unit Testing.pdf
How to Make the Most of Regression and Unit Testing.pdf
Abhay Kumar10 views

Tagless Final Encoding - Algebras and Interpreters and also Programs

  • 1. Tagless Final Encoding Algebras and Interpreters and also Programs an introduction, through the work of Gabriel Volpe Algebras Tagless Final Encoding @philip_schwarz slides by http://fpilluminated.com/
  • 2. This slide deck is a quick, introductory look, at Tagless Final, as explained (in rather more depth than is possible or appropriate here) by Gabriel in his great book: Practical FP in Scala, a Hands- on Approach. In the first six slides, we are going to see Gabriel introduce the key elements of the technique. Tagless final is all about algebras and interpreters. Yet, something is missing when it comes to writing applications… Tagless final is a great technique used to structure purely functional applications. Yes, as you’ll see, to the two official pillars of Tagless Final, i.e. algebras and interpreters, Gabriel adds a third one: programs. Algebras Tagless Final Encoding @philip_schwarz
  • 3. Algebras An algebra describes a new language (DSL) within a host language, in this case, Scala. This is a tagless final encoded algebra; tagless algebra, or algebra for short: a simple interface that abstracts over the effect type using a type constructor F[_]. Do not confuse algebras with typeclasses, which in Scala, happen to share the same encoding. The difference is that typeclasses should have coherent instances, whereas tagless algebras could have many implementations, or more commonly called interpreters. … Overall, tagless algebras seem a perfect fit for encoding business concepts. For example, an algebra responsible for managing items could be encoded as follows. Nothing new, right? This tagless final encoded algebra is merely an interface that abstracts over the effect type. Notice that neither the algebra nor its functions have any typeclass constraint. If you find yourself needing to add a typeclass constraint, such as Monad, to your algebra, what you probably need is a program. The reason being that typeclass constraints define capabilities, which belong in programs and interpreters. Algebras should remain completely abstract. trait Items[F[_]] { def getAll: F[List[Item]] def add(item: Item): F[Unit] } trait Counter[F[_]] { def increment: F[Unit] def get: F[Int] } Tagless algebras should not have typeclass constraints Tips
  • 4. Interpreters We would normally have two interpreters per algebra: one for testing and one for doing real things. For instance, we could have two different implementations of our Counter. A default interpreter using Redis. And a test interpreter using an in-memory data structure. Interpreters help us encapsulate state and allow separation of concerns: the interface knows nothing about the implementation details. Moreover, interpreters can be written either using a concrete datatype such as IO or going polymorphic all the way, as we did in this case. object Counter { @newtype case class RedisKey(value: String) def make[F[_]: Functor]( key: RedisKey, cmd: RedisCommands[F, String, Int] ): Counter[F] = new Counter[F] { def increment: F[Unit] = cmd.increment(key.value).void def get: F[Int] = cmd.get(key.value).map(_.getOrElse(0)) } } def testCounter[F[_]]( ref: Ref[F, Int] ): Counter[F] = new Counter[F] { def increment: F[Unit] = ref.update(_ + 1) def get: F[Int] = ref.get }
  • 5. We are currently working our way through slides containing excerpts from Chapter 2: Tagless Final Encoding. The next slide is an exception in that it contains an excerpt from Chapter1: Best Practices, which has already introduced (in a less formal way), the concept of an interpreter for the Counter trait (without yet referring to the latter as an algebra).
  • 6. In-memory counter Let’s say we need an in-memory counter that needs to be accessed and modified by other components. Here is what our interface could look like. It has a higher-kinded type F[_], representing an abstract effect, which most of the time ends up being IO, but it could really be any other concrete type that fits the shape. Next, we need to define an interpreter in the companion object of our interface, in this case using a Ref. We will talk more about it in the next section. … Moving on, it’s worth highlighting that other programs will interact with this counter solely via its interface. E.g. // prints out 0,1,6 when executed def program(c: Counter[IO]): IO[Unit] = for { _ <- c.get.flatMap(IO.println) _ <- c.increment _ <- c.get.flatMap(IO.println) _ <- c.increment.replicateA(5).void _ <- c.get.flatMap(IO.println) } yield () In the next chapter, we will discuss whether it is best to pass the dependency implicitly or explicitly. object Counter { def make[F[_]: Functor: Ref.Make]: F[Counter[F]] = Ref.of[F, Int](0).map { ref => new Counter[F] { def increment: F[Unit] = ref.update(_ + 1) def get: F[Int] = ref.get } } } trait Counter[F[_]] { def increment: F[Unit] def get: F[Int] } import cats.Functor import cats.effect.kernel.Ref import cats.syntax.functor._
  • 7. Programs Tagless final is all about algebras and interpreters. Yet, something is missing when it comes to writing applications: we need to use these algebras to describe business logic, and this logic belongs in what I like to call programs. Although it is not an official name – and it is not mentioned in the original tagless final paper – it is how we will be referring to such interfaces in this book. Say we need to increase a counter every time there is a new item added. We could encode it as follows. Observe the characteristics of this program. It is pure business logic, and it holds no state at all, which in any case, must be encapsulated in the interpreters. Notice the typeclass constraints as well; it is a good practice to have them in programs instead of tagless algebras. … Moreover, we can discuss typeclass constraints. In this case, we only need Apply to use *> (alias for productR). However, it would also work with Applicative or Monad. The rule of thumb is to limit ourselves to adopt the least powerful typeclass that gets the job done. It is worth mentioning that Apply itself doesn’t specify the semantics of composition solely with this constraint, *> might combine its arguments sequentially or parallelly, depending on the underlying typeclass instance. To ensure our composition is sequential, we could use FlatMap instead of Apply. class ItemsCounter[F[_]: Apply]( counter: Counter[F], items: Items[F] ){ def addItem(item: Item): F[Unit] = items.add(item) *> counter.increment } Programs can make use of algebras and other programs Notes When adding a typeclass constraint, remember about the principle of least power Tips
  • 8. Other kinds of programs might be directly encoded as functions. Furthermore, we could have programs composed of other programs. Whether we encode programs in one way or another, they should describe pure business logic and nothing else. The question is: what is pure business logic? We could try and define a set of rules to abide by. It is allowed to: • Combine pure computations in terms of tagless algebras and programs. – Only doing what our effect constraints allows us to do. • Perform logging (or console stuff) only via a tagless algebra. – In Chapter 8, we will see how to ignore logging or console stuff in tests, which are most of the time irrelevant in such context. You can use this as a reference. However, the answer should come up as a collective agreement within your team. def program[F[_]: Console: Monad]: F[Unit] = for { _ <- Console[F].println("Enter your name: ") n <- Console[F].readLine _ <- Console[F].println(s"Hello $n!") } yield () class MasterMind[F[_]: Console: Monad]( itemsCounter: ItemsCounter[F], counter: Counter[F] ){ def logic(item: Item): F[Unit] = for { _ <- itemsCounter.addItem(item) c <- counter.get _ <- Console[F].println(s"Number of items: $c") } yield () }
  • 9. def program(counter: Counter[IO]): IO[Unit] = for { _ <- display(counter) _ <- counter.increment _ <- display(counter) _ <- repeat(5){ counter.increment } _ <- display(counter) } yield () def display(counter: Counter[IO]): IO[Unit] = counter.get.flatMap(IO.println) def repeat(n: Int)(action: IO[Unit]): IO[Unit] = action.replicateA(n).void def program(c: Counter[IO]): IO[Unit] = for { _ <- c.get.flatMap(IO.println) _ <- c.increment _ <- c.get.flatMap(IO.println) _ <- c.increment.replicateA(5).void _ <- c.get.flatMap(IO.println) } yield () REFACTOR Let’s refactor a little bit the first program that we came across.
  • 10. On the next slide, we are going to take that program and wrap it in a tiny application that can be executed. We are also going to use a little bit of UML, to show how the program uses an algebra (i.e. an interface) implemented by an interpreter. To do this, we are going to slightly abuse UML, along the lines described by Martin Fowler in https://martinfowler.com/bliki/BallAndSocket.html. Program Algebra Interface Algebra is used (required) by Program. Interface Algebra is realized (implemented) by Interpreter. Interpreter Algebra Program Interpreter Algebra Interface Algebra, which is realized (implemented) by Interpreter, is used (required) by Program. Algebra ball Algebra interface. In a further twist, I’ll also be putting Algebra inside the ball. socket mated socket and ball valid UML notation invalid UML notation @philip_schwarz
  • 11. import cats.effect.IO import cats.effect.IOApp.Simple object Application extends Simple { // prints out 0, 1, 6 when executed override def run: IO[Unit] = Counter.make[IO].flatMap(program(_)) private def program(counter: Counter[IO]): IO[Unit] = for { _ <- display(counter) _ <- counter.increment _ <- display(counter) _ <- repeat(5){ counter.increment } _ <- display(counter) } yield () private def display(counter: Counter[IO]): IO[Unit] = counter.get.flatMap(IO.println) private def repeat(n: Int)(action: IO[Unit]): IO[Unit] = action.replicateA(n).void } trait Counter[F[_]] { def increment: F[Unit] def get: F[Int] } Algebra Program object Counter { def make[F[_]: Functor: Ref.Make]: F[Counter[F]] = Ref.of[F, Int](0).map { ref => new Counter[F] { def increment: F[Unit] = ref.update(_ + 1) def get: F[Int] = ref.get } } } Interpreter
  • 12. Next, we take the mastermind program that we saw earlier, and use it in a tiny application that tests it a bit. Because we are going to need it in the next slide, here is a much pared down version of the Item referenced by the program. import io.estatico.newtype.macros.newtype import java.util.UUID object item { @newtype case class ItemId(value: UUID) @newtype case class ItemName(value: String) @newtype case class ItemDescription(value: String) case class Item(uuid: ItemId, name: ItemName, description: ItemDescription) }
  • 13. class ItemsCounter[F[_]: Apply]( counter: Counter[F], items: Items[F] ){ def addItem(item: Item): F[Unit] = items.add(item) *> counter.increment } class MasterMind[F[_]: Console: Monad]( itemsCounter: ItemsCounter[F], counter: Counter[F] ){ def logic(item: Item): F[Unit] = for { _ <- itemsCounter.addItem(item) c <- counter.get _ <- Console[F].println(s"Number of items: $c") } yield () } trait Counter[F[_]] { def increment: F[Unit] def get: F[Int] } trait Items[F[_]] { def getAll: F[List[Item]] def add(item: Item): F[Unit] } object TestCounter { def make[F[_]: Functor: Ref.Make]: F[Counter[F]] = Ref.of[F, Int[(0).map { ref => new Counter[F] { def increment: F[Unit] = ref.update(_ + 1) def get: F[Int] = ref.get } } object TestItems { def make[F[_]: Functor : Ref.Make]: F[Items[F]] = Ref.of[F, Map[ItemId, Item]](Map.empty).map { ref => new Items[F] { def getAll: F[List[Item]] = ref.get.map(_.values.toList) def add(item: Item): F[Unit] = ref.update(_ + (item.uuid -> item)) } } } import cats.effect.IO import cats.effect.IOApp.Simple import item.{Item, ItemDescription, ItemId, ItemName} import java.util.UUID object TestMasterMindProgram extends Simple { val item = Item( ItemId(UUID.fromString("0c69d914-6ff6-11ee-b962-0242ac120002")), ItemName("Practical FP in Scala"), ItemDescription("A great book") ) override def run: IO[Unit] = { for { counter <- TestCounter.make[IO] items <- TestItems.make[IO] itemsCounter = new ItemsCounter[IO](counter, items) masterMind = new MasterMind(itemsCounter, counter) itemsBefore <- items.getAll countBefore <- counter.get _ = assert(itemsBefore.isEmpty) _ = assert(countBefore == 0) _ <- masterMind.logic(item) itemsAfter <- items.getAll countAfter <- counter.get _ = assert(itemsAfter.sameElements(List(item))) _ = assert(countAfter == 1) } yield () } } Algebra Interpreter Program Algebra Interpreter Program Program
  • 14. Of course this was just a quick introduction to the Tagless Final technique. See the book for much more depth, and many other important aspects of using the technique. The next slide contains just a taster.
  • 15. Some might question the decision to invest in this technique for a business application, claiming it entails great complexity. This is a fair concern but let’s ask ourselves, what’s the alternative? Using IO directly in the entire application? By all means, this could work, but at what cost? At the very least, we would be giving up on parametricity† and the principle of least power. There is a huge mix of concerns. How can we possibly reason about this function? How can we even test it? We got ourselves into a very uncomfortable situation. Now let’s compare it against the abstract equivalent of it. Instead of performing side-effects, we have now typeclass constraints and capabilities. Teams making use of this technique will immediately understand that all we can do in the body of the constrained function is to compose Counter, Log, and Time actions sequentially as well as to use any property made available by the Monad constraint. It is true, however, that the Scala compiler does not enforce it so this is up to the discipline of the team. Since Scala is a hybrid language, the only thing stopping us from running wild side-effects in this function is self-discipline and peer reviews. However, good practices are required in any team for multiple purposes, so I would argue it is not necessarily a bad thing, as we can do the same thing in programs encoded directly in IO. † https://en.wikipedia.org/wiki/Parametricity
  • 16. That’s all. I hope you found it useful.