mrs-tptp 0.2.2

A robust TPTP parser for Rust using winnow
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
//! THF (Typed Higher-order Form) AST types.
//!
//! THF is higher-order logic with lambda abstraction, function application,
//! and higher-order types.

use super::common::*;

/// A THF statement
#[derive(Debug, Clone, PartialEq)]
pub enum THFStatement<'a> {
    /// A logical formula
    Logical(THFFormula<'a>),
    /// A type declaration: name : type
    Typing(THFTyping<'a>),
    /// A subtype declaration: type << type
    Subtype(THFType<'a>, THFType<'a>),
    /// A sequent
    Sequent(Vec<THFFormula<'a>>, Vec<THFFormula<'a>>),
    /// A logic specification (NXF/NHF): $modal == [ ... ]
    Logic(LogicSpecification<'a>),
}

/// A THF type declaration
#[derive(Debug, Clone, PartialEq)]
pub struct THFTyping<'a> {
    pub symbol: THFTypedSymbol<'a>,
    pub typ: THFType<'a>,
}

/// A symbol being typed in THF
#[derive(Debug, Clone, PartialEq)]
pub enum THFTypedSymbol<'a> {
    /// An atomic word
    Atom(AtomicWord<'a>),
    /// A defined word
    Defined(DefinedWord<'a>),
    /// A system word
    System(SystemWord<'a>),
}

/// A THF formula
#[derive(Debug, Clone, PartialEq)]
pub enum THFFormula<'a> {
    /// Atomic formula (including constants and propositions)
    Atomic(THFAtomicFormula<'a>),
    /// Variable
    Variable(&'a str),
    /// Negation: ~F
    Negation(Box<THFFormula<'a>>),
    /// Quantified formula: Q [vars : types] : F
    Quantified {
        quantifier: THFQuantifier,
        variables: Vec<THFVariable<'a>>,
        formula: Box<THFFormula<'a>>,
    },
    /// Binary formula: F op G
    Binary {
        left: Box<THFFormula<'a>>,
        connective: THFBinaryConnective,
        right: Box<THFFormula<'a>>,
    },
    /// Application: F @ G
    Application(Box<THFFormula<'a>>, Box<THFFormula<'a>>),
    /// Lambda abstraction: ^[vars] : F
    Lambda {
        variables: Vec<THFVariable<'a>>,
        body: Box<THFFormula<'a>>,
    },
    /// Equality: F = G
    Equality(Box<THFFormula<'a>>, Box<THFFormula<'a>>),
    /// Inequality: F != G
    Inequality(Box<THFFormula<'a>>, Box<THFFormula<'a>>),
    /// Parenthesized formula
    Parens(Box<THFFormula<'a>>),
    /// Type annotation: F : type
    Typed(Box<THFFormula<'a>>, THFType<'a>),
    /// Conditional (THX): $ite(cond, then, else)
    Conditional {
        condition: Box<THFFormula<'a>>,
        then_branch: Box<THFFormula<'a>>,
        else_branch: Box<THFFormula<'a>>,
    },
    /// Let expression (THX)
    Let {
        definitions: Vec<THFLetDef<'a>>,
        body: Box<THFFormula<'a>>,
    },
    /// Tuple: [f1, f2, ...]
    Tuple(Vec<THFFormula<'a>>),
    /// A number
    Number(Number<'a>),
    /// A distinct object
    DistinctObject(&'a str),
    /// Non-classical operator (NHF): {#op}(formula) or {#op}
    NonClassical {
        operator: NonClassicalOperator<'a>,
        formula: Option<Box<THFFormula<'a>>>,
    },
    /// A connective used as a term: (&), (~), (~&), etc.
    ConnectiveTerm(THFBinaryConnective),
    /// Unary connective as term: (~)
    UnaryConnectiveTerm,
    /// TH1: Quantifier used as a term: (!!), (??), (@@+), (@@-)
    QuantifierTerm(THFQuantifier),
    /// TH1: Equality as a term: (@=)
    EqualityTerm,
    /// TH1: A type used as a formula (e.g., as argument to @=)
    TypeAsFormula(THFType<'a>),
}

impl<'a> THFFormula<'a> {
    /// Create an application F @ G
    pub fn app(f: THFFormula<'a>, g: THFFormula<'a>) -> Self {
        THFFormula::Application(Box::new(f), Box::new(g))
    }

    /// Create a lambda abstraction
    pub fn lambda(variables: Vec<THFVariable<'a>>, body: THFFormula<'a>) -> Self {
        THFFormula::Lambda {
            variables,
            body: Box::new(body),
        }
    }
}

/// A THF variable with type annotation
#[derive(Debug, Clone, PartialEq)]
pub struct THFVariable<'a> {
    pub name: &'a str,
    pub typ: Option<THFType<'a>>,
}

impl<'a> THFVariable<'a> {
    pub fn new(name: &'a str) -> Self {
        THFVariable { name, typ: None }
    }

    pub fn typed(name: &'a str, typ: THFType<'a>) -> Self {
        THFVariable {
            name,
            typ: Some(typ),
        }
    }
}

/// THF quantifiers (including higher-order quantifiers)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum THFQuantifier {
    /// ! (universal)
    Forall,
    /// ? (existential)
    Exists,
    /// ^ (lambda - treated as quantifier in some contexts)
    Lambda,
    /// @+ (choice/epsilon)
    Choice,
    /// @- (description/iota)
    Description,
    /// !> (type forall, TH1)
    ForallType,
    /// ?* (type exists, TH1)
    ExistsType,
}

impl THFQuantifier {
    pub fn as_str(&self) -> &'static str {
        match self {
            THFQuantifier::Forall => "!",
            THFQuantifier::Exists => "?",
            THFQuantifier::Lambda => "^",
            THFQuantifier::Choice => "@+",
            THFQuantifier::Description => "@-",
            THFQuantifier::ForallType => "!>",
            THFQuantifier::ExistsType => "?*",
        }
    }
}

/// THF binary connectives (extends FOF connectives)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum THFBinaryConnective {
    // Logical connectives
    /// <=> (equivalence)
    Iff,
    /// => (implication)
    Impl,
    /// <= (reverse implication)
    RevImpl,
    /// <~> (xor)
    Xor,
    /// ~| (nor)
    Nor,
    /// ~& (nand)
    Nand,
    /// | (or)
    Or,
    /// & (and)
    And,
    // Type constructors
    /// > (function type)
    Arrow,
    /// * (product type)
    Product,
    /// + (sum type)
    Sum,
    // Other
    /// := (assignment)
    Assign,
    /// == (meta identity)
    MetaIdentity,
}

impl THFBinaryConnective {
    pub fn as_str(&self) -> &'static str {
        match self {
            THFBinaryConnective::Iff => "<=>",
            THFBinaryConnective::Impl => "=>",
            THFBinaryConnective::RevImpl => "<=",
            THFBinaryConnective::Xor => "<~>",
            THFBinaryConnective::Nor => "~|",
            THFBinaryConnective::Nand => "~&",
            THFBinaryConnective::Or => "|",
            THFBinaryConnective::And => "&",
            THFBinaryConnective::Arrow => ">",
            THFBinaryConnective::Product => "*",
            THFBinaryConnective::Sum => "+",
            THFBinaryConnective::Assign => ":=",
            THFBinaryConnective::MetaIdentity => "==",
        }
    }

    /// Convert from common BinaryConnective
    pub fn from_common(c: BinaryConnective) -> Self {
        match c {
            BinaryConnective::Iff => THFBinaryConnective::Iff,
            BinaryConnective::Impl => THFBinaryConnective::Impl,
            BinaryConnective::RevImpl => THFBinaryConnective::RevImpl,
            BinaryConnective::Xor => THFBinaryConnective::Xor,
            BinaryConnective::Nor => THFBinaryConnective::Nor,
            BinaryConnective::Nand => THFBinaryConnective::Nand,
            BinaryConnective::Or => THFBinaryConnective::Or,
            BinaryConnective::And => THFBinaryConnective::And,
        }
    }
}

/// A THF atomic formula
#[derive(Debug, Clone, PartialEq)]
pub enum THFAtomicFormula<'a> {
    /// Plain atomic: f(args) or constant
    Plain(AtomicWord<'a>, Vec<THFFormula<'a>>),
    /// Defined atomic: $f(args)
    Defined(DefinedWord<'a>, Vec<THFFormula<'a>>),
    /// System atomic: $$f(args)
    System(SystemWord<'a>, Vec<THFFormula<'a>>),
    /// $true
    True,
    /// $false
    False,
}

/// A THF let definition
#[derive(Debug, Clone, PartialEq)]
pub struct THFLetDef<'a> {
    pub symbol: AtomicWord<'a>,
    pub type_args: Vec<&'a str>,
    pub params: Vec<THFVariable<'a>>,
    pub definition: THFFormula<'a>,
}

/// A THF type
#[derive(Debug, Clone, PartialEq)]
pub enum THFType<'a> {
    /// Atomic type
    Atomic(AtomicWord<'a>),
    /// Defined type ($i, $o, $int, etc.)
    Defined(THFDefinedType),
    /// Type variable
    Variable(&'a str),
    /// Function type: a > b
    Arrow(Box<THFType<'a>>, Box<THFType<'a>>),
    /// Product type: a * b
    Product(Box<THFType<'a>>, Box<THFType<'a>>),
    /// Sum type: a + b
    Sum(Box<THFType<'a>>, Box<THFType<'a>>),
    /// Type application: F(args)
    Application(Box<THFType<'a>>, Vec<THFType<'a>>),
    /// Tuple type: [t1, t2, ...]
    Tuple(Vec<THFType<'a>>),
    /// Quantified type: !> [vars] : type (for polymorphism with $tType variables)
    Quantified {
        quantifier: THFQuantifier,
        variables: Vec<&'a str>,
        typ: Box<THFType<'a>>,
    },
    /// Dependent quantified type: !> [X: T, ...] : type (for dependent types with non-$tType)
    DependentQuantified {
        quantifier: THFQuantifier,
        variables: Vec<THFVariable<'a>>,
        typ: Box<THFType<'a>>,
    },
    /// Parenthesized type
    Parens(Box<THFType<'a>>),
    /// Mapping type (for higher-kinded types)
    Mapping(Vec<THFType<'a>>, Box<THFType<'a>>),
}

impl<'a> THFType<'a> {
    /// Individual type ($i)
    pub fn individual() -> Self {
        THFType::Defined(THFDefinedType::I)
    }

    /// Boolean type ($o)
    pub fn boolean() -> Self {
        THFType::Defined(THFDefinedType::O)
    }

    /// Type type ($tType)
    pub fn ttype() -> Self {
        THFType::Defined(THFDefinedType::TType)
    }

    /// Create a function type a > b
    pub fn arrow(from: THFType<'a>, to: THFType<'a>) -> Self {
        THFType::Arrow(Box::new(from), Box::new(to))
    }
}

/// THF defined types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum THFDefinedType {
    /// $i - individual
    I,
    /// $o - boolean
    O,
    /// $int - integer
    Int,
    /// $rat - rational
    Rat,
    /// $real - real
    Real,
    /// $tType - type of types
    TType,
}

impl THFDefinedType {
    pub fn as_str(&self) -> &'static str {
        match self {
            THFDefinedType::I => "$i",
            THFDefinedType::O => "$o",
            THFDefinedType::Int => "$int",
            THFDefinedType::Rat => "$rat",
            THFDefinedType::Real => "$real",
            THFDefinedType::TType => "$tType",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "$i" | "i" => Some(THFDefinedType::I),
            "$o" | "o" => Some(THFDefinedType::O),
            "$int" | "int" => Some(THFDefinedType::Int),
            "$rat" | "rat" => Some(THFDefinedType::Rat),
            "$real" | "real" => Some(THFDefinedType::Real),
            "$tType" | "tType" => Some(THFDefinedType::TType),
            _ => None,
        }
    }
}

/// Non-classical operators (NHF - modal, temporal, epistemic)
#[derive(Debug, Clone, PartialEq)]
pub enum NonClassicalOperator<'a> {
    /// {#box} or [.] or [..] - necessity (modal)
    Box,
    /// {#dia} or <.> or <..> - possibility (modal)
    Diamond,
    /// {#always} - always (temporal)
    Always,
    /// {#eventually} - eventually (temporal)
    Eventually,
    /// {#knows} - knows (epistemic)
    Knows,
    /// {#believes} - believes (epistemic)
    Believes,
    /// Short form box with index: [#name]
    ShortBox(Option<&'a str>),
    /// Short form diamond with index: <#name>
    ShortDiamond(Option<&'a str>),
    /// Custom operator with optional index
    Custom {
        name: &'a str,
        index: Option<&'a str>,
    },
}

impl<'a> NonClassicalOperator<'a> {
    pub fn as_str(&self) -> &'static str {
        match self {
            NonClassicalOperator::Box => "{#box}",
            NonClassicalOperator::Diamond => "{#dia}",
            NonClassicalOperator::Always => "{#always}",
            NonClassicalOperator::Eventually => "{#eventually}",
            NonClassicalOperator::Knows => "{#knows}",
            NonClassicalOperator::Believes => "{#believes}",
            NonClassicalOperator::ShortBox(_) => "[.]",
            NonClassicalOperator::ShortDiamond(_) => "<.>",
            NonClassicalOperator::Custom { .. } => "{#custom}",
        }
    }
}

/// Logic specification for non-classical logics (NXF/NHF)
#[derive(Debug, Clone, PartialEq)]
pub struct LogicSpecification<'a> {
    /// The logic family (e.g., $modal, $alethic_modal, $epistemic_modal)
    pub logic_family: &'a str,
    /// Properties of the logic
    pub properties: Vec<LogicProperty<'a>>,
}

/// A property in a logic specification
#[derive(Debug, Clone, PartialEq)]
pub enum LogicProperty<'a> {
    /// Simple key-value: key == value
    KeyValue { key: &'a str, value: LogicValue<'a> },
    /// Nested list: key == [ ... ]
    KeyList {
        key: &'a str,
        values: Vec<LogicProperty<'a>>,
    },
}

/// A value in a logic specification
#[derive(Debug, Clone, PartialEq)]
pub enum LogicValue<'a> {
    /// A simple identifier (e.g., $rigid, $constant)
    Atom(&'a str),
    /// A quoted string (e.g., 'LOG002_1.l')
    String(&'a str),
    /// A list of values
    List(Vec<LogicValue<'a>>),
    /// A property assignment: name == value
    Property {
        name: &'a str,
        value: Box<LogicValue<'a>>,
    },
}