This commit is contained in:
Mark Thom
2020-04-26 20:15:27 -06:00
36 changed files with 374 additions and 95 deletions

View File

@@ -27,7 +27,7 @@ libc = "0.2.62"
nix = "0.15.0" nix = "0.15.0"
num-rug-adapter = { optional = true, version = "0.1.3" } num-rug-adapter = { optional = true, version = "0.1.3" }
ordered-float = "0.5.0" 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" ref_thread_local = "0.0.0"
rug = { version = "1.4.0", optional = true } rug = { version = "1.4.0", optional = true }
rustyline = "6.0.0" rustyline = "6.0.0"

View File

@@ -239,6 +239,40 @@ only the standard predicate `(=)/2` is used.
Definite clause grammars as provided by `library(dcgs)` are ideally Definite clause grammars as provided by `library(dcgs)` are ideally
suited for reasoning about strings. suited for reasoning about strings.
### Tabling (SLG resolution)
One of the foremost attractions of Prolog is that logical consequences
of pure programs can be derived by various execution strategies
that differ regarding essential properties such as termination,
completeness and efficiency.
The default execution strategy of Prolog is depth-first search with
chronological backtracking. This strategy is very efficient. Its main
drawback is that it is *incomplete*: It may fail to find any solution
even if one exists.
Scryer Prolog supports an alternative execution strategy which is
called *tabling* and also known as tabled execution and
SLG resolution. To enable tabled execution for a predicate, use
[`library(tabling)`](src/prolog/lib/tabling.pl) and add a `(table)/1`
directive for the desired predicate indicator. For example, if we
write:
```
:- use_module(library(tabling)).
:- table a/0.
a :- a.
```
Then the query `?- a.` *terminates* (and fails), whereas it
does not terminate with the default execution strategy.
Scryer Prolog implements tabling via *delimited continuations* as
described in [*Tabling as a Library with Delimited
Control*](https://biblio.ugent.be/publication/6880648/file/6885145.pdf)
by Desouter et. al.
### Modules ### Modules
Scryer has a simple predicate-based module system. It provides a Scryer has a simple predicate-based module system. It provides a
@@ -307,6 +341,14 @@ The modules that ship with Scryer Prolog are also called
* [`time`](src/prolog/lib/time.pl) * [`time`](src/prolog/lib/time.pl)
`time/1` reports the CPU time of a goal. It is useful `time/1` reports the CPU time of a goal. It is useful
for measuring the performance of your code. for measuring the performance of your code.
* [`cont`](src/prolog/lib/cont.pl)
Provides *delimited continuations* via `reset/3` and `shift/1`.
To read contents of external files, use `phrase_from_file/2` from
[`library(pio)`](src/prolog/lib/pio.pl) to apply a DCG to
file contents. The predicates in
[`library(charsio)`](src/prolog/lib/charsio.pl) are also useful for
parsing.
To use predicates provided by the `lists` library, write: To use predicates provided by the `lists` library, write:
@@ -343,7 +385,6 @@ REPL:
``` ```
?- [user]. ?- [user].
(type Enter + Ctrl-D to terminate the stream when finished)
:- module(test, [local_member/2]). :- module(test, [local_member/2]).
:- use_module(library(lists)). :- use_module(library(lists)).

View File

@@ -23,6 +23,7 @@ use std::ops::{Add, Div, Mul, Neg, Sub};
use std::rc::Rc; use std::rc::Rc;
use std::vec::Vec; use std::vec::Vec;
#[derive(Debug)]
pub struct ArithInstructionIterator<'a> { pub struct ArithInstructionIterator<'a> {
state_stack: Vec<TermIterState<'a>>, state_stack: Vec<TermIterState<'a>>,
} }
@@ -68,6 +69,7 @@ impl<'a> ArithInstructionIterator<'a> {
} }
} }
#[derive(Debug)]
pub enum ArithTermRef<'a> { pub enum ArithTermRef<'a> {
Constant(&'a Constant), Constant(&'a Constant),
Op(ClauseName, usize), // name, arity. Op(ClauseName, usize), // name, arity.
@@ -109,6 +111,7 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
} }
} }
#[derive(Debug)]
pub struct ArithmeticEvaluator<'a> { pub struct ArithmeticEvaluator<'a> {
bindings: &'a AllocVarDict, bindings: &'a AllocVarDict,
interm: Vec<ArithmeticTerm>, interm: Vec<ArithmeticTerm>,

View File

@@ -8,7 +8,7 @@ use ref_thread_local::RefThreadLocal;
use std::collections::BTreeMap; use std::collections::BTreeMap;
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CompareNumberQT { pub enum CompareNumberQT {
GreaterThan, GreaterThan,
LessThan, LessThan,
@@ -31,7 +31,7 @@ impl CompareNumberQT {
} }
} }
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareTermQT { pub enum CompareTermQT {
LessThan, LessThan,
LessThanOrEqual, LessThanOrEqual,
@@ -50,7 +50,7 @@ impl CompareTermQT {
} }
} }
#[derive(Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum ArithmeticTerm { pub enum ArithmeticTerm {
Reg(RegType), Reg(RegType),
Interm(usize), Interm(usize),
@@ -67,7 +67,7 @@ impl ArithmeticTerm {
} }
} }
#[derive(Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub enum InlinedClauseType { pub enum InlinedClauseType {
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm), CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
IsAtom(RegType), IsAtom(RegType),
@@ -145,7 +145,7 @@ impl InlinedClauseType {
} }
} }
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SystemClauseType { pub enum SystemClauseType {
AbolishClause, AbolishClause,
AbolishModuleClause, AbolishModuleClause,
@@ -178,6 +178,7 @@ pub enum SystemClauseType {
ExpandTerm, ExpandTerm,
FetchGlobalVar, FetchGlobalVar,
FetchGlobalVarWithOffset, FetchGlobalVarWithOffset,
FileToChars,
GetChar, GetChar,
GetSingleChar, GetSingleChar,
ResetAttrVarState, ResetAttrVarState,
@@ -312,6 +313,7 @@ impl SystemClauseType {
&SystemClauseType::FetchGlobalVarWithOffset => { &SystemClauseType::FetchGlobalVarWithOffset => {
clause_name!("$fetch_global_var_with_offset") clause_name!("$fetch_global_var_with_offset")
} }
&SystemClauseType::FileToChars => clause_name!("$file_to_chars"),
&SystemClauseType::GetChar => clause_name!("$get_char"), &SystemClauseType::GetChar => clause_name!("$get_char"),
&SystemClauseType::GetSingleChar => clause_name!("$get_single_char"), &SystemClauseType::GetSingleChar => clause_name!("$get_single_char"),
&SystemClauseType::ResetAttrVarState => clause_name!("$reset_attr_var_state"), &SystemClauseType::ResetAttrVarState => clause_name!("$reset_attr_var_state"),
@@ -461,6 +463,7 @@ impl SystemClauseType {
("$expand_goal", 2) => Some(SystemClauseType::ExpandGoal), ("$expand_goal", 2) => Some(SystemClauseType::ExpandGoal),
("$fetch_global_var", 2) => Some(SystemClauseType::FetchGlobalVar), ("$fetch_global_var", 2) => Some(SystemClauseType::FetchGlobalVar),
("$fetch_global_var_with_offset", 3) => Some(SystemClauseType::FetchGlobalVarWithOffset), ("$fetch_global_var_with_offset", 3) => Some(SystemClauseType::FetchGlobalVarWithOffset),
("$file_to_chars", 2) => Some(SystemClauseType::FileToChars),
("$get_char", 1) => Some(SystemClauseType::GetChar), ("$get_char", 1) => Some(SystemClauseType::GetChar),
("$get_single_char", 1) => Some(SystemClauseType::GetSingleChar), ("$get_single_char", 1) => Some(SystemClauseType::GetSingleChar),
("$points_to_cont_reset_marker", 1) => { ("$points_to_cont_reset_marker", 1) => {
@@ -554,7 +557,7 @@ impl SystemClauseType {
} }
} }
#[derive(Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub enum BuiltInClauseType { pub enum BuiltInClauseType {
AcyclicTerm, AcyclicTerm,
Arg, Arg,
@@ -572,7 +575,7 @@ pub enum BuiltInClauseType {
Sort, Sort,
} }
#[derive(Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClauseType { pub enum ClauseType {
BuiltIn(BuiltInClauseType), BuiltIn(BuiltInClauseType),
CallN, CallN,

View File

@@ -17,12 +17,14 @@ use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
use std::vec::Vec; use std::vec::Vec;
#[derive(Debug)]
pub struct CodeGenerator<TermMarker> { pub struct CodeGenerator<TermMarker> {
marker: TermMarker, marker: TermMarker,
pub var_count: IndexMap<Rc<Var>, usize>, pub var_count: IndexMap<Rc<Var>, usize>,
non_counted_bt: bool, non_counted_bt: bool,
} }
#[derive(Debug)]
pub struct ConjunctInfo<'a> { pub struct ConjunctInfo<'a> {
pub perm_vs: VariableFixtures<'a>, pub perm_vs: VariableFixtures<'a>,
pub num_of_chunks: usize, pub num_of_chunks: usize,

View File

@@ -12,6 +12,7 @@ use std::cell::Cell;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug)]
pub struct DebrayAllocator { pub struct DebrayAllocator {
bindings: IndexMap<Rc<Var>, VarData>, bindings: IndexMap<Rc<Var>, VarData>,
arg_c: usize, arg_c: usize,

View File

@@ -13,6 +13,7 @@ use std::rc::Rc;
use std::vec::Vec; use std::vec::Vec;
// labeled with chunk numbers. // labeled with chunk numbers.
#[derive(Debug)]
pub enum VarStatus { pub enum VarStatus {
Perm(usize), Perm(usize),
Temp(usize, TempVarData), // Perm(chunk_num) | Temp(chunk_num, _) 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. // Perm: 0 initially, a stack register once processed.
// Temp: labeled with chunk_num and temp offset (unassigned if 0). // Temp: labeled with chunk_num and temp offset (unassigned if 0).
#[derive(Debug)]
pub enum VarData { pub enum VarData {
Perm(usize), Perm(usize),
Temp(usize, usize, TempVarData), Temp(usize, usize, TempVarData),
@@ -36,6 +38,7 @@ impl VarData {
} }
} }
#[derive(Debug)]
pub struct TempVarData { pub struct TempVarData {
pub last_term_arity: usize, pub last_term_arity: usize,
pub use_set: OccurrenceSet, pub use_set: OccurrenceSet,
@@ -79,6 +82,7 @@ impl TempVarData {
type VariableFixture<'a> = (VarStatus, Vec<&'a Cell<VarReg>>); type VariableFixture<'a> = (VarStatus, Vec<&'a Cell<VarReg>>);
#[derive(Debug)]
pub struct VariableFixtures<'a>{ pub struct VariableFixtures<'a>{
perm_vars: IndexMap<Rc<Var>, VariableFixture<'a>>, perm_vars: IndexMap<Rc<Var>, VariableFixture<'a>>,
last_chunk_temp_vars: IndexSet<Rc<Var>> last_chunk_temp_vars: IndexSet<Rc<Var>>
@@ -248,6 +252,7 @@ impl<'a> VariableFixtures<'a> {
} }
} }
#[derive(Debug)]
pub struct UnsafeVarMarker { pub struct UnsafeVarMarker {
pub unsafe_vars: IndexMap<RegType, usize>, pub unsafe_vars: IndexMap<RegType, usize>,
pub safe_vars: IndexSet<RegType>, pub safe_vars: IndexSet<RegType>,

View File

@@ -22,7 +22,7 @@ pub type PredicateKey = (ClauseName, usize); // name, arity.
// of vars (we get their adjoining cells this way). // of vars (we get their adjoining cells this way).
pub type JumpStub = Vec<Term>; pub type JumpStub = Vec<Term>;
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum TopLevel { pub enum TopLevel {
Declaration(Declaration), Declaration(Declaration),
Fact(Term, usize, usize), // Term, line_num, col_num 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 { pub enum Level {
Deep, Deep,
Root, Root,
@@ -58,7 +58,7 @@ impl Level {
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum QueryTerm { pub enum QueryTerm {
// register, clause type, subterms, use default call policy. // register, clause type, subterms, use default call policy.
Clause(Cell<RegType>, ClauseType, Vec<Box<Term>>, bool), Clause(Cell<RegType>, ClauseType, Vec<Box<Term>>, bool),
@@ -86,13 +86,13 @@ impl QueryTerm {
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub struct Rule { pub struct Rule {
pub head: (ClauseName, Vec<Box<Term>>, QueryTerm), pub head: (ClauseName, Vec<Box<Term>>, QueryTerm),
pub clauses: Vec<QueryTerm>, pub clauses: Vec<QueryTerm>,
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub struct Predicate(pub Vec<PredicateClause>); pub struct Predicate(pub Vec<PredicateClause>);
impl Predicate { impl Predicate {
@@ -114,7 +114,7 @@ impl Predicate {
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum ListingSource { pub enum ListingSource {
File(ClauseName, PathBuf), // filename, path File(ClauseName, PathBuf), // filename, path
User, User,
@@ -326,7 +326,7 @@ impl ClauseConsistency for Predicate {
pub type CompiledResult = (Predicate, VecDeque<TopLevel>); pub type CompiledResult = (Predicate, VecDeque<TopLevel>);
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum PredicateClause { pub enum PredicateClause {
Fact(Term, usize, usize), // Term, line number, column number. Fact(Term, usize, usize), // Term, line number, column number.
Rule(Rule, 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 { pub enum ModuleSource {
Library(ClauseName), Library(ClauseName),
File(ClauseName), File(ClauseName),
@@ -392,13 +392,13 @@ impl ModuleSource {
pub type ScopedPredicateKey = (ClauseName, PredicateKey); // module name, predicate indicator. pub type ScopedPredicateKey = (ClauseName, PredicateKey); // module name, predicate indicator.
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum MultiFileIndicator { pub enum MultiFileIndicator {
LocalScoped(ClauseName, usize), // name, arity LocalScoped(ClauseName, usize), // name, arity
ModuleScoped(ScopedPredicateKey), ModuleScoped(ScopedPredicateKey),
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum Declaration { pub enum Declaration {
Dynamic(ClauseName, usize), // name, arity Dynamic(ClauseName, usize), // name, arity
EndOfFile, 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); pub struct OpDecl(pub usize, pub Specifier, pub ClauseName);
impl OpDecl { impl OpDecl {
@@ -558,18 +558,19 @@ pub fn fetch_op_spec(
pub type ModuleDir = IndexMap<ClauseName, Module>; pub type ModuleDir = IndexMap<ClauseName, Module>;
#[derive(Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ModuleExport { pub enum ModuleExport {
OpDecl(OpDecl), OpDecl(OpDecl),
PredicateKey(PredicateKey), PredicateKey(PredicateKey),
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub struct ModuleDecl { pub struct ModuleDecl {
pub name: ClauseName, pub name: ClauseName,
pub exports: Vec<ModuleExport>, pub exports: Vec<ModuleExport>,
} }
#[derive(Debug)]
pub struct Module { pub struct Module {
pub atom_tbl: TabledData<Atom>, pub atom_tbl: TabledData<Atom>,
pub module_decl: ModuleDecl, pub module_decl: ModuleDecl,
@@ -587,7 +588,7 @@ pub struct Module {
pub listing_src: ListingSource, pub listing_src: ListingSource,
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum Number { pub enum Number {
Float(OrderedFloat<f64>), Float(OrderedFloat<f64>),
Integer(Rc<Integer>), Integer(Rc<Integer>),

View File

@@ -7,6 +7,7 @@ use std::cmp::Ordering;
use std::ops::Deref; use std::ops::Deref;
use std::vec::Vec; use std::vec::Vec;
#[derive(Debug)]
pub struct HCPreOrderIterator<'a> { pub struct HCPreOrderIterator<'a> {
pub machine_st: &'a MachineState, pub machine_st: &'a MachineState,
pub state_stack: Vec<Addr>, pub state_stack: Vec<Addr>,
@@ -127,6 +128,7 @@ pub trait MutStackHCIterator<'b> where Self: Iterator
fn stack(&'b mut self) -> Self::MutStack; fn stack(&'b mut self) -> Self::MutStack;
} }
#[derive(Debug)]
pub struct HCPostOrderIterator<'a> { pub struct HCPostOrderIterator<'a> {
base_iter: HCPreOrderIterator<'a>, base_iter: HCPreOrderIterator<'a>,
parent_stack: Vec<(usize, Addr)>, // number of children, parent node. 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> { pub struct HCAcyclicIterator<'a> {
iter: HCPreOrderIterator<'a>, iter: HCPreOrderIterator<'a>,
seen: IndexSet<Addr>, seen: IndexSet<Addr>,
@@ -269,6 +272,7 @@ impl<'a> Iterator for HCAcyclicIterator<'a>
} }
} }
#[derive(Debug)]
pub struct HCZippedAcyclicIterator<'a> { pub struct HCZippedAcyclicIterator<'a> {
i1: HCPreOrderIterator<'a>, i1: HCPreOrderIterator<'a>,
i2: HCPreOrderIterator<'a>, i2: HCPreOrderIterator<'a>,

View File

@@ -18,7 +18,7 @@ use std::ops::{Range, RangeFrom};
use std::rc::Rc; use std::rc::Rc;
/* contains the location, name, precision and Specifier of the parent op. */ /* contains the location, name, precision and Specifier of the parent op. */
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum DirectedOp { pub enum DirectedOp {
Left(ClauseName, SharedOpDesc), Left(ClauseName, SharedOpDesc),
Right(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 { enum TokenOrRedirect {
Atom(ClauseName), Atom(ClauseName),
BarAsOp, BarAsOp,
@@ -197,6 +197,7 @@ pub trait HCValueOutputter {
fn range_from(&self, range: RangeFrom<usize>) -> &str; fn range_from(&self, range: RangeFrom<usize>) -> &str;
} }
#[derive(Debug)]
pub struct PrinterOutputter { pub struct PrinterOutputter {
contents: String, contents: String,
} }
@@ -335,6 +336,7 @@ impl MachineState {
type ReverseHeapVarDict = IndexMap<Addr, Rc<Var>>; type ReverseHeapVarDict = IndexMap<Addr, Rc<Var>>;
#[derive(Debug)]
pub struct HCPrinter<'a, Outputter> { pub struct HCPrinter<'a, Outputter> {
outputter: Outputter, outputter: Outputter,
machine_st: &'a MachineState, machine_st: &'a MachineState,

View File

@@ -45,6 +45,7 @@ impl ArithmeticTerm {
} }
} }
#[derive(Debug)]
pub enum ChoiceInstruction { pub enum ChoiceInstruction {
DefaultRetryMeElse(usize), DefaultRetryMeElse(usize),
DefaultTrustMe, DefaultTrustMe,
@@ -75,6 +76,7 @@ impl ChoiceInstruction {
} }
} }
#[derive(Debug)]
pub enum CutInstruction { pub enum CutInstruction {
Cut(RegType), Cut(RegType),
GetLevel(RegType), GetLevel(RegType),
@@ -104,6 +106,7 @@ impl CutInstruction {
} }
} }
#[derive(Debug)]
pub enum IndexedChoiceInstruction { pub enum IndexedChoiceInstruction {
Retry(usize), Retry(usize),
Trust(usize), Trust(usize),
@@ -140,6 +143,7 @@ impl IndexedChoiceInstruction {
} }
} }
#[derive(Debug)]
pub enum Line { pub enum Line {
Arithmetic(ArithmeticInstruction), Arithmetic(ArithmeticInstruction),
Choice(ChoiceInstruction), Choice(ChoiceInstruction),
@@ -175,7 +179,7 @@ impl Line {
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum ArithmeticInstruction { pub enum ArithmeticInstruction {
Add(ArithmeticTerm, ArithmeticTerm, usize), Add(ArithmeticTerm, ArithmeticTerm, usize),
Sub(ArithmeticTerm, ArithmeticTerm, usize), Sub(ArithmeticTerm, ArithmeticTerm, usize),
@@ -374,6 +378,7 @@ impl ArithmeticInstruction {
} }
} }
#[derive(Debug)]
pub enum ControlInstruction { pub enum ControlInstruction {
Allocate(usize), // num_frames. Allocate(usize), // num_frames.
// name, arity, perm_vars after threshold, last call, use default call policy. // name, arity, perm_vars after threshold, last call, use default call policy.
@@ -419,6 +424,7 @@ impl ControlInstruction {
} }
} }
#[derive(Debug)]
pub enum IndexingInstruction { pub enum IndexingInstruction {
SwitchOnTerm(usize, usize, usize, usize), SwitchOnTerm(usize, usize, usize, usize),
SwitchOnConstant(usize, IndexMap<Constant, usize>), SwitchOnConstant(usize, IndexMap<Constant, usize>),
@@ -459,7 +465,7 @@ impl IndexingInstruction {
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum FactInstruction { pub enum FactInstruction {
GetConstant(Level, Constant, RegType), GetConstant(Level, Constant, RegType),
GetList(Level, RegType), GetList(Level, RegType),
@@ -571,7 +577,7 @@ impl FactInstruction {
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum QueryInstruction { pub enum QueryInstruction {
GetVariable(RegType, usize), GetVariable(RegType, usize),
PutConstant(Level, Constant, RegType), PutConstant(Level, Constant, RegType),

View File

@@ -6,11 +6,12 @@ use crate::prolog::machine::machine_indices::*;
use std::cell::Cell; use std::cell::Cell;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fmt;
use std::iter::*; use std::iter::*;
use std::rc::Rc; use std::rc::Rc;
use std::vec::Vec; use std::vec::Vec;
#[derive(Clone)] #[derive(Debug, Clone)]
pub enum TermRef<'a> { pub enum TermRef<'a> {
AnonVar(Level), AnonVar(Level),
Cons(Level, &'a Cell<RegType>, &'a Term, &'a Term), Cons(Level, &'a Cell<RegType>, &'a Term, &'a Term),
@@ -33,6 +34,7 @@ impl<'a> TermRef<'a> {
} }
} }
#[derive(Debug)]
pub enum TermIterState<'a> { pub enum TermIterState<'a> {
AnonVar(Level), AnonVar(Level),
Constant(Level, &'a Cell<RegType>, &'a Constant), Constant(Level, &'a Cell<RegType>, &'a Constant),
@@ -124,6 +126,7 @@ impl<'a> TermIterState<'a> {
} }
} }
#[derive(Debug)]
pub struct QueryIterator<'a> { pub struct QueryIterator<'a> {
state_stack: Vec<TermIterState<'a>>, state_stack: Vec<TermIterState<'a>>,
} }
@@ -294,6 +297,7 @@ impl<'a> Iterator for QueryIterator<'a> {
} }
} }
#[derive(Debug)]
pub struct FactIterator<'a> { pub struct FactIterator<'a> {
state_queue: VecDeque<TermIterState<'a>>, state_queue: VecDeque<TermIterState<'a>>,
iterable_root: bool, iterable_root: bool,
@@ -402,6 +406,7 @@ pub fn breadth_first_iter(term: &Term, iterable_root: bool) -> FactIterator {
FactIterator::new(term, iterable_root) FactIterator::new(term, iterable_root)
} }
#[derive(Debug)]
pub enum ChunkedTerm<'a> { pub enum ChunkedTerm<'a> {
HeadClause(ClauseName, &'a Vec<Box<Term>>), HeadClause(ClauseName, &'a Vec<Box<Term>>),
BodyTerm(&'a QueryTerm), BodyTerm(&'a QueryTerm),
@@ -439,6 +444,18 @@ pub struct ChunkedIterator<'a> {
cut_var_in_head: bool, 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<dyn Iterator<Item = ChunkedTerm<'a>> + '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<ChunkedTerm<'a>>); type ChunkedIteratorItem<'a> = (usize, usize, Vec<ChunkedTerm<'a>>);
type RuleBodyIteratorItem<'a> = (usize, usize, Vec<&'a QueryTerm>); type RuleBodyIteratorItem<'a> = (usize, usize, Vec<&'a QueryTerm>);

View File

@@ -47,7 +47,7 @@ extend_var_list(Value, VarList, NewVarList, VarType) :-
term_variables(Value, Vars), term_variables(Value, Vars),
extend_var_list_(Vars, 0, VarList, NewVarList, VarType). extend_var_list_(Vars, 0, VarList, NewVarList, VarType).
extend_var_list_([], N, VarList, VarList, _). extend_var_list_([], _, VarList, VarList, _).
extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :- extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :-
( var_list_contains_variable(VarList, V) -> ( var_list_contains_variable(VarList, V) ->
extend_var_list_(Vs, N, VarList, NewVarList, VarType) extend_var_list_(Vs, N, VarList, NewVarList, VarType)
@@ -60,36 +60,44 @@ extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :-
char_type(Char, Type) :- char_type(Char, Type) :-
( var(Char) -> throw(error(instantiation_error, char_type/2)) ( var(Char) -> throw(error(instantiation_error, char_type/2))
; atom_length(Char, 1) -> ; atom_length(Char, 1) ->
( ground(Type) -> '$char_type'(Char, Type) ( ground(Type) ->
; Type = symbolic_control, '$char_type'(Char, Type) ( ctype(Type) ->
; Type = layout, '$char_type'(Char, Type) '$char_type'(Char, Type)
; Type = symbolic_hexadecimal, Char = x ; throw(error(domain_error(char_type, Type), char_type/2))
; Type = octal_digit, '$char_type'(Char, Type) )
; Type = binary_digit, '$char_type'(Char, Type) ; ctype(Type),
; Type = hexadecimal_digit, '$char_type'(Char, Type) '$char_type'(Char, Type)
; Type = exponent, '$char_type'(Char, Type)
; Type = sign, '$char_type'(Char, Type)
; Type = upper, '$char_type'(Char, Type)
; Type = lower, '$char_type'(Char, Type)
; Type = graphic, '$char_type'(Char, Type)
; Type = alpha, '$char_type'(Char, Type)
; Type = decimal_digit, '$char_type'(Char, Type)
; Type = alnum, '$char_type'(Char, Type)
; Type = meta, '$char_type'(Char, Type)
; Type = solo, '$char_type'(Char, Type)
; Type = prolog, '$char_type'(Char, Type)
; Type = alphabetic, '$char_type'(Char, Type)
; Type = whitespace, '$char_type'(Char, Type)
; Type = control, '$char_type'(Char, Type)
; Type = numeric, '$char_type'(Char, Type)
; Type = ascii, '$char_type'(Char, Type)
; Type = ascii_punctuation, '$char_type'(Char, Type)
; Type = ascii_graphic, '$char_type'(Char, Type)
) )
; throw(error(type_error(in_character, Char), char_type/2)) ; throw(error(type_error(in_character, Char), char_type/2))
). ).
ctype(alnum).
ctype(alpha).
ctype(alphabetic).
ctype(ascii).
ctype(ascii_graphic).
ctype(ascii_punctuation).
ctype(binary_digit).
ctype(control).
ctype(decimal_digit).
ctype(exponent).
ctype(graphic).
ctype(hexadecimal_digit).
ctype(layout).
ctype(lower).
ctype(meta).
ctype(numeric).
ctype(octal_digit).
ctype(prolog).
ctype(sign).
ctype(solo).
ctype(symbolic_control).
ctype(symbolic_hexadecimal).
ctype(upper).
ctype(whitespace).
get_single_char(C) :- get_single_char(C) :-
( var(C) -> '$get_single_char'(C) ( var(C) -> '$get_single_char'(C)
; atom_length(C, 1) -> '$get_single_char'(C) ; atom_length(C, 1) -> '$get_single_char'(C)

11
src/prolog/lib/pio.pl Normal file
View File

@@ -0,0 +1,11 @@
:- module(pio, [phrase_from_file/2]).
:- use_module(library(dcgs)).
phrase_from_file(NT, File) :-
( var(File) -> throw(error(instantiation_error, phrase_from_file/2))
; (\+ atom(File) ; File = []) ->
throw(error(domain_error(source_sink, File), phrase_from_file/2))
; '$file_to_chars'(File, Chars),
phrase(NT, Chars)
).

View File

@@ -8,6 +8,7 @@ pub static PROJECT_ATTRS: &str = include_str!("project_attributes.pl");
pub(super) type Bindings = Vec<(usize, Addr)>; pub(super) type Bindings = Vec<(usize, Addr)>;
#[derive(Debug)]
pub(super) struct AttrVarInitializer { pub(super) struct AttrVarInitializer {
pub(super) attribute_goals: Vec<Addr>, pub(super) attribute_goals: Vec<Addr>,
pub(super) attr_var_queue: Vec<usize>, pub(super) attr_var_queue: Vec<usize>,

View File

@@ -12,6 +12,7 @@ use indexmap::IndexSet;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::mem; use std::mem;
#[derive(Debug)]
pub struct CodeRepo { pub struct CodeRepo {
pub(super) cached_query: Code, pub(super) cached_query: Code,
pub(super) goal_expanders: Code, pub(super) goal_expanders: Code,

View File

@@ -409,6 +409,7 @@ fn compile_into_module_impl(
Ok(compiler.drop_expansions(&mut wam.code_repo)) Ok(compiler.drop_expansions(&mut wam.code_repo))
} }
#[derive(Debug)]
pub struct GatherResult { pub struct GatherResult {
dynamic_clause_map: DynamicClauseMap, dynamic_clause_map: DynamicClauseMap,
pub(crate) worker_results: Vec<PredicateCompileQueue>, pub(crate) worker_results: Vec<PredicateCompileQueue>,
@@ -423,6 +424,7 @@ pub struct GatherResult {
in_situ_module_dir: ModuleStubDir, in_situ_module_dir: ModuleStubDir,
} }
#[derive(Debug)]
pub struct ClauseCodeGenerator { pub struct ClauseCodeGenerator {
len_offset: usize, len_offset: usize,
code: Code, code: Code,
@@ -529,6 +531,7 @@ fn insert_or_refresh_term_dir_quantum(
} }
} }
#[derive(Debug)]
pub struct ListingCompiler { pub struct ListingCompiler {
module: Option<Module>, module: Option<Module>,
user_term_dir: TermDir, user_term_dir: TermDir,

View File

@@ -7,7 +7,7 @@ use std::ops::IndexMut;
type Trail = Vec<(Ref, HeapCellValue)>; type Trail = Vec<(Ref, HeapCellValue)>;
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum AttrVarPolicy { pub enum AttrVarPolicy {
DeepCopy, DeepCopy,
StripAttributes StripAttributes
@@ -28,6 +28,7 @@ fn copy_term<T: CopierTarget>(target: T, addr: Addr, attr_var_policy: AttrVarPol
copy_term_state.copy_term_impl(addr); copy_term_state.copy_term_impl(addr);
} }
#[derive(Debug)]
struct CopyTermState<T: CopierTarget> { struct CopyTermState<T: CopierTarget> {
trail: Trail, trail: Trail,
scan: usize, scan: usize,

View File

@@ -11,6 +11,7 @@ use std::mem;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::ptr; use std::ptr;
#[derive(Debug)]
pub(crate) struct StandardHeapTraits {} pub(crate) struct StandardHeapTraits {}
impl RawBlockTraits for StandardHeapTraits { impl RawBlockTraits for StandardHeapTraits {
@@ -25,6 +26,7 @@ impl RawBlockTraits for StandardHeapTraits {
} }
} }
#[derive(Debug)]
pub(crate) struct HeapTemplate<T: RawBlockTraits> { pub(crate) struct HeapTemplate<T: RawBlockTraits> {
buf: RawBlock<T>, buf: RawBlock<T>,
_marker: PhantomData<HeapCellValue>, _marker: PhantomData<HeapCellValue>,
@@ -39,6 +41,7 @@ impl<T: RawBlockTraits> Drop for HeapTemplate<T> {
} }
} }
#[derive(Debug)]
pub(crate) pub(crate)
struct HeapIntoIter<T: RawBlockTraits> { struct HeapIntoIter<T: RawBlockTraits> {
offset: usize, offset: usize,
@@ -72,6 +75,7 @@ impl<T: RawBlockTraits> Iterator for HeapIntoIter<T> {
} }
} }
#[derive(Debug)]
pub(crate) pub(crate)
struct HeapIter<'a, T: RawBlockTraits> { struct HeapIter<'a, T: RawBlockTraits> {
offset: usize, offset: usize,
@@ -110,6 +114,7 @@ fn print_heap_terms<'a, I: Iterator<Item = &'a HeapCellValue>>(heap: I, h: usize
} }
} }
#[derive(Debug)]
pub(crate) pub(crate)
struct HeapIterMut<'a, T: RawBlockTraits> { struct HeapIterMut<'a, T: RawBlockTraits> {
offset: usize, offset: usize,

View File

@@ -10,12 +10,13 @@ use std::rc::Rc;
pub(crate) type MachineStub = Vec<HeapCellValue>; pub(crate) type MachineStub = Vec<HeapCellValue>;
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum ErrorProvenance { enum ErrorProvenance {
Constructed, // if constructed, offset the addresses. Constructed, // if constructed, offset the addresses.
Received, // otherwise, preserve the addresses. Received, // otherwise, preserve the addresses.
} }
#[derive(Debug)]
pub(super) struct MachineError { pub(super) struct MachineError {
stub: MachineStub, stub: MachineStub,
location: Option<(usize, usize)>, // line_num, col_num location: Option<(usize, usize)>, // line_num, col_num
@@ -447,7 +448,7 @@ impl MachineError {
} }
} }
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Permission { pub enum Permission {
Access, Access,
Create, Create,
@@ -469,7 +470,7 @@ impl Permission {
} }
// from 7.12.2 b) of 13211-1:1995 // from 7.12.2 b) of 13211-1:1995
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum ValidType { pub enum ValidType {
Atom, Atom,
Atomic, Atomic,
@@ -514,7 +515,7 @@ impl ValidType {
} }
} }
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum DomainErrorType { pub enum DomainErrorType {
NotLessThanZero, NotLessThanZero,
Order, Order,
@@ -534,7 +535,7 @@ impl DomainErrorType {
} }
// from 7.12.2 f) of 13211-1:1995 // from 7.12.2 f) of 13211-1:1995
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum RepFlag { pub enum RepFlag {
Character, Character,
CharacterCode, CharacterCode,
@@ -558,7 +559,7 @@ impl RepFlag {
} }
// from 7.12.2 g) of 13211-1:1995 // from 7.12.2 g) of 13211-1:1995
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum EvalError { pub enum EvalError {
FloatOverflow, FloatOverflow,
Undefined, Undefined,
@@ -578,7 +579,7 @@ impl EvalError {
} }
// used by '$skip_max_list'. // used by '$skip_max_list'.
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(super) enum CycleSearchResult { pub(super) enum CycleSearchResult {
EmptyList, EmptyList,
NotList, NotList,
@@ -722,6 +723,7 @@ impl MachineState {
} }
} }
#[derive(Debug)]
pub enum ExistenceError { pub enum ExistenceError {
Module(ClauseName), Module(ClauseName),
Procedure(ClauseName, usize), Procedure(ClauseName, usize),
@@ -729,6 +731,7 @@ pub enum ExistenceError {
Stream(Addr), Stream(Addr),
} }
#[derive(Debug)]
pub enum SessionError { pub enum SessionError {
CannotOverwriteBuiltIn(ClauseName), CannotOverwriteBuiltIn(ClauseName),
CannotOverwriteImport(ClauseName), CannotOverwriteImport(ClauseName),
@@ -741,6 +744,7 @@ pub enum SessionError {
ParserError(ParserError), ParserError(ParserError),
} }
#[derive(Debug)]
pub enum EvalSession { pub enum EvalSession {
EntrySuccess, EntrySuccess,
Error(SessionError), Error(SessionError),

View File

@@ -21,16 +21,17 @@ use std::cell::RefCell;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::{BTreeMap, VecDeque}; use std::collections::{BTreeMap, VecDeque};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt;
use std::mem; use std::mem;
use std::ops::{Add, AddAssign, Sub, SubAssign}; use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::rc::Rc; 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 struct OrderedOpDirKey(pub ClauseName, pub Fixity);
pub type OssifiedOpDir = BTreeMap<OrderedOpDirKey, (usize, Specifier)>; pub type OssifiedOpDir = BTreeMap<OrderedOpDirKey, (usize, Specifier)>;
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DBRef { pub enum DBRef {
NamedPred(ClauseName, usize, Option<SharedOpDesc>), NamedPred(ClauseName, usize, Option<SharedOpDesc>),
Op( Op(
@@ -43,7 +44,7 @@ pub enum DBRef {
} }
// 7.2 // 7.2
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TermOrderCategory { pub enum TermOrderCategory {
Variable, Variable,
FloatingPoint, FloatingPoint,
@@ -52,7 +53,7 @@ pub enum TermOrderCategory {
Compound, Compound,
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Addr { pub enum Addr {
AttrVar(usize), AttrVar(usize),
Char(char), Char(char),
@@ -71,7 +72,7 @@ pub enum Addr {
Usize(usize), Usize(usize),
} }
#[derive(Clone, Copy, Hash, Eq, PartialEq, PartialOrd)] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, PartialOrd)]
pub enum Ref { pub enum Ref {
AttrVar(usize), AttrVar(usize),
HeapCell(usize), HeapCell(usize),
@@ -361,7 +362,7 @@ impl SubAssign<usize> for Addr {
} }
} }
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum TrailRef { pub enum TrailRef {
Ref(Ref), Ref(Ref),
AttrVarHeapLink(usize), AttrVarHeapLink(usize),
@@ -374,6 +375,7 @@ impl From<Ref> for TrailRef {
} }
} }
#[derive(Debug)]
pub enum HeapCellValue { pub enum HeapCellValue {
Addr(Addr), Addr(Addr),
Atom(ClauseName, Option<SharedOpDesc>), Atom(ClauseName, Option<SharedOpDesc>),
@@ -446,7 +448,7 @@ impl From<Addr> for HeapCellValue {
} }
} }
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum IndexPtr { pub enum IndexPtr {
DynamicUndefined, // a predicate, declared as dynamic, whose location in code is as yet undefined. DynamicUndefined, // a predicate, declared as dynamic, whose location in code is as yet undefined.
Undefined, Undefined,
@@ -456,7 +458,7 @@ pub enum IndexPtr {
UserTermExpansion UserTermExpansion
} }
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct CodeIndex(pub Rc<RefCell<(IndexPtr, ClauseName)>>); pub struct CodeIndex(pub Rc<RefCell<(IndexPtr, ClauseName)>>);
impl CodeIndex { impl CodeIndex {
@@ -511,7 +513,7 @@ impl From<(usize, ClauseName)> for CodeIndex {
} }
} }
#[derive(Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum DynamicAssertPlace { pub enum DynamicAssertPlace {
Back, Back,
Front, Front,
@@ -535,7 +537,7 @@ impl DynamicAssertPlace {
} }
} }
#[derive(Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum DynamicTransactionType { pub enum DynamicTransactionType {
Abolish, Abolish,
Assert(DynamicAssertPlace), Assert(DynamicAssertPlace),
@@ -545,7 +547,7 @@ pub enum DynamicTransactionType {
Retract, // dynamic index of the clause to remove. 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 { pub enum REPLCodePtr {
CompileBatch, CompileBatch,
UseModule, UseModule,
@@ -554,7 +556,7 @@ pub enum REPLCodePtr {
UseQualifiedModuleFromFile UseQualifiedModuleFromFile
} }
#[derive(Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum CodePtr { pub enum CodePtr {
BuiltInClause(BuiltInClauseType, LocalCodePtr), // local is the successor call. BuiltInClause(BuiltInClauseType, LocalCodePtr), // local is the successor call.
CallN(usize, LocalCodePtr, bool), // arity, local, last call. CallN(usize, LocalCodePtr, bool), // arity, local, last call.
@@ -773,7 +775,7 @@ impl AddAssign<usize> for CodePtr {
pub type HeapVarDict = IndexMap<Rc<Var>, Addr>; pub type HeapVarDict = IndexMap<Rc<Var>, Addr>;
pub type AllocVarDict = IndexMap<Rc<Var>, VarData>; pub type AllocVarDict = IndexMap<Rc<Var>, VarData>;
#[derive(Clone)] #[derive(Debug, Clone)]
pub struct DynamicPredicateInfo { pub struct DynamicPredicateInfo {
pub(super) clauses_subsection_p: usize, // a LocalCodePtr::DirEntry value. 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<ClauseName, (Ball, Option<usize>)>; pub type GlobalVarDir = IndexMap<ClauseName, (Ball, Option<usize>)>;
#[derive(Debug)]
pub(crate) struct ModuleStub { pub(crate) struct ModuleStub {
pub(crate) atom_tbl: TabledData<Atom>, pub(crate) atom_tbl: TabledData<Atom>,
pub(crate) in_situ_code_dir: InSituCodeDir, pub(crate) in_situ_code_dir: InSituCodeDir,
@@ -810,6 +813,7 @@ impl ModuleStub {
pub(crate) type ModuleStubDir = IndexMap<ClauseName, ModuleStub>; pub(crate) type ModuleStubDir = IndexMap<ClauseName, ModuleStub>;
pub(crate) type StreamAliasDir = IndexMap<ClauseName, Stream>; pub(crate) type StreamAliasDir = IndexMap<ClauseName, Stream>;
#[derive(Debug)]
pub struct IndexStore { pub struct IndexStore {
pub(super) atom_tbl: TabledData<Atom>, pub(super) atom_tbl: TabledData<Atom>,
pub(super) code_dir: CodeDir, pub(super) code_dir: CodeDir,
@@ -960,6 +964,7 @@ impl IndexStore {
pub type CodeDir = BTreeMap<PredicateKey, CodeIndex>; pub type CodeDir = BTreeMap<PredicateKey, CodeIndex>;
pub type TermDir = IndexMap<PredicateKey, (Predicate, VecDeque<TopLevel>)>; pub type TermDir = IndexMap<PredicateKey, (Predicate, VecDeque<TopLevel>)>;
#[derive(Debug)]
pub struct TermDirQuantumEntry { pub struct TermDirQuantumEntry {
pub old_terms: (Predicate, VecDeque<TopLevel>), pub old_terms: (Predicate, VecDeque<TopLevel>),
pub new_terms: (Predicate, VecDeque<TopLevel>), pub new_terms: (Predicate, VecDeque<TopLevel>),
@@ -988,6 +993,7 @@ impl TermDirQuantumEntry {
} }
} }
#[derive(Debug)]
pub struct TermDirQuantum(IndexMap<PredicateKey, TermDirQuantumEntry>); pub struct TermDirQuantum(IndexMap<PredicateKey, TermDirQuantumEntry>);
impl TermDirQuantum { 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 { pub enum CompileTimeHook {
GoalExpansion, GoalExpansion,
TermExpansion, TermExpansion,
@@ -1085,6 +1091,16 @@ pub enum RefOrOwned<'a, T: 'a> {
Owned(T), 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> { impl<'a, T> RefOrOwned<'a, T> {
pub fn as_ref(&'a self) -> &'a T { pub fn as_ref(&'a self) -> &'a T {
match self { match self {

View File

@@ -21,10 +21,12 @@ use indexmap::{IndexMap, IndexSet};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt;
use std::io::Write; use std::io::Write;
use std::mem; use std::mem;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
#[derive(Debug)]
pub(crate) struct HeapPStrIter<'a> { pub(crate) struct HeapPStrIter<'a> {
focus: Addr, focus: Addr,
machine_st: &'a MachineState, machine_st: &'a MachineState,
@@ -74,7 +76,7 @@ impl<'a> HeapPStrIter<'a> {
} }
} }
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(crate) enum PStrIteratee { pub(crate) enum PStrIteratee {
Char(char), Char(char),
PStrSegment(usize, usize), PStrSegment(usize, usize),
@@ -306,6 +308,7 @@ fn compare_pstr_to_string<'a>(
Some(s_offset) Some(s_offset)
} }
#[derive(Debug)]
pub struct Ball { pub struct Ball {
pub(super) boundary: usize, pub(super) boundary: usize,
pub(super) stub: Heap, pub(super) stub: Heap,
@@ -357,6 +360,7 @@ impl Ball {
} }
} }
#[derive(Debug)]
pub(super) struct CopyTerm<'a> { pub(super) struct CopyTerm<'a> {
state: &'a mut MachineState, state: &'a mut MachineState,
} }
@@ -404,6 +408,7 @@ impl<'a> CopierTarget for CopyTerm<'a> {
} }
} }
#[derive(Debug)]
pub(super) struct CopyBallTerm<'a> { pub(super) struct CopyBallTerm<'a> {
stack: &'a mut Stack, stack: &'a mut Stack,
heap: &'a mut Heap, heap: &'a mut Heap,
@@ -529,13 +534,13 @@ impl IndexMut<RegType> for MachineState {
pub type Registers = Vec<Addr>; pub type Registers = Vec<Addr>;
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(super) enum MachineMode { pub(super) enum MachineMode {
Read, Read,
Write, Write,
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub(super) enum HeapPtr { pub(super) enum HeapPtr {
HeapCell(usize), HeapCell(usize),
PStrChar(usize, usize), PStrChar(usize, usize),
@@ -576,6 +581,7 @@ impl Default for HeapPtr {
} }
} }
#[derive(Debug)]
pub struct MachineState { pub struct MachineState {
pub(super) s: HeapPtr, pub(super) s: HeapPtr,
pub(super) p: CodePtr, pub(super) p: CodePtr,
@@ -934,7 +940,7 @@ fn try_in_situ(
pub(crate) type CallResult = Result<(), Vec<HeapCellValue>>; pub(crate) type CallResult = Result<(), Vec<HeapCellValue>>;
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 { fn retry_me_else(&mut self, machine_st: &mut MachineState, offset: usize) -> CallResult {
let b = machine_st.b; let b = machine_st.b;
let n = machine_st.stack.index_or_frame(b).prelude.univ_prelude.num_cells; 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); downcast!(dyn CallPolicy);
#[derive(Debug)]
pub(crate) struct DefaultCallPolicy {} pub(crate) struct DefaultCallPolicy {}
impl CallPolicy for DefaultCallPolicy {} impl CallPolicy for DefaultCallPolicy {}
#[derive(Debug)]
pub(crate) struct CWILCallPolicy { pub(crate) struct CWILCallPolicy {
pub(crate) prev_policy: Box<dyn CallPolicy>, pub(crate) prev_policy: Box<dyn CallPolicy>,
count: Integer, 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 // returns true iff we fail or cut redirected the MachineState's p itself
fn cut(&mut self, machine_st: &mut MachineState, r: RegType) -> bool; 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 false
} }
#[derive(Debug)]
pub(crate) struct DefaultCutPolicy {} pub(crate) struct DefaultCutPolicy {}
pub(super) fn deref_cut(machine_st: &mut MachineState, r: RegType) { 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 { pub(crate) struct SCCCutPolicy {
// locations of cleaners, cut points, the previous block // locations of cleaners, cut points, the previous block
cont_pts: Vec<(Addr, usize, usize)>, cont_pts: Vec<(Addr, usize, usize)>,

View File

@@ -53,6 +53,7 @@ use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
#[derive(Debug)]
pub struct MachinePolicies { pub struct MachinePolicies {
call_policy: Box<dyn CallPolicy>, call_policy: Box<dyn CallPolicy>,
cut_policy: Box<dyn CutPolicy>, cut_policy: Box<dyn CutPolicy>,
@@ -79,6 +80,7 @@ impl Default for MachinePolicies {
} }
} }
#[derive(Debug)]
pub struct Machine { pub struct Machine {
pub(super) machine_st: MachineState, pub(super) machine_st: MachineState,
pub(super) inner_heap: Heap, pub(super) inner_heap: Heap,

View File

@@ -7,6 +7,7 @@ use std::ops::RangeFrom;
use std::slice; use std::slice;
use std::str; use std::str;
#[derive(Debug)]
pub struct PartialString { pub struct PartialString {
buf: *const u8, buf: *const u8,
len: usize, len: usize,
@@ -46,6 +47,7 @@ fn scan_for_terminator<Iter: Iterator<Item = char>>(iter: Iter) -> usize {
terminator_idx terminator_idx
} }
#[derive(Debug)]
pub struct PStrIter { pub struct PStrIter {
buf: *const u8, buf: *const u8,
len: usize, len: usize,

View File

@@ -14,6 +14,7 @@ pub(crate) trait RawBlockTraits {
} }
} }
#[derive(Debug)]
pub(crate) struct RawBlock<T: RawBlockTraits> { pub(crate) struct RawBlock<T: RawBlockTraits> {
pub(crate) size: usize, pub(crate) size: usize,
pub(crate) base: *const u8, pub(crate) base: *const u8,

View File

@@ -7,6 +7,7 @@ use std::mem;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::ptr; use std::ptr;
#[derive(Debug)]
struct StackTraits {} struct StackTraits {}
impl RawBlockTraits for StackTraits { impl RawBlockTraits for StackTraits {
@@ -35,6 +36,7 @@ const fn prelude_size<Prelude>() -> usize {
(size & !(align - 1)) + align (size & !(align - 1)) + align
} }
#[derive(Debug)]
pub struct Stack { pub struct Stack {
buf: RawBlock<StackTraits>, buf: RawBlock<StackTraits>,
_marker: PhantomData<Addr>, _marker: PhantomData<Addr>,
@@ -47,11 +49,12 @@ impl Drop for Stack {
} }
} }
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct FramePrelude { pub struct FramePrelude {
pub num_cells: usize, pub num_cells: usize,
} }
#[derive(Debug)]
pub struct AndFramePrelude { pub struct AndFramePrelude {
pub univ_prelude: FramePrelude, pub univ_prelude: FramePrelude,
pub e: usize, pub e: usize,
@@ -59,6 +62,7 @@ pub struct AndFramePrelude {
pub interrupt_cp: LocalCodePtr, pub interrupt_cp: LocalCodePtr,
} }
#[derive(Debug)]
pub struct AndFrame { pub struct AndFrame {
pub prelude: AndFramePrelude, pub prelude: AndFramePrelude,
} }
@@ -99,6 +103,7 @@ impl IndexMut<usize> for AndFrame {
} }
} }
#[derive(Debug)]
pub struct OrFramePrelude { pub struct OrFramePrelude {
pub univ_prelude: FramePrelude, pub univ_prelude: FramePrelude,
pub e: usize, pub e: usize,
@@ -113,6 +118,7 @@ pub struct OrFramePrelude {
pub attr_var_init_bindings_b: usize, pub attr_var_init_bindings_b: usize,
} }
#[derive(Debug)]
pub struct OrFrame { pub struct OrFrame {
pub prelude: OrFramePrelude, pub prelude: OrFramePrelude,
} }

View File

@@ -11,13 +11,13 @@ use std::hash::{Hash, Hasher};
use std::net::TcpStream; use std::net::TcpStream;
use std::rc::Rc; use std::rc::Rc;
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StreamType { pub enum StreamType {
Binary, Binary,
Text, Text,
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EOFAction { pub enum EOFAction {
EOFCode, EOFCode,
Error, Error,
@@ -37,7 +37,26 @@ pub enum StreamInstance {
TcpStream(TcpStream), 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<RefCell<StreamInstance>>); struct WrappedStreamInstance(Rc<RefCell<StreamInstance>>);
impl WrappedStreamInstance { impl WrappedStreamInstance {
@@ -95,7 +114,7 @@ impl fmt::Display for StreamError {
impl Error for StreamError {} impl Error for StreamError {}
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StreamOptions { pub struct StreamOptions {
pub stream_type: StreamType, pub stream_type: StreamType,
pub reposition: bool, 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 struct Stream {
pub options: StreamOptions, pub options: StreamOptions,
stream_inst: WrappedStreamInstance, stream_inst: WrappedStreamInstance,

View File

@@ -24,8 +24,9 @@ use indexmap::IndexSet;
use std::cmp; use std::cmp;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::io::{stdout, Write}; use std::io::{stdout, Read, Write};
use std::iter::once; use std::iter::once;
use std::fs::File;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration; use std::time::Duration;
@@ -60,6 +61,7 @@ pub fn get_single_char() -> char {
c c
} }
#[derive(Debug)]
struct BrentAlgState { struct BrentAlgState {
hare: Addr, hare: Addr,
tortoise: Addr, tortoise: Addr,
@@ -1519,6 +1521,90 @@ impl MachineState {
} }
}; };
} }
&SystemClauseType::FileToChars => {
// TODO: Replace this with stream.
use std::io;
let a1 = self.store(self.deref(self[temp_v!(1)]));
let a2 = self.store(self.deref(self[temp_v!(2)]));
let file_name = match a1 {
Addr::Con(h) if self.heap.atom_at(h) => {
if let HeapCellValue::Atom(name, _) = &self.heap[h] {
name.as_str().to_string()
}
else {
unreachable!()
}
}
Addr::Char(c) => {
c.to_string()
}
_ => unreachable!()
};
let name = clause_name!("$file_to_chars");
let mut file = match File::open(&file_name) {
Ok(f) => f,
Err(e) => {
let file_name_ = clause_name!(file_name.clone(),
indices.atom_tbl.clone());
let arity = 2;
let stub = MachineError::functor_stub(name.clone(), arity);
let h = self.heap.h();
let err = match e.kind() {
io::ErrorKind::NotFound => {
MachineError::existence_error(
h,
ExistenceError::SourceSink(
ModuleSource::File(file_name_)
),
)
}
io::ErrorKind::PermissionDenied => {
let stub = MachineError::functor_stub(
name.clone(),
arity
);
let source_sink = self.store(self.deref(a1));
MachineError::permission_error(
h,
Permission::Access,
"source_sink",
source_sink
)
}
_ => unreachable!() // Not nice.
};
let err = self.error_form(err, stub);
self.throw_exception(err);
return Ok(());
}
};
let char_list = {
let mut buffer = String::new();
match file.read_to_string(&mut buffer) {
Ok(_size) => {
let chars = buffer.chars().map(|c| Addr::Char(c));
Addr::HeapCell(self.heap.to_list(chars))
}
Err(_e) => {
// This case if the data isn't UTF-8 valid.
let mut buffer = Vec::new();
let _ = match file.read_to_end(&mut buffer) {
Ok(size) => size,
Err(_e) => unreachable!()
};
let chars = buffer
.into_iter()
.map(|b| Addr::Char(b as char));
Addr::HeapCell(self.heap.to_list(chars))
}
}
};
self.unify(char_list, a2);
}
&SystemClauseType::GetChar => { &SystemClauseType::GetChar => {
let mut iter = parsing_stream(current_input_stream.clone()); let mut iter = parsing_stream(current_input_stream.clone());
let result = iter.next(); let result = iter.next();

View File

@@ -47,6 +47,7 @@ fn extract_from_list(
} }
} }
#[derive(Debug)]
pub struct TermStream<'a> { pub struct TermStream<'a> {
stack: Vec<Term>, stack: Vec<Term>,
pub(crate) wam: &'a mut Machine, 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. top_level_terms: Vec<(Term, usize, usize)>, // term, line_num, col_num.
} }
#[derive(Debug)]
pub struct ExpansionAdditionResult { pub struct ExpansionAdditionResult {
term_expansion_additions: (Predicate, VecDeque<TopLevel>), term_expansion_additions: (Predicate, VecDeque<TopLevel>),
goal_expansion_additions: (Predicate, VecDeque<TopLevel>), goal_expansion_additions: (Predicate, VecDeque<TopLevel>),

View File

@@ -14,6 +14,7 @@ use std::borrow::BorrowMut;
use std::cell::Cell; use std::cell::Cell;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt;
use std::mem; use std::mem;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::rc::Rc; use std::rc::Rc;
@@ -23,6 +24,15 @@ enum IndexSource<'a, T> {
Local(&'a mut 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> { fn op_dir<'a, 'b: 'a>(from: &'b IndexSource<'a, IndexStore>) -> RefOrOwned<'a, OpDir> {
match from { match from {
IndexSource::TermStream => RefOrOwned::Owned(OpDir::new()), 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> { struct CompositeIndices<'a, 'b, 'c> {
term_stream: &'b mut TermStream<'a>, term_stream: &'b mut TermStream<'a>,
index_src: IndexSource<'c, IndexStore>, index_src: IndexSource<'c, IndexStore>,
@@ -690,6 +701,7 @@ fn setup_declaration<'a, 'b, 'c>(
} }
} }
#[derive(Debug)]
struct RelationWorker { struct RelationWorker {
flags: MachineFlags, flags: MachineFlags,
dynamic_clauses: Vec<(Term, Term)>, // Head, Body. dynamic_clauses: Vec<(Term, Term)>, // Head, Body.
@@ -1141,6 +1153,7 @@ pub type DynamicClause = Vec<(Term, Term)>;
pub type DynamicClauseMap = IndexMap<(ClauseName, usize), DynamicClause>; pub type DynamicClauseMap = IndexMap<(ClauseName, usize), DynamicClause>;
#[derive(Debug)]
pub struct TopLevelBatchWorker<'a> { pub struct TopLevelBatchWorker<'a> {
pub(crate) term_stream: TermStream<'a>, pub(crate) term_stream: TermStream<'a>,
rel_worker: RelationWorker, rel_worker: RelationWorker,

View File

@@ -35,6 +35,7 @@ pub mod readline {
} }
} }
#[derive(Debug)]
pub struct ReadlineStream { pub struct ReadlineStream {
rl: Editor<()>, rl: Editor<()>,
pending_input: Cursor<String>, pending_input: Cursor<String>,
@@ -114,12 +115,14 @@ fn write_term_to_heap(term: &Term, machine_st: &mut MachineState) -> TermWriteRe
term_writer.write_term_to_heap(term) term_writer.write_term_to_heap(term)
} }
#[derive(Debug)]
struct TermWriter<'a> { struct TermWriter<'a> {
machine_st: &'a mut MachineState, machine_st: &'a mut MachineState,
queue: SubtermDeque, queue: SubtermDeque,
var_dict: HeapVarDict, var_dict: HeapVarDict,
} }
#[derive(Debug)]
pub struct TermWriteResult { pub struct TermWriteResult {
pub(crate) heap_loc: usize, pub(crate) heap_loc: usize,
pub(crate) var_dict: HeapVarDict, pub(crate) var_dict: HeapVarDict,