Set Theory Library
A comprehensive mathematical set theory library for Rust implementing standard set operations, multisets, and set laws verification following discrete mathematics principles.
Table of Contents
- Overview
- Features
- Installation
- Quick Start
- Documentation
- Module Structure
- Usage Examples
- Set Operations
- Set Laws
- Multiset Operations
- Testing
- Performance
- Academic References
- Contributing
- License
Overview
This library provides a complete implementation of Set Theory concepts from discrete mathematics, designed for educational purposes and practical applications. It follows the curriculum from Institut Teknologi Sumatera (ITERA) and Institut Teknologi Bandung (ITB) Discrete Mathematics courses.
The library implements:
- CustomSet: Mutable set with unique elements
- FrozenSet: Immutable, thread-safe set
- MultiSet: Set allowing duplicate elements with multiplicity tracking
- Set Operations: Union, intersection, complement, difference, symmetric difference
- Advanced Operations: Cartesian product, power set, partition validation
- Set Laws: Verification of all standard set theory laws
Features
| Feature | Description |
|---|---|
| CustomSet | Mutable set with unique elements |
| FrozenSet | Immutable, thread-safe set |
| MultiSet | Set with multiplicity tracking |
| Basic Operations | Union, intersection, complement, difference |
| Advanced Operations | Cartesian product, power set, partition |
| Set Laws | All 11 standard set theory laws |
| Lazy Evaluation | Memory-efficient power set generation |
| Type Safety | Full generic implementations |
| Test Coverage | 80+ comprehensive unit tests |
| Documentation | Full Rust doc comments |
Installation
Add this to your Cargo.toml:
[]
= "1.0.0"
Or install via cargo:
Quick Start
use CustomSet;
use SetOperations;
// Create sets
let a = from;
let b = from;
// Perform operations
let union = union;
let intersection = intersection;
println!; // {1, 2, 3, 4, 5, 6}
println!; // {3, 4}
Documentation
Generate local documentation:
View online documentation: https://docs.rs/set_theory
Module Structure
set_theory/
├── src/
│ ├── lib.rs # Main library entry point
│ ├── main.rs # Demo application
│ ├── traits/
│ │ ├── mod.rs
│ │ └── math_set.rs # Core MathSet trait
│ ├── models/
│ │ ├── mod.rs
│ │ ├── custom_set.rs # Mutable set implementation
│ │ ├── frozen_set.rs # Immutable set implementation
│ │ └── multiset.rs # Multiset implementation
│ ├── operations/
│ │ ├── mod.rs
│ │ ├── basic_ops.rs # Basic set operations
│ │ ├── advanced_ops.rs # Advanced operations
│ │ └── lazy_ops.rs # Lazy evaluation
│ ├── laws/
│ │ ├── mod.rs
│ │ └── set_laws.rs # Set law verification
│ └── utils/
│ ├── mod.rs
│ ├── equality.rs # Equality utilities
│ └── pretty_print.rs # Formatting utilities
├── tests/
│ ├── mod.rs
│ ├── custom_set_tests.rs
│ ├── multiset_tests.rs
│ ├── operations_tests.rs
│ └── laws_tests.rs
├── Cargo.toml
├── README.md
└── LICENSE
Usage Examples
Creating Sets
use CustomSet;
// Empty set
let empty = empty;
// From vector
let numbers = from;
// From predicate
let evens = from_predicate;
// From iterator
let set: = .collect;
Basic Set Operations
use CustomSet;
use SetOperations;
let a = from;
let b = from;
// Intersection: A ∩ B
let intersection = intersection;
println!; // {4, 5}
// Union: A ∪ B
let union = union;
println!; // {1, 2, 3, 4, 5, 6, 7, 8}
// Difference: A - B
let difference = difference;
println!; // {1, 2, 3}
// Symmetric Difference: A ⊕ B
let sym_diff = symmetric_difference;
println!; // {1, 2, 3, 6, 7, 8}
// Complement: A' (with respect to universal set)
let universal = from;
let complement = complement;
println!; // {6, 7, 8, 9, 10}
Subset Operations
use CustomSet;
let a = from;
let b = from;
println!; // true
println!; // true
println!; // false
println!; // false
println!; // false
println!; // false
Power Set (Lazy Evaluation)
use CustomSet;
let a = from;
let power_set: = a.power_set.collect;
println!; // 8 (2^3)
for subset in &power_set
// Output:
// ∅
// {1}
// {2}
// {3}
// {1, 2}
// {1, 3}
// {2, 3}
// {1, 2, 3}
Cartesian Product
use CustomSet;
use AdvancedSetOperations;
let x = from;
let y = from;
let cartesian = cartesian_product;
println!;
// {(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')}
println!; // 4
Multiset Operations
use MultiSet;
let p = from;
let q = from;
println!; // {a:3, b:1, c:1}
println!; // {a:1, b:2, d:1}
// Union (max multiplicity)
println!; // {a:3, b:2, c:1, d:1}
// Intersection (min multiplicity)
println!; // {a:1, b:1}
// Difference
println!; // {a:2, c:1}
// Sum
println!; // {a:4, b:3, c:1, d:1}
Set Operations Reference
| Operation | Notation | Method | Description |
|---|---|---|---|
| Intersection | A ∩ B | intersection() |
Elements in both sets |
| Union | A ∪ B | union() |
Elements in either set |
| Complement | A' | complement() |
Elements not in A (w.r.t universal) |
| Difference | A - B | difference() |
Elements in A but not B |
| Symmetric Difference | A ⊕ B | symmetric_difference() |
Elements in either but not both |
| Subset | A ⊆ B | is_subset_of() |
A is contained in B |
| Proper Subset | A ⊂ B | is_proper_subset_of() |
A ⊆ B and A ≠ B |
| Superset | A ⊇ B | is_superset_of() |
B is contained in A |
| Power Set | P(A) | power_set() |
All subsets of A |
| Cartesian Product | A × B | cartesian_product() |
All ordered pairs |
Set Laws
This library verifies all 11 standard set theory laws:
| # | Law | Formula | Method |
|---|---|---|---|
| 1 | Identity | A ∪ ∅ = A, A ∩ U = A | identity_union(), identity_intersection() |
| 2 | Null/Domination | A ∩ ∅ = ∅, A ∪ U = U | null_intersection(), null_union() |
| 3 | Complement | A ∪ A' = U, A ∩ A' = ∅ | complement_union(), complement_intersection() |
| 4 | Idempotent | A ∪ A = A, A ∩ A = A | idempotent_union(), idempotent_intersection() |
| 5 | Involution | (A')' = A | involution() |
| 6 | Absorption | A ∪ (A ∩ B) = A, A ∩ (A ∪ B) = A | absorption_union(), absorption_intersection() |
| 7 | Commutative | A ∪ B = B ∪ A, A ∩ B = B ∩ A | commutative_union(), commutative_intersection() |
| 8 | Associative | A ∪ (B ∪ C) = (A ∪ B) ∪ C | associative_union(), associative_intersection() |
| 9 | Distributive | A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) | distributive_union(), distributive_intersection() |
| 10 | De Morgan's | (A ∪ B)' = A' ∩ B' | de_morgan_union(), de_morgan_intersection() |
| 11 | 0/1 | ∅' = U, U' = ∅ | law_zero(), law_one() |
Example: Verifying Set Laws
use CustomSet;
use SetLaws;
let a = from;
let b = from;
let universal = from;
// Verify laws
assert!;
assert!;
assert!;
assert!;
Testing
Run all tests:
Run specific test modules:
# CustomSet tests
# MultiSet tests
# Operations tests
# Set Laws tests
Run with verbose output:
Test Coverage Summary
| Module | Tests | Coverage |
|---|---|---|
custom_set_tests |
25+ | Core functionality |
multiset_tests |
15+ | Multiset operations |
operations_tests |
20+ | Set operations |
laws_tests |
20+ | Law verification |
| Total | 80+ | Comprehensive |
Performance
| Operation | Time Complexity | Space Complexity |
|---|---|---|
contains() |
O(1) average | O(1) |
add() |
O(1) average | O(1) |
remove() |
O(1) average | O(1) |
union() |
O(n + m) | O(n + m) |
intersection() |
O(min(n, m)) | O(min(n, m)) |
difference() |
O(n) | O(n) |
complement() |
O( | U |
power_set() |
O(2^n) lazy | O(n) |
cartesian_product() |
O(n × m) | O(n × m) |
Academic References
This library is based on the following academic materials:
-
ITERA Discrete Mathematics Course (IF2120)
- Institut Teknologi Sumatera
- Lecture slides: "02 - Himpunan.pdf"
- Topics: Set theory, operations, laws, multisets
-
ITB Discrete Mathematics (Adapted)
- Institut Teknologi Bandung
- Original slides by Dr. Rinaldi
- Standard set theory curriculum
-
Textbooks
- Rosen, K. H. "Discrete Mathematics and Its Applications"
- Standard set theory principles and notation
Key Concepts from Course Material
| Concept | Slide Reference | Implementation |
|---|---|---|
| Set Definition | Slide 2-4 | CustomSet |
| Multiset | Slide 82-86 | MultiSet |
| Set Operations | Slide 28-35 | SetOperations |
| Cartesian Product | Slide 38-39 | AdvancedSetOperations |
| Power Set | Slide 27 | power_set() |
| Set Laws | Slide 48-49 | SetLaws |
| Inclusion-Exclusion | Slide 67-73 | inclusion_exclusion_*() |
| Partition | Slide 81 | is_partition() |
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Code Style
- Follow Rust standard formatting (
cargo fmt) - All code must pass Clippy lints (
cargo clippy) - All tests must pass (
cargo test) - Document all public items with Rust doc comments
Documentation Standards
All public items must include:
## Summary- Brief description## Description- Detailed explanation## Arguments- Parameter descriptions## Returns- Return value description## Examples- Usage examples## Theory Reference- Academic reference (if applicable)
License
This project is licensed under the MIT License - see the LICENSE file for details.