All files / src/laws/typeclass/parameterized Applicative.ts

100% Statements 118/118
100% Branches 14/14
100% Functions 2/2
100% Lines 118/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 1751x         1x 1x 1x   1x 1x 1x 1x   1x             1x                 6x 6x 6x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   1x                 12x 12x 12x 12x 12x   12x 12x   12x 12x 12x 12x 12x 12x 12x   12x 12x 12x 12x 12x 12x   12x 12x 12x 12x 12x 12x 12x 1000x 1000x 1000x 1000x 1000x 1000x 1000x   1000x 12x   12x 12x 12x 12x 12x 12x   1000x 12x   12x 12x 12x 12x 12x 12x   12x 12x 12x 12x 12x 12x 1000x 1000x 1000x 1000x 1000x 1000x 1000x 12x       12x 4x 4x 4x 4x 4x 4x 4x 300x 300x 300x 300x 300x 300x 300x 300x 4x 4x 8x 12x 12x                              
import {
  Applicative as AP,
  Monad as MD,
  SemiApplicative as SA,
} from '@effect/typeclass'
import {Applicative as optionApplicative} from '@effect/typeclass/data/Option'
import {identity, pipe} from 'effect'
import {apply, compose} from 'effect/Function'
import type {TypeLambda} from 'effect/HKT'
import {addLawSets, Law, lawTests} from '../../../law.js'
import {monoidLaws} from '../concrete/Monoid.js'
import {withOuterOption} from './compose.js'
import {covariantLaws} from './Covariant.js'
import type {BuildParameterized, ParameterizedGiven as Given} from './given.js'
import {unfoldGiven} from './given.js'
import {BuildInternal} from './internal.js'
 
/**
 * Typeclass laws for `Applicative`.
 * @category typeclass laws
 */
export const applicativeLaws: BuildParameterized<ApplicativeTypeLambda> = <
  F1 extends TypeLambda,
  A,
  B = A,
  C = A,
  R = never,
  O = unknown,
  E = unknown,
>(
  given: Given<ApplicativeTypeLambda, F1, A, B, C, R, O, E>,
  suffix?: string,
) => {
  const {Monoid: monoid, F, fa, equalsA, getEquivalence} = unfoldGiven(given)
 
  return pipe(
    buildLaws(`Alternative${suffix ?? ''}`, given),
    pipe(given, covariantLaws, addLawSets),
    addLawSets(
      buildLaws(...withOuterOption('Applicative', given, optionApplicative)),
    ),
    pipe(
      {
        suffix: 'Applicative.getMonoid()',
        a: fa,
        F: pipe(monoid, AP.getMonoid(F)<A, R, O, E>),
        equalsA: getEquivalence(equalsA),
      },
      monoidLaws,
      addLawSets,
    ),
  )
}
 
const buildLaws: BuildInternal<ApplicativeTypeLambda> = <
  F extends TypeLambda,
  A,
  B = A,
  C = A,
  R = never,
  O = unknown,
  E = unknown,
>(
  name: string,
  given: Given<ApplicativeTypeLambda, F, A, B, C, R, O, E>,
) => {
  const {F, a, fa, equalsFa, equalsFb, equalsFc, ab, fabOf, fbcOf} =
    unfoldGiven(given)
 
  const [fab, fbc] = [fabOf(F.of), fbcOf(F.of)],
    [ap, of, map] = [SA.ap(F), F.of, F.map]
 
  return lawTests(
    name,
    Law(
      'identity',
      'id ▹ of ▹ ap(a) = a',
      fa,
    )(a => equalsFa(pipe(identity<A>, of, ap(a)), a)),
 
    Law(
      'homomorphism',
      'ab ▹ of ▹ (a ▹ of ▹ ap) = a ▹ ab ▹ of',
      a,
      ab,
    )((a, ab) => equalsFb(pipe(ab, of, ap(of(a))), pipe(a, ab, of))),
 
    Law(
      'associative composition',
      'fbc ▹ map(compose) ▹ ap(fab) ▹ ap(fa) = fbc ▹ ap(fab ▹ ap(fa))',
      fa,
      fab,
      fbc,
    )((fa, fab, fbc) => {
      const left = pipe(
        fbc,
        map(bc => compose(bc)<A>),
        ap(fab),
        ap(fa),
      )
      const right = pipe(fbc, ap(pipe(fab, ap(fa))))
 
      return equalsFc(left, right)
    }),
 
    Law(
      'interchange',
      'fab ▹ ap(of(a)) = a ▹ apply ▹ of ▹ ap(Fab)',
      a,
      fab,
    )((a, fab) => {
      type AB = (ab: (a: A) => B) => B
      return equalsFb(pipe(fab, ap(of(a))), pipe(a, apply, of<AB>, ap(fab)))
    }),
 
    Law(
      'map consistency',
      'fa ▹ map(ab) = ab ▹ of ▹ ap(fa)',
      fa,
      ab,
    )((fa, ab) => equalsFb(pipe(fa, map(ab)), pipe(ab, F.of, ap(fa)))),
 
    Law(
      'product consistency',
      'fab ▹ ap(fa) = product(fab, fa) ▹ map(([ab, a]) ⇒ ab(a))',
      fa,
      fab,
    )((fa, fab) =>
      equalsFb(
        pipe(fab, ap(fa)),
        pipe(
          F.product(fab, fa),
          map(([f, a]) => f(a)),
        ),
      ),
    ),
 
    // Some applicatives do not have monads. For example: when the applicative is
    // composed we have no monad for this law.
    ...('flatMap' in F
      ? [
          Law(
            'flatMap consistency',
            'fab ▹ ap(fa) = fab ▹ flatMap(ab ⇒ map(fa, ab))',
            fa,
            fab,
          )((fa, fab) => {
            const flatMap = F.flatMap as MD.Monad<F>['flatMap']
            return equalsFb(
              ap(fab, fa),
              pipe(
                fab,
                flatMap(ab => map(fa, ab)),
              ),
            )
          }),
        ]
      : []),
  )
}
 
/**
 * Type lambda for the `Applicative` typeclass.
 * @category type lambda
 */
export interface ApplicativeTypeLambda extends TypeLambda {
  readonly type: AP.Applicative<this['Target'] & TypeLambda>
}
 
declare module './given.js' {
  interface ParameterizedLambdas {
    Applicative: ApplicativeTypeLambda
  }
}