From c98e86956426c49a2aa8d8004d2430166dd6ae51 Mon Sep 17 00:00:00 2001 From: notoria Date: Sun, 26 Apr 2020 02:18:45 +0200 Subject: [PATCH] Implemented the Debug trait for all data structures --- Cargo.toml | 2 +- src/prolog/arithmetic.rs | 3 ++ src/prolog/clause_types.rs | 14 +++---- src/prolog/codegen.rs | 2 + src/prolog/debray_allocator.rs | 1 + src/prolog/examples/least_time.pl | 2 +- src/prolog/fixtures.rs | 5 +++ src/prolog/forms.rs | 29 +++++++------- src/prolog/heap_iter.rs | 4 ++ src/prolog/heap_print.rs | 6 ++- src/prolog/instructions.rs | 12 ++++-- src/prolog/iterators.rs | 19 +++++++++- src/prolog/lib/between.pl | 2 +- src/prolog/lib/tabling/batched_worklist.pl | 2 +- src/prolog/machine/attributed_variables.rs | 1 + src/prolog/machine/code_repo.rs | 1 + src/prolog/machine/compile.rs | 3 ++ src/prolog/machine/copier.rs | 3 +- src/prolog/machine/heap.rs | 5 +++ src/prolog/machine/machine_errors.rs | 18 +++++---- src/prolog/machine/machine_indices.rs | 44 +++++++++++++++------- src/prolog/machine/machine_state.rs | 22 ++++++++--- src/prolog/machine/mod.rs | 2 + src/prolog/machine/partial_string.rs | 2 + src/prolog/machine/raw_block.rs | 1 + src/prolog/machine/stack.rs | 8 +++- src/prolog/machine/streams.rs | 29 +++++++++++--- src/prolog/machine/system_calls.rs | 1 + src/prolog/machine/term_expansion.rs | 2 + src/prolog/machine/toplevel.rs | 13 +++++++ src/prolog/read.rs | 3 ++ src/tests/predicates.pl | 2 +- src/tests/setup_call_cleanup.pl | 2 +- 33 files changed, 198 insertions(+), 67 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 462ca356..8cc5497a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ libc = "0.2.62" nix = "0.15.0" num-rug-adapter = { optional = true, version = "0.1.3" } ordered-float = "0.5.0" -prolog_parser = { version = "0.8.53", default-features = false } +prolog_parser = { version = "0.8.54", default-features = false } ref_thread_local = "0.0.0" rug = { version = "1.4.0", optional = true } rustyline = "6.0.0" diff --git a/src/prolog/arithmetic.rs b/src/prolog/arithmetic.rs index 6946f5f4..fb20678e 100644 --- a/src/prolog/arithmetic.rs +++ b/src/prolog/arithmetic.rs @@ -23,6 +23,7 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; use std::rc::Rc; use std::vec::Vec; +#[derive(Debug)] pub struct ArithInstructionIterator<'a> { state_stack: Vec>, } @@ -68,6 +69,7 @@ impl<'a> ArithInstructionIterator<'a> { } } +#[derive(Debug)] pub enum ArithTermRef<'a> { Constant(&'a Constant), Op(ClauseName, usize), // name, arity. @@ -109,6 +111,7 @@ impl<'a> Iterator for ArithInstructionIterator<'a> { } } +#[derive(Debug)] pub struct ArithmeticEvaluator<'a> { bindings: &'a AllocVarDict, interm: Vec, diff --git a/src/prolog/clause_types.rs b/src/prolog/clause_types.rs index 756080ca..9b550c35 100644 --- a/src/prolog/clause_types.rs +++ b/src/prolog/clause_types.rs @@ -8,7 +8,7 @@ use ref_thread_local::RefThreadLocal; use std::collections::BTreeMap; -#[derive(Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum CompareNumberQT { GreaterThan, LessThan, @@ -31,7 +31,7 @@ impl CompareNumberQT { } } -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum CompareTermQT { LessThan, LessThanOrEqual, @@ -50,7 +50,7 @@ impl CompareTermQT { } } -#[derive(Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum ArithmeticTerm { Reg(RegType), Interm(usize), @@ -67,7 +67,7 @@ impl ArithmeticTerm { } } -#[derive(Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum InlinedClauseType { CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm), IsAtom(RegType), @@ -145,7 +145,7 @@ impl InlinedClauseType { } } -#[derive(Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum SystemClauseType { AbolishClause, AbolishModuleClause, @@ -557,7 +557,7 @@ impl SystemClauseType { } } -#[derive(Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum BuiltInClauseType { AcyclicTerm, Arg, @@ -575,7 +575,7 @@ pub enum BuiltInClauseType { Sort, } -#[derive(Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum ClauseType { BuiltIn(BuiltInClauseType), CallN, diff --git a/src/prolog/codegen.rs b/src/prolog/codegen.rs index 34d5a1f2..ffe9490d 100644 --- a/src/prolog/codegen.rs +++ b/src/prolog/codegen.rs @@ -17,12 +17,14 @@ use std::cell::Cell; use std::rc::Rc; use std::vec::Vec; +#[derive(Debug)] pub struct CodeGenerator { marker: TermMarker, pub var_count: IndexMap, usize>, non_counted_bt: bool, } +#[derive(Debug)] pub struct ConjunctInfo<'a> { pub perm_vs: VariableFixtures<'a>, pub num_of_chunks: usize, diff --git a/src/prolog/debray_allocator.rs b/src/prolog/debray_allocator.rs index 28eb4401..64fa1461 100644 --- a/src/prolog/debray_allocator.rs +++ b/src/prolog/debray_allocator.rs @@ -12,6 +12,7 @@ use std::cell::Cell; use std::collections::BTreeSet; use std::rc::Rc; +#[derive(Debug)] pub struct DebrayAllocator { bindings: IndexMap, VarData>, arg_c: usize, diff --git a/src/prolog/examples/least_time.pl b/src/prolog/examples/least_time.pl index 4f040340..100c68d5 100644 --- a/src/prolog/examples/least_time.pl +++ b/src/prolog/examples/least_time.pl @@ -30,7 +30,7 @@ valid_time([H1,H2,M1,M2], T) :- memberd_t(H2, [0,1,2,3,4,5,6,7,8,9], TH2), memberd_t(M1, [0,1,2,3,4,5], TM1), memberd_t(M2, [0,1,2,3,4,5,6,7,8,9], TM2), - ( maplist(=(true), [TH1, TH2, TM1, TM2]) -> + ( maplist(=(true), [TH1, TH2, TM1, TM2]) -> ( H1 =:= 2 -> ( H2 =< 3 -> T = true diff --git a/src/prolog/fixtures.rs b/src/prolog/fixtures.rs index 6400bfec..70a3b9b8 100644 --- a/src/prolog/fixtures.rs +++ b/src/prolog/fixtures.rs @@ -13,6 +13,7 @@ use std::rc::Rc; use std::vec::Vec; // labeled with chunk numbers. +#[derive(Debug)] pub enum VarStatus { Perm(usize), Temp(usize, TempVarData), // Perm(chunk_num) | Temp(chunk_num, _) @@ -22,6 +23,7 @@ pub type OccurrenceSet = BTreeSet<(GenContext, usize)>; // Perm: 0 initially, a stack register once processed. // Temp: labeled with chunk_num and temp offset (unassigned if 0). +#[derive(Debug)] pub enum VarData { Perm(usize), Temp(usize, usize, TempVarData), @@ -36,6 +38,7 @@ impl VarData { } } +#[derive(Debug)] pub struct TempVarData { pub last_term_arity: usize, pub use_set: OccurrenceSet, @@ -79,6 +82,7 @@ impl TempVarData { type VariableFixture<'a> = (VarStatus, Vec<&'a Cell>); +#[derive(Debug)] pub struct VariableFixtures<'a>{ perm_vars: IndexMap, VariableFixture<'a>>, last_chunk_temp_vars: IndexSet> @@ -248,6 +252,7 @@ impl<'a> VariableFixtures<'a> { } } +#[derive(Debug)] pub struct UnsafeVarMarker { pub unsafe_vars: IndexMap, pub safe_vars: IndexSet, diff --git a/src/prolog/forms.rs b/src/prolog/forms.rs index 95af1e8e..9f95a855 100644 --- a/src/prolog/forms.rs +++ b/src/prolog/forms.rs @@ -22,7 +22,7 @@ pub type PredicateKey = (ClauseName, usize); // name, arity. // of vars (we get their adjoining cells this way). pub type JumpStub = Vec; -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum TopLevel { Declaration(Declaration), Fact(Term, usize, usize), // Term, line_num, col_num @@ -42,7 +42,7 @@ impl TopLevel { } } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum Level { Deep, Root, @@ -58,7 +58,7 @@ impl Level { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum QueryTerm { // register, clause type, subterms, use default call policy. Clause(Cell, ClauseType, Vec>, bool), @@ -86,13 +86,13 @@ impl QueryTerm { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct Rule { pub head: (ClauseName, Vec>, QueryTerm), pub clauses: Vec, } -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct Predicate(pub Vec); impl Predicate { @@ -114,7 +114,7 @@ impl Predicate { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum ListingSource { File(ClauseName, PathBuf), // filename, path User, @@ -326,7 +326,7 @@ impl ClauseConsistency for Predicate { pub type CompiledResult = (Predicate, VecDeque); -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum PredicateClause { Fact(Term, usize, usize), // Term, line number, column number. Rule(Rule, usize, usize), // Term, line number, column number. @@ -371,7 +371,7 @@ impl PredicateClause { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum ModuleSource { Library(ClauseName), File(ClauseName), @@ -392,13 +392,13 @@ impl ModuleSource { pub type ScopedPredicateKey = (ClauseName, PredicateKey); // module name, predicate indicator. -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum MultiFileIndicator { LocalScoped(ClauseName, usize), // name, arity ModuleScoped(ScopedPredicateKey), } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum Declaration { Dynamic(ClauseName, usize), // name, arity EndOfFile, @@ -433,7 +433,7 @@ impl Declaration { } } -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct OpDecl(pub usize, pub Specifier, pub ClauseName); impl OpDecl { @@ -558,18 +558,19 @@ pub fn fetch_op_spec( pub type ModuleDir = IndexMap; -#[derive(Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum ModuleExport { OpDecl(OpDecl), PredicateKey(PredicateKey), } -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct ModuleDecl { pub name: ClauseName, pub exports: Vec, } +#[derive(Debug)] pub struct Module { pub atom_tbl: TabledData, pub module_decl: ModuleDecl, @@ -587,7 +588,7 @@ pub struct Module { pub listing_src: ListingSource, } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum Number { Float(OrderedFloat), Integer(Rc), diff --git a/src/prolog/heap_iter.rs b/src/prolog/heap_iter.rs index 1815ee19..c30918fb 100644 --- a/src/prolog/heap_iter.rs +++ b/src/prolog/heap_iter.rs @@ -7,6 +7,7 @@ use std::cmp::Ordering; use std::ops::Deref; use std::vec::Vec; +#[derive(Debug)] pub struct HCPreOrderIterator<'a> { pub machine_st: &'a MachineState, pub state_stack: Vec, @@ -127,6 +128,7 @@ pub trait MutStackHCIterator<'b> where Self: Iterator fn stack(&'b mut self) -> Self::MutStack; } +#[derive(Debug)] pub struct HCPostOrderIterator<'a> { base_iter: HCPreOrderIterator<'a>, parent_stack: Vec<(usize, Addr)>, // number of children, parent node. @@ -229,6 +231,7 @@ impl<'b, 'a: 'b> MutStackHCIterator<'b> for HCPreOrderIterator<'a> { } } +#[derive(Debug)] pub struct HCAcyclicIterator<'a> { iter: HCPreOrderIterator<'a>, seen: IndexSet, @@ -269,6 +272,7 @@ impl<'a> Iterator for HCAcyclicIterator<'a> } } +#[derive(Debug)] pub struct HCZippedAcyclicIterator<'a> { i1: HCPreOrderIterator<'a>, i2: HCPreOrderIterator<'a>, diff --git a/src/prolog/heap_print.rs b/src/prolog/heap_print.rs index a57be201..5e36c35c 100644 --- a/src/prolog/heap_print.rs +++ b/src/prolog/heap_print.rs @@ -18,7 +18,7 @@ use std::ops::{Range, RangeFrom}; use std::rc::Rc; /* contains the location, name, precision and Specifier of the parent op. */ -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum DirectedOp { Left(ClauseName, SharedOpDesc), Right(ClauseName, SharedOpDesc), @@ -162,7 +162,7 @@ fn char_to_string(is_quoted: bool, c: char) -> String { } } -#[derive(Clone)] +#[derive(Debug, Clone)] enum TokenOrRedirect { Atom(ClauseName), BarAsOp, @@ -197,6 +197,7 @@ pub trait HCValueOutputter { fn range_from(&self, range: RangeFrom) -> &str; } +#[derive(Debug)] pub struct PrinterOutputter { contents: String, } @@ -335,6 +336,7 @@ impl MachineState { type ReverseHeapVarDict = IndexMap>; +#[derive(Debug)] pub struct HCPrinter<'a, Outputter> { outputter: Outputter, machine_st: &'a MachineState, diff --git a/src/prolog/instructions.rs b/src/prolog/instructions.rs index 6b88c52f..93199289 100644 --- a/src/prolog/instructions.rs +++ b/src/prolog/instructions.rs @@ -45,6 +45,7 @@ impl ArithmeticTerm { } } +#[derive(Debug)] pub enum ChoiceInstruction { DefaultRetryMeElse(usize), DefaultTrustMe, @@ -75,6 +76,7 @@ impl ChoiceInstruction { } } +#[derive(Debug)] pub enum CutInstruction { Cut(RegType), GetLevel(RegType), @@ -104,6 +106,7 @@ impl CutInstruction { } } +#[derive(Debug)] pub enum IndexedChoiceInstruction { Retry(usize), Trust(usize), @@ -140,6 +143,7 @@ impl IndexedChoiceInstruction { } } +#[derive(Debug)] pub enum Line { Arithmetic(ArithmeticInstruction), Choice(ChoiceInstruction), @@ -175,7 +179,7 @@ impl Line { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum ArithmeticInstruction { Add(ArithmeticTerm, ArithmeticTerm, usize), Sub(ArithmeticTerm, ArithmeticTerm, usize), @@ -374,6 +378,7 @@ impl ArithmeticInstruction { } } +#[derive(Debug)] pub enum ControlInstruction { Allocate(usize), // num_frames. // name, arity, perm_vars after threshold, last call, use default call policy. @@ -419,6 +424,7 @@ impl ControlInstruction { } } +#[derive(Debug)] pub enum IndexingInstruction { SwitchOnTerm(usize, usize, usize, usize), SwitchOnConstant(usize, IndexMap), @@ -459,7 +465,7 @@ impl IndexingInstruction { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum FactInstruction { GetConstant(Level, Constant, RegType), GetList(Level, RegType), @@ -571,7 +577,7 @@ impl FactInstruction { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum QueryInstruction { GetVariable(RegType, usize), PutConstant(Level, Constant, RegType), diff --git a/src/prolog/iterators.rs b/src/prolog/iterators.rs index 83c6fab2..28da88e4 100644 --- a/src/prolog/iterators.rs +++ b/src/prolog/iterators.rs @@ -6,11 +6,12 @@ use crate::prolog::machine::machine_indices::*; use std::cell::Cell; use std::collections::VecDeque; +use std::fmt; use std::iter::*; use std::rc::Rc; use std::vec::Vec; -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum TermRef<'a> { AnonVar(Level), Cons(Level, &'a Cell, &'a Term, &'a Term), @@ -33,6 +34,7 @@ impl<'a> TermRef<'a> { } } +#[derive(Debug)] pub enum TermIterState<'a> { AnonVar(Level), Constant(Level, &'a Cell, &'a Constant), @@ -124,6 +126,7 @@ impl<'a> TermIterState<'a> { } } +#[derive(Debug)] pub struct QueryIterator<'a> { state_stack: Vec>, } @@ -294,6 +297,7 @@ impl<'a> Iterator for QueryIterator<'a> { } } +#[derive(Debug)] pub struct FactIterator<'a> { state_queue: VecDeque>, iterable_root: bool, @@ -402,6 +406,7 @@ pub fn breadth_first_iter(term: &Term, iterable_root: bool) -> FactIterator { FactIterator::new(term, iterable_root) } +#[derive(Debug)] pub enum ChunkedTerm<'a> { HeadClause(ClauseName, &'a Vec>), BodyTerm(&'a QueryTerm), @@ -439,6 +444,18 @@ pub struct ChunkedIterator<'a> { cut_var_in_head: bool, } +impl<'a> fmt::Debug for ChunkedIterator<'a> { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("ChunkedIterator") + .field("chunk_num", &self.chunk_num) + // Hacky solution. + .field("iter", &"Box> + 'a>") + .field("deep_cut_encountered", &self.deep_cut_encountered) + .field("cut_var_in_head", &self.cut_var_in_head) + .finish() + } +} + type ChunkedIteratorItem<'a> = (usize, usize, Vec>); type RuleBodyIteratorItem<'a> = (usize, usize, Vec<&'a QueryTerm>); diff --git a/src/prolog/lib/between.pl b/src/prolog/lib/between.pl index 1f49dfc7..16c73145 100644 --- a/src/prolog/lib/between.pl +++ b/src/prolog/lib/between.pl @@ -9,7 +9,7 @@ between(Lower, Upper, X) :- must_be(integer, Lower), must_be(integer, Upper), can_be(integer, X), - between_(Lower, Upper, X). + between_(Lower, Upper, X). between_(Lower, Upper, Lower) :- Lower =< Upper. diff --git a/src/prolog/lib/tabling/batched_worklist.pl b/src/prolog/lib/tabling/batched_worklist.pl index 444ef6ce..36a67c16 100644 --- a/src/prolog/lib/tabling/batched_worklist.pl +++ b/src/prolog/lib/tabling/batched_worklist.pl @@ -291,7 +291,7 @@ wkl_add_to_existing_answer_cluster(Worklist, Answer) :- wkl_add_to_new_answer_cluster( wkl_worklist(Dll,_Ria,_FlagExecutingWork,_AlreadyInMetaworklist,_TableIdentifier), Answer,AnswerClusterPointer -) :- +) :- dll_append_left(Dll,wkl_answer_cluster(AnswerFlag),AnswerClusterPointer), put_atts(AnswerFlag, wkl_answer_cluster([Answer])). diff --git a/src/prolog/machine/attributed_variables.rs b/src/prolog/machine/attributed_variables.rs index b216a48c..d853a5f8 100644 --- a/src/prolog/machine/attributed_variables.rs +++ b/src/prolog/machine/attributed_variables.rs @@ -8,6 +8,7 @@ pub static PROJECT_ATTRS: &str = include_str!("project_attributes.pl"); pub(super) type Bindings = Vec<(usize, Addr)>; +#[derive(Debug)] pub(super) struct AttrVarInitializer { pub(super) attribute_goals: Vec, pub(super) attr_var_queue: Vec, diff --git a/src/prolog/machine/code_repo.rs b/src/prolog/machine/code_repo.rs index e56a3571..eba8c5c9 100644 --- a/src/prolog/machine/code_repo.rs +++ b/src/prolog/machine/code_repo.rs @@ -12,6 +12,7 @@ use indexmap::IndexSet; use std::collections::VecDeque; use std::mem; +#[derive(Debug)] pub struct CodeRepo { pub(super) cached_query: Code, pub(super) goal_expanders: Code, diff --git a/src/prolog/machine/compile.rs b/src/prolog/machine/compile.rs index 3e059ffa..10bc46e9 100644 --- a/src/prolog/machine/compile.rs +++ b/src/prolog/machine/compile.rs @@ -409,6 +409,7 @@ fn compile_into_module_impl( Ok(compiler.drop_expansions(&mut wam.code_repo)) } +#[derive(Debug)] pub struct GatherResult { dynamic_clause_map: DynamicClauseMap, pub(crate) worker_results: Vec, @@ -423,6 +424,7 @@ pub struct GatherResult { in_situ_module_dir: ModuleStubDir, } +#[derive(Debug)] pub struct ClauseCodeGenerator { len_offset: usize, code: Code, @@ -529,6 +531,7 @@ fn insert_or_refresh_term_dir_quantum( } } +#[derive(Debug)] pub struct ListingCompiler { module: Option, user_term_dir: TermDir, diff --git a/src/prolog/machine/copier.rs b/src/prolog/machine/copier.rs index c5a3ef18..08c41944 100644 --- a/src/prolog/machine/copier.rs +++ b/src/prolog/machine/copier.rs @@ -7,7 +7,7 @@ use std::ops::IndexMut; type Trail = Vec<(Ref, HeapCellValue)>; -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum AttrVarPolicy { DeepCopy, StripAttributes @@ -28,6 +28,7 @@ fn copy_term(target: T, addr: Addr, attr_var_policy: AttrVarPol copy_term_state.copy_term_impl(addr); } +#[derive(Debug)] struct CopyTermState { trail: Trail, scan: usize, diff --git a/src/prolog/machine/heap.rs b/src/prolog/machine/heap.rs index eceb55a3..9d8cbfe7 100644 --- a/src/prolog/machine/heap.rs +++ b/src/prolog/machine/heap.rs @@ -11,6 +11,7 @@ use std::mem; use std::ops::{Index, IndexMut}; use std::ptr; +#[derive(Debug)] pub(crate) struct StandardHeapTraits {} impl RawBlockTraits for StandardHeapTraits { @@ -25,6 +26,7 @@ impl RawBlockTraits for StandardHeapTraits { } } +#[derive(Debug)] pub(crate) struct HeapTemplate { buf: RawBlock, _marker: PhantomData, @@ -39,6 +41,7 @@ impl Drop for HeapTemplate { } } +#[derive(Debug)] pub(crate) struct HeapIntoIter { offset: usize, @@ -72,6 +75,7 @@ impl Iterator for HeapIntoIter { } } +#[derive(Debug)] pub(crate) struct HeapIter<'a, T: RawBlockTraits> { offset: usize, @@ -110,6 +114,7 @@ fn print_heap_terms<'a, I: Iterator>(heap: I, h: usize } } +#[derive(Debug)] pub(crate) struct HeapIterMut<'a, T: RawBlockTraits> { offset: usize, diff --git a/src/prolog/machine/machine_errors.rs b/src/prolog/machine/machine_errors.rs index 9948787c..fd1e9397 100644 --- a/src/prolog/machine/machine_errors.rs +++ b/src/prolog/machine/machine_errors.rs @@ -10,12 +10,13 @@ use std::rc::Rc; pub(crate) type MachineStub = Vec; -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] enum ErrorProvenance { Constructed, // if constructed, offset the addresses. Received, // otherwise, preserve the addresses. } +#[derive(Debug)] pub(super) struct MachineError { stub: MachineStub, location: Option<(usize, usize)>, // line_num, col_num @@ -447,7 +448,7 @@ impl MachineError { } } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum Permission { Access, Create, @@ -469,7 +470,7 @@ impl Permission { } // from 7.12.2 b) of 13211-1:1995 -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum ValidType { Atom, Atomic, @@ -514,7 +515,7 @@ impl ValidType { } } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum DomainErrorType { NotLessThanZero, Order, @@ -534,7 +535,7 @@ impl DomainErrorType { } // from 7.12.2 f) of 13211-1:1995 -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum RepFlag { Character, CharacterCode, @@ -558,7 +559,7 @@ impl RepFlag { } // from 7.12.2 g) of 13211-1:1995 -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum EvalError { FloatOverflow, Undefined, @@ -578,7 +579,7 @@ impl EvalError { } // used by '$skip_max_list'. -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub(super) enum CycleSearchResult { EmptyList, NotList, @@ -722,6 +723,7 @@ impl MachineState { } } +#[derive(Debug)] pub enum ExistenceError { Module(ClauseName), Procedure(ClauseName, usize), @@ -729,6 +731,7 @@ pub enum ExistenceError { Stream(Addr), } +#[derive(Debug)] pub enum SessionError { CannotOverwriteBuiltIn(ClauseName), CannotOverwriteImport(ClauseName), @@ -741,6 +744,7 @@ pub enum SessionError { ParserError(ParserError), } +#[derive(Debug)] pub enum EvalSession { EntrySuccess, Error(SessionError), diff --git a/src/prolog/machine/machine_indices.rs b/src/prolog/machine/machine_indices.rs index a11d2c1a..fe885bb9 100644 --- a/src/prolog/machine/machine_indices.rs +++ b/src/prolog/machine/machine_indices.rs @@ -21,16 +21,17 @@ use std::cell::RefCell; use std::cmp::Ordering; use std::collections::{BTreeMap, VecDeque}; use std::convert::TryFrom; +use std::fmt; use std::mem; use std::ops::{Add, AddAssign, Sub, SubAssign}; use std::rc::Rc; -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct OrderedOpDirKey(pub ClauseName, pub Fixity); pub type OssifiedOpDir = BTreeMap; -#[derive(Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum DBRef { NamedPred(ClauseName, usize, Option), Op( @@ -43,7 +44,7 @@ pub enum DBRef { } // 7.2 -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum TermOrderCategory { Variable, FloatingPoint, @@ -52,7 +53,7 @@ pub enum TermOrderCategory { Compound, } -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Addr { AttrVar(usize), Char(char), @@ -71,7 +72,7 @@ pub enum Addr { Usize(usize), } -#[derive(Clone, Copy, Hash, Eq, PartialEq, PartialOrd)] +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, PartialOrd)] pub enum Ref { AttrVar(usize), HeapCell(usize), @@ -361,7 +362,7 @@ impl SubAssign for Addr { } } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub enum TrailRef { Ref(Ref), AttrVarHeapLink(usize), @@ -374,6 +375,7 @@ impl From for TrailRef { } } +#[derive(Debug)] pub enum HeapCellValue { Addr(Addr), Atom(ClauseName, Option), @@ -446,7 +448,7 @@ impl From for HeapCellValue { } } -#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] pub enum IndexPtr { DynamicUndefined, // a predicate, declared as dynamic, whose location in code is as yet undefined. Undefined, @@ -456,7 +458,7 @@ pub enum IndexPtr { UserTermExpansion } -#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] +#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct CodeIndex(pub Rc>); impl CodeIndex { @@ -511,7 +513,7 @@ impl From<(usize, ClauseName)> for CodeIndex { } } -#[derive(Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum DynamicAssertPlace { Back, Front, @@ -535,7 +537,7 @@ impl DynamicAssertPlace { } } -#[derive(Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum DynamicTransactionType { Abolish, Assert(DynamicAssertPlace), @@ -545,7 +547,7 @@ pub enum DynamicTransactionType { Retract, // dynamic index of the clause to remove. } -#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)] pub enum REPLCodePtr { CompileBatch, UseModule, @@ -554,7 +556,7 @@ pub enum REPLCodePtr { UseQualifiedModuleFromFile } -#[derive(Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum CodePtr { BuiltInClause(BuiltInClauseType, LocalCodePtr), // local is the successor call. CallN(usize, LocalCodePtr, bool), // arity, local, last call. @@ -773,7 +775,7 @@ impl AddAssign for CodePtr { pub type HeapVarDict = IndexMap, Addr>; pub type AllocVarDict = IndexMap, VarData>; -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct DynamicPredicateInfo { pub(super) clauses_subsection_p: usize, // a LocalCodePtr::DirEntry value. } @@ -793,6 +795,7 @@ pub type DynamicCodeDir = IndexMap<(ClauseName, ClauseName, usize), DynamicPredi pub type GlobalVarDir = IndexMap)>; +#[derive(Debug)] pub(crate) struct ModuleStub { pub(crate) atom_tbl: TabledData, pub(crate) in_situ_code_dir: InSituCodeDir, @@ -810,6 +813,7 @@ impl ModuleStub { pub(crate) type ModuleStubDir = IndexMap; pub(crate) type StreamAliasDir = IndexMap; +#[derive(Debug)] pub struct IndexStore { pub(super) atom_tbl: TabledData, pub(super) code_dir: CodeDir, @@ -960,6 +964,7 @@ impl IndexStore { pub type CodeDir = BTreeMap; pub type TermDir = IndexMap)>; +#[derive(Debug)] pub struct TermDirQuantumEntry { pub old_terms: (Predicate, VecDeque), pub new_terms: (Predicate, VecDeque), @@ -988,6 +993,7 @@ impl TermDirQuantumEntry { } } +#[derive(Debug)] pub struct TermDirQuantum(IndexMap); impl TermDirQuantum { @@ -1031,7 +1037,7 @@ impl TermDirQuantum { } } -#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] pub enum CompileTimeHook { GoalExpansion, TermExpansion, @@ -1085,6 +1091,16 @@ pub enum RefOrOwned<'a, T: 'a> { Owned(T), } +impl<'a, T: 'a + fmt::Debug> fmt::Debug for RefOrOwned<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + &RefOrOwned::Borrowed(ref borrowed) => + write!(f, "Borrowed({:?})", borrowed), + &RefOrOwned::Owned(ref owned) => write!(f, "Owned({:?})", owned), + } + } +} + impl<'a, T> RefOrOwned<'a, T> { pub fn as_ref(&'a self) -> &'a T { match self { diff --git a/src/prolog/machine/machine_state.rs b/src/prolog/machine/machine_state.rs index d247abe5..97e05e31 100644 --- a/src/prolog/machine/machine_state.rs +++ b/src/prolog/machine/machine_state.rs @@ -21,10 +21,12 @@ use indexmap::{IndexMap, IndexSet}; use std::cmp::Ordering; use std::convert::TryFrom; +use std::fmt; use std::io::Write; use std::mem; use std::ops::{Index, IndexMut}; +#[derive(Debug)] pub(crate) struct HeapPStrIter<'a> { focus: Addr, machine_st: &'a MachineState, @@ -74,7 +76,7 @@ impl<'a> HeapPStrIter<'a> { } } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub(crate) enum PStrIteratee { Char(char), PStrSegment(usize, usize), @@ -100,7 +102,7 @@ impl<'a> Iterator for HeapPStrIter<'a> { } else { Addr::EmptyList }; - + return Some(PStrIteratee::PStrSegment(h, n)); } else { unreachable!() @@ -306,6 +308,7 @@ fn compare_pstr_to_string<'a>( Some(s_offset) } +#[derive(Debug)] pub struct Ball { pub(super) boundary: usize, pub(super) stub: Heap, @@ -357,6 +360,7 @@ impl Ball { } } +#[derive(Debug)] pub(super) struct CopyTerm<'a> { state: &'a mut MachineState, } @@ -404,6 +408,7 @@ impl<'a> CopierTarget for CopyTerm<'a> { } } +#[derive(Debug)] pub(super) struct CopyBallTerm<'a> { stack: &'a mut Stack, heap: &'a mut Heap, @@ -529,13 +534,13 @@ impl IndexMut for MachineState { pub type Registers = Vec; -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub(super) enum MachineMode { Read, Write, } -#[derive(Clone)] +#[derive(Debug, Clone)] pub(super) enum HeapPtr { HeapCell(usize), PStrChar(usize, usize), @@ -576,6 +581,7 @@ impl Default for HeapPtr { } } +#[derive(Debug)] pub struct MachineState { pub(super) s: HeapPtr, pub(super) p: CodePtr, @@ -934,7 +940,7 @@ fn try_in_situ( pub(crate) type CallResult = Result<(), Vec>; -pub(crate) trait CallPolicy: Any { +pub(crate) trait CallPolicy: Any + fmt::Debug { fn retry_me_else(&mut self, machine_st: &mut MachineState, offset: usize) -> CallResult { let b = machine_st.b; let n = machine_st.stack.index_or_frame(b).prelude.univ_prelude.num_cells; @@ -1517,10 +1523,12 @@ impl CallPolicy for CWILCallPolicy { downcast!(dyn CallPolicy); +#[derive(Debug)] pub(crate) struct DefaultCallPolicy {} impl CallPolicy for DefaultCallPolicy {} +#[derive(Debug)] pub(crate) struct CWILCallPolicy { pub(crate) prev_policy: Box, count: Integer, @@ -1601,7 +1609,7 @@ impl CWILCallPolicy { } } -pub(crate) trait CutPolicy: Any { +pub(crate) trait CutPolicy: Any + fmt::Debug { // returns true iff we fail or cut redirected the MachineState's p itself fn cut(&mut self, machine_st: &mut MachineState, r: RegType) -> bool; } @@ -1627,6 +1635,7 @@ fn cut_body(machine_st: &mut MachineState, addr: &Addr) -> bool { false } +#[derive(Debug)] pub(crate) struct DefaultCutPolicy {} pub(super) fn deref_cut(machine_st: &mut MachineState, r: RegType) { @@ -1641,6 +1650,7 @@ impl CutPolicy for DefaultCutPolicy { } } +#[derive(Debug)] pub(crate) struct SCCCutPolicy { // locations of cleaners, cut points, the previous block cont_pts: Vec<(Addr, usize, usize)>, diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index c951135f..215d10b1 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -53,6 +53,7 @@ use std::path::PathBuf; use std::rc::Rc; use std::sync::atomic::AtomicBool; +#[derive(Debug)] pub struct MachinePolicies { call_policy: Box, cut_policy: Box, @@ -79,6 +80,7 @@ impl Default for MachinePolicies { } } +#[derive(Debug)] pub struct Machine { pub(super) machine_st: MachineState, pub(super) inner_heap: Heap, diff --git a/src/prolog/machine/partial_string.rs b/src/prolog/machine/partial_string.rs index cb95b44c..ea36d409 100644 --- a/src/prolog/machine/partial_string.rs +++ b/src/prolog/machine/partial_string.rs @@ -7,6 +7,7 @@ use std::ops::RangeFrom; use std::slice; use std::str; +#[derive(Debug)] pub struct PartialString { buf: *const u8, len: usize, @@ -46,6 +47,7 @@ fn scan_for_terminator>(iter: Iter) -> usize { terminator_idx } +#[derive(Debug)] pub struct PStrIter { buf: *const u8, len: usize, diff --git a/src/prolog/machine/raw_block.rs b/src/prolog/machine/raw_block.rs index 64135048..e44b714b 100644 --- a/src/prolog/machine/raw_block.rs +++ b/src/prolog/machine/raw_block.rs @@ -14,6 +14,7 @@ pub(crate) trait RawBlockTraits { } } +#[derive(Debug)] pub(crate) struct RawBlock { pub(crate) size: usize, pub(crate) base: *const u8, diff --git a/src/prolog/machine/stack.rs b/src/prolog/machine/stack.rs index 4468899c..071cb3a0 100644 --- a/src/prolog/machine/stack.rs +++ b/src/prolog/machine/stack.rs @@ -7,6 +7,7 @@ use std::mem; use std::ops::{Index, IndexMut}; use std::ptr; +#[derive(Debug)] struct StackTraits {} impl RawBlockTraits for StackTraits { @@ -35,6 +36,7 @@ const fn prelude_size() -> usize { (size & !(align - 1)) + align } +#[derive(Debug)] pub struct Stack { buf: RawBlock, _marker: PhantomData, @@ -47,11 +49,12 @@ impl Drop for Stack { } } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] pub struct FramePrelude { pub num_cells: usize, } +#[derive(Debug)] pub struct AndFramePrelude { pub univ_prelude: FramePrelude, pub e: usize, @@ -59,6 +62,7 @@ pub struct AndFramePrelude { pub interrupt_cp: LocalCodePtr, } +#[derive(Debug)] pub struct AndFrame { pub prelude: AndFramePrelude, } @@ -99,6 +103,7 @@ impl IndexMut for AndFrame { } } +#[derive(Debug)] pub struct OrFramePrelude { pub univ_prelude: FramePrelude, pub e: usize, @@ -113,6 +118,7 @@ pub struct OrFramePrelude { pub attr_var_init_bindings_b: usize, } +#[derive(Debug)] pub struct OrFrame { pub prelude: OrFramePrelude, } diff --git a/src/prolog/machine/streams.rs b/src/prolog/machine/streams.rs index 1653defb..d9a83934 100644 --- a/src/prolog/machine/streams.rs +++ b/src/prolog/machine/streams.rs @@ -11,13 +11,13 @@ use std::hash::{Hash, Hasher}; use std::net::TcpStream; use std::rc::Rc; -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum StreamType { Binary, Text, } -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum EOFAction { EOFCode, Error, @@ -37,7 +37,26 @@ pub enum StreamInstance { TcpStream(TcpStream), } -#[derive(Clone)] +impl fmt::Debug for StreamInstance { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match self { + &StreamInstance::Bytes(ref bytes) => + write!(fmt, "Bytes({:?})", bytes), + &StreamInstance::DynReadSource(_) => + write!(fmt, "DynReadSource(_)"), // Hacky solution. + &StreamInstance::File(ref file) => write!(fmt, "File({:?})", file), + &StreamInstance::Null => write!(fmt, "Null"), + &StreamInstance::ReadlineStream(ref readline_stream) => + write!(fmt, "ReadlineStream({:?})", readline_stream), + &StreamInstance::Stdin => write!(fmt, "Stdin"), + &StreamInstance::Stdout => write!(fmt, "Stdout"), + &StreamInstance::TcpStream(ref tcp_stream) => + write!(fmt, "TcpStream({:?})", tcp_stream), + } + } +} + +#[derive(Debug, Clone)] struct WrappedStreamInstance(Rc>); impl WrappedStreamInstance { @@ -95,7 +114,7 @@ impl fmt::Display for StreamError { impl Error for StreamError {} -#[derive(Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct StreamOptions { pub stream_type: StreamType, pub reposition: bool, @@ -115,7 +134,7 @@ impl Default for StreamOptions { } } -#[derive(Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Stream { pub options: StreamOptions, stream_inst: WrappedStreamInstance, diff --git a/src/prolog/machine/system_calls.rs b/src/prolog/machine/system_calls.rs index 4c0fed6b..f3074839 100644 --- a/src/prolog/machine/system_calls.rs +++ b/src/prolog/machine/system_calls.rs @@ -61,6 +61,7 @@ pub fn get_single_char() -> char { c } +#[derive(Debug)] struct BrentAlgState { hare: Addr, tortoise: Addr, diff --git a/src/prolog/machine/term_expansion.rs b/src/prolog/machine/term_expansion.rs index d8690483..06d7b3be 100644 --- a/src/prolog/machine/term_expansion.rs +++ b/src/prolog/machine/term_expansion.rs @@ -47,6 +47,7 @@ fn extract_from_list( } } +#[derive(Debug)] pub struct TermStream<'a> { stack: Vec, pub(crate) wam: &'a mut Machine, @@ -57,6 +58,7 @@ pub struct TermStream<'a> { top_level_terms: Vec<(Term, usize, usize)>, // term, line_num, col_num. } +#[derive(Debug)] pub struct ExpansionAdditionResult { term_expansion_additions: (Predicate, VecDeque), goal_expansion_additions: (Predicate, VecDeque), diff --git a/src/prolog/machine/toplevel.rs b/src/prolog/machine/toplevel.rs index e4198a68..c8004aca 100644 --- a/src/prolog/machine/toplevel.rs +++ b/src/prolog/machine/toplevel.rs @@ -14,6 +14,7 @@ use std::borrow::BorrowMut; use std::cell::Cell; use std::collections::VecDeque; use std::convert::TryFrom; +use std::fmt; use std::mem; use std::ops::DerefMut; use std::rc::Rc; @@ -23,6 +24,15 @@ enum IndexSource<'a, T> { Local(&'a mut T) } +impl<'a, T: fmt::Debug> fmt::Debug for IndexSource<'a, T> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match self { + IndexSource::TermStream => write!(fmt, "TermStream"), + IndexSource::Local(ref local) => write!(fmt, "Local({:?})", local), + } + } +} + fn op_dir<'a, 'b: 'a>(from: &'b IndexSource<'a, IndexStore>) -> RefOrOwned<'a, OpDir> { match from { IndexSource::TermStream => RefOrOwned::Owned(OpDir::new()), @@ -30,6 +40,7 @@ fn op_dir<'a, 'b: 'a>(from: &'b IndexSource<'a, IndexStore>) -> RefOrOwned<'a, O } } +#[derive(Debug)] struct CompositeIndices<'a, 'b, 'c> { term_stream: &'b mut TermStream<'a>, index_src: IndexSource<'c, IndexStore>, @@ -690,6 +701,7 @@ fn setup_declaration<'a, 'b, 'c>( } } +#[derive(Debug)] struct RelationWorker { flags: MachineFlags, dynamic_clauses: Vec<(Term, Term)>, // Head, Body. @@ -1141,6 +1153,7 @@ pub type DynamicClause = Vec<(Term, Term)>; pub type DynamicClauseMap = IndexMap<(ClauseName, usize), DynamicClause>; +#[derive(Debug)] pub struct TopLevelBatchWorker<'a> { pub(crate) term_stream: TermStream<'a>, rel_worker: RelationWorker, diff --git a/src/prolog/read.rs b/src/prolog/read.rs index 72be1b18..28d35358 100644 --- a/src/prolog/read.rs +++ b/src/prolog/read.rs @@ -35,6 +35,7 @@ pub mod readline { } } + #[derive(Debug)] pub struct ReadlineStream { rl: Editor<()>, pending_input: Cursor, @@ -114,12 +115,14 @@ fn write_term_to_heap(term: &Term, machine_st: &mut MachineState) -> TermWriteRe term_writer.write_term_to_heap(term) } +#[derive(Debug)] struct TermWriter<'a> { machine_st: &'a mut MachineState, queue: SubtermDeque, var_dict: HeapVarDict, } +#[derive(Debug)] pub struct TermWriteResult { pub(crate) heap_loc: usize, pub(crate) var_dict: HeapVarDict, diff --git a/src/tests/predicates.pl b/src/tests/predicates.pl index 44d0b064..024c242d 100644 --- a/src/tests/predicates.pl +++ b/src/tests/predicates.pl @@ -33,6 +33,6 @@ test_queries_on_predicates :- assertz((p(X, Y) :- q(Z), p(X, X))), once(p(X,b)), retract((p(X, Y) :- q(Z), p(X, X))), - retract(q(z)). + retract(q(z)). :- initialization(test_queries_on_predicates). diff --git a/src/tests/setup_call_cleanup.pl b/src/tests/setup_call_cleanup.pl index a5869731..331f50f4 100644 --- a/src/tests/setup_call_cleanup.pl +++ b/src/tests/setup_call_cleanup.pl @@ -24,5 +24,5 @@ test_queries_on_setup_call_cleanup :- findall(Pat, catch(setup_call_cleanup(true,throw(goal),throw(cl)), Pat, true), [goal]), findall(Pat, catch(( setup_call_cleanup(true,(G=1;G=2),throw(cl)), throw(cont)), Pat, true), [cont]), findall([X,Y], (setup_call_cleanup(true, (X=1;X=2), writeq(a)), setup_call_cleanup(true,(Y=1;Y=2),writeq(b)), !), [[1,1]]). - + :- initialization(test_queries_on_setup_call_cleanup). -- 2.54.0