From 37a906cf374e86397664a0c896a580a340e17bcb Mon Sep 17 00:00:00 2001 From: bakaq Date: Sun, 11 Aug 2024 14:12:34 -0300 Subject: [PATCH 1/7] Value::from_heapcell() --- src/machine/lib_machine.rs | 36 +++++++++------------------------- src/machine/parsed_results.rs | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index dba599c88..354e540d3 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -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}; @@ -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::String(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 diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 0dd8236f8..2fb6ea247 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -1,11 +1,19 @@ use crate::atom_table::*; +use crate::heap_print::PrinterOutputter; +use crate::heap_print::{HCPrinter, HCValueOutputter}; +use crate::parser::ast; use dashu::*; +use indexmap::IndexMap; use ordered_float::OrderedFloat; use std::collections::BTreeMap; 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; pub type QueryResult = Result; @@ -133,6 +141,35 @@ pub enum Value { Var, } +impl Value { + pub(crate) fn from_heapcell( + machine: &mut Machine, + heap_cell: &HeapCellValue, + var_names: &IndexMap, + ) -> Self { + 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(), + *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); + + let outputter = printer.print(); + + let output: String = outputter.result(); + Value::try_from(output).expect("Couldn't convert Houtput to Value") + } +} + impl From> for QueryMatch { fn from(bindings: BTreeMap<&str, Value>) -> Self { QueryMatch { From 82a7d7efbd63db879bff1c837febd35ccc6bc043 Mon Sep 17 00:00:00 2001 From: bakaq Date: Sun, 11 Aug 2024 16:17:40 -0300 Subject: [PATCH 2/7] 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 a61e0c634..ec9339c59 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 354e540d3..c42bcae03 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 2fb6ea247..94dd5d986 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 { From 12aaae5fc0b8b82fe7514808f8769c20935dfc63 Mon Sep 17 00:00:00 2001 From: bakaq Date: Thu, 15 Aug 2024 04:55:20 -0300 Subject: [PATCH 3/7] Differentiate anonymous variables --- src/machine/lib_machine.rs | 38 +++++++++++++++--- src/machine/parsed_results.rs | 75 +++++++++++++++++++++++++++++------ 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index c42bcae03..ec264ed4c 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -34,7 +34,7 @@ impl Iterator for QueryState<'_> { type Item = Result; fn next(&mut self) -> Option { - let var_names = &self.var_names; + let var_names = &mut self.var_names; let term_write_result = &self.term; let machine = &mut self.machine; @@ -91,11 +91,18 @@ impl Iterator for QueryState<'_> { let mut bindings: BTreeMap = 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_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 term = Value::from_heapcell(machine, *term_to_be_printed, var_names); + let term = Value::from_heapcell(machine, *term_to_be_printed, &mut var_names.clone()); if let Value::Var(ref term_str) = term { if *term_str == var_key.to_string() { @@ -682,7 +689,7 @@ mod tests { ".".into(), vec![ Value::Atom("b".into()), - Value::AnonVar, // Anonymous variable + Value::Var("_A".into()), // Anonymous variable ], ), ], @@ -764,4 +771,25 @@ 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())]), + }), + ])) + ); + } } diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 94dd5d986..06a8873b3 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -1,10 +1,11 @@ use crate::atom_table::*; use crate::heap_iter::{stackful_post_order_iter, NonListElider}; use crate::machine::{F64Offset, F64Ptr, Fixnum, HeapCellValueTag}; -use crate::parser::ast; +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; @@ -138,14 +139,31 @@ pub enum Value { List(Vec), Structure(String, Vec), Var(String), - AnonVar, +} + +/// 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: &IndexMap, + var_names: &mut IndexMap, ) -> Self { // Adapted from MachineState::read_term_from_heap let mut term_stack = vec![]; @@ -155,6 +173,18 @@ impl Value { 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); @@ -195,18 +225,37 @@ impl Value { 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); + 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::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)); } From 7afc08af473294fa9be4c49e345131a02c22994d Mon Sep 17 00:00:00 2001 From: bakaq Date: Fri, 16 Aug 2024 00:01:25 -0300 Subject: [PATCH 4/7] Represent atom [] as empty list --- src/machine/parsed_results.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 06a8873b3..305e6a4d7 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -302,7 +302,12 @@ impl Value { */ if arity == 0 { - term_stack.push(Value::Atom(name.as_str().to_string())); + 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 ..) From 8736526b355e6756b1047a9615610e40171cac52 Mon Sep 17 00:00:00 2001 From: bakaq Date: Fri, 16 Aug 2024 00:34:46 -0300 Subject: [PATCH 5/7] Fix lists of chars as strings --- src/machine/parsed_results.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 305e6a4d7..cedbc9f03 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -194,6 +194,20 @@ impl Value { 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::>().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::>().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) @@ -213,13 +227,6 @@ impl Value { 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); From c1b88cee99eee02448ab70fa92877cbf11737975 Mon Sep 17 00:00:00 2001 From: bakaq Date: Fri, 16 Aug 2024 08:08:05 -0300 Subject: [PATCH 6/7] Fix partial strings ending in lists --- src/machine/parsed_results.rs | 69 +++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index cedbc9f03..05b0f7f15 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -227,7 +227,9 @@ impl Value { Value::List(elems) } }, - _ => Value::Structure(".".into(), vec![head, tail]), + _ => { + Value::Structure(".".into(), vec![head, tail]) + } }; term_stack.push(list); } @@ -326,37 +328,50 @@ impl Value { (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( + match tail { + Value::Atom(atom) => { + if atom == "[]" { + term_stack.push(Value::String(atom.as_str().to_string())); + } + }, + Value::List(l) => { + let mut list: Vec = 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 = atom + .as_str() + .to_string() + .chars() + .map(|x| Value::Atom(x.to_string())) + .collect(); + + let mut partial_list = Value::Structure( ".".into(), vec![ - last, - partial_list, + list.pop().unwrap(), + tail, ], ); - } - term_stack.push(partial_list); + 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. From e24396d78af471729253463f638905fc0cf6af43 Mon Sep 17 00:00:00 2001 From: bakaq Date: Mon, 19 Aug 2024 14:20:43 -0300 Subject: [PATCH 7/7] Fix order of variables in bindings --- src/machine/lib_machine.rs | 95 ++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index ec264ed4c..e44e54e4e 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -90,8 +90,10 @@ impl Iterator for QueryState<'_> { let mut bindings: BTreeMap = BTreeMap::new(); - for (var_key, term_to_be_printed) in &term_write_result.var_dict { - let var_name = var_key.to_string(); + 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, @@ -102,15 +104,32 @@ impl Iterator for QueryState<'_> { } } - let term = Value::from_heapcell(machine, *term_to_be_printed, &mut var_names.clone()); + let mut term = + Value::from_heapcell(machine, *term_to_be_printed, &mut var_names.clone()); if let Value::Var(ref term_str) = term { - if *term_str == var_key.to_string() { + if *term_str == var_name { continue; } + + // 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_key.to_string(), term); + bindings.insert(var_name, term); } // NOTE: there are outstanding choicepoints, backtrack @@ -240,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"). + "#, ), ); @@ -291,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( @@ -349,7 +368,7 @@ mod tests { machine.load_module_string( "facts", r#" - :- discontiguous(subject_class/2). + :- discontiguous(subject_class/2). "# .to_string(), ); @@ -365,7 +384,7 @@ mod tests { machine.load_module_string( "facts", r#" - list([1,2,3]). + list([1,2,3]). "# .to_string(), ); @@ -394,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"). + "#, ), ); @@ -428,8 +447,8 @@ mod tests { "facts", String::from( r#" - triple("a", "new", "b"). - "#, + triple("a", "new", "b"). + "#, ), ); @@ -497,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"). + "#, ), ); @@ -532,7 +551,7 @@ mod tests { r#" :- discontiguous(property_resolve/2). subject_class("Todo", c). - "#, + "#, ), ); @@ -556,7 +575,7 @@ mod tests { r#" a("true for a"). b("true for b"). - "#, + "#, ), ); @@ -588,7 +607,7 @@ mod tests { r#" triple("a", "p1", "b"). triple("a", "p2", "b"). - "#, + "#, ), ); @@ -721,7 +740,7 @@ mod tests { male(stephen). parent(albert,edward). father(F,C):-parent(F,C),male(F). - "#, + "#, ), ); @@ -792,4 +811,22 @@ mod tests { ])) ); } + + #[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()), + } + ),])) + ); + } }