Skip to content
forked from RemyK888/formalis

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.

License

Notifications You must be signed in to change notification settings

Ayomits/formalis

 
 

Repository files navigation

Formalis

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.


✨ Features

  • 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

📦 Installation

npm install formalism
yarn add formalism
pnpm add formalism
bun add formalism

🧠 Core Concepts

Formal sets in Formalis are not built by listing elements, but by defining properties (predicates):

A = { x ∈ ℕ | x is even }

import { NumericSetFactory } from 'formalis';

const A = NumericSetFactory.evenNumbers();
A.has(4); // true
A.has(5); // false

🧩 Defining Your Own Set

B = { x ∈ ℕ | x² < 100 }

import { 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();

⚙️ Evaluation Strategies

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'
);

🔀 Set Operations

Let:

A = { x | P(x) }, B = { x | Q(x) }

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)

🔄 Transforming Sets

f(A) = { f(x) | x ∈ A }

import { transform } from 'formalis';

const squared = transform(A, x => x * x, y => y < 1000);
squared.has(36); // true if 6 ∈ A

🔣 Combining Sets Logically

import { combineSets } from 'formalis';

const disjunction = combineSets([A, B], (a, b) => a || b);
const xor = combineSets([A, B], (a, b) => a !== b);

📐 Mathematical Extensions

import { NumberTheory } from 'formalis';

const primeSet = NumberTheory.primes();
primeSet.has(7); // true
primeSet.has(9); // false

📊 Sampling & Enumeration

import { randomSample } from 'formalis';

const sample = randomSample(myFiniteSet, 5);
import { breadthFirstEnumeration } from 'formalis';

for (const el of breadthFirstEnumeration(mySet)) {
  // explore mySet
}

📚 API Summary

  • SetFactory.fromPredicate(...)
  • SetBuilder
  • Set operations: union, intersection, difference, complement
  • Advanced: cartesianProduct, powerSet, transform
  • Strategies: 'finite', 'lazy', 'memoized', 'parallel'
  • Extensions: NumberTheory.primes() etc.

🏁 Benchmarking

A standalone benchmark script is available to evaluate the performance of Formalis operations. To run it:

node test/benchmark.js

This 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.


📄 License

MIT — feel free to use and contribute.


🧱 Roadmap

  • Stream-based lazy sets
  • Real parallelism (Web Workers or threads)
  • Logical inference on predicates
  • Type narrowing through evaluation

💬 Support

If you have any problems or questions, feel free to contact RemyK.



Made with ❤ by RemyK

About

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.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 91.6%
  • JavaScript 8.4%