mirror of
https://github.com/github/semantic.git
synced 2024-11-29 02:44:36 +03:00
Rename the A type parameter to Leaf.
This commit is contained in:
parent
810c6081b9
commit
20ef96b483
@ -3,10 +3,10 @@
|
||||
/// This is “free” in the sense of “unconstrained” rather than “zero-cost”; it’s the comonad obtained by taking a functor (in this case `Syntax`) and adding the minimum necessary details (the `Annotation` paired with it) to satisfy the comonad laws.
|
||||
///
|
||||
/// This type is dual to `Free`. Where `Free` is inhabited by syntax trees where some terms are replaced with `Annotation`s, `Cofree` is inhabited by syntax trees where all terms are annotated with `Annotation`s. In Doubt, this allows us to e.g. annotate terms with source range information, categorization, etc.
|
||||
public enum Cofree<A, Annotation> {
|
||||
indirect case Unroll(Annotation, Syntax<Cofree, A>)
|
||||
public enum Cofree<Leaf, Annotation> {
|
||||
indirect case Unroll(Annotation, Syntax<Cofree, Leaf>)
|
||||
|
||||
public var unwrap: Syntax<Cofree, A> {
|
||||
public var unwrap: Syntax<Cofree, Leaf> {
|
||||
switch self {
|
||||
case let .Unroll(_, rest):
|
||||
return rest
|
||||
@ -14,7 +14,7 @@ public enum Cofree<A, Annotation> {
|
||||
}
|
||||
|
||||
|
||||
public init(_ annotation: Annotation, _ syntax: Syntax<Cofree, A>) {
|
||||
public init(_ annotation: Annotation, _ syntax: Syntax<Cofree, Leaf>) {
|
||||
self = .Unroll(annotation, syntax)
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public enum Cofree<A, Annotation> {
|
||||
/// 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(annotate: Annotation -> Syntax<Annotation, A>)(_ seed: Annotation) -> Cofree {
|
||||
public static func coiterate(annotate: Annotation -> Syntax<Annotation, Leaf>)(_ seed: Annotation) -> Cofree {
|
||||
return .Unroll(seed, annotate(seed).map(coiterate(annotate)))
|
||||
}
|
||||
}
|
||||
@ -42,7 +42,7 @@ extension Cofree: CustomDebugStringConvertible {
|
||||
// MARK: - Functor
|
||||
|
||||
extension Cofree {
|
||||
public func map<Other>(transform: Annotation -> Other) -> Cofree<A, Other> {
|
||||
public func map<Other>(transform: Annotation -> Other) -> Cofree<Leaf, Other> {
|
||||
return .Unroll(transform(extract), unwrap.map { $0.map(transform) })
|
||||
}
|
||||
}
|
||||
@ -60,12 +60,12 @@ extension Cofree {
|
||||
}
|
||||
|
||||
/// Returns a new `Cofree` by recursively applying `transform` to each node, producing the annotations for the copy.
|
||||
public func extend<Other>(transform: Cofree -> Other) -> Cofree<A, Other> {
|
||||
public func extend<Other>(transform: Cofree -> Other) -> Cofree<Leaf, Other> {
|
||||
return .Unroll(transform(self), unwrap.map { $0.extend(transform) })
|
||||
}
|
||||
|
||||
/// Returns a new `Cofree` constructed by recursively annotating each subtree with itself.
|
||||
public var duplicate: Cofree<A, Cofree<A, Annotation>> {
|
||||
public var duplicate: Cofree<Leaf, Cofree<Leaf, Annotation>> {
|
||||
return extend(id)
|
||||
}
|
||||
}
|
||||
@ -74,13 +74,13 @@ extension Cofree {
|
||||
// MARK: - Equality
|
||||
|
||||
extension Cofree {
|
||||
public static func equals(annotation annotation: (Annotation, Annotation) -> Bool, leaf: (A, A) -> Bool)(_ left: Cofree, _ right: Cofree) -> Bool {
|
||||
public static func equals(annotation annotation: (Annotation, Annotation) -> Bool, leaf: (Leaf, Leaf) -> Bool)(_ left: Cofree, _ right: Cofree) -> Bool {
|
||||
return annotation(left.extract, right.extract)
|
||||
&& Syntax.equals(ifLeaf: leaf, ifRecur: Cofree.equals(annotation: annotation, leaf: leaf))(left.unwrap, right.unwrap)
|
||||
}
|
||||
}
|
||||
|
||||
public func == <A: Equatable, Annotation: Equatable> (left: Cofree<A, Annotation>, right: Cofree<A, Annotation>) -> Bool {
|
||||
public func == <Leaf: Equatable, Annotation: Equatable> (left: Cofree<Leaf, Annotation>, right: Cofree<Leaf, Annotation>) -> Bool {
|
||||
return Cofree.equals(annotation: ==, leaf: ==)(left, right)
|
||||
}
|
||||
|
||||
@ -88,7 +88,7 @@ public func == <A: Equatable, Annotation: Equatable> (left: Cofree<A, Annotation
|
||||
// MARK: - JSON
|
||||
|
||||
extension Cofree {
|
||||
public func JSON(annotation annotation: Annotation -> Doubt.JSON, leaf: A -> Doubt.JSON) -> Doubt.JSON {
|
||||
public func JSON(annotation annotation: Annotation -> Doubt.JSON, leaf: Leaf -> Doubt.JSON) -> Doubt.JSON {
|
||||
return [
|
||||
"extract": annotation(extract),
|
||||
"unwrap": unwrap.JSON(ifLeaf: leaf, ifRecur: { $0.JSON(annotation: annotation, leaf: leaf) })
|
||||
@ -96,7 +96,7 @@ extension Cofree {
|
||||
}
|
||||
}
|
||||
|
||||
extension Cofree where A: CustomJSONConvertible, Annotation: CustomJSONConvertible {
|
||||
extension Cofree where Leaf: CustomJSONConvertible, Annotation: CustomJSONConvertible {
|
||||
public var JSON: Doubt.JSON {
|
||||
return JSON(annotation: { $0.JSON }, leaf: { $0.JSON })
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user