From: Mark Date: Tue, 24 Oct 2023 18:23:05 +0000 (-0600) Subject: fix cycle detection in lists by looking backward analogously to the same process... X-Git-Tag: remove~11 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=4ab3e23b1f47a5c42b5e3bf3afb921d2cc101d05;p=scryer-prolog.git fix cycle detection in lists by looking backward analogously to the same process for structures (#2130) --- diff --git a/src/machine/cycle_detection.rs b/src/machine/cycle_detection.rs index 03cf9834..342dbdec 100644 --- a/src/machine/cycle_detection.rs +++ b/src/machine/cycle_detection.rs @@ -186,6 +186,15 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { self.heap[last_cell_loc].set_mark_bit(self.mark_phase); } + if self.cycle_detection_active() { + for idx in (self.next as usize .. last_cell_loc).rev() { + if self.heap[idx].get_forwarding_bit() { + self.cycle_found = true; + return None; + } + } + } + self.heap[last_cell_loc].set_forwarding_bit(true); self.next = self.heap[last_cell_loc].get_value(); diff --git a/src/tests/acyclic_term.pl b/src/tests/acyclic_term.pl index 9c08d9c8..3c2b9377 100644 --- a/src/tests/acyclic_term.pl +++ b/src/tests/acyclic_term.pl @@ -29,6 +29,16 @@ term6(A) :- A=[B|B], B=[C|C]. +term7(A) :- + B=[C|D], + A=[D|C], + B=[B|D]. + +term8(A) :- + B=C, + B=[C|_D], + A=[C|_E]. + test("acyclic_term_1", ( L = [_Y,[M,B],B|M], acyclic_term(L) )). @@ -235,6 +245,16 @@ test("acyclic_term#2125", ( \+ acyclic_term(B) )). +test("acyclic_term#2130_1", ( + term7(T), + \+ acyclic_term(T) +)). + +test("acyclic_term#2130_2", ( + term8(T), + \+ acyclic_term(T) +)). + main :- findall(test(Name, Goal), test(Name, Goal), Tests), run_tests(Tests, Failed),