/*! * 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 } from "./kinds"; import { Functor, FunctorLaws } from "./functor"; /** * 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 declare 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; /** @hidden */ static readonly _funSupertypeIds: string[]; /** @hidden */ static readonly _funErasure: CoflatMap; } /** * 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 declare abstract class CoflatMapLaws implements FunctorLaws { /** * The {@link CoflatMap} designated instance for `F`, * to be tested. */ 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>; /** * ``` * fa.coflatten.coflatten <-> fa.coflatten.map(_.coflatten) * ``` */ coflattenThroughMap(fa: HK): Equiv>>>; /** * ``` * fa.coflatMap(f) <-> fa.coflatten.map(f) * ``` */ coflattenCoherence(fa: HK, f: (a: HK) => B): Equiv>; /** * ``` * fa.coflatten <-> fa.coflatMap(identity) * ``` */ 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>; } /** * 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 declare const coflatMapOf: (c: Constructor) => CoflatMap; /** * Given an {@link CoflatMap} instance, returns the {@link CoflatMapLaws} * associated with it. */ export declare function coflatMapLawsOf(instance: CoflatMap): CoflatMapLaws; /** * `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 declare 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>; /** @hidden */ static readonly _funTypeId: string; /** @hidden */ static readonly _funSupertypeIds: string[]; /** @hidden */ static readonly _funErasure: Comonad; } /** * 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 declare abstract class ComonadLaws implements CoflatMapLaws { /** * The {@link Comonad} designated instance for `F`, * to be tested. */ readonly F: Comonad; /** * ``` * fa.coflatten.extract <-> fa * ``` */ extractCoflattenIdentity(fa: HK): Equiv>; /** * ``` * fa.coflatten.map(_.extract) <-> fa * ``` */ mapCoflattenIdentity(fa: HK): Equiv>; /** * ``` * fa.map(f) <-> fa.coflatMap(fa0 => f(fa0.extract)) * ``` */ mapCoflatMapCoherence(fa: HK, f: (a: A) => B): Equiv>; /** * ``` * fa.coflatMap(_.extract) <-> fa * ``` */ comonadLeftIdentity(fa: HK): Equiv>; /** * ``` * fa.coflatMap(f).extract <-> f(fa) * ``` */ comonadRightIdentity(fa: HK, f: (a: HK) => B): Equiv; /** 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>; } /** * 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 declare const comonadOf: (c: Constructor) => Comonad; /** * Given an {@link Comonad} instance, returns the {@link ComonadLaws} * associated with it. */ export declare function comonadLawsOf(instance: Comonad): ComonadLaws;