From 82a7d7efbd63db879bff1c837febd35ccc6bc043 Mon Sep 17 00:00:00 2001 From: bakaq Date: Sun, 11 Aug 2024 16:17:40 -0300 Subject: [PATCH] Value::from_heapcell() implemented with heap walk --- benches/setup.rs | 6 +- src/machine/lib_machine.rs | 138 ++++++++++++++++++++--- src/machine/parsed_results.rs | 201 +++++++++++++++++++++++++++++----- 3 files changed, 300 insertions(+), 45 deletions(-) diff --git a/benches/setup.rs b/benches/setup.rs index a61e0c63..ec9339c5 100644 --- a/benches/setup.rs +++ b/benches/setup.rs @@ -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| { diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index 354e540d..c42bcae0 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -95,9 +95,9 @@ impl Iterator for QueryState<'_> { continue; } - let term = Value::from_heapcell(machine, term_to_be_printed, var_names); + let term = Value::from_heapcell(machine, *term_to_be_printed, var_names); - if let Value::String(ref term_str) = term { + if let Value::Var(ref term_str) = term { if *term_str == var_key.to_string() { continue; } @@ -221,8 +221,6 @@ impl Machine { #[cfg(test)] mod tests { - use ordered_float::OrderedFloat; - use super::*; use crate::machine::{QueryMatch, QueryResolution, Value}; @@ -304,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()), } ),])) ); @@ -317,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()), } ),])) ); @@ -328,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()) }), ])) ); @@ -370,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()), + ]), } ),])) ); @@ -443,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(); @@ -598,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 + "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() { diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 2fb6ea24..94dd5d98 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -1,6 +1,6 @@ use crate::atom_table::*; -use crate::heap_print::PrinterOutputter; -use crate::heap_print::{HCPrinter, HCValueOutputter}; +use crate::heap_iter::{stackful_post_order_iter, NonListElider}; +use crate::machine::{F64Offset, F64Ptr, Fixnum, HeapCellValueTag}; use crate::parser::ast; use dashu::*; use indexmap::IndexMap; @@ -10,10 +10,9 @@ use std::collections::HashMap; use std::fmt::Display; use std::fmt::Write; use std::iter::FromIterator; -use std::sync::Arc; -use super::HeapCellValue; use super::Machine; +use super::{HeapCellValue, Number}; pub type QueryResult = Result; @@ -32,7 +31,7 @@ pub fn write_prolog_value_as_json( 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::(s.as_str()) { //treat as string literal @@ -134,39 +133,191 @@ pub enum Value { Integer(Integer), Rational(Rational), Float(OrderedFloat), - Atom(Atom), + Atom(String), String(String), List(Vec), - Structure(Atom, Vec), - Var, + Structure(String, Vec), + Var(String), + AnonVar, } impl Value { pub(crate) fn from_heapcell( machine: &mut Machine, - heap_cell: &HeapCellValue, + heap_cell: HeapCellValue, var_names: &IndexMap, ) -> Self { - let mut printer = HCPrinter::new( + // Adapted from MachineState::read_term_from_heap + let mut term_stack = vec![]; + let iter = stackful_post_order_iter::( &mut machine.machine_st.heap, - Arc::clone(&machine.machine_st.atom_tbl), &mut machine.machine_st.stack, - &machine.indices.op_dir, - PrinterOutputter::new(), - *heap_cell, + heap_cell, ); - 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); + 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::List(mut elems) => { + elems.insert(0, head); + Value::List(elems) + }, + Value::String(mut elems) => match head { + Value::Atom(ref a) if a.chars().collect::>().len() == 1 => { + // Handle lists of char as strings + elems.insert(0, a.chars().next().unwrap()); + Value::String(elems) + }, + _ => { + let mut elems: Vec = elems + .chars() + .map(|x| Value::Atom(x.into())) + .collect(); + elems.insert(0, head); + Value::List(elems) + } + }, + Value::Atom(atom) if atom == "[]" => match head { + Value::Atom(ref a) if a.chars().collect::>().len() == 1 => { + // Handle lists of char as strings + Value::String(a.to_string()) + } + _ => Value::List(vec![head]), + }, + _ => Value::Structure(".".into(), vec![head, tail]), + }; + term_stack.push(list); + } + (HeapCellValueTag::Var | HeapCellValueTag::AttrVar | HeapCellValueTag::StackVar) => { + if let Some(ast::Var::Named(name)) = var_names.get(&addr).map(|x| x.borrow().clone()) { + term_stack.push(Value::Var(name)); + } else { + // TODO: These variables aren't actually anonymous, they just aren't in the + // query. Give names to them to differentiate distinct variables. + term_stack.push(Value::AnonVar); + } + } + //(HeapCellValueTag::Cons | HeapCellValueTag::CStr | HeapCellValueTag::Fixnum | + // HeapCellValueTag::Char | HeapCellValueTag::F64) => { + // term_stack.push(Term::Literal(Cell::default(), Literal::try_from(addr).unwrap())); + //} + (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 { + term_stack.push(Value::Atom(name.as_str().to_string())); + } else { + let subterms = term_stack + .drain(term_stack.len() - arity ..) + .collect(); - let outputter = printer.print(); + term_stack.push(Value::Structure(name.as_str().to_string(), subterms)); + } + } + (HeapCellValueTag::PStr, atom) => { + let tail = term_stack.pop().unwrap(); + + if let Value::Atom(atom) = tail { + if atom == "[]" { + term_stack.push(Value::String(atom.as_str().to_string())); + } + } else { + let mut list: Vec = 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), + )); + } + */ + _ => { + } + ); + } - let output: String = outputter.result(); - Value::try_from(output).expect("Couldn't convert Houtput to Value") + debug_assert_eq!(term_stack.len(), 1); + term_stack.pop().unwrap() } } @@ -386,7 +537,7 @@ impl TryFrom 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![]; @@ -400,7 +551,7 @@ impl TryFrom 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 { -- 2.54.0