Skip to content

Functional Programming

  • Side Effect: An unpredictable change in the state of the environment that would break the substitution model and function will no longer be referentially transparent.
  • When a side-effect is tracked and controlled we call it an effect (Scalar Conf 2025)
    • functions returning effectful results are hard to compose
  • Effects types:
    • Monadic: They use monads (flatMap and map) to compose functions using. e.g Cats-Effect and ZIO
    • Algebraic: Function type tells exactly what effects are being used, and can handle each effect separately

How can you extend a system with both new data cases (variants) and new operations (behaviors) without modifying existing code, recompiling, or breaking type safety?

For example, consider an algebra that allows adding two number.

  • A new data case, would be ability to extend algebra to include multiplication
  • A new operation, would be ability to print the addition as a string expression

tagless final solves the expression problem by, defining a new capability, for example multiplication as a separate trait, and creating a new trait that extends the original trait and the multiplication capability trait

// The base capability algebra
trait BaseAlgebra[F[_]]:
def num(n: Int): F[Int]
def add(lhs: F[Int], rhs: F[Int]): F[Int]
// program based only on the algebra
def program[F[_]](implicit alg: BaseAlgebra[F]): F[Int] =
alg.add(alg.num(5), alg.num(10))
// Base implementation for evaluation, interpreter #1
import cats.Id
val evalBase = new BaseAlgebra[Id]:
def num(n: Int): Id[Int] = n
def add(lhs: Id[Int], rhs: Id[Int]): Id[Int] = lhs + rhs
// Running the base program: returns 15
val result1 = program(evalBase)

Add pretty printing operation

type StringOut[A] = String
// A brand-new operation implemented via a new interpreter
val printBase = new BaseAlgebra[StringOut]:
def num(n: Int): StringOut[Int] = n.toString
def add(lhs: StringOut[Int], rhs: StringOut[Int]): StringOut[Int] = s"($lhs + $rhs)"
// Running the EXACT SAME program with the new operation: returns "(5 + 10)"
val result2 = program(printBase)

Add ability to multiply two numbers

// A completely separate data case/capability added downstream
trait MulAlgebra[F[_]]:
def mul(lhs: F[Int], rhs: F[Int]): F[Int]
// extend the two interpreters ...
// This program requires both independent algebras to exist
def extendedProgram[F[_]: {BaseAlgebra as base, MulAlgebra as extra}]: F[Int] =
base.add(base.num(2), extra.mul(base.num(3), base.num(4)))
  • A category S consists of
    • objects A, B, C etc.
    • morphisms (or arrows) that map between different objects and have two fundamental properties
      • composition: morphisms h . (g . f) == (h . g) . f
      • identity: f . id == f Category Theory
  • In Scala, objects correspond to Types and morphisms correspond to pure functions, associative composition is similar to andThen
  • A Functor is a structure preserving mapping between categories
    • In Scala, since we always work with types and functions, functors are always in the same category, ie, endofunctors
  • Tagless Final encoding uses functions whereas Initial encoding uses data types
    • Initial: hard to extend algebra (add operations), Final: hard to add interpreters
  • Anamorphism: A higher-order function that generates a complex data structure from a single seed value. Also known as unfold
  • Catamorphism: Reduces a structure to a value, opposite of anamorphism. Also known as fold