mpl/docs/ARCHITECTURE.md
developtheweb 468a8045c7 Sync all reference docs with the grammar
- precedence.csv regenerated to the actual 14-level chain (postfix and
  prefix levels, guarded alternatives, definition vs assignment)
- glyph-escapes.md regenerated from the lexer: 73 glyphs, exactly one
  ASCII escape each, verified code points; M1-deferred symbols listed
  separately; add \middot so every glyph has an escape
- math_prog_lang.md: M0/M1 symbol split, canonical handler arrow, one-role
  semicolon, real precedence table and brace-disambiguation rules; the ten
  example programs are now embedded verbatim and CI-parsed
- Whitepaper (md + tex + appendices): every mpl code block parses or is
  re-fenced as an explicitly-labelled M1+ design sketch; symbol tables
  replaced by a pointer to glyph-escapes.md; unbuilt tooling and unmeasured
  claims reworded as planned/envisioned
- docs/ARCHITECTURE.md: status preamble, real grammar excerpt, planned
  sections labelled as such
- DocumentationTest now also covers math_prog_lang.md and the whitepaper
- CHANGELOG and DECISIONS.md updated
2026-07-09 03:19:43 -04:00

285 lines
No EOL
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# MPL Architecture
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.
## Overview
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 │
└─────────────────────────────────────────────────────┘
```
## Core Components
### 1. Grammar Definition (`src/main/antlr4/MPL.g4`)
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](../glyph-escapes.md))
- **Effect Operators**: Exception handling (↯/↴), concurrency (‖), resources (⊕/⊖)
- **Precedence Rules**: a documented chain ([precedence.csv](../precedence.csv))
- **CI-clean**: compiles with zero ANTLR errors and warnings (`-Werror`)
Key grammar rules (excerpted from the real grammar):
```antlr
// 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 ;
```
### 2. Symbol System
MPL uses a three-tier symbol system:
1. **Unicode Symbols** (Primary)
- Direct mathematical notation: ∀, λ, ∈, ⟹
- Effect operators: ↯, ↴, ‖, ⇀, ↽
- Type symbols: , , , , , 𝔹
2. **ASCII Escapes** (Fallback)
- Every symbol has exactly one escape: `\forall`, `\lambda`, …
- Defined in `glyph-escapes.md` (kept in lockstep with the lexer)
3. **Multi-Modal Input** (Future)
- Voice recognition for mathematical terms
- Visual palette selection
- Handwriting recognition
### 3. Type System (planned, M1)
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
### 4. Effect System
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 |
### 5. Parser Implementation
The parser is built using ANTLR 4 with Java:
```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);
```
### 6. Runtime Architecture (planned)
The MPL runtime provides:
1. **Memory Management**
- Automatic reference counting
- Resource scope tracking (RAII)
- Parallel GC for concurrent code
2. **Concurrency Runtime**
- Green threads for ‖ operator
- Channel implementation for ⇀/↽
- STM for atomic sections ⌈⌉
3. **Standard Library**
- Mathematical functions
- I/O operations
- Collection manipulation
- Network primitives
## Compilation Pipeline
### Phase 1: Lexical Analysis
1. Unicode normalization (NFC)
2. Symbol recognition
3. ASCII escape expansion
4. Token stream generation
### Phase 2: Parsing
1. Grammar rule matching
2. Precedence resolution
3. AST construction
4. Syntax error recovery
### Phase 3: Semantic Analysis (planned)
1. Symbol table construction
2. Type inference
3. Effect analysis
4. Semantic error checking
### Phase 4: Optimization (planned)
1. Constant folding
2. Common subexpression elimination
3. Parallelism detection
4. Effect optimization
### Phase 5: Code Generation (planned)
Options for different targets:
- **JVM Bytecode**: For Java interoperability
- **LLVM IR**: For native compilation
- **JavaScript**: For web execution
- **Python**: For educational use
## Error Handling (planned)
Today the parser emits standard ANTLR diagnostics. The goal is comprehensive error messages with:
1. **Unicode-aware positioning**: Correct column numbers for multi-byte characters
2. **Multi-language messages**: Errors in user's native language
3. **Visual error display**: Highlighting problematic symbols
4. **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
```
## Performance Considerations (planned)
1. **Parser Performance**
- O(n) parsing for most constructs
- Memoization for complex expressions
- Incremental parsing support
2. **Unicode Handling**
- Zero-copy string processing
- Efficient symbol lookup tables
- Caching for escape conversions
3. **Parallel Execution**
- Work-stealing for ‖ operator
- Lock-free channel implementation
- NUMA-aware memory allocation
## Extension Points
The architecture supports extensions via:
1. **Grammar Extensions**: New operators in MPL.g4
2. **Type Extensions**: Custom type definitions
3. **Effect Extensions**: New computational effects
4. **Backend Extensions**: Additional compilation targets
## Security Considerations (planned)
1. **Input Validation**
- Unicode homograph detection
- Bidirectional text sanitization
- Resource limit enforcement
2. **Sandboxing**
- Capability-based security for I/O
- Memory limits for student code
- Time limits for execution
3. **Effect Isolation**
- Effect types prevent unauthorized operations
- Resource tracking prevents leaks
- Concurrency limits prevent DoS
## Future Architecture Goals
1. **Language Server Protocol (LSP)**
- Real-time error checking
- Symbol completion
- Refactoring support
2. **REPL Implementation**
- Interactive development
- Notebook integration
- Visualization support
3. **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.