use crate::containers::AppendOnlyVec;
use crate::containers::DenseId;
use crate::containers::IndexLike;
use crate::containers::MapToken;
use crate::containers::ShrinkPolicy;
use crate::containers::SpMap;
use crate::containers::VecToken;
use crate::id::{ENodeKind, id_at};
#[derive(Clone, Copy, Debug)]
pub struct SortRegistryToken(MapToken);
#[derive(Clone, Copy, Debug)]
pub struct OpRegistryToken {
map: MapToken,
completion: VecToken,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AssocDir {
Left,
Right,
Both,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Clamp {
None,
Idempotent,
Nilpotent { order: u8 },
}
#[derive(Clone, Debug)]
pub enum UnitRef {
Lit { token: String },
Ctor { term: crate::ast::Term },
}
#[derive(Clone, Debug)]
pub enum OpKind<S: DenseId> {
Normal {
arg_sorts: Vec<S>,
},
Commutative {
arg_sorts: [S; 2],
},
A {
arg_sort: S,
dir: AssocDir,
},
MSet {
arg_sort: S,
clamp: Clamp,
identity: Option<UnitRef>,
cancellative: bool,
},
Set {
arg_sort: S,
clamp: Clamp,
identity: Option<UnitRef>,
cancellative: bool,
},
Lit,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OpMeta {
pub is_constructor: bool,
pub cost: u32,
pub unextractable: bool,
}
impl Default for OpMeta {
fn default() -> Self {
Self {
is_constructor: false,
cost: 1,
unextractable: false,
}
}
}
#[derive(Clone, Debug)]
pub struct OpInfo<S: DenseId> {
pub name: String,
pub return_sort: S,
pub kind: OpKind<S>,
pub is_constructor: bool,
pub cost: u32,
pub unextractable: bool,
}
impl<S: DenseId> OpInfo<S> {
pub fn canon_class(&self) -> ENodeKind {
match &self.kind {
OpKind::Normal { arg_sorts } => match arg_sorts.len() {
0 => ENodeKind::Plain0,
1 => ENodeKind::Plain1,
2 => ENodeKind::Plain2,
3 => ENodeKind::Plain3,
_ => ENodeKind::PlainN,
},
OpKind::Commutative { .. } => ENodeKind::SPair,
OpKind::A { .. } => ENodeKind::Seq,
OpKind::MSet { .. } => ENodeKind::MSet,
OpKind::Set { .. } => ENodeKind::Set,
OpKind::Lit => ENodeKind::Lit,
}
}
}
#[derive(Debug)]
pub struct SortRegistry<S: DenseId, const TRACK: bool> {
map: SpMap<String, (), S::Index, TRACK>,
builtin_count: usize,
concrete_count: usize,
_phantom: core::marker::PhantomData<S>,
}
impl<S: DenseId, const TRACK: bool> Default for SortRegistry<S, TRACK> {
fn default() -> Self {
Self::new()
}
}
impl<S: DenseId, const TRACK: bool> SortRegistry<S, TRACK> {
pub fn new() -> Self {
Self {
map: SpMap::new(),
builtin_count: 0,
concrete_count: 0,
_phantom: core::marker::PhantomData,
}
}
pub fn register_builtins(&mut self, sort_names: &[&str]) {
assert!(
self.map.is_empty(),
"register_builtins must be called on empty registry"
);
for name in sort_names {
self.map
.try_insert(name.to_string(), ())
.expect("registry id space exhausted for its index word");
}
self.builtin_count = self.map.len();
self.concrete_count = self.map.len();
}
pub fn intern(&mut self, name: &str) -> S {
if let Some(id) = self.map.id_of(&name.to_owned()) {
return id_at::<S>(id.as_usize());
}
let id = self
.map
.try_insert(name.to_owned(), ())
.expect("registry id space exhausted for its index word");
id_at::<S>(id.as_usize())
}
pub fn name(&self, id: S) -> &str {
self.map.key(id.to_index())
}
pub fn is_builtin(&self, id: S) -> bool {
id.to_usize() < self.builtin_count
}
pub fn is_concrete(&self, id: S) -> bool {
id.to_usize() < self.concrete_count
}
pub fn concrete_count(&self) -> usize {
self.concrete_count
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn len(&self) -> usize {
self.map.log_len().as_usize()
}
pub fn id_by_name(&self, name: &str) -> Option<S> {
self.map
.id_of(&name.to_owned())
.map(|id| id_at::<S>(id.as_usize()))
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> SortRegistryToken {
SortRegistryToken(
self.map
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
)
}
pub fn restore(&mut self, token: SortRegistryToken) {
self.map
.try_restore(token.0)
.expect("restore: token minted by this container's own mark");
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CompletionKind {
NotCompletion,
MSet,
Set,
}
#[derive(Clone, Copy, Debug)]
struct CompletionSlot {
kind: CompletionKind,
msets: usize,
sets: usize,
}
#[cfg(test)]
thread_local! {
static COMPLETION_OPS_SCANS: core::cell::Cell<usize> = const { core::cell::Cell::new(0) };
}
#[derive(Debug)]
pub struct OpRegistry<O: DenseId, S: DenseId, const TRACK: bool> {
map: SpMap<String, OpInfo<S>, O::Index, TRACK>,
completion: AppendOnlyVec<CompletionSlot, O::Index, TRACK>,
builtin_count: usize,
concrete_sort_count: usize,
_phantom: core::marker::PhantomData<O>,
}
impl<O: crate::DenseId, S: DenseId, const TRACK: bool> Default for OpRegistry<O, S, TRACK> {
fn default() -> Self {
Self::new()
}
}
impl<O: crate::DenseId, S: DenseId, const TRACK: bool> OpRegistry<O, S, TRACK> {
pub fn new() -> Self {
Self {
map: SpMap::new(),
completion: AppendOnlyVec::new(),
builtin_count: 0,
concrete_sort_count: 0,
_phantom: core::marker::PhantomData,
}
}
pub fn register_builtins<
L: crate::literal::LitVal,
M: crate::lit_model::LitModel<Value = L>,
>(
&mut self,
model: &M,
sorts: &SortRegistry<S, TRACK>,
) {
assert!(
self.map.is_empty(),
"register_builtins must be called on empty registry"
);
for op_desc in model.ops() {
let arg_sorts: Vec<S> = op_desc
.arg_sorts
.iter()
.map(|name| {
sorts.id_by_name(name).unwrap_or_else(|| {
panic!("unknown sort '{name}' in LitOpDesc '{}'", op_desc.name)
})
})
.collect();
let ret_sort = sorts.id_by_name(op_desc.ret_sort).unwrap_or_else(|| {
panic!(
"unknown sort '{}' in LitOpDesc '{}'",
op_desc.ret_sort, op_desc.name
)
});
self.insert(
op_desc.name,
ret_sort,
OpKind::Normal { arg_sorts },
OpMeta::default(),
);
}
for sort_desc in model.sorts() {
let sort_id = sorts.id_by_name(sort_desc.name).unwrap();
let lit_name = format!("@{}", sort_desc.name);
self.insert(&lit_name, sort_id, OpKind::Lit, OpMeta::default());
}
self.builtin_count = self.map.log_len().as_usize();
self.concrete_sort_count = sorts.concrete_count();
}
pub fn is_builtin(&self, id: O) -> bool {
id.to_usize() < self.builtin_count
}
pub fn is_prim_op(&self, id: O) -> bool {
self.is_builtin(id) && !matches!(self.map.get_val(id.to_index()).kind, OpKind::Lit)
}
pub fn register(&mut self, name: &str, arg_sorts: &[S], return_sort: S) -> O {
self.insert(
name,
return_sort,
OpKind::Normal {
arg_sorts: arg_sorts.to_vec(),
},
OpMeta::default(),
)
}
pub fn register_c(&mut self, name: &str, arg_sorts: [S; 2], return_sort: S) -> O {
self.insert(
name,
return_sort,
OpKind::Commutative { arg_sorts },
OpMeta::default(),
)
}
pub fn register_a(&mut self, name: &str, arg_sort: S, return_sort: S, dir: AssocDir) -> O {
self.insert(
name,
return_sort,
OpKind::A { arg_sort, dir },
OpMeta::default(),
)
}
pub fn register_mset(&mut self, name: &str, arg_sort: S, return_sort: S) -> O {
self.insert(
name,
return_sort,
OpKind::MSet {
arg_sort,
clamp: Clamp::None,
identity: None,
cancellative: false,
},
OpMeta::default(),
)
}
pub fn register_set(&mut self, name: &str, arg_sort: S, return_sort: S) -> O {
self.insert(
name,
return_sort,
OpKind::Set {
arg_sort,
clamp: Clamp::Idempotent,
identity: None,
cancellative: false,
},
OpMeta::default(),
)
}
pub fn register_lit(&mut self, name: &str, return_sort: S) -> O {
self.insert(name, return_sort, OpKind::Lit, OpMeta::default())
}
pub fn register_kind(&mut self, name: &str, return_sort: S, kind: OpKind<S>) -> O {
self.insert(name, return_sort, kind, OpMeta::default())
}
pub fn register_kind_meta(
&mut self,
name: &str,
return_sort: S,
kind: OpKind<S>,
meta: OpMeta,
) -> O {
self.insert(name, return_sort, kind, meta)
}
pub fn info(&self, id: O) -> &OpInfo<S> {
self.map.get_val(id.to_index())
}
pub fn names(&self) -> impl Iterator<Item = &str> + '_ {
self.map.iter().map(|(_, i)| i.name.as_str())
}
pub fn meta_table(&self) -> Vec<OpMeta> {
self.map
.iter()
.map(|(_, i)| OpMeta {
is_constructor: i.is_constructor,
cost: i.cost,
unextractable: i.unextractable,
})
.collect()
}
pub fn is_mset(&self, id: O) -> bool {
matches!(self.map.get_val(id.to_index()).kind, OpKind::MSet { .. })
}
pub fn mset_ops(&self) -> impl Iterator<Item = O> + '_ {
self.ops_of_kind(CompletionKind::MSet)
}
fn ops_of_kind(&self, kind: CompletionKind) -> impl Iterator<Item = O> + '_ {
self.completion
.as_slice()
.iter()
.enumerate()
.filter(move |(_, slot)| slot.kind == kind)
.map(|(i, _)| id_at::<O>(i))
}
fn completion_counts(&self) -> (usize, usize) {
match self.completion.as_slice().last() {
Some(slot) => (slot.msets, slot.sets),
None => (0, 0),
}
}
pub fn mset_op_count(&self) -> usize {
self.completion_counts().0
}
pub fn is_set(&self, id: O) -> bool {
matches!(self.map.get_val(id.to_index()).kind, OpKind::Set { .. })
}
pub fn set_ops(&self) -> impl Iterator<Item = O> + '_ {
self.ops_of_kind(CompletionKind::Set)
}
pub fn set_op_count(&self) -> usize {
self.completion_counts().1
}
pub fn completion_ops(&self) -> Vec<O> {
#[cfg(test)]
COMPLETION_OPS_SCANS.with(|n| n.set(n.get() + 1));
let mut v: Vec<O> = self.mset_ops().collect();
v.extend(self.set_ops());
v
}
pub fn completion_op_count(&self) -> usize {
let (msets, sets) = self.completion_counts();
msets + sets
}
pub fn completion_column(&self, id: O) -> Option<usize> {
let slot = self.completion.as_slice().get(id.to_usize())?;
match slot.kind {
CompletionKind::NotCompletion => None,
CompletionKind::MSet => Some(slot.msets - 1),
CompletionKind::Set => Some(self.completion_counts().0 + slot.sets - 1),
}
}
pub fn id_by_name(&self, name: &str) -> Option<O> {
self.map
.id_of(&name.to_owned())
.map(|id| id_at::<O>(id.as_usize()))
}
pub fn lit_op_for_sort(&self, sort: S) -> Option<O> {
self.map
.iter()
.enumerate()
.find(|(_, (_, info))| matches!(info.kind, OpKind::Lit) && info.return_sort == sort)
.map(|(i, _)| id_at::<O>(i))
}
pub fn find_lift_op(&self, from: S, to: S) -> Option<O> {
self.map
.iter()
.enumerate()
.find(|(_, (_, info))| {
info.return_sort == to
&& matches!(&info.kind, OpKind::Normal { arg_sorts } if arg_sorts.len() == 1 && arg_sorts[0] == from)
})
.map(|(i, _)| id_at::<O>(i))
}
fn insert(&mut self, name: &str, return_sort: S, kind: OpKind<S>, meta: OpMeta) -> O {
assert!(
!self.map.contains_key(&name.to_owned()),
"operator '{name}' already registered"
);
match &kind {
OpKind::Commutative { arg_sorts } => assert!(
arg_sorts[0] == arg_sorts[1],
"commutative operator '{name}' must use the same sort for both arguments"
),
OpKind::A { arg_sort, .. }
| OpKind::MSet { arg_sort, .. }
| OpKind::Set { arg_sort, .. } => assert!(
*arg_sort == return_sort,
"associative operator '{name}' must use the same argument and return sort"
),
OpKind::Normal { .. } | OpKind::Lit => {}
}
if self.builtin_count > 0 && return_sort.to_usize() < self.concrete_sort_count {
panic!(
"operator '{name}' cannot return concrete sort (index {})",
return_sort.to_usize()
);
}
let completion_kind = match kind {
OpKind::MSet { .. } => CompletionKind::MSet,
OpKind::Set { .. } => CompletionKind::Set,
_ => CompletionKind::NotCompletion,
};
let id = self
.map
.try_insert(
name.to_owned(),
OpInfo {
name: name.to_owned(),
return_sort,
kind,
is_constructor: meta.is_constructor,
cost: meta.cost,
unextractable: meta.unextractable,
},
)
.expect("registry id space exhausted for its index word");
let (msets, sets) = self.completion_counts();
let slot = CompletionSlot {
kind: completion_kind,
msets: msets + usize::from(completion_kind == CompletionKind::MSet),
sets: sets + usize::from(completion_kind == CompletionKind::Set),
};
let slot_id = self
.completion
.try_push(slot)
.expect("completion table shares the op log's index word");
debug_assert_eq!(
slot_id.as_usize(),
id.as_usize(),
"one completion slot per op, pushed in op-id order"
);
id_at::<O>(id.as_usize())
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> OpRegistryToken {
OpRegistryToken {
map: self
.map
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
completion: self
.completion
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
}
}
pub fn restore(&mut self, token: OpRegistryToken) {
self.map
.try_restore(token.map)
.expect("restore: token minted by this container's own mark");
self.completion
.try_restore(token.completion)
.expect("restore: token minted by this container's own mark");
debug_assert_eq!(
self.completion.len().as_usize(),
self.map.log_len().as_usize(),
"the completion table is truncated with the op log"
);
}
}
#[derive(Clone, Copy, Debug)]
pub struct RuleRegistryToken(MapToken);
#[derive(Clone, Debug)]
pub struct RuleInfo {
pub name: String,
pub lhs: String,
pub rhs: String,
}
pub struct RuleRegistry<const TRACK: bool> {
map: SpMap<String, RuleInfo, <crate::id::RuleId as DenseId>::Index, TRACK>,
}
impl<const TRACK: bool> Default for RuleRegistry<TRACK> {
fn default() -> Self {
Self::new()
}
}
impl<const TRACK: bool> RuleRegistry<TRACK> {
pub fn new() -> Self {
Self { map: SpMap::new() }
}
pub fn register(&mut self, name: &str, lhs: &str, rhs: &str) -> crate::id::RuleId {
let id = self
.map
.try_insert(
name.to_owned(),
RuleInfo {
name: name.to_owned(),
lhs: lhs.to_owned(),
rhs: rhs.to_owned(),
},
)
.expect("registry id space exhausted for its index word");
id_at::<crate::id::RuleId>(id.as_usize())
}
pub fn info(&self, id: crate::id::RuleId) -> &RuleInfo {
self.map.get_val(id.to_index())
}
pub fn name(&self, id: crate::id::RuleId) -> &str {
self.map.key(id.to_index())
}
pub fn len(&self) -> usize {
self.map.log_len().as_usize()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn id_by_name(&self, name: &str) -> Option<crate::id::RuleId> {
self.map
.id_of(&name.to_owned())
.map(|id| id_at::<crate::id::RuleId>(id.as_usize()))
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> RuleRegistryToken {
RuleRegistryToken(
self.map
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
)
}
pub fn restore(&mut self, token: RuleRegistryToken) {
self.map
.try_restore(token.0)
.expect("restore: token minted by this container's own mark");
}
}
#[derive(Clone, Copy, Debug)]
pub struct AxiomRegistryToken(MapToken);
#[derive(Clone, Debug)]
pub struct AxiomInfo<G: Copy> {
pub name: String,
pub lhs: G,
pub rhs: G,
}
pub struct AxiomRegistry<G: Copy + DenseId, const TRACK: bool> {
map: SpMap<String, AxiomInfo<G>, <crate::id::AxiomId as DenseId>::Index, TRACK>,
}
impl<G: Copy + DenseId, const TRACK: bool> Default for AxiomRegistry<G, TRACK> {
fn default() -> Self {
Self::new()
}
}
impl<G: Copy + DenseId, const TRACK: bool> AxiomRegistry<G, TRACK> {
pub fn new() -> Self {
Self { map: SpMap::new() }
}
pub fn register(&mut self, name: &str, lhs: G, rhs: G) -> crate::id::AxiomId {
let id = self
.map
.try_insert(
name.to_owned(),
AxiomInfo {
name: name.to_owned(),
lhs,
rhs,
},
)
.expect("registry id space exhausted for its index word");
id_at::<crate::id::AxiomId>(id.as_usize())
}
pub fn info(&self, id: crate::id::AxiomId) -> &AxiomInfo<G> {
self.map.get_val(id.to_index())
}
pub fn name(&self, id: crate::id::AxiomId) -> &str {
self.map.key(id.to_index())
}
pub fn len(&self) -> usize {
self.map.log_len().as_usize()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn mark(&mut self, shrink: ShrinkPolicy) -> AxiomRegistryToken {
AxiomRegistryToken(
self.map
.try_mark(shrink)
.expect("mark: frame depth is bounded by the saturation driver"),
)
}
pub fn restore(&mut self, token: AxiomRegistryToken) {
self.map
.try_restore(token.0)
.expect("restore: token minted by this container's own mark");
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::id::{OpId, RuleId, SortId};
type SR = SortRegistry<SortId, false>;
type OR = OpRegistry<OpId, SortId, false>;
fn rule_id_bound() -> usize {
let bound = 1usize << 15; assert!(RuleId::try_new(bound - 1).is_some(), "bound is too high");
assert!(RuleId::try_new(bound).is_none(), "bound is too low");
bound
}
#[test]
fn rule_registry_fills_its_id_space_without_aliasing() {
let bound = rule_id_bound();
let mut r: RuleRegistry<false> = RuleRegistry::new();
for i in 0..bound {
assert_eq!(r.register(&format!("r{i}"), "l", "r").to_usize(), i);
}
assert_eq!(r.len(), bound);
assert_eq!(
r.name(id_at::<RuleId>(bound - 1)),
format!("r{}", bound - 1)
);
assert_eq!(r.id_by_name("r0").unwrap().to_usize(), 0);
}
#[test]
#[should_panic(expected = "the id space is exhausted")]
fn rule_registry_past_its_id_space_panics() {
let bound = rule_id_bound();
let mut r: RuleRegistry<false> = RuleRegistry::new();
for i in 0..=bound {
r.register(&format!("r{i}"), "l", "r");
}
}
#[test]
fn builtin_sorts() {
let mut r = SR::new();
r.register_builtins(&["Bool", "Int", "Real"]);
assert_eq!(r.name(SortId::new(0)), "Bool");
assert_eq!(r.name(SortId::new(1)), "Int");
assert_eq!(r.name(SortId::new(2)), "Real");
assert!(r.is_builtin(SortId::new(0)));
assert!(r.is_builtin(SortId::new(2)));
let user = r.intern("BitVec");
assert!(!r.is_builtin(user));
}
#[test]
fn sort_intern_dedup() {
let mut r = SR::new();
let a = r.intern("BitVec");
let b = r.intern("BitVec");
assert_eq!(a, b);
}
#[test]
fn canon_class_all_kinds() {
let mut sorts = SR::new();
let bool_sort = sorts.intern("Bool");
let int_sort = sorts.intern("Int");
let mut ops = OR::new();
let lit = ops.register_lit("BLit", bool_sort);
let not = ops.register("Not", &[bool_sort], bool_sort);
let ite = ops.register("ITE", &[bool_sort, int_sort, int_sort], int_sort);
let eq = ops.register_c("Eq", [int_sort, int_sort], bool_sort);
let sub = ops.register_a("Sub", int_sort, int_sort, AssocDir::Left);
let add = ops.register_mset("Add", int_sort, int_sort);
let and = ops.register_set("And", bool_sort, bool_sort);
assert_eq!(ops.info(lit).canon_class(), ENodeKind::Lit);
assert_eq!(ops.info(not).canon_class(), ENodeKind::Plain1);
assert_eq!(ops.info(ite).canon_class(), ENodeKind::Plain3);
assert_eq!(ops.info(eq).canon_class(), ENodeKind::SPair);
assert_eq!(ops.info(sub).canon_class(), ENodeKind::Seq);
assert_eq!(ops.info(add).canon_class(), ENodeKind::MSet);
assert_eq!(ops.info(and).canon_class(), ENodeKind::Set);
}
#[test]
fn plain0_for_constants() {
let int_sort = SortId::new(1);
let mut ops = OR::new();
let zero = ops.register("Zero", &[], int_sort);
assert_eq!(ops.info(zero).canon_class(), ENodeKind::Plain0);
}
#[test]
fn plainn_for_high_arity() {
let int_sort = SortId::new(1);
let mut ops = OR::new();
let f = ops.register("F4", &[int_sort; 4], int_sort);
assert_eq!(ops.info(f).canon_class(), ENodeKind::PlainN);
}
#[test]
fn id_by_name() {
let int_sort = SortId::new(1);
let mut ops = OR::new();
let add = ops.register_mset("Add", int_sort, int_sort);
assert_eq!(ops.id_by_name("Add"), Some(add));
assert_eq!(ops.id_by_name("nonexistent"), None);
}
#[test]
#[should_panic(expected = "already registered")]
fn duplicate_name_panics() {
let int_sort = SortId::new(1);
let mut ops = OR::new();
ops.register_mset("Add", int_sort, int_sort);
ops.register_mset("Add", int_sort, int_sort);
}
#[test]
#[should_panic(expected = "must use the same argument and return sort")]
fn associative_signature_mismatch_panics() {
let mut ops = OR::new();
ops.register_a("Bad", SortId::new(0), SortId::new(1), AssocDir::Both);
}
#[test]
#[should_panic(expected = "must use the same argument and return sort")]
fn ac_signature_mismatch_panics() {
let mut ops = OR::new();
ops.register_mset("Bad", SortId::new(0), SortId::new(1));
}
#[test]
#[should_panic(expected = "must use the same sort for both arguments")]
fn commutative_argument_sort_mismatch_panics() {
let mut ops = OR::new();
ops.register_c("Bad", [SortId::new(0), SortId::new(1)], SortId::new(2));
}
#[test]
fn mset_ops_lists_only_mset() {
let mut sorts = SR::new();
let bool_sort = sorts.intern("Bool");
let int_sort = sorts.intern("Int");
let mut ops = OR::new();
ops.register("Not", &[bool_sort], bool_sort); ops.register_c("Eq", [int_sort, int_sort], bool_sort); ops.register_a("Sub", int_sort, int_sort, AssocDir::Left); let add = ops.register_mset("Add", int_sort, int_sort); let mul = ops.register_mset("Mul", int_sort, int_sort); ops.register_set("And", bool_sort, bool_sort);
let mut mset_ids: Vec<OpId> = ops.mset_ops().collect();
mset_ids.sort();
assert_eq!(mset_ids, vec![add, mul]);
assert!(ops.is_mset(add));
assert!(ops.is_mset(mul));
assert!(!ops.is_mset(ops.id_by_name("And").unwrap()));
assert!(!ops.is_mset(ops.id_by_name("Sub").unwrap()));
assert!(!ops.is_mset(ops.id_by_name("Not").unwrap()));
}
#[test]
fn mset_ops_empty_when_none_registered() {
let int_sort = SortId::new(1);
let mut ops = OR::new();
ops.register("F", &[int_sort], int_sort);
assert_eq!(ops.mset_ops().count(), 0);
}
#[test]
fn completion_ops_mset_then_set_in_registration_order() {
let mut sorts = SR::new();
let bool_sort = sorts.intern("Bool");
let int_sort = sorts.intern("Int");
let mut ops = OR::new();
let add = ops.register_mset("Add", int_sort, int_sort);
let and = ops.register_set("And", bool_sort, bool_sort);
let mul = ops.register_mset("Mul", int_sort, int_sort);
let or = ops.register_set("Or", bool_sort, bool_sort);
ops.register("Not", &[bool_sort], bool_sort);
assert_eq!(ops.completion_ops(), vec![add, mul, and, or]);
assert_eq!(ops.completion_op_count(), 4);
assert_eq!(ops.set_op_count(), 2);
assert_eq!(ops.mset_op_count(), 2);
assert_eq!(ops.completion_column(add), Some(0));
assert_eq!(ops.completion_column(mul), Some(1));
assert_eq!(ops.completion_column(and), Some(2));
assert_eq!(ops.completion_column(or), Some(3));
assert_eq!(ops.completion_column(ops.id_by_name("Not").unwrap()), None);
assert!(ops.is_set(and) && ops.is_set(or));
assert!(!ops.is_set(add));
}
#[test]
fn completion_column_does_not_rebuild_the_column_array() {
let mut sorts = SR::new();
let bool_sort = sorts.intern("Bool");
let int_sort = sorts.intern("Int");
let mut ops = OR::new();
let add = ops.register_mset("Add", int_sort, int_sort);
let and = ops.register_set("And", bool_sort, bool_sort);
let not = ops.register("Not", &[bool_sort], bool_sort);
let scans = || COMPLETION_OPS_SCANS.with(|n| n.get());
let before = scans();
for _ in 0..1000 {
assert_eq!(ops.completion_column(add), Some(0));
assert_eq!(ops.completion_column(and), Some(1));
assert_eq!(ops.completion_column(not), None);
assert_eq!(ops.completion_op_count(), 2);
}
assert_eq!(
scans(),
before,
"completion_column rebuilt the column array"
);
let _ = ops.completion_ops();
assert_eq!(scans(), before + 1);
}
#[test]
fn completion_columns_restore_with_the_op_log() {
type TrackedOR = OpRegistry<OpId, SortId, true>;
let mut sorts = SR::new();
let bool_sort = sorts.intern("Bool");
let int_sort = sorts.intern("Int");
let mut ops = TrackedOR::new();
let add = ops.register_mset("Add", int_sort, int_sort);
let and = ops.register_set("And", bool_sort, bool_sort);
assert_eq!(
(ops.completion_column(add), ops.completion_column(and)),
(Some(0), Some(1))
);
let token = ops.mark(ShrinkPolicy::Never);
let mul = ops.register_mset("Mul", int_sort, int_sort);
let or = ops.register_set("Or", bool_sort, bool_sort);
assert_eq!(ops.completion_ops(), vec![add, mul, and, or]);
assert_eq!(ops.completion_column(mul), Some(1));
assert_eq!(ops.completion_column(and), Some(2));
assert_eq!(ops.completion_column(or), Some(3));
assert_eq!(ops.completion_op_count(), 4);
ops.restore(token);
assert_eq!(ops.completion_ops(), vec![add, and]);
assert_eq!(ops.completion_column(add), Some(0));
assert_eq!(ops.completion_column(and), Some(1));
assert_eq!(ops.mset_op_count(), 1);
assert_eq!(ops.set_op_count(), 1);
assert_eq!(ops.completion_op_count(), 2);
assert_eq!(ops.completion_column(mul), None);
assert_eq!(ops.completion_column(or), None);
let mul2 = ops.register_mset("Mul", int_sort, int_sort);
assert_eq!(mul2, mul);
assert_eq!(ops.completion_column(mul2), Some(1));
assert_eq!(ops.completion_column(and), Some(2));
}
#[test]
fn completion_ops_empty_when_none() {
let int_sort = SortId::new(1);
let mut ops = OR::new();
ops.register("F", &[int_sort], int_sort);
assert_eq!(ops.completion_ops(), Vec::<OpId>::new());
assert_eq!(ops.completion_op_count(), 0);
}
}