mirror of
https://github.com/github/semantic.git
synced 2024-12-26 08:25:19 +03:00
Generalize Patch
from Fix<A>
to A
.
This commit is contained in:
parent
e33f733d83
commit
34258b98b8
@ -8,7 +8,7 @@ public enum Algorithm<A, B> {
|
||||
public typealias Term = Fix<A>
|
||||
|
||||
/// The type of `Patch`es produced by `Algorithm`s.
|
||||
public typealias Patch = Doubt.Patch<A>
|
||||
public typealias Patch = Doubt.Patch<Term>
|
||||
|
||||
/// The type of `Diff`s which `Algorithm`s produce.
|
||||
public typealias Diff = Free<A, Patch>
|
||||
|
@ -107,7 +107,7 @@ public enum Free<A, B>: CustomDebugStringConvertible, CustomDocConvertible, Synt
|
||||
}
|
||||
|
||||
|
||||
extension Free where B: PatchConvertible, B.Element == A {
|
||||
extension Free where B: PatchConvertible, B.Element == Fix<A> {
|
||||
public typealias Term = Fix<A>
|
||||
|
||||
private func discardNullTerms(syntax: Syntax<Term?, A>) -> Term? {
|
||||
@ -190,9 +190,9 @@ extension Free where A: CustomJSONConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
extension Free where A: CustomJSONConvertible, B: PatchConvertible, B.Element == A {
|
||||
extension Free where A: CustomJSONConvertible, B: PatchConvertible, B.Element == Fix<A> {
|
||||
public var JSON: Doubt.JSON {
|
||||
return JSON { $0.patch.JSON }
|
||||
return JSON { $0.patch.JSON { $0.JSON } }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
/// A patch to some part of a `Syntax` tree.
|
||||
public enum Patch<A>: CustomDebugStringConvertible, CustomDocConvertible {
|
||||
case Replace(Fix<A>, Fix<A>)
|
||||
case Insert(Fix<A>)
|
||||
case Delete(Fix<A>)
|
||||
case Replace(A, A)
|
||||
case Insert(A)
|
||||
case Delete(A)
|
||||
|
||||
public var state: (before: Fix<A>?, after: Fix<A>?) {
|
||||
public var state: (before: A?, after: A?) {
|
||||
switch self {
|
||||
case let .Replace(a, b):
|
||||
return (a, b)
|
||||
@ -56,8 +56,8 @@ public enum Patch<A>: CustomDebugStringConvertible, CustomDocConvertible {
|
||||
// MARK: CustomDocConvertible
|
||||
|
||||
public var doc: Doc {
|
||||
return (state.before?.doc.bracket("{-", "-}") ?? .Empty)
|
||||
<> (state.after?.doc.bracket("{+", "+}") ?? .Empty)
|
||||
return (state.before.map(Doc.init)?.bracket("{-", "-}") ?? .Empty)
|
||||
<> (state.after.map(Doc.init)?.bracket("{+", "+}") ?? .Empty)
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,8 +66,8 @@ public enum Patch<A>: CustomDebugStringConvertible, CustomDocConvertible {
|
||||
|
||||
extension Patch {
|
||||
public static func equals(param: (A, A) -> Bool)(_ left: Patch, _ right: Patch) -> Bool {
|
||||
return Optional.equals(Fix.equals(param))(left.state.before, right.state.before)
|
||||
&& Optional.equals(Fix.equals(param))(left.state.after, right.state.after)
|
||||
return Optional.equals(param)(left.state.before, right.state.before)
|
||||
&& Optional.equals(param)(left.state.after, right.state.after)
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,8 +77,8 @@ extension Patch {
|
||||
extension Patch {
|
||||
public func hash(param: A -> Hash) -> Hash {
|
||||
return Hash.Ordered([
|
||||
state.before.map { $0.hash(param) } ?? Hash.Empty,
|
||||
state.after.map { $0.hash(param) } ?? Hash.Empty
|
||||
state.before.map(param) ?? Hash.Empty,
|
||||
state.after.map(param) ?? Hash.Empty
|
||||
])
|
||||
}
|
||||
}
|
||||
@ -92,17 +92,17 @@ extension Patch {
|
||||
case let .Replace(a, b):
|
||||
return [
|
||||
"replace": [
|
||||
"before": a.JSON(ifLeaf),
|
||||
"after": b.JSON(ifLeaf),
|
||||
"before": ifLeaf(a),
|
||||
"after": ifLeaf(b),
|
||||
]
|
||||
]
|
||||
case let .Insert(b):
|
||||
return [
|
||||
"insert": b.JSON(ifLeaf),
|
||||
"insert": ifLeaf(b),
|
||||
]
|
||||
case let .Delete(a):
|
||||
return [
|
||||
"delete": a.JSON(ifLeaf)
|
||||
"delete": ifLeaf(a)
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -111,12 +111,12 @@ extension Patch {
|
||||
|
||||
extension Patch where A: CustomJSONConvertible {
|
||||
public var JSON: Doubt.JSON {
|
||||
return self.JSON { $0.JSON }
|
||||
return JSON { $0.JSON }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// A hack to enable constrained extensions on `Free<A, Patch<A>>`.
|
||||
/// A hack to enable constrained extensions on `Free<A, Patch<Fix<A>>`.
|
||||
public protocol PatchConvertible {
|
||||
typealias Element
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
/// Computes the SES (shortest edit script), i.e. the shortest sequence of diffs (`Free<A, Patch<A>>`) for two arrays of terms (`Fix<A>`) which would suffice to transform `a` into `b`.
|
||||
///
|
||||
/// This is computed w.r.t. an `equals` function, which computes the equality of leaf nodes within terms, and a `recur` function, which produces diffs representing matched-up terms.
|
||||
public func SES<A>(a: [Fix<A>], _ b: [Fix<A>], recur: (Fix<A>, Fix<A>) -> Free<A, Patch<A>>) -> [Free<A, Patch<A>>] {
|
||||
public func SES<A>(a: [Fix<A>], _ b: [Fix<A>], recur: (Fix<A>, Fix<A>) -> Free<A, Patch<Fix<A>>>) -> [Free<A, Patch<Fix<A>>>] {
|
||||
typealias Term = Fix<A>
|
||||
typealias Diff = Free<A, Patch<A>>
|
||||
typealias Diff = Free<A, Patch<Term>>
|
||||
|
||||
if a.isEmpty { return b.map { Diff.Pure(Patch.Insert($0)) } }
|
||||
if b.isEmpty { return a.map { Diff.Pure(Patch.Delete($0)) } }
|
||||
|
@ -49,7 +49,7 @@ private func delete(term: Term) -> Diff {
|
||||
}
|
||||
|
||||
private typealias Term = Fix<Info>
|
||||
private typealias Diff = Free<Info, Patch<Info>>
|
||||
private typealias Diff = Free<Info, Patch<Fix<Info>>>
|
||||
|
||||
private let a = Term.Leaf(.Literal("a", []))
|
||||
private let b = Term.Leaf(.Literal("b", []))
|
||||
|
Loading…
Reference in New Issue
Block a user