This document describes the high-level architecture of the Mathematical Programming Language (MPL) implementation.
Status: only the lexing and parsing layers exist today (see the README's project status). Everything from semantic analysis down — and the runtime, error-message, performance and security sections below — describes the target architecture, not shipped code.
MPL is designed as a multi-layer system that transforms mathematical notation into executable code:
┌─────────────────────────────────────────────────────┐
│ User Input │
│ (Unicode Symbols / ASCII Escapes / Voice / Visual) │
└────────────────────┬───────────────────────────────┘
│
┌────────────────────▼───────────────────────────────┐
│ Input Processing Layer │
│ • Unicode Normalization (NFC) │
│ • Bidirectional Text Support │
│ • ASCII Escape Expansion │
└────────────────────┬───────────────────────────────┘
│
┌────────────────────▼───────────────────────────────┐
│ Lexical Analysis │
│ • ANTLR 4 Lexer (MPL.g4) │
│ • Token Stream Generation │
│ • Symbol Recognition │
└────────────────────┬───────────────────────────────┘
│
┌────────────────────▼───────────────────────────────┐
│ Syntactic Analysis │
│ • ANTLR 4 Parser (MPL.g4) │
│ • Precedence Resolution │
│ • AST Construction │
└────────────────────┬───────────────────────────────┘
│
┌────────────────────▼───────────────────────────────┐
│ Semantic Analysis │
│ • Type Inference │
│ • Effect Analysis │
│ • Symbol Resolution │
└────────────────────┬───────────────────────────────┘
│
┌────────────────────▼───────────────────────────────┐
│ Optimization │
│ • Constant Folding │
│ • Dead Code Elimination │
│ • Parallelism Detection │
└────────────────────┬───────────────────────────────┘
│
┌────────────────────▼───────────────────────────────┐
│ Code Generation │
│ • Target Platform Selection │
│ • Bytecode / Native Code Generation │
│ • Runtime Library Linking │
└─────────────────────────────────────────────────────┘
The heart of MPL is its ANTLR 4 grammar that defines:
- Mathematical Operators: every glyph with exactly one meaning and one ASCII escape (glyph-escapes.md)
- Effect Operators: Exception handling (↯/↴), concurrency (‖), resources (⊕/⊖)
- Precedence Rules: a documented chain (precedence.csv)
- CI-clean: compiles with zero ANTLR errors and warnings (
-Werror)
Key grammar rules (excerpted from the real grammar):
// Definition and assignment levels of the precedence chain
defExpr : assignExpr (DEFINITION defExpr)? ; // x ≜ e
assignExpr : condExpr (LEFTARROW assignExpr)? ; // x ← e
// Guarded alternatives: (condition ⟹ result) | fallback
condExpr : impliesExpr (BAR impliesExpr)* ;
// λx: body, λx,y: body, λx∈ℝ: body
lambda : LAMBDA_VAR pattern (IN condExpr)? COLON expr ;MPL uses a three-tier symbol system:
-
Unicode Symbols (Primary)
- Direct mathematical notation: ∀, λ, ∈, ⟹
- Effect operators: ↯, ↴, ‖, ⇀, ↽
- Type symbols: ℕ, ℤ, ℚ, ℝ, ℂ, 𝔹
-
ASCII Escapes (Fallback)
- Every symbol has exactly one escape:
\forall,\lambda, … - Defined in
glyph-escapes.md(kept in lockstep with the lexer)
- Every symbol has exactly one escape:
-
Multi-Modal Input (Future)
- Voice recognition for mathematical terms
- Visual palette selection
- Handwriting recognition
MPL features a hybrid type system:
Types := BaseType | FunctionType | CollectionType | EffectType
BaseType := ℕ | ℤ | ℚ | ℝ | ℂ | 𝔹 | String | Unit
FunctionType := Type → Type
CollectionType := [Type] | {Type} | (Type₁, Type₂, ...)
EffectType := Type ! {Exception, IO, Concurrent, Resource}
Type inference follows Hindley-Milner with extensions for:
- Numeric type promotion
- Effect tracking
- Parallel composition
MPL tracks computational effects at the type level:
| Effect | Symbol | Purpose |
|---|---|---|
| Exception | ↯/↴ | Throwing and catching errors |
| Concurrency | ‖ | Parallel execution |
| Channels | ⇀/↽ | Message passing |
| Resources | ⊕/⊖ | Acquisition/release |
| Atomicity | ⌈⌉ | Atomic sections |
| Metaprogramming | ⌜⌝/⌞⌟ | Code quotation/evaluation |
The parser is built using ANTLR 4 with Java:
// Parser initialization
MPLLexer lexer = new MPLLexer(CharStreams.fromString(input));
MPLParser parser = new MPLParser(new CommonTokenStream(lexer));
// Parse with error handling
parser.addErrorListener(new MPLErrorListener());
ParseTree tree = parser.program();
// Visit AST
MPLVisitor visitor = new MPLASTBuilder();
AST ast = visitor.visit(tree);The MPL runtime provides:
-
Memory Management
- Automatic reference counting
- Resource scope tracking (RAII)
- Parallel GC for concurrent code
-
Concurrency Runtime
- Green threads for ‖ operator
- Channel implementation for ⇀/↽
- STM for atomic sections ⌈⌉
-
Standard Library
- Mathematical functions
- I/O operations
- Collection manipulation
- Network primitives
- Unicode normalization (NFC)
- Symbol recognition
- ASCII escape expansion
- Token stream generation
- Grammar rule matching
- Precedence resolution
- AST construction
- Syntax error recovery
- Symbol table construction
- Type inference
- Effect analysis
- Semantic error checking
- Constant folding
- Common subexpression elimination
- Parallelism detection
- Effect optimization
Options for different targets:
- JVM Bytecode: For Java interoperability
- LLVM IR: For native compilation
- JavaScript: For web execution
- Python: For educational use
Today the parser emits standard ANTLR diagnostics. The goal is comprehensive error messages with:
- Unicode-aware positioning: Correct column numbers for multi-byte characters
- Multi-language messages: Errors in user's native language
- Visual error display: Highlighting problematic symbols
- Suggestion system: Common fixes for typical mistakes
Envisioned example error:
Error at line 3, column 12:
(x > 0 ⟹ x | -x
^
Syntax error: missing ')' before '|'
Guarded alternatives read: (condition ⟹ result) | fallback
-
Parser Performance
- O(n) parsing for most constructs
- Memoization for complex expressions
- Incremental parsing support
-
Unicode Handling
- Zero-copy string processing
- Efficient symbol lookup tables
- Caching for escape conversions
-
Parallel Execution
- Work-stealing for ‖ operator
- Lock-free channel implementation
- NUMA-aware memory allocation
The architecture supports extensions via:
- Grammar Extensions: New operators in MPL.g4
- Type Extensions: Custom type definitions
- Effect Extensions: New computational effects
- Backend Extensions: Additional compilation targets
-
Input Validation
- Unicode homograph detection
- Bidirectional text sanitization
- Resource limit enforcement
-
Sandboxing
- Capability-based security for I/O
- Memory limits for student code
- Time limits for execution
-
Effect Isolation
- Effect types prevent unauthorized operations
- Resource tracking prevents leaks
- Concurrency limits prevent DoS
-
Language Server Protocol (LSP)
- Real-time error checking
- Symbol completion
- Refactoring support
-
REPL Implementation
- Interactive development
- Notebook integration
- Visualization support
-
Distributed Execution
- Cluster support for ‖
- Distributed channels
- Fault tolerance
This architecture enables MPL to achieve its goal of cognitive universality while maintaining performance and safety suitable for educational environments.