This document describes the high-level architecture of the Mathematical Programming Language (MPL) implementation.
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:
- 70+ Mathematical Operators: From basic arithmetic to advanced calculus
- Effect Operators: Exception handling (↯/↴), concurrency (‖), resources (⊕/⊖)
- Precedence Rules: Mathematically consistent operator precedence
- Zero Conflicts: No shift/reduce or reduce/reduce conflicts
Key grammar features:
// Example: Function definition
functionDef : name=IDENTIFIER '≜' lambda ;
lambda : 'λ' params ':' expression ;
// Example: Mathematical operations
expression : expression '×' expression # Multiplication
| expression '÷' expression # Division
| '∑' '(' var '∈' range ':' expression ')' # Summation
;MPL uses a three-tier symbol system:
-
Unicode Symbols (Primary)
- Direct mathematical notation: ∀, ∃, λ, ∑, ∏
- Effect operators: ↯, ↴, ‖, ⇀, ↽
- Type symbols: ℕ, ℤ, ℝ, ℂ, 𝔹
-
ASCII Escapes (Fallback)
- Every symbol has an escape:
\forall,\lambda,\sum - Bidirectional conversion supported
- Defined in
glyph-escapes.md
- Every symbol has an 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
MPL provides 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
Example error:
Error at line 3, column 15:
∑(i ∈ [1,10] : i²²)
^^
Syntax error: Unexpected ² after ²
Did you mean: i² × ² or i⁴?
-
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.