liveBook · Manning

Algebraic design

As explained in Functional Programming in Scala, Second Edition

Algebraic design is a software-design approach that specifies an algebra, including its operations and laws, before choosing concrete data representations.

Algebraic design specifies an interface and its laws before selecting concrete data representations. An algebra consists of operations over data types together with rules describing how those operations relate. Deferring representation keeps attention on the concrete problem while the required operations and guarantees are discovered. section 9.1

A practical algebraic-design process proceeds by:

  • Choosing a small, concrete domain that exposes the essential operations without distracting details. section 9.1
  • Defining primitive operations and compositional combinators as the intended interface. section 9.8
  • Writing laws and sample usages that describe how the operations should behave. section 9.1
  • Letting the interface, laws, usages, and implementation difficulties iteratively guide the eventual representation. section 9.8

Parser combinators

A parser algebra can begin with a primitive character parser and an execution operation: `char(c): Parser[Char]` and `run(input): Either[ParseError, A]`. Its laws and examples state that `char(c).run(c.toString) == Right(c)`. The algebra then grows through operations such as `string`, choice with `or`, repetition with `listOfN`, and mapping or sequencing. The representation of `Parser` is chosen only after these capabilities and relationships are clear. section 9.1

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Explains algebra, laws, primitives, combinators, and iterative API refinement, while deferring concrete representation details.
Examples
Uses character and string parsers, choice, repetition, sequencing, sample laws, and a JSON parser as concrete design artifacts.
What it assumes
Assumes familiarity with functions and data types, while defining algebras and demonstrating parser design through simple examples.
What you can do after
Build it from scratch by selecting primitives, defining combinators and laws, and refining an interface through examples and implementation feedback.
How it connects
Connects parser combinators, functional design, API design, parser generators, laws, and data type representations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.