use std::marker::PhantomData;
use stageleft::properties::Property;
use crate::live_collections::boundedness::Boundedness;
use crate::live_collections::keyed_singleton::KeyedSingletonBound;
use crate::live_collections::singleton::SingletonBound;
use crate::live_collections::stream::{ExactlyOnce, Ordering, Retries, TotalOrder};
#[sealed::sealed]
pub trait CommutativeProof {
fn register_proof(&self, expr: &syn::Expr);
}
#[sealed::sealed]
pub trait IdempotentProof {
fn register_proof(&self, expr: &syn::Expr);
}
#[sealed::sealed]
pub trait MonotoneProof {
fn register_proof(&self, expr: &syn::Expr);
}
#[sealed::sealed]
pub trait OrderPreservingProof {
fn register_proof(&self, expr: &syn::Expr);
}
pub struct ManualProof();
#[sealed::sealed]
impl CommutativeProof for ManualProof {
fn register_proof(&self, _expr: &syn::Expr) {}
}
#[sealed::sealed]
impl IdempotentProof for ManualProof {
fn register_proof(&self, _expr: &syn::Expr) {}
}
#[sealed::sealed]
impl MonotoneProof for ManualProof {
fn register_proof(&self, _expr: &syn::Expr) {}
}
#[sealed::sealed]
impl OrderPreservingProof for ManualProof {
fn register_proof(&self, _expr: &syn::Expr) {}
}
#[doc(inline)]
pub use crate::__manual_proof__ as manual_proof;
#[macro_export]
macro_rules! __manual_proof__ {
($(#[doc = $doc:expr])+) => {
$crate::properties::ManualProof()
};
}
pub enum NotProved {}
pub enum Proved {}
pub struct AggFuncAlgebra<Commutative = NotProved, Idempotent = NotProved, Monotone = NotProved>(
Option<Box<dyn CommutativeProof>>,
Option<Box<dyn IdempotentProof>>,
Option<Box<dyn MonotoneProof>>,
PhantomData<(Commutative, Idempotent, Monotone)>,
);
impl<C, I, M> AggFuncAlgebra<C, I, M> {
pub fn commutative(
self,
proof: impl CommutativeProof + 'static,
) -> AggFuncAlgebra<Proved, I, M> {
AggFuncAlgebra(Some(Box::new(proof)), self.1, self.2, PhantomData)
}
pub fn idempotent(self, proof: impl IdempotentProof + 'static) -> AggFuncAlgebra<C, Proved, M> {
AggFuncAlgebra(self.0, Some(Box::new(proof)), self.2, PhantomData)
}
pub fn monotone(self, proof: impl MonotoneProof + 'static) -> AggFuncAlgebra<C, I, Proved> {
AggFuncAlgebra(self.0, self.1, Some(Box::new(proof)), PhantomData)
}
pub(crate) fn register_proof(self, expr: &syn::Expr) {
if let Some(comm_proof) = self.0 {
comm_proof.register_proof(expr);
}
if let Some(idem_proof) = self.1 {
idem_proof.register_proof(expr);
}
if let Some(monotone_proof) = self.2 {
monotone_proof.register_proof(expr);
}
}
}
impl<C, I, M> Property for AggFuncAlgebra<C, I, M> {
type Root = AggFuncAlgebra;
fn make_root(_target: &mut Option<Self>) -> Self::Root {
AggFuncAlgebra(None, None, None, PhantomData)
}
}
pub struct MapFuncAlgebra<OrderPreserving = NotProved>(
Option<Box<dyn OrderPreservingProof>>,
PhantomData<OrderPreserving>,
);
impl<O> MapFuncAlgebra<O> {
pub fn order_preserving(
self,
proof: impl OrderPreservingProof + 'static,
) -> MapFuncAlgebra<Proved> {
MapFuncAlgebra(Some(Box::new(proof)), PhantomData)
}
pub(crate) fn register_proof(self, expr: &syn::Expr) {
if let Some(proof) = self.0 {
proof.register_proof(expr);
}
}
}
impl<O> Property for MapFuncAlgebra<O> {
type Root = MapFuncAlgebra;
fn make_root(_target: &mut Option<Self>) -> Self::Root {
MapFuncAlgebra(None, PhantomData)
}
}
#[diagnostic::on_unimplemented(
message = "Because the input stream has ordering `{O}`, the closure must demonstrate commutativity with a `commutative = ...` annotation.",
label = "required for this call",
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."
)]
#[sealed::sealed]
pub trait ValidCommutativityFor<O: Ordering> {}
#[sealed::sealed]
impl ValidCommutativityFor<TotalOrder> for NotProved {}
#[sealed::sealed]
impl<O: Ordering> ValidCommutativityFor<O> for Proved {}
#[diagnostic::on_unimplemented(
message = "Because the input stream has retries `{R}`, the closure must demonstrate idempotence with an `idempotent = ...` annotation.",
label = "required for this call",
note = "To intentionally process the stream by observing non-deterministic (randomly duplicated) retries, use `.assume_retries`. This introduces non-determinism so avoid unless necessary."
)]
#[sealed::sealed]
pub trait ValidIdempotenceFor<R: Retries> {}
#[sealed::sealed]
impl ValidIdempotenceFor<ExactlyOnce> for NotProved {}
#[sealed::sealed]
impl<R: Retries> ValidIdempotenceFor<R> for Proved {}
#[sealed::sealed]
pub trait ApplyMonotoneStream<P, B2: SingletonBound> {}
#[sealed::sealed]
impl<B: Boundedness> ApplyMonotoneStream<NotProved, B> for B {}
#[sealed::sealed]
impl<B: Boundedness> ApplyMonotoneStream<Proved, B::StreamToMonotone> for B {}
#[sealed::sealed]
pub trait ApplyMonotoneKeyedStream<P, B2: KeyedSingletonBound> {}
#[sealed::sealed]
impl<B: Boundedness> ApplyMonotoneKeyedStream<NotProved, B> for B {}
#[sealed::sealed]
impl<B: Boundedness> ApplyMonotoneKeyedStream<Proved, B::KeyedStreamToMonotone> for B {}
#[sealed::sealed]
pub trait ApplyOrderPreservingSingleton<P, B2: SingletonBound> {}
#[sealed::sealed]
impl<B: SingletonBound> ApplyOrderPreservingSingleton<NotProved, B::UnderlyingBound> for B {}
#[sealed::sealed]
impl<B: SingletonBound> ApplyOrderPreservingSingleton<Proved, B> for B {}