//! This file defines the behaviour of hylomorphisms. //! //! A hylomorphism is a composition of an anamorphism with a //! catamorphism. But it is separated into a distinct trait as we can //! implement hylomorphisms more efficiently by short-circuiting //! during the expansion by the anamorphism. use crate::{ algebra::Algebra, catana::{Ana, Cata}, coalgebra::Coalgebra, functor::Functor, }; /// A type implementing Hylo is able to expand from a value of type /// `U` into a recursive structure holding values of type `G`, and /// also to collapse from that recursive structure to a value of type /// `T`. /// /// Types which implement [`Cata`] and [`Ana`] compatibly can /// automatically implement [`Hylo`] as follows. /// /// But of course this is not efficient, and types should implement in /// a more efficient manner, if available. /// /// ```ignore /// Inter::ana(value, coalg).cata(alg) /// ``` pub trait Hylo: Cata + Ana where F: Functor, G: Functor = F>, H: Functor = G>, A: Algebra, C: Coalgebra, { /// Expand from `value` to an intermediate recursive structure, by /// means of a coalgebra, and then collapse into the result value, /// by means of an algebra. fn hylo(value: U, alg: A, coalg: C) -> T; }