pub struct Fcn(/* private fields */);Expand description
A finite function. Cloning is a reference-count increment.
The representation is canonical by domain, so equal functions have equal representations:
- domain
1..n(including the empty domain): a tuple (TLCTupleValue); - a non-empty domain of strings: a record, fields sorted;
- any other domain: a general function (TLC
FcnRcdValue), pairs sorted by domain element.
A record also remembers whether it is a record literal (TLC RecordValue: built
by [f |-> e] or enumerated from [f : S]) or a function that happens to have a
string domain (TLC FcnRcdValue: built by :>, @@, [x \in S |-> e] or
enumerated from [S -> T]). TLC compares and prints the two differently; equality,
hashing and fingerprints ignore the distinction.
Likewise a tuple remembers whether it is a tuple literal (TLC TupleValue: built
by <<...>>, Append, \o and the other sequence operators, or enumerated from
S1 \X S2) or a function with domain 1..n built any other way (TLC
FcnRcdValue: [i \in 1..n |-> e], :>, @@, or an element of [S -> T]). TLC
reports function-application errors and value kinds differently for the two
(Fcn::is_tuple_literal); equality, hashing and fingerprints ignore it.
Implementations§
Source§impl Fcn
impl Fcn
Sourcepub fn domain_equals(&self, dom: &Set) -> Option<bool>
pub fn domain_equals(&self, dom: &Set) -> Option<bool>
Whether the function’s domain, as a set, equals dom (DOMAIN f = dom
under ==), decided without building the domain set: by the bounds of
an interval, by lookup in an enumerated set, or factor by factor for a
product of those. None when no such shortcut applies, or when
building the domain set could fail (domain elements TLC cannot
compare); the caller then builds the set.
use dive_value::{Ctx, Int, Value};
let ctx = Ctx::default();
let (Value::Fcn(f), Value::Set(d)) = (
Value::tuple(vec![Value::TRUE, Value::FALSE]),
Value::interval(Int::Small(1), Int::Small(2), ctx),
) else {
unreachable!()
};
assert_eq!(f.domain_equals(&d), Some(true));Source§impl Fcn
impl Fcn
Sourcepub fn is_record_literal(&self) -> bool
pub fn is_record_literal(&self) -> bool
Whether this is a record literal (TLC RecordValue), as opposed to a function
that has a string domain (see Fcn).
Sourcepub fn is_tuple_literal(&self) -> bool
pub fn is_tuple_literal(&self) -> bool
Whether this is a tuple literal (TLC TupleValue), as opposed to a function
with domain 1..n that TLC keeps as a FcnRcdValue (see Fcn).
Sourcepub fn order(&self) -> ValueOrder
pub fn order(&self) -> ValueOrder
The order the fields or domain are sorted in.
Sourcepub fn as_tuple(&self) -> Option<&[Value]>
pub fn as_tuple(&self) -> Option<&[Value]>
The elements, if the function is a tuple (domain 1..n).
Sourcepub fn as_record(&self) -> Option<&[(Atom, Value)]>
pub fn as_record(&self) -> Option<&[(Atom, Value)]>
The fields in order, if the function is a record.
Sourcepub fn entry(&self, i: usize) -> Option<(Value, &Value)>
pub fn entry(&self, i: usize) -> Option<(Value, &Value)>
The i-th domain element and its value, in normalized domain order.
Sourcepub fn entry_cow(&self, i: usize) -> Option<(Cow<'_, Value>, &Value)>
pub fn entry_cow(&self, i: usize) -> Option<(Cow<'_, Value>, &Value)>
The i-th entry, borrowing stored keys where possible.
Sourcepub fn checked_domain_index(&self, argument: &Value) -> DomainLookup
pub fn checked_domain_index(&self, argument: &Value) -> DomainLookup
An exact index or miss for eager integer/Cartesian domains. Unknown means
the argument requires the caller’s original checked comparison path.
use dive_value::{Ctx, DomainLookup, Value};
let Value::Fcn(f) = Value::fcn(
vec![(Value::Int(0), Value::TRUE), (Value::Int(1), Value::FALSE)],
Ctx::dive(),
)? else { unreachable!() };
assert_eq!(f.checked_domain_index(&Value::Int(1)), DomainLookup::Found(1));
assert_eq!(f.checked_domain_index(&Value::Int(4)), DomainLookup::Missing);
assert_eq!(f.checked_domain_index(&Value::TRUE), DomainLookup::Unknown);Sourcepub fn tlc_domain_indices(&self) -> Option<&[usize]>
pub fn tlc_domain_indices(&self) -> Option<&[usize]>
Cached TLC key positions for a proven provider-free general domain. This implementation view does not mark the function TLC-normalized.
Sourcepub fn iter(&self) -> impl Iterator<Item = (Value, &Value)> + '_
pub fn iter(&self) -> impl Iterator<Item = (Value, &Value)> + '_
Iterates over (domain element, value) in normalized domain order.
Sourcepub fn values(&self) -> impl Iterator<Item = &Value> + '_
pub fn values(&self) -> impl Iterator<Item = &Value> + '_
Iterates over the values in normalized domain order, without building the domain elements.
Sourcepub fn apply(&self, arg: &Value) -> Option<&Value>
pub fn apply(&self, arg: &Value) -> Option<&Value>
Applies the function to arg, returning None when arg is not in the domain.
Sourcepub fn with_value_at(
&self,
i: usize,
new: Value,
ctx: Ctx,
) -> Result<Self, Value>
pub fn with_value_at( &self, i: usize, new: Value, ctx: Ctx, ) -> Result<Self, Value>
Replaces one range value while preserving an already canonical domain.
Returns new unchanged when rebuilding in ctx might normalize keys or
change the function’s representation; callers then use their original path.
§Errors
Returns the proposed value for an invalid index or an unproved domain.