summaryrefslogtreecommitdiff
path: root/chain/src/item/genins.rs
blob: 43807f8f0ddef3a9858f99fa9306e50e233cabf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! This module implements the generation and insertion of item
//! derivation forests.
//!
//! This is used for the chain-rule machine to conveniently produce
//! item derivations into a forest.  This forest can serve as a rough
//! approximation of the parse forests, and can be executed in other
//! semirings later on.

use super::*;
use crate::{atom::DefaultAtom, default::Error, item::default::DefaultForest, Edge};
use grammar::{Error as GrammarError, GrammarLabel, GrammarLabelType, TNT};
use graph::Graph;

use core::borrow::Borrow;
use std::collections::HashSet as Set;

/// Convert an error telling us that an index is out of bounds.
///
/// # Panics
///
/// The function panics if the error is not of the expected kind.
fn index_out_of_bounds_conversion(ge: GrammarError) -> Error {
    match ge {
        GrammarError::IndexOutOfBounds(index, bound) => Error::IndexOutOfBounds(index, bound),
        _ => Error::Invalid,
    }
}
/// A helper function to generate a fragment of forest.
///
/// It simply constructs a root node and then appends
/// successive nodes as successive children of the previous
/// node.  Also the starting positions will all be set to the
/// same position.
///
/// If the input is empty, this returns an empty forest;
/// otherwise the result is not empty.
pub fn generate_fragment(
    labels: impl AsRef<[GrammarLabelType]>,
    pos: usize,
) -> Result<DefaultForest<ForestLabel<GrammarLabel>>, crate::default::Error> {
    let mut labels_iter = labels.as_ref().iter();
    let mut result: DefaultForest<ForestLabel<GrammarLabel>>;

    if let Some(first_label) = labels_iter.next() {
        result = DefaultForest::new_leaf(GrammarLabel::new(*first_label, pos));

        let mut index = 0;

        for label in labels_iter {
            result.plant(
                index,
                DefaultForest::new_leaf(GrammarLabel::new(*label, pos)),
                false,
            )?;

            // To prevent duplication of labels causing
            // panics, we query the index of the new node.
            index = result
                .query_label(GrammarLabel::new(*label, pos).into())
                // REVIEW: Perhaps a LabelNoNode error?
                .ok_or(Error::Invalid)?;
        }
    } else {
        result = Default::default();
    }

    Ok(result)
}

/// Generate a virtual fragment representing the left-linear null
/// closure \[nt\]^t.
pub fn virtual_generate_fragment(
    atom: impl Borrow<DefaultAtom>,
    nt: usize,
    t: usize,
    pos: usize,
) -> Result<DefaultForest<ForestLabel<GrammarLabel>>, crate::default::Error> {
    let atom = atom.borrow();

    let non_start = atom.nth_accumulator(nt).unwrap() * 2;

    let mut result = DefaultForest::default();

    for (label, child_iter) in atom.labels_of(non_start)? {
        if matches!(*label.get_value(),
                    Some(TNT::Ter(ter)) if ter == t)
        {
            for child in child_iter {
                // #[allow(unused_must_use)]
                // {
                //     dbg!(non_start, child, atom.query_expansion(non_start, child));
                // }

                let line: Vec<GrammarLabelType> = atom
                    .query_expansion(non_start, child)
                    .map_err(index_out_of_bounds_conversion)?
                    .iter()
                    .copied()
                    .flatten()
                    .flat_map(|(nt, rule)| [(*rule).into(), TNT::Non(*nt).into()])
                    .rev()
                    .chain(std::iter::once(TNT::Ter(t).into()))
                    .collect();

                if result.is_empty() {
                    result = generate_fragment(line, pos)?;
                } else {
                    result.plant(0, generate_fragment(line, pos)?, false)?;
                }
            }
        }
    }

    Ok(result)
}

impl DefaultForest<ForestLabel<GrammarLabel>> {
    /// Insert an item derivation forest into a recording forest.
    ///
    /// We need the help of other things just for finding the correct
    /// places to insert these item fragments.
    pub fn insert_item(
        &mut self,
        label: Edge,
        fragment: impl Borrow<DefaultForest<ForestLabel<GrammarLabel>>>,
        atom_child_iter: impl Iterator<Item = usize>,
        atom: &DefaultAtom,
    ) -> Result<PaSe, Error> {
        let fragment = fragment.borrow();

        let pase = label.forest_source();

        let fragment_root = if let Some(root) = fragment.root() {
            root
        } else {
            return Err(Error::EmptyItem);
        };

        let pos = fragment
            .vertex_label(fragment_root)?
            .ok_or(Error::NodeNoLabel(fragment_root))?
            .label()
            .start();

        // Ensure the last node in the PaSe is a terminal or a
        // non-terminal node, as an extra safety guard during
        // development.
        #[cfg(debug_assertions)]
        {
            if let Some(parent) = pase.parent() {
                assert!(matches!(
                    self.vertex_label(self.nth_child(parent.node(), parent.edge())?),
                    Ok(Some(label))
                        if label.label().label().tnt().is_some()));
            } else if let Some((_, leaf)) = pase.segment() {
                assert!(matches!(
                    self.vertex_label(leaf),
                    Ok(Some(label))
                        if label.label().label().tnt().is_some()));
            } else {
                unreachable!("a pase is neither a parent nor a segment");
            }
        }

        let mut is_empty_segment = false;

        let mut parents: Vec<Parent> = {
            let mut result = Vec::new();

            if let Some(parent) = pase.parent() {
                result.push(parent);
            } else {
                let (root, leaf) = pase.segment().unwrap();
                let mut seen_nodes = Set::new();

                let mut stack = if root == leaf {
                    // an empty segment means the root
                    is_empty_segment = true;

                    result.push(Parent::new(root, 0));
                    vec![]
                } else {
                    vec![root]
                };

                while let Some(top) = stack.pop() {
                    if seen_nodes.contains(&top) {
                        continue;
                    }

                    seen_nodes.insert(top);

                    for (index, child) in self.children_of(top)?.enumerate() {
                        if child == leaf {
                            result.push(Parent::new(top, index));
                        } else {
                            stack.push(child);
                        }
                    }
                }
            }

            result
        };

        for parent in parents.iter() {
            if !self.has_node(parent.node()) {
                return Err(Error::IndexOutOfBounds(parent.node(), self.nodes_len()));
            }
        }

        if !is_empty_segment {
            parents = parents
                .into_iter()
                .flat_map(|parent| {
                    self.parents_of(parent.node()).unwrap().filter(|n| {
                        matches!(
                            self.vertex_label(n.node())
                                .unwrap()
                                .unwrap()
                                .label()
                                .label()
                                .tnt(),
                            Some(TNT::Non(_))
                        )
                    })
                })
                .collect();
        }

        let mut non_empty = false;

        for atom_child in atom_child_iter {
            // dbg!(label.label(), atom_child);
            // Find reduction information.
            let reduction_info = atom
                .query_reduction(label.label(), atom_child)
                .map_err(index_out_of_bounds_conversion)?;

            let mut stack = parents.clone();
            let mut second_stack = Vec::new();

            // locate the nodes
            for reduction_nt in reduction_info.iter().copied().flatten().rev() {
                while let Some(mut node) = stack.pop() {
                    // REVIEW: A lot of labels appear here.
                    // Perhaps I need to refactor something?
                    let mut node_label = self
                        .vertex_label(node.node())?
                        .ok_or_else(|| Error::NodeNoLabel(node.node()))?;

                    if matches!(
                        node_label
                            .label()
                            .label(),
                        GrammarLabelType::TNT(TNT::Non(nt)) if nt == *reduction_nt
                    ) {
                        // TODO: Try to update the end index of the
                        // node; if the node already has an ending
                        // index, clone the node, and update the
                        // ending index of the cloned node; otherwise,
                        // just set the ending index.

                        if node_label.label().end().is_some() {
                            let cloned_node =
                                self.clone_node(node.node(), node.edge() + 1, false)?;

                            self.transform_label(cloned_node, |mut label| {
                                label.set_end(pos + 1);
                                label
                            })?;

                            node_label = self
                                .vertex_label(node.node())?
                                .ok_or_else(|| Error::NodeNoLabel(node.node()))?;
                        } else {
                            self.transform_label(node.node(), |mut label| {
                                label.set_end(pos + 1);
                                label
                            })?;
                        }

                        if node_label.clone_index().is_some() {
                            let mut parent_iter = self.parents_of(node.node())?;

                            #[cfg(debug_assertions)]
                            assert_eq!(parent_iter.len(), 1);

                            node = parent_iter.next().unwrap();

                            #[cfg(debug_assertions)]
                            assert!(self
                                .vertex_label(node.node())?
                                .ok_or_else(|| Error::NodeNoLabel(node.node()))?
                                .is_packed());
                        }

                        let parents_iter = self.parents_of(node.node())?;

                        let to_update = parents_iter
                            .clone()
                            .map(|parent| parent.node())
                            .collect::<Vec<_>>();

                        for parent in parents_iter {
                            let parent_label = self
                                .vertex_label(parent.node())?
                                .ok_or_else(|| Error::NodeNoLabel(parent.node()))?
                                .label();

                            if parent_label.label().rule().is_none() {
                                crate::item::default::print_labels(atom, self.borrow()).unwrap();
                                self.print_viz("dbg forest.gv").unwrap();

                                dbg!(parent, parent_label, label, node);

                                panic!("assumption fails");
                            }

                            // debug_assert!(parent_label.label().rule().is_some());
                            // debug_assert!(parent_label.end().is_none());

                            second_stack.extend(self.parents_of(parent.node())?.filter(|n| {
                                matches!(self.vertex_label(n.node()),
                                         Ok(Some(label))
                                         if matches!(
                                             label.label().label().tnt(),
                                             Some(TNT::Non(_))))
                            }));
                        }

                        for node in to_update {
                            self.transform_label(node, |mut label| {
                                label.set_end(pos + 1);
                                label
                            })?;
                        }
                    }
                }

                std::mem::swap(&mut stack, &mut second_stack);

                if stack.is_empty() {
                    break;
                }
            }

            if stack.is_empty() {
                dbg!(
                    label,
                    atom_child,
                    parents,
                    reduction_info,
                    atom.query_reduction(label.label(), atom_child).unwrap()
                );

                self.print_viz("dbg forest.gv").unwrap();

                #[cfg(test)]
                genins_test::print_labels(atom, self).unwrap();

                return Err(Error::CannotPlant);
            }

            for parent in stack.into_iter() {
                let was_last_child = parent.edge() + 1 >= self.degree(parent.node())?;

                let overlap = {
                    if let Ok(child) = self.nth_child(parent.node(), parent.edge()) {
                        let edge_th_child = child;
                        let child_label = self
                            .vertex_label(edge_th_child)?
                            .ok_or(Error::NodeNoLabel(edge_th_child))?;
                        let child_start = child_label.label().start();
                        let child_end = child_label.label().end();

                        child_start >= pos || matches!(child_end, Some(end) if end > pos)
                    } else {
                        // the root case
                        false
                    }
                };

                if overlap {
                    let cloned_node = self.clone_node(parent.node(), parent.edge() + 1, false)?;

                    self.plant(cloned_node, fragment, non_empty)?;
                } else if was_last_child {
                    // dbg!(&fragment);
                    self.plant(parent.node(), fragment, non_empty)?;
                } else {
                    let nth_child = self.nth_child(parent.node(), parent.edge() + 1)?;

                    if self.is_prefix(nth_child, fragment)? {
                        // do nothing
                        continue;
                    }

                    // dbg!(
                    //     label,
                    //     atom_child,
                    //     &parents,
                    //     reduction_info,
                    //     atom.query_reduction(label.label(), atom_child).unwrap()
                    // );

                    // self.print_viz("dbg forest.gv").unwrap();

                    let cloned_node = self.clone_node(parent.node(), parent.edge() + 1, false)?;

                    self.plant(cloned_node, fragment, non_empty)?;
                }

                non_empty = true;
            }
        }

        // If the iterator is empty, just plant at the specified
        // child.
        if !non_empty {
            for parent in parents.into_iter() {
                let nth_child = self.nth_child(parent.node(), parent.edge())?;

                self.plant(nth_child, fragment, non_empty)?;

                non_empty = true;
            }
        }

        let result = if fragment.nodes_len() == 2 {
            let root_label = fragment.vertex_label(0)?.unwrap();
            let leaf_label = fragment.vertex_label(1)?.unwrap();

            // it has been planted, so should be safe.
            let node = self.query_label(root_label).unwrap();

            let edge = {
                let mut result = None;

                for (index, child) in self.children_of(node)?.enumerate() {
                    if matches!(self.vertex_label(child)?, Some(child_label) if child_label == leaf_label)
                    {
                        result = Some(index);
                        break;
                    }
                }

                if let Some(result) = result {
                    result
                } else {
                    unreachable!("the forest is wrongly planted");
                }
            };
            // dbg!(node, edge, root_label, leaf_label);
            PaSe::Parent(node, edge)
        } else {
            let root_label = fragment.vertex_label(0)?.unwrap();
            let leaf_label = fragment.vertex_label(fragment.nodes_len() - 1)?.unwrap();
            // dbg!(root_label, leaf_label);

            PaSe::Segment(
                self.query_label(root_label).unwrap(),
                self.query_label(leaf_label).unwrap(),
            )
        };

        Ok(result)
    }
}

#[cfg(test)]
mod genins_test {
    use super::*;
    #[allow(unused_imports)]
    use crate::{default::DefaultChain, item::default::leaf, Chain};

    use grammar::test_grammar_helper::*;

    pub fn print_labels(
        atom: impl Borrow<DefaultAtom>,
        forest: impl Borrow<DefaultForest<ForestLabel<GrammarLabel>>>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let forest = forest.borrow();
        let atom = atom.borrow();

        for node in forest.nodes() {
            let label = forest.vertex_label(node)?;

            if let Some(label) = label {
                let label = label.label.label();

                match label {
                    GrammarLabelType::TNT(tnt) => {
                        println!("{tnt} = {}", atom.name_of_tnt(tnt)?);
                    }
                    GrammarLabelType::Rule(pos) => {
                        println!("pos {pos} = {}", atom.rule_pos_string(pos)?);
                    }
                }
            } else {
                return Err(Error::NodeNoLabel(node).into());
            }
        }

        Ok(())
    }

    #[test]
    fn test_generate_fragment() -> Result<(), Box<dyn std::error::Error>> {
        let grammar = new_notes_grammar()?;

        let atom = DefaultAtom::from_grammar(grammar)?;

        #[cfg(feature = "test-print-viz")]
        atom.print_nfa("genins nfa.gv")?;

        let fragment = generate_fragment([72.into(), TNT::Non(0).into()], 0)?;

        let mut test_fragment = leaf!(
            GrammarLabel::new(GrammarLabelType::from(72), 0),
            GrammarLabel
        );

        test_fragment.plant(
            0,
            leaf!(
                GrammarLabel::new(GrammarLabelType::from(TNT::Non(0)), 0),
                GrammarLabel
            ),
            false,
        )?;

        assert_eq!(fragment, test_fragment);

        // virtual fragments

        println!("nt = 0, t = 3");

        let virtual_fragment = virtual_generate_fragment(&atom, 0, 3, 0)?;

        assert_eq!(virtual_fragment.nodes_len(), 7);

        let virtual_node = virtual_fragment.vertex_label(5)?.unwrap().label();

        let test_fragment = generate_fragment(
            [
                TNT::Non(0).into(),
                2.into(),
                TNT::Non(1).into(),
                8.into(),
                TNT::Non(2).into(),
                virtual_node.label(),
                TNT::Ter(3).into(),
            ],
            0,
        )?;

        print_labels(&atom, &virtual_fragment)?;

        assert_eq!(virtual_fragment, test_fragment);

        #[cfg(feature = "test-print-viz")]
        virtual_fragment.print_viz("virtual fragment (0, 3).gv")?;

        println!("nt = 3, t = 2");

        let virtual_fragment = virtual_generate_fragment(&atom, 3, 2, 1)?;

        let test_fragment =
            generate_fragment([TNT::Non(3).into(), 38.into(), TNT::Ter(2).into()], 1)?;

        print_labels(&atom, &virtual_fragment)?;

        assert_eq!(virtual_fragment, test_fragment);

        #[cfg(feature = "test-print-viz")]
        virtual_fragment.print_viz("virtual fragment (3, 2).gv")?;

        // querying reductions

        assert!(matches!(atom.query_reduction(17, 9), Ok(Some(&[1]))));

        assert!(matches!(atom.query_reduction(35, 9), Ok(Some(&[1, 2]))));
        assert!(matches!(atom.query_reduction(35, 25), Ok(Some(&[2]))));

        Ok(())
    }

    #[test]
    fn test_reduction() -> Result<(), Box<dyn std::error::Error>> {
        let grammar = new_paren_grammar()?;

        let atom = DefaultAtom::from_grammar(grammar)?;

        #[cfg(feature = "test-print-viz")]
        atom.print_nfa("genins nfa.gv")?;

        // querying reductions

        println!("{:?}", atom.query_reduction(32, 17)?);

        // assert!(matches!(atom.query_reduction(35, 25), Ok(Some(&[2]))));

        Ok(())
    }
}