pub enum TermComponents {
Empty,
Word(String),
Variable(usize),
Compound(Box<[Term]>),
}Expand description
复合词项组分
- ⚠️
Variants§
Empty
不包含任何组分
- 📄占位符
Word(String)
仅包含一个字符串作为「名称」
- 📄词语
Variable(usize)
仅包含一个非负整数作为「变量」
- 📄变量
§有关「变量命名」的笔记
- 📝在NAL中,变量词项的语义仅在单个词项之内有效
- 📄这两个词项是语义等价的:
<A --> $x>和<B --> $y> - 📄这两个词项同样是语义等价的:
(&&,<#1 --> lock>,<#2 --> key>)和(&&,<#1 --> key>,<#2 --> lock>)
- 📄这两个词项是语义等价的:
- ✨具体原理:单个词项之内,变量名的改变不会影响其逻辑语义,只要名字不发生冲突(重命名后不和任一变量名相同)
- 📌这意味着:若需确定「带变量词项」的唯一性,就需要一套「自动重命名」机制来保证变量「语义相等」
- 📄此即:重命名后,可以直接通过「数据判等」实现「语义判等」逻辑
- 📌这意味着:若需确定「带变量词项」的唯一性,就需要一套「自动重命名」机制来保证变量「语义相等」
Compound(Box<[Term]>)
一般复合词项
- 📌一元、二元、多元词项
- 🚩【2024-06-12 21:18:23】现在统一「一元复合词项」「二元复合词项」和「多元复合词项」
- 📌统一使用「构造后定长数组」实现,无需复杂match匹配(少两个分支)
- 📌统一「可交换」与「不可交换」两类词项(无需考虑复合词项数目)
- 📌使用堆分配的「构造后定长数组」规避「循环包含」问题
词项类型举例:
- 📄乘积
- 📄否定
- 📄继承、蕴含
- 🚩通过「构造时自动去重并排序」实现「集合无序性」
- 📄外延差、内涵差
- 📄外延集、内涵集
- 📄外延交、内涵交
- 📄合取、析取
- 📄相似、等价
§【2024-06-12 22:12:25】有关「像」的结构实现 笔记
- 📝实际上,「有索引」和「使用占位符」是两个相同的方案
- 🎯这两个方案的核心目标皆为「在『外延像/内涵像』中取到『像占位符』的位置」
- 📄差别仅在手段不同
- 📌「有索引」的方法:多加一个字段,特别标识「像占位符位置」以便快速索引
- 📌「使用占位符」的方法:允许「占位符」独立作为词项,在「获取像占位符位置」时依靠「索引像占位符」获取位置
- 💭两个方案各有优劣
- ⚠️「有索引」的方法:以设计换性能——需要多添加字段,在不应「另创新类」的情况下徒增许多模式匹配需要
- ⚠️「使用占位符」的方法:以性能换设计——在每次「检索像占位符位置」均需要
O(内容词项数)的时间复杂度
- 🚩【2024-06-12 22:11:47】综合多方考量,最终确定(并唯一选择)第二种「使用占位符」的方案
Implementations§
Source§impl TermComponents
内建属性
impl TermComponents
内建属性
Sourcepub fn get(&self, index: usize) -> Option<&Term>
pub fn get(&self, index: usize) -> Option<&Term>
获取指定位置的组分(不一定有)
- ⚠️对于「带索引序列」不受「索引」影响
- 📄对「像」不受「像占位符」影响
Sourcepub unsafe fn get_unchecked(&self, index: usize) -> &Term
pub unsafe fn get_unchecked(&self, index: usize) -> &Term
获取指定位置的组分(不检查,直接返回元素)
- ⚠️对于「带索引序列」不受「索引」影响
- 📄对「像」不受「像占位符」影响
§Safety
⚠️只有在「确保索引不会越界」才不会引发panic和未定义行为(UB)
Sourcepub fn iter(&self) -> impl Iterator<Item = &Term>
pub fn iter(&self) -> impl Iterator<Item = &Term>
获取其中「所有元素」的迭代器
- 🚩返回一个迭代器,迭代其中所有「元素」
- ⚠️并非「深迭代」:仅迭代自身的下一级词项,不会递归深入
Sourcepub fn sort_dedup_term_vec(terms: &mut Vec<Term>)
pub fn sort_dedup_term_vec(terms: &mut Vec<Term>)
对「词项数组」重排并去重
Sourcepub fn clone_to_vec(&self) -> Vec<Term>
pub fn clone_to_vec(&self) -> Vec<Term>
获取内部所有词项,拷贝成变长数组
- 🎯用于复合词项增删相关
Sourcepub fn contain_type(&self, identifier: &str) -> bool
pub fn contain_type(&self, identifier: &str) -> bool
判断「是否包含指定类型的词项」
- 🎯支持「词项」中的方法,递归判断「是否含有变量」
- 🚩【2024-04-21 20:35:23】目前直接基于迭代器
- 📌牺牲一定性能,加快开发速度
Sourcepub fn structural_match(&self, other: &Self) -> bool
pub fn structural_match(&self, other: &Self) -> bool
判断「结构模式上是否匹配」
- 🚩判断二者在「结构大小」与(可能有的)「结构索引」是否符合
- ⚠️非递归:不会递归比较「组分是否对应匹配」
- 🎯变量替换中的「相同结构之模式替换」
- 📄
variable::find_substitute
Source§impl TermComponents
impl TermComponents
Sourcepub fn contain_var(&self) -> bool
pub fn contain_var(&self) -> bool
判断「是否包含变量(词项)」
- 🎯支持「词项」中的方法,递归判断「是否含有变量」
- 🚩【2024-04-21 20:35:23】目前直接基于迭代器
- 📌牺牲一定性能,加快开发速度
Trait Implementations§
Source§impl Clone for TermComponents
impl Clone for TermComponents
Source§fn clone(&self) -> TermComponents
fn clone(&self) -> TermComponents
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TermComponents
impl Debug for TermComponents
impl Eq for TermComponents
Source§impl From<TermComponents> for Vec<Term>
impl From<TermComponents> for Vec<Term>
Source§fn from(value: TermComponents) -> Self
fn from(value: TermComponents) -> Self
将「词项组分」转换为「可变数组<词项>」
- 🚩原子词项⇒空数组
- 🚩复合词项⇒其内所有词项构成的数组
Source§impl From<TermComponents> for Box<[Term]>
impl From<TermComponents> for Box<[Term]>
Source§fn from(value: TermComponents) -> Self
fn from(value: TermComponents) -> Self
将「词项组分」转换为「定长数组<词项>」
- 🚩原子词项⇒空数组
- 🚩复合词项⇒其内所有词项构成的数组
- ℹ️与上述对
Vec的转换不同:此处直接使用Box::new([])构造空数组
Source§impl Hash for TermComponents
impl Hash for TermComponents
Source§impl Ord for TermComponents
impl Ord for TermComponents
Source§fn cmp(&self, other: &TermComponents) -> Ordering
fn cmp(&self, other: &TermComponents) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl PartialEq for TermComponents
impl PartialEq for TermComponents
Source§impl PartialOrd for TermComponents
impl PartialOrd for TermComponents
impl StructuralPartialEq for TermComponents
Auto Trait Implementations§
impl Freeze for TermComponents
impl RefUnwindSafe for TermComponents
impl Send for TermComponents
impl Sync for TermComponents
impl Unpin for TermComponents
impl UnsafeUnpin for TermComponents
impl UnwindSafe for TermComponents
Blanket Implementations§
Source§impl<T> BoostWithOption for T
impl<T> BoostWithOption for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> JoinTo for T
impl<T> JoinTo for T
Source§fn join_to<S>(self, out: &mut String, sep: impl AsRef<str>)
fn join_to<S>(self, out: &mut String, sep: impl AsRef<str>)
将字串集中拼接到一个「目标字串」中,中途不创建任何辅助字符串 Read more
Source§impl<T> JoinTo for T
impl<T> JoinTo for T
Source§fn join_to<S>(self, out: &mut String, sep: impl AsRef<str>)
fn join_to<S>(self, out: &mut String, sep: impl AsRef<str>)
将字串集中拼接到一个「目标字串」中,中途不创建任何辅助字符串 Read more