1
1
mirror of https://github.com/github/semantic.git synced 2024-12-29 18:06:14 +03:00

Implement Syntax equality using a function which expresses the equality of the functor.

This commit is contained in:
Rob Rix 2015-09-30 14:42:44 -04:00
parent 92d844f416
commit a2b9f9ce17

View File

@ -2,19 +2,23 @@ public func == (left: Term, right: Term) -> Bool {
return left.syntax == right.syntax
}
public func == <F: Equatable, A: Equatable> (left: Syntax<F, A>, right: Syntax<F, A>) -> Bool {
private func equals<F, A: Equatable>(left: Syntax<F, A>, _ right: Syntax<F, A>, _ recur: (F, F) -> Bool) -> Bool {
switch (left, right) {
case (.Empty, .Empty):
return true
case let (.Leaf(l1), .Leaf(l2)):
return l1 == l2
case let (.Branch(v1), .Branch(v2)):
return v1 == v2
return v1.count == v2.count && zip(v1, v2).reduce(true) { $0 && recur($1.0, $1.1) }
default:
return false
}
}
public func == <F: Equatable, A: Equatable> (left: Syntax<F, A>, right: Syntax<F, A>) -> Bool {
return equals(left, right, ==)
}
public func == (left: Diff, right: Diff) -> Bool {
switch (left, right) {
case let (.Patch(a1, b1), .Patch(a2, b2)):