Skip to main content

Handler

Trait Handler 

Source
pub trait Handler {
    // Required methods
    fn on_sample_f64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<f64>,
    ) -> f64;
    fn on_sample_bool(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<bool>,
    ) -> bool;
    fn on_sample_u64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<u64>,
    ) -> u64;
    fn on_sample_usize(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<usize>,
    ) -> usize;
    fn on_observe_f64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<f64>,
        value: f64,
    );
    fn on_observe_bool(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<bool>,
        value: bool,
    );
    fn on_observe_u64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<u64>,
        value: u64,
    );
    fn on_observe_usize(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<usize>,
        value: usize,
    );
    fn on_factor(&mut self, logw: f64);
    fn finish(self) -> Trace
       where Self: Sized;

    // Provided methods
    fn on_sample_i64(
        &mut self,
        addr: &Address,
        _dist: &dyn Distribution<i64>,
    ) -> i64 { ... }
    fn on_observe_i64(
        &mut self,
        addr: &Address,
        _dist: &dyn Distribution<i64>,
        _value: i64,
    ) { ... }
}
Expand description

Core trait for interpreting probabilistic model effects.

Handlers define how to interpret the three fundamental effects in probabilistic programming: sampling, observation, and factoring. Different implementations enable different execution modes.

To change what an existing handler does at some kinds of site and forward the rest to it, wrap it in a Delegate instead of implementing every method.

Example:


// Use a built-in handler
let mut rng = StdRng::seed_from_u64(42);
let handler = PriorHandler {
    rng: &mut rng,
    trace: Trace::default()
};
let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap());
let (result, trace) = runtime::handler::run(handler, model);

Required Methods§

Source

fn on_sample_f64(&mut self, addr: &Address, dist: &dyn Distribution<f64>) -> f64

Handle an f64 sampling operation (continuous distributions).

Source

fn on_sample_bool( &mut self, addr: &Address, dist: &dyn Distribution<bool>, ) -> bool

Handle a bool sampling operation (Bernoulli).

Source

fn on_sample_u64(&mut self, addr: &Address, dist: &dyn Distribution<u64>) -> u64

Handle a u64 sampling operation (Poisson, Binomial).

Source

fn on_sample_usize( &mut self, addr: &Address, dist: &dyn Distribution<usize>, ) -> usize

Handle a usize sampling operation (Categorical).

Source

fn on_observe_f64( &mut self, addr: &Address, dist: &dyn Distribution<f64>, value: f64, )

Handle an f64 observation operation.

Source

fn on_observe_bool( &mut self, addr: &Address, dist: &dyn Distribution<bool>, value: bool, )

Handle a bool observation operation.

Source

fn on_observe_u64( &mut self, addr: &Address, dist: &dyn Distribution<u64>, value: u64, )

Handle a u64 observation operation.

Source

fn on_observe_usize( &mut self, addr: &Address, dist: &dyn Distribution<usize>, value: usize, )

Handle a usize observation operation.

Source

fn on_factor(&mut self, logw: f64)

Handle a factor operation.

This method is called when the model encounters a factor operation. The handler typically adds the log-weight to the trace.

§Arguments
  • logw - Log-weight to add to the model’s total weight
Source

fn finish(self) -> Trace
where Self: Sized,

Finalize the handler and return the accumulated trace.

This method is called after model execution completes to retrieve the final trace containing all choices and log-weights.

Provided Methods§

Source

fn on_sample_i64( &mut self, addr: &Address, _dist: &dyn Distribution<i64>, ) -> i64

Handle an i64 sampling operation (signed discrete distributions).

This has a default implementation that panics so that handlers written before the i64 sample path existed keep compiling unchanged; every handler shipped in this crate overrides it. A model only reaches this method if it contains a Model::SampleI64 node (e.g. DiscreteUniform).

Source

fn on_observe_i64( &mut self, addr: &Address, _dist: &dyn Distribution<i64>, _value: i64, )

Handle an i64 observation operation.

Defaults to a panic for the same forward-compatibility reason as Handler::on_sample_i64; all in-crate handlers override it.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Handler for SafeScoreGivenTrace

Source§

impl Handler for ScoreGivenTrace

Source§

impl<'e> Handler for StrictScoreGivenTrace<'e>

Source§

impl<'r, 'f, 'e, R: RngCore> Handler for ReconcilingScoreGivenTrace<'r, 'f, 'e, R>

Source§

impl<'r, R: RngCore> Handler for PriorHandler<'r, R>

Source§

impl<'r, R: RngCore> Handler for ReplayHandler<'r, R>

Source§

impl<'r, R: RngCore> Handler for SafeReplayHandler<'r, R>

Source§

impl<H: Handler, O: Overrides<H>> Handler for Delegate<H, O>

Every method calls the hook of the same name on overrides, passing inner.