import type * as P from "@principia/prelude"; import * as HKT from "@principia/prelude/HKT"; import * as E from "../Either"; import type { DecodeError, ErrorInfo } from "./decode-error"; import * as DE from "./DecodeError"; import type { C } from "./model"; /** * @internal */ export const SE = DE.getSemigroup(); /** * @internal */ const both_ = ( fa: E.Either, fb: E.Either ): E.Either => E.isLeft(fa) ? E.isLeft(fb) ? E.left(SE.combine_(fa.left, fb.left)) : fa : E.isLeft(fb) ? fb : E.right([fa.right, fb.right]); /** * @internal */ const alt_ = (me: E.Either, that: () => E.Either): E.Either => { if (E.isRight(me)) { return me; } const ea = that(); return E.isLeft(ea) ? E.left(SE.combine_(me.left, ea.left)) : ea; }; /** * @internal */ export const M: P.MonadFail<[E.URI], C> & P.Applicative<[E.URI], C> & P.Alt<[E.URI], C> & P.Bifunctor<[E.URI], E.V> = HKT.instance({ ...E.MonadFail, ...E.Bifunctor, unit: E.unit, both_, both: (fb: E.Either) => (fa: E.Either) => both_(fa, fb), alt_, alt: (that: () => E.Either) => (me: E.Either) => alt_(me, that) });