]> Repositorios git - scryer-prolog.git/commitdiff
remove list remnants from stack iteration in printer when cyclic (#2131)
authorMark <[email protected]>
Tue, 24 Oct 2023 20:10:11 +0000 (14:10 -0600)
committerMark <[email protected]>
Tue, 24 Oct 2023 20:10:11 +0000 (14:10 -0600)
src/heap_iter.rs
src/heap_print.rs
src/machine/cycle_detection.rs
src/tests/acyclic_term.pl

index 75c8829ba0a2d88bda9852cce2a33d378f701dec..2eeba0077e471ed1ccee6cd9dba5f5fd3622bded 100644 (file)
@@ -2257,9 +2257,7 @@ mod tests {
                 list_loc_as_cell!(1)
             );
             assert_eq!(iter.next().unwrap(), cyclic_link);
-
             assert_eq!(iter.next().unwrap(), cyclic_link);
-
             assert_eq!(iter.next().unwrap(), cyclic_link);
 
             assert_eq!(iter.next(), None);
index 1ad88f29e53c3f73ca28b25f3269bb4df6407dbe..2d92cd2d5840c9a2d094d961a55d959fa12787a0 100644 (file)
@@ -888,6 +888,19 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
                     var_opt => {
                         if is_cyclic && cell.is_compound(self.iter.heap) {
                             // self-referential variables are marked "cyclic".
+                            read_heap_cell!(cell,
+                                (HeapCellValueTag::Lis, vh) => {
+                                    if self.iter.heap[vh].get_forwarding_bit() {
+                                        self.iter.pop_stack();
+                                    }
+
+                                    if self.iter.heap[vh+1].get_forwarding_bit() {
+                                        self.iter.pop_stack();
+                                    }
+                                }
+                                _ => {}
+                            );
+
                             match var_opt {
                                 Some(var) => {
                                     // If the term is bound to a named variable,
index 342dbdeca01cb366081966e0fab965cd2616442a..692186cba8920c9258f4055ce341e502f59844bf 100644 (file)
@@ -175,6 +175,15 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> {
                             None => return None,
                         };
 
+                        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;
+                                }
+                            }
+                        }
+
                         if (last_cell_loc + 1) as u64 == self.next {
                             if self.backward() {
                                 return None;
@@ -186,15 +195,6 @@ 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 3c2b93776111ca011c1b28eba43bcdfcc66c5968..065a4d9fa1869a50ee9a46af8c1b80b74b9a65e7 100644 (file)
@@ -255,6 +255,11 @@ test("acyclic_term#2130_2", (
     \+ acyclic_term(T)
 )).
 
+test("acyclic_term#2131", (
+    A=[B],C=[B],C=[A],
+    \+ acyclic_term(C)
+)).
+
 main :-
     findall(test(Name, Goal), test(Name, Goal), Tests),
     run_tests(Tests, Failed),