// ets_tracing: off /* eslint-disable prefer-const */ import * as Chunk from "../Collections/Immutable/Chunk/core.js" import * as Tp from "../Collections/Immutable/Tuple/index.js" import { _A, _E, _R, _S1, _S2, _U, _W } from "../Effect/commons.js" import type { EffectURI } from "../Effect/effect.js" import * as E from "../Either/core.js" import { Stack } from "../Stack/index.js" import type { HasUnify } from "../Utils/index.js" /** * `XPure[W, S1, S2, R, E, A]` is a purely functional description of a * computation that requires an environment `R` and an initial state `S1` and * may either fail with an `E` or succeed with an updated state `S2` and an `A` * along with in either case a log with entries of type `W`. Because of its * polymorphism `ZPure` can be used to model a variety of effects including * context, state, failure, and logging. */ export interface XPure extends HasUnify { readonly _tag: "XPure" readonly [_S1]: (_: S1) => void readonly [_S2]: () => S2 readonly [_U]: EffectURI readonly [_W]: () => W readonly [_E]: () => E readonly [_A]: () => A readonly [_R]: (_: R) => void } export abstract class XPureBase implements XPure { readonly _tag = "XPure"; readonly [_S1]!: (_: S1) => void; readonly [_S2]!: () => S2; readonly [_U]!: EffectURI; readonly [_W]!: () => W; readonly [_E]!: () => E; readonly [_A]!: () => A; readonly [_R]!: (_: R) => void } /** * @ets_optimize remove */ function concrete( _: XPure ): asserts _ is Concrete { // } class Succeed extends XPureBase { readonly _xptag = "Succeed" constructor(readonly a: A) { super() } } class Log extends XPureBase { readonly _xptag = "Log" constructor(readonly w: W) { super() } } class Suspend extends XPureBase { readonly _xptag = "Suspend" constructor(readonly f: () => XPure) { super() } } class Fail extends XPureBase { readonly _xptag = "Fail" constructor(readonly e: E) { super() } } class Modify extends XPureBase { readonly _xptag = "Modify" constructor(readonly run: (s1: S1) => Tp.Tuple<[S2, A]>) { super() } } class FlatMap extends XPureBase< W | W2, S1, S3, R & R1, E1 | E, B > { readonly _xptag = "FlatMap" constructor( readonly value: XPure, readonly cont: (a: A) => XPure ) { super() } } class Fold extends XPureBase< W | W1 | W2, S1, S3, R, E2, B > { readonly _xptag = "Fold" constructor( readonly value: XPure, readonly failure: (e: E1) => XPure, readonly success: (a: A) => XPure ) { super() } } class Get extends XPureBase { readonly _xptag = "Get" constructor(readonly get: (s: S1) => XPure) { super() } } class Access extends XPureBase { readonly _xptag = "Access" constructor(readonly access: (r: R) => XPure) { super() } } class Provide extends XPureBase { readonly _xptag = "Provide" constructor(readonly r: R, readonly cont: XPure) { super() } } type Concrete = | Succeed | Fail | Log | Get | Modify | FlatMap | Fold | Access | Provide | Suspend /** * Extends this computation with another computation that depends on the * result of this computation by running the first computation, using its * result to generate a second computation, and running that computation. */ export function chain( f: (a: A) => XPure ) { return ( self: XPure ): XPure => new FlatMap(self, f) } /** * Extends this computation with another computation that depends on the * result of this computation by running the first computation, using its * result to generate a second computation, and running that computation. */ export function chain_( self: XPure, f: (a: A) => XPure ): XPure { return new FlatMap(self, f) } /** * Returns a computation that effectfully "peeks" at the success of this one. */ export function tap(f: (a: A) => XPure) { return ( self: XPure ): XPure => tap_(self, f) } /** * Returns a computation that effectfully "peeks" at the success of this one. */ export function tap_( self: XPure, f: (a: A) => XPure ): XPure { return chain_(self, (a) => map_(f(a), () => a)) } /** * Constructs a computation that always succeeds with the specified value, * passing the state through unchanged. */ export function succeed(a: A): XPure { return new Succeed(a) } /** * Constructs a computation that logs w. */ export function log(w: W): XPure { return new Log(w) } /** * Constructs a computation that logs w. */ export function logWith(f: () => W) { return suspend(() => log(f())) } /** * Constructs a computation that always succeeds with the specified value, * passing the state through unchanged. */ export function fail(a: E): XPure { return new Fail(a) } /** * Extends this computation with another computation that depends on the * result of this computation by running the first computation, using its * result to generate a second computation, and running that computation. */ export function map_( self: XPure, f: (a: A) => B ) { return chain_(self, (a) => succeed(f(a))) } /** * Extends this computation with another computation that depends on the * result of this computation by running the first computation, using its * result to generate a second computation, and running that computation. */ export function map(f: (a: A) => B) { return (self: XPure) => chain_(self, (a) => succeed(f(a))) } /** * Recovers from errors by accepting one computation to execute for the case * of an error, and one computation to execute for the case of success. */ export function foldM_( self: XPure, failure: (e: E) => XPure, success: (a: A) => XPure ): XPure { return new Fold( self, failure, success ) } /** * Recovers from errors by accepting one computation to execute for the case * of an error, and one computation to execute for the case of success. */ export function foldM( failure: (e: E) => XPure, success: (a: A) => XPure ) { return (self: XPure) => foldM_(self, failure, success) } /** * Folds over the failed or successful results of this computation to yield * a computation that does not fail, but succeeds with the value of the left * or righr function passed to `fold`. */ export function fold(failure: (e: E) => B, success: (a: A) => C) { return (self: XPure) => fold_(self, failure, success) } /** * Folds over the failed or successful results of this computation to yield * a computation that does not fail, but succeeds with the value of the left * or righr function passed to `fold`. */ export function fold_( self: XPure, failure: (e: E) => B, success: (a: A) => C ): XPure { return foldM_( self, (e) => succeed(failure(e)), (a) => succeed(success(a)) ) } /** * Recovers from all errors. */ export function catchAll( failure: (e: E) => XPure ) { return (self: XPure) => catchAll_(self, failure) } /** * Recovers from all errors. */ export function catchAll_( self: XPure, failure: (e: E) => XPure ) { return foldM_(self, failure, (a) => succeed(a)) } /** * Returns a computation whose error and success channels have been mapped * by the specified functions, `f` and `g`. */ export function bimap(f: (e: E) => E1, g: (a: A) => A1) { return (self: XPure) => bimap_(self, f, g) } /** * Returns a computation whose error and success channels have been mapped * by the specified functions, `f` and `g`. */ export function bimap_( self: XPure, f: (e: E) => E1, g: (a: A) => A1 ) { return foldM_( self, (e) => fail(f(e)), (a) => succeed(g(a)) ) } /** * Transforms the error type of this computation with the specified * function. */ export function mapError(f: (e: E) => E1) { return (self: XPure) => mapError_(self, f) } /** * Transforms the error type of this computation with the specified * function. */ export function mapError_( self: XPure, f: (e: E) => E1 ) { return catchAll_(self, (e) => fail(f(e))) } /** * Constructs a computation from the specified modify function. */ export function modify( f: (s: S1) => Tp.Tuple<[S2, A]> ): XPure { return new Modify(f) } /** * Constructs a computation from the specified modify function. */ export function set(s: S) { return modify(() => Tp.tuple<[S, void]>(s, undefined)) } /** * Constructs a computation from the specified update function. */ export function update( f: (s: S1) => S2 ): XPure { return modify((s) => Tp.tuple(f(s), undefined)) } /** * Constructs a computation that always returns the `Unit` value, passing the * state through unchanged. */ export const unit = succeed(undefined as void) /** * Transforms the initial state of this computation` with the specified * function. */ export function contramapInput(f: (s: S0) => S1) { return (self: XPure) => chain_(update(f), () => self) } /** * Transforms the initial state of this computation` with the specified * function. */ export function provideSome(f: (s: R0) => R1) { return (self: XPure) => accessM((r: R0) => provideAll(f(r))(self)) } /** * Provides this computation with its required environment. */ export function provideAll(r: R) { return ( self: XPure ): XPure => new Provide(r, self) } /** * Provides this computation with its required environment. */ export function provideAll_( self: XPure, r: R ): XPure { return new Provide(r, self) } /** * Provides some of the environment required to run this effect, * leaving the remainder `R0` and combining it automatically using spread. */ export function provide(r: R) { return ( next: XPure ): XPure => provideSome((r0: R0) => ({ ...r0, ...r }))(next) } /** * Get the state monadically */ export function getM( f: (_: S1) => XPure ): XPure { return new Get(f) } /** * Get the state with the function f */ export function get(f: (_: S) => A): XPure { return getM((s: S) => succeed(f(s))) } /** * Access the environment monadically */ export function accessM( f: (_: R) => XPure ): XPure { return new Access(f) } /** * Access the environment with the function f */ export function access(f: (_: R) => A): XPure { return accessM((r: R) => succeed(f(r))) } /** * Access the environment */ export function environment(): XPure { return accessM((r: R) => succeed(r)) } /** * Returns a computation whose failure and success have been lifted into an * `Either`. The resulting computation cannot fail, because the failure case * has been exposed as part of the `Either` success case. */ export function either( self: XPure ): XPure> { return fold_(self, E.left, E.right) } /** * Executes this computation and returns its value, if it succeeds, but * otherwise executes the specified computation. */ export function orElseEither( that: () => XPure ) { return ( self: XPure ): XPure> => orElseEither_(self, that) } /** * Executes this computation and returns its value, if it succeeds, but * otherwise executes the specified computation. */ export function orElseEither_( self: XPure, that: () => XPure ): XPure> { return foldM_( self, () => map_(that(), (a) => E.right(a)), (a) => succeed(E.left(a)) ) } /** * Combines this computation with the specified computation, passing the * updated state from this computation to that computation and combining the * results of both using the specified function. */ export function zipWith( that: XPure, f: (a: A, b: B) => C ) { return ( self: XPure ): XPure => zipWith_(self, that, f) } /** * Combines this computation with the specified computation, passing the * updated state from this computation to that computation and combining the * results of both using the specified function. */ export function zipWith_( self: XPure, that: XPure, f: (a: A, b: B) => C ) { return chain_(self, (a) => map_(that, (b) => f(a, b))) } /** * Combines this computation with the specified computation, passing the * updated state from this computation to that computation and combining the * results of both into a tuple. */ export function zip(that: XPure) { return (self: XPure) => zip_(self, that) } /** * Combines this computation with the specified computation, passing the * updated state from this computation to that computation and combining the * results of both into a tuple. */ export function zip_( self: XPure, that: XPure ) { return zipWith_(self, that, Tp.tuple) } /** * Suspend a computation, useful in recursion */ export function suspend( f: () => XPure ): XPure { return new Suspend(f) } /** * Lift a sync (non failable) computation */ export function succeedWith(f: () => A) { return suspend(() => succeed(f())) } /** * Lift a sync (non failable) computation */ export function tryCatch(onThrow: (u: unknown) => E) { return (f: () => A) => suspend(() => { try { return succeed(f()) } catch (u) { return fail(onThrow(u)) } }) } class FoldFrame { readonly _xptag = "FoldFrame" constructor( readonly failure: (e: any) => XPure, readonly apply: (e: any) => XPure ) {} } class ApplyFrame { readonly _xptag = "ApplyFrame" constructor(readonly apply: (e: any) => XPure) {} } type Frame = FoldFrame | ApplyFrame class Runtime { stack: Stack | undefined = undefined pop() { const nextInstr = this.stack if (nextInstr) { this.stack = this.stack?.previous } return nextInstr?.value } push(cont: Frame) { this.stack = new Stack(cont, this.stack) } findNextErrorHandler() { let unwinding = true while (unwinding) { const nextInstr = this.pop() if (nextInstr == null) { unwinding = false } else { if (nextInstr._xptag === "FoldFrame") { unwinding = false this.push(new ApplyFrame(nextInstr.failure)) } } } } runAll( self: XPure, s: S1 ): Tp.Tuple<[Chunk.Chunk, E.Either>]> { let s0 = s as any let a: any = undefined let environments: Stack | undefined = undefined let failed = false let curXPure = self as XPure | undefined let logs = Chunk.empty() while (curXPure != null) { concrete(curXPure) const xp = curXPure switch (xp._xptag) { case "FlatMap": { concrete(xp.value) const nested = xp.value const continuation = xp.cont switch (nested._xptag) { case "Succeed": { curXPure = continuation(nested.a) break } case "Modify": { const updated = nested.run(s0) s0 = updated.get(0) a = updated.get(1) curXPure = continuation(a) break } default: { curXPure = nested this.push(new ApplyFrame(continuation)) } } break } case "Log": { logs = Chunk.append_(logs, xp.w) a = undefined const nextInstr = this.pop() curXPure = nextInstr?.apply(a) break } case "Suspend": { curXPure = xp.f() break } case "Succeed": { a = xp.a const nextInstr = this.pop() if (nextInstr) { curXPure = nextInstr.apply(a) } else { curXPure = undefined } break } case "Fail": { this.findNextErrorHandler() const nextInst = this.pop() if (nextInst) { curXPure = nextInst.apply(xp.e) } else { failed = true a = xp.e curXPure = undefined } break } case "Fold": { const state = s0 this.push( new FoldFrame((c) => chain_(set(state), () => xp.failure(c)), xp.success) ) curXPure = xp.value break } case "Access": { curXPure = xp.access(environments?.value || {}) break } case "Get": { curXPure = xp.get(s0) break } case "Provide": { environments = new Stack(xp.r, environments) curXPure = foldM_( xp.cont, (e) => chain_( succeedWith(() => { environments = environments?.previous }), () => fail(e) ), (a) => chain_( succeedWith(() => { environments = environments?.previous }), () => succeed(a) ) ) break } case "Modify": { const updated = xp.run(s0) s0 = updated.get(0) a = updated.get(1) const nextInst = this.pop() if (nextInst) { curXPure = nextInst.apply(a) } else { curXPure = undefined } break } } } if (failed) { return Tp.tuple(logs, E.left(a)) } return Tp.tuple(logs, E.right(Tp.tuple(s0, a))) } } /** * Runs this computation with the specified initial state, returning both the * log and either all the failures that occurred or the updated state and the * result. */ export function runAll_( self: XPure, s: S1 ): Tp.Tuple<[Chunk.Chunk, E.Either>]> { return new Runtime().runAll(self, s) } /** * Runs this computation with the specified initial state, returning either a * failure or the updated state and the result */ export function runAll( s: S1 ): ( self: XPure ) => Tp.Tuple<[Chunk.Chunk, E.Either>]> { return (self) => runAll_(self, s) } /** * Runs this computation to produce its result. */ export function run(self: XPure): A { return runState_(self, undefined).get(1) } /** * Runs this computation with the specified initial state, returning both * the updated state and the result. */ export function runState_( self: XPure, s: S1 ): Tp.Tuple<[S2, A]> { const result = new Runtime().runAll(self, s).get(1) if (result._tag === "Left") { throw result.left } return result.right } /** * Runs this computation with the specified initial state, returning both * the updated state and the result. * * @ets_data_first runState_ */ export function runState( s: S1 ): (self: XPure) => Tp.Tuple<[S2, A]> { return (self) => runState_(self, s) } /** * Runs this computation to produce its result or the first failure to * occur. */ export function runEither( self: XPure ): E.Either { return E.map_(new Runtime().runAll(self, undefined).get(1), (x) => x.get(1)) } /** * Runs this computation to produce its result and the log. */ export function runLog( self: XPure ): Tp.Tuple<[Chunk.Chunk, A]> { const result = new Runtime().runAll(self, undefined) const e = result.get(1) if (e._tag === "Left") { throw e.left } return Tp.tuple(result.get(0), e.right.get(1)) } /** * Runs this computation with the specified initial state, returning the * result and discarding the updated state. */ export function runResult_( self: XPure, s: S1 ): A { return runState_(self, s)[1] } /** * Runs this computation with the specified initial state, returning the * result and discarding the updated state. */ export function runResult( s: S1 ): (self: XPure) => A { return (self) => runResult_(self, s) }