Typeclass Laws

Extends Typeclass Law Predicate Equivalence transitivity $a=b \, ∧ \, b=c$$⇒$$a=c$ symmetry $a=b$$⇔$$b=a$ reflexivity $a$$=$$a$ Order transitivity $a≤b ∧ b≤c$$⇒$$a≤c$ antisymmetry $a≤b ∧ b≤a$$⇒$$a=b$ reflexivity $a$$≤$$a$ connectivity $a≤b$$∨$$b≤a$ complement consistency $a≤b$ $⇒$ $a \ngtr b$ Bounded lower bounded $a$$≥$ $\minBound$ upper bounded $a$ $≤$ $\maxBound$ Semigroup associativity $(a ⊕ b) ⊕ c$ $=$ $a ⊕ (b ⊕ c)$ combineMany associativity $\combineMany(a, [b, c])$ $=$ $a ⊕ (b ⊕ c)$ Monoid left identity $∅ \! ⊕ a$$=$$a$ right identity $a ⊕ ∅$$=$$a$ Invariant identity $\imap\left(\id, \id\right)$$=$$\id$ composition $\imap(ab, ba) \∘ \imap(bc, cb)$ $=$ $\imap(bc \∘ ab, ba \∘ cb)$ Contravariant identity $\contramap(\id)$$=$$\id$ composition $\contramap(ba) \∘ \contramap(cb)$ $=$ $\contramap(ba \∘ cb)$ Covariant identity $\map(\id)$$=$$\id$ composition $\map(ab) \∘ \map(bc)$ $=$ $\map(bc \∘ ab)$ Applicative identity $\id \▹ \of \▹ \ap(a)$$=$$a$ homomorphism $ab \▹ \of \▹ (a \▹ \of \▹ \ap)$ $=$ $a \▹ ab \▹ \of$ associative composition $\fbc \▹ \map(\compose) \▹ \ap(\fab) \▹ \ap(\fa)$ $=$ $\fbc \▹ \ap\(\fab \▹ \ap(\fa)\)$ interchange $\fab \▹ \ap\(\of(a)\)$ $=$ $a \▹ \apply \▹ \of \▹ \ap(\fab)$ map consistency $\fa \▹ \map(ab)$ $=$ $ab \▹ \of \▹ \ap(\fa)$ product consistency $\fab \▹ \ap(\fa)$ $=$ $\product(\fab, \fa) \▹ \map\(([ab, a]) ⇒ ab(a)\)$ flatMap consistency $\ap(\fa)$ $=$ $\flatMap\(ab ⇒ \!\map(\fa, ab)\)$ Monad left identity $\of \∘ \flatMap(\afb)$ $=$ $\afb$ right identity $\flatMap(\of)$$=$$\id$ associativity $\flatMap(\afb) \∘ \flatMap(\bfc)$ $=$ $\flatMap\(\flatMap(\bfc) \∘ \afb\)$ map consistency $\map(ab)$ $=$ $\flatMap(\of \∘ ab)$ SemiAlternative associativity $(\fa_1 ⊕ \fa_2) ⊕ \fa_3$ $=$$\fa_1 ⊕ (\fa_2 ⊕ \fa_3)$ coproductMany associativity $\coproductMany(\fa_1, [\fa_2, \fa_3])$ $=$$\fa_1 ⊕ (\fa_2 ⊕ \fa_3)$ Alternative left identity $∅ ⊕ \fa$$=$$\fa$ right identity $\fa ⊕ ∅$ $=$ $\fa$ coproductAll zero $\coproductAll\emptyArray$ $=$ $∅$ right absorption $\ap(\fab, ∅)$ $=$ $∅$ left distributivity $\coproduct(\fa_1, \fa_2) \▹ \map(ab)$ $=$ $\coproduct\(\map(\fa_1, ab), \map(\fa_2, ab)\)$ right distributivity $\coproduct(\fab_1, \fab_2) \▹ \ap(\fa)$ $=$ $\coproduct\(\ap(\fab_1, fa), \ap(\fab_2, fa)\)$ Traversable identity $\Id.\traverse(\id)$$=$$\id$ composition $\G.\map\(\H.\traverse(bhc)\)\x \∘ \G.\traverse(agb)$ $=$ $\GH.\traverse\(\G.\map(bhc)\x \∘ agb\)$ map consistency $\F.\map(ab)$ $=$ $\Id.\traverse(ab)$ Foldable reduce $\reduce(b, bab)$$=$$\combineMap\(\a\Monoid\ofEndo\xx)(a ⇒ b ⇒ \bab(b, a)\)$ combineMap $\reduce(∅, ⊕)$$=$$\combineMap(monoid\,)(\id)$ reduceRight $\reduceRight(∅, (p,a) ⇒ aa(a) ⊕ p)$ $=$ $\combineMap(monoid\,)(aa)$ reduceKind $\reduceKind(\a\Monad\ofId\xx)$ $=$ $\reduce$ toArray $\toArray$ $=$ $\reduce([\,], \Array\kern-1pt.\append)$ Filterable identity $\filterMap(\Option.\fn{some})$$=$$\id$ composition $\filterMap(boc)\x \∘ \filterMap(aob)$$=$$\filterMap\(\x\Option.\flatMap(boc)\x \∘ aob\)$
🔗tip: Typeclasses and laws link to their source code.