pub struct Registry { /* private fields */ }Expand description
The distributions and pure functions programs can call, by name.
Registry::new (also Default) holds the built-ins: the distributions
Normal, Uniform, LogNormal, Exponential, Beta, Gamma,
InverseGamma, StudentT, Cauchy, Laplace, Weibull, ChiSquared
(f64 sites), Bernoulli (bool), Binomial, Poisson (u64),
Categorical (usize) and DiscreteUniform (i64), with fugue’s
constructors and parameterizations; and the functions exp, ln (alias
log), sqrt, abs, floor, sin, cos, tanh (one argument) and
pow, min, max (two), all on f64. Categorical takes either its
probabilities as arguments, Categorical(0.2, 0.8), or one array,
Categorical(probs), of any length.
A host adds its own with register_distribution
and register_function; registering an
existing name replaces it. Names a program can call are identifiers (the
text syntax’s); an entry under any other name is unreachable.
use fugue::program::{Arity, Data, HostDist, Program, Registry, SiteType, Value};
use fugue::{Categorical, Distribution};
let mut registry = Registry::new();
// A usize site whose probabilities come from the program's arguments.
registry.register_distribution("Pick", SiteType::Usize, Arity::Exact(1), |args| {
let p = args[0].as_f64().ok_or("Pick: p must be a number")?;
let d = Categorical::new(vec![1.0 - p, p]).map_err(|e| e.to_string())?;
Ok(HostDist::Usize(d.clone_box()))
});
// A pure function over ints and bools.
registry.register_function("bump", Arity::Exact(2), |args| {
let n = args[0].as_i64().ok_or("bump: n must be an integer")?;
let on = args[1].as_bool().ok_or("bump: flag must be a bool")?;
Ok(Value::Int(if on { n + 1 } else { n }))
});
let program = Program::parse(
r#"let k <- sample(addr!("k"), Pick(0.25));
pure(bump(3, k == 1))"#,
)
.unwrap();
assert!(program.compile(®istry, &Data::new()).is_ok());
// Unknown names are compile-time errors.
assert!(program.compile(&Registry::new(), &Data::new()).is_err());Implementations§
Source§impl Registry
impl Registry
Sourcepub fn empty() -> Registry
pub fn empty() -> Registry
A registry with nothing in it, for hosts that want to expose only their own vocabulary.
Sourcepub fn register_distribution<F>(
&mut self,
name: impl Into<String>,
site: SiteType,
arity: Arity,
ctor: F,
) -> &mut Registry
pub fn register_distribution<F>( &mut self, name: impl Into<String>, site: SiteType, arity: Arity, ctor: F, ) -> &mut Registry
Register a distribution constructor under name.
site declares the type of the sites it builds; the constructor must
return a HostDist of that type. The declaration is what lets a site
keep a stable type when construction fails: an Err from the
constructor (or a HostDist of another type) is a runtime soft error —
the site is filled with a placeholder of the declared type, the
execution’s weight becomes -inf, and a warning is recorded (see the
module docs’ error policy). arity is checked at compile time.
The constructor sees the argument values exactly as the program
computed them; Value’s accessors apply the language’s coercions.
It must be deterministic in its arguments (replay and scoring rebuild
the same site many times) and must not panic.
Sourcepub fn register_function<F>(
&mut self,
name: impl Into<String>,
arity: Arity,
f: F,
) -> &mut Registry
pub fn register_function<F>( &mut self, name: impl Into<String>, arity: Arity, f: F, ) -> &mut Registry
Register a pure function under name. arity is checked at compile
time; an Err at runtime is a soft error (weight -inf plus a
warning). The function must be deterministic and must not panic.
Sourcepub fn has_distribution(&self, name: &str) -> bool
pub fn has_distribution(&self, name: &str) -> bool
Whether a distribution is registered under name.
Sourcepub fn has_function(&self, name: &str) -> bool
pub fn has_function(&self, name: &str) -> bool
Whether a function is registered under name.
Sourcepub fn distribution_site_type(&self, name: &str) -> Option<SiteType>
pub fn distribution_site_type(&self, name: &str) -> Option<SiteType>
The declared site type of the distribution registered under name.