Struct arithmetic_typing::TypeEnvironment

source ·
pub struct TypeEnvironment<Prim: PrimitiveType = Num> { /* private fields */ }
Expand description

Environment containing type information on named variables.

§Examples

See the crate docs for examples of usage.

§Concrete and partially specified types

The environment retains full info on the types even if the type is not concrete. Non-concrete types are tied to an environment. An environment will panic on inserting a non-concrete type via Self::insert() or other methods.

// An easy way to get a non-concrete type is to involve `any`.
let code = "(x, ...) = (1, 2, 3) as any;";
let code = Parser::parse_statements(code)?;

let mut env: TypeEnvironment = Prelude::iter().collect();
env.process_statements(&code)?;
assert!(!env["x"].is_concrete());

Implementations§

source§

impl<Prim: PrimitiveType> TypeEnvironment<Prim>

source

pub fn new() -> Self

Creates an empty environment.

source

pub fn get(&self, name: &str) -> Option<&Type<Prim>>

Gets type of the specified variable.

source

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

Iterates over variables contained in this env.

source

pub fn insert(&mut self, name: &str, ty: impl Into<Type<Prim>>) -> &mut Self

Sets type of a variable.

§Panics
  • Will panic if ty is not concrete. Non-concrete types are tied to the environment; inserting them into an env is a logical error.
source

pub fn insert_constraint( &mut self, constraint: impl Constraint<Prim>, ) -> &mut Self

Inserts a Constraint into the environment so that it can be used when parsing type annotations.

Adding a constraint is not mandatory for it to be usable during type inference; this method only influences whether the constraint is recognized during type parsing.

source

pub fn insert_object_safe_constraint( &mut self, constraint: impl ObjectSafeConstraint<Prim>, ) -> &mut Self

Inserts an ObjectSafeConstraint into the environment so that it can be used when parsing type annotations.

Other than more strict type requirements, this method is identical to Self::insert_constraint.

source

pub fn process_statements<'a, T>( &mut self, block: &Block<'a, T>, ) -> Result<Type<Prim>, Errors<Prim>>
where T: Grammar<Type<'a> = TypeAst<'a>>, NumArithmetic: MapPrimitiveType<T::Lit, Prim = Prim> + TypeArithmetic<Prim>,

Processes statements with the default type arithmetic. After processing, the environment will contain type info about newly declared vars.

This method is a shortcut for calling process_with_arithmetic with NumArithmetic::without_comparisons().

source

pub fn process_with_arithmetic<'a, T, A>( &mut self, arithmetic: &A, block: &Block<'a, T>, ) -> Result<Type<Prim>, Errors<Prim>>
where T: Grammar<Type<'a> = TypeAst<'a>>, A: MapPrimitiveType<T::Lit, Prim = Prim> + TypeArithmetic<Prim>,

Processes statements with a given arithmetic. After processing, the environment will contain type info about newly declared vars.

§Errors

Even if there are any type errors, all statements in the block will be executed to completion and all errors will be reported. However, the environment will not include any vars beyond the first failing statement.

Trait Implementations§

source§

impl<Prim: Clone + PrimitiveType> Clone for TypeEnvironment<Prim>

source§

fn clone(&self) -> TypeEnvironment<Prim>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Prim: Debug + PrimitiveType> Debug for TypeEnvironment<Prim>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim: PrimitiveType> Default for TypeEnvironment<Prim>

source§

fn default() -> Self

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

impl<Prim: PrimitiveType, S, Ty> Extend<(S, Ty)> for TypeEnvironment<Prim>
where S: Into<String>, Ty: Into<Type<Prim>>,

source§

fn extend<I: IntoIterator<Item = (S, Ty)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<Prim: PrimitiveType, S, Ty> FromIterator<(S, Ty)> for TypeEnvironment<Prim>
where S: Into<String>, Ty: Into<Type<Prim>>,

source§

fn from_iter<I: IntoIterator<Item = (S, Ty)>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<Prim: PrimitiveType> Index<&str> for TypeEnvironment<Prim>

§

type Output = Type<Prim>

The returned type after indexing.
source§

fn index(&self, name: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<Prim> Freeze for TypeEnvironment<Prim>

§

impl<Prim = Num> !RefUnwindSafe for TypeEnvironment<Prim>

§

impl<Prim> Send for TypeEnvironment<Prim>

§

impl<Prim> Sync for TypeEnvironment<Prim>

§

impl<Prim> Unpin for TypeEnvironment<Prim>
where Prim: Unpin,

§

impl<Prim = Num> !UnwindSafe for TypeEnvironment<Prim>

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.