public struct Key: Hashable { static var index = 0 public let id: Int init() { self.id = Self.index Self.index += 1 } } public struct Variable { public let key = Key() } public struct Bindings { private var storage: [Key : Any] = [:] public init( _ value: repeat (Variable, each T) ) { _ = (repeat storage[(each value).0.key] = (each value).1) } } public protocol Expression { associatedtype Result func evaluate(_ bindings: Bindings) throws -> Result } public struct True: Expression { public init() {} public func evaluate(_ bindings: Bindings) throws -> Bool { true } } public struct Predicate { var variables: (repeat Variable) var expression: any Expression public init( builder: (repeat Variable) -> Expr ) where Expr: Expression { self.variables = (repeat Variable()) self.expression = builder(repeat each self.variables) } public func evaluate( _ input: repeat each Input ) throws -> Bool { return try expression.evaluate( .init(repeat (each self.variables, each input)) ) } } extension Sequence { public func filter(_ predicate: Predicate) throws -> [Element] { try self.filter { try predicate.evaluate($0) } } }