/*! * Copyright (c) 2017 by The Funfix Project Developers. * Some rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { Either, Right, Left, applyMixins } from "funfix-core" import { HK, Equiv, Constructor, getTypeClassInstance } from "./kinds" import { Apply, ApplyLaws, Applicative, ApplicativeLaws, ApplicativeError, ApplicativeErrorLaws } from "./applicative" /** * The `FlatMap` type class is a lightweight {@link Monad}. * * It exposes [flatMap]{@link FlatMap.flatMap}, which allows to have a * value in a context (`F`) and then feed that into a function that * takes a normal value and returns a value in a context * (`A => F`). * * One motivation for separating this out from `Monad` is that there are * situations where we can implement `flatMap` but not `pure`. For example, * we can implement `map` or `flatMap` that transforms the values of a * `Map` type, but we can't implement `pure` (because we wouldn't * know what key to use when instantiating the new `Map`). * * Must obey the laws defined in {@link FlatMapLaws}. * * Note that having an `Monad` instance implies * {@link Functor} and {@link Apply} implementations are also available, * as `FlatMap` is a subtype of these. * * ## Implementation notes * * Even though in TypeScript the Funfix library is using `abstract class` to * express type classes, when implementing this type class it is recommended * that you implement it as a mixin using "`implements`", instead of extending * it directly with "`extends`". See * [TypeScript: Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html} * for details and note that we already have `applyMixins` defined. * * Implementation example: * * ```typescript * import { * HK, FlatMap, Either, * registerTypeClassInstance, * applyMixins * } from "../src/funfix" * * // Type alias defined for readability. * // HK is our encoding for higher-kinded types. * type BoxK = HK, T> * * class Box implements HK, T> { * constructor(public value: T) {} * * // Implements HK, A>, not really needed, but useful in order * // to avoid type casts. Note they can and should be undefined: * readonly _funKindF: Box * readonly _funKindA: T * } * * class BoxFlatMap implements FlatMap> { * map(fa: BoxK, f: (a: A) => B): Box { * return new Box(f((fa as Box).value)) * } * * flatMap(fa: BoxK, f: (a: A) => BoxK): Box { * return f((fa as Box).value) as Box * } * * tailRecM(a: A, f: (a: A) => BoxK>): Box { * let cursor = a * while (true) { * const box = f(cursor) as Box> * const v = box.value * if (v.isRight()) return new Box(v.get()) * cursor = v.swap().get() * } * } * * // Mixed-in, as these have default implementations * map2: (fa: BoxK, fb: BoxK, f: (a: A, b: B) => Z) => Box * ap: (fa: BoxK, ff: BoxK<(a: A) => B>) => Box * product: (fa: BoxK, fb: BoxK) => Box<[A, B]> * unit: () => Box * followedBy: (fa: BoxK, fb: BoxK) => Box * followedByL: (fa: BoxK, fb: () => BoxK) => Box * forEffect: (fa: BoxK, fb: BoxK) => Box * forEffectL: (fa: BoxK, fb: () => BoxK) => Box * } * * // Call needed in order to implement `map`, `map2`, `product`, etc. * // using the default implementations defined by `FlatMap`, because * // we are using `implements` instead of `extends` above and * // because in this sample we want the default implementations, * // but note that you can always provide your own * applyMixins(BoxFlatMap, [FlatMap]) * * // Registering global Functor instance for Box, needed in order * // for the `functorOf(Box)`, `applyOf(Box)`, `applicativeOf(Box)` * // and `flatMapOf(Box)` calls to work * registerTypeClassInstance(FlatMap)(Box, new BoxFunctor()) * ``` * * We are using `implements` in order to support multiple inheritance and to * avoid inheriting any `static` members. In the Flow definitions (e.g. * `.js.flow` files) for Funfix these type classes are defined with * "`interface`", as they are meant to be interfaces that sometimes have * default implementations and not classes. * * ## Credits * * This type class is inspired by the equivalent in Haskell's * standard library and the implementation is inspired by the * [Typelevel Cats]{@link http://typelevel.org/cats/} project. */ export abstract class FlatMap implements Apply { abstract flatMap(fa: HK, f: (a: A) => HK): HK /** Inherited from {@link Functor.map}. */ abstract map(fa: HK, f: (a: A) => B): HK /** * Keeps calling `f` until a `Right(b)` is returned. * * Based on Phil Freeman's * [Stack Safety for Free]{@link http://functorial.com/stack-safety-for-free/index.pdf}. * * Implementations of this method should use constant stack space relative to `f`. */ abstract tailRecM(a: A, f: (a: A) => HK>): HK /** * Sequentially compose two actions, discarding any value produced * by the first. * * See [followedByL]{@link FlatMap.followedByL} for a lazy version. */ followedBy(fa: HK, fb: HK): HK { return this.flatMap(fa, _ => fb) } /** * Sequentially compose two actions, discarding any value produced * by the first. * * See [followedBy]{@link FlatMap.followedBy} for the strict version. */ followedByL(fa: HK, fb: () => HK): HK { return this.flatMap(fa, _ => fb()) } /** * Sequentially compose two actions, discarding any value * produced by the second. * * See [forEffectL]{@link FlatMap.forEffectL} for the lazy version. */ forEffect(fa: HK, fb: HK): HK { return this.flatMap(fa, a => this.map(fb, _ => a)) } /** * Sequentially compose two actions, discarding any value * produced by the second. * * See [forEffect]{@link FlatMap.forEffect} for the strict version. */ forEffectL(fa: HK, fb: () => HK): HK { return this.flatMap(fa, a => this.map(fb(), _ => a)) } /** Inherited from {@link Apply.ap}. */ ap(fa: HK, ff: HK B>): HK { return this.flatMap(fa, a => this.map(ff, f => f(a))) } /** Inherited from {@link Apply.map2}. */ map2(fa: HK, fb: HK, f: (a: A, b: B) => Z): HK { return this.flatMap(fa, a => this.map(fb, b => f(a, b))) } /** Inherited from {@link Apply.product}. */ product(fa: HK, fb: HK): HK { return this.flatMap(fa, a => this.map(fb, b => [a, b] as [A, B])) } // Implements TypeClass /** @hidden */ static readonly _funTypeId: string = "flatMap" /** @hidden */ static readonly _funSupertypeIds: string[] = ["functor", "apply"] /** @hidden */ static readonly _funErasure: FlatMap } /** * Type class laws defined for {@link FlatMap}. * * This is an abstract definition. In order to use it in unit testing, * the implementor must think of a strategy to evaluate the truthiness * of the returned `Equiv` values. * * Even though in TypeScript the Funfix library is using classes to * express these laws, when implementing this class it is recommended * that you implement it as a mixin using `implements`, instead of extending * it directly with `extends`. See * [TypeScript: Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html} * for details and note that we already have `applyMixins` defined. * * We are doing this in order to support multiple inheritance and to * avoid inheriting any `static` members. In the Flow definitions (e.g. * `.js.flow` files) for Funfix these classes are defined with * `interface`, as they are meant to be interfaces that sometimes have * default implementations and not classes. */ export abstract class FlatMapLaws implements ApplyLaws { /** * The {@link Apply} designated instance for `F`, * to be tested. */ public readonly F: FlatMap /** * ```typescript * fa.flatMap(f).flatMap(g) <-> fa.flatMap(a => f(a).flatMap(g)) * ``` */ flatMapAssociativity(fa: HK, f: (a: A) => HK, g: (b: B) => HK): Equiv> { const F = this.F return Equiv.of( F.flatMap(F.flatMap(fa, f), g), F.flatMap(fa, a => F.flatMap(f(a), g)) ) } /** * ```typescript * fab.ap(fa) <-> fab.flatMap(f => fa.map(f)) * ``` */ flatMapConsistentApply(fa: HK, fab: HK B>): Equiv> { const F = this.F return Equiv.of( F.ap(fa, fab), F.flatMap(fab, f => F.map(fa, f)) ) } /** * ```typescript * fa.followedBy(fb) <-> fa.flatMap(_ => fb) * ``` */ followedByConsistency(fa: HK, fb: HK): Equiv> { const F = this.F return Equiv.of( F.followedBy(fa, fb), F.flatMap(fa, _ => fb) ) } /** * ```typescript * fa.followedBy(() => fb) <-> fa.flatMap(_ => fb) * ``` */ followedByLConsistency(fa: HK, fb: HK): Equiv> { const F = this.F return Equiv.of( F.followedByL(fa, () => fb), F.flatMap(fa, _ => fb) ) } /** * ```typescript * fa.forEffect(fb) <-> fa.flatMap(a => fb.map(_ => a)) * ``` */ forEffectConsistency(fa: HK, fb: HK): Equiv> { const F = this.F return Equiv.of( F.forEffect(fa, fb), F.flatMap(fa, a => F.map(fb, _ => a)) ) } /** * ```typescript * fa.forEffectL(() => fb) <-> fa.flatMap(a => fb.map(_ => a)) * ``` */ forEffectLConsistency(fa: HK, fb: HK): Equiv> { const F = this.F return Equiv.of( F.forEffectL(fa, () => fb), F.flatMap(fa, a => F.map(fb, _ => a)) ) } tailRecMConsistentFlatMap(a: A, f: (a: A) => HK): Equiv> { const F = this.F const bounce = (n: number) => { return F.tailRecM([a, n] as [A, number], x => { const [a0, i] = x return i > 0 ? F.map(f(a0), a1 => Left([a1, i - 1] as [A, number])) : F.map(f(a0), Right) }) } /* * The law is for n >= 1 * bounce(n) == bounce(n - 1).flatMap(f) * * Many monads blow up if n gets too large here (for instance List, becomes * multiplicative, so the memory is exponential in n). */ return Equiv.of(bounce(1), F.flatMap(bounce(0), f)) } /** Mixed-in from {@link FunctorLaws.covariantIdentity}. */ covariantIdentity: (fa: HK) => Equiv> /** Mixed-in from {@link FunctorLaws.covariantComposition}. */ covariantComposition: (fa: HK, f: (a: A) => B, g: (b: B) => C) => Equiv> /** Mixed in from {@link ApplyLaws.applyComposition}. */ applyComposition: (fa: HK, fab: HK B>, fbc: HK C>) => Equiv> /** Mixed in from {@link ApplyLaws.applyProductConsistency}. */ applyProductConsistency: (fa: HK, f: HK B>) => Equiv> /** Mixed in from {@link ApplyLaws.applyMap2Consistency}. */ applyMap2Consistency: (fa: HK, f: HK B>) => Equiv> } applyMixins(FlatMapLaws, [ApplyLaws]) /** * Given a {@link Constructor} reference, returns its associated * {@link FlatMap} instance if it exists, or throws a `NotImplementedError` * in case there's no such association. * * ```typescript * import { Option, FlatMap, flatMapOf } from "funfix" * * const F: FlatMap> = flatMapOf(Option) * ``` */ export const flatMapOf: (c: Constructor) => FlatMap = getTypeClassInstance(FlatMap) /** * Given an {@link FlatMap} instance, returns the {@link FlatMapLaws} * associated with it. */ export function flatMapLawsOf(instance: FlatMap): FlatMapLaws { return new (class extends FlatMapLaws { public readonly F = instance })() } /** * The `Monad` type class. * * Allows composition of dependent effectful functions. * * A `Monad` instance is defined by two operations: * * - `pure` from {@link Applicative}, which lifts an `A` value * in the `F` context * - `flatMap`, which allows us to have a value in a context (`F`) * and then feed that into a function that takes a normal value and * returns a value in a context (`A => F`) * * See [Monads for functional programming]{@link http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf}, * by Philip Wadler. * * Must obey the laws defined in {@link MonadLaws}. * * Note that having an `Applicative` instance implies * {@link Functor}, {@link Apply}, {@link Applicative} and {@link FlatMap} * implementations are also available, as `Monad` is a subtype * of these type classes. * * ## Implementation notes * * Even though in TypeScript the Funfix library is using `abstract class` to * express type classes, when implementing this type class it is recommended * that you implement it as a mixin using "`implements`", instead of extending * it directly with "`extends`". See * [TypeScript: Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html} * for details and note that we already have `applyMixins` defined. * * Implementation example: * * ```typescript * import { * HK, Monad, Either, * registerTypeClassInstance, * applyMixins * } from "../src/funfix" * * // Type alias defined for readability. * // HK is our encoding for higher-kinded types. * type BoxK = HK, T> * * class Box implements HK, T> { * constructor(public value: T) {} * * // Implements HK, A>, not really needed, but useful in order * // to avoid type casts. Note they can and should be undefined: * readonly _funKindF: Box * readonly _funKindA: T * } * * class BoxMonad implements Monad> { * pure(a: A): Box { return new Box(a) } * * flatMap(fa: BoxK, f: (a: A) => BoxK): Box { * return f((fa as Box).value) as Box * } * * tailRecM(a: A, f: (a: A) => BoxK>): Box { * let cursor = a * while (true) { * const box = f(cursor) as Box> * const v = box.value * if (v.isRight()) return new Box(v.get()) * cursor = v.swap().get() * } * } * * // Mixed-in, as these have default implementations * map: (fa: BoxK, f: (a: A) => B) => Box * map2: (fa: BoxK, fb: BoxK, f: (a: A, b: B) => Z) => Box * ap: (fa: BoxK, ff: BoxK<(a: A) => B>) => Box * product: (fa: BoxK, fb: BoxK) => Box<[A, B]> * unit: () => Box * followedBy: (fa: BoxK, fb: BoxK) => Box * followedByL: (fa: BoxK, fb: () => BoxK) => Box * forEffect: (fa: BoxK, fb: BoxK) => Box * forEffectL: (fa: BoxK, fb: () => BoxK) => Box * } * * // Call needed in order to implement `map`, `map2`, `product`, etc. * // using the default implementations defined by `Monad`, because * // we are using `implements` instead of `extends` above and * // because in this sample we want the default implementations, * // but note that you can always provide your own * applyMixins(BoxMonad, [Monad]) * * // Registering global Monad instance for Box, needed in order * // for the `functorOf(Box)`, `applyOf(Box)`, `applicativeOf(Box)`, * // `flatMapOf(Box)` and `monadOf(Box)` calls to work * registerTypeClassInstance(Monad)(Box, new BoxFunctor()) * ``` * * We are using `implements` in order to support multiple inheritance and to * avoid inheriting any `static` members. In the Flow definitions (e.g. * `.js.flow` files) for Funfix these type classes are defined with * "`interface`", as they are meant to be interfaces that sometimes have * default implementations and not classes. * * ## Credits * * This type class is inspired by the equivalent in Haskell's * standard library and the implementation is inspired by the * [Typelevel Cats]{@link http://typelevel.org/cats/} project. */ export abstract class Monad implements FlatMap, Applicative { /** Inherited from {@link Applicative.pure}. */ abstract pure(a: A): HK /** Inherited from {@link FlatMap.flatMap}. */ abstract flatMap(fa: HK, f: (a: A) => HK): HK /** Inherited from {@link FlatMap.tailRecM}. */ abstract tailRecM(a: A, f: (a: A) => HK>): HK /** Inherited from {@link Apply.ap}. */ ap(fa: HK, ff: HK B>): HK { return this.flatMap(fa, a => this.map(ff, f => f(a))) } /** Inherited from {@link Functor.map}. */ map(fa: HK, f: (a: A) => B): HK { return this.flatMap(fa, a => this.pure(f(a))) } /** Inherited from {@link Apply.map2}. */ map2(fa: HK, fb: HK, f: (a: A, b: B) => Z): HK { const F = this return F.flatMap(fa, a => F.map(fb, b => f(a, b))) } /** Inherited from {@link Apply.product}. */ product(fa: HK, fb: HK): HK { const F = this return F.flatMap(fa, a => F.map(fb, b => [a, b] as [A, B])) } /** Mixed-in from {@link Applicative.unit}. */ unit: () => HK /** Mixed-in from {@link FlatMap.followedBy}. */ followedBy: (fa: HK, fb: HK) => HK /** Mixed-in from {@link FlatMap.followedByL}. */ followedByL: (fa: HK, fb: () => HK) => HK /** Mixed-in from {@link FlatMap.forEffect}. */ forEffect: (fa: HK, fb: HK) => HK /** Mixed-in from {@link FlatMap.forEffectL}. */ forEffectL: (fa: HK, fb: () => HK) => HK // Implements TypeClass /** @hidden */ static readonly _funTypeId: string = "monad" /** @hidden */ static readonly _funSupertypeIds: string[] = ["functor", "apply", "applicative", "flatMap"] /** @hidden */ static readonly _funErasure: Monad } applyMixins(Monad, [Applicative, FlatMap]) /** * Type class laws defined for {@link Monad}. * * This is an abstract definition. In order to use it in unit testing, * the implementor must think of a strategy to evaluate the truthiness * of the returned `Equiv` values. * * Even though in TypeScript the Funfix library is using classes to * express these laws, when implementing this class it is recommended * that you implement it as a mixin using `implements`, instead of extending * it directly with `extends`. See * [TypeScript: Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html} * for details and note that we already have `applyMixins` defined. * * We are doing this in order to support multiple inheritance and to * avoid inheriting any `static` members. In the Flow definitions (e.g. * `.js.flow` files) for Funfix these classes are defined with * `interface`, as they are meant to be interfaces that sometimes have * default implementations and not classes. */ export abstract class MonadLaws implements ApplicativeLaws, FlatMapLaws { /** * The {@link Monad} designated instance for `F`, * to be tested. */ public readonly F: Monad monadLeftIdentity(a: A, f: (a: A) => HK): Equiv> { const F = this.F return Equiv.of(F.flatMap(F.pure(a), f), f(a)) } monadRightIdentity(fa: HK): Equiv> { const F = this.F return Equiv.of(F.flatMap(fa, F.pure), fa) } mapFlatMapCoherence(fa: HK, f: (a: A) => B): Equiv> { const F = this.F return Equiv.of(F.flatMap(fa, a => F.pure(f(a))), F.map(fa, f)) } tailRecMStackSafety(): Equiv> { const F = this.F const n = 10000 const res = F.tailRecM(0, i => F.pure(i < n ? Left(i + 1) : Right(i))) return Equiv.of(res, F.pure(n)) } /** Mixed-in from {@link FunctorLaws.covariantIdentity}. */ covariantIdentity: (fa: HK) => Equiv> /** Mixed-in from {@link FunctorLaws.covariantComposition}. */ covariantComposition: (fa: HK, f: (a: A) => B, g: (b: B) => C) => Equiv> /** Mixed-in from {@link ApplyLaws.applyComposition}. */ applyComposition: (fa: HK, fab: HK B>, fbc: HK C>) => Equiv> /** Mixed-in from {@link ApplyLaws.applyProductConsistency}. */ applyProductConsistency: (fa: HK, f: HK B>) => Equiv> /** Mixed-in from {@link ApplyLaws.applyMap2Consistency}. */ applyMap2Consistency: (fa: HK, f: HK B>) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applyComposition}. */ applicativeIdentity: (fa: HK) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeHomomorphism}. */ applicativeHomomorphism: (a: A, f: (a: A) => B) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeInterchange}. */ applicativeInterchange: (a: A, ff: HK B>) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeMap}. */ applicativeMap: (fa: HK, f: (a: A) => B) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeComposition}. */ applicativeComposition: (fa: HK, fab: HK B>, fbc: HK C>) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeUnit}. */ applicativeUnit: (a: A) => Equiv> /** Mixed-in from {@link FlatMapLaws.flatMapAssociativity}. */ flatMapAssociativity: (fa: HK, f: (a: A) => HK, g: (b: B) => HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.flatMapConsistentApply}. */ flatMapConsistentApply: (fa: HK, fab: HK B>) => Equiv> /** Mixed-in from {@link FlatMapLaws.followedByConsistency}. */ followedByConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.followedByLConsistency}. */ followedByLConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.forEffectConsistency}. */ forEffectConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.forEffectLConsistency}. */ forEffectLConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.tailRecMConsistentFlatMap}. */ tailRecMConsistentFlatMap: (a: A, f: (a: A) => HK) => Equiv> } applyMixins(MonadLaws, [ApplicativeLaws, FlatMapLaws]) /** * Given a {@link Constructor} reference, returns its associated * {@link Monad} instance if it exists, or throws a `NotImplementedError` * in case there's no such association. * * ```typescript * import { Option, Monad, monadOf } from "funfix" * * const F: Monad> = monadOf(Option) * ``` */ export const monadOf: (c: Constructor) => Monad = getTypeClassInstance(Monad) /** * Given an {@link Monad} instance, returns the {@link MonadLaws} * associated with it. */ export function monadLawsOf(instance: Monad): MonadLaws { return new (class extends MonadLaws { public readonly F = instance })() } /** * The `MonadError` type class is a {@link Applicative} that * also allows you to raise and or handle an error value. * * This type class allows one to abstract over error-handling * applicative types. * * MUST follow the law defined in {@link MonadErrorLaws}. * * ## Implementation notes * * Even though in TypeScript the Funfix library is using `abstract class` to * express type classes, when implementing this type class it is recommended * that you implement it as a mixin using "`implements`", instead of extending * it directly with "`extends`". See * [TypeScript: Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html} * for details and note that we already have `applyMixins` defined. * * Implementation example: * * ```typescript * import { * HK, * MonadError, * registerTypeClassInstance, * applyMixins, * Try * } from "funfix" * * // Type alias defined for readability. * // HK is our encoding for higher-kinded types. * type BoxK = HK, T> * * class Box implements HK, T> { * constructor(public value: Try) {} * * // Implements HK, A>, not really needed, but useful in order * // to avoid type casts. Note they can and should be undefined: * readonly _funKindF: Box * readonly _funKindA: T * } * * class BoxMonadError implements MonadError, any> { * pure(a: A): Box { return new Box(Try.success(a)) } * * flatMap(fa: BoxK, f: (a: A) => BoxK): Box { * throw new NotImplementedError("Provide implementation") * } * * tailRecM(a: A, f: (a: A) => BoxK>): Box { * throw new NotImplementedError("Provide implementation") * } * * raise(e: any): HK, A> { * return new Box(Try.failure(e)) * } * * recoverWith(fa: BoxK, f: (e: any) => BoxK): HK, A> { * return new Box((fa as Box).value.recoverWith(e => (f(e) as Box).value)) * } * * // Mixed in * map: (fa: BoxK, f: (a: A) => B) => Box * map2: (fa: BoxK, fb: BoxK, f: (a: A, b: B) => Z) => Box * ap: (fa: BoxK, ff: BoxK<(a: A) => B>) => Box * product: (fa: BoxK, fb: BoxK) => Box<[A, B]> * unit: () => Box * followedBy: (fa: BoxK, fb: BoxK) => Box * followedByL: (fa: BoxK, fb: () => BoxK) => Box * forEffect: (fa: BoxK, fb: BoxK) => Box * forEffectL: (fa: BoxK, fb: () => BoxK) => Box * recover: (fa: HK, A>, f: (e: any) => A) => HK, A> * attempt: (fa: HK, A>) => HK, Either> * } * * // Call needed in order to implement `map`, `map2`, `product`, etc. * // using the default implementations defined by `MonadError`, * // because we are using `implements` instead of `extends` above and * // because in this sample we want the default implementations, * // but note that you can always provide your own * applyMixins(BoxMonadError, [MonadError]) * * // Registering global MonadError instance for Box, needed in order * // for the `functorOf(Box)`, `applyOf(Box)`, `applicativeOf(Box)` * // and `monadErrorOf(Box)` calls to work * registerTypeClassInstance(MonadError)(Box, new BoxMonadError()) * ``` * * We are using `implements` in order to support multiple inheritance and to * avoid inheriting any `static` members. In the Flow definitions (e.g. * `.js.flow` files) for Funfix these type classes are defined with * "`interface`", as they are meant to be interfaces that sometimes have * default implementations and not classes. * * ## Credits * * This type class is inspired by the equivalent in Haskell's * standard library and the implementation is inspired by the * [Typelevel Cats]{@link http://typelevel.org/cats/} project. */ export abstract class MonadError implements ApplicativeError, Monad { /** Inherited from {@link Applicative.pure}. */ abstract pure(a: A): HK /** Inherited from {@link ApplicativeError.raise}. */ abstract raise(e: E): HK /** Inherited from {@link FlatMap.flatMap}. */ abstract flatMap(fa: HK, f: (a: A) => HK): HK /** Inherited from {@link FlatMap.tailRecM}. */ abstract tailRecM(a: A, f: (a: A) => HK>): HK /** Inherited from {@link ApplicativeError.recoverWith}. */ abstract recoverWith(fa: HK, f: (e: E) => HK): HK /** Mixed-in from {@link ApplicativeError.recover}. */ recover: (fa: HK, f: (e: E) => A) => HK /** Mixed-in from {@link ApplicativeError.attempt}. */ attempt: (fa: HK) => HK> /** Mixed-in from {@link Applicative.unit}. */ unit: () => HK /** Mixed-in from {@link Applicative.map}. */ map: (fa: HK, f: (a: A) => B) => HK /** Mixed-in from {@link Apply.map2}. */ map2: (fa: HK, fb: HK, f: (a: A, b: B) => Z) => HK /** Mixed-in from {@link Apply.product}. */ product: (fa: HK, fb: HK) => HK /** Mixed-in from {@link FlatMap.followedBy}. */ followedBy: (fa: HK, fb: HK) => HK /** Mixed-in from {@link FlatMap.followedByL}. */ followedByL: (fa: HK, fb: () => HK) => HK /** Mixed-in from {@link FlatMap.forEffect}. */ forEffect: (fa: HK, fb: HK) => HK /** Mixed-in from {@link FlatMap.forEffectL}. */ forEffectL: (fa: HK, fb: () => HK) => HK /** Mixed-in from {@link Monad.ap}. */ ap: (fa: HK, ff: HK B>) => HK // Implements TypeClass /** @hidden */ static readonly _funTypeId: string = "monadError" /** @hidden */ static readonly _funSupertypeIds: string[] = ["functor", "apply", "applicative", "monad", "applicativeError"] /** @hidden */ static readonly _funErasure: MonadError } applyMixins(MonadError, [Monad, ApplicativeError]) /** * Type class laws defined for {@link MonadError}. * * This is an abstract definition. In order to use it in unit testing, * the implementor must think of a strategy to evaluate the truthiness * of the returned `Equiv` values. * * Even though in TypeScript the Funfix library is using classes to * express these laws, when implementing this class it is recommended * that you implement it as a mixin using `implements`, instead of extending * it directly with `extends`. See * [TypeScript: Mixins]{@link https://www.typescriptlang.org/docs/handbook/mixins.html} * for details and note that we already have `applyMixins` defined. * * We are doing this in order to support multiple inheritance and to * avoid inheriting any `static` members. In the Flow definitions (e.g. * `.js.flow` files) for Funfix these classes are defined with * `interface`, as they are meant to be interfaces that sometimes have * default implementations and not classes. */ export abstract class MonadErrorLaws implements ApplicativeErrorLaws, MonadLaws { /** * The {@link MonadError} designated instance for `F`, * to be tested. */ public readonly F: MonadError monadErrorLeftZero(e: E, f: (a: A) => HK): Equiv> { const F = this.F return Equiv.of(F.flatMap(F.raise(e), f), F.raise(e)) } /** Mixed-in from {@link ApplicativeErrorLaws.applicativeErrorRecoverWith}. */ applicativeErrorRecoverWith: (e: E, f: (e: E) => HK) => Equiv> /** Mixed-in from {@link ApplicativeErrorLaws.applicativeErrorRecover}. */ applicativeErrorRecover: (e: E, f: (e: E) => A) => Equiv> /** Mixed-in from {@link ApplicativeErrorLaws.recoverWithPure}. */ recoverWithPure: (a: A, f: (e: E) => HK) => Equiv> /** Mixed-in from {@link ApplicativeErrorLaws.recoverPure}. */ recoverPure: (a: A, f: (e: E) => A) => Equiv> /** Mixed-in from {@link ApplicativeErrorLaws.raiseErrorAttempt}. */ raiseErrorAttempt: (e: E) => Equiv>> /** Mixed-in from {@link ApplicativeErrorLaws.pureAttempt}. */ pureAttempt: (a: A) => Equiv>> /** Mixed-in from {@link MonadLaws.monadLeftIdentity}. */ monadLeftIdentity: (a: A, f: (a: A) => HK) => Equiv> /** Mixed-in from {@link MonadLaws.monadRightIdentity}. */ monadRightIdentity: (fa: HK) => Equiv> /** Mixed-in from {@link MonadLaws.mapFlatMapCoherence}. */ mapFlatMapCoherence: (fa: HK, f: (a: A) => B) => Equiv> /** Mixed-in from {@link MonadLaws.tailRecMStackSafety}. */ tailRecMStackSafety: () => Equiv> /** Mixed-in from {@link FlatMapLaws.flatMapAssociativity}. */ flatMapAssociativity: (fa: HK, f: (a: A) => HK, g: (b: B) => HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.flatMapConsistentApply}. */ flatMapConsistentApply: (fa: HK, fab: HK B>) => Equiv> /** Mixed-in from {@link FlatMapLaws.followedByConsistency}. */ followedByConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.followedByLConsistency}. */ followedByLConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.forEffectConsistency}. */ forEffectConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.forEffectLConsistency}. */ forEffectLConsistency: (fa: HK, fb: HK) => Equiv> /** Mixed-in from {@link FlatMapLaws.tailRecMConsistentFlatMap}. */ tailRecMConsistentFlatMap: (a: A, f: (a: A) => HK) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeIdentity}. */ applicativeIdentity: (fa: HK) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeHomomorphism}. */ applicativeHomomorphism: (a: A, f: (a: A) => B) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeInterchange}. */ applicativeInterchange: (a: A, ff: HK B>) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeMap}. */ applicativeMap: (fa: HK, f: (a: A) => B) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeComposition}. */ applicativeComposition: (fa: HK, fab: HK B>, fbc: HK C>) => Equiv> /** Mixed-in from {@link ApplicativeLaws.applicativeUnit}. */ applicativeUnit: (a: A) => Equiv> /** Mixed-in from {@link FunctorLaws.covariantIdentity}. */ covariantIdentity: (fa: HK) => Equiv> /** Mixed-in from {@link FunctorLaws.covariantComposition}. */ covariantComposition: (fa: HK, f: (a: A) => B, g: (b: B) => C) => Equiv> /** Mixed-in from {@link ApplyLaws.applyComposition}. */ applyComposition: (fa: HK, fab: HK B>, fbc: HK C>) => Equiv> /** Mixed-in from {@link ApplyLaws.applyProductConsistency}. */ applyProductConsistency: (fa: HK, f: HK B>) => Equiv> /** Mixed-in from {@link ApplyLaws.applyMap2Consistency}. */ applyMap2Consistency: (fa: HK, f: HK B>) => Equiv> } applyMixins(MonadErrorLaws, [MonadLaws, ApplicativeErrorLaws]) /** * Given a {@link Constructor} reference, returns its associated * {@link MonadError} instance if it exists, or throws a `NotImplementedError` * in case there's no such association. * * ```typescript * import { IO, MonadError, monadErrorOf } from "funfix" * * const F: MonadError> = monadErrorOf(IO) * ``` */ export const monadErrorOf: (c: Constructor) => MonadError = getTypeClassInstance(MonadError) /** * Given an {@link MonadError} instance, returns the * {@link MonadErrorLaws} associated with it. */ export function monadErrorLawsOf(instance: MonadError): MonadErrorLaws { return new (class extends MonadErrorLaws { public readonly F = instance })() }