Skip to main content

Module predicate

Module predicate 

Source
Expand description

§Predicate

This module provides the Predicate type, representing intensional sets as predicate functions.

§Overview

A Predicate<A> wraps a function that determines whether a value of type A satisfies a certain condition. Predicates are composable and support logical operations such as AND, OR, NOT, and difference, making them useful for representing and manipulating sets defined by conditions.

§Features

  • Functional, composable representation of sets
  • Logical operations: union, intersection, difference, and negation
  • Operator overloading for expressive predicate composition
  • Implements Semigroup and Monoid traits for algebraic composition

§Functional Programming Context

Predicate<A> implements several functional programming type classes:

  • Semigroup: Predicates can be combined using logical OR via combine
  • Monoid: Provides an identity element (empty) that always returns false

These implementations enable predicates to work seamlessly with other functional abstractions in Rustica.

§Type Class Laws

§Semigroup Laws

  • Associativity: (a.combine(b)).combine(c) == a.combine(b.combine(c))
    • The order in which predicates are combined using logical OR doesn’t matter.

§Monoid Laws

  • Left Identity: empty().combine(a) == a

    • Combining a predicate with the empty predicate (always returns false) using OR yields the original predicate.
  • Right Identity: a.combine(empty()) == a

    • The identity property holds regardless of the order of combination.

§Set Operation Laws

  • Commutativity of Union: a.union(b) == b.union(a)

    • The order of operands in a union operation doesn’t matter.
  • Associativity of Intersection: a.intersection(b).intersection(c) == a.intersection(b.intersection(c))

    • The order of operations for intersection doesn’t affect the result.
  • Distributivity: a.intersection(b.union(c)) == a.intersection(b).union(a.intersection(c))

    • Intersection distributes over union, similar to multiplication over addition.
  • Complement Laws: a.negate().negate() == a

    • Double negation yields the original predicate.

§Type Class Implementations

  • Semigroup: combine creates a union of predicates (logical OR)
  • Monoid: empty creates a predicate that always returns false
  • HKT: Higher-kinded type representation for advanced type-level operations

§Quick Start

use rustica::datatypes::wrapper::predicate::Predicate;
use rustica::traits::{semigroup::Semigroup, monoid::Monoid};

// Create predicates for different conditions
let is_positive = Predicate::new(|&x: &i32| x > 0);
let is_even = Predicate::new(|&x: &i32| x % 2 == 0);

// Test values against predicates
assert!(is_positive.contains(&5));
assert!(!is_positive.contains(&-3));
assert!(is_even.contains(&4));
assert!(!is_even.contains(&3));

// Combine predicates with logical operations
let is_positive_or_even = is_positive.union(&is_even); // OR
let is_positive_and_even = is_positive.intersection(&is_even); // AND

assert!(is_positive_or_even.contains(&6)); // positive and even
assert!(is_positive_and_even.contains(&6)); // both conditions
assert!(!is_positive_and_even.contains(&3)); // positive but odd

§Usage

This module is ideal for use cases where sets are defined by properties or conditions rather than explicit enumeration.

Structs§

Predicate
An intensional set, defined by a predicate function.