flix

0.78.0

Functor.flix

/*
 *  Copyright 2020 Matthew Lutze, Esben Bjerre, Magnus Madsen
 *
 * Use of this source code is governed by the Apache 2.0 license
 * that can be found in the LICENSE.md file.
 */

///
/// Trait for types that can be mapped over, and its functions.
///
pub mod Functor {

    ///
    /// Trait for types that can be mapped over.
    ///
    pub trait Functor[m: Type -> Type] {
        ///
        /// Applies the function `f` to `x` preserving its structure.
        ///
        pub def map(f: a -> b \ ef, x: m[a]): m[b] \ ef
    }

    ///
    /// Replaces the value `a` in `s` by the given value `x` preserving the structure of `s`.
    ///
    pub def replace [a: Type, b: Type, m: Type -> Type] (s: m[a], x: b): m[b] with Functor[m] = Functor.map(_ -> x, s)

    ///
    /// Replaces the value `a` in `s` with `Unit` preserving the structure of `s`.
    ///
    /// This function is typically used to discard the return value of computing `s`.
    ///
    pub def ignore(s: m[a]): m[Unit] with Functor[m] = s `Functor.replace` ()

    ///
    /// `<$>` is an operator alias for `map`.
    ///
    pub def <$>(f: a -> b \ ef, x: m[a]): m[b] \ ef with Functor[m] = Functor.map(f, x)

    ///
    /// `<$$>` is the operator `<$>` with its arguments flipped.
    ///
    pub def <$$>(x: m[a], f: a -> b \ ef): m[b] \ ef with Functor[m] = Functor.map(f, x)

    ///
    /// `<$` is the operator `$>` with its arguments flipped.
    ///
    pub def <$(x: a, ma: f[b]): f[a] with Functor[f] = Functor.replace(ma, x)

    ///
    /// `$>` is an operator alias for `replace`.
    ///
    pub def $>(ma: f[a], x: b): f[b] with Functor[f] = Functor.replace(ma, x)

}