Skip to main content

Scope

Struct Scope 

Source
pub struct Scope<T: Hash + Eq> { /* private fields */ }
Expand description

Utility to implement lookup scopes.

Scope implements the logic needed to support name lookup in a local scope, similar to what may many need in simple programming languages or in logical formalisms. It supports looking up by name elements of any given type and implements Stack to support incremental interfaces, e.g., in SAT or SMT solvers.

A call to lookup() method produces an instance of LookupSet which can be used to further filter and navigate the objects found by name. LookupSet in turn is in charge of emitting detailed error diagnostics when a lookup fails, helping to standardize such diagnostics across all formally and its clients.

Example:

use formally::{support::*, smt::*};

let mut scope = Scope::new();
scope.push();
scope.add("x", Loc::new(42));

println!("x: {}", scope.lookup(Identifier::from("x")).one()?);

scope.pop();
assert!(scope.lookup(Identifier::from("x")).all()?.is_empty());

Here the LookupSet::one() method requires a single result to exist and emits detailed diagnostics if this is not the case. See its documentation for details.

The element type needs to implement Hash and Eq. These are used to ensure that if the same element is added twice under the same name, it is treated as a single entry.

Scope objects support being nested under other Scope objects. Nesting simulates how one would implement a nested scope in a typical language, such as the scope under a function definition that sees its arguments locally but the elements declared outside globally. Elements defined in the local Scope shadow the ones defined in the parent, but if no element is defined at all with a given name, lookup is delegated to the parent.

Example:

use formally::{support::*, smt::*};

let mut parent = Scope::new();
parent.add("x", term!(p));
parent.add("x", term!(q));
parent.add("y", term!(and p q));

let mut scope = Scope::new().with_parent(parent);

scope.add("x", term!(=> p q));

scope.lookup("x").one()?;
scope.lookup("y").one()?;

Note that the first call to one() succeeds here because the only element declared in scope with name "x" shadows all the elements with the same name in parent. The second one succeeds as well because "y" is found in the parent.

Implementations§

Source§

impl<T: Hash + Eq> Scope<T>

Source

pub fn new() -> Self

Construct an empty Scope

Source

pub fn with_parent(self, parent: Scope<T>) -> Scope<T>

Construct a new Scope similar to self but nested under a different parent.

Source

pub fn set_parent(&mut self, parent: Scope<T>)

Set a new parent to the current Scope.

Source

pub fn add(&mut self, name: &str, element: T)

Add an element to the Scope under the given name.

Source

pub fn lookup<'i>( &self, name: impl Into<Identifier<'i>>, ) -> LookupSet<'_, 'i, T, &T>

Source§

impl<T: Clone + Hash + Eq> Scope<T>

Source

pub fn merge(&mut self, other: &Scope<T>)

Clone all the elements of the given scope into self, registering them under the same names.

Trait Implementations§

Source§

impl<T: Hash + Eq> Clone for Scope<T>

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<T: Hash + Eq> Default for Scope<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Hash + Eq> Stack for Scope<T>

Source§

fn push(&mut self) -> Result<()>

Push the current state of the object to the stack.
Source§

fn pop_n(&mut self, n: usize) -> Result<()>

Pop n frames from the stack, restoring the state of the nth topmost frame. Read more
Source§

fn pop(&mut self) -> Result<()>

Shortcut for pop_n(1)

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.