AffineFoldLaws

dev.constructive.eo.laws.AffineFoldLaws
trait AffineFoldLaws[S, A]

Law equations for an AffineFold[S, A] — a read-only 0-or-1 focus optic whose carrier is Affine and whose write side is pinned to Unit.

AffineFold's observable surface is two operations — getOption and foldMap — and the laws below pin down their agreement:

  1. getOptionConsistent: getOption and foldMap agree on the single hit. Phrased via foldMap(List(_)): the full fold collects every focus into a list; for AffineFold that list has zero or one element, whose head is exactly the getOption result.
  2. missIsEmpty: when getOption(s) is None, foldMap(f)(s) must equal Monoid.empty under every monoid M. Tested at Int (additive) for scalacheck coverage.
  3. hitIsSingleton: when getOption(s) is Some(a), foldMap(f)(s) must equal f(a) under every monoid. Tested at Int as above.

Laws (2) and (3) are the two "branches" of an invariant that's trivially true by the Affine carrier's ForgetfulFold instance — they're nonetheless worth stating because the AffineFold's value is exactly that a user-supplied partial matcher fits the miss/hit contract. A constructor that returned the wrong branch would fail law (2) or (3) for the wronged input.

Ported from Monocle's monocle.law.AffineFoldLaws (which states getAll.headOption === getOption directly); our phrasing goes through the generic foldMap extension to stay within the existential-optic surface.

Attributes

Source
AffineFoldLaws.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def af: AffineFold[S, A]

The optic under test.

The optic under test.

Attributes

Source
AffineFoldLaws.scala

Concrete methods

def getOptionConsistent(s: S): Boolean

getOption agrees with the foldMap-list head.

getOption agrees with the foldMap-list head.

Attributes

Source
AffineFoldLaws.scala
def hitIsSingleton(s: S, f: A => Int): Boolean

Hit ⇒ foldMap(f)(s) == f(a) where a = getOption(s).get, under any Monoid[Int]-valued f.

Hit ⇒ foldMap(f)(s) == f(a) where a = getOption(s).get, under any Monoid[Int]-valued f.

Attributes

Source
AffineFoldLaws.scala
def missIsEmpty(s: S, f: A => Int): Boolean

Miss ⇒ foldMap collapses to the monoid identity under any Monoid[Int]-valued f.

Miss ⇒ foldMap collapses to the monoid identity under any Monoid[Int]-valued f.

Attributes

Source
AffineFoldLaws.scala