Bifunctor: Mapping Over Both Sides

Map an error, a value or both, with one method written for any type with two sides.

What You'll Learn

  • Map either side of a type with two type arguments, or both, with bimap, first and second
  • Write one method for any two-sided type, through its Bifunctor
  • Predict which of bimap's two functions runs, for each instance
  • Tell a type's Bifunctor from its Functor, and know which to use
  • Check an instance against the four Bifunctor laws with hkj-test's BifunctorLaws

See Example Code

The code on this page is BifunctorBook.java, with OrderError from OrderError.java: the page includes both, so the build compiles and runs them. BifunctorBookTest.java holds what the page says each instance does, and FoundationsLawsBookTest.java checks the laws. BifunctorExample.java maps an Either, a Tuple2, a Validated and a Writer in turn.


Two sides, two functions

If you have caught a service's exception and thrown your own, while converting what it returns, you have written a bimap by hand. With the error held as a value, bimap converts both sides without throwing, and one method can serve any type with two sides.

An Either<L, R> holds an error or a value, and a Tuple2<A, B> holds two values. A Functor maps one type argument and leaves the other alone. Bifunctor maps either of the two, or both.

For one value, each type has its own bimap, such as Either.bimap or an Effect Path's EitherPath.bimap, and application code stays there. This page is for a method written once for any type with two sides. Bifunctor<F> declares bimap, and builds first and second from it:

public interface Bifunctor<F extends WitnessArity<TypeArity.Binary>> {
  <A, B, C, D> Kind2<F, C, D> bimap(
      Function<? super A, ? extends C> f, Function<? super B, ? extends D> g, Kind2<F, A, B> fab);

  default <A, B, C> Kind2<F, C, B> first(Function<? super A, ? extends C> f, Kind2<F, A, B> fab) {
    return bimap(f, Function.identity(), fab);
  }

  default <A, B, D> Kind2<F, A, D> second(Function<? super B, ? extends D> g, Kind2<F, A, B> fab) {
    return bimap(Function.identity(), g, fab);
  }
}

bimap takes a function for each side. first maps only the first side and second only the second, each passing Function.identity() for the side it leaves. F has binary arity, so Kind2<F, A, B> keeps both type arguments open.

The code on this page uses these imports, with Order and OrderStatus from the order records the Optics chapter introduces:

import static org.higherkindedj.hkt.either.EitherKindHelper.EITHER;
import static org.higherkindedj.hkt.validated.ValidatedKindHelper.VALIDATED;

import org.higherkindedj.example.book.foundations.OrderError;
import org.higherkindedj.example.book.optics.cast.Order;
import org.higherkindedj.example.book.optics.cast.OrderStatus;
import org.higherkindedj.hkt.Bifunctor;
import org.higherkindedj.hkt.Kind2;
import org.higherkindedj.hkt.TypeArity;
import org.higherkindedj.hkt.WitnessArity;
import org.higherkindedj.hkt.either.Either;
import org.higherkindedj.hkt.either.EitherBifunctor;
import org.higherkindedj.hkt.validated.Validated;
import org.higherkindedj.hkt.validated.ValidatedBifunctor;

One method for any two-sided type

The order service refuses a request with an OrderError:

/** Why the order service refused a request. */
public sealed interface OrderError {

  /** No order has this id. */
  record NotFound(UUID orderId) implements OrderError {}

  /** The order has shipped, so it can no longer change. */
  record AlreadyShipped(UUID orderId) implements OrderError {}

  /** Typed as {@code OrderError}, so a chain that starts with it can end in any other variant. */
  static OrderError notFound(UUID orderId) {
    return new NotFound(orderId);
  }

  static OrderError alreadyShipped(UUID orderId) {
    return new AlreadyShipped(orderId);
  }
}

A caller sees a message rather than the OrderError, and the order's status rather than the whole Order. forCaller makes that change on both sides, for any type with a Bifunctor:

  // One method for any type with two sides: the error becomes the message a caller sees, and the
  // order becomes its status
  public static <F extends WitnessArity<TypeArity.Binary>> Kind2<F, String, OrderStatus> forCaller(
      Bifunctor<F> bifunctor, Kind2<F, OrderError, Order> result) {
    return bifunctor.bimap(BifunctorBook::message, Order::status, result);
  }

  static String message(OrderError error) {
    return switch (error) {
      case OrderError.NotFound _ -> "no such order";
      case OrderError.AlreadyShipped _ -> "the order has shipped";
    };
  }

The same method runs on an Either and on a Validated, where ORDER is any Order with status NEW. widen2 and narrow2 are widen and narrow for a Kind2, as Bifunctor and Functor explains:

    Either<String, OrderStatus> found =
        EITHER.narrow2(forCaller(EitherBifunctor.INSTANCE, EITHER.widen2(Either.right(ORDER))));
    // Right(NEW)

    Validated<String, OrderStatus> missing =
        VALIDATED.narrow2(
            forCaller(
                ValidatedBifunctor.INSTANCE,
                VALIDATED.widen2(Validated.invalid(OrderError.notFound(ORDER.id())))));
    // Invalid(no such order)

forCaller never asks which side it was given. Each instance applies the function for the side its value holds.

What bimap runs

Whether one function runs or both depends on the instance:

Typebimap runsInstance
Either: a Left or a RightThe first function on a Left, the second on a RightEitherBifunctor.INSTANCE
Validated: an Invalid or a ValidThe first on an Invalid, the second on a ValidValidatedBifunctor.INSTANCE
EitherOrBoth: a Left, a Right or a BothOne on a Left or a Right, and both on a BothEitherOrBothBifunctor.INSTANCE
Tuple2: two valuesBothTuple2Bifunctor.INSTANCE
Writer: a log and a valueBoth: the first on the log, the second on the valueWriterBifunctor.INSTANCE
Const: a value, and a type it holds no value ofOnly the first: the second changes a type, and never runsConstBifunctor.INSTANCE

These are all the instances Higher-Kinded-J ships. Instances has no entry for a type class with a binary witness, so take each from its INSTANCE field. Const explains why its second type is a phantom type.

Bifunctor and Functor

Either has two witnesses. EitherKind.Witness<L> fixes the error type, and is the one Functor, Monad and MonadError take. EitherKind2.Witness leaves both types open, and is the one Bifunctor takes. From the same Either, EITHER.widen gives the Kind a Functor takes, and EITHER.widen2 the Kind2 a Bifunctor takes.

Instances.functor(either()) maps only the Right, as second does. Use the Functor when only the value changes, and the Bifunctor when the error changes too.

Four laws

A Bifunctor keeps four laws. The first two are Functor's laws for both sides at once. The last two tie first and second to bimap, and matter because most instances write their own, as Either's first calls mapLeft.

Laws

  • Identity: mapping both sides with a -> a changes nothing. bimap(a -> a, b -> b, fab) equals fab.
  • Composition: two bimaps equal one bimap of both pairs. bimap(f2, g2, bimap(f1, g1, fab)) equals bimap(f1.andThen(f2), g1.andThen(g2), fab).
  • First: first is a bimap that leaves the second side alone. first(f, fab) equals bimap(f, b -> b, fab).
  • Second: second is a bimap that leaves the first side alone. second(g, fab) equals bimap(a -> a, g, fab).

Two values are equal when they hold equal values on the same sides. The laws assume functions that never return null, and Values, never null says what each type does with one.

BifunctorLaws, in hkj-test's org.higherkindedj.hkt.laws package, checks all four on any instance, your own included. You pass it the equality to use. KindEquivalence builds equalities for a Kind, not a Kind2, so the test writes this one as a lambda that narrows both sides. It uses these imports, with JUnit's:

import static org.higherkindedj.hkt.either.EitherKindHelper.EITHER;
import static org.higherkindedj.hkt.tuple.Tuple2KindHelper.TUPLE2;

import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Stream;
import org.higherkindedj.example.book.foundations.OrderError;
import org.higherkindedj.example.book.optics.cast.Customer;
import org.higherkindedj.example.book.optics.cast.Order;
import org.higherkindedj.hkt.Bifunctor;
import org.higherkindedj.hkt.Kind2;
import org.higherkindedj.hkt.TypeArity;
import org.higherkindedj.hkt.WitnessArity;
import org.higherkindedj.hkt.either.Either;
import org.higherkindedj.hkt.either.EitherBifunctor;
import org.higherkindedj.hkt.laws.BifunctorLaws;
import org.higherkindedj.hkt.tuple.Tuple2;
import org.higherkindedj.hkt.tuple.Tuple2Bifunctor;

The test checks the laws on an Either, both a Right and a Left, and on a Tuple2, where ORDER is any Order:

  <F extends WitnessArity<TypeArity.Binary>, A, B> void assertBifunctorLaws(
      Bifunctor<F> bifunctor,
      Kind2<F, A, B> fab,
      Function<A, String> describe,
      Function<B, Customer> customerOf,
      BiPredicate<Kind2<F, ?, ?>, Kind2<F, ?, ?>> same) {
    BifunctorLaws.assertIdentity(bifunctor, fab, same);
    BifunctorLaws.assertComposition(
        bifunctor, fab, describe, String::toUpperCase, customerOf, Customer::name, same);
    BifunctorLaws.assertFirstConsistency(bifunctor, fab, describe, same);
    BifunctorLaws.assertSecondConsistency(bifunctor, fab, customerOf, same);
  }

  static Stream<Either<OrderError, Order>> results() {
    return Stream.of(Either.right(ORDER), Either.left(OrderError.notFound(ORDER.id())));
  }

  @ParameterizedTest
  @MethodSource("results")
  @DisplayName("Either's Bifunctor keeps all four laws, on a Right and on a Left")
  void eitherBifunctorLaws(Either<OrderError, Order> result) {
    assertBifunctorLaws(
        EitherBifunctor.INSTANCE,
        EITHER.widen2(result),
        error -> error.getClass().getSimpleName(),
        Order::customer,
        (a, b) -> EITHER.narrow2(a).equals(EITHER.narrow2(b)));
  }

  @Test
  @DisplayName("Tuple2's Bifunctor keeps all four laws, mapping both of its values")
  void tuple2BifunctorLaws() {
    assertBifunctorLaws(
        Tuple2Bifunctor.INSTANCE,
        TUPLE2.widen2(new Tuple2<>("checked", ORDER)),
        String::strip,
        Order::customer,
        (a, b) -> TUPLE2.narrow2(a).equals(TUPLE2.narrow2(b)));
  }

Things people get wrong

Common Misunderstandings

  • "bimap runs both functions." Only where both sides hold a value: a Tuple2, a Writer and an EitherOrBoth's Both. An Either or a Validated runs one.
  • "bimap can turn a Left into a Right." It maps each side in place, so a Left stays a Left. To recover from an error, use EitherPath.recover or MonadError.
  • "Const's second changes its value." It changes only a type. A Const holds no value of its second type, so the function never runs.
  • "A type's Functor is its Bifunctor." They are separate instances, over separate witnesses. Either's Functor maps only the Right, as its Bifunctor's second does.

See Also

  • Profunctor: two type arguments, where the first is what a function takes
  • Functor: mapping a container with one type argument
  • Either, Validated and EitherOrBoth: two-sided types whose first side is an error or a warning
  • Type Arity: binary witnesses, and how they differ from unary ones

Further Reading

  • Mark Seemann, Bifunctors: bifunctors explained with Either and tuples

Previous: Applicative, Selective or Monad? Next: Profunctor