pub trait ModelExt<A>: Sized {
// Required method
fn bind<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>;
// Provided methods
fn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> Model<B> { ... }
fn and_then<B>(
self,
k: impl FnOnce(A) -> Model<B> + Send + 'static,
) -> Model<B> { ... }
}Expand description
ModelExt<A> provides monadic operations for composing Model<A> values.
Provides bind, map, and and_then for chaining and transforming probabilistic computations.
Example:
// Transform result with map
let transformed = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
.map(|x| x * 2.0);
// Chain dependent computations with bind
let dependent = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
.bind(|x| sample(addr!("y"), Normal::new(x, 0.5).unwrap()));Required Methods§
Sourcefn bind<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>
fn bind<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>
Monadic bind operation (>>=).
Chains two probabilistic computations where the second depends on the result of the first.
This is the fundamental operation for building complex probabilistic models from simpler parts.
The function k takes the result of this model and returns a new model.
Example:
// Dependent sampling: y depends on x
let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
.bind(|x| sample(addr!("y"), Normal::new(x, 0.1).unwrap()));Provided Methods§
Sourcefn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> Model<B>
fn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> Model<B>
Apply a function, f, to transform the result of this model.
This is the functor map operation - it transforms the output of a model without adding any additional probabilistic behavior.
Example:
// Transform the sampled value
let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
.map(|x| x.exp()); // Apply exponential functionSourcefn and_then<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>
fn and_then<B>(self, k: impl FnOnce(A) -> Model<B> + Send + 'static) -> Model<B>
Alias for bind - chains dependent probabilistic computations.
This method provides a more familiar interface for Rust developers used to Option::and_then and Result::and_then.
Example:
// Dependent sampling: y depends on x
let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap())
.and_then(|x| sample(addr!("y"), Normal::new(x, 0.1).unwrap()));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".