From cb59c3003af10c872a1b13a36cae511ff2bfca94 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 1 Jan 2023 11:04:46 -0700 Subject: [PATCH] correct chunk type labeling --- Cargo.lock | 59 +++++++++++++++++++++++++++++++++++++++- src/fixtures.rs | 2 +- src/forms.rs | 10 +++++-- src/machine/disjuncts.rs | 44 ++++++++++++++++-------------- 4 files changed, 91 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7cc11f21..542afd09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2608,9 +2608,30 @@ dependencies = [ ] [[package]] -name = "windows_aarch64_gnullvm" +name = "windows-sys" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc 0.42.1", + "windows_i686_gnu 0.42.1", + "windows_i686_msvc 0.42.1", + "windows_x86_64_gnu 0.42.1", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc 0.42.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" [[package]] @@ -2625,6 +2646,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + [[package]] name = "windows_i686_gnu" version = "0.36.1" @@ -2637,6 +2664,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + [[package]] name = "windows_i686_msvc" version = "0.36.1" @@ -2649,6 +2682,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + [[package]] name = "windows_x86_64_gnu" version = "0.36.1" @@ -2667,6 +2706,18 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + [[package]] name = "windows_x86_64_msvc" version = "0.36.1" @@ -2679,6 +2730,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" + [[package]] name = "xmlparser" version = "0.13.5" diff --git a/src/fixtures.rs b/src/fixtures.rs index 5f73c716..9e1f28fe 100644 --- a/src/fixtures.rs +++ b/src/fixtures.rs @@ -148,7 +148,7 @@ impl VariableFixtures { ) { let chunk_num = term_loc.chunk_num(); - let mut status = self.temp_vars.swap_remove(generated_var_index).unwrap_or_else(|| { + let mut status = self.temp_vars.swap_remove(&generated_var_index).unwrap_or_else(|| { TempVarStatus { chunk_num, temp_var_data: TempVarData::new(classify_info.arity), diff --git a/src/forms.rs b/src/forms.rs index ac7649d5..9cc6f1ba 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -79,8 +79,8 @@ pub enum CallPolicy { Counted, } -#[derive(Debug, Clone, Copy)] -enum ChunkType { +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum ChunkType { Head, Mid, Last, @@ -95,6 +95,11 @@ impl ChunkType { ChunkType::Last => GenContext::Last(chunk_num), } } + + #[inline(always)] + pub fn is_last(self) -> bool { + self == ChunkType::Last + } } #[derive(Debug)] @@ -104,6 +109,7 @@ pub enum QueryTerm { Cut, Not(Vec), IfThen(Vec, Vec), + LocalCut(Cell), // for IfThen. Branch(Vec>), ChunkTypeBoundary(ChunkType), } diff --git a/src/machine/disjuncts.rs b/src/machine/disjuncts.rs index ee1711e6..24969ff3 100644 --- a/src/machine/disjuncts.rs +++ b/src/machine/disjuncts.rs @@ -202,8 +202,6 @@ pub struct VarRecord { pub num_occurrences: usize, } -// TODO: already exists a VarData! although it may no longer exist?? -// Also, the name is too similar to VarInfo. Think of better names! pub struct VarData { pub records: Vec, pub fixtures: VariableFixtures, @@ -243,47 +241,50 @@ fn flatten_into_disjunct(build_stack: &mut Vec, preceding_len: usize) if let QueryTerm::Branch(ref mut disjuncts) = &mut build_stack[preceding_len] { disjuncts.push(iter.collect()); + } else { + unreachable!(); } } fn term_in_other_chunk(term: &Term) -> Option { match term { Term::Clause(_, name, terms) => Some(!ClauseType::is_inbuilt(*name, terms.len())), - Term::Literal(_, Literal::Atom(atom!("!"))) | - Term::Literal(_, Literal::Char('!')) => Some(false), + Term::Literal(_, Literal::Atom(atom!("!")) | Literal::Char('!')) => Some(false), Term::Literal(_, Literal::Atom(name)) => Some(!ClauseType::is_inbuilt(*name, 0)), Term::Var(..) => Some(true), _ => None, } } -// returns true if the insertion of SetLastChunkType was the final push. +// returns true if SetLastChunkType was pushed. // expects that iter iterates over a conjunct of Terms in reverse order. fn insert_set_last_chunk_type( state_stack: &mut Vec, mut iter: impl Iterator, ) -> bool { let beg = state_stack.len(); - let mut idx = beg; + + let mut will_break = false; + let mut last_chunk_delim = beg; while let Some(traversal_st) = iter.next() { match traversal_st { TraversalState::Term(term) | TraversalState::BuildIf(_, term) => { - let mut will_break = false; + will_break = false; match term_in_other_chunk(&term) { - Some(true) if idx > beg => will_break = true, - Some(_) => idx += 1, + Some(true) if last_chunk_delim > beg => will_break = true, + Some(_) => last_chunk_delim += 1, None => will_break = true, } if will_break { + // recall that iter iterates in reverse order. + // therefore this is the correct push order. state_stack.push(TraversalState::SetLastChunkType); state_stack.push(traversal_st); break; - } else { - state_stack.push(traversal_st); } } _ => { @@ -293,7 +294,7 @@ fn insert_set_last_chunk_type( } state_stack.extend(iter); - idx == state_stack.len() + will_break } impl VariableClassifier { @@ -513,9 +514,11 @@ impl VariableClassifier { .chain(std::iter::once(terms[0])) .map(TraversalState::Term); - if let ChunkType::Last = chunk_type { - if !insert_set_last_chunk_type(&mut state_stack, iter) { - chunk_type = ChunkType::Mid; + if ChunkType::Mid != chunk_type { + if insert_set_last_chunk_type(&mut state_stack, iter) { + if chunk_type.is_last() { + chunk_type = ChunkType::Mid; + } } } else { state_stack.extend(iter); @@ -575,9 +578,11 @@ impl VariableClassifier { TraversalState::Term(if_term)] .into_iter(); - if let ChunkType::Last = chunk_type { - if !insert_set_last_chunk_type(&mut state_stack, iter) { - chunk_type = ChunkType::Mid; + if ChunkType::Mid != chunk_type { + if insert_set_last_chunk_type(&mut state_stack, iter) { + if chunk_type.is_last() { + chunk_type = ChunkType::Mid; + } } } } @@ -686,8 +691,7 @@ impl VariableClassifier { ), ); } - Term::Literal(_, Literal::Atom(atom!("!"))) | - Term::Literal(_, Literal::Char('!')) => { + Term::Literal(_, Literal::Atom(atom!("!")) | Literal::Char('!')) => { build_stack.push(QueryTerm::Cut); } Term::Literal(cell, Literal::Atom(name)) => { -- 2.54.0