From 6bc8fe428553539dfc352ddc0926e009779f33a2 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Thu, 22 Oct 2015 18:04:51 -0400 Subject: [PATCH] Define `coiterate` over `CofreeType`. --- prototype/Doubt/Cofree.swift | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/prototype/Doubt/Cofree.swift b/prototype/Doubt/Cofree.swift index 71ea55a8a..98044a58a 100644 --- a/prototype/Doubt/Cofree.swift +++ b/prototype/Doubt/Cofree.swift @@ -17,16 +17,6 @@ public enum Cofree { public init(_ annotation: Annotation, _ syntax: Syntax) { self = .Unroll(annotation, syntax) } - - - /// Constructs a cofree by coiteration. - /// - /// This is an _anamorphism_ (from the Greek “ana,” “upwards”; compare “anabolism”), a generalization of unfolds over regular trees (and datatypes isomorphic to them). The initial seed is used as the annotation of the returned value. The continuation of the structure is unpacked by applying `annotate` to the seed and mapping the resulting syntax’s values recursively. In this manner, the structure is unfolded bottom-up, starting with `seed` and ending at the leaves. - /// - /// As this is the dual of `Free.iterate`, it’s unsurprising that we have a similar guarantee: coiteration is linear in the size of the constructed tree. - public static func coiterate(unfold: Annotation -> Syntax)(_ seed: Annotation) -> Cofree { - return (curry(Unroll)(seed) <<< { $0.map(coiterate(unfold)) } <<< unfold) <| seed - } } @@ -125,6 +115,15 @@ extension CofreeType { public static func Wrap(annotation: Annotation)(syntax: Syntax) -> Self { return Self(annotation, syntax) } + + /// Constructs a cofree by coiteration. + /// + /// This is an _anamorphism_ (from the Greek “ana,” “upwards”; compare “anabolism”), a generalization of unfolds over regular trees (and datatypes isomorphic to them). The initial seed is used as the annotation of the returned value. The continuation of the structure is unpacked by applying `annotate` to the seed and mapping the resulting syntax’s values recursively. In this manner, the structure is unfolded bottom-up, starting with `seed` and ending at the leaves. + /// + /// As this is the dual of `Free.iterate`, it’s unsurprising that we have a similar guarantee: coiteration is linear in the size of the constructed tree. + public static func coiterate(unfold: Annotation -> Syntax)(_ seed: Annotation) -> Self { + return (Wrap(seed) <<< { $0.map(coiterate(unfold)) } <<< unfold) <| seed + } } extension Cofree: CofreeType {}