1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 02:44:36 +03:00

Documentation for reiterate.

This commit is contained in:
Rob Rix 2015-10-22 18:31:11 -04:00
parent 397af0d02d
commit 95adbdf042

View File

@ -47,6 +47,13 @@ public func hylo<A, B, Leaf>(down: Syntax<B, Leaf> -> B, _ up: A -> Syntax<A, Le
return up >>> { $0.map(hylo(down, up)) } >>> down return up >>> { $0.map(hylo(down, up)) } >>> down
} }
/// Reiteration through `Syntax`.
///
/// This is a form of hylomorphism (from the Aristotelian philosophy that form and matter are one). As such, it returns a function of type `A B` whose call-tree is linear in the size of the nodes produced by `up`. Conceptually, its the composition of a catamorphism (see also `TermType.cata`, `Free.iterate`) and an anamorphism (see also `Free.ana`, `CofreeType.coiterate`), but is implemented by [Stream fusion](http://lambda-the-ultimate.org/node/2192) and as such enjoys O(n) time complexity, O(1) size complexity, and small constant factors for both (modulo inadvisable implementations of `up` and `down`).
///
/// Hylomorphisms are used to construct diffs corresponding to equal terms; see also `CofreeType.zip`.
///
/// `reiterate` can be used with arbitrary functors which can eliminate to and introduce with `Annotation` & `Syntax` pairs.
public func reiterate<A, B, Leaf, Annotation>(down: (Annotation, Syntax<B, Leaf>) -> B, _ up: A -> (Annotation, Syntax<A, Leaf>)) -> A -> B { public func reiterate<A, B, Leaf, Annotation>(down: (Annotation, Syntax<B, Leaf>) -> B, _ up: A -> (Annotation, Syntax<A, Leaf>)) -> A -> B {
return up >>> { ($0, $1.map(reiterate(down, up))) } >>> down return up >>> { ($0, $1.map(reiterate(down, up))) } >>> down
} }