Foundations

"The purpose of abstracting is not to be vague, but to create a new semantic level in which one can be absolutely precise."

— Edsger W. Dijkstra, The Humble Programmer


Dijkstra's new semantic level, in this chapter, is Kind<F, A>: a precise way to say "some container F, holding an A" without saying which container. Java lets you write a method for a List, and another for an Optional, but not one method for any container at all; Kind is how Higher-Kinded-J gets there. Application code rarely needs this level, since Effect Paths cover it, and How to read this chapter sends you to the page you came for. Here is one method written at that level. It totals an order wherever the order happens to be:

  // One method for any container F: the Functor passed in knows how to map inside F
  public static <F extends WitnessArity<TypeArity.Unary>> Kind<F, BigDecimal> totals(
      Functor<F> functor, Kind<F, Order> orders) {
    return functor.map(FoundationsIntroBook::total, orders);
  }

The same method, run on three different containers:

    Maybe<BigDecimal> maybeTotal =
        MAYBE.narrow(totals(functor(maybe()), MAYBE.widen(Maybe.just(order))));
    // Just(50.00)
    Either<String, Order> found = Either.right(order);
    Either<String, BigDecimal> eitherTotal =
        EITHER.narrow(totals(functor(either()), EITHER.widen(found)));
    // Right(50.00)
    List<BigDecimal> listTotals =
        LIST.narrow(totals(functor(list()), LIST.widen(List.of(order, refill))));
    // [50.00, 7.50]

Nothing in totals knows about Maybe, Either or List. The Functor passed in does: it is the value that knows how to map inside its own container. functor(maybe()) asks for the one that belongs to Maybe, with functor from Instances and maybe from Witnesses. The bound on F only says that F holds one type, as Maybe does; Type Arity explains it. The widen and narrow calls move a value between its own type and the Kind view of it, whose F is a witness type standing for the container.

That is the whole of Foundations, in three layers:

flowchart TB
    accTitle: The three layers of Foundations, and where Effect Paths sit
    accDescr: The Kind encoding is the base. Type classes such as Functor and Monad are written over it. Core types such as Maybe, Either and IO provide instances of those type classes. Effect Paths such as MaybePath and EitherPath wrap the same core types directly, without going through the type classes.
    K["Kind encoding<br/>Kind#lt;F, A#gt;, witnesses, widen and narrow"]
    TC["Type classes<br/>Functor, Applicative, Monad and more"]
    CT["Core types<br/>Maybe, Either, Validated, IO and more"]
    EP["Effect Paths<br/>MaybePath, EitherPath, IOPath and more"]
    TC -->|"written over"| K
    CT -->|"provide instances of"| TC
    EP -->|"wrap directly"| CT
    classDef step fill:#8caaee,stroke:#1e66f5,color:#232634
    classDef domain fill:#a6d189,stroke:#40a02b,color:#232634
    class K,TC step
    class CT,EP domain

In words: type classes are written over the Kind encoding, and the core types provide instances of them. Effect Paths such as MaybePath and EitherPath wrap the same core types and call them directly, without going through the type classes. GenericPath is the bridge the other way: it lifts any Kind and its Monad to the Path level.

Which level am I working at?

  • Effect Paths (MaybePath, EitherPath, ValidationPath): application code. Concrete types you call directly, with no witness, widen or narrow to write. Most code stays here, and the Effect Paths chapter teaches it.
  • Type classes over Kind (Functor<F>, Monad<F>): one method for any container, as totals is. This chapter.
  • Your own Kind (a witness, widen and narrow of your own): making a type of your own work with the type classes, in Adding Your Own Type.

When two effects must stack and no Path covers them, Path or Transformer? decides.

Before you start

Your project builds and runs on Java 25. Higher-Kinded-J is built on it today, and parts of the library use preview features, which tie the build to that one release. Most of this chapter needs no preview flag: Maybe, Either, Validated and the type classes compile and run without one. VTask's Scope and Par, and the parallel VStream operations, need --enable-preview, as Prerequisites explains. The HKJ Gradle or Maven plugin sets the flags for you.


How to read this chapter

You can ship with Effect Paths without reading this chapter. Start from what you came for:

You wantStart at
One type explained, such as Either, Validated or MaybeCore Types, or Supported Types to find the right one
Every error in a form or request reported at onceValidationPath in application code; Validated and Accumulating Assembly for the type beneath it
The whole stack seen in one expressionOne Line, Six Layers
One method that works for every container, as totals doesThe pages under Ship in the Chapter Contents, read in order
A type of your own that joins the libraryAdding Your Own Type
A comparison with Vavr, Cyclops or Arrow-KtHow does this compare?, in the Foundations FAQ
The names you know from Scala, Haskell or VavrWhat are the names here?, in the Foundations FAQ
Which laws an instance keeps, and which hkj-test checksLaws and Where HKJ Bends Them
The type class interfaces at a glanceQuick Reference
Kind, Functor, Applicative and Monad beside the Java they replace, on one pageFoundations Journey: Cheatsheet
The everyday API insteadEffect Paths

Hands-On Learning

The Foundations journey practises the Ship pages as exercises, from Kind basics through Functor, Applicative and Monad.


Chapter Contents

Ship, read in order:

  1. One Line, Six Layers: The whole stack in one expression
  2. How the Encoding Works: Witness types, Kind, and widen and narrow
  3. Working with Kind: Writing generic code over Kind
  4. Obtaining Instances: Where a type class instance comes from
  5. Functor: Mapping inside any container
  6. Applicative: Combining independent values
  7. Monad: Sequencing steps that depend on each other
  8. MonadError: Raising errors and recovering from them
  9. For Comprehension: Chains of steps that read in order
  10. Check Your Understanding: Twelve questions on the Ship pages, with a plan for your score

On demand, when a task calls for it:

  1. Higher-Kinded Types: The encoding in depth, and adding your own type
  2. Type Classes: Monoid, Traverse, natural transformations and more
  3. Core Types: Either, Validated, IO, VTask and the rest

Look it up, when you hold a question:

  1. Supported Types: Which type to choose, and what it supports
  2. Quick Reference: Every type class and its key methods, and the ready-made monoids
  3. Foundations FAQ: Design questions, runtime cost, comparisons, and the names you know from Scala, Haskell or Vavr
  4. Laws and Where HKJ Bends Them: The instances that bend a law, and which laws hkj-test checks
  5. Example Index: Every runnable basic example, with the page that explains it

Next: One Line, Six Layers