2015-10-06 23:50:23 +03:00
|
|
|
/// 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.
|
2015-10-14 00:40:14 +03:00
|
|
|
public func SES<A>(a: [Fix<A>], _ b: [Fix<A>], equals: (A, A) -> Bool, recur: (Fix<A>, Fix<A>) -> Free<A, Patch<A>>?) -> [Free<A, Patch<A>>] {
|
2015-10-06 23:50:23 +03:00
|
|
|
typealias Term = Fix<A>
|
|
|
|
typealias Diff = Free<A, Patch<A>>
|
|
|
|
|
|
|
|
if a.isEmpty { return b.map { Diff.Pure(Patch.Insert($0)) } }
|
|
|
|
if b.isEmpty { return a.map { Diff.Pure(Patch.Delete($0)) } }
|
|
|
|
|
|
|
|
func cost(diff: Diff) -> Int {
|
2015-10-14 01:19:56 +03:00
|
|
|
return diff.map(const(1)).iterate { syntax in
|
2015-10-06 23:50:23 +03:00
|
|
|
switch syntax {
|
|
|
|
case .Leaf:
|
|
|
|
return 0
|
|
|
|
case let .Indexed(costs):
|
|
|
|
return costs.reduce(0, combine: +)
|
|
|
|
case let .Keyed(costs):
|
2015-10-07 20:04:03 +03:00
|
|
|
return costs.values.reduce(0, combine: +)
|
2015-10-06 23:50:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cons(diff: Diff, rest: Memo<Stream<(Diff, Int)>>) -> Stream<(Diff, Int)> {
|
|
|
|
return .Cons((diff, cost(diff) + costOfStream(rest)), rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func costOfStream(stream: Memo<Stream<(Diff, Int)>>) -> Int {
|
|
|
|
return stream.value.first?.1 ?? 0
|
|
|
|
}
|
|
|
|
|
2015-10-07 20:09:22 +03:00
|
|
|
func min<A>(a: A, _ rest: A..., _ isLessThan: (A, A) -> Bool) -> A {
|
|
|
|
return rest.reduce(a, combine: { isLessThan($0, $1) ? $0 : $1 })
|
|
|
|
}
|
|
|
|
|
2015-10-07 00:29:33 +03:00
|
|
|
// A matrix whose values are streams representing paths through the edit graph, carrying both the diff & the cost of the remainder of the path.
|
2015-10-06 23:50:23 +03:00
|
|
|
var matrix: Matrix<Stream<(Diff, Int)>>!
|
2015-10-07 00:36:41 +03:00
|
|
|
matrix = Matrix(width: a.count + 1, height: b.count + 1) { i, j in
|
2015-10-07 00:36:49 +03:00
|
|
|
// Some explanation is warranted:
|
|
|
|
//
|
|
|
|
// 1. `matrix` captures itself during construction, because each vertex in the edit graph depends on other vertices. This is safe, because a) `Matrix` populates its fields lazily, and b) vertices only depend on those vertices downwards and rightwards of them.
|
|
|
|
//
|
|
|
|
// 2. `matrix` is sized bigger than `a.count` x `b.count`. This is safe, because a) we only get a[i]/b[j] when right/down are non-nil (respectively), and b) right/down are found by looking up elements (i + 1, j) & (i, j + 1) in the matrix, which returns `nil` when out of bounds. So we only access a[i] and b[j] when i and j are in bounds.
|
|
|
|
|
2015-10-06 23:50:23 +03:00
|
|
|
let right = matrix[i + 1, j]
|
|
|
|
let down = matrix[i, j + 1]
|
|
|
|
let diagonal = matrix[i + 1, j + 1]
|
|
|
|
|
2015-10-06 23:59:29 +03:00
|
|
|
let recur = {
|
2015-10-07 00:46:37 +03:00
|
|
|
Term.equals(equals)($0, $1)
|
2015-10-07 00:47:04 +03:00
|
|
|
? Diff($1)
|
2015-10-06 23:59:29 +03:00
|
|
|
: recur($0, $1)
|
|
|
|
}
|
2015-10-06 23:50:23 +03:00
|
|
|
|
|
|
|
if let right = right, down = down, diagonal = diagonal {
|
2015-10-14 00:50:22 +03:00
|
|
|
let right = (right, Diff.Pure(Patch.Delete(a[i])), costOfStream(right))
|
|
|
|
let down = (down, Diff.Pure(Patch.Insert(b[j])), costOfStream(down))
|
2015-10-14 00:51:03 +03:00
|
|
|
let diagonal = recur(a[i], b[j]).map { (diagonal, $0, costOfStream(diagonal)) }
|
2015-10-14 00:55:47 +03:00
|
|
|
// nominate the best edge to continue along, not considering diagonal if `recur` returned `nil`.
|
2015-10-14 00:55:02 +03:00
|
|
|
let (best, diff, _) = diagonal
|
|
|
|
.map { min($0, right, down) { $0.2 < $1.2 } }
|
|
|
|
?? min(right, down) { $0.2 < $1.2 }
|
2015-10-06 23:50:23 +03:00
|
|
|
return cons(diff, rest: best)
|
|
|
|
}
|
|
|
|
|
|
|
|
// right extent of the edit graph; can only move down
|
|
|
|
if let down = down {
|
2015-10-07 00:01:17 +03:00
|
|
|
return cons(Diff.Pure(Patch.Insert(b[j])), rest: down)
|
2015-10-06 23:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// bottom extent of the edit graph; can only move right
|
|
|
|
if let right = right {
|
2015-10-07 00:01:17 +03:00
|
|
|
return cons(Diff.Pure(Patch.Delete(a[i])), rest: right)
|
2015-10-06 23:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// bottom-right corner of the edit graph
|
2015-10-07 00:36:41 +03:00
|
|
|
return Stream.Nil
|
2015-10-06 23:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Array(matrix[0, 0]!.value.map { diff, _ in diff })
|
|
|
|
}
|
2015-10-13 07:51:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
import Memo
|
2015-10-14 01:19:56 +03:00
|
|
|
import Prelude
|
2015-10-13 07:51:43 +03:00
|
|
|
import Stream
|