1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00
semantic/prototype/Doubt/Free.swift

210 lines
7.3 KiB
Swift
Raw Normal View History

2015-10-02 20:54:39 +03:00
/// The free monad over `Syntax`.
///
/// This is free in the sense of unconstrained rather than zero-cost; its the monad obtained by taking a functor (in this case `Syntax`) and adding the minimum necessary details (the `Pure` case) to satisfy the monad laws.
///
/// `Syntax` is a non-recursive type parameterized by the type of its child nodes. Instantiating it to `Free` makes it recursive through the `Roll` case, and allows it to wrap values of type `B` through the `Pure` case.
///
/// In Doubt, this allows us to represent diffs as values of the `Free` monad obtained from `Syntax`, injecting `Patch` into the tree; or otherwise put, a diff is a tree of mutually-recursive `Free.Roll`/`Syntax` nodes with `Pure` nodes injecting the actual changes.
public enum Free<A, B>: CustomDebugStringConvertible, SyntaxConvertible {
2015-10-02 21:07:29 +03:00
/// The injection of a value of type `B` into the `Syntax` tree.
2015-10-02 20:43:43 +03:00
case Pure(B)
2015-10-02 21:07:29 +03:00
2015-10-02 21:08:03 +03:00
/// A recursive instantiation of `Syntax`, unrolling another iteration of the recursive type.
2015-10-02 20:43:43 +03:00
indirect case Roll(Syntax<Free, A>)
2015-10-02 20:59:55 +03:00
2015-10-07 00:45:24 +03:00
/// Recursively copies a `Fix<A>` into a `Free<A, B>`, essentially mapping `Fix.In` onto `Free.Roll`.
public init(_ fix: Fix<A>) {
self = .Roll(fix.out.map(Free.init))
}
public init<Term: TermType where Term.LeafType == A>(_ term: Term) {
self = .Roll(term.out.map(Free.init))
}
2015-10-07 00:45:24 +03:00
2015-10-02 21:29:45 +03:00
public func analysis<C>(@noescape ifPure ifPure: B -> C, @noescape ifRoll: Syntax<Free, A> -> C) -> C {
2015-10-02 21:26:53 +03:00
switch self {
case let .Pure(b):
return ifPure(b)
case let .Roll(s):
return ifRoll(s)
}
}
2015-10-06 19:47:33 +03:00
/// Reduce the receiver by iteration.
///
/// `Pure` values are simply unpacked. `Roll` values are mapped recursively, and then have `transform` applied to them.
///
/// This forms a _catamorphism_ (from the Greek cata, downwards; compare catastrophe), a generalization of folds over regular trees (and datatypes isomorphic to them). It operates at the leaves first, and then branches near the periphery, recursively collapsing values by whatever is computed by `transform`. Catamorphisms are themselves an example of _recursion schemes_, which characterize specific well-behaved patterns of recursion. This gives `iterate` some useful properties for computations performed over trees.
///
/// Due to the character of recursion captured by catamorphisms, `iterate` ensures that computation will not only halt, but will further be linear in the size of the receiver. (Nesting a call to `iterate` will therefore result in O(n²) complexity.) This guarantee is achieved by careful composition of calls to `map` with recursive calls to `iterate`, only calling `transform` once the recursive call has completed. `transform` is itself non-recursive, receiving a `Syntax` whose recurrences have already been flattened to `B`.
///
/// The linearity of `iterate` in the size of the receiver makes it trivial to compute said size, by counting leaves as 1 and summing branches children:
///
/// func size<A, B>(free: Free<A, B>) -> Int {
/// return free.iterate { flattenedSyntax in
/// switch flattenedSyntax {
/// case .Leaf:
/// return 1
/// case let .Indexed(children):
/// return children.reduce(0, combine: +)
/// case let .Keyed(children):
/// return children.lazy.map { $1 }.reduce(0, combine: +)
/// }
/// }
/// }
///
/// While not every function on a given `Free` can be computed using `iterate`, these guarantees of termination and complexity, as well as the brevity and focus on the operation being performed n times, make it a desirable scaffolding for any function which can.
///
/// For a lucid, in-depth tutorial on recursion schemes, I recommend [Patrick Thomson](https://twitter.com/importantshock)s _[An Introduction to Recursion Schemes](http://patrickthomson.ghost.io/an-introduction-to-recursion-schemes/)_ and _[Recursion Schemes, Part 2: A Mob of Morphisms](http://patrickthomson.ghost.io/recursion-schemes-part-2/)_.
2015-10-06 18:05:02 +03:00
public func iterate(transform: Syntax<B, A> -> B) -> B {
2015-10-06 18:04:14 +03:00
return analysis(
ifPure: id,
2015-10-06 18:05:02 +03:00
ifRoll: { transform($0.map { $0.iterate(transform) }) })
2015-10-06 18:04:14 +03:00
}
// MARK: Functor
2015-10-02 20:59:55 +03:00
public func map<C>(@noescape transform: B -> C) -> Free<A, C> {
2015-10-02 21:35:18 +03:00
return analysis(ifPure: { .Pure(transform($0)) }, ifRoll: { .Roll($0.map { $0.map(transform) }) })
2015-10-02 20:59:55 +03:00
}
2015-10-02 21:03:34 +03:00
// MARK: Monad
2015-10-02 21:03:34 +03:00
public func flatMap<C>(@noescape transform: B -> Free<A, C>) -> Free<A, C> {
2015-10-02 21:36:01 +03:00
return analysis(ifPure: transform, ifRoll: { .Roll($0.map { $0.flatMap(transform) }) })
2015-10-02 21:03:34 +03:00
}
2015-10-06 23:43:20 +03:00
// MARK: CustomDebugStringConvertible
public var debugDescription: String {
switch self {
case let .Pure(b):
return ".Pure(\(String(reflecting: b)))"
case let .Roll(s):
return ".Roll(\(String(reflecting: s)))"
}
}
2015-10-07 16:50:31 +03:00
2015-10-10 01:04:29 +03:00
// MARK: SyntaxConvertible
public init(syntax: Syntax<Free, A>) {
self = .Roll(syntax)
}
2015-10-02 20:43:43 +03:00
}
2015-10-02 21:15:17 +03:00
extension Free where B: PatchConvertible, B.Element == Fix<A> {
public typealias Term = Fix<A>
private func discardNullTerms(syntax: Syntax<Term?, A>) -> Term? {
switch syntax {
case let .Leaf(a):
return .Leaf(a)
case let .Indexed(a):
return .Indexed(a.flatMap(id))
case let .Keyed(a):
return .Keyed(Dictionary(elements: a.flatMap { k, v in v.map { (k, $0) } }))
}
}
public var before: Term? {
return map { $0.patch.state.before }.iterate(self.discardNullTerms)
2015-10-07 04:35:47 +03:00
}
public var after: Term? {
return map { $0.patch.state.after }.iterate(self.discardNullTerms)
}
2015-10-07 15:43:30 +03:00
public var inverse: Free {
return map { B(patch: $0.patch.inverse) }
}
}
2015-10-02 21:15:17 +03:00
// MARK: - Equality
extension Free {
public static func equals(ifPure ifPure: (B, B) -> Bool, ifRoll: (A, A) -> Bool)(_ left: Free, _ right: Free) -> Bool {
switch (left, right) {
case let (.Pure(a), .Pure(b)):
return ifPure(a, b)
case let (.Roll(a), .Roll(b)):
return Syntax.equals(ifLeaf: ifRoll, ifRecur: equals(ifPure: ifPure, ifRoll: ifRoll))(a, b)
default:
return false
}
}
}
2015-10-02 21:18:43 +03:00
public func == <A: Equatable, B: Equatable> (left: Free<A, B>, right: Free<A, B>) -> Bool {
return Free.equals(ifPure: ==, ifRoll: ==)(left, right)
}
2015-10-02 21:35:07 +03:00
// MARK: - Hashing
extension Free {
public func hash(ifPure ifPure: B -> Hash, ifRoll: A -> Hash) -> Hash {
return analysis(ifPure: ifPure, ifRoll: { $0.hash(ifLeaf: ifRoll, ifRecur: { $0.hash(ifPure: ifPure, ifRoll: ifRoll) }) })
}
}
extension Free where A: Hashable, B: Hashable {
public var hash: Hash {
return hash(ifPure: Hash.init, ifRoll: Hash.init)
}
}
2015-10-08 14:21:08 +03:00
2015-10-09 15:39:57 +03:00
// MARK: - JSON
2015-10-08 14:21:08 +03:00
extension Free {
public func JSON(ifPure ifPure: B -> Doubt.JSON, ifLeaf: A -> Doubt.JSON) -> Doubt.JSON {
2015-10-08 14:30:02 +03:00
return analysis(
2015-10-09 22:31:10 +03:00
ifPure: ifPure,
2015-10-08 14:30:34 +03:00
ifRoll: {
2015-10-09 22:31:10 +03:00
$0.JSON(ifLeaf: ifLeaf, ifRecur: { $0.JSON(ifPure: ifPure, ifLeaf: ifLeaf) })
2015-10-08 14:30:34 +03:00
})
2015-10-08 14:21:08 +03:00
}
}
extension Free where A: CustomJSONConvertible {
public func JSON(ifPure: B -> Doubt.JSON) -> Doubt.JSON {
return JSON(ifPure: ifPure, ifLeaf: { $0.JSON })
}
}
extension Free where A: CustomJSONConvertible, B: PatchConvertible, B.Element == Fix<A> {
public var JSON: Doubt.JSON {
return JSON { $0.patch.JSON { $0.JSON } }
}
}
2015-10-13 07:55:20 +03:00
2015-10-14 20:34:29 +03:00
// MARK: - FreeConvertible
/// A hack to work around the unavailability of same-type requirements.
public protocol FreeConvertible {
typealias RollType
typealias PureType
init(free: Free<RollType, PureType>)
var free: Free<RollType, PureType> { get }
}
extension Free: FreeConvertible {
public init(free: Free<A, B>) { self = free }
public var free: Free { return self }
}
2015-10-13 07:55:20 +03:00
import Prelude