1use crate::containers::AppendOnlyVec;
6use crate::containers::DenseId;
7use crate::containers::IndexLike;
8use crate::containers::MapToken;
9use crate::containers::ShrinkPolicy;
10use crate::containers::SpMap;
11use crate::containers::VecToken;
12use crate::id::{ENodeKind, id_at};
13
14#[derive(Clone, Copy, Debug)]
16pub struct SortRegistryToken(MapToken);
17
18#[derive(Clone, Copy, Debug)]
21pub struct OpRegistryToken {
22 map: MapToken,
23 completion: VecToken,
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub enum AssocDir {
34 Left,
35 Right,
36 Both,
37}
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum Clamp {
45 None,
47 Idempotent,
50 Nilpotent { order: u8 },
70}
71
72#[derive(Clone, Debug)]
76pub enum UnitRef {
77 Lit { token: String },
79 Ctor { term: crate::ast::Term },
81}
82
83#[derive(Clone, Debug)]
91pub enum OpKind<S: DenseId> {
92 Normal {
93 arg_sorts: Vec<S>,
94 },
95 Commutative {
96 arg_sorts: [S; 2],
97 },
98 A {
99 arg_sort: S,
100 dir: AssocDir,
101 },
102 MSet {
107 arg_sort: S,
108 clamp: Clamp,
109 identity: Option<UnitRef>,
110 cancellative: bool,
111 },
112 Set {
116 arg_sort: S,
117 clamp: Clamp,
118 identity: Option<UnitRef>,
119 cancellative: bool,
120 },
121 Lit,
122}
123
124#[derive(Clone, Copy, Debug, PartialEq, Eq)]
134pub struct OpMeta {
135 pub is_constructor: bool,
139 pub cost: u32,
142 pub unextractable: bool,
144}
145
146impl Default for OpMeta {
147 fn default() -> Self {
148 Self {
149 is_constructor: false,
150 cost: 1,
151 unextractable: false,
152 }
153 }
154}
155
156#[derive(Clone, Debug)]
158pub struct OpInfo<S: DenseId> {
159 pub name: String,
160 pub return_sort: S,
161 pub kind: OpKind<S>,
162 pub is_constructor: bool,
163 pub cost: u32,
165 pub unextractable: bool,
167}
168
169impl<S: DenseId> OpInfo<S> {
170 pub fn canon_class(&self) -> ENodeKind {
171 match &self.kind {
172 OpKind::Normal { arg_sorts } => match arg_sorts.len() {
173 0 => ENodeKind::Plain0,
174 1 => ENodeKind::Plain1,
175 2 => ENodeKind::Plain2,
176 3 => ENodeKind::Plain3,
177 _ => ENodeKind::PlainN,
178 },
179 OpKind::Commutative { .. } => ENodeKind::SPair,
180 OpKind::A { .. } => ENodeKind::Seq,
181 OpKind::MSet { .. } => ENodeKind::MSet,
182 OpKind::Set { .. } => ENodeKind::Set,
183 OpKind::Lit => ENodeKind::Lit,
184 }
185 }
186}
187
188#[derive(Debug)]
190pub struct SortRegistry<S: DenseId, const TRACK: bool> {
191 map: SpMap<String, (), S::Index, TRACK>,
193 builtin_count: usize,
194 concrete_count: usize,
195 _phantom: core::marker::PhantomData<S>,
196}
197
198impl<S: DenseId, const TRACK: bool> Default for SortRegistry<S, TRACK> {
199 fn default() -> Self {
200 Self::new()
201 }
202}
203
204impl<S: DenseId, const TRACK: bool> SortRegistry<S, TRACK> {
205 pub fn new() -> Self {
206 Self {
207 map: SpMap::new(),
208 builtin_count: 0,
209 concrete_count: 0,
210 _phantom: core::marker::PhantomData,
211 }
212 }
213
214 pub fn register_builtins(&mut self, sort_names: &[&str]) {
215 assert!(
216 self.map.is_empty(),
217 "register_builtins must be called on empty registry"
218 );
219 for name in sort_names {
220 self.map
221 .try_insert(name.to_string(), ())
222 .expect("registry id space exhausted for its index word");
223 }
224 self.builtin_count = self.map.len();
225 self.concrete_count = self.map.len();
226 }
227
228 pub fn intern(&mut self, name: &str) -> S {
229 if let Some(id) = self.map.id_of(&name.to_owned()) {
230 return id_at::<S>(id.as_usize());
233 }
234 let id = self
235 .map
236 .try_insert(name.to_owned(), ())
237 .expect("registry id space exhausted for its index word");
238 id_at::<S>(id.as_usize())
239 }
240
241 pub fn name(&self, id: S) -> &str {
242 self.map.key(id.to_index())
243 }
244
245 pub fn is_builtin(&self, id: S) -> bool {
246 id.to_usize() < self.builtin_count
247 }
248
249 pub fn is_concrete(&self, id: S) -> bool {
250 id.to_usize() < self.concrete_count
251 }
252
253 pub fn concrete_count(&self) -> usize {
254 self.concrete_count
255 }
256
257 pub fn is_empty(&self) -> bool {
258 self.map.is_empty()
259 }
260
261 pub fn len(&self) -> usize {
262 self.map.log_len().as_usize()
263 }
264
265 pub fn id_by_name(&self, name: &str) -> Option<S> {
266 self.map
267 .id_of(&name.to_owned())
268 .map(|id| id_at::<S>(id.as_usize()))
269 }
270
271 pub fn mark(&mut self, shrink: ShrinkPolicy) -> SortRegistryToken {
272 SortRegistryToken(
273 self.map
274 .try_mark(shrink)
275 .expect("mark: frame depth is bounded by the saturation driver"),
276 )
277 }
278
279 pub fn restore(&mut self, token: SortRegistryToken) {
280 self.map
281 .try_restore(token.0)
282 .expect("restore: token minted by this container's own mark");
283 }
284}
285
286#[derive(Clone, Copy, Debug, PartialEq, Eq)]
289enum CompletionKind {
290 NotCompletion,
291 MSet,
292 Set,
293}
294
295#[derive(Clone, Copy, Debug)]
304struct CompletionSlot {
305 kind: CompletionKind,
306 msets: usize,
308 sets: usize,
310}
311
312#[cfg(test)]
313thread_local! {
314 static COMPLETION_OPS_SCANS: core::cell::Cell<usize> = const { core::cell::Cell::new(0) };
319}
320
321#[derive(Debug)]
323pub struct OpRegistry<O: DenseId, S: DenseId, const TRACK: bool> {
324 map: SpMap<String, OpInfo<S>, O::Index, TRACK>,
326 completion: AppendOnlyVec<CompletionSlot, O::Index, TRACK>,
335 builtin_count: usize,
336 concrete_sort_count: usize,
337 _phantom: core::marker::PhantomData<O>,
338}
339
340impl<O: crate::DenseId, S: DenseId, const TRACK: bool> Default for OpRegistry<O, S, TRACK> {
341 fn default() -> Self {
342 Self::new()
343 }
344}
345
346impl<O: crate::DenseId, S: DenseId, const TRACK: bool> OpRegistry<O, S, TRACK> {
347 pub fn new() -> Self {
348 Self {
349 map: SpMap::new(),
350 completion: AppendOnlyVec::new(),
351 builtin_count: 0,
352 concrete_sort_count: 0,
353 _phantom: core::marker::PhantomData,
354 }
355 }
356
357 pub fn register_builtins<
361 L: crate::literal::LitVal,
362 M: crate::lit_model::LitModel<Value = L>,
363 >(
364 &mut self,
365 model: &M,
366 sorts: &SortRegistry<S, TRACK>,
367 ) {
368 assert!(
369 self.map.is_empty(),
370 "register_builtins must be called on empty registry"
371 );
372 for op_desc in model.ops() {
373 let arg_sorts: Vec<S> = op_desc
374 .arg_sorts
375 .iter()
376 .map(|name| {
377 sorts.id_by_name(name).unwrap_or_else(|| {
378 panic!("unknown sort '{name}' in LitOpDesc '{}'", op_desc.name)
379 })
380 })
381 .collect();
382 let ret_sort = sorts.id_by_name(op_desc.ret_sort).unwrap_or_else(|| {
383 panic!(
384 "unknown sort '{}' in LitOpDesc '{}'",
385 op_desc.ret_sort, op_desc.name
386 )
387 });
388 self.insert(
389 op_desc.name,
390 ret_sort,
391 OpKind::Normal { arg_sorts },
392 OpMeta::default(),
393 );
394 }
395 for sort_desc in model.sorts() {
397 let sort_id = sorts.id_by_name(sort_desc.name).unwrap();
398 let lit_name = format!("@{}", sort_desc.name);
399 self.insert(&lit_name, sort_id, OpKind::Lit, OpMeta::default());
400 }
401 self.builtin_count = self.map.log_len().as_usize();
402 self.concrete_sort_count = sorts.concrete_count();
403 }
404
405 pub fn is_builtin(&self, id: O) -> bool {
407 id.to_usize() < self.builtin_count
408 }
409
410 pub fn is_prim_op(&self, id: O) -> bool {
412 self.is_builtin(id) && !matches!(self.map.get_val(id.to_index()).kind, OpKind::Lit)
413 }
414
415 pub fn register(&mut self, name: &str, arg_sorts: &[S], return_sort: S) -> O {
416 self.insert(
417 name,
418 return_sort,
419 OpKind::Normal {
420 arg_sorts: arg_sorts.to_vec(),
421 },
422 OpMeta::default(),
423 )
424 }
425
426 pub fn register_c(&mut self, name: &str, arg_sorts: [S; 2], return_sort: S) -> O {
427 self.insert(
428 name,
429 return_sort,
430 OpKind::Commutative { arg_sorts },
431 OpMeta::default(),
432 )
433 }
434
435 pub fn register_a(&mut self, name: &str, arg_sort: S, return_sort: S, dir: AssocDir) -> O {
436 self.insert(
437 name,
438 return_sort,
439 OpKind::A { arg_sort, dir },
440 OpMeta::default(),
441 )
442 }
443
444 pub fn register_mset(&mut self, name: &str, arg_sort: S, return_sort: S) -> O {
447 self.insert(
448 name,
449 return_sort,
450 OpKind::MSet {
451 arg_sort,
452 clamp: Clamp::None,
453 identity: None,
454 cancellative: false,
455 },
456 OpMeta::default(),
457 )
458 }
459
460 pub fn register_set(&mut self, name: &str, arg_sort: S, return_sort: S) -> O {
463 self.insert(
464 name,
465 return_sort,
466 OpKind::Set {
467 arg_sort,
468 clamp: Clamp::Idempotent,
469 identity: None,
470 cancellative: false,
471 },
472 OpMeta::default(),
473 )
474 }
475
476 pub fn register_lit(&mut self, name: &str, return_sort: S) -> O {
477 self.insert(name, return_sort, OpKind::Lit, OpMeta::default())
478 }
479
480 pub fn register_kind(&mut self, name: &str, return_sort: S, kind: OpKind<S>) -> O {
485 self.insert(name, return_sort, kind, OpMeta::default())
486 }
487
488 pub fn register_kind_meta(
494 &mut self,
495 name: &str,
496 return_sort: S,
497 kind: OpKind<S>,
498 meta: OpMeta,
499 ) -> O {
500 self.insert(name, return_sort, kind, meta)
501 }
502
503 pub fn info(&self, id: O) -> &OpInfo<S> {
504 self.map.get_val(id.to_index())
505 }
506
507 pub fn names(&self) -> impl Iterator<Item = &str> + '_ {
511 self.map.iter().map(|(_, i)| i.name.as_str())
512 }
513
514 pub fn meta_table(&self) -> Vec<OpMeta> {
519 self.map
520 .iter()
521 .map(|(_, i)| OpMeta {
522 is_constructor: i.is_constructor,
523 cost: i.cost,
524 unextractable: i.unextractable,
525 })
526 .collect()
527 }
528
529 pub fn is_mset(&self, id: O) -> bool {
532 matches!(self.map.get_val(id.to_index()).kind, OpKind::MSet { .. })
533 }
534
535 pub fn mset_ops(&self) -> impl Iterator<Item = O> + '_ {
539 self.ops_of_kind(CompletionKind::MSet)
540 }
541
542 fn ops_of_kind(&self, kind: CompletionKind) -> impl Iterator<Item = O> + '_ {
544 self.completion
545 .as_slice()
546 .iter()
547 .enumerate()
548 .filter(move |(_, slot)| slot.kind == kind)
549 .map(|(i, _)| id_at::<O>(i))
550 }
551
552 fn completion_counts(&self) -> (usize, usize) {
556 match self.completion.as_slice().last() {
557 Some(slot) => (slot.msets, slot.sets),
558 None => (0, 0),
559 }
560 }
561
562 pub fn mset_op_count(&self) -> usize {
565 self.completion_counts().0
566 }
567
568 pub fn is_set(&self, id: O) -> bool {
570 matches!(self.map.get_val(id.to_index()).kind, OpKind::Set { .. })
571 }
572
573 pub fn set_ops(&self) -> impl Iterator<Item = O> + '_ {
576 self.ops_of_kind(CompletionKind::Set)
577 }
578
579 pub fn set_op_count(&self) -> usize {
581 self.completion_counts().1
582 }
583
584 pub fn completion_ops(&self) -> Vec<O> {
595 #[cfg(test)]
596 COMPLETION_OPS_SCANS.with(|n| n.set(n.get() + 1));
597 let mut v: Vec<O> = self.mset_ops().collect();
598 v.extend(self.set_ops());
599 v
600 }
601
602 pub fn completion_op_count(&self) -> usize {
605 let (msets, sets) = self.completion_counts();
606 msets + sets
607 }
608
609 pub fn completion_column(&self, id: O) -> Option<usize> {
617 let slot = self.completion.as_slice().get(id.to_usize())?;
618 match slot.kind {
619 CompletionKind::NotCompletion => None,
620 CompletionKind::MSet => Some(slot.msets - 1),
623 CompletionKind::Set => Some(self.completion_counts().0 + slot.sets - 1),
624 }
625 }
626
627 pub fn id_by_name(&self, name: &str) -> Option<O> {
628 self.map
629 .id_of(&name.to_owned())
630 .map(|id| id_at::<O>(id.as_usize()))
631 }
632
633 pub fn lit_op_for_sort(&self, sort: S) -> Option<O> {
634 self.map
635 .iter()
636 .enumerate()
637 .find(|(_, (_, info))| matches!(info.kind, OpKind::Lit) && info.return_sort == sort)
638 .map(|(i, _)| id_at::<O>(i))
639 }
640
641 pub fn find_lift_op(&self, from: S, to: S) -> Option<O> {
643 self.map
644 .iter()
645 .enumerate()
646 .find(|(_, (_, info))| {
647 info.return_sort == to
648 && matches!(&info.kind, OpKind::Normal { arg_sorts } if arg_sorts.len() == 1 && arg_sorts[0] == from)
649 })
650 .map(|(i, _)| id_at::<O>(i))
651 }
652
653 fn insert(&mut self, name: &str, return_sort: S, kind: OpKind<S>, meta: OpMeta) -> O {
654 assert!(
655 !self.map.contains_key(&name.to_owned()),
656 "operator '{name}' already registered"
657 );
658 if self.builtin_count > 0 && return_sort.to_usize() < self.concrete_sort_count {
659 panic!(
660 "operator '{name}' cannot return concrete sort (index {})",
661 return_sort.to_usize()
662 );
663 }
664 let completion_kind = match kind {
665 OpKind::MSet { .. } => CompletionKind::MSet,
666 OpKind::Set { .. } => CompletionKind::Set,
667 _ => CompletionKind::NotCompletion,
668 };
669 let id = self
670 .map
671 .try_insert(
672 name.to_owned(),
673 OpInfo {
674 name: name.to_owned(),
675 return_sort,
676 kind,
677 is_constructor: meta.is_constructor,
678 cost: meta.cost,
679 unextractable: meta.unextractable,
680 },
681 )
682 .expect("registry id space exhausted for its index word");
683 let (msets, sets) = self.completion_counts();
684 let slot = CompletionSlot {
685 kind: completion_kind,
686 msets: msets + usize::from(completion_kind == CompletionKind::MSet),
687 sets: sets + usize::from(completion_kind == CompletionKind::Set),
688 };
689 let slot_id = self
690 .completion
691 .try_push(slot)
692 .expect("completion table shares the op log's index word");
693 debug_assert_eq!(
694 slot_id.as_usize(),
695 id.as_usize(),
696 "one completion slot per op, pushed in op-id order"
697 );
698 id_at::<O>(id.as_usize())
699 }
700
701 pub fn mark(&mut self, shrink: ShrinkPolicy) -> OpRegistryToken {
702 OpRegistryToken {
703 map: self
704 .map
705 .try_mark(shrink)
706 .expect("mark: frame depth is bounded by the saturation driver"),
707 completion: self
708 .completion
709 .try_mark(shrink)
710 .expect("mark: frame depth is bounded by the saturation driver"),
711 }
712 }
713
714 pub fn restore(&mut self, token: OpRegistryToken) {
715 self.map
716 .try_restore(token.map)
717 .expect("restore: token minted by this container's own mark");
718 self.completion
719 .try_restore(token.completion)
720 .expect("restore: token minted by this container's own mark");
721 debug_assert_eq!(
722 self.completion.len().as_usize(),
723 self.map.log_len().as_usize(),
724 "the completion table is truncated with the op log"
725 );
726 }
727}
728
729#[derive(Clone, Copy, Debug)]
735pub struct RuleRegistryToken(MapToken);
736
737#[derive(Clone, Debug)]
739pub struct RuleInfo {
740 pub name: String,
741 pub lhs: String,
742 pub rhs: String,
743}
744
745pub struct RuleRegistry<const TRACK: bool> {
747 map: SpMap<String, RuleInfo, <crate::id::RuleId as DenseId>::Index, TRACK>,
750}
751
752impl<const TRACK: bool> Default for RuleRegistry<TRACK> {
753 fn default() -> Self {
754 Self::new()
755 }
756}
757
758impl<const TRACK: bool> RuleRegistry<TRACK> {
759 pub fn new() -> Self {
760 Self { map: SpMap::new() }
761 }
762
763 pub fn register(&mut self, name: &str, lhs: &str, rhs: &str) -> crate::id::RuleId {
764 let id = self
765 .map
766 .try_insert(
767 name.to_owned(),
768 RuleInfo {
769 name: name.to_owned(),
770 lhs: lhs.to_owned(),
771 rhs: rhs.to_owned(),
772 },
773 )
774 .expect("registry id space exhausted for its index word");
775 id_at::<crate::id::RuleId>(id.as_usize())
776 }
777
778 pub fn info(&self, id: crate::id::RuleId) -> &RuleInfo {
779 self.map.get_val(id.to_index())
780 }
781
782 pub fn name(&self, id: crate::id::RuleId) -> &str {
783 self.map.key(id.to_index())
784 }
785
786 pub fn len(&self) -> usize {
787 self.map.log_len().as_usize()
788 }
789
790 pub fn is_empty(&self) -> bool {
791 self.map.is_empty()
792 }
793
794 pub fn id_by_name(&self, name: &str) -> Option<crate::id::RuleId> {
795 self.map
796 .id_of(&name.to_owned())
797 .map(|id| id_at::<crate::id::RuleId>(id.as_usize()))
798 }
799
800 pub fn mark(&mut self, shrink: ShrinkPolicy) -> RuleRegistryToken {
801 RuleRegistryToken(
802 self.map
803 .try_mark(shrink)
804 .expect("mark: frame depth is bounded by the saturation driver"),
805 )
806 }
807
808 pub fn restore(&mut self, token: RuleRegistryToken) {
809 self.map
810 .try_restore(token.0)
811 .expect("restore: token minted by this container's own mark");
812 }
813}
814
815#[derive(Clone, Copy, Debug)]
821pub struct AxiomRegistryToken(MapToken);
822
823#[derive(Clone, Debug)]
825pub struct AxiomInfo<G: Copy> {
826 pub name: String,
827 pub lhs: G,
828 pub rhs: G,
829}
830
831pub struct AxiomRegistry<G: Copy + DenseId, const TRACK: bool> {
833 map: SpMap<String, AxiomInfo<G>, <crate::id::AxiomId as DenseId>::Index, TRACK>,
837}
838
839impl<G: Copy + DenseId, const TRACK: bool> Default for AxiomRegistry<G, TRACK> {
840 fn default() -> Self {
841 Self::new()
842 }
843}
844
845impl<G: Copy + DenseId, const TRACK: bool> AxiomRegistry<G, TRACK> {
846 pub fn new() -> Self {
847 Self { map: SpMap::new() }
848 }
849
850 pub fn register(&mut self, name: &str, lhs: G, rhs: G) -> crate::id::AxiomId {
851 let id = self
852 .map
853 .try_insert(
854 name.to_owned(),
855 AxiomInfo {
856 name: name.to_owned(),
857 lhs,
858 rhs,
859 },
860 )
861 .expect("registry id space exhausted for its index word");
862 id_at::<crate::id::AxiomId>(id.as_usize())
863 }
864
865 pub fn info(&self, id: crate::id::AxiomId) -> &AxiomInfo<G> {
866 self.map.get_val(id.to_index())
867 }
868
869 pub fn name(&self, id: crate::id::AxiomId) -> &str {
870 self.map.key(id.to_index())
871 }
872
873 pub fn len(&self) -> usize {
874 self.map.log_len().as_usize()
875 }
876
877 pub fn is_empty(&self) -> bool {
878 self.map.is_empty()
879 }
880
881 pub fn mark(&mut self, shrink: ShrinkPolicy) -> AxiomRegistryToken {
882 AxiomRegistryToken(
883 self.map
884 .try_mark(shrink)
885 .expect("mark: frame depth is bounded by the saturation driver"),
886 )
887 }
888
889 pub fn restore(&mut self, token: AxiomRegistryToken) {
890 self.map
891 .try_restore(token.0)
892 .expect("restore: token minted by this container's own mark");
893 }
894}
895
896#[cfg(test)]
897mod tests {
898 use super::*;
899 use crate::id::{OpId, RuleId, SortId};
900
901 type SR = SortRegistry<SortId, false>;
902 type OR = OpRegistry<OpId, SortId, false>;
903
904 fn rule_id_bound() -> usize {
914 let bound = 1usize << 15; assert!(RuleId::try_new(bound - 1).is_some(), "bound is too high");
916 assert!(RuleId::try_new(bound).is_none(), "bound is too low");
917 bound
918 }
919
920 #[test]
922 fn rule_registry_fills_its_id_space_without_aliasing() {
923 let bound = rule_id_bound();
924 let mut r: RuleRegistry<false> = RuleRegistry::new();
925 for i in 0..bound {
926 assert_eq!(r.register(&format!("r{i}"), "l", "r").to_usize(), i);
927 }
928 assert_eq!(r.len(), bound);
929 assert_eq!(
931 r.name(id_at::<RuleId>(bound - 1)),
932 format!("r{}", bound - 1)
933 );
934 assert_eq!(r.id_by_name("r0").unwrap().to_usize(), 0);
935 }
936
937 #[test]
939 #[should_panic(expected = "the id space is exhausted")]
940 fn rule_registry_past_its_id_space_panics() {
941 let bound = rule_id_bound();
942 let mut r: RuleRegistry<false> = RuleRegistry::new();
943 for i in 0..=bound {
944 r.register(&format!("r{i}"), "l", "r");
945 }
946 }
947
948 #[test]
949 fn builtin_sorts() {
950 let mut r = SR::new();
951 r.register_builtins(&["Bool", "Int", "Real"]);
952 assert_eq!(r.name(SortId::new(0)), "Bool");
953 assert_eq!(r.name(SortId::new(1)), "Int");
954 assert_eq!(r.name(SortId::new(2)), "Real");
955 assert!(r.is_builtin(SortId::new(0)));
956 assert!(r.is_builtin(SortId::new(2)));
957 let user = r.intern("BitVec");
958 assert!(!r.is_builtin(user));
959 }
960
961 #[test]
962 fn sort_intern_dedup() {
963 let mut r = SR::new();
964 let a = r.intern("BitVec");
965 let b = r.intern("BitVec");
966 assert_eq!(a, b);
967 }
968
969 #[test]
970 fn canon_class_all_kinds() {
971 let mut sorts = SR::new();
972 let bool_sort = sorts.intern("Bool");
973 let int_sort = sorts.intern("Int");
974
975 let mut ops = OR::new();
976 let lit = ops.register_lit("BLit", bool_sort);
977 let not = ops.register("Not", &[bool_sort], bool_sort);
978 let ite = ops.register("ITE", &[bool_sort, int_sort, int_sort], int_sort);
979 let eq = ops.register_c("Eq", [int_sort, int_sort], bool_sort);
980 let sub = ops.register_a("Sub", int_sort, int_sort, AssocDir::Left);
981 let add = ops.register_mset("Add", int_sort, int_sort);
982 let and = ops.register_set("And", bool_sort, bool_sort);
983
984 assert_eq!(ops.info(lit).canon_class(), ENodeKind::Lit);
985 assert_eq!(ops.info(not).canon_class(), ENodeKind::Plain1);
986 assert_eq!(ops.info(ite).canon_class(), ENodeKind::Plain3);
987 assert_eq!(ops.info(eq).canon_class(), ENodeKind::SPair);
988 assert_eq!(ops.info(sub).canon_class(), ENodeKind::Seq);
989 assert_eq!(ops.info(add).canon_class(), ENodeKind::MSet);
990 assert_eq!(ops.info(and).canon_class(), ENodeKind::Set);
991 }
992
993 #[test]
994 fn plain0_for_constants() {
995 let int_sort = SortId::new(1);
996 let mut ops = OR::new();
997 let zero = ops.register("Zero", &[], int_sort);
998 assert_eq!(ops.info(zero).canon_class(), ENodeKind::Plain0);
999 }
1000
1001 #[test]
1002 fn plainn_for_high_arity() {
1003 let int_sort = SortId::new(1);
1004 let mut ops = OR::new();
1005 let f = ops.register("F4", &[int_sort; 4], int_sort);
1006 assert_eq!(ops.info(f).canon_class(), ENodeKind::PlainN);
1007 }
1008
1009 #[test]
1010 fn id_by_name() {
1011 let int_sort = SortId::new(1);
1012 let mut ops = OR::new();
1013 let add = ops.register_mset("Add", int_sort, int_sort);
1014 assert_eq!(ops.id_by_name("Add"), Some(add));
1015 assert_eq!(ops.id_by_name("nonexistent"), None);
1016 }
1017
1018 #[test]
1019 #[should_panic(expected = "already registered")]
1020 fn duplicate_name_panics() {
1021 let int_sort = SortId::new(1);
1022 let mut ops = OR::new();
1023 ops.register_mset("Add", int_sort, int_sort);
1024 ops.register_mset("Add", int_sort, int_sort);
1025 }
1026
1027 #[test]
1028 fn mset_ops_lists_only_mset() {
1029 let mut sorts = SR::new();
1030 let bool_sort = sorts.intern("Bool");
1031 let int_sort = sorts.intern("Int");
1032
1033 let mut ops = OR::new();
1034 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();
1043 mset_ids.sort();
1044 assert_eq!(mset_ids, vec![add, mul]);
1045
1046 assert!(ops.is_mset(add));
1047 assert!(ops.is_mset(mul));
1048 assert!(!ops.is_mset(ops.id_by_name("And").unwrap()));
1050 assert!(!ops.is_mset(ops.id_by_name("Sub").unwrap()));
1051 assert!(!ops.is_mset(ops.id_by_name("Not").unwrap()));
1052 }
1053
1054 #[test]
1055 fn mset_ops_empty_when_none_registered() {
1056 let int_sort = SortId::new(1);
1057 let mut ops = OR::new();
1058 ops.register("F", &[int_sort], int_sort);
1059 assert_eq!(ops.mset_ops().count(), 0);
1060 }
1061
1062 #[test]
1063 fn completion_ops_mset_then_set_in_registration_order() {
1064 let mut sorts = SR::new();
1065 let bool_sort = sorts.intern("Bool");
1066 let int_sort = sorts.intern("Int");
1067
1068 let mut ops = OR::new();
1069 let add = ops.register_mset("Add", int_sort, int_sort);
1070 let and = ops.register_set("And", bool_sort, bool_sort);
1071 let mul = ops.register_mset("Mul", int_sort, int_sort);
1072 let or = ops.register_set("Or", bool_sort, bool_sort);
1073 ops.register("Not", &[bool_sort], bool_sort); assert_eq!(ops.completion_ops(), vec![add, mul, and, or]);
1077 assert_eq!(ops.completion_op_count(), 4);
1078 assert_eq!(ops.set_op_count(), 2);
1079 assert_eq!(ops.mset_op_count(), 2);
1080
1081 assert_eq!(ops.completion_column(add), Some(0));
1083 assert_eq!(ops.completion_column(mul), Some(1));
1084 assert_eq!(ops.completion_column(and), Some(2));
1085 assert_eq!(ops.completion_column(or), Some(3));
1086 assert_eq!(ops.completion_column(ops.id_by_name("Not").unwrap()), None);
1087
1088 assert!(ops.is_set(and) && ops.is_set(or));
1090 assert!(!ops.is_set(add));
1091 }
1092
1093 #[test]
1097 fn completion_column_does_not_rebuild_the_column_array() {
1098 let mut sorts = SR::new();
1099 let bool_sort = sorts.intern("Bool");
1100 let int_sort = sorts.intern("Int");
1101
1102 let mut ops = OR::new();
1103 let add = ops.register_mset("Add", int_sort, int_sort);
1104 let and = ops.register_set("And", bool_sort, bool_sort);
1105 let not = ops.register("Not", &[bool_sort], bool_sort);
1106
1107 let scans = || COMPLETION_OPS_SCANS.with(|n| n.get());
1108 let before = scans();
1109 for _ in 0..1000 {
1110 assert_eq!(ops.completion_column(add), Some(0));
1111 assert_eq!(ops.completion_column(and), Some(1));
1112 assert_eq!(ops.completion_column(not), None);
1113 assert_eq!(ops.completion_op_count(), 2);
1114 }
1115 assert_eq!(
1116 scans(),
1117 before,
1118 "completion_column rebuilt the column array"
1119 );
1120
1121 let _ = ops.completion_ops();
1123 assert_eq!(scans(), before + 1);
1124 }
1125
1126 #[test]
1131 fn completion_columns_restore_with_the_op_log() {
1132 type TrackedOR = OpRegistry<OpId, SortId, true>;
1133 let mut sorts = SR::new();
1134 let bool_sort = sorts.intern("Bool");
1135 let int_sort = sorts.intern("Int");
1136
1137 let mut ops = TrackedOR::new();
1138 let add = ops.register_mset("Add", int_sort, int_sort);
1139 let and = ops.register_set("And", bool_sort, bool_sort);
1140 assert_eq!(
1141 (ops.completion_column(add), ops.completion_column(and)),
1142 (Some(0), Some(1))
1143 );
1144
1145 let token = ops.mark(ShrinkPolicy::Never);
1146 let mul = ops.register_mset("Mul", int_sort, int_sort);
1147 let or = ops.register_set("Or", bool_sort, bool_sort);
1148 assert_eq!(ops.completion_ops(), vec![add, mul, and, or]);
1150 assert_eq!(ops.completion_column(mul), Some(1));
1151 assert_eq!(ops.completion_column(and), Some(2));
1152 assert_eq!(ops.completion_column(or), Some(3));
1153 assert_eq!(ops.completion_op_count(), 4);
1154
1155 ops.restore(token);
1156 assert_eq!(ops.completion_ops(), vec![add, and]);
1157 assert_eq!(ops.completion_column(add), Some(0));
1158 assert_eq!(ops.completion_column(and), Some(1));
1159 assert_eq!(ops.mset_op_count(), 1);
1160 assert_eq!(ops.set_op_count(), 1);
1161 assert_eq!(ops.completion_op_count(), 2);
1162 assert_eq!(ops.completion_column(mul), None);
1164 assert_eq!(ops.completion_column(or), None);
1165
1166 let mul2 = ops.register_mset("Mul", int_sort, int_sort);
1168 assert_eq!(mul2, mul);
1169 assert_eq!(ops.completion_column(mul2), Some(1));
1170 assert_eq!(ops.completion_column(and), Some(2));
1171 }
1172
1173 #[test]
1174 fn completion_ops_empty_when_none() {
1175 let int_sort = SortId::new(1);
1176 let mut ops = OR::new();
1177 ops.register("F", &[int_sort], int_sort);
1178 assert_eq!(ops.completion_ops(), Vec::<OpId>::new());
1179 assert_eq!(ops.completion_op_count(), 0);
1180 }
1181}