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

100% Statements 90/90
100% Branches 7/7
100% Functions 2/2
100% Lines 90/90

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 1541x 1x   1x 1x 1x         1x                     1x 1x 1x             1x 8x 8x   8x 8x 8x 8x 8x 8x   1x                 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x                   16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x       16x 16x 16x 16x 16x   16x 16x 16x 16x 16x   16x 16x   16x 16x 16x 16x 16x   16x 16x 16x   16x 16x 16x 16x 1600x 1600x 1600x 1600x 16x   16x 6x 6x 6x 6x 6x 6x 6x 600x 600x 6x 6x 10x 16x 16x                              
import {option, unary, unaryToKind} from '#arbitrary'
import {addLawSets, Law, lawTests} from '#law'
import {Covariant as CO, Traversable as TA} from '@effect/typeclass'
import {Applicative as arrayApplicative} from '@effect/typeclass/data/Array'
import {Applicative as identityApplicative} from '@effect/typeclass/data/Identity'
import {
  Applicative as optionApplicative,
  Covariant as optionCovariant,
  Traversable as optionTraversable,
} from '@effect/typeclass/data/Option'
import {
  Array as AR,
  Equivalence as EQ,
  flow,
  identity,
  Option as OP,
  pipe,
} from 'effect'
import type {ReadonlyArrayTypeLambda} from 'effect/Array'
import type {Kind, TypeLambda} from 'effect/HKT'
import type {OptionTypeLambda} from 'effect/Option'
import fc from 'fast-check'
import {composeApplicative} from '../../../compose.js'
import {withOuterOption} from './compose.js'
import type {BuildParameterized, ParameterizedGiven as Given} from './given.js'
 
/**
 * Typeclass laws for `Traversable`.
 * @category typeclass laws
 */
export const traversableLaws: BuildParameterized<TraversableTypeLambda> = (
  given,
  suffix?,
) =>
  pipe(
    buildLaws(`Traversable${suffix ?? ''}`, given),
    addLawSets(
      buildLaws(...withOuterOption('Traversable', given, optionTraversable)),
    ),
  )
 
const buildLaws = <
  F1 extends TypeLambda,
  A,
  B = A,
  C = A,
  R = never,
  O = unknown,
  E = unknown,
>(
  name: string,
  {
    F,
    equalsA,
    equalsB,
    equalsC,
    getEquivalence,
    getArbitrary,
    a,
    b,
    c,
  }: Given<TraversableTypeLambda, F1, A, B, C, R, O, E>,
) => {
  type Data<I extends TypeLambda, T> = Kind<I, R, O, E, T>
 
  type G = OptionTypeLambda
  type H = ReadonlyArrayTypeLambda
 
  type DataF<T> = Data<F1, T>
  type DataG<T> = Data<G, T>
  type DataH<T> = Data<H, T>
 
  const fa = getArbitrary(a),
    [equalsFa, equalsFb, equalsFc] = [
      getEquivalence(equalsA),
      getEquivalence(equalsB),
      getEquivalence(equalsC),
    ],
    [traverseIdentity, traverseG, traverseH, traverseGH] = [
      F.traverse(identityApplicative),
      F.traverse(optionApplicative),
      F.traverse(arrayApplicative),
      F.traverse(composeApplicative(optionApplicative, arrayApplicative)),
    ],
    mapG = optionCovariant.map,
    [ab, agb, bhc]: [
      fc.Arbitrary<(a: A) => B>,
      fc.Arbitrary<(a: A) => DataG<B>>,
      fc.Arbitrary<(a: B) => DataH<C>>,
    ] = [
      unary<A>()(b),
      pipe(b, unaryToKind<A>()<OptionTypeLambda, R, O, E>(option)),
      pipe(c, unaryToKind<B>()<ReadonlyArrayTypeLambda, R, O, E>(fc.array)),
    ]
 
  const equalsGHFc: EQ.Equivalence<OP.Option<readonly DataF<C>[]>> = pipe(
    equalsFc,
    AR.getEquivalence,
    OP.getEquivalence,
  )
 
  return lawTests(
    name,
 
    Law(
      'identity',
      'Id.traverse(id) = id',
      fa,
    )(fa => equalsFa(pipe(fa, traverseIdentity(identity)), fa)),
 
    Law(
      'composition',
      'G.map(H.traverse(bhc)) ∘ G.traverse(agb)' +
        ' = GH.traverse(G.map(bhc) ∘ agb)',
      fa,
      agb,
      bhc,
    )((fa, agb, bhc) =>
      equalsGHFc(
        pipe(fa, traverseG(agb), mapG(traverseH(bhc))),
        pipe(fa, traverseGH(flow(agb, mapG(bhc)))),
      ),
    ),
 
    ...('map' in F
      ? [
          Law(
            'map consistency',
            'fa ▹ F.map(ab) = fa ▹ Id.traverse(ab)',
            fa,
            ab,
          )((fa, ab) => {
            const map = F.map as CO.Covariant<F1>['map']
            return equalsFb(pipe(fa, map(ab)), pipe(fa, traverseIdentity(ab)))
          }),
        ]
      : []),
  )
}
 
/**
 * Type lambda for the `Traversable` typeclass.
 * @category type lambda
 */
export interface TraversableTypeLambda extends TypeLambda {
  readonly type: TA.Traversable<this['Target'] & TypeLambda>
}
 
declare module './given.js' {
  interface ParameterizedLambdas {
    Traversable: TraversableTypeLambda
  }
}