pub enum Value {
F64(f64),
Int(i64),
U64(u64),
Usize(usize),
Bool(bool),
Arr(Arc<Vec<f64>>),
}Expand description
A value computed by a Program.
Sample sites bind their natural type — Bernoulli binds Value::Bool,
Poisson/Binomial bind Value::U64, Categorical binds
Value::Usize, DiscreteUniform binds Value::Int, continuous
distributions bind Value::F64 — and a compiled program builds a
Model<Value>, so a program returns whichever of these it computes.
The language is dynamically typed with a few numeric coercions, and the accessors below expose exactly those coercions, so a host function reading its arguments sees the same conversions the built-ins do:
| accessor | accepts |
|---|---|
as_f64 | any number; true/false as 1.0/0.0 |
as_bool | a bool; a number as “is nonzero” (NaN has no truth value) |
as_i64 | an integer in range, an integral finite F64, a bool as 0/1 |
as_u64 / as_usize | as as_i64, and non-negative |
as_array | an array |
use fugue::program::Value;
assert_eq!(Value::Usize(2).as_f64(), Some(2.0));
assert_eq!(Value::F64(3.0).as_usize(), Some(3));
assert_eq!(Value::F64(2.5).as_i64(), None);
assert_eq!(Value::Int(-1).as_u64(), None);
assert_eq!(Value::Bool(true).as_f64(), Some(1.0));
assert_eq!(Value::F64(0.0).as_bool(), Some(false));
assert_eq!(Value::from(vec![0.5, 0.5]).as_array(), Some(&[0.5, 0.5][..]));Variants§
F64(f64)
A float: 2.0-style literals, continuous sites, float arithmetic.
Int(i64)
A signed integer: 2-style literals, loop variables, .len(),
integer arithmetic, and i64 sites (DiscreteUniform).
U64(u64)
An unsigned count: the value of a u64 site (Poisson, Binomial).
Usize(usize)
An index: the value of a usize site (Categorical).
Bool(bool)
A boolean: true/false, comparisons, &&/||/!, and bool
sites (Bernoulli).
Arr(Arc<Vec<f64>>)
An array of numbers: a data array or an array literal [a, b, c].
Implementations§
Source§impl Value
impl Value
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
The value’s type as the language names it in messages: "f64",
"int", "u64", "usize", "bool" or "array".
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
The value as an f64: any number converts, and a bool is 1.0/0.0.
None for an array.
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
The value’s truth: a bool is itself, a number is “nonzero”. None for
NaN (which is neither) and for an array.
Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
The value as an i64: an integer in range, a finite integral F64 in
range, or a bool as 0/1. None otherwise.
Sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
The value as a u64: as as_i64, but non-negative
(and a U64 above i64::MAX converts too).
Trait Implementations§
Source§impl Display for Value
Formats the value the way Rust’s Display formats the corresponding Rust
value, which is what makes addr!("x", i) in a program byte-identical to
addr!("x", i) in compiled Rust: 3, 0.5, true. An array prints as
[1, 2.5] (arrays are not valid address indices).
impl Display for Value
Formats the value the way Rust’s Display formats the corresponding Rust
value, which is what makes addr!("x", i) in a program byte-identical to
addr!("x", i) in compiled Rust: 3, 0.5, true. An array prints as
[1, 2.5] (arrays are not valid address indices).