1pub trait ProbabilisticContract {
7 fn verify(&self, confidence: f64) -> VerificationResult;
9}
10
11pub trait StatisticalProperty {
13 type Value;
15
16 fn holds(&self, value: &Self::Value) -> bool;
18}
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum VerificationResult {
23 Verified,
25 Violated,
27 Inconclusive,
29}
30
31#[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 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 pub fn probability(&self) -> f64 {
57 self.probability
58 }
59
60 pub fn description(&self) -> &str {
62 &self.description
63 }
64
65 pub fn is_rare(&self, threshold: f64) -> bool {
67 self.probability < threshold
68 }
69}
70
71#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub enum EventVerification {
74 Impossible,
76 Rare,
78 Probable,
80}
81
82impl EventVerification {
83 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}