]> Repositorios git - scryer-prolog.git/commitdiff
add copy_term/3 (#232)
authorMark Thom <[email protected]>
Fri, 29 Nov 2019 07:44:23 +0000 (00:44 -0700)
committerMark Thom <[email protected]>
Fri, 29 Nov 2019 07:44:23 +0000 (00:44 -0700)
README.md
src/prolog/clause_types.rs
src/prolog/lib/atts.pl
src/prolog/machine/copier.rs
src/prolog/machine/machine_state.rs
src/prolog/machine/machine_state_impl.rs
src/prolog/machine/project_attributes.pl
src/prolog/machine/system_calls.rs

index d6c31433cc521520b2707bc1d71480db4bf42c76..636144990af309e21867594a95f839a9efe53904 100644 (file)
--- a/README.md
+++ b/README.md
@@ -175,7 +175,7 @@ The following predicates are built-in to Scryer.
 * `clause/2`
 * `compare/3`
 * `compound/1`
-* `copy_term/2`
+* `copy_term/{2,3}`
 * `current_predicate/1`
 * `current_op/3`
 * `cyclic_term/1`
index 82bd8cde6076d538af3a6d21e654182a05e194e0..a82464056020ab7e0531f09a26ce729b399cf150 100644 (file)
@@ -166,6 +166,7 @@ pub enum SystemClauseType {
     CharCode,
     CharsToNumber,
     CodesToNumber,
+    CopyTermWithoutAttrVars,
     CheckCutPoint,
     CopyToLiftedHeap,
     DeleteAttribute,
@@ -264,6 +265,7 @@ impl SystemClauseType {
             &SystemClauseType::CharCode => clause_name!("$char_code"),
             &SystemClauseType::CharsToNumber => clause_name!("$chars_to_number"),
             &SystemClauseType::CodesToNumber => clause_name!("$codes_to_number"),
+            &SystemClauseType::CopyTermWithoutAttrVars => clause_name!("$copy_term_without_attr_vars"),
             &SystemClauseType::CheckCutPoint => clause_name!("$check_cp"),
             &SystemClauseType::REPL(REPLCodePtr::CompileBatch) => clause_name!("$compile_batch"),
            &SystemClauseType::REPL(REPLCodePtr::UseModule) => clause_name!("$use_module"),
@@ -392,6 +394,7 @@ impl SystemClauseType {
             ("$char_code", 2) => Some(SystemClauseType::CharCode),
             ("$chars_to_number", 2) => Some(SystemClauseType::CharsToNumber),
             ("$codes_to_number", 2) => Some(SystemClauseType::CodesToNumber),
+            ("$copy_term_without_attr_vars", 2) => Some(SystemClauseType::CopyTermWithoutAttrVars),
             ("$check_cp", 1) => Some(SystemClauseType::CheckCutPoint),
             ("$compile_batch", 0) => Some(SystemClauseType::REPL(REPLCodePtr::CompileBatch)),
             ("$copy_to_lh", 2) => Some(SystemClauseType::CopyToLiftedHeap),
index 3fc36974904319ed8fc1ed845294e3c388326dba..75d01ed5adf5712c8391c340ca0ea47f73ec78f5 100644 (file)
@@ -149,3 +149,4 @@ call_residue_vars(Goal, Vars) :-
     '$get_attr_var_queue_delim'(B),
     call(Goal),
     '$get_attr_var_queue_beyond'(B, Vars).
+    
index 6889704499d54f0bb84bed85c646ff86b53d72e3..f900ec8f6f4e44e6da9bade73a0db86caa8e62f2 100644 (file)
@@ -5,6 +5,12 @@ use std::ops::IndexMut;
 
 type Trail = Vec<(Ref, HeapCellValue)>;
 
+#[derive(Clone, Copy)]
+pub enum AttrVarPolicy {
+    DeepCopy,
+    StripAttributes
+}
+
 pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
     fn threshold(&self) -> usize;
     fn push(&mut self, _: HeapCellValue);
@@ -13,9 +19,10 @@ pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
     fn stack(&mut self) -> &mut AndStack;
 }
 
-pub(crate) fn copy_term<T: CopierTarget>(target: T, addr: Addr) {
-    let mut copy_term_state = CopyTermState::new(target);
-    copy_term_state.copy_term_impl(addr);    
+pub(crate)
+fn copy_term<T: CopierTarget>(target: T, addr: Addr, attr_var_policy: AttrVarPolicy) {
+    let mut copy_term_state = CopyTermState::new(target, attr_var_policy);
+    copy_term_state.copy_term_impl(addr);
 }
 
 struct CopyTermState<T: CopierTarget> {
@@ -23,15 +30,17 @@ struct CopyTermState<T: CopierTarget> {
     scan: usize,
     old_h: usize,
     target: T,
+    attr_var_policy: AttrVarPolicy
 }
 
 impl<T: CopierTarget> CopyTermState<T> {
-    fn new(target: T) -> Self {
+    fn new(target: T, attr_var_policy: AttrVarPolicy) -> Self {
         CopyTermState {
             trail: vec![],
             scan: 0,
             old_h: target.threshold(),
             target,
+            attr_var_policy
         }
     }
 
@@ -41,6 +50,14 @@ impl<T: CopierTarget> CopyTermState<T> {
         &mut self.target[scan]
     }
 
+    fn attr_var_redirect_tag(&self) -> impl Fn(usize) -> Addr {
+        if let AttrVarPolicy::DeepCopy = self.attr_var_policy {
+            Addr::AttrVar
+        } else {
+            Addr::HeapCell
+        }
+    }
+
     fn reinstantiate_var(&mut self, addr: Addr, threshold: usize) {
         match addr {
             Addr::HeapCell(h) => {
@@ -58,8 +75,10 @@ impl<T: CopierTarget> CopyTermState<T> {
                 ));
             }
             Addr::AttrVar(h) => {
-                self.target[threshold] = HeapCellValue::Addr(Addr::AttrVar(threshold));
-                self.target[h] = HeapCellValue::Addr(Addr::AttrVar(threshold));
+                let redirect_tag = self.attr_var_redirect_tag();
+
+                self.target[threshold] = HeapCellValue::Addr(redirect_tag(threshold));
+                self.target[h] = HeapCellValue::Addr(redirect_tag(threshold));
                 self.trail
                     .push((Ref::AttrVar(h), HeapCellValue::Addr(Addr::AttrVar(h))));
             }
@@ -94,10 +113,22 @@ impl<T: CopierTarget> CopyTermState<T> {
         let rd = self.target.store(self.target.deref(ra));
 
         match rd.clone() {
-            Addr::AttrVar(h) | Addr::HeapCell(h) if h >= self.old_h => {
+            Addr::AttrVar(h) if h >= self.old_h => {
+                let redirect_tag = self.attr_var_redirect_tag();
+                self.target[threshold] = HeapCellValue::Addr(redirect_tag(h));
+            }
+            Addr::HeapCell(h) if h >= self.old_h => {
                 self.target[threshold] = HeapCellValue::Addr(rd)
             }
-            ra @ Addr::AttrVar(_) | ra @ Addr::HeapCell(..) | ra @ Addr::StackCell(..) => {
+            Addr::AttrVar(h) => {
+                if Addr::AttrVar(h) == rd {
+                    self.reinstantiate_var(Addr::AttrVar(h), threshold);
+                } else {
+                    let redirect_tag = self.attr_var_redirect_tag();
+                    self.target[threshold] = HeapCellValue::Addr(redirect_tag(h));
+                }
+            }
+            ra @ Addr::HeapCell(..) | ra @ Addr::StackCell(..) => {
                 if ra == rd {
                     self.reinstantiate_var(ra, threshold);
                 } else {
@@ -121,20 +152,29 @@ impl<T: CopierTarget> CopyTermState<T> {
         let rd = self.target.store(self.target.deref(addr.clone()));
 
         match rd.clone() {
-            Addr::AttrVar(h) | Addr::HeapCell(h) if h >= self.old_h => {
+            Addr::AttrVar(h) if h >= self.old_h => {
+                let redirect_tag = self.attr_var_redirect_tag();
+                *self.value_at_scan() = HeapCellValue::Addr(redirect_tag(h));
+                self.scan += 1;
+            }
+            Addr::HeapCell(h) if h >= self.old_h => {
                 *self.value_at_scan() = HeapCellValue::Addr(rd);
                 self.scan += 1;
             }
             Addr::AttrVar(h) if addr == rd => {
+                let redirect_tag = self.attr_var_redirect_tag();
                 let threshold = self.target.threshold();
+
                 self.target
-                    .push(HeapCellValue::Addr(Addr::AttrVar(threshold)));
+                    .push(HeapCellValue::Addr(redirect_tag(threshold)));
 
-                let list_val = self.target[h + 1].clone();
-                self.target.push(list_val);
+                if let Addr::AttrVar(_) = redirect_tag(threshold) {
+                    let list_val = self.target[h + 1].clone();
+                    self.target.push(list_val);
+                }
 
                 self.reinstantiate_var(addr, threshold);
-                *self.value_at_scan() = HeapCellValue::Addr(Addr::AttrVar(threshold));
+                *self.value_at_scan() = HeapCellValue::Addr(redirect_tag(threshold));
             }
             _ if addr == rd => {
                 let scan = self.scan;
@@ -185,8 +225,8 @@ impl<T: CopierTarget> CopyTermState<T> {
                 HeapCellValue::Addr(addr) => match addr {
                     Addr::Lis(addr) => self.copy_list(addr),
                     addr @ Addr::AttrVar(_)
-                    | addr @ Addr::HeapCell(_)
-                    | addr @ Addr::StackCell(..) => self.copy_var(addr),
+                  | addr @ Addr::HeapCell(_)
+                  | addr @ Addr::StackCell(..) => self.copy_var(addr),
                     Addr::Str(addr) => self.copy_structure(addr),
                     Addr::Con(_) | Addr::DBRef(_) => self.scan += 1,
                 },
index d09b5745bc6034918291eddd9e53aae91ec7c863..3d451f7da6411f3987a5f13cb366e0070821c3b7 100644 (file)
@@ -729,7 +729,7 @@ pub(crate) trait CallPolicy: Any {
                 return_from_clause!(machine_st.last_call, machine_st)
             }
             &BuiltInClauseType::CopyTerm => {
-                machine_st.copy_term();
+                machine_st.copy_term(AttrVarPolicy::DeepCopy);
                 return_from_clause!(machine_st.last_call, machine_st)
             }
             &BuiltInClauseType::Eq => {
index f48d77ff3d8a985303ced86873e4ae0a995dd705..9b913e83907660e9dcaefe422efe31c6b5f1fa45 100644 (file)
@@ -2044,6 +2044,7 @@ impl MachineState {
         copy_term(
             CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut self.ball.stub),
             addr,
+            AttrVarPolicy::DeepCopy,
         );
     }
 
@@ -2941,13 +2942,13 @@ impl MachineState {
         }
     }
 
-    pub(super) fn copy_term(&mut self) {
+    pub(super) fn copy_term(&mut self, attr_var_policy: AttrVarPolicy) {
         let old_h = self.heap.h;
 
         let a1 = self[temp_v!(1)].clone();
         let a2 = self[temp_v!(2)].clone();
 
-        copy_term(CopyTerm::new(self), a1);
+        copy_term(CopyTerm::new(self), a1, attr_var_policy);
         self.unify(Addr::HeapCell(old_h), a2);
     }
 
index 7349bdf5095e3e8a4f68602d89a4b475f5cfd546..60980406b22be1f1d3d816931d0126ec3c2248a6 100644 (file)
@@ -83,3 +83,10 @@ gather_modules_for_attrs(Attrs, Modules, Modules) :-
 gather_modules_for_attrs([Attr|Attrs], [Module|Modules], Modules0) :-
     '$module_of'(Module, Attr),
     gather_modules_for_attrs(Attrs, Modules, Modules0).
+
+copy_term(Source, Dest, Goals) :-
+    term_variables(Source, Vars),
+    gather_modules(Vars, Modules, _),
+    call_attribute_goals(Modules, call_query_var_goals, Vars),
+    '$fetch_attribute_goals'(Goals0),
+    '$copy_term_without_attr_vars'([Source | Goals0], [Dest | Goals]).
index e168849b896219cf62b38e0da1d7cfd557bb1e2b..f66ef2749705d3547f4d86511d77d92749b56ca9 100644 (file)
@@ -353,7 +353,7 @@ impl MachineState {
         copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 3)));
         copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 2)));
 
-        copy_term(copy_ball_term, copy_target);
+        copy_term(copy_ball_term, copy_target, AttrVarPolicy::DeepCopy);
         threshold + lh_offset + 2
     }
 
@@ -867,6 +867,9 @@ impl MachineState {
                     _ => self.fail = true,
                 };
             }
+            &SystemClauseType::CopyTermWithoutAttrVars => {
+                self.copy_term(AttrVarPolicy::StripAttributes);
+            }
             &SystemClauseType::FetchGlobalVar => {
                 let key = self[temp_v!(1)].clone();
 
@@ -1364,7 +1367,7 @@ impl MachineState {
                 self.truncate_if_no_lifted_heap_diff(|_| Addr::Con(Constant::EmptyList))
             }
             &SystemClauseType::FetchAttributeGoals => {
-                let mut attr_goals = mem::replace(&mut self.attr_var_init.attribute_goals, vec![]);
+                let mut attr_goals = self.attr_var_init.attribute_goals.clone();
 
                 attr_goals.sort_unstable_by(|a1, a2| self.compare_term_test(a1, a2));
                 self.term_dedup(&mut attr_goals);
@@ -1667,6 +1670,7 @@ impl MachineState {
                 copy_term(
                     CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
                     value,
+                    AttrVarPolicy::DeepCopy,
                 );
 
                 let offset = self[temp_v!(3)].clone();
@@ -1905,7 +1909,7 @@ impl MachineState {
                     ContinueResult::ContinueQuery => ';',
                     ContinueResult::Conclude => '.'
                 };
-
+                
                 let target = self[temp_v!(1)].clone();
                 self.unify(Addr::Con(Constant::Char(c)), target);
             }
@@ -1970,6 +1974,7 @@ impl MachineState {
                 copy_term(
                     CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
                     value,
+                    AttrVarPolicy::DeepCopy,
                 );
 
                 indices.global_variables.insert(key, (ball, None));
@@ -1990,6 +1995,7 @@ impl MachineState {
                 copy_term(
                     CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
                     value.clone(),
+                    AttrVarPolicy::DeepCopy,
                 );
 
                 let stub = ball.copy_and_align(h);