]> Repositorios git - scryer-prolog.git/commitdiff
fix cycle detection in lists by looking backward analogously to the same process...
authorMark <[email protected]>
Tue, 24 Oct 2023 18:23:05 +0000 (12:23 -0600)
committerMark <[email protected]>
Tue, 24 Oct 2023 18:23:05 +0000 (12:23 -0600)
src/machine/cycle_detection.rs
src/tests/acyclic_term.pl

index 03cf9834458f87ca31fc3083efc30d349ead7be6..342dbdeca01cb366081966e0fab965cd2616442a 100644 (file)
@@ -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();
index 9c08d9c831f864a18f069c8df690d91b6849730f..3c2b93776111ca011c1b28eba43bcdfcc66c5968 100644 (file)
@@ -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),