Coyoneda: Maps Held for One Pass

Hold every map for one pass, and give an instruction set the Functor it lacks.

"Never put off till to-morrow what you can do day after to-morrow just as well."

— Mark Twain, The Late Benjamin Franklin

Twain signs the maxim with Franklin's initials, in a sketch that mocks Franklin's maxims. A Coyoneda takes it as advice: every map is written down and put off. Only when you lower it is the work done, all of it in one pass.

Coyoneda at a glance

ModelsA value of F with the map calls made on it held back, to be applied in one pass
ReplacesA Functor written by hand for an instruction set, or a chain of map calls that each walk the structure
Effect PathNone
InstancesFunctor
Choose it whenAn instruction set has no Functor of its own; otherwise use the Functor that @EffectAlgebra generates, or compare every type in Choosing a Type

What You'll Learn

  • Hold three maps on a list, and apply them in one walk
  • Give an instruction set with no Functor one, so that a Free program can hold it
  • Run that program with Coyoneda.interpreter, or one held value with foldMap
  • Choose between Coyoneda and a Functor of the set's own, such as the one @EffectAlgebra generates

See Example Code

The code on this page is CoyonedaBook.java: the page includes it, so the build compiles and runs it. CoyonedaBookTest.java holds what the page says each method does.

Where Coyoneda fits

An instruction set from @EffectAlgebra gets a generated Functor, built on its records' mapK, which extends each record's continuation: the function from the instruction's result to what comes next. So it never needs Coyoneda. Coyoneda is for a set you write yourself whose records carry no continuation, and for holding many maps on a structure that is costly to walk.


Three maps, one walk

A Java Stream applies each element's maps in one pass. List's map, like the map of most types with a Functor, does not. It walks the list and builds a new one, so three maps in a row make three walks and two lists that are thrown away. Coyoneda gives any such type the stream's single pass. It holds the list, and each map adds its function to the ones it holds without applying anything. lower hands them all to the list's Functor as one map, which passes each element through the functions in turn:

flowchart TB
    accTitle: How a Coyoneda holds three maps and applies them in one pass
    accDescr: lift wraps the basket's list of lines in a Coyoneda that holds no functions. Three calls to map add three functions to it, and apply none. lower hands the three to the List Functor as one map, which walks the list once and gives the list of prices.
    L["the basket's lines"]:::domain -->|lift| C0["Coyoneda: the list, no functions"]:::domain
    C0 -->|"map, map, map"| C3["Coyoneda: the list, three functions held"]:::domain
    C3 -->|"lower: one walk"| R["the new prices"]:::domain
    classDef domain fill:#a6d189,stroke:#40a02b,color:#232634

The code on this page uses these imports:

import static org.higherkindedj.hkt.coyoneda.CoyonedaKindHelper.COYONEDA;
import static org.higherkindedj.hkt.id.IdKindHelper.ID;
import static org.higherkindedj.hkt.list.ListKindHelper.LIST;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.higherkindedj.example.book.optics.cast.LineItem;
import org.higherkindedj.hkt.Kind;
import org.higherkindedj.hkt.Natural;
import org.higherkindedj.hkt.TypeArity;
import org.higherkindedj.hkt.WitnessArity;
import org.higherkindedj.hkt.coyoneda.Coyoneda;
import org.higherkindedj.hkt.coyoneda.CoyonedaFunctor;
import org.higherkindedj.hkt.coyoneda.CoyonedaKind;
import org.higherkindedj.hkt.effect.FreePath;
import org.higherkindedj.hkt.effect.Path;
import org.higherkindedj.hkt.id.Id;
import org.higherkindedj.hkt.id.IdKind;
import org.higherkindedj.hkt.id.IdMonad;
import org.higherkindedj.hkt.list.ListKind;
import org.higherkindedj.hkt.list.ListMonad;

The list here stands in for a structure that is costly to walk, such as a large tree or a page of results from a remote store. The basket holds a lamp at 40.00 and four bulbs at 2.50 each, and VAT is 1.20, so each line's price with VAT is 48.00 for the lamp and 3.00 for a bulb:

    // Three maps, held as one: nothing is applied yet
    Coyoneda<ListKind.Witness, BigDecimal> withVat =
        Coyoneda.lift(LIST.widen(BASKET))
            .map(LineItem::price)
            .map(price -> price.multiply(VAT))
            .map(price -> price.setScale(2, RoundingMode.HALF_EVEN));

    // Lowering applies all three to each line, in one walk over the list, with List's Functor
    // (ListMonad.INSTANCE is List's Monad, and so its Functor too)
    List<BigDecimal> prices = LIST.narrow(withVat.lower(ListMonad.INSTANCE));
    // [48.00, 3.00]

Through List's own map, the same three functions walk the list three times; through Coyoneda, once. The held functions are applied in a loop, so a chain of a hundred thousand maps is lowered without overflowing the stack.

An instruction set with no Functor

A Free program needs a Functor for its instructions, to put the rest of the program after each one. OrderOp's records carry a continuation k, so @EffectAlgebra builds that Functor from mapK. The warehouse's instruction set cannot have one, because each record fixes its own result type and carries nothing for a map to extend:

  // The warehouse's instructions. Each record fixes its own result type and carries no
  // continuation, so there is nothing a map could change: StockOp can have no Functor.
  sealed interface StockOp<A> extends Kind<StockOp.Witness, A> {
    final class Witness implements WitnessArity<TypeArity.Unary> {
      private Witness() {}
    }

    // How many of a SKU the warehouse holds
    record Available(String sku) implements StockOp<Integer> {}

    // Sets a quantity aside for an order, if the warehouse holds that many
    record Reserve(String sku, int quantity) implements StockOp<Boolean> {}
  }

Lifted into a Coyoneda, an instruction gets the Functor it lacks: CoyonedaFunctor holds each map instead of applying it. A FreePath over CoyonedaKind.Witness<StockOp.Witness> then holds the warehouse's instructions:

  // Coyoneda gives any instruction set a Functor, so Free can hold StockOp's instructions
  static final CoyonedaFunctor<StockOp.Witness> STOCK = CoyonedaFunctor.instance();

  static <A> FreePath<CoyonedaKind.Witness<StockOp.Witness>, A> step(StockOp<A> op) {
    return Path.freeLift(COYONEDA.lift(op), STOCK);
  }

  // Reserves a line if the warehouse holds enough of its SKU: the second step needs the count
  static FreePath<CoyonedaKind.Witness<StockOp.Witness>, Boolean> reserveLine(LineItem line) {
    return step(new StockOp.Available(line.sku()))
        .via(
            held ->
                held >= line.quantity()
                    ? step(new StockOp.Reserve(line.sku(), line.quantity()))
                    : Path.freePure(false, STOCK));
  }

Running a program over Coyoneda

The interpreter is written for StockOp alone, as a Natural with a case for each record. Its warehouse holds three lamps and two bulbs:

  // Runs each instruction against stock held in memory
  static final class Warehouse implements Natural<StockOp.Witness, IdKind.Witness> {
    final Map<String, Integer> levels = new HashMap<>(Map.of("LAMP", 3, "BULB", 2));

    @Override
    @SuppressWarnings("unchecked") // each record fixes A; the switch cannot say so
    public <A> Kind<IdKind.Witness, A> apply(Kind<StockOp.Witness, A> fa) {
      Object result =
          switch ((StockOp<A>) fa) {
            case StockOp.Available available -> levels.getOrDefault(available.sku(), 0);
            case StockOp.Reserve reserve -> {
              int held = levels.getOrDefault(reserve.sku(), 0);
              if (held < reserve.quantity()) {
                yield false;
              }
              levels.put(reserve.sku(), held - reserve.quantity());
              yield true;
            }
          };
      return (Kind<IdKind.Witness, A>) ID.widen(Id.of(result));
    }
  }

Coyoneda.interpreter(warehouse, functor) runs each instruction with it, and then applies the maps the Coyoneda held, with Id's Functor:

    Warehouse warehouse = new Warehouse();
    IdMonad id = IdMonad.instance();

    // Coyoneda.interpreter runs each instruction with the warehouse, then applies its held maps
    boolean lampReserved =
        ID.narrow(
                reserveLine(BASKET.get(0))
                    .foldMap(Coyoneda.interpreter(warehouse, id), id)
                    .runKind())
            .value();
    // true

    boolean bulbsReserved =
        ID.narrow(
                reserveLine(BASKET.get(1))
                    .foldMap(Coyoneda.interpreter(warehouse, id), id)
                    .runKind())
            .value();
    // false

The basket's second line wants four bulbs and the warehouse holds two, so nothing is set aside, and the warehouse's count is unchanged. One held value runs through an interpreter the same way, with foldMap:

    // One held value runs through an interpreter with foldMap, and needs no Functor for StockOp
    Coyoneda<StockOp.Witness, Boolean> enoughForTwo =
        Coyoneda.lift(new StockOp.Available("LAMP")).map(count -> count >= 2);

    boolean enough = ID.narrow(enoughForTwo.foldMap(new Warehouse(), id)).value();
    // true

Why it is called Coyoneda

Coyoneda is named for the co-Yoneda lemma: for a Functor F, a Coyoneda<F, A> and a Kind<F, A> hold the same information. lift and lower turn one into the other, and lowering a lifted list gives back the list. So holding the maps loses nothing: it only decides when they run.

A held value is never null

Coyoneda.lift(null) and Coyoneda.apply(null, f) throw at the call. A held function runs only when you lower or interpret, through the target's own map, so a null it returns meets that type's rule, which Values, never null sets out. Id, for one, keeps the null as its value.

Creating instances

Coyoneda.lift(fa) holds a value with no functions, and Coyoneda.apply(fx, f) holds it with one. COYONEDA.lift(fa), from CoyonedaKindHelper, gives the same, already widened to a Kind for a Free program.

Methods

MethodWhat it does
coyoneda.map(f)Hold one more function, applied after the others
lower(functor)Apply the held functions with F's own Functor, in one map
foldMap(natural, functor)Run the held value through an interpreter into G, then apply the held functions with G's Functor
Coyoneda.interpreter(natural, functor)An interpreter for a program whose instructions are Coyonedas
underlying()The value held, before any function

Working with Kind

COYONEDA.widen and COYONEDA.narrow, from CoyonedaKindHelper, convert between a Coyoneda and its Kind. Its one instance, CoyonedaFunctor, works for any F, because its map only holds the function:

    CoyonedaFunctor<ListKind.Witness> functor = CoyonedaFunctor.instance();

    // A Coyoneda is already its own Kind, so widen and narrow are casts
    Kind<CoyonedaKind.Witness<ListKind.Witness>, Integer> quantities =
        COYONEDA.widen(Coyoneda.lift(LIST.widen(BASKET)).map(LineItem::quantity));
    Kind<CoyonedaKind.Witness<ListKind.Witness>, Integer> doubled =
        functor.map(quantity -> quantity * 2, quantities);

    List<Integer> lowered = LIST.narrow(COYONEDA.narrow(doubled).lower(ListMonad.INSTANCE));
    // [2, 8]

Coyoneda has no Applicative or Monad: combining two held values needs F's own.

When to use Coyoneda

You needUse
A Free program over an instruction set whose records carry no continuationCoyoneda
A Free program over an @EffectAlgebra instruction setIts generated Functor, with no Coyoneda
Many maps on a structure that is costly to walkCoyoneda, lowered once
A few maps on a List or an OptionalTheir own map
A pipeline over a Java StreamThe stream, which already applies each element's maps in one pass

Hands-On Learning

Practise lifting, mapping and lowering in Tutorial 09: Coyoneda (5 exercises), from the Advanced Patterns journey.


Previous: EitherF Next: Const