Skip to main content

CompiledProgram

Struct CompiledProgram 

Source
pub struct CompiledProgram { /* private fields */ }
Expand description

A checked program bound to its data, ready to build models.

build constructs a fresh single-use Model, so || compiled.build() is the model_fn every fugue inference driver takes. A CompiledProgram is Send + Sync; clones share their warning buffer.

use fugue::program::{Data, Program, Registry, Value};
use fugue::*;
use rand::{rngs::StdRng, SeedableRng};

let program = Program::parse(
    r#"let p <- sample(addr!("p"), Beta(2.0, 2.0));
       for i in 0..flips.len() {
           observe(addr!("flip", i), Bernoulli(p), flips[i]);
       }
       pure(p > 0.5)"#,
)
.unwrap();
let data = Data::from_json(r#"{"flips": [true, false, true, true]}"#).unwrap();
let compiled = program.compile(&Registry::new(), &data).unwrap();

let mut rng = StdRng::seed_from_u64(7);
let (favours_heads, trace) = runtime::handler::run(
    PriorHandler { rng: &mut rng, trace: Trace::default() },
    compiled.build(),
);
assert!(matches!(favours_heads, Value::Bool(_)));
// Addresses are exactly what `addr!` builds in Rust.
assert!(trace.get_f64(&addr!("p")).is_some());
assert!(trace.log_likelihood.is_finite());

// Any inference driver takes it.
let draws = adaptive_mcmc_chain(&mut rng, || compiled.build(), 200, 100);
assert_eq!(draws.len(), 200);

Implementations§

Source§

impl CompiledProgram

Source

pub fn build(&self) -> Model<Value>

Build a fresh single-use model returning the program’s value.

Source

pub fn build_f64(&self) -> Model<f64>

Build a model returning the program’s value as an f64: a number converts, a bool reads as 1.0/0.0, an array as NaN — the playground interpreter’s Model<f64> contract. For other result types, map build with Value’s accessors:

use fugue::program::{Data, Program, Registry};
use fugue::*;

let compiled = Program::parse(r#"let k <- sample(addr!("k"), Poisson(3.0)); pure(k)"#)
    .unwrap()
    .compile(&Registry::new(), &Data::new())
    .unwrap();
let as_f64: Model<f64> = compiled.build_f64();
let as_u64: Model<u64> = compiled.build().map(|v| v.as_u64().unwrap_or(0));
Source

pub fn take_warnings(&self) -> Vec<String>

Drain the runtime warnings accumulated by this program’s models so far (each a soft error that was mapped to weight -inf). At most 64 are kept between drains.

use fugue::program::{Data, Program, Registry};
use fugue::*;
use rand::{rngs::StdRng, SeedableRng};

let compiled = Program::parse(r#"let s <- sample(addr!("s"), Normal(0.0, -1.0)); pure(s)"#)
    .unwrap()
    .compile(&Registry::new(), &Data::new())
    .unwrap();
let (_, trace) = runtime::handler::run(
    PriorHandler { rng: &mut StdRng::seed_from_u64(1), trace: Trace::default() },
    compiled.build(),
);
assert_eq!(trace.total_log_weight(), f64::NEG_INFINITY);
let warnings = compiled.take_warnings();
assert!(warnings[0].starts_with("sample `s`"));
assert!(compiled.take_warnings().is_empty());

Trait Implementations§

Source§

impl Clone for CompiledProgram

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CompiledProgram

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V