https://github.com/p2js/set-theory
rust type-system set theory
https://github.com/p2js/set-theory
rust rust-lang set-theory sets type-system type-systems
Last synced: 2 months ago
JSON representation
rust type-system set theory
- Host: GitHub
- URL: https://github.com/p2js/set-theory
- Owner: p2js
- Created: 2024-04-12T16:38:01.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-30T18:33:12.000Z (4 months ago)
- Last Synced: 2025-02-13T06:48:00.352Z (4 months ago)
- Topics: rust, rust-lang, set-theory, sets, type-system, type-systems
- Language: Rust
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# set-theory
Type-system set theory in rust.
This is a quick project thrown together in a couple hours to explore the representation of infinite sets and interoperability between different set types.
## The Empty set
The `EmptySet` type represents an empty set. It is simply an empty `struct` that implements the `Set` trait.
```rs
use set_theory::sets::EmptySet;let es = EmptySet::new();
assert!(!es.contains(&0));
assert!(!es.contains(&1.5));
```## Finite and Infinite sets
The `FiniteSet` type can store a set of finite values, and uses a `HashSet` internally.
```rs
use set_theory::sets::FiniteSet;let fs = FiniteSet::new(&[1, 2, 3]);
assert!(fs.contains(&1));
assert!(!fs.contains(&4));
```The `PredicateSet` is meant to represent an infinite set as a list of predicates (functions that take in a single `T` argument and return a boolean), in a sort of emulation of set builder notation. A value will count as being in the set if all the predicates evaluate to true.
```rs
use set_theory::sets::PredicateSet;// set of all even integers greater than 5
let ps = PredicateSet::new(&[|&x| x > 5, |&x| x % 2 == 0]);assert!(ps.contains(&6));
assert!(!ps.contains(&7));
```## Set operations
The library includes types for the unions, intersections, complements and cartesian products of two sets. It also includes a power set type, which will implement `Set>` for any `Set` and will check for whether a set is in the powerset by checking that all the elements are contained in the original set.
All of these types use dynamic trait object references to allow for operations on different types of sets.
```rs
use set_theory::operations::{Union, PowerSet};
use set_theory::sets::{FiniteSet, PredicateSet};let a = FiniteSet::new(&[1, 5]);
let b = PredicateSet::new(&[|&x| x > 5, |&x| x % 2 == 0]);let aub = Union::of(&a, &b);
assert!(aub.contains(&5));
assert!(aub.contains(&6));let pa = PowerSet::of(&a);
assert!(pa.contains(&a));
assert!(pa.contains(&FiniteSet::new(&[1])));
```## Relations
Relations can be used to encode relationships between elements in a set through a predicate.
```rs
use set_theory::sets::PredicateSet;
use set_theory::relations::Relation;let c = PredicateSet::::all();
// <= relation
let is_less_than = Relation::on(&c, |a, b| b - a > 0);assert!(is_less_than.relates(&2, &4));
assert!(!is_less_than.relates(&2, &1));
```## Interacting with sets
All set types in the library implement a common `Set` trait. The trait only requires implementation of a [function to check whether a value is in the set](https://en.wikipedia.org/wiki/Indicator_function). This is because infinite sets do not store any values, so any method to access some list of values would be extremely difficult to impossible.