Created
November 26, 2023 22:02
-
-
Save JadenGeller/85f745d6b548286b4ab404632c257360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Lattice { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self | |
} | |
extension Lattice { | |
mutating func join(with other: Self) { | |
self = .join(self, other) | |
} | |
mutating func meet(with other: Self) { | |
self = .meet(self, other) | |
} | |
} | |
extension Set: Lattice { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self { | |
return lhs.union(rhs) | |
} | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self { | |
return lhs.intersection(rhs) | |
} | |
} | |
extension Optional: Lattice where Wrapped: Lattice { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self { | |
switch (lhs, rhs) { | |
case let (lhs?, rhs?): Wrapped.join(lhs, rhs) | |
case (nil, let rhs?): rhs | |
case (let lhs?, nil): lhs | |
default: nil | |
} | |
} | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self { | |
guard case let (lhs?, rhs?) = (lhs, rhs) else { return nil } | |
return Wrapped.meet(lhs, rhs) | |
} | |
} | |
extension PartialRangeFrom: Lattice where Bound: Comparable { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self { | |
Swift.min(lhs.lowerBound, rhs.lowerBound)... | |
} | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self { | |
Swift.max(lhs.lowerBound, rhs.lowerBound)... | |
} | |
} | |
extension PartialRangeThrough: Lattice where Bound: Comparable { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self { | |
...Swift.max(lhs.upperBound, rhs.upperBound) | |
} | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self { | |
...Swift.min(lhs.upperBound, rhs.upperBound) | |
} | |
} | |
extension PartialRangeUpTo: Lattice where Bound: Comparable { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self { | |
..<Swift.min(lhs.upperBound, rhs.upperBound) | |
} | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self { | |
..<Swift.max(lhs.upperBound, rhs.upperBound) | |
} | |
} | |
extension Range: Lattice where Bound: Comparable { | |
static func join(_ lhs: Self, _ rhs: Self) -> Self { | |
let lowerBound = Swift.min(lhs.lowerBound, rhs.lowerBound) | |
let upperBound = Swift.max(lhs.upperBound, rhs.upperBound) | |
guard upperBound > lowerBound else { return .init(uncheckedBounds: (lower: lowerBound, upper: lowerBound)) } | |
return .init(uncheckedBounds: (lower: lowerBound, upper: upperBound)) | |
} | |
static func meet(_ lhs: Self, _ rhs: Self) -> Self { | |
let lowerBound = Swift.max(lhs.lowerBound, rhs.lowerBound) | |
let upperBound = Swift.min(lhs.upperBound, rhs.upperBound) | |
guard upperBound > lowerBound else { return .init(uncheckedBounds: (lower: lowerBound, upper: lowerBound)) } | |
return .init(uncheckedBounds: (lower: lowerBound, upper: upperBound)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment