From cc7e21170ff3f35acadfd1a3313dbdb5382d78c0 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Mon, 8 Feb 2021 18:46:33 -0700 Subject: [PATCH] correct faulty removal of clauses in abolish/1 --- src/forms.rs | 18 +++- src/machine/compile.rs | 10 +- src/machine/loader.rs | 168 ++++++++++++++++++++------------- src/machine/machine_indices.rs | 22 ++++- src/machine/system_calls.rs | 2 +- 5 files changed, 148 insertions(+), 72 deletions(-) diff --git a/src/forms.rs b/src/forms.rs index 11090601..408d691d 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -711,5 +711,21 @@ impl PredicateSkeleton { clauses: self.clauses, } } - */ + */ + + pub fn target_pos_of_clause_clause_loc( + &self, + clause_clause_loc: usize, + clause_assert_margin: usize, + ) -> usize { + let search_result = self.clause_clause_locs[0..clause_assert_margin] + .binary_search_by(|loc| clause_clause_loc.cmp(&loc)); + + search_result.unwrap_or_else(|_| { + self.clause_clause_locs[clause_assert_margin..] + .binary_search_by(|loc| loc.cmp(&clause_clause_loc)) + .unwrap() + + clause_assert_margin + }) + } } diff --git a/src/machine/compile.rs b/src/machine/compile.rs index af25b3e8..faac9421 100644 --- a/src/machine/compile.rs +++ b/src/machine/compile.rs @@ -1047,7 +1047,7 @@ impl<'a> LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&self.compilation_target, &key) + .get_predicate_skeleton_mut(&self.compilation_target, &key) { Some(skeleton) => { self.retraction_info @@ -1120,7 +1120,7 @@ impl<'a> LoadState<'a> { let skeleton = match self .wam .indices - .get_predicate_skeleton(&self.compilation_target, &key) + .get_predicate_skeleton_mut(&self.compilation_target, &key) { Some(skeleton) if !skeleton.clauses.is_empty() => skeleton, _ => { @@ -1212,7 +1212,7 @@ impl<'a> LoadState<'a> { let skeleton = match self .wam .indices - .get_predicate_skeleton(&self.compilation_target, &key) + .get_predicate_skeleton_mut(&self.compilation_target, &key) { Some(skeleton) => skeleton, None => { @@ -1540,7 +1540,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { } } - match self.load_state.wam.indices.get_predicate_skeleton( + match self.load_state.wam.indices.get_predicate_skeleton_mut( &self.load_state.compilation_target, &(clause_name!("$clause"), 2), ) { @@ -1563,7 +1563,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .load_state .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) if append_or_prepend.is_append() => { self.load_state.retraction_info.push_record( diff --git a/src/machine/loader.rs b/src/machine/loader.rs index e713916f..90364ce7 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -199,7 +199,7 @@ impl<'a> Drop for LoadState<'a> { RetractionRecord::AppendedModuleExtensiblePredicate(module_name, key) => { self.wam .indices - .get_predicate_skeleton(&CompilationTarget::Module(module_name), &key) + .get_predicate_skeleton_mut(&CompilationTarget::Module(module_name), &key) .map(|skeleton| { skeleton.clauses.pop_back(); }); @@ -207,7 +207,7 @@ impl<'a> Drop for LoadState<'a> { RetractionRecord::PrependedModuleExtensiblePredicate(module_name, key) => { self.wam .indices - .get_predicate_skeleton(&CompilationTarget::Module(module_name), &key) + .get_predicate_skeleton_mut(&CompilationTarget::Module(module_name), &key) .map(|skeleton| { skeleton.clauses.pop_front(); }); @@ -266,7 +266,7 @@ impl<'a> Drop for LoadState<'a> { RetractionRecord::AppendedUserExtensiblePredicate(key) => { self.wam .indices - .get_predicate_skeleton(&CompilationTarget::User, &key) + .get_predicate_skeleton_mut(&CompilationTarget::User, &key) .map(|skeleton| { skeleton.clauses.pop_back(); }); @@ -274,7 +274,7 @@ impl<'a> Drop for LoadState<'a> { RetractionRecord::PrependedUserExtensiblePredicate(key) => { self.wam .indices - .get_predicate_skeleton(&CompilationTarget::User, &key) + .get_predicate_skeleton_mut(&CompilationTarget::User, &key) .map(|skeleton| { skeleton.clauses.pop_front(); }); @@ -412,7 +412,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton.clause_clause_locs.truncate_front(len); @@ -430,7 +430,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &(clause_name!("$clause"), 2)) + .get_predicate_skeleton_mut(&compilation_target, &(clause_name!("$clause"), 2)) { Some(skeleton) => { skeleton.clause_clause_locs.truncate_front(len); @@ -446,7 +446,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton.clause_clause_locs.truncate_back(len); @@ -464,7 +464,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &(clause_name!("$clause"), 2)) + .get_predicate_skeleton_mut(&compilation_target, &(clause_name!("$clause"), 2)) { Some(skeleton) => { skeleton.clause_clause_locs.truncate_back(len); @@ -476,7 +476,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton.clauses.pop_back(); @@ -488,7 +488,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton.clauses.pop_front(); @@ -500,7 +500,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton.clauses.truncate_back(len); @@ -517,7 +517,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton.clauses[target_pos].clause_start = clause_start; @@ -535,7 +535,7 @@ impl<'a> Drop for LoadState<'a> { match self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) => { skeleton @@ -1365,7 +1365,7 @@ impl Machine { .load_state .wam .indices - .get_predicate_skeleton(&compilation_target, &key) + .get_predicate_skeleton_mut(&compilation_target, &key) .map(|skeleton| skeleton.is_dynamic = true); loader.compile_clause_clauses( @@ -1407,53 +1407,98 @@ impl Machine { _ => CompilationTarget::Module(module_name), }; - let mut loader = Loader::new(LiveTermStream::new(ListingSource::User), self); - loader.load_state.compilation_target = compilation_target; + let abolish_clause = || { + let mut loader = Loader::new(LiveTermStream::new(ListingSource::User), self); + loader.load_state.compilation_target = compilation_target; - match loader - .load_state - .wam - .indices - .get_predicate_skeleton(&loader.load_state.compilation_target, &key) - { - Some(skeleton) => { - skeleton.clauses.clear(); - skeleton.clause_clause_locs.clear(); - } - _ => { - unreachable!(); - } - } + let clause_clause_compilation_target = + match &loader.load_state.compilation_target { + CompilationTarget::User => { + CompilationTarget::Module(clause_name!("builtins")) + } + module => { + module.clone() + } + }; + + let mut clause_assert_margin = loader + .load_state + .wam + .indices + .modules + .get(&clause_clause_compilation_target.module_name()) + .map(|module| module.clause_assert_margin) + .unwrap(); - let code_index = loader.load_state.get_or_insert_code_index(key); - code_index.set(IndexPtr::DynamicUndefined); + let mut clause_clause_target_poses: Vec<_> = loader + .load_state + .wam + .indices + .get_predicate_skeleton(&loader.load_state.compilation_target, &key) + .map(|skeleton| { + loader + .load_state + .wam + .indices + .get_predicate_skeleton( + &clause_clause_compilation_target, + &(clause_name!("$clause"), 2), + ) + .map(|clause_clause_skeleton| { + skeleton.clause_clause_locs + .iter() + .map(|clause_clause_loc| { + clause_clause_skeleton.target_pos_of_clause_clause_loc( + *clause_clause_loc, + clause_assert_margin, + ) + }) + .collect() + }) + .unwrap() + }) + .unwrap(); - match loader.load_state.compilation_target { - CompilationTarget::User => { - loader.load_state.compilation_target = - CompilationTarget::Module(clause_name!("builtins")); + loader + .load_state + .wam + .indices + .get_predicate_skeleton_mut(&loader.load_state.compilation_target, &key) + .map(|skeleton| { + skeleton.clauses.clear(); + skeleton.clause_clause_locs.clear(); + }); + + let code_index = loader.load_state.get_or_insert_code_index(key); + code_index.set(IndexPtr::DynamicUndefined); + + loader.load_state.compilation_target = clause_clause_compilation_target; + + while let Some(target_pos) = clause_clause_target_poses.pop() { + loader.load_state.retract_clause((clause_name!("$clause"), 2), target_pos); + + if target_pos < clause_assert_margin { + clause_assert_margin -= 1; + } } - _ => {} + + loader + .load_state + .wam + .indices + .modules + .get_mut(&loader.load_state.compilation_target.module_name()) + .map(|module| module.clause_assert_margin = clause_assert_margin); + + LiveTermStream::evacuate(loader) }; - match loader.load_state.wam.indices.get_predicate_skeleton( - &loader.load_state.compilation_target, - &(clause_name!("$clause"), 2), - ) { - Some(skeleton) => { - skeleton.clauses.clear(); - skeleton.clause_clause_locs.clear(); - } - _ => { - unreachable!(); + match abolish_clause() { + Ok(_) => {} + Err(e) => { + self.throw_session_error(e, (clause_name!("abolish"), 1)); } } - - let clause_clause_code_index = loader - .load_state - .get_or_insert_code_index((clause_name!("$clause"), 2)); - - clause_clause_code_index.set(IndexPtr::DynamicUndefined); } pub(crate) fn retract_clause(&mut self) { @@ -1507,17 +1552,12 @@ impl Machine { &(clause_name!("$clause"), 2), ) { Some(skeleton) => { - let search_result = skeleton.clause_clause_locs[0..clause_assert_margin] - .binary_search_by(|loc| clause_clause_loc.cmp(&loc)); - - let result = search_result.unwrap_or_else(|_| { - skeleton.clause_clause_locs[clause_assert_margin..] - .binary_search_by(|loc| loc.cmp(&clause_clause_loc)) - .unwrap() - + clause_assert_margin - }); + let target_pos = skeleton.target_pos_of_clause_clause_loc( + clause_clause_loc, + clause_assert_margin, + ); - if result < clause_assert_margin { + if target_pos < clause_assert_margin { loader .load_state .wam @@ -1527,7 +1567,7 @@ impl Machine { .map(|module| module.clause_assert_margin -= 1); } - result + target_pos } None => { unreachable!(); diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index 6bb9dffd..cb39fede 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -720,7 +720,7 @@ impl Default for IndexStore { } impl IndexStore { - pub fn get_predicate_skeleton( + pub fn get_predicate_skeleton_mut( &mut self, compilation_target: &CompilationTarget, key: &PredicateKey, @@ -740,6 +740,26 @@ impl IndexStore { } } + pub fn get_predicate_skeleton( + &self, + compilation_target: &CompilationTarget, + key: &PredicateKey, + ) -> Option<&PredicateSkeleton> { + match (key.0.as_str(), key.1) { + ("term_expansion", 2) => self.extensible_predicates.get(key), + _ => match compilation_target { + CompilationTarget::User => self.extensible_predicates.get(key), + CompilationTarget::Module(ref module_name) => { + if let Some(module) = self.modules.get(module_name) { + module.extensible_predicates.get(key) + } else { + None + } + } + }, + } + } + pub fn remove_predicate_skeleton( &mut self, compilation_target: &CompilationTarget, diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index a7574333..8251c3fe 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -4631,7 +4631,7 @@ impl MachineState { copy_term( CopyBallTerm::new(&mut self.stack, &mut self.heap, &mut ball.stub), - value.clone(), + value, AttrVarPolicy::DeepCopy, ); -- 2.54.0