hydro_lang/properties/
mod.rs1use std::marker::PhantomData;
4
5use stageleft::properties::Property;
6
7use crate::live_collections::boundedness::Boundedness;
8use crate::live_collections::keyed_singleton::KeyedSingletonBound;
9use crate::live_collections::singleton::SingletonBound;
10use crate::live_collections::stream::{ExactlyOnce, Ordering, Retries, TotalOrder};
11
12#[sealed::sealed]
14pub trait CommutativeProof {
15 fn register_proof(&self, expr: &syn::Expr);
19}
20
21#[sealed::sealed]
23pub trait IdempotentProof {
24 fn register_proof(&self, expr: &syn::Expr);
28}
29
30#[sealed::sealed]
32pub trait MonotoneProof {
33 fn register_proof(&self, expr: &syn::Expr);
37}
38
39#[sealed::sealed]
41pub trait OrderPreservingProof {
42 fn register_proof(&self, expr: &syn::Expr);
46}
47
48#[sealed::sealed]
50pub trait ConsistencyProof {}
51
52pub struct ManualProof();
57#[sealed::sealed]
58impl CommutativeProof for ManualProof {
59 fn register_proof(&self, _expr: &syn::Expr) {}
60}
61#[sealed::sealed]
62impl IdempotentProof for ManualProof {
63 fn register_proof(&self, _expr: &syn::Expr) {}
64}
65#[sealed::sealed]
66impl MonotoneProof for ManualProof {
67 fn register_proof(&self, _expr: &syn::Expr) {}
68}
69#[sealed::sealed]
70impl OrderPreservingProof for ManualProof {
71 fn register_proof(&self, _expr: &syn::Expr) {}
72}
73#[sealed::sealed]
74impl ConsistencyProof for ManualProof {}
75
76#[doc(inline)]
77pub use crate::__manual_proof__ as manual_proof;
78
79#[macro_export]
80macro_rules! __manual_proof__ {
98 ($(#[doc = $doc:expr])+) => {
99 $crate::properties::ManualProof()
100 };
101}
102
103pub enum NotProved {}
105
106pub enum Proved {}
108
109pub trait IsProved {
111 const IS_PROVED: bool;
113}
114impl IsProved for NotProved {
115 const IS_PROVED: bool = false;
116}
117impl IsProved for Proved {
118 const IS_PROVED: bool = true;
119}
120
121pub struct AggFuncAlgebra<Commutative = NotProved, Idempotent = NotProved, Monotone = NotProved>(
139 Option<Box<dyn CommutativeProof>>,
140 Option<Box<dyn IdempotentProof>>,
141 Option<Box<dyn MonotoneProof>>,
142 PhantomData<(Commutative, Idempotent, Monotone)>,
143);
144
145impl<C, I, M> AggFuncAlgebra<C, I, M> {
146 pub fn commutative(
148 self,
149 proof: impl CommutativeProof + 'static,
150 ) -> AggFuncAlgebra<Proved, I, M> {
151 AggFuncAlgebra(Some(Box::new(proof)), self.1, self.2, PhantomData)
152 }
153
154 pub fn idempotent(self, proof: impl IdempotentProof + 'static) -> AggFuncAlgebra<C, Proved, M> {
156 AggFuncAlgebra(self.0, Some(Box::new(proof)), self.2, PhantomData)
157 }
158
159 pub fn monotone(self, proof: impl MonotoneProof + 'static) -> AggFuncAlgebra<C, I, Proved> {
161 AggFuncAlgebra(self.0, self.1, Some(Box::new(proof)), PhantomData)
162 }
163
164 pub(crate) fn register_proof(self, expr: &syn::Expr) {
166 if let Some(comm_proof) = self.0 {
167 comm_proof.register_proof(expr);
168 }
169
170 if let Some(idem_proof) = self.1 {
171 idem_proof.register_proof(expr);
172 }
173
174 if let Some(monotone_proof) = self.2 {
175 monotone_proof.register_proof(expr);
176 }
177 }
178}
179
180impl<C, I, M> Property for AggFuncAlgebra<C, I, M> {
181 type Root = AggFuncAlgebra;
182
183 fn make_root(_target: &mut Option<Self>) -> Self::Root {
184 AggFuncAlgebra(None, None, None, PhantomData)
185 }
186}
187
188pub struct MapFuncAlgebra<OrderPreserving = NotProved>(
192 Option<Box<dyn OrderPreservingProof>>,
193 PhantomData<OrderPreserving>,
194);
195
196impl<O> MapFuncAlgebra<O> {
197 pub fn order_preserving(
199 self,
200 proof: impl OrderPreservingProof + 'static,
201 ) -> MapFuncAlgebra<Proved> {
202 MapFuncAlgebra(Some(Box::new(proof)), PhantomData)
203 }
204
205 pub(crate) fn register_proof(self, expr: &syn::Expr) {
207 if let Some(proof) = self.0 {
208 proof.register_proof(expr);
209 }
210 }
211}
212
213impl<O> Property for MapFuncAlgebra<O> {
214 type Root = MapFuncAlgebra;
215
216 fn make_root(_target: &mut Option<Self>) -> Self::Root {
217 MapFuncAlgebra(None, PhantomData)
218 }
219}
220
221#[diagnostic::on_unimplemented(
223 message = "Because the input stream has ordering `{O}`, the closure must demonstrate commutativity with a `commutative = ...` annotation.",
224 label = "required for this call",
225 note = "To intentionally process the stream by observing a non-deterministic (shuffled) order of elements, use `.assume_ordering`. This introduces non-determinism so avoid unless necessary."
226)]
227#[sealed::sealed]
228pub trait ValidCommutativityFor<O: Ordering> {}
229#[sealed::sealed]
230impl ValidCommutativityFor<TotalOrder> for NotProved {}
231#[sealed::sealed]
232impl<O: Ordering> ValidCommutativityFor<O> for Proved {}
233
234#[diagnostic::on_unimplemented(
236 message = "Because the input stream has retries `{R}`, the closure must demonstrate idempotence with an `idempotent = ...` annotation.",
237 label = "required for this call",
238 note = "To intentionally process the stream by observing non-deterministic (randomly duplicated) retries, use `.assume_retries`. This introduces non-determinism so avoid unless necessary."
239)]
240#[sealed::sealed]
241pub trait ValidIdempotenceFor<R: Retries> {}
242#[sealed::sealed]
243impl ValidIdempotenceFor<ExactlyOnce> for NotProved {}
244#[sealed::sealed]
245impl<R: Retries> ValidIdempotenceFor<R> for Proved {}
246
247#[sealed::sealed]
250pub trait ApplyMonotoneStream<P, B2: SingletonBound> {}
251
252#[sealed::sealed]
253impl<B: Boundedness> ApplyMonotoneStream<NotProved, B> for B {}
254
255#[sealed::sealed]
256impl<B: Boundedness> ApplyMonotoneStream<Proved, B::StreamToMonotone> for B {}
257
258#[sealed::sealed]
261pub trait ApplyMonotoneKeyedStream<P, B2: KeyedSingletonBound> {}
262
263#[sealed::sealed]
264impl<B: Boundedness> ApplyMonotoneKeyedStream<NotProved, B> for B {}
265
266#[sealed::sealed]
267impl<B: Boundedness> ApplyMonotoneKeyedStream<Proved, B::KeyedStreamToMonotone> for B {}
268
269#[sealed::sealed]
272pub trait ApplyOrderPreservingSingleton<P, B2: SingletonBound> {}
273
274#[sealed::sealed]
275impl<B: SingletonBound> ApplyOrderPreservingSingleton<NotProved, B::UnderlyingBound> for B {}
276
277#[sealed::sealed]
278impl<B: SingletonBound> ApplyOrderPreservingSingleton<Proved, B> for B {}