Approach Registry Architecture
Purpose
The ARES Approach Registry is the "Omni-Database" of the compiler, containing the formal definitions, cross-language templates, and complexity metadata for every high-level algorithm supported by the DSL. It functions as a semantic bridge that allows the developer to specify what to do (e.g., use dijkstra) while the registry handles the how for each target language. This abstraction enables the ARES compiler to remain "Universal" and "Meta," as adding support for a new algorithm only requires adding a entry to the registry.
Prerequisites
- Approach Metadata: Each algorithm must be defined with a name, description, layer, and complexity.
- Target Templates: Emitters require target-specific strings (C++, Python, TS) for code inlining.
- Dependency List: UMR requires a list of external libraries required by the approach.
Dependencies
src/registry/approach_registry.ts: The central management class.- Modular Categories:
cp_core.tscp_advanced.tsswe_systems.tsdata_science.tscp_specialized.ts
src/types.ts: Defines theApproachInfostructure.
Formal Definition
Let be the Approach Registry. is a set of ApproachInfo objects .
An ApproachInfo is a tuple:
Where:
- : Canonical name (and a set of aliases).
- : Human-readable description.
- : Registry layer (1: Primitive, 2: Utility, 3: Full Algorithm).
- : Big-O Complexity (used for Phase 5 routing).
- : Required external modules.
- : Language-specific emission templates.
Operational Behavior
- Lazy Categorization: Approaches are grouped into functional categories (Core, Advanced, SWE, DS) to ensure modularity and ease of maintenance.
- Alias Resolution: The registry supports multiple names for the same algorithm (e.g.,
prefix_summight be aliased ascumulative_sum), allowing for flexible developer input. - Template Substitution: Templates use double-curly braces (e.g.,
{{target}},{{params.x}}) for dynamic injection of AST variables and parameters during emission. - Canonical Search: The
search()method allows the Semantic Analyzer to provide "Did you mean?" suggestions when an unknown approach is used. - Multi-Language Parity: The registry enforces that every approach has a template for each supported backend, though some may be no-ops or warnings if the language is unsuitable for that algorithm.
Implementation Mapping
Implementation details in src/registry/approach_registry.ts:
- Internal Map:
approaches(canonical names) andaliases(alternate names) (Lines 12-13). - Registry Initialization:
initializeRegistry()(Line 19) loads all modular category files. - Base Primitives: Lines 28-61 define built-in primitives like
sortandbinary_searchdirectly in the registry. - Search API:
search(query)(Line 115) performs fuzzy matching against name, description, and aliases. - Categorical Retrieval:
getByCategory(category)(Line 103) filters the registry for specific sub-domains.
Mathematical Model
The search function for a query string is defined as: Where represents the substantive substring inclusion relation.
Complexity
- Initialization Time: where is the total number of approaches (typically ms for hundreds of entries).
- Lookup Time: constant-time access via the Internal Map.
- Search Time: linear scan of the registry.
Examples
Approach Definition:
typescript{ name: "prefix_sum", aliases: ["pre_sum", "cumulative_sum"], complexity: "O(N)", cpp_template: "// C++ implementation...", py_template: "import itertools\n{{target}} = list(itertools.accumulate({{target}}))" }
Traces
Trace for use binary_search on data with target=x:
- Generator calls
registry.get("binary_search"). - Returns the
ApproachInfofor Binary Search. - Emitter finds
cpp_template. - Replaces
{{target}}withdata. - Replaces
{{params.target}}withx. - Emits the final C++ code to the source buffer.
Edge Cases
- Case Insensitivity: All lookups and search queries are neutralized to lowercase to prevent stylistic mismatch errors.
- Ambiguous Aliases: If two approaches share the same alias, the registry priority is determined by the order of registration (Modular categories after Core categories).
- Missing Templates: If an emitter requests a template for a language that wasn't provided, it falls back to a descriptive comment in the generated code.
Failure Modes
- Duplicate Names: Registering an approach with a name that already exists over-writes the previous entry.
- Invalid Placeholders: Using a placeholder in a template that isn't provided by the parser (e.g.,
{{params.y}}when onlyxwas passed) results in the literal placeholder remaining in the output.
Compatibility
- Extensibility: New approaches can be added by simply creating a new
.tscategory file and importing it into the main registry.
Testing
- Parity Verification:
tests/registry.test.tsiterates through the entire registry and ensures that every entry has a non-empty template for C++, Python, and TypeScript. - Complexity Correctness: Verifies that complexity strings follow the standard Big-O notation recognized by the Profiler.
Related Entities
src/semantics/analyzer.ts: The primary consumer of registry search functionality.6_compiler_and_runtime/04_cpp_emitter.md: Uses these templates for emission.8_registry_approaches/01_cp_core.md: Deep dive into the core CP algorithms.
Source Attribution
- Primary Source:
src/registry/approach_registry.ts. - Patterns: Inspired by the "Strategy Pattern" in software architecture.