pub struct Name(/* private fields */);Expand description
A hierarchical name.
Names identify constants, inductives, and other declarations. They form a
tree: Nat.add.comm is Str(Str(Str(Anonymous, "Nat"), "add"), "comm").
Structural sharing (wave5 Stage F): Name is a newtype over a
reference-counted cell holding its outermost component (NameKind), so
cloning a whole name — e.g. the name held by every Const referencing a
constant — is an O(1) refcount bump, not a deep copy of the component chain.
The parent link inside NameKind is itself a Name, so a name shares its
entire prefix with any longer name built on it: at Mathlib scale (millions of
recurring names and prefixes) residency is proportional to the number of
distinct names, not to how many times each is referenced.
Each name also stores a hash of its own structure, computed when it is
built from its parent’s stored hash and its last component (see the
kernel-private structural_hash module) — so an Expr that mentions a
name (a Const, a binder, a projection) folds it into its own hash in O(1), without
walking the component chain.
Equality is exactly structural: the same allocation is equal, different
stored hashes are different (equal names always hash equal), and otherwise
the components are compared. Hash writes the components as it always did
(not the stored hash), so a HashMap<Name, _> behaves as before.
Match on the outermost component through Name::view: a
match name.view() { NameView::Str(parent, s) => .. } reads like the former
match name { Name::Str(parent, s) => .. }.
Implementations§
Source§impl Name
impl Name
Sourcepub fn mk_str(parent: Name, s: impl Into<String>) -> Name
pub fn mk_str(parent: Name, s: impl Into<String>) -> Name
Construct parent.s (string component). O(1) in the length of the
name — shares parent and reads its stored hash.
Sourcepub fn mk_num(parent: Name, n: u64) -> Name
pub fn mk_num(parent: Name, n: u64) -> Name
Construct parent.n (numeric component). O(1) — shares parent and
reads its stored hash.
Sourcepub fn append_str(self, s: impl Into<String>) -> Self
pub fn append_str(self, s: impl Into<String>) -> Self
Append a string component to this name.
Sourcepub fn append_num(self, n: u64) -> Self
pub fn append_num(self, n: u64) -> Self
Append a numeric component to this name.
Sourcepub fn is_anonymous(&self) -> bool
pub fn is_anonymous(&self) -> bool
Check if this is the anonymous name.
Sourcepub fn from_str(s: &str) -> Self
pub fn from_str(s: &str) -> Self
Create a Name from a dot-separated string.
Name::from_str("Nat.add.comm") produces the same as
Name::str("Nat").append_str("add").append_str("comm").
Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Returns the depth (number of components) of this name.
Anonymous has depth 0, Name::str("Nat") has depth 1,
Name::str("Nat").append_str("add") has depth 2.
Sourcepub fn last_str(&self) -> Option<&str>
pub fn last_str(&self) -> Option<&str>
Return the last string component of this name, if any.
For Nat.add, returns Some("add").
Sourcepub fn root(&self) -> Option<&str>
pub fn root(&self) -> Option<&str>
Return the root (top-level) component as a string.
For Nat.add.comm, returns "Nat".
Sourcepub fn has_prefix(&self, prefix: &Name) -> bool
pub fn has_prefix(&self, prefix: &Name) -> bool
Check whether this name has prefix as a (strict) prefix.
Nat.add.comm.has_prefix(Nat) is true.
Sourcepub fn components(&self) -> Vec<String>
pub fn components(&self) -> Vec<String>
Collect all components from root to leaf.
Returns a vector of (is_num, string_or_num) pairs.
Sourcepub fn from_components(comps: &[String]) -> Self
pub fn from_components(comps: &[String]) -> Self
Reconstruct a Name from a list of string components.
Numeric strings are converted to Num components.
Sourcepub fn replace_last(self, new_last: impl Into<String>) -> Self
pub fn replace_last(self, new_last: impl Into<String>) -> Self
Replace the last string component with new_last.
If the name ends in a numeric component, appends new_last instead.
Sourcepub fn freshen(self, suffix: u64) -> Self
pub fn freshen(self, suffix: u64) -> Self
Produce a “fresh” version of this name by appending a suffix number.
Used to avoid name collisions during elaboration.
Sourcepub fn in_namespace(&self, ns: &Name) -> bool
pub fn in_namespace(&self, ns: &Name) -> bool
Check whether this name is in the given namespace.
A name is in namespace ns if it has ns as a strict prefix.
Sourcepub fn with_suffix(self, suffix: impl Into<String>) -> Self
pub fn with_suffix(self, suffix: impl Into<String>) -> Self
Append a suffix string to this name.
Sourcepub fn to_ident_string(&self) -> String
pub fn to_ident_string(&self) -> String
Convert to a string suitable for use as a Rust identifier.
Dots and special characters are replaced with underscores.
Sourcepub fn is_str_name(&self) -> bool
pub fn is_str_name(&self) -> bool
Check whether this name is a string name (last component is a string).
Sourcepub fn is_num_name(&self) -> bool
pub fn is_num_name(&self) -> bool
Check whether this name is a numeric name (last component is a number).