Skip to main content

HashConsArena

Struct HashConsArena 

Source
pub struct HashConsArena { /* private fields */ }
Expand description

A hash-consing wrapper around Arena<Expr>.

Every mk_* method checks a HashMap<ExprKey, Idx<Expr>> before allocating. If an equal node already exists in the arena the existing Idx is returned (a hit). Otherwise the node is allocated and indexed (a miss).

§Structural sharing guarantee

For any two calls mk_foo(…args…) with structurally identical arguments, the returned Idx<Expr> values are equal. This means:

  • O(1) equality of deduplicated nodes via index comparison
  • Significantly reduced arena memory for expression-heavy workloads (e.g. type checking with many repeated sub-expressions)

Implementations§

Source§

impl HashConsArena

Source

pub fn new() -> Self

Creates an empty HashConsArena.

Source

pub fn with_capacity(cap: usize) -> Self

Creates a HashConsArena with pre-allocated capacity for cap nodes.

Source

pub fn get(&self, idx: Idx<Expr>) -> &Expr

Retrieve the Expr stored at idx.

Source

pub fn len(&self) -> usize

Returns the number of distinct nodes in the arena.

Source

pub fn is_empty(&self) -> bool

Returns true when no nodes have been allocated.

Source

pub fn stats(&self) -> HashConsStats

Returns a statistics snapshot.

Source

pub fn arena(&self) -> &Arena<Expr>

Borrow the underlying Arena<Expr> for read-only access.

Source

pub fn mk_sort(&mut self, level: Level) -> Idx<Expr>

Allocate (or deduplicate) Expr::Sort(level).

Source

pub fn mk_bvar(&mut self, idx: u32) -> Idx<Expr>

Allocate (or deduplicate) Expr::BVar(idx).

Source

pub fn mk_fvar(&mut self, id: FVarId) -> Idx<Expr>

Allocate (or deduplicate) Expr::FVar(id).

Source

pub fn mk_const(&mut self, name: Name, levels: Vec<Level>) -> Idx<Expr>

Allocate (or deduplicate) Expr::Const(name, levels).

Source

pub fn mk_app(&mut self, f_expr: Expr, a_expr: Expr) -> Idx<Expr>

Allocate (or deduplicate) Expr::App(f_expr, a_expr).

f_expr and a_expr are cloned once to build the ExprKey; no extra clones occur on a cache hit.

Source

pub fn mk_lam( &mut self, bi: BinderInfo, name: Name, dom: Expr, body: Expr, ) -> Idx<Expr>

Allocate (or deduplicate) Expr::Lam(bi, name, dom, body).

Source

pub fn mk_pi( &mut self, bi: BinderInfo, name: Name, dom: Expr, cod: Expr, ) -> Idx<Expr>

Allocate (or deduplicate) Expr::Pi(bi, name, dom, cod).

Source

pub fn mk_let( &mut self, name: Name, ty: Expr, val: Expr, body: Expr, ) -> Idx<Expr>

Allocate (or deduplicate) Expr::Let(name, ty, val, body).

Source

pub fn mk_lit(&mut self, lit: Literal) -> Idx<Expr>

Allocate (or deduplicate) Expr::Lit(lit).

Source

pub fn mk_proj( &mut self, name: Name, field_idx: u32, struct_expr: Expr, ) -> Idx<Expr>

Allocate (or deduplicate) Expr::Proj(name, field_idx, struct_expr).

Trait Implementations§

Source§

impl Default for HashConsArena

Source§

fn default() -> Self

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

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

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.