rusty-alto 0.2.0

Weighted tree automata and interpreted regular tree grammars with Alto-compatible I/O
Documentation
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
//! Memoizing adapter from rich implicit states to dense state IDs.

use crate::{
    BottomUpTa, DetBottomUpTa, Explicit, ExplicitBuilder, FxHashMap, IndexedBottomUpTa, Interner,
    StateId, Symbol, TopDownTa,
};
use smallvec::SmallVec;
use std::cell::{Ref, RefCell};
use std::hash::Hash;

type Results = SmallVec<[StateId; 2]>;

/// Runtime cache counters for [`Memo`].
///
/// Hit and miss counters are collected only when the `stats` feature is
/// enabled. Without that feature they are reported as zero, while `num_states`
/// is always available.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MemoStats {
    /// Number of transition queries answered from the cache.
    pub hits: u64,
    /// Number of transition queries forwarded to the inner automaton.
    pub misses: u64,
    /// Number of distinct inner states that have been assigned dense IDs.
    pub num_states: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum CacheKey {
    Nullary(Symbol),
    Unary(Symbol, StateId),
    Binary(Symbol, StateId, StateId),
    Higher(Symbol, Box<[StateId]>),
}

impl CacheKey {
    fn new(f: Symbol, children: &[StateId]) -> Self {
        match children.len() {
            0 => Self::Nullary(f),
            1 => Self::Unary(f, children[0]),
            2 => Self::Binary(f, children[0], children[1]),
            _ => Self::Higher(f, Box::from(children)),
        }
    }

    fn symbol(&self) -> Symbol {
        match self {
            Self::Nullary(f) | Self::Unary(f, _) | Self::Binary(f, _, _) | Self::Higher(f, _) => *f,
        }
    }

    fn children_vec(&self) -> Vec<StateId> {
        match self {
            Self::Nullary(_) => Vec::new(),
            Self::Unary(_, q) => vec![*q],
            Self::Binary(_, q0, q1) => vec![*q0, *q1],
            Self::Higher(_, children) => children.to_vec(),
        }
    }
}

/// Memoizing adapter from implicit automata to dense [`StateId`] states.
///
/// `Memo` lets an automaton with rich states act like an automaton over
/// [`StateId`]. On a cache miss, it resolves child IDs back to the inner state
/// type, asks the inner automaton for results, interns those results, and
/// caches the dense IDs. On a cache hit, it replays the stored IDs directly.
///
/// This type uses interior mutability and is intended for single-threaded
/// execution. To keep a snapshot of the discovered rules, call
/// [`Memo::into_explicit`].
pub struct Memo<A: BottomUpTa> {
    inner: A,
    interner: RefCell<Interner<A::State>>,
    cache: RefCell<FxHashMap<CacheKey, Results>>,
    accepting_cache: RefCell<FxHashMap<StateId, bool>>,
    #[cfg(feature = "stats")]
    hits: RefCell<u64>,
    #[cfg(feature = "stats")]
    misses: RefCell<u64>,
}

impl<A: BottomUpTa> Memo<A> {
    /// Wrap an automaton in an empty memoization cache.
    pub fn new(inner: A) -> Self {
        Self {
            inner,
            interner: RefCell::new(Interner::new()),
            cache: RefCell::new(FxHashMap::default()),
            accepting_cache: RefCell::new(FxHashMap::default()),
            #[cfg(feature = "stats")]
            hits: RefCell::new(0),
            #[cfg(feature = "stats")]
            misses: RefCell::new(0),
        }
    }

    /// Borrow the interner that maps inner states to dense IDs.
    ///
    /// This is useful for inspection. Prefer [`Memo::resolve`] when you only
    /// need to map one ID back to an inner state.
    pub fn interner(&self) -> Ref<'_, Interner<A::State>> {
        self.interner.borrow()
    }

    /// Resolve a dense ID back to the wrapped automaton's state type.
    ///
    /// Panics if the ID has not been discovered by this memoizer, or if it is
    /// [`StateId::STUCK`].
    pub fn resolve(&self, id: StateId) -> A::State {
        self.interner.borrow().resolve(id).clone()
    }

    /// Return current memoization statistics.
    pub fn stats(&self) -> MemoStats {
        MemoStats {
            #[cfg(feature = "stats")]
            hits: *self.hits.borrow(),
            #[cfg(not(feature = "stats"))]
            hits: 0,
            #[cfg(feature = "stats")]
            misses: *self.misses.borrow(),
            #[cfg(not(feature = "stats"))]
            misses: 0,
            num_states: self.interner.borrow().len() as u32,
        }
    }

    /// Freeze all queried transitions into an [`Explicit`] automaton.
    ///
    /// The result contains only the fragment that has actually been queried.
    /// If a transition was never requested, it will not appear in the explicit
    /// automaton. The returned [`Interner`] lets callers map the dense states
    /// back to the original inner state values.
    pub fn into_explicit(self) -> (Explicit, Interner<A::State>) {
        let num_states = self.interner.borrow().len();
        let mut accepting = Vec::new();
        for idx in 0..num_states {
            let q = StateId(idx as u32);
            if self.is_accepting(&q) {
                accepting.push(q);
            }
        }

        let interner = self.interner.into_inner();
        let cache = self.cache.into_inner();
        let mut builder = ExplicitBuilder::new();
        for _ in 0..num_states {
            builder.new_state();
        }
        for q in accepting {
            builder.add_accepting(q);
        }
        for (key, results) in cache {
            let symbol = key.symbol();
            let children = key.children_vec();
            for result in results {
                builder.add_rule(symbol, children.clone(), result);
            }
        }
        (builder.build(), interner)
    }

    fn resolve_children(&self, children: &[StateId]) -> SmallVec<[A::State; 4]> {
        let interner = self.interner.borrow();
        children
            .iter()
            .map(|&q| interner.resolve(q).clone())
            .collect()
    }

    fn intern_results(&self, results: Vec<A::State>) -> Results {
        let mut interner = self.interner.borrow_mut();
        let mut dense = Results::new();
        for q in results {
            let id = interner.intern(q);
            if !dense.contains(&id) {
                dense.push(id);
            }
        }
        dense
    }

    fn record_hit(&self) {
        #[cfg(feature = "stats")]
        {
            *self.hits.borrow_mut() += 1;
        }
    }

    fn record_miss(&self) {
        #[cfg(feature = "stats")]
        {
            *self.misses.borrow_mut() += 1;
        }
    }
}

impl<A: BottomUpTa> BottomUpTa for Memo<A> {
    type State = StateId;

    fn step(&self, f: Symbol, children: &[StateId], out: &mut dyn FnMut(StateId)) {
        let key = CacheKey::new(f, children);
        {
            let cache = self.cache.borrow();
            if let Some(results) = cache.get(&key) {
                self.record_hit();
                for &q in results {
                    out(q);
                }
                return;
            }
        }

        self.record_miss();
        let resolved = self.resolve_children(children);
        let mut raw = Vec::new();
        self.inner.step(f, &resolved, &mut |q| raw.push(q));
        let dense = self.intern_results(raw);
        self.cache.borrow_mut().insert(key.clone(), dense);
        let cache = self.cache.borrow();
        if let Some(results) = cache.get(&key) {
            for &q in results {
                out(q);
            }
        }
    }

    fn is_accepting(&self, q: &StateId) -> bool {
        if q.is_stuck() {
            return false;
        }
        if let Some(&accepting) = self.accepting_cache.borrow().get(q) {
            return accepting;
        }
        let state = self.interner.borrow().resolve(*q).clone();
        let accepting = self.inner.is_accepting(&state);
        self.accepting_cache.borrow_mut().insert(*q, accepting);
        accepting
    }
}

impl<A: DetBottomUpTa> DetBottomUpTa for Memo<A> {
    fn step_det(&self, f: Symbol, children: &[StateId]) -> Option<StateId> {
        let key = CacheKey::new(f, children);
        {
            let cache = self.cache.borrow();
            if let Some(results) = cache.get(&key) {
                self.record_hit();
                return (results.len() == 1).then_some(results[0]);
            }
        }

        self.record_miss();
        let resolved = self.resolve_children(children);
        let mut dense = Results::new();
        if let Some(q) = self.inner.step_det(f, &resolved) {
            let id = self.interner.borrow_mut().intern(q);
            dense.push(id);
        }
        let answer = (dense.len() == 1).then_some(dense[0]);
        self.cache.borrow_mut().insert(key, dense);
        answer
    }
}

impl<A: IndexedBottomUpTa> IndexedBottomUpTa for Memo<A> {
    fn step_partial(
        &self,
        f: Symbol,
        position: usize,
        state_at_position: &StateId,
        out: &mut dyn FnMut(&[StateId], StateId),
    ) {
        if state_at_position.is_stuck() {
            return;
        }

        let inner_state = self.interner.borrow().resolve(*state_at_position).clone();
        let mut raw = Vec::new();
        self.inner
            .step_partial(f, position, &inner_state, &mut |children, result| {
                raw.push((children.to_vec(), result));
            });

        for (children, result) in raw {
            let mut interner = self.interner.borrow_mut();
            let dense_children: SmallVec<[StateId; 4]> = children
                .into_iter()
                .map(|child| interner.intern(child))
                .collect();
            let dense_result = interner.intern(result);
            drop(interner);
            out(&dense_children, dense_result);
        }
    }
}

impl<A: TopDownTa> TopDownTa for Memo<A> {
    fn step_topdown(&self, parent: &StateId, out: &mut dyn FnMut(Symbol, &[StateId])) {
        if parent.is_stuck() {
            return;
        }

        let inner_parent = self.interner.borrow().resolve(*parent).clone();
        let mut raw = Vec::new();
        self.inner
            .step_topdown(&inner_parent, &mut |symbol, children| {
                raw.push((symbol, children.to_vec()));
            });

        for (symbol, children) in raw {
            let mut interner = self.interner.borrow_mut();
            let dense_children: SmallVec<[StateId; 4]> = children
                .into_iter()
                .map(|child| interner.intern(child))
                .collect();
            drop(interner);
            out(symbol, &dense_children);
        }
    }

    fn initial_states(&self, out: &mut dyn FnMut(StateId)) {
        let mut raw = Vec::new();
        self.inner.initial_states(&mut |q| raw.push(q));
        let mut interner = self.interner.borrow_mut();
        for q in raw {
            out(interner.intern(q));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone)]
    struct Leaf;

    impl BottomUpTa for Leaf {
        type State = &'static str;

        fn step(&self, f: Symbol, children: &[Self::State], out: &mut dyn FnMut(Self::State)) {
            if f == Symbol(0) && children.is_empty() {
                out("leaf");
            }
            if f == Symbol(1) && children == ["leaf", "leaf"] {
                out("root");
            }
        }

        fn is_accepting(&self, q: &Self::State) -> bool {
            *q == "root"
        }
    }

    impl IndexedBottomUpTa for Leaf {
        fn step_partial(
            &self,
            f: Symbol,
            position: usize,
            state_at_position: &&'static str,
            out: &mut dyn FnMut(&[&'static str], &'static str),
        ) {
            if f == Symbol(1) && position < 2 && *state_at_position == "leaf" {
                out(&["leaf", "leaf"], "root");
            }
        }
    }

    impl TopDownTa for Leaf {
        fn step_topdown(
            &self,
            parent: &&'static str,
            out: &mut dyn FnMut(Symbol, &[&'static str]),
        ) {
            match *parent {
                "leaf" => out(Symbol(0), &[]),
                "root" => out(Symbol(1), &["leaf", "leaf"]),
                _ => {}
            }
        }

        fn initial_states(&self, out: &mut dyn FnMut(&'static str)) {
            out("root");
        }
    }

    #[test]
    fn memo_answers_like_inner() {
        let memo = Memo::new(Leaf);
        let mut leaves = Vec::new();
        memo.step(Symbol(0), &[], &mut |q| leaves.push(q));
        let mut roots = Vec::new();
        memo.step(Symbol(1), &[leaves[0], leaves[0]], &mut |q| roots.push(q));
        assert_eq!(memo.resolve(roots[0]), "root");
        assert!(memo.is_accepting(&roots[0]));
    }

    #[test]
    fn into_explicit_preserves_discovered_fragment() {
        let memo = Memo::new(Leaf);
        let mut leaves = Vec::new();
        memo.step(Symbol(0), &[], &mut |q| leaves.push(q));
        let mut roots = Vec::new();
        memo.step(Symbol(1), &[leaves[0], leaves[0]], &mut |q| roots.push(q));
        let (explicit, interner) = memo.into_explicit();
        assert_eq!(interner.resolve(roots[0]), &"root");
        assert_eq!(
            explicit.step_det(Symbol(1), &[leaves[0], leaves[0]]),
            Some(roots[0])
        );
        assert!(explicit.is_accepting(&roots[0]));
    }

    #[test]
    fn indexed_partial_forwards_through_memo() {
        let memo = Memo::new(Leaf);
        let mut leaves = Vec::new();
        memo.step(Symbol(0), &[], &mut |q| leaves.push(q));

        let mut found = Vec::new();
        memo.step_partial(Symbol(1), 0, &leaves[0], &mut |children, result| {
            found.push((children.to_vec(), result));
        });

        assert_eq!(found.len(), 1);
        assert_eq!(found[0].0, vec![leaves[0], leaves[0]]);
        assert_eq!(memo.resolve(found[0].1), "root");
    }

    #[test]
    fn topdown_forwards_through_memo() {
        let memo = Memo::new(Leaf);
        let mut initials = Vec::new();
        memo.initial_states(&mut |q| initials.push(q));

        let mut rules = Vec::new();
        memo.step_topdown(&initials[0], &mut |symbol, children| {
            rules.push((symbol, children.to_vec()));
        });

        assert_eq!(memo.resolve(initials[0]), "root");
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].0, Symbol(1));
        assert_eq!(rules[0].1.len(), 2);
    }
}