add randomness predicates, small but consequential changes to TrailRef
This commit is contained in:
@@ -178,10 +178,11 @@ impl From<Ref> for Addr {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum TrailRef {
|
||||
Ref(Ref),
|
||||
AttrVarLink(usize, Addr),
|
||||
AttrVarHeapLink(usize),
|
||||
AttrVarListLink(usize, usize),
|
||||
}
|
||||
|
||||
impl From<Ref> for TrailRef {
|
||||
|
||||
@@ -648,9 +648,15 @@ impl MachineState {
|
||||
self.tr += 1;
|
||||
}
|
||||
}
|
||||
TrailRef::AttrVarLink(h, prev_addr) => {
|
||||
TrailRef::AttrVarHeapLink(h) => {
|
||||
if h < self.hb {
|
||||
self.trail.push(TrailRef::AttrVarLink(h, prev_addr));
|
||||
self.trail.push(TrailRef::AttrVarHeapLink(h));
|
||||
self.tr += 1;
|
||||
}
|
||||
}
|
||||
TrailRef::AttrVarListLink(h, l) => {
|
||||
if h < self.hb {
|
||||
self.trail.push(TrailRef::AttrVarListLink(h, l));
|
||||
self.tr += 1;
|
||||
}
|
||||
}
|
||||
@@ -680,7 +686,7 @@ impl MachineState {
|
||||
// additions, now that deleted attributes can be undeleted by
|
||||
// backtracking.
|
||||
for i in (a1..a2).rev() {
|
||||
match self.trail[i].clone() {
|
||||
match self.trail[i] {
|
||||
TrailRef::Ref(Ref::HeapCell(h)) => {
|
||||
self.heap[h] = HeapCellValue::Addr(Addr::HeapCell(h))
|
||||
}
|
||||
@@ -690,8 +696,11 @@ impl MachineState {
|
||||
TrailRef::Ref(Ref::StackCell(fr, sc)) => {
|
||||
self.and_stack[fr][sc] = Addr::StackCell(fr, sc)
|
||||
}
|
||||
TrailRef::AttrVarLink(h, prev_addr) => {
|
||||
self.heap[h] = HeapCellValue::Addr(prev_addr)
|
||||
TrailRef::AttrVarHeapLink(h) => {
|
||||
self.heap[h] = HeapCellValue::Addr(Addr::HeapCell(h));
|
||||
}
|
||||
TrailRef::AttrVarListLink(h, l) => {
|
||||
self.heap[h] = HeapCellValue::Addr(Addr::Lis(l));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -735,13 +744,14 @@ impl MachineState {
|
||||
let mut i = self.or_stack[b].tr;
|
||||
|
||||
while i < self.tr {
|
||||
let tr_i = self.trail[i].clone();
|
||||
let tr_i = self.trail[i];
|
||||
let hb = self.hb;
|
||||
|
||||
match tr_i {
|
||||
TrailRef::Ref(Ref::AttrVar(tr_i))
|
||||
| TrailRef::Ref(Ref::HeapCell(tr_i))
|
||||
| TrailRef::AttrVarLink(tr_i, _) => {
|
||||
| TrailRef::Ref(Ref::HeapCell(tr_i))
|
||||
| TrailRef::AttrVarHeapLink(tr_i)
|
||||
| TrailRef::AttrVarListLink(tr_i, _) => {
|
||||
if tr_i < hb {
|
||||
i += 1;
|
||||
} else {
|
||||
@@ -765,7 +775,7 @@ impl MachineState {
|
||||
i += 1;
|
||||
} else {
|
||||
let tr = self.tr;
|
||||
let val = self.trail[tr - 1].clone();
|
||||
let val = self.trail[tr - 1];
|
||||
self.trail[i] = val;
|
||||
self.trail.pop();
|
||||
self.tr -= 1;
|
||||
|
||||
@@ -17,6 +17,8 @@ use crate::prolog::ordered_float::OrderedFloat;
|
||||
use crate::prolog::read::{readline, PrologStream};
|
||||
use crate::prolog::rug::Integer;
|
||||
|
||||
use crate::ref_thread_local::RefThreadLocal;
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
|
||||
use std::collections::VecDeque;
|
||||
@@ -679,7 +681,8 @@ impl MachineState {
|
||||
let c = self.int_to_char_code(&n, "atom_codes", 2)?;
|
||||
chars.push(c as char);
|
||||
}
|
||||
&Addr::Con(Constant::CharCode(c)) => chars.push(c as char),
|
||||
&Addr::Con(Constant::CharCode(c)) =>
|
||||
chars.push(c as char),
|
||||
_ => {
|
||||
let err = MachineError::type_error(
|
||||
ValidType::Integer,
|
||||
@@ -1018,8 +1021,14 @@ impl MachineState {
|
||||
tail
|
||||
};
|
||||
|
||||
let trail_ref = match old_addr {
|
||||
Addr::HeapCell(h) => TrailRef::AttrVarHeapLink(h),
|
||||
Addr::Lis(l) => TrailRef::AttrVarListLink(l1 + 1, l),
|
||||
_ => unreachable!()
|
||||
};
|
||||
|
||||
self.heap[l1 + 1] = HeapCellValue::Addr(tail);
|
||||
self.trail(TrailRef::AttrVarLink(l1 + 1, old_addr));
|
||||
self.trail(trail_ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1041,7 +1050,7 @@ impl MachineState {
|
||||
};
|
||||
|
||||
self.heap[h + 1] = HeapCellValue::Addr(tail);
|
||||
self.trail(TrailRef::AttrVarLink(h + 1, Addr::Lis(l)));
|
||||
self.trail(TrailRef::AttrVarListLink(h + 1, l));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -1255,6 +1264,19 @@ impl MachineState {
|
||||
_ => self.fail = true,
|
||||
}
|
||||
}
|
||||
&SystemClauseType::Maybe => {
|
||||
let result = {
|
||||
let mut rand = RANDOM_STATE.borrow_mut();
|
||||
|
||||
if rand.bits(1) == 0 {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
self.fail = result;
|
||||
}
|
||||
&SystemClauseType::OpDeclaration => {
|
||||
let priority = self[temp_v!(1)].clone();
|
||||
let specifier = self[temp_v!(2)].clone();
|
||||
@@ -1834,6 +1856,31 @@ impl MachineState {
|
||||
}
|
||||
&SystemClauseType::SetBall =>
|
||||
self.set_ball(),
|
||||
&SystemClauseType::SetSeed => {
|
||||
let seed = self.store(self.deref(self[temp_v!(1)].clone()));
|
||||
|
||||
let seed = match seed {
|
||||
Addr::Con(Constant::Integer(n)) =>
|
||||
n,
|
||||
Addr::Con(Constant::CharCode(c)) =>
|
||||
Integer::from(c),
|
||||
Addr::Con(Constant::Rational(r)) => {
|
||||
if r.denom() == &1 {
|
||||
r.numer().clone()
|
||||
} else {
|
||||
self.fail = true;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.fail = true;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let mut rand = RANDOM_STATE.borrow_mut();
|
||||
rand.seed(&seed);
|
||||
}
|
||||
&SystemClauseType::SkipMaxList =>
|
||||
if let Err(err) = self.skip_max_list() {
|
||||
return Err(err);
|
||||
|
||||
Reference in New Issue
Block a user