/*! * 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 { HK, Equiv, Constructor, getTypeClassInstance } from "./kinds" import { Functor, FunctorLaws } from "./functor" import { applyMixins, id } from "funfix-core" /** * The `CoflatMap` type class, a weaker version of {@link Comonad}, * exposing `coflatMap`, but not `extract`. * * This type class is exposed in addition to `Comonad` because * there are data types for which we can't implement `extract`, but * that could still benefit from an `coflatMap` definition. * * MUST obey the laws defined in {@link CoflatMapLaws}. * * Note that having a `CoflatMap` instance implies that a * {@link Functor} implementation is also available, which is why * `CoflatMap` is a subtype of `Functor`. * * ## 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, CoflatMap, * registerTypeClassInstance, * applyMixins * } 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: T) {} * * // Implements HK, A>, not really needed, but useful in order * // to avoid type casts. Note these can and should be undefined: * readonly _funKindF: Box * readonly _funKindA: T * } * * class BoxCoflatMap implements CoflatMap> { * map(fa: BoxK, f: (a: A) => B): Box { * const a = (fa as Box).value * return new Box(f(a)) * } * * coflatMap(fa: BoxK, ff: (a: BoxK) => B): BoxK { * return new Box(Success(ff(fa))) * } * * coflatten(fa: BoxK): BoxK> { * return new Box(Success(fa)) * } * } * * // At the moment of writing, this call is not needed, but it is * // recommended anyway to future-proof the code ;-) * applyMixins(BoxCoflatMap, [CoflatMap]) * * // Registering global CoflatMap instance for Box, needed in order * // for the `coflatMapOf(Box)` calls to work * registerTypeClassInstance(CoflatMap)(Box, new BoxCoflatMap()) * ``` * * 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 CoflatMap implements Functor { /** * `coflatMap` is the dual of `flatMap` on {@link FlatMap}. * * It applies a value in a context to a function that takes a * value in a context and returns a normal value. */ abstract coflatMap(fa: HK, ff: (a: HK) => B): HK /** * `coflatten` is the dual of `flatten` on {@link FlatMap}. * * Whereas `flatten` removes a layer of `F`, coflatten adds a * layer of `F`. */ abstract coflatten(fa: HK): HK> /** Inherited from {@link Functor.map}. */ map: (fa: HK, f: (a: A) => B) => HK /** @hidden */ static readonly _funTypeId: string = "coflatMap" /** @hidden */ static readonly _funSupertypeIds: string[] = ["functor"] /** @hidden */ static readonly _funErasure: CoflatMap } applyMixins(CoflatMap, [Functor]) /** * Type class laws defined for {@link CoflatMap}. * * 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 CoflatMapLaws implements FunctorLaws { /** * The {@link CoflatMap} designated instance for `F`, * to be tested. */ public readonly F: CoflatMap /** * ``` * fa.coflatMap(f).coflatMap(g) <-> fa.coflatMap(x => g(x.coflatMap(f))) * ``` */ coflatMapAssociativity(fa: HK, f: (a: HK) => B, g: (b: HK) => C): Equiv> { const F = this.F return Equiv.of( F.coflatMap(F.coflatMap(fa, f), g), F.coflatMap(fa, a => g(F.coflatMap(a, f))) ) } /** * ``` * fa.coflatten.coflatten <-> fa.coflatten.map(_.coflatten) * ``` */ coflattenThroughMap(fa: HK): Equiv>>> { const F = this.F return Equiv.of( F.coflatten(F.coflatten(fa)), F.map(F.coflatten(fa), F.coflatten) ) } /** * ``` * fa.coflatMap(f) <-> fa.coflatten.map(f) * ``` */ coflattenCoherence(fa: HK, f: (a: HK) => B): Equiv> { const F = this.F return Equiv.of( F.coflatMap(fa, f), F.map(F.coflatten(fa), f) ) } /** * ``` * fa.coflatten <-> fa.coflatMap(identity) * ``` */ coflatMapIdentity(fa: HK): Equiv>> { const F = this.F return Equiv.of( F.coflatten(fa), F.coflatMap(fa, id) ) } /** 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> } applyMixins(CoflatMapLaws, [FunctorLaws]) /** * Given a {@link Constructor} reference, returns its associated * {@link CoflatMap} instance if it exists, or throws a `NotImplementedError` * in case there's no such association. * * ```typescript * import { Option, CoflatMap, coflatMapOf } from "funfix" * * const F: CoflatMap> = coflatMapOf(Option) * ``` */ export const coflatMapOf: (c: Constructor) => CoflatMap = getTypeClassInstance(CoflatMap) /** * Given an {@link CoflatMap} instance, returns the {@link CoflatMapLaws} * associated with it. */ export function coflatMapLawsOf(instance: CoflatMap): CoflatMapLaws { return new (class extends CoflatMapLaws { public readonly F = instance })() } /** * `Comonad` is the dual of {@link Monad}. * * Whereas Monads allow for the composition of effectful functions, * Comonads allow for composition of functions that extract the * value from their context. * * Example: * * ```typescript * const F = comonadOf(Eval) * * F.extract(Eval.of(() => 2)) // 2 * ``` * * Note that having an `Comonad` instance implies {@link Functor} and * {@link CoflatMap} implementations are also available, which is why * `Comonad` is a subtype of `Functor` and `CoflatMap`. * * ## 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, Comonad, * registerTypeClassInstance, * applyMixins * } 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: 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 BoxComonad implements Comonad> { * map(fa: BoxK, f: (a: A) => B): BoxK { * const a = (fa as Box).value * return new Box(f(a)) * } * * coflatMap(fa: BoxK, ff: (a: BoxK) => B): BoxK { * return new Box(ff(fa)) * } * * coflatten(fa: BoxK): BoxK> { * return new Box(fa) * } * * extract(fa: BoxK): A { * return (fa as Box).value * } * } * * // At the moment of writing, this call is not needed, but it is * // recommended anyway to future-proof the code ;-) * applyMixins(BoxComonad, [Comonad]) * * // Registering global Comonad instance for Box, needed in order * // for the `functorOf(Box)`, `coflatMapOf(Box)` and `comonadOf(Box)` * // calls to work * registerTypeClassInstance(Comonad)(Box, new BoxComonad()) * ``` * * 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 Comonad implements CoflatMap { /** * `extract` is the dual of `pure` on {@link Monad} * (via {@link Applicative}) and extracts the value from * its context. * * Example: * * ```typescript * const cm = comonadOf(Eval) * * cm.extract(Eval.of(() => 10)) //=> 10 * ``` */ abstract extract(fa: HK): A /** Inherited from {@link Functor.map}. */ map: (fa: HK, f: (a: A) => B) => HK /** Inherited from {@link CoflatMap.coflatMap}. */ coflatMap: (fa: HK, ff: (a: HK) => B) => HK /** Inherited from {@link CoflatMap.coflatten}. */ coflatten: (fa: HK) => HK> // Implements TypeClass /** @hidden */ static readonly _funTypeId: string = "comonad" /** @hidden */ static readonly _funSupertypeIds: string[] = ["functor", "coflatMap"] /** @hidden */ static readonly _funErasure: Comonad } applyMixins(Comonad, [CoflatMap]) /** * Type class laws defined for {@link Comonad}. * * 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 ComonadLaws implements CoflatMapLaws { /** * The {@link Comonad} designated instance for `F`, * to be tested. */ public readonly F: Comonad /** * ``` * fa.coflatten.extract <-> fa * ``` */ extractCoflattenIdentity(fa: HK): Equiv> { const F = this.F return Equiv.of( F.extract(F.coflatten(fa)), fa ) } /** * ``` * fa.coflatten.map(_.extract) <-> fa * ``` */ mapCoflattenIdentity(fa: HK): Equiv> { const F = this.F return Equiv.of( F.map(F.coflatten(fa), F.extract), fa ) } /** * ``` * fa.map(f) <-> fa.coflatMap(fa0 => f(fa0.extract)) * ``` */ mapCoflatMapCoherence(fa: HK, f: (a: A) => B): Equiv> { const F = this.F return Equiv.of( F.map(fa, f), F.coflatMap(fa, fa0 => f(F.extract(fa0))) ) } /** * ``` * fa.coflatMap(_.extract) <-> fa * ``` */ comonadLeftIdentity(fa: HK): Equiv> { const F = this.F return Equiv.of( F.coflatMap(fa, F.extract), fa ) } /** * ``` * fa.coflatMap(f).extract <-> f(fa) * ``` */ comonadRightIdentity(fa: HK, f: (a: HK) => B): Equiv { const F = this.F return Equiv.of( F.extract(F.coflatMap(fa, f)), f(fa) ) } /** Mixed-in from {@link CoflatMapLaws.coflatMapAssociativity}. */ coflatMapAssociativity: (fa: HK, f: (a: HK) => B, g: (b: HK) => C) => Equiv> /** Mixed-in from {@link CoflatMapLaws.coflattenThroughMap}. */ coflattenThroughMap: (fa: HK) => Equiv>>> /** Mixed-in from {@link CoflatMapLaws.coflattenCoherence}. */ coflattenCoherence: (fa: HK, f: (a: HK) => B) => Equiv> /** Mixed-in from {@link CoflatMapLaws.coflatMapIdentity}. */ coflatMapIdentity: (fa: HK) => 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> } applyMixins(ComonadLaws, [CoflatMapLaws]) /** * Given a {@link Constructor} reference, returns its associated * {@link Comonad} instance if it exists, or throws a `NotImplementedError` * in case there's no such association. * * ```typescript * import { Option, Comonad, comonadOf } from "funfix" * * const F: Comonad> = comonadOf(Option) * ``` */ export const comonadOf: (c: Constructor) => Comonad = getTypeClassInstance(Comonad) /** * Given an {@link Comonad} instance, returns the {@link ComonadLaws} * associated with it. */ export function comonadLawsOf(instance: Comonad): ComonadLaws { return new (class extends ComonadLaws { public readonly F = instance })() }