oxidd-parser 0.5.0

Parsers for logic file formats
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
//! Negation normal form parser for an extended version of [c2d's][c2d] d-DNNF
//! output format
//!
//! The format extensions subsume [Bella's][bella] wDNNF format.
//!
//! [c2d]: http://reasoning.cs.ucla.edu/c2d/
//! [bella]: https://github.com/Illner/BellaCompiler

// spell-checker:ignore multispace

use nom::bytes::complete::tag;
use nom::character::complete::{char, i64, line_ending, multispace0, space0, space1, u64};
use nom::combinator::{consumed, cut, eof, value};
use nom::error::{ContextError, FromExternalError, ParseError, context};
use nom::multi::many0_count;
use nom::sequence::{preceded, terminated};
use nom::{IResult, Offset};
use rustc_hash::FxHashSet;

use crate::util::{
    self, MAX_CAPACITY, context_loc, eol, fail, fail_with_contexts, line_span, usize, word_span,
};
use crate::{Circuit, GateKind, Literal, ParseOptions, Problem, Tree, Var, VarSet};

/// Parses a problem line, i.e., `nnf <#nodes> <#edges> <#inputs>`
///
/// Returns the three numbers along with their spans.
fn problem_line<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
    input: &'a [u8],
) -> IResult<&'a [u8], [(&'a [u8], usize); 3], E> {
    let inner = |input| {
        let (input, _) = context(
            "all lines in the preamble must begin with 'c' or 'nnf'",
            cut(tag("nnf")),
        )(input)?;
        let (input, _) = space1(input)?;
        let (input, num_nodes) = consumed(usize)(input)?;
        let (input, _) = space1(input)?;
        let (input, num_edges) = consumed(usize)(input)?;
        let (input, _) = space1(input)?;
        let (input, num_inputs) = consumed(usize)(input)?;
        value([num_nodes, num_edges, num_inputs], line_ending)(input)
    };

    context_loc(
        || line_span(input),
        "problem line must have format 'nnf <#nodes> <#edges> <#inputs>'",
        cut(inner),
    )(input)
}

/// Parses the preamble, i.e., all `c` and `nnf` lines at the beginning of the
/// file
///
/// Note: `parse_orders` only instructs the parser to treat comment lines as
/// order lines. In case a var order is given, this function ensures that they
/// are valid. However, if no var order is given, then the returned var order is
/// empty.
fn preamble<'a, E>(
    parse_var_order: bool,
) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], (VarSet, [(&'a [u8], usize); 3]), E>
where
    E: ParseError<&'a [u8]> + ContextError<&'a [u8]> + FromExternalError<&'a [u8], String>,
{
    move |mut input| {
        if parse_var_order {
            let mut vars = VarSet {
                len: 0,
                order: Vec::new(),
                order_tree: None,
                names: Vec::new(),
            };

            let mut max_var_span = [].as_slice(); // in the name mapping / linear order
            let mut tree_max_var = ([].as_slice(), 0); // dummy value
            let mut name_set: FxHashSet<&str> = Default::default();

            loop {
                let next_input = match preceded(char::<_, E>('c'), space1)(input) {
                    Ok((i, _)) => i,
                    Err(_) => break,
                };
                if let Ok((next_input, _)) = preceded(tag("vo"), space1::<_, E>)(next_input) {
                    // variable order tree
                    if vars.order_tree.is_some() {
                        let msg = "variable order tree may only be given once";
                        return fail(line_span(input), msg);
                    }
                    let t: Tree<Var>;
                    (input, (t, tree_max_var)) =
                        terminated(util::tree(true, true), eol)(next_input)?;

                    // The variable order tree takes precedence (and determines the linear order)
                    vars.order.clear();
                    vars.order.reserve(tree_max_var.1 + 1);
                    t.flatten_into(&mut vars.order);
                    vars.order_tree = Some(t);
                } else if let Ok((next_input, ((var_span, var), name))) =
                    util::var_order_record::<E>(next_input)
                {
                    // var order line
                    input = next_input;
                    if var == 0 {
                        return fail(var_span, "variable number must be greater than 0");
                    }
                    if var > MAX_CAPACITY {
                        return fail(var_span, "variable number too large");
                    }

                    let num_vars = var as usize;
                    let var = num_vars - 1;

                    if num_vars > vars.names.len() {
                        vars.names.resize(num_vars, None);
                        vars.order.reserve(num_vars - vars.names.len());
                        max_var_span = var_span;
                    } else if vars.names[var].is_some() {
                        return fail(var_span, "second occurrence of variable in order");
                    }
                    // always write Some to mark the variable as present
                    vars.names[var] = Some(if let Some(name) = name {
                        let Ok(name) = std::str::from_utf8(name) else {
                            return fail(name, "invalid UTF-8");
                        };
                        if !name_set.insert(name) {
                            return fail(name.as_bytes(), "second occurrence of variable name");
                        }
                        name.to_owned()
                    } else {
                        String::new()
                    });
                    if vars.order_tree.is_none() {
                        vars.order.push(var);
                    }
                } else {
                    return fail(
                        line_span(input),
                        "expected a variable order record ('c <var> [<name>]') or a variable order tree ('c vo <tree>')",
                    );
                }
            }

            if vars.order_tree.is_none() && vars.names.len() != vars.order.len() {
                return fail_with_contexts([
                    (input, "expected another variable order line"),
                    (max_var_span, "note: maximal variable number given here"),
                ]);
            }

            let (next_input, sizes) = problem_line(input)?;
            let [_, _, num_vars] = sizes;
            if vars.order_tree.is_none() {
                if !vars.order.is_empty() && num_vars.1 != vars.order.len() {
                    return fail_with_contexts([
                        (num_vars.0, "number of variables does not match"),
                        (max_var_span, "note: maximal variable number given here"),
                    ]);
                }
            } else {
                if num_vars.1 != tree_max_var.1 + 1 {
                    return fail_with_contexts([
                        (num_vars.0, "number of variables does not match"),
                        (tree_max_var.0, "note: maximal variable number given here"),
                    ]);
                }
                if vars.names.len() > num_vars.1 {
                    return fail_with_contexts([
                        (max_var_span, "name assigned to non-existing variable"),
                        (num_vars.0, "note: number of variables given here"),
                    ]);
                }
            }

            // cleanup: we used `Some(String::new())` to mark unnamed variables as present
            while let Some(name) = vars.names.last() {
                if !name.as_ref().is_some_and(String::is_empty) {
                    break;
                }
                vars.names.pop();
            }
            for name in &mut vars.names {
                if name.as_ref().is_some_and(String::is_empty) {
                    *name = None;
                }
            }

            vars.len = num_vars.1;
            #[cfg(debug_assertions)]
            vars.check_valid();
            Ok((next_input, (vars, sizes)))
        } else {
            let (input, sizes) = preceded(many0_count(util::comment), problem_line)(input)?;
            let [_, _, (_, num_vars)] = sizes;
            Ok((input, (VarSet::new(num_vars), sizes)))
        }
    }
}

/// Parse a NNF file
pub fn parse<'a, E>(options: &ParseOptions) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Problem, E>
where
    E: ParseError<&'a [u8]> + ContextError<&'a [u8]> + FromExternalError<&'a [u8], String>,
{
    let parse_var_orders = options.var_order;
    let check_acyclic = options.check_acyclic;

    move |input| {
        let (mut input, (vars, [num_nodes, num_edges, num_inputs])) =
            preamble(parse_var_orders)(input)?;

        if num_nodes.1 == 0 {
            return fail(num_nodes.0, "NNF must have at least one node");
        }

        let mut circuit = Circuit::new(vars);
        circuit.reserve_gates(num_nodes.1);
        circuit.reserve_gate_inputs(num_edges.1);

        let mut nodes = Vec::with_capacity(num_nodes.1);
        let mut gate_spans = Vec::with_capacity(num_nodes.1);

        for _ in 0..num_nodes.1 {
            let (inp, l) = match input {
                [kind @ (b'A' | b'a' | b'B' | b'b' | b'X' | b'x'), inp @ ..] => {
                    let kind = if let b'X' | b'x' = kind {
                        GateKind::Xor
                    } else {
                        GateKind::And
                    };
                    let (mut inp, children) = preceded(space1, u64)(inp)?;
                    if children == 0 {
                        (inp, kind.empty_gate())
                    } else {
                        let l = circuit.push_gate(kind);
                        for _ in 0..children {
                            let (i, child) = preceded(space1, consumed(u64))(inp)?;
                            inp = i;

                            if child.1 >= num_nodes.1 as u64 {
                                return fail_with_contexts([
                                    (child.0, "invalid node number"),
                                    (num_nodes.0, "number of nodes given here"),
                                ]);
                            }
                            // In contrast to the original c2d format, we do not enforce a
                            // topological order on the input. We just collect the node IDs now and
                            // map them to `Literal`s later.
                            circuit.push_gate_input(Literal(child.1 as usize));
                        }
                        gate_spans.push(&input[..input.offset(inp)]);
                        (inp, l)
                    }
                }
                [b'O' | b'o', inp @ ..] => {
                    let (inp, conflict) = preceded(space1, consumed(u64))(inp)?;
                    if conflict.1 > num_inputs.1 as u64 {
                        return fail_with_contexts([
                            (conflict.0, "invalid variable"),
                            (num_inputs.0, "number of input variables given here"),
                        ]);
                    }

                    let (mut inp, children) = preceded(space1, consumed(u64))(inp)?;

                    if conflict.1 != 0 && children.1 != 2 {
                        let child_msg = "expected 2 children, since a conflict variable is given";
                        let conflict_msg =
                            "using 0 in place of the conflict variable here allows arbitrary arity";
                        return fail_with_contexts([
                            (children.0, child_msg),
                            (conflict.0, conflict_msg),
                        ]);
                    }

                    if children.1 == 0 {
                        (inp, Literal::FALSE)
                    } else {
                        let l = circuit.push_gate(GateKind::Or);
                        for _ in 0..children.1 {
                            let (i, child) = preceded(space1, consumed(u64))(inp)?;
                            inp = i;

                            if child.1 >= num_nodes.1 as u64 {
                                return fail_with_contexts([
                                    (child.0, "invalid node number"),
                                    (num_nodes.0, "number of nodes given here"),
                                ]);
                            }
                            circuit.push_gate_input(Literal(child.1 as usize));
                        }
                        gate_spans.push(&input[..input.offset(inp)]);
                        (inp, l)
                    }
                }
                [b'L' | b'l', inp @ ..] => {
                    let (inp, lit) = preceded(space1, consumed(i64))(inp)?;
                    let var = lit.1.unsigned_abs();
                    if var == 0 || var > num_inputs.1 as u64 {
                        return fail_with_contexts([
                            (lit.0, "invalid literal"),
                            (num_inputs.0, "number of input variables given here"),
                        ]);
                    }
                    (inp, Literal::from_input(lit.1 < 0, (var - 1) as usize))
                }
                inp => {
                    return fail(
                        word_span(inp),
                        "expected a node ('A', 'B', 'O', 'X', or 'L')",
                    );
                }
            };
            nodes.push(l);
            input = preceded(space0, line_ending)(inp)?.0;
        }

        let (input, _) = preceded(multispace0, eof)(input)?;

        for l in circuit.gates.all_elements_mut() {
            *l = nodes[l.0];
        }
        if check_acyclic && let Some(l) = circuit.find_cycle() {
            return fail(
                gate_spans[l.get_gate_no().unwrap()],
                "node depends on itself",
            );
        }

        let problem = Problem {
            circuit,
            details: crate::ProblemDetails::Root(*nodes.last().unwrap()),
        };
        Ok((input, problem))
    }
}

#[cfg(test)]
mod tests {
    use nom::Finish;

    use crate::{Gate, util::test::*};

    use super::*;

    /// NNF taken from the c2d manual
    #[test]
    fn c2d_example() {
        let input = b"nnf 15 17 4\n\
            L -3\n\
            L -2\n\
            L 1\n\
            A 3 2 1 0\n\
            L 3\n\
            O 3 2 4 3\n\
            L -4\n\
            A 2 6 5\n\
            L 4\n\
            A 2 2 8\n\
            A 2 1 4\n\
            L 2\n\
            O 2 2 11 10\n\
            A 2 12 9\n\
            O 4 2 13 7\n";

        let (input, problem) = parse::<()>(&OPTS_NO_ORDER)(input).finish().unwrap();
        assert!(input.is_empty());

        let (circuit, root) = unwrap_problem(problem);
        let inputs = circuit.inputs();
        assert_eq!(inputs.len(), 4);
        assert!(inputs.order().is_none());

        let nodes = &[
            !v(2), // L -3
            !v(1), // L -2
            v(0),  // L 1
            g(0),  // A 3 2 1 0
            v(2),  // L 3
            g(1),  // O 3 2 4 3
            !v(3), // L -4
            g(2),  // A 2 6 5
            v(3),  // L 4
            g(3),  // A 2 2 8
            g(4),  // A 2 1 4
            v(1),  // L 2
            g(5),  // O 2 2 11 10
            g(6),  // A 2 12 9
            g(7),  // O 4 2 13 7
        ];
        assert_eq!(root, *nodes.last().unwrap());

        for (i, &gate) in [
            Gate::and(&[nodes[2], nodes[1], nodes[0]]), // 0: A 3 2 1 0
            Gate::or(&[nodes[4], nodes[3]]),            // 1: O 3 2 4 3
            Gate::and(&[nodes[6], nodes[5]]),           // 2: A 2 6 5
            Gate::and(&[nodes[2], nodes[8]]),           // 3: A 2 2 8
            Gate::and(&[nodes[1], nodes[4]]),           // 4: A 2 1 4
            Gate::or(&[nodes[11], nodes[10]]),          // 5: O 2 2 11 10
            Gate::and(&[nodes[12], nodes[9]]),          // 6: A 2 12 9
            Gate::or(&[nodes[13], nodes[7]]),           // 7: O 4 2 13 7
        ]
        .iter()
        .enumerate()
        {
            assert_eq!(circuit.gate(g(i)), Some(gate), "mismatch for gate {i}");
        }
    }
}