use eager_stackful_preorder_iter in variable_set, add term_variables/1 tests

This commit is contained in:
Mark
2023-10-04 15:12:25 -06:00
parent 1bfdea7527
commit 0ad4427f83
5 changed files with 106 additions and 14 deletions

View File

@@ -12,6 +12,14 @@ use modular_bitfield::prelude::*;
use std::ops::Deref; use std::ops::Deref;
use std::vec::Vec; use std::vec::Vec;
#[inline(always)]
pub fn eager_stackful_preorder_iter(
heap: &mut Heap,
value: HeapCellValue,
) -> EagerStackfulPreOrderHeapIter {
EagerStackfulPreOrderHeapIter::new(heap, value)
}
/* /*
* Unlike StackfulPreOrderHeapIter, this iterator not only marks * Unlike StackfulPreOrderHeapIter, this iterator not only marks
* cyclic terms for the sake of skipping them at the second visit but * cyclic terms for the sake of skipping them at the second visit but

View File

@@ -1621,7 +1621,7 @@ impl MachineState {
// returns true on failure. // returns true on failure.
pub fn ground_test(&mut self) -> bool { pub fn ground_test(&mut self) -> bool {
let iter = EagerStackfulPreOrderHeapIter::new(&mut self.heap, self.registers[1]); let iter = eager_stackful_preorder_iter(&mut self.heap, self.registers[1]);
for term in iter { for term in iter {
if term.is_var() { if term.is_var() {

View File

@@ -583,20 +583,11 @@ impl MachineState {
seen_set: &mut IndexSet<HeapCellValue, S>, seen_set: &mut IndexSet<HeapCellValue, S>,
value: HeapCellValue, value: HeapCellValue,
) { ) {
let mut iter = stackful_preorder_iter::<NonListElider>(&mut self.heap, &mut self.stack, value); let iter = eager_stackful_preorder_iter(&mut self.heap, value);
while let Some(value) = iter.next() { for term in iter {
let value = unmark_cell_bits!(value); if term.is_var() {
seen_set.insert(term);
if value.is_var() {
let value = unmark_cell_bits!(heap_bound_store(
iter.heap,
heap_bound_deref(iter.heap, value)
));
if value.is_var() {
seen_set.insert(value);
}
} }
} }
} }

View File

@@ -0,0 +1,84 @@
/**/
:- use_module(library(format)).
:- use_module(library(dcgs)).
:- use_module(library(lists)).
:- use_module(library(debug)).
test("term_variables#1400", (
term_variables(A+B*C/B-D, Vars),
term_variables(t(A,B,C,D), Vars),
Vars = [A,B,C,D]
)).
test("term_variables#1405", (
\+ (B=[C|D],C=[_|D],C=[B|B], term_variables(B,_), false)
)).
test("term_variables#1409", (
G_0 = (A=[B|B],A=[C|C]), G_0, term_variables(G_0, Vars), Vars = [B]
)).
test("term_variables#1410", (
\+ \+ (G_0 = ( A=s(A) ), G_0, term_variables(G_0, Vars), Vars = []),
E_0 = (_=[B|B]), G_0 = (E_0,\_=B), G_0, term_variables(G_0, Vars)
)).
test("term_variables#1412", (
G_0 = =([A|B],[A|B]), G_0, term_variables(G_0, Vars),
Vars = [A,B]
)).
test("term_variables#1414", (
\+ (\B=A,C=[A|D],B=[a,b|E],C=[D|E], term_variables(\E,_), false)
)).
test("term_variables#2063", (
A=[B|C], B=[A], term_variables([B], Vars),
Vars = [C]
)).
main :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests(Tests, Failed),
show_failed(Failed),
halt.
main_quiet :-
findall(test(Name, Goal), test(Name, Goal), Tests),
run_tests_quiet(Tests, Failed),
( Failed = [] ->
format("All tests passed", [])
; format("Some tests failed", [])
),
halt.
run_tests([], []).
run_tests([test(Name, Goal)|Tests], Failed) :-
format("Running test \"~s\"~n", [Name]),
( call(Goal) ->
Failed = Failed1
; format("Failed test \"~s\"~n", [Name]),
Failed = [Name|Failed1]
),
run_tests(Tests, Failed1).
run_tests_quiet([], []).
run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
( call(Goal) ->
Failed = Failed1
; Failed = [Name|Failed1]
),
run_tests_quiet(Tests, Failed1).
portray_failed_([]) --> [].
portray_failed_([F|Fs]) -->
"\"", F, "\"", "\n", portray_failed_(Fs).
portray_failed([]) --> [].
portray_failed([F|Fs]) -->
"\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
show_failed(Failed) :-
phrase(portray_failed(Failed), F),
format("~s", [F]).

View File

@@ -93,3 +93,12 @@ fn ground_tests() {
"All tests passed", "All tests passed",
); );
} }
#[test]
fn term_variables_tests() {
run_top_level_test_with_args(
&["src/tests/term_variables.pl", "-f", "-g", "main_quiet"],
"",
"All tests passed",
);
}