UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

144 lines 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoundedSetDomain = exports.BoundedSetBottom = void 0; const set_1 = require("../../util/collections/set"); const logic_1 = require("../../util/logic"); const abstract_domain_1 = require("./abstract-domain"); const lattice_1 = require("./lattice"); /* eslint-disable @typescript-eslint/unified-signatures */ /** The Bottom element of the bounded set domain as empty set */ exports.BoundedSetBottom = new Set(); /** * The bounded set abstract domain as sets of possible values bounded by a `limit` indicating the maximum number of inferred values. * The Bottom element is defined as the empty set and the Top element is defined as {@link Top} symbol. * @template T - Type of the values in the abstract domain * @template Value - Type of the constraint in the abstract domain (Top, Bottom, or an actual value) */ class BoundedSetDomain extends abstract_domain_1.AbstractDomain { limit; setType; /** * @param limit - A limit for the maximum number of elements to store in the set * @param setType - An optional set constructor for the domain elements if the type `T` is not storable in a HashSet */ constructor(value, limit = abstract_domain_1.DEFAULT_INFERENCE_LIMIT, setType = Set) { if (value !== lattice_1.Top) { if (Array.isArray(value)) { super((value.length > limit ? lattice_1.Top : new setType(value))); } else { super((value.size > limit ? lattice_1.Top : new setType(value))); } } else { super(value); } this.limit = limit; this.setType = setType; } create(value) { return new BoundedSetDomain(value, this.limit, this.setType); } static top(limit, setType) { return new BoundedSetDomain(lattice_1.Top, limit, setType); } static bottom(limit, setType) { return new BoundedSetDomain(exports.BoundedSetBottom, limit, setType); } static abstract(concrete, limit, setType) { return new BoundedSetDomain(concrete, limit, setType); } top() { return BoundedSetDomain.top(this.limit, this.setType); } bottom() { return BoundedSetDomain.bottom(this.limit, this.setType); } equals(other) { return this.value === other.value || (this.isValue() && other.isValue() && (0, set_1.setEquals)(this.value, other.value)); } leq(other) { return other.value === lattice_1.Top || (this.isValue() && this.value.isSubsetOf(other.value)); } join(other) { const otherValue = other instanceof BoundedSetDomain ? other.value : Array.isArray(other) ? new this.setType(other) : other; if (this.value === lattice_1.Top || otherValue === lattice_1.Top) { return this.top(); } else { return this.create(this.value.union(otherValue)); } } meet(other) { const otherValue = other instanceof BoundedSetDomain ? other.value : Array.isArray(other) ? new this.setType(other) : other; if (this.value === lattice_1.Top) { return this.create(otherValue); } else if (otherValue === lattice_1.Top) { return this.create(this.value); } else { return this.create(this.value.intersection(otherValue)); } } /** * Subtracts another abstract value from the current abstract value by removing all elements of the other abstract value from the current abstract value. */ subtract(other) { const otherValue = other instanceof BoundedSetDomain ? other.value : Array.isArray(other) ? new this.setType(other) : other; if (this.value === lattice_1.Top) { return this.top(); } else if (otherValue === lattice_1.Top) { return this.create(this.value); } else { return this.create(this.value.difference(otherValue)); } } widen(other) { return other.leq(this) ? this.create(this.value) : this.top(); } narrow(other) { return this.isTop() ? this.create(other.value) : this.create(this.value); } concretize(limit) { return this.value === lattice_1.Top || this.value.size > limit ? lattice_1.Top : this.value; } abstract(concrete) { return BoundedSetDomain.abstract(concrete, this.limit, this.setType); } satisfies(value) { if (this.isValue() && this.value.has(value)) { return this.value.size === 1 ? logic_1.Ternary.Always : logic_1.Ternary.Maybe; } else if (this.isTop()) { return logic_1.Ternary.Maybe; } return logic_1.Ternary.Never; } toJson() { if (this.value === lattice_1.Top) { return this.value.description; } return this.value.values().toArray(); } toString() { if (this.value === lattice_1.Top) { return lattice_1.TopSymbol; } const string = this.value.values().map(abstract_domain_1.domainElementToString).toArray().join(', '); return `{${string}}`; } isTop() { return this.value === lattice_1.Top; } isBottom() { return this.value !== lattice_1.Top && this.value.size === 0; } isValue() { return this.value !== lattice_1.Top; } } exports.BoundedSetDomain = BoundedSetDomain; //# sourceMappingURL=bounded-set-domain.js.map