import { AtomicPromise } from './promise'; import { curry } from './curry'; import { push } from './array'; export interface Maybe { fmap(f: (a: a) => b): Maybe; ap(this: Maybe<(a: a) => z>, a: Maybe): Maybe; ap(this: Maybe<(a: a, b: b) => z>, a: Maybe): Maybe<(b: b) => z>; ap(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe): Maybe<(b: b, c: c) => z>; ap(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe): Maybe<(b: b, c: c, d: d) => z>; ap(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe): Maybe<(b: b, c: c, d: d, e: e) => z>; bind(f: (a: a) => Maybe): Maybe; join(this: Maybe>): Maybe; guard(cond: boolean): Maybe; extract(): a; extract(nothing: () => a): a; extract(nothing: () => b): a | b; extract(nothing: () => b, just: (a: a) => b): b; } class Just implements Maybe { constructor( private readonly value: a, ) { } public fmap(f: (a: a) => b): Just { return new Just(f(this.value)); } public ap(this: Maybe<(a: a) => z>, a: Maybe): Maybe; public ap(this: Maybe<(a: a, b: b) => z>, a: Maybe): Maybe<(b: b) => z>; public ap(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe): Maybe<(b: b, c: c) => z>; public ap(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe): Maybe<(b: b, c: c, d: d) => z>; public ap(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe): Maybe<(b: b, c: c, d: d, e: e) => z>; public ap(this: Maybe<(...as: any[]) => z>, a: Maybe): Maybe { return Maybe.ap(this, a); } public bind(f: (a: a) => Just): Just; public bind(f: (a: a) => Nothing): Nothing; public bind(f: (a: a) => Maybe): Maybe; public bind(f: (a: a) => Maybe): Maybe { return f(this.value); } public join(this: Just>): Maybe { return this.value; } public guard(cond: boolean): Maybe { return cond ? this : Maybe.mzero; } public extract(): a; public extract(nothing: () => a): a; public extract(nothing: () => b): a | b; public extract(nothing: () => b, just: (a: a) => b): b; public extract(nothing?: () => b, just?: (a: a) => b): a | b { if (just !== undefined) return just(this.value); return this.value; assert(nothing); } } class Nothing implements Maybe { public fmap(f: (a: never) => b): Nothing { return this; assert(f); } public ap(this: Maybe<(a: a) => z>, a: Maybe): Nothing; public ap(this: Maybe<(a: a, b: b) => z>, a: Maybe): Nothing; public ap(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe): Nothing; public ap(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe): Nothing; public ap(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe): Nothing; public ap(this: Maybe<(...as: any[]) => z>, _: Maybe): Nothing { return this as Nothing; } public bind(f: (a: never) => Maybe): Nothing { return this; assert(f); } public join(this: Maybe>): Nothing { return this as Nothing; } public guard(cond: boolean): Nothing { return this; assert(cond); } public extract(): never; public extract(nothing: () => b): b; public extract(nothing: () => b, just: (a: never) => b): b; public extract(nothing?: () => b): b { if (nothing !== undefined) return nothing(); throw new Error(`Spica: Maybe: Nothing value is extracted`); assert(just); } } type just = Just; function just(value: a): Just { return new Just(value); } type nothing = Nothing; const nothing = new Nothing(); export { just as Just, nothing as Nothing, }; export namespace Maybe { export function fmap(f: (a: a) => b, m: Maybe): Maybe { return m.fmap(f); } export const pure = just; export function ap(mf: Maybe<(a: a) => b>, ma: Maybe): Maybe; export function ap(mf: Maybe<(a: a) => b>): (ma: Maybe) => Maybe; export function ap(af: Maybe<(a: a) => b>, aa?: Maybe): Maybe | ((aa: Maybe) => Maybe) { return aa ? af.bind(f => aa.fmap(curry(f))) : (aa: Maybe) => ap(af, aa); } export const Return = pure; export function bind(f: (a: a) => Maybe, m: Maybe): Maybe { return m.bind(f); } export function sequence(fm: Maybe[]): Maybe; export function sequence(fm: Maybe>): AtomicPromise>; export function sequence(fm: Maybe[] | Maybe>): Maybe | AtomicPromise> { return Array.isArray(fm) ? fm.reduce((acc, m) => acc.bind(as => m.fmap(a => push(as, [a]))), Return([])) : fm.extract(() => AtomicPromise.resolve(mzero), a => AtomicPromise.resolve(a).then(Return)); } export const mzero = nothing; export function mplus(ml: Maybe, mr: Maybe): Maybe { return ml.extract(() => mr, () => ml); } }