From f14c8a2aeab18a9bfa380df28f94736580e08f48 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 9 Aug 2023 11:42:03 +0800 Subject: Fix a bug of using incorrect forest nodes to plant. Previously some incorrect forest nodes will be used for planting new nodes. I cannot fix the root cause of their presence in the chain-rule machine. But I can ignore them when they are encountered. Of course I would like to really prevent them from existing, but still cannot figure out how. --- chain/src/default.rs | 19 ++++++++++++++-- chain/src/item/genins.rs | 11 ++++++---- chain/src/lib.rs | 9 ++++++-- src/lib.rs | 56 ++++++++++++++++-------------------------------- 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/chain/src/default.rs b/chain/src/default.rs index 1e0eba2..11f0c93 100644 --- a/chain/src/default.rs +++ b/chain/src/default.rs @@ -633,6 +633,7 @@ impl Chain for DefaultChain { let mut new_label = *child_label; new_label.set_true_source(true_pavi); + // new_label.set_forest_source(pavi); output.extend( std::iter::repeat(new_label.into()) @@ -723,6 +724,20 @@ impl Chain for DefaultChain { continue; } + #[allow(clippy::single_match)] + match label.forest_source() { + PaVi::Parent(node, _, _) => match self.forest.vertex_label(node) { + Ok(Some(label)) => match label.label().end() { + Some(end) if end != pos => { + continue; + } + _ => {} + }, + _ => {} + }, + _ => {} + } + let virtual_node = self .atom .atom(non, t) @@ -911,7 +926,7 @@ impl Chain for DefaultChain { // First perform reduction. - let _ = self.forest.print_viz("forest before reduction.gv"); + // let _ = self.forest.print_viz("forest before reduction.gv"); // self.forest.reduction(root, pos, ter, &self.atom, true)?; @@ -1070,7 +1085,7 @@ impl Chain for DefaultChain { self.atom.print_virtual(); let _ = item::default::print_labels(&self.atom, &self.forest); - let _ = self.forest.print_viz("forest before extraction.gv"); + // let _ = self.forest.print_viz("forest before extraction.gv"); result = self.forest.extract(pos)?; diff --git a/chain/src/item/genins.rs b/chain/src/item/genins.rs index 262728f..e3e79b6 100644 --- a/chain/src/item/genins.rs +++ b/chain/src/item/genins.rs @@ -212,7 +212,7 @@ impl DefaultForest> { // Whether or not to print detailed graphs of each step of // operation for debugging purposes. let mut to_print = false; - // let mut to_print = (8..=10).contains(&pos); + // let mut to_print = (11..=12).contains(&pos); if std::fs::metadata("output/").is_err() { to_print = false; @@ -419,8 +419,8 @@ impl DefaultForest> { .collect(); } - // if pos == 9 { - // dbg!(&parents); + // if pos == 12 { + // dbg!(num, &parents, pavi, label); // } let mut non_empty = false; @@ -535,7 +535,6 @@ impl DefaultForest> { parents, reduction_info, atom.query_reduction(label.label(), atom_child).unwrap(), - is_empty_segment, atom.trace(0, 3, atom_child) .into_iter() .flatten() @@ -616,6 +615,10 @@ impl DefaultForest> { unreachable!("the forest is wrongly planted"); } + // if pos == 11 { + // dbg!(root_label, node, edge, child); + // } + // dbg!(node, edge, root_label, leaf_label); PaVi::Parent(node, edge, child) } else { diff --git a/chain/src/lib.rs b/chain/src/lib.rs index bf3a8b4..f16ebb2 100644 --- a/chain/src/lib.rs +++ b/chain/src/lib.rs @@ -61,8 +61,13 @@ impl Edge { self.forest_source } - /// Return the associated bottom edge of the edge from which - /// onwards we shall perform the reduction. + /// Set the associated forest edge of the edge of the edge. + pub fn set_forest_source(&mut self, source: PaVi) { + self.forest_source = source; + } + + /// Set the associated bottom edge of the edge from which onwards + /// we shall perform the reduction. pub fn set_true_source(&mut self, true_source: PaVi) { self.true_source = true_source; } diff --git a/src/lib.rs b/src/lib.rs index aed8536..3483847 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -556,23 +556,15 @@ extern "C" fn parser_parse( fn read_label(label: *mut std::os::raw::c_uchar) -> ForestLabel { let status: u8 = unsafe { *label }; - let label_status: ForestLabelType; - - match status { - 0 => { - label_status = ForestLabelType::Plain; - } - 1 => { - label_status = ForestLabelType::Packed; - } - _ => { - label_status = ForestLabelType::Cloned(usize::from_be_bytes( - unsafe { std::slice::from_raw_parts(label.add(1), 8) } - .try_into() - .unwrap(), - )); - } - } + let label_status: ForestLabelType = match status { + 0 => ForestLabelType::Plain, + 1 => ForestLabelType::Packed, + _ => ForestLabelType::Cloned(usize::from_be_bytes( + unsafe { std::slice::from_raw_parts(label.add(1), 8) } + .try_into() + .unwrap(), + )), + }; let start = usize::from_be_bytes( unsafe { std::slice::from_raw_parts(label.add(9), 8) } @@ -594,19 +586,11 @@ fn read_label(label: *mut std::os::raw::c_uchar) -> ForestLabel { .unwrap(), ); - let inner_label: GrammarLabel; - - match discriminant { - 0 => { - inner_label = GrammarLabel::new_closed(TNT::Ter(content), start, end); - } - 1 => { - inner_label = GrammarLabel::new_closed(TNT::Non(content), start, end); - } - _ => { - inner_label = GrammarLabel::new_closed(content, start, end); - } - } + let inner_label: GrammarLabel = match discriminant { + 0 => GrammarLabel::new_closed(TNT::Ter(content), start, end), + 1 => GrammarLabel::new_closed(TNT::Non(content), start, end), + _ => GrammarLabel::new_closed(content, start, end), + }; ForestLabel::new(inner_label, label_status) } @@ -667,7 +651,7 @@ extern "C" fn print_forest( let special_marks = unsafe { std::slice::from_raw_parts((*forest_vec).data.add(8), 3) }; - if special_marks != &[114, 101, 112] { + if special_marks != [114, 101, 112] { return_error!( format!( "the forest does not begin with the special mark\nThe first bytes are: \ @@ -785,16 +769,12 @@ extern "C" fn print_forest( let result = format!("{preamble}{post}"); - let parsed_filename; - - match unsafe { std::ffi::CStr::from_ptr(filename).to_str() } { - Ok(ccstr) => { - parsed_filename = ccstr; - } + let parsed_filename = match unsafe { std::ffi::CStr::from_ptr(filename).to_str() } { + Ok(ccstr) => ccstr, Err(e) => { return_error!(format!("error: {e}"), error_len, error_cap, error_vec); } - } + }; if std::fs::metadata(parsed_filename).is_ok() { let _ = std::fs::remove_file(parsed_filename); -- cgit v1.2.3-18-g5258