Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value heap walk in lib_machine #2475

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benches/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> {
"benches/edges.pl", // name of the prolog module file to load. use the same file in multiple benchmarks
"independent_set_count(ky, Count).", // query to benchmark in the context of the loaded module. consider making the query adjustable to tune the run time to ~0.1s
Strategy::Reuse,
btreemap! { "Count" => Value::try_from("2869176".to_string()).unwrap() },
btreemap! { "Count" => Value::Integer(2869176.into()) },
),
(
"numlist",
"benches/numlist.pl",
"run_numlist(1000000, Head).",
Strategy::Reuse,
btreemap! { "Head" => Value::try_from("1".to_string()).unwrap()},
btreemap! { "Head" => Value::Integer(1.into())},
),
(
"csv_codename",
"benches/csv.pl",
"get_codename(\"0020\",Name).",
Strategy::Reuse,
btreemap! { "Name" => Value::try_from("SPACE".to_string()).unwrap()},
btreemap! { "Name" => Value::String("SPACE".into())},
),
]
.map(|b| {
Expand Down
170 changes: 128 additions & 42 deletions src/machine/lib_machine.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::atom_table;
use crate::heap_print::{HCPrinter, HCValueOutputter, PrinterOutputter};
use crate::machine::machine_indices::VarKey;
use crate::machine::mock_wam::CompositeOpDir;
use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
Expand Down Expand Up @@ -96,32 +94,16 @@ impl Iterator for QueryState<'_> {
if var_key.to_string().starts_with('_') {
continue;
}
let mut printer = HCPrinter::new(
&mut machine.machine_st.heap,
Arc::clone(&machine.machine_st.atom_tbl),
&mut machine.machine_st.stack,
&machine.indices.op_dir,
PrinterOutputter::new(),
*term_to_be_printed,
);

printer.ignore_ops = false;
printer.numbervars = true;
printer.quoted = true;
printer.max_depth = 1000; // NOTE: set this to 0 for unbounded depth
printer.double_quotes = true;
printer.var_names.clone_from(var_names);

let outputter = printer.print();

let output: String = outputter.result();

if var_key.to_string() != output {
bindings.insert(
var_key.to_string(),
Value::try_from(output).expect("Couldn't convert Houtput to Value"),
);

let term = Value::from_heapcell(machine, *term_to_be_printed, var_names);

if let Value::Var(ref term_str) = term {
if *term_str == var_key.to_string() {
continue;
}
}

bindings.insert(var_key.to_string(), term);
}

// NOTE: there are outstanding choicepoints, backtrack
Expand Down Expand Up @@ -239,8 +221,6 @@ impl Machine {

#[cfg(test)]
mod tests {
use ordered_float::OrderedFloat;

use super::*;
use crate::machine::{QueryMatch, QueryResolution, Value};

Expand Down Expand Up @@ -322,8 +302,8 @@ mod tests {
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"C" => Value::from("c"),
"Actions" => Value::from("[{action: \"addLink\", source: \"this\", predicate: \"todo://state\", target: \"todo://ready\"}]"),
"C" => Value::Atom("c".into()),
"Actions" => Value::Atom("[{action: \"addLink\", source: \"this\", predicate: \"todo://state\", target: \"todo://ready\"}]".into()),
}
),]))
);
Expand All @@ -335,8 +315,8 @@ mod tests {
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"C" => Value::from("xyz"),
"Actions" => Value::from("[{action: \"addLink\", source: \"this\", predicate: \"recipe://title\", target: \"literal://string:Meta%20Muffins\"}]"),
"C" => Value::Atom("xyz".into()),
"Actions" => Value::Atom("[{action: \"addLink\", source: \"this\", predicate: \"recipe://title\", target: \"literal://string:Meta%20Muffins\"}]".into()),
}
),]))
);
Expand All @@ -346,10 +326,10 @@ mod tests {
result,
Ok(QueryResolution::Matches(vec![
QueryMatch::from(btreemap! {
"Class" => Value::from("Todo")
"Class" => Value::String("Todo".into())
}),
QueryMatch::from(btreemap! {
"Class" => Value::from("Recipe")
"Class" => Value::String("Recipe".into())
}),
]))
);
Expand Down Expand Up @@ -388,13 +368,11 @@ mod tests {
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"X" => Value::List(
Vec::from([
Value::Float(OrderedFloat::from(1.0)),
Value::Float(OrderedFloat::from(2.0)),
Value::Float(OrderedFloat::from(3.0))
])
)
"X" => Value::List(vec![
Value::Integer(1.into()),
Value::Integer(2.into()),
Value::Integer(3.into()),
]),
}
),]))
);
Expand Down Expand Up @@ -461,6 +439,7 @@ mod tests {

#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
#[ignore = "uses old flawed interface"]
fn integration_test() {
let mut machine = Machine::new_lib();

Expand Down Expand Up @@ -616,6 +595,113 @@ mod tests {
);
}

#[test]
#[cfg_attr(miri, ignore)]
fn atom_quoting() {
let mut machine = Machine::new_lib();

let query = "X = '.'.".into();

let result = machine.run_query(query);

assert_eq!(
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"X" => Value::Atom(".".into()),
}
)]))
);
}

#[test]
#[cfg_attr(miri, ignore)]
fn rational_number() {
use crate::parser::dashu::rational::RBig;
let mut machine = Machine::new_lib();

let query = "X is 1 rdiv 2.".into();

let result = machine.run_query(query);

assert_eq!(
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"X" => Value::Rational(RBig::from_parts(1.into(), 2u32.into())),
}
)]))
);
}

#[test]
#[cfg_attr(miri, ignore)]
fn big_integer() {
use crate::parser::dashu::integer::IBig;
let mut machine = Machine::new_lib();

let query = "X is 10^100.".into();

let result = machine.run_query(query);

assert_eq!(
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"X" => Value::Integer(IBig::from(10).pow(100)),
}
)]))
);
}

#[test]
#[cfg_attr(miri, ignore)]
fn complicated_term() {
let mut machine = Machine::new_lib();

let query = "X = a(\"asdf\", [42, 2.54, asdf, a, [a,b|_], Z]).".into();

let result = machine.run_query(query);

let expected = Value::Structure(
// Composite term
bakaq marked this conversation as resolved.
Show resolved Hide resolved
"a".into(),
vec![
Value::String("asdf".into()), // String
Value::List(vec![
Value::Integer(42.into()), // Fixnum
Value::Float(2.54.into()), // Float
Value::Atom("asdf".into()), // Atom
Value::Atom("a".into()), // Char
Value::Structure(
// Partial string
".".into(),
vec![
Value::Atom("a".into()),
Value::Structure(
".".into(),
vec![
Value::Atom("b".into()),
Value::AnonVar, // Anonymous variable
],
),
],
),
Value::Var("Z".into()), // Named variable
]),
],
);

assert_eq!(
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"X" => expected,
}
)]))
);
}

#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
fn issue_2341() {
Expand Down
Loading
Loading