lolli-parse 0.2.0

Parser for the Lolli linear logic workbench
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
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
//! # lolli-parse
//!
//! Parser for the Lolli linear logic workbench.
//!
//! This crate provides parsing functionality for linear logic formulas and sequents.
//!
//! ## Supported Syntax
//!
//! | Connective | Unicode | ASCII |
//! |------------|---------|-------|
//! | Tensor | ⊗ | * |
//! | Par | ⅋ | \`|\` |
//! | Lolli | ⊸ | -o |
//! | With | & | & |
//! | Plus | ⊕ | + |
//! | Bang | ! | ! |
//! | Why not | ? | ? |
//! | Negation | A⊥ | A^ |
//! | Turnstile | ⊢ | \|- |
//!
//! ## Example
//!
//! ```
//! use lolli_parse::{parse_formula, parse_sequent};
//!
//! let formula = parse_formula("A -o B").unwrap();
//! assert_eq!(formula.pretty(), "(A ⊸ B)");
//!
//! let sequent = parse_sequent("A, B |- A * B").unwrap();
//! assert_eq!(sequent.antecedent.len(), 2);
//! ```

#![warn(missing_docs)]
#![warn(clippy::all)]

use pest::Parser;
use pest_derive::Parser;

pub use lolli_core::{Formula, Sequent, TwoSidedSequent};

#[derive(Parser)]
#[grammar = "grammar.pest"]
struct LolliParser;

/// Parse error type.
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
    /// Unexpected token in input
    #[error("Unexpected token: {0}")]
    UnexpectedToken(String),

    /// Unknown operator
    #[error("Unknown operator: {0}")]
    UnknownOperator(String),

    /// Unexpected rule during parsing
    #[error("Unexpected rule: {0}")]
    UnexpectedRule(String),

    /// Pest parsing error
    #[error("Parse error: {0}")]
    PestError(#[from] pest::error::Error<Rule>),

    /// Empty input
    #[error("Empty input")]
    EmptyInput,
}

/// Parse a formula from a string.
///
/// # Examples
///
/// ```
/// use lolli_parse::parse_formula;
///
/// // Simple atom
/// let f = parse_formula("A").unwrap();
///
/// // Linear implication
/// let f = parse_formula("A -o B").unwrap();
///
/// // Complex formula
/// let f = parse_formula("!A * B -o C + D").unwrap();
/// ```
///
/// # Errors
///
/// Returns a `ParseError` if the input is not a valid formula.
pub fn parse_formula(input: &str) -> Result<Formula, ParseError> {
    let pairs = LolliParser::parse(Rule::formula, input)?;
    let pair = pairs.into_iter().next().ok_or(ParseError::EmptyInput)?;
    build_formula(pair)
}

/// Parse a sequent from a string.
///
/// # Examples
///
/// ```
/// use lolli_parse::parse_sequent;
///
/// // Two-sided sequent
/// let s = parse_sequent("A, B |- C").unwrap();
///
/// // One-sided sequent (right side only)
/// let s = parse_sequent("|- A, B").unwrap();
/// ```
///
/// # Errors
///
/// Returns a `ParseError` if the input is not a valid sequent.
pub fn parse_sequent(input: &str) -> Result<TwoSidedSequent, ParseError> {
    let pairs = LolliParser::parse(Rule::sequent, input)?;
    let pair = pairs.into_iter().next().ok_or(ParseError::EmptyInput)?;
    build_sequent(pair)
}

use pest::iterators::Pair;

fn build_formula(pair: Pair<Rule>) -> Result<Formula, ParseError> {
    match pair.as_rule() {
        Rule::formula => {
            let inner = pair.into_inner().next().ok_or(ParseError::EmptyInput)?;
            build_formula(inner)
        }
        Rule::lolli_expr => build_lolli_expr(pair),
        Rule::par_expr => build_left_assoc_binary(pair, Rule::par_op, Formula::par),
        Rule::tensor_expr => build_left_assoc_binary(pair, Rule::tensor_op, Formula::tensor),
        Rule::plus_expr => build_left_assoc_binary(pair, Rule::plus_op, Formula::plus),
        Rule::with_expr => build_left_assoc_binary(pair, Rule::with_op, Formula::with),
        Rule::unary_expr => build_unary_expr(pair),
        Rule::primary_expr => build_primary_expr(pair),
        Rule::ident => Ok(Formula::Atom(pair.as_str().to_string())),
        Rule::one => Ok(Formula::One),
        Rule::bottom => Ok(Formula::Bottom),
        Rule::top => Ok(Formula::Top),
        Rule::zero => Ok(Formula::Zero),
        _ => Err(ParseError::UnexpectedRule(format!("{:?}", pair.as_rule()))),
    }
}

fn build_lolli_expr(pair: Pair<Rule>) -> Result<Formula, ParseError> {
    let mut inner = pair.into_inner();
    let first = inner.next().ok_or(ParseError::EmptyInput)?;
    let mut result = build_formula(first)?;

    // Check for lolli operator and right side
    while let Some(op_or_expr) = inner.next() {
        if op_or_expr.as_rule() == Rule::lolli_op {
            // Get the right side (which is itself a lolli_expr for right associativity)
            let right = inner.next().ok_or(ParseError::EmptyInput)?;
            let right_formula = build_formula(right)?;
            result = Formula::lolli(result, right_formula);
        } else {
            // It's the right side directly
            let right_formula = build_formula(op_or_expr)?;
            result = Formula::lolli(result, right_formula);
        }
    }

    Ok(result)
}

fn build_left_assoc_binary<F>(
    pair: Pair<Rule>,
    _op_rule: Rule,
    constructor: F,
) -> Result<Formula, ParseError>
where
    F: Fn(Formula, Formula) -> Formula,
{
    let mut inner = pair.into_inner();
    let first = inner.next().ok_or(ParseError::EmptyInput)?;
    let mut result = build_formula(first)?;

    while let Some(next) = inner.next() {
        // Skip operator tokens
        if next.as_rule() == _op_rule {
            continue;
        }
        let right = build_formula(next)?;
        result = constructor(result, right);
    }

    Ok(result)
}

fn build_unary_expr(pair: Pair<Rule>) -> Result<Formula, ParseError> {
    let mut inner = pair.into_inner().peekable();

    // Check for prefix operators
    let first = inner.peek().ok_or(ParseError::EmptyInput)?;

    match first.as_rule() {
        Rule::bang_op => {
            inner.next(); // consume the operator
            let operand = inner.next().ok_or(ParseError::EmptyInput)?;
            let formula = build_formula(operand)?;
            Ok(Formula::of_course(formula))
        }
        Rule::whynot_op => {
            inner.next(); // consume the operator
            let operand = inner.next().ok_or(ParseError::EmptyInput)?;
            let formula = build_formula(operand)?;
            Ok(Formula::why_not(formula))
        }
        _ => {
            // Primary expression with optional negation suffix
            let primary = inner.next().ok_or(ParseError::EmptyInput)?;
            let mut formula = build_formula(primary)?;

            // Check for negation suffix
            if let Some(suffix) = inner.next() {
                if suffix.as_rule() == Rule::negation_suffix {
                    formula = formula.negate();
                }
            }

            Ok(formula)
        }
    }
}

fn build_primary_expr(pair: Pair<Rule>) -> Result<Formula, ParseError> {
    let inner = pair.into_inner().next().ok_or(ParseError::EmptyInput)?;
    build_formula(inner)
}

fn build_sequent(pair: Pair<Rule>) -> Result<TwoSidedSequent, ParseError> {
    let mut antecedent = Vec::new();
    let mut succedent = Vec::new();
    let mut seen_turnstile = false;

    for inner in pair.into_inner() {
        match inner.as_rule() {
            Rule::turnstile => {
                seen_turnstile = true;
            }
            Rule::formula_list => {
                let formulas = build_formula_list(inner)?;
                if seen_turnstile {
                    succedent = formulas;
                } else {
                    antecedent = formulas;
                }
            }
            _ => {}
        }
    }

    Ok(TwoSidedSequent::new(antecedent, succedent))
}

fn build_formula_list(pair: Pair<Rule>) -> Result<Vec<Formula>, ParseError> {
    let mut formulas = Vec::new();
    for inner in pair.into_inner() {
        if inner.as_rule() == Rule::formula {
            formulas.push(build_formula(inner)?);
        }
    }
    Ok(formulas)
}

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

    #[test]
    fn test_parse_atoms() {
        let f = parse_formula("A").unwrap();
        assert_eq!(f, Formula::atom("A"));

        let f = parse_formula("foo").unwrap();
        assert_eq!(f, Formula::atom("foo"));

        let f = parse_formula("FileHandle").unwrap();
        assert_eq!(f, Formula::atom("FileHandle"));
    }

    #[test]
    fn test_parse_units() {
        assert_eq!(parse_formula("1").unwrap(), Formula::One);
        assert_eq!(parse_formula("one").unwrap(), Formula::One);
        assert_eq!(parse_formula("bot").unwrap(), Formula::Bottom);
        assert_eq!(parse_formula("bottom").unwrap(), Formula::Bottom);
        assert_eq!(parse_formula("top").unwrap(), Formula::Top);
        assert_eq!(parse_formula("0").unwrap(), Formula::Zero);
        assert_eq!(parse_formula("zero").unwrap(), Formula::Zero);
    }

    #[test]
    fn test_parse_negation() {
        let f = parse_formula("A^").unwrap();
        assert_eq!(f, Formula::neg_atom("A"));

        let f = parse_formula("A⊥").unwrap();
        assert_eq!(f, Formula::neg_atom("A"));
    }

    #[test]
    fn test_parse_unary() {
        let f = parse_formula("!A").unwrap();
        assert_eq!(f, Formula::of_course(Formula::atom("A")));

        let f = parse_formula("?A").unwrap();
        assert_eq!(f, Formula::why_not(Formula::atom("A")));

        let f = parse_formula("!!A").unwrap();
        assert_eq!(
            f,
            Formula::of_course(Formula::of_course(Formula::atom("A")))
        );
    }

    #[test]
    fn test_parse_lolli() {
        let f = parse_formula("A -o B").unwrap();
        assert_eq!(f, Formula::lolli(Formula::atom("A"), Formula::atom("B")));

        // Right associativity: A -o B -o C = A -o (B -o C)
        let f = parse_formula("A -o B -o C").unwrap();
        assert_eq!(
            f,
            Formula::lolli(
                Formula::atom("A"),
                Formula::lolli(Formula::atom("B"), Formula::atom("C"))
            )
        );
    }

    #[test]
    fn test_parse_tensor() {
        let f = parse_formula("A * B").unwrap();
        assert_eq!(f, Formula::tensor(Formula::atom("A"), Formula::atom("B")));

        // Left associativity: A * B * C = (A * B) * C
        let f = parse_formula("A * B * C").unwrap();
        assert_eq!(
            f,
            Formula::tensor(
                Formula::tensor(Formula::atom("A"), Formula::atom("B")),
                Formula::atom("C")
            )
        );
    }

    #[test]
    fn test_parse_plus() {
        let f = parse_formula("A + B").unwrap();
        assert_eq!(f, Formula::plus(Formula::atom("A"), Formula::atom("B")));
    }

    #[test]
    fn test_parse_with() {
        let f = parse_formula("A & B").unwrap();
        assert_eq!(f, Formula::with(Formula::atom("A"), Formula::atom("B")));
    }

    #[test]
    fn test_parse_parentheses() {
        let f = parse_formula("(A -o B)").unwrap();
        assert_eq!(f, Formula::lolli(Formula::atom("A"), Formula::atom("B")));

        // Override precedence
        let f = parse_formula("(A + B) * C").unwrap();
        assert_eq!(
            f,
            Formula::tensor(
                Formula::plus(Formula::atom("A"), Formula::atom("B")),
                Formula::atom("C")
            )
        );
    }

    #[test]
    fn test_parse_precedence() {
        // * binds tighter than -o
        // A * B -o C = (A * B) -o C
        let f = parse_formula("A * B -o C").unwrap();
        assert_eq!(
            f,
            Formula::lolli(
                Formula::tensor(Formula::atom("A"), Formula::atom("B")),
                Formula::atom("C")
            )
        );

        // In our grammar (following the hierarchy):
        // lolli < par < tensor < plus < with < unary
        // So * binds tighter than +
        // A + B * C = (A + B) * C
        let f = parse_formula("A + B * C").unwrap();
        assert_eq!(
            f,
            Formula::tensor(
                Formula::plus(Formula::atom("A"), Formula::atom("B")),
                Formula::atom("C")
            )
        );

        // & binds tightest
        // A & B + C = (A & B) + C
        let f = parse_formula("A & B + C").unwrap();
        assert_eq!(
            f,
            Formula::plus(
                Formula::with(Formula::atom("A"), Formula::atom("B")),
                Formula::atom("C")
            )
        );
    }

    #[test]
    fn test_parse_complex() {
        let f = parse_formula("!A * B -o ?C + D").unwrap();
        // !A * B = tensor(!A, B)
        // ?C + D = plus(?C, D)
        // result = lolli(tensor(!A, B), plus(?C, D))
        assert_eq!(
            f,
            Formula::lolli(
                Formula::tensor(
                    Formula::of_course(Formula::atom("A")),
                    Formula::atom("B")
                ),
                Formula::plus(Formula::why_not(Formula::atom("C")), Formula::atom("D"))
            )
        );
    }

    #[test]
    fn test_parse_sequent() {
        let s = parse_sequent("A, B |- C").unwrap();
        assert_eq!(s.antecedent.len(), 2);
        assert_eq!(s.succedent.len(), 1);
        assert_eq!(s.antecedent[0], Formula::atom("A"));
        assert_eq!(s.antecedent[1], Formula::atom("B"));
        assert_eq!(s.succedent[0], Formula::atom("C"));
    }

    #[test]
    fn test_parse_sequent_one_sided() {
        let s = parse_sequent("|- A, B").unwrap();
        assert_eq!(s.antecedent.len(), 0);
        assert_eq!(s.succedent.len(), 2);
    }

    #[test]
    fn test_parse_sequent_complex() {
        let s = parse_sequent("A * B, C -o D |- E + F").unwrap();
        assert_eq!(s.antecedent.len(), 2);
        assert_eq!(s.succedent.len(), 1);
    }

    #[test]
    fn test_roundtrip() {
        let formulas = vec![
            "A",
            "A -o B",
            "A * B",
            "A + B",
            "A & B",
            "!A",
            "?A",
            "1",
            "bot",
            "top",
            "0",
            "A * B -o C",
            "!A * ?B -o C + D",
        ];

        for input in formulas {
            let f = parse_formula(input).unwrap();
            // Just verify it parses without error
            let _ = f.pretty();
        }
    }
}