]> Repositorios git - scryer-prolog.git/commitdiff
omit anonymous variables from read_term variable_names and singletons lists
authorMark <[email protected]>
Mon, 17 Jul 2023 04:22:44 +0000 (22:22 -0600)
committerMark <[email protected]>
Mon, 17 Jul 2023 19:20:10 +0000 (13:20 -0600)
src/machine/machine_indices.rs
src/machine/machine_state.rs
src/machine/mock_wam.rs
src/read.rs

index 3e358db995b5f8bc0a48fcb4aeedaca1d4070899..7389cacacb68dfcc65e80c8c7a07ebbabe563a4c 100644 (file)
@@ -227,7 +227,32 @@ impl CodeIndex {
     }
 }
 
-pub(crate) type HeapVarDict = IndexMap<VarPtr, HeapCellValue, FxBuildHasher>;
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum VarKey {
+    AnonVar(usize),
+    VarPtr(VarPtr),
+}
+
+impl VarKey {
+    #[inline]
+    pub(crate) fn to_string(&self) -> String {
+        match self {
+            VarKey::AnonVar(h) => format!("_{}", h),
+            VarKey::VarPtr(var) => var.borrow().to_string(),
+        }
+    }
+
+    #[inline(always)]
+    pub(crate) fn is_anon(&self) -> bool {
+        if let VarKey::AnonVar(_) = self {
+            true
+        } else {
+            false
+        }
+    }
+}
+
+pub(crate) type HeapVarDict = IndexMap<VarKey, HeapCellValue, FxBuildHasher>;
 
 pub(crate) type GlobalVarDir = IndexMap<Atom, (Ball, Option<HeapCellValue>), FxBuildHasher>;
 
index c8a9cbe0c70534269181be517343b59cd7c328df..f7237836e770165929776f469a5ee0a99f98d69e 100644 (file)
@@ -486,13 +486,13 @@ impl MachineState {
     pub fn read_term_body(&mut self, mut term_write_result: TermWriteResult) -> CallResult {
         fn push_var_eq_functors<'a>(
             heap: &mut Heap,
-            iter: impl Iterator<Item = (&'a VarPtr, &'a HeapCellValue)>,
+            iter: impl Iterator<Item = (&'a VarKey, &'a HeapCellValue)>,
             atom_tbl: &mut AtomTable,
         ) -> Vec<HeapCellValue> {
             let mut list_of_var_eqs = vec![];
 
             for (var, binding) in iter {
-                let var_atom = atom_tbl.build_with(&var.borrow().to_string());
+                let var_atom = atom_tbl.build_with(&var.to_string());
                 let h = heap.len();
 
                 heap.push(atom_as_cell!(atom!("="), 2));
@@ -542,7 +542,11 @@ impl MachineState {
 
         let singleton_var_list = push_var_eq_functors(
             &mut self.heap,
-            term_write_result.var_dict.iter().filter(|(_, binding)| {
+            term_write_result.var_dict.iter().filter(|(var_name, binding)| {
+                if var_name.is_anon() {
+                    return false;
+                }
+
                 if let Some(r) = binding.as_var() {
                     *singleton_var_set.get(&r).unwrap_or(&false)
                 } else {
@@ -565,7 +569,7 @@ impl MachineState {
 
         let list_of_var_eqs = push_var_eq_functors(
             &mut self.heap,
-            var_list.iter().map(|(var_name, var,_)| (var_name,var)),
+            var_list.iter().filter_map(|(var_name, var,_)| if var_name.is_anon() { None } else { Some((var_name,var)) }),
             &mut self.atom_tbl,
         );
 
index 70264ac9a6f62718d0b74597854b3b2a123a647e..257735c6680fcbc50b055220a98ed62eeef9ce97 100644 (file)
@@ -71,7 +71,12 @@ impl MockWAM {
         printer.var_names = term_write_result
             .var_dict
             .into_iter()
-            .map(|(var, cell)| (cell, var))
+            .map(|(var, cell)| {
+                match var {
+                    VarKey::VarPtr(var) => (cell, var.clone()),
+                    VarKey::AnonVar(_) => (cell, VarPtr::from(var.to_string()))
+                }
+            })
             .collect();
 
         Ok(printer.print().result())
index dfba51479cae1ff25f43d0f5e6f8f49ebca511bf..fac2e4e804824216f9c9a568e59cd0afa6e7166b 100644 (file)
@@ -259,9 +259,9 @@ impl CharRead for ReadlineStream {
 }
 
 #[inline]
-pub(crate) fn write_term_to_heap(
-    term: &Term,
-    heap: &mut Heap,
+pub(crate) fn write_term_to_heap<'a, 'b>(
+    term: &'a Term,
+    heap: &'b mut Heap,
     atom_tbl: &mut AtomTable,
 ) -> Result<TermWriteResult, CompilationError> {
     let term_writer = TermWriter::new(heap, atom_tbl);
@@ -294,7 +294,7 @@ impl<'a, 'b> TermWriter<'a, 'b> {
     }
 
     #[inline]
-    fn modify_head_of_queue(&mut self, term: &TermRef<'a>, h: usize) {
+    fn modify_head_of_queue(&mut self, term: &TermRef, h: usize) {
         if let Some((arity, site_h)) = self.queue.pop_front() {
             self.heap[site_h] = self.term_as_addr(term, h);
 
@@ -310,7 +310,7 @@ impl<'a, 'b> TermWriter<'a, 'b> {
         self.heap.push(heap_loc_as_cell!(h));
     }
 
-    fn term_as_addr(&mut self, term: &TermRef<'a>, h: usize) -> HeapCellValue {
+    fn term_as_addr(&mut self, term: &TermRef, h: usize) -> HeapCellValue {
         match term {
             &TermRef::Cons(..) => list_loc_as_cell!(h),
             &TermRef::AnonVar(_) | &TermRef::Var(..) => heap_loc_as_cell!(h),
@@ -329,7 +329,7 @@ impl<'a, 'b> TermWriter<'a, 'b> {
         }
     }
 
-    fn write_term_to_heap(mut self, term: &'a Term) -> Result<TermWriteResult, CompilationError> {
+    fn write_term_to_heap(mut self, term: &Term) -> Result<TermWriteResult, CompilationError> {
         let heap_loc = self.heap.len();
 
         for term in breadth_first_iter(term, RootIterationPolicy::Iterated) {
@@ -383,17 +383,19 @@ impl<'a, 'b> TermWriter<'a, 'b> {
                         self.push_stub_addr();
                     }
                 }
-                &TermRef::AnonVar(Level::Root) | &TermRef::Literal(Level::Root, ..) => {
+                &TermRef::AnonVar(Level::Root) | TermRef::Literal(Level::Root, ..) => {
                     let addr = self.term_as_addr(&term, h);
                     self.heap.push(addr);
                 }
                 &TermRef::Var(Level::Root, _, ref var_ptr) => {
                     let addr = self.term_as_addr(&term, h);
-                    self.var_dict.insert(var_ptr.clone(), heap_loc_as_cell!(h));
+                    self.var_dict.insert(VarKey::VarPtr(var_ptr.clone()), addr);
                     self.heap.push(addr);
                 }
                 &TermRef::AnonVar(_) => {
                     if let Some((arity, site_h)) = self.queue.pop_front() {
+                        self.var_dict.insert(VarKey::AnonVar(h), heap_loc_as_cell!(site_h));
+
                         if arity > 1 {
                             self.queue.push_front((arity - 1, site_h + 1));
                         }
@@ -422,10 +424,12 @@ impl<'a, 'b> TermWriter<'a, 'b> {
                 }
                 &TermRef::Var(_, _, ref var) => {
                     if let Some((arity, site_h)) = self.queue.pop_front() {
-                        if let Some(addr) = self.var_dict.get(var).cloned() {
+                        let var_key = VarKey::VarPtr(var.clone());
+
+                        if let Some(addr) = self.var_dict.get(&var_key).cloned() {
                             self.heap[site_h] = addr;
                         } else {
-                            self.var_dict.insert(var.clone(), heap_loc_as_cell!(site_h));
+                            self.var_dict.insert(var_key, heap_loc_as_cell!(site_h));
                         }
 
                         if arity > 1 {