Skip to main content

amari_flynn/
contracts.rs

1//! Probabilistic contract traits and types
2
3/// Trait for probabilistic contracts
4///
5/// Contracts that can be verified statistically
6pub trait ProbabilisticContract {
7    /// Verify the contract holds with given confidence
8    fn verify(&self, confidence: f64) -> VerificationResult;
9}
10
11/// Trait for statistical properties
12pub trait StatisticalProperty {
13    /// The type of value this property applies to
14    type Value;
15
16    /// Check if the property holds for a given value
17    fn holds(&self, value: &Self::Value) -> bool;
18}
19
20/// Result of verification
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum VerificationResult {
23    /// Contract verified to hold
24    Verified,
25    /// Contract violated
26    Violated,
27    /// Inconclusive (insufficient evidence)
28    Inconclusive,
29}
30
31/// Represents a rare but possible event
32///
33/// Rare events (P << 1) are tracked separately from impossible events (P = 0)
34#[derive(Clone, Debug)]
35pub struct RareEvent<T> {
36    probability: f64,
37    description: String,
38    _phantom: core::marker::PhantomData<T>,
39}
40
41impl<T> RareEvent<T> {
42    /// Create a rare event with given probability and description
43    pub fn new(probability: f64, description: impl Into<String>) -> Self {
44        assert!(
45            (0.0..1.0).contains(&probability),
46            "Rare event probability must be in (0, 1)"
47        );
48        Self {
49            probability,
50            description: description.into(),
51            _phantom: core::marker::PhantomData,
52        }
53    }
54
55    /// Get the probability of this rare event
56    pub fn probability(&self) -> f64 {
57        self.probability
58    }
59
60    /// Get the description
61    pub fn description(&self) -> &str {
62        &self.description
63    }
64
65    /// Check if this event is rare (P < threshold)
66    pub fn is_rare(&self, threshold: f64) -> bool {
67        self.probability < threshold
68    }
69}
70
71/// Event verification status
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub enum EventVerification {
74    /// Event is impossible (P = 0)
75    Impossible,
76    /// Event is rare but possible (0 < P << 1)
77    Rare,
78    /// Event is probable (P >= threshold)
79    Probable,
80}
81
82impl EventVerification {
83    /// Classify event based on probability
84    pub fn classify(probability: f64, rare_threshold: f64) -> Self {
85        if probability == 0.0 {
86            EventVerification::Impossible
87        } else if probability < rare_threshold {
88            EventVerification::Rare
89        } else {
90            EventVerification::Probable
91        }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn test_rare_event() {
101        let event = RareEvent::<()>::new(0.001, "critical_hit");
102        assert_eq!(event.probability(), 0.001);
103        assert!(event.is_rare(0.01));
104        assert!(!event.is_rare(0.0001));
105    }
106
107    #[test]
108    fn test_event_classification() {
109        assert_eq!(
110            EventVerification::classify(0.0, 0.01),
111            EventVerification::Impossible
112        );
113        assert_eq!(
114            EventVerification::classify(0.005, 0.01),
115            EventVerification::Rare
116        );
117        assert_eq!(
118            EventVerification::classify(0.5, 0.01),
119            EventVerification::Probable
120        );
121    }
122}