summaryrefslogtreecommitdiff
path: root/nfa/src/lib.rs
blob: de71f25d6792f72bdc4bf255d791b763445fde0b (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
#![warn(missing_docs)]
//! This crate implements non-deterministic finite automata.
//!
//! By default this uses the graph from the crate [`graph`].  To use
//! another external graph, add a module in which the external graph
//! implements the Graph trait from the [`graph`] crate, and then use
//! that external graph type as [`Graph`][graph::Graph] here.

pub mod error;

extern crate graph;

use core::fmt::Display;

use std::ops::{Deref, DerefMut};

use graph::{Graph, GraphLabel, LabelExtGraph};

use error::Error;

pub use desrec::DesRec;

use default::regex::RegexType;

/// The expected behaviour of a regular language.
///
/// Nondeterministic finite automata are equivalent to regular
/// languages.  Since regular languages are easier to understand for a
/// human being, nondeterministic finite automata include the data for
/// the equivalent regular languages.
pub trait Regex<T: GraphLabel>: Graph + Display + Clone {
    /// Return the label of a vertex, or an error if the node is
    /// invalid.
    fn vertex_label(&self, node_id: usize) -> Result<T, Error>;

    #[inline]
    /// Return the root node of the regular language.
    ///
    /// Implementations can follow different conventions for the root
    /// node, and hence this function.
    ///
    /// If the regular language is empty, the implementation should
    /// return None.
    ///
    /// The default implementation uses the convention that the root
    /// node is always the first node.
    fn root(&self) -> Option<usize> {
        if self.is_empty() {
            None
        } else {
            Some(0)
        }
    }

    // TODO: Add functions that determine if certain "positions" in a
    // regular language satisfy some special properties, like at the
    // end of a Kleene star, or at the end of a regular language, et
    // cetera.  These might be needed later.
}

/// Since `Option<T>` does not inherit the `Display` from `T`, we wrap
/// it to provide an automatic implementation of `Display`.
///
/// # Convert to `Option`
///
/// One can dereference a `DOption` to obtain an `Option`.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct DOption<T>(pub Option<T>);

impl<T> Default for DOption<T> {
    fn default() -> Self {
        Self(None)
    }
}

impl<T> Deref for DOption<T> {
    type Target = Option<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for DOption<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Display> Display for DOption<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.deref() {
            Some(value) => Display::fmt(value, f),
            None => write!(f, "ε"),
        }
    }
}

/// Substitute or Carry
///
/// This enumeration indicates whether a label from a regular
/// expression should be substituted by another regular expression, or
/// to be carried around in the resulting nondeterministic finite
/// automaton, in the process of the [`to_nfa`][Nfa::to_nfa] function.
///
/// # Transform labels
///
/// The label that is returned to be carried can be different from the
/// original label, as a way to transform the labels.
///
/// # Remark on the abbreviation
///
/// It happens "by chance" that this abbreviation coincides with the
/// abbreviation of "system on chip".  Since I know nothing about this
/// topic, this is just a meaningless pun.
#[derive(Debug, Copy, Clone)]
pub enum SoC<T> {
    /// To be substituted by another regular expression.
    Sub(usize),
    /// To carry around this label.
    Carry(T),
}

/// This type records some information that is obtained from the
/// process of converting a regular expression to a nondeterministic
/// finite automaton.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Default)]
pub struct NfaLabel<T: GraphLabel> {
    /// A terminal or a non-terminal.
    value: T,
    /// The corresponding position in the rules.
    moved: usize,
    /// Whether this comes from left-linear expansion.
    left_p: bool,
}

impl<T: GraphLabel> NfaLabel<T> {
    /// Construct a new label.
    #[inline]
    pub fn new(value: T, moved: usize, left_p: bool) -> Self {
        Self {
            value,
            moved,
            left_p,
        }
    }

    /// Retrieve the value from the label.
    #[inline]
    pub fn get_value(&self) -> T {
        self.value
    }

    /// Retrieve the moved position from the label.
    #[inline]
    pub fn get_moved(&self) -> usize {
        self.moved
    }

    /// Retrieve whether or not the label comes from left-linear
    /// expansions.
    #[inline]
    pub fn is_left_p(&self) -> bool {
        self.left_p
    }
}

impl<T: GraphLabel> Display for NfaLabel<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "edge {} at {}{}", self.value, self.moved, {
            if self.left_p {
                ", by left"
            } else {
                ""
            }
        })
    }
}

/// A convenient alias of the information of two edges.
///
/// If the tuple is (a, b, c, la, lb), then the first edge goes from a
/// to b, is labelled la, and the second edge goes from b to c, and is
/// labelled by lb.
#[derive(Debug, Clone, Copy)]
pub struct TwoEdges<T: Copy>(usize, usize, usize, T, T);

impl<T: Copy> TwoEdges<T> {
    /// Extract the first edge.
    pub fn first_edge(&self) -> (usize, usize, T) {
        (self.0, self.1, self.3)
    }

    /// Extract the second edge.
    pub fn second_edge(&self) -> (usize, usize, T) {
        (self.1, self.2, self.4)
    }
}

/// The type of nondeterministic finite automata that is obtained from
/// a regular expression, via the method [`to_nfa`][Nfa::to_nfa].
pub type LabelType<T> = NfaLabel<DOption<T>>;

/// The expected behvaiour of a nondeterministic finite automaton.
///
/// Every NFA is a special labelled graph.
pub trait Nfa<T: GraphLabel>: LabelExtGraph<T> {
    /// When we convert a regular expression to a nondeterministic
    /// finite automaton, the label should be optional, so we use a
    /// different type for the result type.
    type FromRegex<S: GraphLabel + Display + Default>;

    /// Return a state-minimal NFA equivalent with the original one.
    ///
    /// This is not required.  It is just to allow me to experiment
    /// with NFA optimization algorithms.
    fn minimize(&self) -> Result<Box<Self>, Error> {
        Err(Error::UnsupportedOperation)
    }

    /// Check every node or edge by a given predicate.
    ///
    /// This should also verify that every node and edge has correct
    /// indices, so that we can safely use `unwrap` later.  A
    /// consequence is that, if one only wants to check the validity
    /// of nodes and edges, one can pass a function that always
    /// returns `true`.
    #[inline]
    fn labels_satisfy(&self, f: impl Fn(T) -> bool) -> Result<bool, Error> {
        let nodes_len = self.nodes_len();
        let mut result = true;

        for node in self.nodes() {
            for (label, children_iter) in self.labels_of(node)? {
                for child in children_iter {
                    if child >= nodes_len {
                        dbg!(node, label);
                        return Err(graph::error::Error::IndexOutOfBounds(child, nodes_len).into());
                    }
                }

                // NOTE: Avoid executing `f` if `result` is already
                // false.  But still proceed in verifying that nodes
                // and edges are correct: the correctness of nodes and
                // edges is more important than the function execution
                // results, as the former directly affects the safety
                // of many algorithms.
                if result && !f(*label) {
                    dbg!(node, label);
                    result = false;
                }
            }
        }

        Ok(result)
    }

    /// Build a nondeterministic finite automaton out of a set
    /// `regexps` of regular expressions.
    ///
    /// The `sub_pred` is a predicate function that returns an
    /// indication whether to carry the value around or to substitute
    /// by another regular language in the given set.
    ///
    /// The `default` parameter specifies the label of a default edge,
    /// that goes from a node to another, both of which are not
    /// associated with the given regular languages.
    ///
    /// This function should perform Thompson's construction,
    /// basically.
    fn to_nfa(
        regexps: &[impl Regex<RegexType<T>>],
        sub_pred: impl Fn(T) -> Result<SoC<T>, Error>,
        default: Option<T>,
    ) -> Result<Self::FromRegex<LabelType<T>>, Error>;

    /// Remove all dead states from the nondeterministic finite
    /// automaton.
    ///
    /// A state is dead if there are no edges going to the state, and
    /// if it is not reserved.
    ///
    /// # Note
    ///
    /// Actually an implementation is allowed to just remove all edges
    /// out of the dead nodes.  Then these nodes are considered gone
    /// from the graph, and we don't need to re-index the existing
    /// edges.  We can call this "a poor man's removal of nodes".
    fn remove_dead(&mut self, reserve: impl FnMut(usize) -> bool) -> Result<(), Error>;

    /// Return the *closure* of the nondeterministic finite automaton.
    ///
    /// # Definition
    ///
    /// The closure of a nondeterministic finite automaton N is
    /// defined as the unique *minimal* nondeterministic finite
    /// automaton N+ that can be obtained by adjoining some edges to N
    /// such that if there are edges a -> b and b -> c, and if the
    /// edge a -> b is deemed as *nullable* by some function, then
    /// there is an edge a -> c, where the minimality is the
    /// minimality of the set of edges: if there is another such
    /// nondeterministic finite automaton M satisfying the above
    /// property, then the set of edges of N+ is a subset of the set
    /// of edges of M.
    ///
    /// # Remove edges afterwards
    ///
    /// If `remove_after_p` is true, remove all those nullable edges.
    ///
    /// # Transformation of labels
    ///
    /// We can apply a transformer to labels, to be able to combine
    /// labels in non-trivial ways.  If one just wants the *new* label
    /// that is the label of the edge from b to c in the above
    /// definition, one can use the function that sends `two_edges` to
    /// `two_edges.second_edge().2`.
    ///
    /// # Error
    ///
    /// The function should emit errors if the edges of the
    /// nondeterministic finite automaton point to invalid nodes.
    fn closure(
        &mut self,
        predicate: impl FnMut(T) -> bool,
        remove_after_p: bool,
        transform: impl FnMut(TwoEdges<T>) -> T,
        remove_predicate: impl FnMut(T) -> bool,
    ) -> Result<(), Error>;
}

pub mod default;
pub mod desrec;