Skip to content

Functional Domain Modeling

A domain model built from pure functions over domain types, with side effects kept out of the domain logic and behavior described as algebras with separate interpreters.

The excerpts below are from Debasish Ghosh’s Functional and Reactive Domain Modeling (Manning, 2017, PDF), which builds a banking domain in Scala. The listing and figure numbers refer to that book.

The types themselves (sums, simple values, identity) are in Domain Modeling With Types, and the states a workflow moves through in Workflows And States. The ways of supplying interpreters and effects to an algebra (Reader, tagless final, free monads, ZIO, algebraic effects) are in Effects And Interpreters.

Ghosh’s design principles

Ghosh ends the book (section 10.2, “Rehashing core principles for functional domain modeling”) with seven principles, which work as a map of this note:

  1. Think in expressions. Build behaviour from expressions that compose, not from statements that mutate.
  2. Abstract early, evaluate late. Describe the computation first and run it at the edge.
  3. Use the least powerful abstraction that fits. A more powerful abstraction is more specialized and so reusable in fewer places: monads are more powerful than applicatives, so prefer applicatives where they’re enough.
  4. Publish what to do, hide how to do it within combinators. The API states the domain operations; the combinators decide how they’re carried out.
  5. Decouple the algebra from its implementation, with modules, type classes or free monads.
  6. Isolate bounded contexts.
  7. Prefer futures to actors, because futures compose and actors don’t.

The sections below follow roughly the same order.

Entities, value objects and composition

Every entity or value object in a domain model has a definite lifecycle pattern. In DDD terms the lifecycle has three stages: creation (factories), participation in behaviors (an aggregate changing state), and persistence and retrieval (repositories). In a functional model each stage is a function. Creation is a function from raw inputs to a valid object or an error, a behavior is a function from an object to a new object, and persistence is an effect kept at the edge (next section). Entities differ from value objects only in having an identity that survives those changes; see Domain Modeling With Types.

Concretely, the creational pattern in a functional model is a smart constructor: one entry point that validates, with the case class constructor itself made private, and errors returned as values (Either, Try) instead of thrown. When the fields are independent, the errors should accumulate, so that a form with three bad fields reports all three (see Composing effectful operations below). This is Parse Dont Validate applied to entities.

This is the first step toward appreciating the fact that functional programming is based on function composition, which in turn is similar to the way we treat functions in mathematics. This is known as the compositionality property of functional programs.

Compositionality means you can understand the whole from its parts and the way they’re combined: if f and g are pure, f andThen g holds no surprises that aren’t already in f or g. An analogy is LEGO bricks versus a glued model: bricks can be recombined because each one’s behavior doesn’t depend on what it’s attached to. Hidden state and side effects are the glue.

Keep effects out of the domain

The main threat to compositionality is entangling domain rules and side effects in one function. In the book’s example (listing 1.9), openCheckingAccount first calls Verifications.verifyRecord, an identity check that may go through an external web service and throws if it fails, and only then builds the account. So the domain rule (what a valid new account is) can’t be tested without that service, and a failure of an outside system surfaces as an exception in the middle of domain logic:

Managing side effects is an area that can make a big difference between compositional domain models and noncompositional ones. A model that’s not compositional often suffers from the baggage of boilerplates and glue code. It becomes a maintenance nightmare – difficult to manage and extend. But after you’ve isolated side effects from pure logic, you can write code that offers better compositionality. In our example, openCheckingAccount is once again pure logic.

After the refactoring, verification is its own function, verifyCustomer, which returns its result as a value, and openCheckingAccount only builds the account. The two are glued with verifyCustomer(cust).map(c => openCheckingAccount(c, date)). Gary Bernhardt’s name for this shape is “functional core, imperative shell”.

Exceptions and latency can then be managed as effects that compose alongside the pure abstractions by putting the effect in the return type. A function that may fail returns Try[Account] or Either[AccountError, Account] instead of throwing, and a function that takes time returns Future[Account] or IO[Account] instead of blocking. The signature now says “this can fail” or “this is slow”, and flatMap chains these values just like ordinary ones. Caveat: Scala’s Future schedules work eagerly on construction instead of describing it for later execution, so val p = Future(println("hi")); p.flatMap(_ => p) prints once whereas inlining p prints twice, breaking substitution in a way lazy IO avoids (Rosien, PDF, section 1.3.2).

The catch is stacking: Future[Either[AccountError, Account]] composes clumsily, which is the problem that monad transformers, ZIO’s ZIO[R, E, A] and tagless final each try to solve (Effects And Interpreters).

The object-oriented and production-FP architecture literature reaches the same shape from different directions:

  • Dependency inversion. Percival and Gregory (PDF, chapter 2) argue that hexagonal architecture, ports and adapters, onion architecture and clean architecture are different names for the same thing, and that they all come down to the dependency inversion principle: the domain defines the interfaces it needs, and infrastructure implements them. A port is an algebra and an adapter is an interpreter.
  • The dependency rule. Martin (PDF, chapter 22, “The Clean Architecture”) states the Dependency Rule as source-code dependencies pointing only inwards, towards the policies. His Humble Object pattern (chapter 23) is the imperative shell: a thin, hard-to-test part that does I/O, next to a testable part that holds the logic.
  • A pure domain layer. Granin (PDF, section 2.2.1, “Architecture layers”) makes the domain layer pure and the impure layer as small as possible, in the same spirit.
  • Three layers and inverted mocks. Parsons (PDF, chapters 6-7) splits a Haskell service into Layer 1 (ReaderT over IO for orchestration), a thin Layer 2 of narrow domain capability interfaces, and Layer 3 of pure business logic. Rather than mocking effects, his default move (section 7.1, “Decomposing Effects”) is to run input effects before Layer 3 and pass their results as plain parameters, and to return output effects from Layer 3 as plain data – often a simple command sum type or list of commands rather than a free monad (section 6.3).

Which dependencies count as effects? Seemann’s test for a volatile dependency (PDF, section 1.3.2) is a good one, though it was written for object-oriented DI: a dependency is volatile if it needs a runtime environment set up (a database, a message queue, a web service, the file system), if it doesn’t exist yet or is still being developed, if it isn’t installed on every developer’s machine, or if it’s nondeterministic (the clock, random numbers). Those are the things to push to the edge. Seemann himself notes that DI as he describes it may not be the best solution in functional languages, where passing functions and returning decisions as data often replace it.

Modules as algebras

A module, as defined in a functional domain model, is a collection of functions that operate on a set of types and honor a set of invariants known as domain rules. In mathematical terms, this is known as the algebra of the module. Note that we haven’t yet talked about the implementation, which is not part of the algebra. It’s the contract the behaviors publish that forms the algebra of the API. Implementation comes later in the lifecycle and takes the form of an interpreter of your algebra.

In mathematics an algebra is a set of types, operations on them, and laws the operations obey: integers with + and 0, where x + 0 = x and + is associative. The analogy is the rules of chess versus a chess set. The rules (the algebra) say which moves are legal; a wooden set, a screen or a correspondence game by post (interpreters) are different ways of carrying them out. Critique: in most codebases the laws are never written down, and then “algebra” just means “interface”. The laws are what make the word worth using.

Ghosh does write them down, later in the book. Section 9.4 (“Revisiting the algebra of your model”) restates the algebra as three parts: the types that model entities and value objects, the functions over them grouped into modules, and the laws, which are the business rules. Section 9.5 then turns the laws into property-based tests with ScalaCheck, so they’re checked against generated data rather than documented. That’s the answer to the critique above: an algebra whose laws are tested is more than an interface.

In listing 3.1, Ghosh defines the API by parameterizing AccountService on the domain types (Account, Amount, Balance) rather than fixing concrete classes up front:

trait AccountService[Account, Amount, Balance] {
  def open(no: String, name: String, openDate: Option[Date]): Try[Account]
  def close(account: Account, closeDate: Option[Date]): Try[Account]
  def debit(account: Account, amount: Amount): Try[Account]
  def credit(account: Account, amount: Amount): Try[Account]
  def balance(account: Account): Try[Balance]
}

Here Account, Amount and Balance are type parameters, not concrete classes. The service doesn’t commit to what an account is, so the API can be designed, reasoned about and even tested against laws (crediting and then debiting the same amount leaves the balance unchanged) before any representation exists. It’s an interface that abstracts over the data as well as the implementation, like writing a board game’s rules before choosing the pieces.

The cost is noise: three type parameters on every use. Scala’s abstract type members are the alternative (see Scala Type System).

Why abstract the types at all? Ghosh’s “ladder of goodness” (section 4.3, figure 4.4) gives the argument in steps: pure functions let you compose freely; adding static types gives you algebraic abstractions, functions over types that obey laws; making the functions generic over types gives parametricity; and if you don’t leak implementation details (specialized types, exceptions), parametricity gives you free theorems, properties that hold for every implementation because of the type alone (Wadler’s “Theorems for Free”). A function def f[A](xs: List[A]): List[A] can only rearrange, drop or duplicate elements, because it knows nothing else about A. The more generic the signature, the fewer implementations it has, and the fewer tests it needs (section 9.2.3).

Ghosh’s chapter 4 (“Functional patterns for domain models”) applies the same idea to design patterns. An object-oriented pattern is a recipe you re-implement each time. A functional pattern is a generic algebra with laws, written once, plus an interpreter for each context: a Monoid (an associative op with an identity zero) is defined once, and Money becomes a monoid by interpreting zero and op for money, which immediately gives you summing over any collection of amounts.

Composing effectful operations

To chain the methods of AccountService in a sequence of operations, the return type of each service function supports the monadic model of computation (figure 3.2), expressed in Scala as a for-comprehension:

for {
  a <- open("a-123", "Alice", None)
  b <- credit(a, 1000)
  c <- debit(b, 200)
} yield c

Each step returns Try[Account]. flatMap hands each result to the next step and stops at the first failure, so the caller writes no error checks between the steps. “Supporting the monadic model” means the return type has flatMap and a way to wrap a plain value (pure). Think of an assembly line where any station can pull the cord: the stations don’t check whether earlier ones succeeded, because the line stops by itself.

Stopping at the first failure is right for a sequence where each step needs the previous result. It’s wrong for checks that are independent of each other, such as validating the fields of a form. Ghosh (sections 4.2.2 and 4.2.3) contrasts the two:

  • Applicative composition runs every check and collects all the failures, because no check depends on another’s result. This is what cats’ Validated does.
  • Monadic composition stops at the first failure, because each step may depend on the one before.

His principle of least power (above) says to use the applicative form whenever the checks are independent: it’s less powerful, so it fits more situations, and it reports more.

Section 4.4 shows a third form, composing whole operations as Kleisli arrows, functions A => F[B] for some effect F. Once each step of a trading workflow has that shape, the workflow reads like the business description:

clientOrders andThen execute andThen allocate

where each arrow takes the previous step’s result and runs in the same effect. The types do the gluing: if one step’s output doesn’t match the next step’s input, the composition doesn’t compile.

Failures and events

Treating failures as values also enables centralized failure handling (without forcing the entire application into a single error-handling module): a failure travels through the chain as a Left or Failure and is handled in one place, usually at the edge, where for example the HTTP layer maps domain errors to status codes. That works best when the errors are an ADT (InsufficientFunds, AccountClosed, …) rather than strings or exceptions, so the handler can match on them exhaustively. Critique: a handler far from the failure only knows what the error value tells it, so a vague error type loses context, while a single application-wide error sum type has the opposite flaw – pure functions end up claiming errors such as FileNotFound that they can never raise, and a caller that handles one case cannot narrow the remaining type without open variants or mapLeft boilerplate (Parsons, PDF, chapter 12, “The Trouble with Typed Errors”). Make error types narrow and specific enough to act on; which errors belong in the type at all is in Domain Modeling With Types.

Domain events play the corresponding role on the success path. A domain event is an immutable record of something that happened, named in the past tense: AccountOpened, MoneyDebited. Because events are ordinary values, they can be stored (event sourcing), replayed to rebuild state, and sent to other parts of the system, which react without the sender knowing who they are. Khononov (PDF, chapter 7) and Kleppmann (PDF, chapter 1) connect this to stream processing and CQRS: the append-only event log is the write-optimized source of truth, and current state, caches and analytics tables are deterministic read projections folded from that log.

Event-based modeling complements the functional model – Event-based programming delineates the “what” from the “how” of your model. And this is also what functional programming encourages. Events are small messages that specify what you want to do, and the handler for the event describes the “how” part. No wonder functional programming and event-driven programming play well together.

Strictly, a message that says what you want done is a command (OpenAccount); an event says what has happened (AccountOpened). The quote above, from Ghosh’s chapter 1, blurs the two, but the split matters in practice: a command can be rejected, while an event is a fact that handlers can only react to (and some domain events, such as a fiscal period closing at midnight, come from time passing rather than from any command; Vernon, PDF, chapter 6). Ghosh makes the split himself in chapter 8 (section 8.3.1, “Commands and events in an event-sourced domain model”): a command builds or updates an aggregate, runs the domain validations and any side effects, and, if all goes well, produces an event that’s appended to the event log, the source of truth. Dean and Crettaz (PDF, chapters 8-9) apply the same functional split to event streams: decision logic is a pure function from events to explicit commands, a separate executor performs the side effects, and pipeline steps use “railway-oriented processing” – applicative validation inside a step, fail-fast flatMap across steps, and failed units written to a failure stream as events carrying the original input for replay. Ghosh’s section 8.4.2 pushes the separation one step further by modeling commands as free monads over events, so that a command is a description of which events it would produce, and the interpreter (section 8.4.3, “Interpreters–hideouts for all the interesting stuff”) is where persistence and other effects happen. Free monads are covered in Effects And Interpreters.

How do the two halves of Ghosh’s title – functional and reactive – fit together? Functional covers everything above: pure domain logic, effects as values, algebraic APIs. Reactive refers to the Reactive Manifesto (2014): responsive, resilient, elastic and message-driven. In practice that means non-blocking calls, streams and actors (Akka, in the book). The two pull in different directions, since actors are stateful and side-effecting. Ghosh’s reconciliation treats the two differently:

  • Streams can be part of the model. Section 6.4.2 (“A graph as a domain pipeline”) models a sequence of domain behaviours as a stream graph, describing it separately from running it, so the pipeline itself is a typed domain value.
  • Actors stay out of the API. Akka actors receive messages of any type and return nothing, so they don’t compose and can’t be reasoned about like functions. Ghosh keeps them for specific jobs, such as guarding mutable state that needs mutual exclusion, and his last principle is to reach for futures first.

Modules and bounded contexts

Ghosh’s chapter 5 (“Modularization of domain models”) groups algebras into modules and modules into bounded contexts (section 5.4, “Aggregate modules at bounded context”). In strategic DDD, a bounded context is the consistency boundary of a ubiquitous language: rather than forcing one enterprise-wide Policy or Lead type onto departments that mean different things by the word, each context keeps its own sharp types and model (Vernon, PDF, chapter 2; Khononov, PDF, chapter 3). Contexts talk to each other through explicit context-mapping contracts – messages in a published language, an open-host service, or a downstream anticorruption layer that translates an upstream model into local types (Vernon, chapter 4; Khononov, chapter 4) – never by sharing domain objects. Ghosh’s chapter 5 also shows how a module’s dependencies, such as an account repository, are supplied: as a Kleisli arrow that reads the repository from its environment (section 5.2.1), or through a free monad interpreted later (section 5.5); see Effects And Interpreters.

What a bounded context’s boundary looks like in types (DTOs, trust boundaries, evolution) is in Types At The Boundary.

Library sources

  • Ghosh 2017, Functional and Reactive Domain Modeling – the source of the excerpts: lifecycle patterns and the openCheckingAccount example (chapter 1), AccountService (listing 3.1) and the monadic model of computation; chapter 4 on functional patterns, applicative and monadic effects, the ladder of goodness and Kleisli composition; chapter 5 on modules and bounded contexts; chapter 6 on streams and actors; chapter 8 on commands, events and event sourcing; sections 9.4-9.5 on laws as properties; section 10.2 on principles; on every section
  • Percival et al. 2020, Architecture Patterns with Python – the dependency inversion principle and ports and adapters (chapter 2), and chapters 8 and 10 on events and on commands with command handlers; on Keep effects out of the domain and Failures and events
  • Martin 2018, Clean Architecture – chapter 22, “The Clean Architecture” (the Dependency Rule), and chapter 23, “Presenters and Humble Objects”; on Keep effects out of the domain
  • Granin 2025, Functional Design and Architecture – section 2.2.1, architecture layers with a pure domain layer; on Keep effects out of the domain
  • Parsons 2023, Production Haskell – chapter 6, “Three Layer Haskell Cake”, and chapter 7 (“Invert Your Mocks”, section 7.1, “Decomposing Effects”), on pushing logic into a pure Layer 3 by passing input effects as parameters and returning output effects as plain command data; chapter 12, “The Trouble with Typed Errors”, on why a monolithic error sum type claims impossible failures and loses narrowing when one case is handled; on Keep effects out of the domain and Failures and events
  • Seemann et al. 2019, Dependency Injection: Principles, Practices, and Patterns – section 1.3.2, “Volatile Dependencies”; on Keep effects out of the domain
  • Pilquist et al. 2023, Functional Programming in Scala, 2nd Edition – section 7.3, “The algebra of an API”, and section 9.1, “Designing an algebra first”: an algebra as functions over types plus laws; on Modules as algebras
  • Hughes 1990, Why Functional Programming Matters – section 2, “An Analogy with Structured Programming”, on how the ways a problem can be divided depend on the glue for recombining the parts, with higher-order functions and lazy evaluation as the two glues functional languages add; on the compositionality paragraph in Entities, value objects and composition
  • Rosien 2021, Essential Effects – section 1.1 on the substitution model, section 1.3 on the Effect Pattern (a type that states both the effect and the result, with description separated from execution), and section 1.3.2 showing why Scala’s Future breaks substitution by scheduling side effects eagerly on construction, unlike Cats Effect IO; on Keep effects out of the domain
  • Schwarz 2019, Monads do not Compose – slides showing that functors and applicatives compose generically but monads don’t, which is why transformers exist; on the stacking point in Keep effects out of the domain
  • Welsh et al. 2020, Scala with Cats – chapter 10’s data-validation case study and chapter 5 on monad transformers; on the applicative point in Composing effectful operations and the stacking point in Keep effects out of the domain
  • Schwarz 2023, Kleisli composition, flatMap, join, map, unit – slides on Kleisli composition and how it relates to flatMap; on Composing effectful operations
  • Wlaschin 2018, Domain Modeling Made Functional – chapter 3, “Code Structure Within a Bounded Context” (onion architecture, with I/O pushed to the edges) and “Workflows Within a Bounded Context” (a workflow returns its events rather than publishing them, and domain events are avoided inside a context); chapter 9, “Injecting Dependencies” (explicit parameters instead of an IoC container); chapter 12, “Pushing Persistence to the Edges” (a pure function returns its decision as a choice type for the edge to act on); on Keep effects out of the domain and Failures and events
  • Serrano Mena 2021, Book of Monads, 2nd Edition – section 13.1.1, “Syntax and Interpretation”, on a custom monad’s operations as its algebra (the Scala term) or syntax, with its interpreters as its semantics, one description run by several interpreters, and analyses over the syntax that capture domain invariants (in State, a get after put x returns x), on Modules as algebras; section 5.3.1, “Kleisli Arrows”, on Kleisli arrows and their composition, on Composing effectful operations
  • Vernon 2016, Domain-Driven Design Distilled – chapter 2 on bounded contexts as semantic boundaries of a ubiquitous language (Policy across underwriting, inspections and claims, and ports and adapters with a technology-free domain model), chapter 4 on context mapping (anticorruption layer, open host service, published language, conformist, shared kernel), and chapter 6 on domain events, commands versus time-triggered events (MarketsClosed), and event sourcing; on Keep effects out of the domain, Failures and events and Modules and bounded contexts
  • Khononov 2021, Learning Domain-Driven Design – chapter 3 on bounded contexts as consistency boundaries for a ubiquitous language (Lead as notification in marketing versus lifecycle in sales) and subdomains as discovered versus bounded contexts as designed; chapter 4 on context-mapping patterns; chapter 7, “Modeling the Dimension of Time”, on event-sourced domain models where the append-only event store is the source of truth and multiple read models are projected from it; chapter 8 on ports and adapters and CQRS; on Keep effects out of the domain, Failures and events and Modules and bounded contexts
  • Dean et al. 2019, Event Streams in Action – chapter 8, “Railway-oriented processing”, combining applicative error accumulation (ValidationNel) within a step, fail-fast flatMap across dependent steps, and failure streams whose events embed the failed input for replay; chapter 9, “Commands”, contrasting imperative-mood commands with past-tense events and splitting decide_and_act into a pure make_decision returning commands and an execute_command performing effects; on Composing effectful operations and Failures and events
  • Kleppmann 2016, Making Sense of Stream Processing – chapter 1, “Events and Stream Processing”, joining DDD event sourcing with stream processing: immutable events as the write-optimized source of truth and databases, caches and search indexes as deterministic derived views (CQRS); on Failures and events

Further reading:

  • Schwarz 2023, A sighting of traverse_ in Practical FP in Scala – annotated slides tracing Foldable.traverse_ (Applicative sequencing that discards results) across a List and an Option inside the Redis ShoppingCart.update interpreter of Volpe’s Practical FP in Scala; on Composing effectful operations
  • Richardson 2019, Microservices Patterns: With Examples in Java – chapter 5 on hexagonal service structure, DDD aggregates (root-only updates, ID references across aggregates, one aggregate per transaction) and domain events; chapter 6 on event sourcing, splitting aggregate updates into process(command) returning events (which may reject the command) and apply(event) folding events into state (which cannot fail); on Entities, value objects and composition and Failures and events
  • Volpe 2023, Functional Event-Driven Architecture: Powered by Scala 3 – chapter 1 on event-driven architecture, commands, events and CQRS/ES with Scala 3 enums; section 5.1, “Finite state machines”, modeling a trading engine as a pure FSM.id (State, Command) => (State, Event) wired into an fs2 stream with evalMapAccumulate; on Failures and events
  • Millett et al. 2015, Patterns, Principles, and Practices of Domain-Driven Design – chapter 18, “Domain Events”, separating internal domain events from flat, versioned external events across bounded contexts, and subsection “Return Domain Events” on recording events on the aggregate root for the service layer to dispatch so side effects aren’t interleaved with domain logic; on Failures and events and Modules and bounded contexts
  • Stopford 2018, Designing Event-Driven Systems – chapter 7, “Event Sourcing, CQRS, and Other Stateful Patterns”, distinguishing command sourcing (logging incoming requests for replay) from event sourcing (logging state transitions as the shared source of truth) and polyglot materialized views; on Failures and events
  • Fong et al. 2018, Seven Sketches in Compositionality (arXiv:1803.05316) – section 1.1, “More than the sum of their parts”, defining generative effects as what arises when a lossy observation fails to preserve a system-combining operation (Φ(A)∨Φ(B)≤Φ(A∨B)\Phi(A) \vee \Phi(B) \le \Phi(A \vee B)); on the compositionality paragraph in Entities, value objects and composition
  • Gibbons 2006, Design Patterns as Higher-Order Datatype-Generic Programs – section 4, “Patterns as HODGPs”, writes design patterns as reusable library code, with Composite as a generic recursive datatype, Visitor as a fold, Iterator as a fold that lists the contents and Builder as an unfold; on the design-patterns paragraph in Modules as algebras