Skip to main content

Fcn

Struct Fcn 

Source
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 (TLC TupleValue);
  • 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

Source

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

Source

pub fn kind(&self) -> FcnKind

The domain’s shape.

Source

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).

Source

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).

Source

pub fn order(&self) -> ValueOrder

The order the fields or domain are sorted in.

Source

pub fn len(&self) -> usize

The number of domain elements.

Source

pub fn is_empty(&self) -> bool

Whether the domain is empty (the function <<>>).

Source

pub fn as_tuple(&self) -> Option<&[Value]>

The elements, if the function is a tuple (domain 1..n).

Source

pub fn as_record(&self) -> Option<&[(Atom, Value)]>

The fields in order, if the function is a record.

Source

pub fn entry(&self, i: usize) -> Option<(Value, &Value)>

The i-th domain element and its value, in normalized domain order.

Source

pub fn entry_cow(&self, i: usize) -> Option<(Cow<'_, Value>, &Value)>

The i-th entry, borrowing stored keys where possible.

Source

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);
Source

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.

Source

pub fn iter(&self) -> impl Iterator<Item = (Value, &Value)> + '_

Iterates over (domain element, value) in normalized domain order.

Source

pub fn values(&self) -> impl Iterator<Item = &Value> + '_

Iterates over the values in normalized domain order, without building the domain elements.

Source

pub fn apply(&self, arg: &Value) -> Option<&Value>

Applies the function to arg, returning None when arg is not in the domain.

Source

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.

Source

pub fn ptr_eq(&self, other: &Self) -> bool

Whether two handles share the same allocation.

Source

pub fn addr(&self) -> usize

The address of the shared allocation: while both handles are alive, two handles have the same address exactly when they are ptr_eq.

Trait Implementations§

Source§

impl Clone for Fcn

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Fcn

§

impl !UnwindSafe for Fcn

§

impl Forget for Fcn

§

impl Freeze for Fcn

§

impl Move for Fcn

§

impl Send for Fcn

§

impl Sync for Fcn

§

impl Unpin for Fcn

§

impl UnsafeUnpin for Fcn

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.