clean up detect_cycles

This commit is contained in:
Mark Thom
2018-05-01 10:52:57 -06:00
parent 0458a70527
commit d51c950b77
2 changed files with 12 additions and 33 deletions

View File

@@ -1360,13 +1360,6 @@ impl Addr {
_ => true _ => true
} }
} }
pub fn is_empty_list(&self) -> bool {
match self {
&Addr::Con(Constant::EmptyList) => true,
_ => false
}
}
} }
impl From<Ref> for Addr { impl From<Ref> for Addr {

View File

@@ -30,8 +30,9 @@ macro_rules! try_or_fail {
enum CycleSearchResult { enum CycleSearchResult {
EmptyList, EmptyList,
NotList, NotList,
PartialOrProperList(usize, usize), // returns the list length (up to max), and an offset into the heap. PartialList(usize, usize), // the list length (up to max), and an offset into the heap.
UntouchedList(usize) // return the offset of an uniterated Addr::Lis(offset). ProperList(usize), // the list length.
UntouchedList(usize) // the address of an uniterated Addr::Lis(address).
} }
impl MachineState { impl MachineState {
@@ -1676,8 +1677,7 @@ impl MachineState {
// detect cycles. // detect cycles.
match self.detect_cycles(usize::max_value(), a1.clone()) { match self.detect_cycles(usize::max_value(), a1.clone()) {
CycleSearchResult::PartialOrProperList(_, h) CycleSearchResult::ProperList(_) => {},
if self.store(self.deref(self.heap[h].as_addr(h))).is_empty_list() => {},
_ => return Err(functor!("type_error", 2, [heap_atom!("list"), HeapCellValue::Addr(a1)])) _ => return Err(functor!("type_error", 2, [heap_atom!("list"), HeapCellValue::Addr(a1)]))
}; };
@@ -1742,7 +1742,7 @@ impl MachineState {
loop { loop {
if steps == max_steps { if steps == max_steps {
return CycleSearchResult::PartialOrProperList(steps, hare); return CycleSearchResult::PartialList(steps, hare);
} }
match self.heap[hare].clone() { match self.heap[hare].clone() {
@@ -1758,14 +1758,8 @@ impl MachineState {
} }
}, },
HeapCellValue::Addr(Addr::Con(Constant::EmptyList)) => HeapCellValue::Addr(Addr::Con(Constant::EmptyList)) =>
return CycleSearchResult::PartialOrProperList(steps, hare), return CycleSearchResult::ProperList(steps),
HeapCellValue::Addr(ref hc @ Addr::HeapCell(_)) _ => return CycleSearchResult::PartialList(steps, hare)
if Addr::HeapCell(hare) == self.store(self.deref(hc.clone())) =>
return CycleSearchResult::PartialOrProperList(steps, hare),
HeapCellValue::Addr(ref sc @ Addr::StackCell(..))
if *sc == self.store(self.deref(sc.clone())) =>
return CycleSearchResult::PartialOrProperList(steps, hare),
_ => return CycleSearchResult::NotList
} }
} }
} }
@@ -1780,16 +1774,6 @@ impl MachineState {
} }
} }
/*
'$skip_max_list'(N, Max, Xs0, Xs):
valid modes: Max is always +Max (a non-negative integer), Xs, Xs0 and N are all ?_.
Modes | Conditions for success
======================================================================================
?N, -Xs0 : N = 0, Xs = Xs0.
?N, +Xs0 : Xs0 is a proper or partial list, Xs0 = [X1, X2, ..., XN | Xs], N = Max,
if |Xs0| >= Max, or, Xs = Xs0 and N = |Xs0|.
*/
pub(super) fn skip_max_list(&mut self) { pub(super) fn skip_max_list(&mut self) {
let max = self.store(self.deref(self[temp_v!(2)].clone())); let max = self.store(self.deref(self[temp_v!(2)].clone()));
@@ -1813,8 +1797,10 @@ impl MachineState {
self.finalize_skip_max_list(0, Addr::Lis(l)), self.finalize_skip_max_list(0, Addr::Lis(l)),
CycleSearchResult::EmptyList => CycleSearchResult::EmptyList =>
self.finalize_skip_max_list(0, Addr::Con(Constant::EmptyList)), self.finalize_skip_max_list(0, Addr::Con(Constant::EmptyList)),
CycleSearchResult::PartialOrProperList(n, hc) => CycleSearchResult::PartialList(n, hc) =>
self.finalize_skip_max_list(n, Addr::HeapCell(hc)), self.finalize_skip_max_list(n, Addr::HeapCell(hc)),
CycleSearchResult::ProperList(n) =>
self.finalize_skip_max_list(n, Addr::Con(Constant::EmptyList)),
CycleSearchResult::NotList => { CycleSearchResult::NotList => {
let xs0 = self[temp_v!(3)].clone(); let xs0 = self[temp_v!(3)].clone();
self.finalize_skip_max_list(0, xs0); self.finalize_skip_max_list(0, xs0);