lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! MathML type system for semantic validation.
//!
//! Defines types for mathematical expressions based on Content MathML semantics.

use std::collections::HashMap;
use std::fmt;

/// Mathematical type for expressions.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MathType {
    /// Numeric value (integer, real, complex).
    Number,
    /// Variable/identifier.
    Variable,
    /// Function type with domain and codomain.
    Function {
        /// Number of arguments.
        arity: Arity,
        /// Domain type for each argument.
        domain: Vec<MathType>,
        /// Return type.
        codomain: Box<MathType>,
    },
    /// Binary operator.
    BinaryOp,
    /// Unary operator.
    UnaryOp,
    /// N-ary operator (summation, product, etc.).
    NaryOp,
    /// Relation (equals, less than, etc.).
    Relation,
    /// Set type.
    Set,
    /// Vector type.
    Vector {
        /// Element type.
        element: Box<MathType>,
        /// Optional fixed dimension.
        dimension: Option<usize>,
    },
    /// Matrix type.
    Matrix {
        /// Element type.
        element: Box<MathType>,
        /// Optional dimensions (rows, cols).
        dimensions: Option<(usize, usize)>,
    },
    /// Boolean type.
    Boolean,
    /// Unit type (for side-effect operations).
    Unit,
    /// Type variable for inference.
    TypeVar(u32),
    /// Unknown/unresolved type.
    Unknown,
    /// Error type (for invalid expressions).
    Error(String),
}

impl MathType {
    /// Check if this type is numeric (Number, Variable that could be numeric).
    pub fn is_numeric(&self) -> bool {
        matches!(
            self,
            MathType::Number | MathType::Variable | MathType::TypeVar(_)
        )
    }

    /// Check if this type is a function.
    pub fn is_function(&self) -> bool {
        matches!(self, MathType::Function { .. })
    }

    /// Check if this type is an operator.
    pub fn is_operator(&self) -> bool {
        matches!(
            self,
            MathType::BinaryOp | MathType::UnaryOp | MathType::NaryOp
        )
    }

    /// Check if this type is compatible with another for unification.
    pub fn compatible_with(&self, other: &MathType) -> bool {
        match (self, other) {
            // Same types are compatible
            (a, b) if a == b => true,
            // Type variables are compatible with anything
            (MathType::TypeVar(_), _) | (_, MathType::TypeVar(_)) => true,
            // Unknown is compatible with anything
            (MathType::Unknown, _) | (_, MathType::Unknown) => true,
            // Variable can be numeric
            (MathType::Variable, MathType::Number) | (MathType::Number, MathType::Variable) => true,
            // Functions are compatible if arities match
            (MathType::Function { arity: a1, .. }, MathType::Function { arity: a2, .. }) => {
                a1 == a2
            }
            // Vectors are compatible regardless of dimension
            (MathType::Vector { .. }, MathType::Vector { .. }) => true,
            // Matrices are compatible regardless of dimension
            (MathType::Matrix { .. }, MathType::Matrix { .. }) => true,
            _ => false,
        }
    }
}

impl fmt::Display for MathType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MathType::Number => write!(f, "Number"),
            MathType::Variable => write!(f, "Var"),
            MathType::Function {
                arity,
                domain,
                codomain,
            } => {
                let args: Vec<_> = domain.iter().map(|t| t.to_string()).collect();
                write!(
                    f,
                    "({}) -> {} [arity: {:?}]",
                    args.join(", "),
                    codomain,
                    arity
                )
            }
            MathType::BinaryOp => write!(f, "BinOp"),
            MathType::UnaryOp => write!(f, "UnaryOp"),
            MathType::NaryOp => write!(f, "NaryOp"),
            MathType::Relation => write!(f, "Relation"),
            MathType::Set => write!(f, "Set"),
            MathType::Vector { element, dimension } => {
                if let Some(d) = dimension {
                    write!(f, "Vec<{}>^{}", element, d)
                } else {
                    write!(f, "Vec<{}>", element)
                }
            }
            MathType::Matrix {
                element,
                dimensions,
            } => {
                if let Some((r, c)) = dimensions {
                    write!(f, "Mat<{}>^({}x{})", element, r, c)
                } else {
                    write!(f, "Mat<{}>", element)
                }
            }
            MathType::Boolean => write!(f, "Bool"),
            MathType::Unit => write!(f, "()"),
            MathType::TypeVar(id) => write!(f, "T{}", id),
            MathType::Unknown => write!(f, "?"),
            MathType::Error(msg) => write!(f, "Error({})", msg),
        }
    }
}

/// Arity of a function or operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Arity {
    /// No arguments (constant).
    Nullary,
    /// One argument.
    Unary,
    /// Two arguments.
    Binary,
    /// Three arguments.
    Ternary,
    /// Variable number of arguments.
    Variadic,
    /// Specific number of arguments.
    Fixed(usize),
}

impl Arity {
    /// Check if this arity accepts the given number of arguments.
    pub fn accepts(&self, n: usize) -> bool {
        match self {
            Arity::Nullary => n == 0,
            Arity::Unary => n == 1,
            Arity::Binary => n == 2,
            Arity::Ternary => n == 3,
            Arity::Variadic => true,
            Arity::Fixed(k) => n == *k,
        }
    }

    /// Get minimum required arguments.
    pub fn min_args(&self) -> usize {
        match self {
            Arity::Nullary => 0,
            Arity::Unary => 1,
            Arity::Binary => 2,
            Arity::Ternary => 3,
            Arity::Variadic => 0,
            Arity::Fixed(k) => *k,
        }
    }
}

/// Type signature for a mathematical construct.
#[derive(Debug, Clone)]
pub struct TypeSignature {
    /// Name of the construct.
    pub name: String,
    /// The type.
    pub math_type: MathType,
    /// Alternative names (aliases).
    pub aliases: Vec<String>,
    /// Semantic category.
    pub category: SemanticCategory,
}

impl TypeSignature {
    /// Create a new type signature.
    pub fn new(name: impl Into<String>, math_type: MathType, category: SemanticCategory) -> Self {
        Self {
            name: name.into(),
            math_type,
            aliases: Vec::new(),
            category,
        }
    }

    /// Add an alias.
    pub fn with_alias(mut self, alias: impl Into<String>) -> Self {
        self.aliases.push(alias.into());
        self
    }
}

/// Semantic category of a mathematical construct.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SemanticCategory {
    /// Arithmetic operations.
    Arithmetic,
    /// Algebraic operations.
    Algebra,
    /// Calculus operations.
    Calculus,
    /// Set theory operations.
    SetTheory,
    /// Logic operations.
    Logic,
    /// Linear algebra operations.
    LinearAlgebra,
    /// Trigonometric functions.
    Trigonometry,
    /// Constants.
    Constant,
    /// Variable/identifier.
    Variable,
    /// Delimiter/grouping.
    Delimiter,
    /// Formatting/presentation.
    Presentation,
}

/// Type environment mapping identifiers to types.
#[derive(Debug, Clone, Default)]
pub struct TypeEnvironment {
    /// Variable bindings.
    bindings: HashMap<String, MathType>,
    /// Parent environment (for scoping).
    parent: Option<Box<TypeEnvironment>>,
}

impl TypeEnvironment {
    /// Create a new empty environment.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a child environment.
    pub fn child(&self) -> Self {
        Self {
            bindings: HashMap::new(),
            parent: Some(Box::new(self.clone())),
        }
    }

    /// Bind a variable to a type.
    pub fn bind(&mut self, name: impl Into<String>, ty: MathType) {
        self.bindings.insert(name.into(), ty);
    }

    /// Look up a variable's type.
    pub fn lookup(&self, name: &str) -> Option<&MathType> {
        self.bindings
            .get(name)
            .or_else(|| self.parent.as_ref().and_then(|p| p.lookup(name)))
    }

    /// Check if a variable is bound.
    pub fn contains(&self, name: &str) -> bool {
        self.lookup(name).is_some()
    }
}

/// Result of type checking.
#[derive(Debug, Clone)]
pub struct TypeResult {
    /// Inferred type.
    pub inferred_type: MathType,
    /// Any type errors found.
    pub errors: Vec<TypeError>,
    /// Warnings (non-fatal issues).
    pub warnings: Vec<TypeWarning>,
}

impl TypeResult {
    /// Create a successful result.
    pub fn ok(ty: MathType) -> Self {
        Self {
            inferred_type: ty,
            errors: Vec::new(),
            warnings: Vec::new(),
        }
    }

    /// Create an error result.
    pub fn error(ty: MathType, error: TypeError) -> Self {
        Self {
            inferred_type: ty,
            errors: vec![error],
            warnings: Vec::new(),
        }
    }

    /// Check if type checking succeeded.
    pub fn is_ok(&self) -> bool {
        self.errors.is_empty()
    }

    /// Add an error.
    pub fn with_error(mut self, error: TypeError) -> Self {
        self.errors.push(error);
        self
    }

    /// Add a warning.
    pub fn with_warning(mut self, warning: TypeWarning) -> Self {
        self.warnings.push(warning);
        self
    }
}

/// Type error.
#[derive(Debug, Clone)]
pub struct TypeError {
    /// Error kind.
    pub kind: TypeErrorKind,
    /// Position in the expression (if known).
    pub position: Option<usize>,
    /// Error message.
    pub message: String,
}

impl TypeError {
    /// Create a new type error.
    pub fn new(kind: TypeErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            position: None,
            message: message.into(),
        }
    }

    /// Set position.
    pub fn at(mut self, pos: usize) -> Self {
        self.position = Some(pos);
        self
    }
}

impl fmt::Display for TypeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(pos) = self.position {
            write!(f, "[{}] {:?}: {}", pos, self.kind, self.message)
        } else {
            write!(f, "{:?}: {}", self.kind, self.message)
        }
    }
}

/// Kind of type error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeErrorKind {
    /// Type mismatch.
    TypeMismatch,
    /// Wrong number of arguments.
    ArityMismatch,
    /// Undefined variable.
    UndefinedVariable,
    /// Invalid operator application.
    InvalidOperator,
    /// Division by zero.
    DivisionByZero,
    /// Invalid expression structure.
    InvalidStructure,
    /// Ambiguous type.
    AmbiguousType,
}

/// Type warning (non-fatal issue).
#[derive(Debug, Clone)]
pub struct TypeWarning {
    /// Warning kind.
    pub kind: TypeWarningKind,
    /// Position in the expression.
    pub position: Option<usize>,
    /// Warning message.
    pub message: String,
}

impl TypeWarning {
    /// Create a new warning.
    pub fn new(kind: TypeWarningKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            position: None,
            message: message.into(),
        }
    }
}

/// Kind of type warning.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeWarningKind {
    /// Implicit type coercion.
    ImplicitCoercion,
    /// Unused variable.
    UnusedVariable,
    /// Potential ambiguity.
    Ambiguity,
    /// Deprecated construct.
    Deprecated,
}

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

    #[test]
    fn test_math_type_display() {
        assert_eq!(format!("{}", MathType::Number), "Number");
        assert_eq!(format!("{}", MathType::Variable), "Var");
        assert_eq!(format!("{}", MathType::BinaryOp), "BinOp");
    }

    #[test]
    fn test_math_type_is_numeric() {
        assert!(MathType::Number.is_numeric());
        assert!(MathType::Variable.is_numeric());
        assert!(!MathType::Set.is_numeric());
    }

    #[test]
    fn test_math_type_compatible() {
        assert!(MathType::Number.compatible_with(&MathType::Number));
        assert!(MathType::Number.compatible_with(&MathType::Variable));
        assert!(MathType::TypeVar(0).compatible_with(&MathType::Set));
        assert!(!MathType::Set.compatible_with(&MathType::Number));
    }

    #[test]
    fn test_arity_accepts() {
        assert!(Arity::Nullary.accepts(0));
        assert!(!Arity::Nullary.accepts(1));
        assert!(Arity::Unary.accepts(1));
        assert!(Arity::Binary.accepts(2));
        assert!(Arity::Variadic.accepts(5));
        assert!(Arity::Fixed(3).accepts(3));
        assert!(!Arity::Fixed(3).accepts(2));
    }

    #[test]
    fn test_type_environment() {
        let mut env = TypeEnvironment::new();
        env.bind("x", MathType::Number);
        env.bind(
            "f",
            MathType::Function {
                arity: Arity::Unary,
                domain: vec![MathType::Number],
                codomain: Box::new(MathType::Number),
            },
        );

        assert_eq!(env.lookup("x"), Some(&MathType::Number));
        assert!(env.lookup("f").is_some());
        assert!(env.lookup("y").is_none());
    }

    #[test]
    fn test_type_environment_scoping() {
        let mut parent = TypeEnvironment::new();
        parent.bind("x", MathType::Number);

        let mut child = parent.child();
        child.bind("y", MathType::Variable);

        // Child can see parent's bindings
        assert!(child.lookup("x").is_some());
        assert!(child.lookup("y").is_some());

        // Parent cannot see child's bindings
        assert!(parent.lookup("y").is_none());
    }

    #[test]
    fn test_type_result() {
        let ok = TypeResult::ok(MathType::Number);
        assert!(ok.is_ok());

        let err = TypeResult::error(
            MathType::Error("test".to_string()),
            TypeError::new(TypeErrorKind::TypeMismatch, "mismatch"),
        );
        assert!(!err.is_ok());
    }

    #[test]
    fn test_type_signature() {
        let sig = TypeSignature::new(
            "sin",
            MathType::Function {
                arity: Arity::Unary,
                domain: vec![MathType::Number],
                codomain: Box::new(MathType::Number),
            },
            SemanticCategory::Trigonometry,
        )
        .with_alias("sine");

        assert_eq!(sig.name, "sin");
        assert_eq!(sig.aliases, vec!["sine"]);
        assert_eq!(sig.category, SemanticCategory::Trigonometry);
    }
}