Merge pull request #2475 from bakaq/value_heap_walk

Value heap walk in lib_machine
This commit is contained in:
Mark Thom
2024-08-19 16:33:13 -06:00
committed by GitHub
3 changed files with 491 additions and 76 deletions

View File

@@ -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| {

View File

@@ -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};
@@ -36,7 +34,7 @@ impl Iterator for QueryState<'_> {
type Item = Result<QueryResolutionLine, String>;
fn next(&mut self) -> Option<Self::Item> {
let var_names = &self.var_names;
let var_names = &mut self.var_names;
let term_write_result = &self.term;
let machine = &mut self.machine;
@@ -92,36 +90,46 @@ impl Iterator for QueryState<'_> {
let mut bindings: BTreeMap<String, Value> = BTreeMap::new();
for (var_key, term_to_be_printed) in &term_write_result.var_dict {
if var_key.to_string().starts_with('_') {
continue;
let var_dict = &term_write_result.var_dict;
for (var_key, term_to_be_printed) in var_dict.iter() {
let mut var_name = var_key.to_string();
if var_name.starts_with('_') {
let should_print = var_names.values().any(|x| match x.borrow().clone() {
Var::Named(v) => v == var_name,
_ => false,
});
if !should_print {
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 mut term =
Value::from_heapcell(machine, *term_to_be_printed, &mut var_names.clone());
let outputter = printer.print();
if let Value::Var(ref term_str) = term {
if *term_str == var_name {
continue;
}
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"),
);
// Var dict is in the order things appear in the query. If var_name appears
// after term in the query, switch their places.
let var_name_idx = var_dict
.get_index_of(&VarKey::VarPtr(Var::Named(var_name.clone()).into()))
.unwrap();
let term_idx =
var_dict.get_index_of(&VarKey::VarPtr(Var::Named(term_str.clone()).into()));
if let Some(idx) = term_idx {
if idx < var_name_idx {
let new_term = Value::Var(var_name);
let new_var_name = term_str.into();
term = new_term;
var_name = new_var_name;
}
}
}
bindings.insert(var_name, term);
}
// NOTE: there are outstanding choicepoints, backtrack
@@ -239,8 +247,6 @@ impl Machine {
#[cfg(test)]
mod tests {
use ordered_float::OrderedFloat;
use super::*;
use crate::machine::{QueryMatch, QueryResolution, Value};
@@ -253,9 +259,9 @@ mod tests {
"facts",
String::from(
r#"
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
),
);
@@ -304,15 +310,15 @@ mod tests {
let mut machine = Machine::new_lib();
machine.load_module_string(
"facts",
r#"
:- discontiguous(subject_class/2).
:- discontiguous(constructor/2).
r#"
:- discontiguous(subject_class/2).
:- discontiguous(constructor/2).
subject_class("Todo", c).
constructor(c, '[{action: "addLink", source: "this", predicate: "todo://state", target: "todo://ready"}]').
subject_class("Todo", c).
constructor(c, '[{action: "addLink", source: "this", predicate: "todo://state", target: "todo://ready"}]').
subject_class("Recipe", xyz).
constructor(xyz, '[{action: "addLink", source: "this", predicate: "recipe://title", target: "literal://string:Meta%20Muffins"}]').
subject_class("Recipe", xyz).
constructor(xyz, '[{action: "addLink", source: "this", predicate: "recipe://title", target: "literal://string:Meta%20Muffins"}]').
"#.to_string());
let result = machine.run_query(String::from(
@@ -322,8 +328,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()),
}
),]))
);
@@ -335,8 +341,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()),
}
),]))
);
@@ -346,10 +352,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())
}),
]))
);
@@ -362,7 +368,7 @@ mod tests {
machine.load_module_string(
"facts",
r#"
:- discontiguous(subject_class/2).
:- discontiguous(subject_class/2).
"#
.to_string(),
);
@@ -378,7 +384,7 @@ mod tests {
machine.load_module_string(
"facts",
r#"
list([1,2,3]).
list([1,2,3]).
"#
.to_string(),
);
@@ -388,13 +394,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()),
]),
}
),]))
);
@@ -409,9 +413,9 @@ mod tests {
"facts",
String::from(
r#"
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
),
);
@@ -443,8 +447,8 @@ mod tests {
"facts",
String::from(
r#"
triple("a", "new", "b").
"#,
triple("a", "new", "b").
"#,
),
);
@@ -461,6 +465,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();
@@ -511,9 +516,9 @@ mod tests {
"facts",
String::from(
r#"
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
),
);
@@ -546,7 +551,7 @@ mod tests {
r#"
:- discontiguous(property_resolve/2).
subject_class("Todo", c).
"#,
"#,
),
);
@@ -570,7 +575,7 @@ mod tests {
r#"
a("true for a").
b("true for b").
"#,
"#,
),
);
@@ -602,7 +607,7 @@ mod tests {
r#"
triple("a", "p1", "b").
triple("a", "p2", "b").
"#,
"#,
),
);
@@ -616,6 +621,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
"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::Var("_A".into()), // 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() {
@@ -628,7 +740,7 @@ mod tests {
male(stephen).
parent(albert,edward).
father(F,C):-parent(F,C),male(F).
"#,
"#,
),
);
@@ -678,4 +790,43 @@ mod tests {
assert_eq!(iterator.next(), Some(Ok(QueryResolutionLine::False)));
assert_eq!(iterator.next(), None);
}
#[test]
#[cfg_attr(miri, ignore)]
fn differentiate_anonymous_variables() {
let mut machine = Machine::new_lib();
let result = machine.run_query("A = [_,_], _B = 1 ; B = [_,_].".into());
assert_eq!(
result,
Ok(QueryResolution::Matches(vec![
QueryMatch::from(btreemap! {
"A" => Value::List(vec![Value::Var("_A".into()), Value::Var("_C".into())]),
"_B" => Value::Integer(1.into()),
}),
QueryMatch::from(btreemap! {
"B" => Value::List(vec![Value::Var("_A".into()), Value::Var("_C".into())]),
}),
]))
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn order_of_variables_in_binding() {
let mut machine = Machine::new_lib();
let result = machine.run_query("X = Y, Z = W.".into());
assert_eq!(
result,
Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! {
"X" => Value::Var("Y".into()),
"Z" => Value::Var("W".into()),
}
),]))
);
}
}

View File

@@ -1,12 +1,20 @@
use crate::atom_table::*;
use crate::heap_iter::{stackful_post_order_iter, NonListElider};
use crate::machine::{F64Offset, F64Ptr, Fixnum, HeapCellValueTag};
use crate::parser::ast::{Var, VarPtr};
use dashu::*;
use indexmap::IndexMap;
use ordered_float::OrderedFloat;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fmt::Display;
use std::fmt::Write;
use std::iter::FromIterator;
use super::Machine;
use super::{HeapCellValue, Number};
pub type QueryResult = Result<QueryResolution, String>;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -24,7 +32,7 @@ pub fn write_prolog_value_as_json<W: Write>(
Value::Integer(i) => write!(writer, "{}", i),
Value::Float(f) => write!(writer, "{}", f),
Value::Rational(r) => write!(writer, "{}", r),
Value::Atom(a) => writer.write_str(&a.as_str()),
Value::Atom(a) => writer.write_str(a.as_str()),
Value::String(s) => {
if let Err(_e) = serde_json::from_str::<serde_json::Value>(s.as_str()) {
//treat as string literal
@@ -126,11 +134,267 @@ pub enum Value {
Integer(Integer),
Rational(Rational),
Float(OrderedFloat<f64>),
Atom(Atom),
Atom(String),
String(String),
List(Vec<Value>),
Structure(Atom, Vec<Value>),
Var,
Structure(String, Vec<Value>),
Var(String),
}
/// This is an auxiliary function to turn a count into names of anonymous variables like _A, _B,
/// _AB, etc...
fn count_to_letter_code(mut count: usize) -> String {
let mut letters = Vec::new();
loop {
let letter_idx = (count % 26) as u32;
letters.push(char::from_u32('A' as u32 + letter_idx).unwrap());
count /= 26;
if count == 0 {
break;
}
}
letters.into_iter().chain("_".chars()).rev().collect()
}
impl Value {
pub(crate) fn from_heapcell(
machine: &mut Machine,
heap_cell: HeapCellValue,
var_names: &mut IndexMap<HeapCellValue, VarPtr>,
) -> Self {
// Adapted from MachineState::read_term_from_heap
let mut term_stack = vec![];
let iter = stackful_post_order_iter::<NonListElider>(
&mut machine.machine_st.heap,
&mut machine.machine_st.stack,
heap_cell,
);
let mut anon_count: usize = 0;
let var_ptr_cmp = |a, b| match a {
Var::Named(name_a) => match b {
Var::Named(name_b) => name_a.cmp(&name_b),
_ => Ordering::Less,
},
_ => match b {
Var::Named(_) => Ordering::Greater,
_ => Ordering::Equal,
},
};
for addr in iter {
let addr = unmark_cell_bits!(addr);
read_heap_cell!(addr,
(HeapCellValueTag::Lis) => {
let tail = term_stack.pop().unwrap();
let head = term_stack.pop().unwrap();
let list = match tail {
Value::Atom(atom) if atom == "[]" => match head {
Value::Atom(ref a) if a.chars().collect::<Vec<_>>().len() == 1 => {
// Handle lists of char as strings
Value::String(a.to_string())
}
_ => Value::List(vec![head]),
},
Value::List(elems) if elems.is_empty() => match head {
Value::Atom(ref a) if a.chars().collect::<Vec<_>>().len() == 1 => {
// Handle lists of char as strings
Value::String(a.to_string())
},
_ => Value::List(vec![head]),
},
Value::List(mut elems) => {
elems.insert(0, head);
Value::List(elems)
},
Value::String(mut elems) => match head {
Value::Atom(ref a) if a.chars().collect::<Vec<_>>().len() == 1 => {
// Handle lists of char as strings
elems.insert(0, a.chars().next().unwrap());
Value::String(elems)
},
_ => {
let mut elems: Vec<Value> = elems
.chars()
.map(|x| Value::Atom(x.into()))
.collect();
elems.insert(0, head);
Value::List(elems)
}
},
_ => {
Value::Structure(".".into(), vec![head, tail])
}
};
term_stack.push(list);
}
(HeapCellValueTag::Var | HeapCellValueTag::AttrVar | HeapCellValueTag::StackVar) => {
let var = var_names.get(&addr).map(|x| x.borrow().clone());
match var {
Some(Var::Named(name)) => term_stack.push(Value::Var(name)),
_ => {
let anon_name = loop {
// Generate a name for the anonymous variable
let anon_name = count_to_letter_code(anon_count);
// Find if this name is already being used
var_names.sort_by(|_, a, _, b| {
var_ptr_cmp(a.borrow().clone(), b.borrow().clone())
});
let binary_result = var_names.binary_search_by(|_,a| {
let var_ptr = Var::Named(anon_name.clone());
var_ptr_cmp(a.borrow().clone(), var_ptr.clone())
});
match binary_result {
Ok(_) => anon_count += 1, // Name already used
Err(_) => {
// Name not used, assign it to this variable
let var_ptr = VarPtr::from(Var::Named(anon_name.clone()));
var_names.insert(addr, var_ptr);
break anon_name;
},
}
};
term_stack.push(Value::Var(anon_name));
},
}
}
(HeapCellValueTag::F64, f) => {
term_stack.push(Value::Float(*f));
}
(HeapCellValueTag::Char, c) => {
term_stack.push(Value::Atom(c.into()));
}
(HeapCellValueTag::Fixnum, n) => {
term_stack.push(Value::Integer(n.into()));
}
(HeapCellValueTag::Cons) => {
match Number::try_from(addr) {
Ok(Number::Integer(i)) => term_stack.push(Value::Integer((*i).clone())),
Ok(Number::Rational(r)) => term_stack.push(Value::Rational((*r).clone())),
_ => {}
}
}
(HeapCellValueTag::CStr, s) => {
term_stack.push(Value::String(s.as_str().to_string()));
}
(HeapCellValueTag::Atom, (name, arity)) => {
//let h = iter.focus().value() as usize;
//let mut arity = arity;
// Not sure why/if this is needed.
// Might find out with better testing later.
/*
if iter.heap.len() > h + arity + 1 {
let value = iter.heap[h + arity + 1];
if let Some(idx) = get_structure_index(value) {
// in the second condition, arity == 0,
// meaning idx cannot pertain to this atom
// if it is the direct subterm of a larger
// structure.
if arity > 0 || !iter.direct_subterm_of_str(h) {
term_stack.push(
Term::Literal(Cell::default(), Literal::CodeIndex(idx))
);
arity += 1;
}
}
}
*/
if arity == 0 {
let atom_name = name.as_str().to_string();
if atom_name == "[]" {
term_stack.push(Value::List(vec![]));
} else {
term_stack.push(Value::Atom(atom_name));
}
} else {
let subterms = term_stack
.drain(term_stack.len() - arity ..)
.collect();
term_stack.push(Value::Structure(name.as_str().to_string(), subterms));
}
}
(HeapCellValueTag::PStr, atom) => {
let tail = term_stack.pop().unwrap();
match tail {
Value::Atom(atom) => {
if atom == "[]" {
term_stack.push(Value::String(atom.as_str().to_string()));
}
},
Value::List(l) => {
let mut list: Vec<Value> = atom
.as_str()
.to_string()
.chars()
.map(|x| Value::Atom(x.to_string()))
.collect();
list.extend(l.into_iter());
term_stack.push(Value::List(list));
},
_ => {
let mut list: Vec<Value> = atom
.as_str()
.to_string()
.chars()
.map(|x| Value::Atom(x.to_string()))
.collect();
let mut partial_list = Value::Structure(
".".into(),
vec![
list.pop().unwrap(),
tail,
],
);
while let Some(last) = list.pop() {
partial_list = Value::Structure(
".".into(),
vec![
last,
partial_list,
],
);
}
term_stack.push(partial_list);
}
}
}
// I dont know if this is needed here.
/*
(HeapCellValueTag::PStrLoc, h) => {
let atom = cell_as_atom_cell!(iter.heap[h]).get_name();
let tail = term_stack.pop().unwrap();
term_stack.push(Term::PartialString(
Cell::default(),
atom.as_str().to_owned(),
Box::new(tail),
));
}
*/
_ => {
}
);
}
debug_assert_eq!(term_stack.len(), 1);
term_stack.pop().unwrap()
}
}
impl From<BTreeMap<&str, Value>> for QueryMatch {
@@ -349,7 +613,7 @@ impl TryFrom<String> for Value {
}
}
Ok(Value::Structure(atom!("{}"), values))
Ok(Value::Structure("{}".into(), values))
} else if trimmed.starts_with("<<") && trimmed.ends_with(">>") {
let iter = trimmed[2..trimmed.len() - 2].split(',');
let mut values = vec![];
@@ -363,7 +627,7 @@ impl TryFrom<String> for Value {
}
}
Ok(Value::Structure(atom!("<<>>"), values))
Ok(Value::Structure("<<>>".into(), values))
} else if !trimmed.contains(',') && !trimmed.contains('\'') && !trimmed.contains('"') {
Ok(Value::String(trimmed.into()))
} else {