Const: The Value That Map Leaves Alone
A Const<C, A> holds a C and no A, so code written to build an A reads a C instead.
"The greatest trick the Devil ever pulled was convincing the world he didn't exist."
— Verbal Kint, The Usual Suspects
Verbal Kint is talking about Keyser Söze, a name that runs through every story he tells. The A in Const<C, A> is that kind of name: it is in every signature, and no object holds it.
| Models | A value of type C, carried through code that expects an A it never holds |
| Replaces | A second loop written beside a traversal, to sum, count or collect what it visits |
| Effect Path | None |
| Instances | Functor, Applicative, Bifunctor |
| Choose it when | Code written for any Applicative, such as a traversal or a Free Applicative program, should read rather than build; otherwise Id, which rebuilds, or compare every type in Choosing a Type |
- Hold a value whose second type is only a type, which every map leaves alone
- Run one traversal two ways: in
Idto rebuild an order's lines, and inConstto add them up - Explain why
Const'sApplicativeneeds aMonoid, and what it combines - Recognise
Constbehind a traversal'sfoldMap, a lens's read and a Free Applicative program's analysis
The code on this page is ConstBook.java: the page includes it, so the build compiles and runs it. ConstBookTest.java holds what the page says each method does.
You rarely build a Const yourself. A traversal's foldMap, getAll and exists run the traversal in a Const of their own, and FreeApAnalyzer.analyseWith runs a program in this one. This page shows the idea they share, for when you write code over any Applicative and want it to read.
A value, and a type that holds nothing
A Const<C, A> holds one value, of type C. Its A is a phantom type: it appears in the type and is stored nowhere. Mapping the A therefore has nothing to change. The code on this page uses these imports:
import static org.higherkindedj.hkt.constant.ConstKindHelper.CONST;
import static org.higherkindedj.hkt.id.IdKindHelper.ID;
import static org.higherkindedj.hkt.instances.Witnesses.id;
import static org.higherkindedj.hkt.list.ListKindHelper.LIST;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.function.Function;
import org.higherkindedj.example.book.optics.cast.LineItem;
import org.higherkindedj.hkt.Applicative;
import org.higherkindedj.hkt.Kind;
import org.higherkindedj.hkt.Kind2;
import org.higherkindedj.hkt.Monoids;
import org.higherkindedj.hkt.TypeArity;
import org.higherkindedj.hkt.WitnessArity;
import org.higherkindedj.hkt.constant.Const;
import org.higherkindedj.hkt.constant.ConstApplicative;
import org.higherkindedj.hkt.constant.ConstBifunctor;
import org.higherkindedj.hkt.constant.ConstKind;
import org.higherkindedj.hkt.constant.ConstKind2;
import org.higherkindedj.hkt.id.Id;
import org.higherkindedj.hkt.instances.Instances;
import org.higherkindedj.hkt.list.ListKind;
import org.higherkindedj.hkt.list.ListTraverse;
// A Const<BigDecimal, LineItem> holds a BigDecimal, and no LineItem at all
Const<BigDecimal, LineItem> total = new Const<>(new BigDecimal("50.00"));
// Mapping the LineItem changes only the type: there is no LineItem to map
Const<BigDecimal, String> relabelled = total.mapSecond(LineItem::sku);
BigDecimal unchanged = relabelled.value();
// 50.00
mapSecond never calls its function, and the Functor's map leaves the value as it is too.
One traversal, two jobs
Foldable and Traverse wrote a traversal once and ran it with Validated and with Either. eachLine is written the same way, for any Applicative: it visits every line, and rebuilds the list inside F:
// Written once, for any Applicative: visits every line, and rebuilds the list in F
static <F extends WitnessArity<TypeArity.Unary>>
Kind<F, Kind<ListKind.Witness, LineItem>> eachLine(
Applicative<F> applicative,
Function<LineItem, Kind<F, LineItem>> visit,
List<LineItem> lines) {
return ListTraverse.INSTANCE.traverse(applicative, visit, LIST.widen(lines));
}
Run in Id, it rebuilds the lines. The basket holds a lamp at 40.00 and four bulbs at 2.50 each, and VAT is 1.20:
// Run in Id, the traversal rebuilds the list: every line gets its price with VAT
Kind<ListKind.Witness, LineItem> repriced =
ID.narrow(
eachLine(
Instances.applicative(id()), line -> ID.widen(Id.of(withVat(line))), BASKET))
.value();
List<BigDecimal> prices = LIST.narrow(repriced).stream().map(LineItem::price).toList();
// [48.00, 3.00]
Run in Const, the same traversal builds nothing, as a reduce over the same loop would. Each visit gives a Const holding the line's total before VAT, and the Applicative adds the totals with Monoids.bigDecimalAddition(), a Monoid that sums money exactly: 40.00 + 4 × 2.50. The list it would have rebuilt is only a type:
// Run in Const, the same traversal builds nothing: the monoid adds the totals before VAT
ConstApplicative<BigDecimal> summing = new ConstApplicative<>(Monoids.bigDecimalAddition());
Kind<ConstKind.Witness<BigDecimal>, Kind<ListKind.Witness, LineItem>> visited =
eachLine(summing, line -> CONST.widen(new Const<>(lineTotal(line))), BASKET);
BigDecimal basketTotal = CONST.narrow(visited).value();
// 50.00
Why the Applicative needs a Monoid
An Applicative's of must give a value with nothing in it, and its ap must combine two. A Const has only its C to do that with, so ConstApplicative takes a Monoid<C>. of gives the monoid's empty value, so an empty basket comes to zero. ap combines the two sides' values, the left one first, so a list monoid collects what each line gives in the order of the lines. There is no flatMap: a next step would need the A, and there is none.
Const behind folds, reads and analyses
The library uses the same idea in two places:
- A traversal's
foldMap, and thegetAllandexistsbuilt on it, run the traversal in a constant functor. A Focus path'sfoldMapuses thisConst; a plainTraversal's fold uses the optics module's own copy of it, which behaves the same. - Free Applicative analyses a program the same way, with an interpreter that gives each instruction's SKU in a
Const.
The idea also explains a lens. A lens written once for any Functor, in the van Laarhoven style, reads its focus when it runs in Const, and sets it when it runs in Id. A lens's modifyF run in Const gives the focus and rebuilds nothing.
A Const always holds a value
new Const<>(null) throws NullPointerException, and so do mapFirst and bimap when their function returns null. A traversal's fold therefore fails at the element whose function returned null, rather than passing the null to its monoid. Values, never null sets out the rule.
Creating instances
new Const<>(value) holds a value, and an applicative's of(anything) gives one holding the monoid's empty value. new ConstApplicative<>(monoid) builds the Applicative for a Monoid<C>, since Const has no Instances entry: its instance depends on the monoid you choose.
Methods
| Method | What it does |
|---|---|
constant.value() | Get the value held |
mapFirst(f) | Change the value |
mapSecond(g) | Change only the phantom type; g is never called |
bimap(f, g) | Both at once; only f touches the value |
new ConstApplicative<>(monoid) | The Applicative that combines values with the monoid |
ConstBifunctor.INSTANCE | The Bifunctor, over ConstKind2 |
Working with Kind
A Const is already its own Kind twice over. As a Kind<ConstKind.Witness<C>, A>, with the value's type fixed, it takes ConstApplicative; CONST.widen and CONST.narrow are casts. As a Kind2<ConstKind2.Witness, C, A> it takes ConstBifunctor, through CONST.widen2 and CONST.narrow2:
ConstBifunctor bifunctor = ConstBifunctor.INSTANCE;
// first changes the value; second changes only the type
Kind2<ConstKind2.Witness, BigDecimal, LineItem> held =
CONST.widen2(new Const<>(new BigDecimal("50.00")));
Kind2<ConstKind2.Witness, BigDecimal, String> rounded =
bifunctor.second(
LineItem::sku, bifunctor.first(sum -> sum.setScale(0, RoundingMode.HALF_EVEN), held));
BigDecimal whole = CONST.narrow2(rounded).value();
// 50
When to use Const
| You need | Use |
|---|---|
To sum, count or collect what code written for any Applicative visits | Const with the matching monoid |
| To read through a traversal or a Focus path | Its foldMap, getAll or exists, which use Const for you |
| To read a Free Applicative program before it runs | FreeApAnalyzer.analyseWith |
| Two values that both hold data | A record |
| One value with no effect | Id |
Previous: Coyoneda Next: Supported Types