]> Repositorios git - scryer-prolog.git/commitdiff
add order preserving tidy_trail, fix random_labeling/2 v0.8.114
authorMark Thom <[email protected]>
Sat, 19 Oct 2019 06:29:50 +0000 (00:29 -0600)
committerMark Thom <[email protected]>
Sat, 19 Oct 2019 06:29:50 +0000 (00:29 -0600)
Cargo.toml
src/prolog/lib/atts.pl
src/prolog/lib/non_iso.pl
src/prolog/machine/attributed_variables.pl
src/prolog/machine/attributed_variables.rs
src/prolog/machine/machine_indices.rs
src/prolog/machine/machine_state_impl.rs
src/prolog/machine/mod.rs
src/prolog/machine/system_calls.rs

index eb7d6107020799504f0b62c6b91521232daebaf3..c39d3b12c4bc17e46ad6187aa4d70eed8dd3a898 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "scryer-prolog"
-version = "0.8.113"
+version = "0.8.114"
 authors = ["Mark Thom <[email protected]>"]
 build = "build.rs"
 repository = "https://github.com/mthom/scryer-prolog"
index 25a84fc50d2365f2876e9129e6df64f66f1ac3b1..5de6d32c403b3ff970b755a07df907c352796c22 100644 (file)
@@ -20,7 +20,7 @@
 
 '$default_attr_list'([PG | PGs], Module, AttrVar) -->
     (  { '$module_of'(Module, PG) } -> [Module:put_atts(AttrVar, PG)]
-    ;  true
+    ;  { true }
     ),
     '$default_attr_list'(PGs, Module, AttrVar).
 '$default_attr_list'([], _, _) --> [].
index e4a45c955a148812a4cc8f99d1f341aa680fcba2..84595e83591fcd3e42df14b0fe793a883624dde4 100644 (file)
@@ -130,11 +130,12 @@ variant(X, Y) :- '$variant'(X, Y).
 maybe :- '$maybe'.
 
 set_random(Seed) :-
-    (  nonvar(Seed) -> 
+    (  nonvar(Seed) ->
        (  Seed = seed(S) ->
-         (  integer(S) -> '$set_seed'(S)
+         (  var(S) -> throw(error(instantiation_error, set_random/1))
+         ;  integer(S) -> '$set_seed'(S)
          ;  throw(error(type_error(integer(S), set_random/1)))
-         )    
+         )
        )
     ;  throw(error(instantiation_error, set_random/1))
-    ).    
+    ).
index f7b5493c5e49e084a1e18de602b2f1f4a98b7bee..1323d2f5536c1582ebc1152a4e6f7d2e206e4716 100644 (file)
@@ -25,6 +25,7 @@ verify_attrs([], _, _, []).
 
 call_verify_attributes(Attrs, _, _, []) :-
     var(Attrs), !.
+call_verify_attributes([], _, _, []).
 call_verify_attributes([Attr|Attrs], Var, Value, ListOfGoalLists) :-
     gather_modules([Attr|Attrs], Modules0),
     sort(Modules0, Modules),
index c1af6aabf67cd730e64cd1fa3ae5920e9dddc299..638c403b7581d7412de61d41d5b52b20c46b54bf 100644 (file)
@@ -14,6 +14,7 @@ pub(super) struct AttrVarInitializer {
     pub(super) attr_var_queue: Vec<usize>,
     pub(super) bindings: Bindings,
     pub(super) cp: LocalCodePtr,
+    pub(super) instigating_p: LocalCodePtr,
     pub(super) verify_attrs_loc: usize,
     pub(super) project_attrs_loc: usize,
 }
@@ -24,6 +25,7 @@ impl AttrVarInitializer {
             attribute_goals: vec![],
             attr_var_queue: vec![],
             bindings: vec![],
+            instigating_p: LocalCodePtr::default(),
             cp: LocalCodePtr::default(),
             verify_attrs_loc,
             project_attrs_loc,
@@ -41,10 +43,12 @@ impl AttrVarInitializer {
 impl MachineState {
     pub(super) fn push_attr_var_binding(&mut self, h: usize, addr: Addr) {
         if self.attr_var_init.bindings.is_empty() {
+            self.attr_var_init.instigating_p = self.p.local();
+            
             if self.last_call {
                 self.attr_var_init.cp = self.cp;
             } else {
-                self.attr_var_init.cp = self.p.local();
+                self.attr_var_init.cp = self.p.local() + 1;
             }
             
             self.p = CodePtr::VerifyAttrInterrupt(self.attr_var_init.verify_attrs_loc);
@@ -201,7 +205,7 @@ impl Machine {
             &mut self.code_repo,
             &mut readline::input_stream(),
         );
-
+               
         self.machine_st
             .print_attribute_goals_string(&self.indices.op_dir)
     }
index db642a5591587fcc3f7a8b04d3c3be0146e93a25..40fda8a7b7702559cc1898bac8811a42c8ee577b 100644 (file)
@@ -469,6 +469,12 @@ pub struct IndexStore {
 }
 
 impl IndexStore {
+    pub fn reset_global_variable_offsets(&mut self) {
+        for (_, ref mut offset) in self.global_variables.values_mut() {
+            *offset = None;
+        }
+    }
+    
     pub fn predicate_exists(
         &self,
         name: ClauseName,
index 81884ffff48d9f1e14273ff38337392a153ed482..32a067745ef1eac11b9c0028e0808a51cd258c53 100644 (file)
@@ -741,29 +741,21 @@ impl MachineState {
         }
 
         let b = self.b - 1;
-        let mut i = self.or_stack[b].tr;
+        let hb = self.hb;
+        let mut offset = 0;
 
-        while i < self.tr {
-            let tr_i = self.trail[i];
-            let hb = self.hb;
-
-            match tr_i {
+        for i in self.or_stack[b].tr .. self.tr {
+            match self.trail[i] {
                 TrailRef::Ref(Ref::AttrVar(tr_i))
               | TrailRef::Ref(Ref::HeapCell(tr_i))
               | TrailRef::AttrVarHeapLink(tr_i)
-              | TrailRef::AttrVarListLink(tr_i, _) => {
-                    if tr_i < hb {
-                        i += 1;
-                    } else {
-                        let tr = self.tr;
-                        let val = self.trail[tr - 1].clone();
-                        self.trail[i] = val;
-                        self.trail.pop();
-                        self.tr -= 1;
-                    }
-                }
+              | TrailRef::AttrVarListLink(tr_i, _) =>
+                  if tr_i >= hb {
+                      offset += 1;
+                  } else {
+                      self.trail[i - offset] = self.trail[i];
+                  },     
                 TrailRef::Ref(Ref::StackCell(fr, _)) => {
-                    let b = self.b - 1;
                     let fr_gi = self.and_stack[fr].global_index;
                     let b_gi = if !self.or_stack.is_empty() {
                         self.or_stack[b].global_index
@@ -771,20 +763,19 @@ impl MachineState {
                         0
                     };
 
-                    if fr_gi < b_gi {
-                        i += 1;
+                    if fr_gi >= b_gi {
+                        offset += 1;
                     } else {
-                        let tr = self.tr;
-                        let val = self.trail[tr - 1];
-                        self.trail[i] = val;
-                        self.trail.pop();
-                        self.tr -= 1;
+                        self.trail[i - offset] = self.trail[i];
                     }
                 }
-            };
+            }
         }
-    }
 
+        self.tr -= offset;
+        self.trail.truncate(self.tr);
+    }
+        
     #[inline]
     fn write_char_to_string(&mut self, s: &mut StringList, c: char) -> bool {
         self.pstr_trail(s.clone());
@@ -1675,7 +1666,7 @@ impl MachineState {
         } else if s.is_expandable() {
             self.heap
                 .push(HeapCellValue::Addr(Addr::Con(Constant::String(s.clone()))));
-            
+
             self.s = h;
             self.mode = MachineMode::Read;
         } else {
@@ -3434,56 +3425,4 @@ impl MachineState {
         self.heap_locs.clear();
         self.lifted_heap.clear();
     }
-
-    pub(super) fn sink_to_snapshot(&mut self) -> MachineState {
-        let mut snapshot = MachineState::with_capacity(0);
-
-        snapshot.hb = self.hb;
-        snapshot.e = self.e;
-        snapshot.b = self.b;
-        snapshot.b0 = self.b0;
-        snapshot.s = self.s;
-        snapshot.tr = self.tr;
-        snapshot.pstr_tr = self.pstr_tr;
-        snapshot.num_of_args = self.num_of_args;
-
-        snapshot.fail = self.fail;
-        snapshot.trail = mem::replace(&mut self.trail, vec![]);
-        snapshot.pstr_trail = mem::replace(&mut self.pstr_trail, vec![]);
-        snapshot.heap = self.heap.take();
-        snapshot.mode = self.mode;
-        snapshot.and_stack = self.and_stack.take();
-        snapshot.or_stack = self.or_stack.take();
-        snapshot.registers = mem::replace(&mut self.registers, vec![]);
-        snapshot.block = self.block;
-
-        snapshot.ball = self.ball.take();
-        snapshot.lifted_heap = mem::replace(&mut self.lifted_heap, vec![]);
-
-        snapshot
-    }
-
-    pub(super) fn absorb_snapshot(&mut self, mut snapshot: MachineState) {
-        self.hb = snapshot.hb;
-        self.e = snapshot.e;
-        self.b = snapshot.b;
-        self.b0 = snapshot.b0;
-        self.s = snapshot.s;
-        self.tr = snapshot.tr;
-        self.pstr_tr = snapshot.pstr_tr;
-        self.num_of_args = snapshot.num_of_args;
-
-        self.fail = snapshot.fail;
-        self.trail = mem::replace(&mut snapshot.trail, vec![]);
-        self.pstr_trail = mem::replace(&mut snapshot.pstr_trail, vec![]);
-        self.heap = snapshot.heap.take();
-        self.mode = snapshot.mode;
-        self.and_stack = snapshot.and_stack.take();
-        self.or_stack = snapshot.or_stack.take();
-        self.registers = mem::replace(&mut snapshot.registers, vec![]);
-        self.block = snapshot.block;
-
-        self.ball = snapshot.ball.take();
-        self.lifted_heap = mem::replace(&mut snapshot.lifted_heap, vec![]);
-    }
 }
index 1c6b4505162c5607752a0d8ae1748aaa22214a92..21ed9670a683b715a50a1a692101a004ad9238ef 100644 (file)
@@ -6,6 +6,7 @@ use crate::prolog::fixtures::*;
 use crate::prolog::forms::*;
 use crate::prolog::heap_print::*;
 use crate::prolog::instructions::*;
+use crate::prolog::machine::heap::Heap;
 use crate::prolog::read::*;
 use crate::prolog::write::{next_keypress, ContinueResult};
 
@@ -71,6 +72,7 @@ impl MachinePolicies {
 
 pub struct Machine {
     pub(super) machine_st: MachineState,
+    pub(super) inner_heap: Heap,
     pub(super) policies: MachinePolicies,
     pub(super) indices: IndexStore,
     pub(super) code_repo: CodeRepo,
@@ -227,13 +229,13 @@ impl Machine {
     }
 
     pub fn run_init_code(&mut self, code: Code) {
-       let old_machine_st = self.machine_st.sink_to_snapshot();
+       let old_machine_st = self.sink_to_snapshot();
        self.machine_st.reset();
 
        self.code_repo.cached_query = code;
        self.run_query(&AllocVarDict::new());
 
-       self.machine_st.absorb_snapshot(old_machine_st);
+       self.absorb_snapshot(old_machine_st);
     }
 
     pub fn run_top_level(&mut self) {
@@ -260,6 +262,7 @@ impl Machine {
     pub fn new(prolog_stream: PrologStream) -> Self {
         let mut wam = Machine {
             machine_st: MachineState::new(),
+            inner_heap: Heap::with_capacity(256 * 256),
             policies: MachinePolicies::new(),
             indices: IndexStore::new(),
             code_repo: CodeRepo::new(),
@@ -570,9 +573,14 @@ impl Machine {
                 };
 
                 let stream = parsing_stream(s.as_bytes());
+                let snapshot = self.sink_to_snapshot();
+                let policies = mem::replace(&mut self.policies, MachinePolicies::new());
 
-                let snapshot = self.machine_st.sink_to_snapshot();
                 self.machine_st.reset();
+                self.machine_st.heap = mem::replace(
+                    &mut self.inner_heap,
+                    Heap::with_capacity(0),
+                );
 
                 let result = match stream_to_toplevel(stream, self) {
                     Ok(packet) => compile_term(self, packet),
@@ -580,6 +588,8 @@ impl Machine {
                 };
 
                 self.handle_eval_session(result, snapshot);
+                self.indices.reset_global_variable_offsets();
+                self.policies = policies;
             }
            REPLCodePtr::UseModule =>
                self.use_module(ModuleSource::Library),
@@ -594,10 +604,66 @@ impl Machine {
         self.machine_st.p = CodePtr::Local(p);
     }
 
+    fn sink_to_snapshot(&mut self) -> MachineState {
+        let mut snapshot = MachineState::with_capacity(0);
+
+        snapshot.hb = self.machine_st.hb;
+        snapshot.e = self.machine_st.e;
+        snapshot.b = self.machine_st.b;
+        snapshot.b0 = self.machine_st.b0;
+        snapshot.s = self.machine_st.s;
+        snapshot.tr = self.machine_st.tr;
+        snapshot.pstr_tr = self.machine_st.pstr_tr;
+        snapshot.num_of_args = self.machine_st.num_of_args;
+
+        snapshot.fail = self.machine_st.fail;
+        snapshot.trail = mem::replace(&mut self.machine_st.trail, vec![]);
+        snapshot.pstr_trail = mem::replace(&mut self.machine_st.pstr_trail, vec![]);
+        snapshot.heap = self.machine_st.heap.take();
+        snapshot.mode = self.machine_st.mode;
+        snapshot.and_stack = self.machine_st.and_stack.take();
+        snapshot.or_stack = self.machine_st.or_stack.take();
+        snapshot.registers = mem::replace(&mut self.machine_st.registers, vec![]);
+        snapshot.block = self.machine_st.block;
+
+        snapshot.ball = self.machine_st.ball.take();
+        snapshot.lifted_heap = mem::replace(&mut self.machine_st.lifted_heap, vec![]);
+
+        snapshot
+    }
+
+    fn absorb_snapshot(&mut self, mut snapshot: MachineState) {
+        self.machine_st.hb = snapshot.hb;
+        self.machine_st.e = snapshot.e;
+        self.machine_st.b = snapshot.b;
+        self.machine_st.b0 = snapshot.b0;
+        self.machine_st.s = snapshot.s;
+        self.machine_st.tr = snapshot.tr;
+        self.machine_st.pstr_tr = snapshot.pstr_tr;
+        self.machine_st.num_of_args = snapshot.num_of_args;
+
+        self.machine_st.fail = snapshot.fail;
+        self.machine_st.trail = mem::replace(&mut snapshot.trail, vec![]);
+        self.machine_st.pstr_trail = mem::replace(&mut snapshot.pstr_trail, vec![]);
+
+        self.inner_heap = self.machine_st.heap.take();
+        self.inner_heap.truncate(0);
+        
+        self.machine_st.heap = snapshot.heap.take();
+        self.machine_st.mode = snapshot.mode;
+        self.machine_st.and_stack = snapshot.and_stack.take();
+        self.machine_st.or_stack = snapshot.or_stack.take();
+        self.machine_st.registers = mem::replace(&mut snapshot.registers, vec![]);
+        self.machine_st.block = snapshot.block;
+
+        self.machine_st.ball = snapshot.ball.take();
+        self.machine_st.lifted_heap = mem::replace(&mut snapshot.lifted_heap, vec![]);
+    }
+
     fn propagate_exception_to_toplevel(&mut self, snapshot: MachineState) {
         let ball = self.machine_st.ball.take();
 
-        self.machine_st.absorb_snapshot(snapshot);
+        self.absorb_snapshot(snapshot);
         self.machine_st.ball = ball;
 
         let h = self.machine_st.heap.h;
@@ -617,7 +683,7 @@ impl Machine {
                 };
 
                 let attr_goals = self.attribute_goals();
-
+                
                 if !(self.machine_st.b > 0) {
                     if bindings.is_empty() {
                         let space = if requires_space(&attr_goals, ".") {
@@ -632,7 +698,7 @@ impl Machine {
                             println!("true.");
                         }
 
-                        self.machine_st.absorb_snapshot(snapshot);
+                        self.absorb_snapshot(snapshot);
                         return;
                     }
                 } else if bindings.is_empty() && attr_goals.is_empty() {
@@ -664,7 +730,7 @@ impl Machine {
                         }
                         ContinueResult::Conclude => {
                            print!(" ...\r\n");
-                            self.machine_st.absorb_snapshot(snapshot);
+                            self.absorb_snapshot(snapshot);
                             return;
                         }
                     };
@@ -676,12 +742,12 @@ impl Machine {
                                 return;
                             } else {
                                print!("false.\r\n");
-                                self.machine_st.absorb_snapshot(snapshot);
+                                self.absorb_snapshot(snapshot);
                                 return;
                             }
                         }
                         EvalSession::Error(err) => {
-                            self.machine_st.absorb_snapshot(snapshot);
+                            self.absorb_snapshot(snapshot);
                             self.throw_session_error(err, (clause_name!("repl"), 0));
                             return;
                         }
@@ -712,7 +778,7 @@ impl Machine {
                 }
             },
             EvalSession::Error(err) => {
-                self.machine_st.absorb_snapshot(snapshot);
+                self.absorb_snapshot(snapshot);
                 self.throw_session_error(err, (clause_name!("repl"), 0));
                 return;
             }
@@ -725,7 +791,7 @@ impl Machine {
             _ => println!("true.")
         }
 
-        self.machine_st.absorb_snapshot(snapshot);
+        self.absorb_snapshot(snapshot);
     }
 
     pub(super) fn run_query(&mut self, alloc_locs: &AllocVarDict) {
@@ -1048,7 +1114,13 @@ impl MachineState {
                 CodePtr::VerifyAttrInterrupt(_) => {
                     self.p = CodePtr::Local(self.attr_var_init.cp);
 
-                    if !self.verify_attr_stepper(indices, policies, code_repo, prolog_stream) {
+                    let instigating_p = CodePtr::Local(self.attr_var_init.instigating_p);
+                    let instigating_instr = code_repo.lookup_instr(false, &instigating_p).unwrap();
+
+                    if instigating_instr.as_ref().is_head_instr() {
+                        let cp = self.p.local();
+                        self.run_verify_attr_interrupt(cp);
+                    } else if !self.verify_attr_stepper(indices, policies, code_repo, prolog_stream) {
                         if self.fail {
                             break;
                         }
index 5673b149cd4c7571ff641d44b3bab7cab86f8f31..483ceeebd1a357ec957c9160cd958c6d998eafbd 100644 (file)
@@ -1686,7 +1686,7 @@ impl MachineState {
                 return Ok(());
             }
             &SystemClauseType::ReturnFromAttributeGoals => {
-                self.deallocate();
+                self.deallocate();                
                 self.p = CodePtr::Local(LocalCodePtr::TopLevel(0, 0));
                 return Ok(());
             }