A powerful and expressive TypeScript library for working with sets defined by logical or mathematical properties, including infinite sets, with strong type safety and evaluation strategies.
- Property-defined sets using predicates or formulas
- Finite and infinite sets with evaluation strategies
- Set operations: union, intersection, difference, complement
- Advanced constructs: cartesian product, power set, transformations
- Type-safe and extensible architecture (interfaces, abstract classes)
- Strategy pattern for performance tuning (lazy, memoized, parallel...)
- Mathematical extensions: number theory, logic
- Composability via factories and builders
npm install formalismyarn add formalismpnpm add formalismbun add formalismFormal sets in Formalis are not built by listing elements, but by defining properties (predicates):
import { NumericSetFactory } from 'formalis';
const A = NumericSetFactory.evenNumbers();
A.has(4); // true
A.has(5); // falseimport { SetFactory } from 'formalis';
const B = SetFactory.fromPredicate({
predicate: x => Number.isInteger(x) && x * x < 100,
description: 'x² < 100'
});Or using the SetBuilder:
import { SetBuilder } from 'formalis';
const C = new SetBuilder<number>()
.withPredicate({ predicate: x => x % 10 === 1, description: 'x ≡ 1 mod 10' })
.withMetadata({ name: 'CongruenceSet' })
.build();Each set is evaluated according to a strategy:
'finite': works on explicitly listed elements'lazy': defers evaluation'memoized': caches results'parallel': enables parallelism (conceptual for now)
const lazySet = SetFactory.fromPredicate(
{ predicate: x => x > 0, description: 'x > 0' },
'lazy'
);Let:
| Operation | Math Notation | Code |
|---|---|---|
| Union | union(A, B) |
|
| Intersection | intersection(A, B) |
|
| Difference | difference(A, B) |
|
| Complement | complement(U, A) |
|
| Cartesian Prod | cartesianProduct(A, B) |
|
| Power Set | powerSet(A) |
import { transform } from 'formalis';
const squared = transform(A, x => x * x, y => y < 1000);
squared.has(36); // true if 6 ∈ Aimport { combineSets } from 'formalis';
const disjunction = combineSets([A, B], (a, b) => a || b);
const xor = combineSets([A, B], (a, b) => a !== b);import { NumberTheory } from 'formalis';
const primeSet = NumberTheory.primes();
primeSet.has(7); // true
primeSet.has(9); // falseimport { randomSample } from 'formalis';
const sample = randomSample(myFiniteSet, 5);import { breadthFirstEnumeration } from 'formalis';
for (const el of breadthFirstEnumeration(mySet)) {
// explore mySet
}SetFactory.fromPredicate(...)SetBuilder- Set operations:
union,intersection,difference,complement - Advanced:
cartesianProduct,powerSet,transform - Strategies:
'finite','lazy','memoized','parallel' - Extensions:
NumberTheory.primes()etc.
A standalone benchmark script is available to evaluate the performance of Formalis operations. To run it:
node test/benchmark.jsThis will execute a series of timed operations and print durations in seconds, such as:
- Predicate evaluation on large ranges
- Set union and intersection
- Sampling and transformation performance
You can customize or extend the script in scripts/benchmark.js to fit your specific use cases.
MIT — feel free to use and contribute.
- Stream-based lazy sets
- Real parallelism (Web Workers or threads)
- Logical inference on predicates
- Type narrowing through evaluation
If you have any problems or questions, feel free to contact RemyK.
- Discord server: Server Link
- GitHub repository: formalis
Made with ❤ by RemyK