Extension: Adding New Algorithmic Approaches
Purpose
This document provides the standard operating procedure for extending the ARES registry. It details the process of authoring new algorithmic intents, creating multi-language code templates, and registering them as first-class approaches. Following this specification ensures that new additions are correctly indexed by the compiler's Phase 3 (Semantic Analysis) and Phases 7/8 (Codegen/Emission) for all supported backends.
Prerequisites
- Registry Layer Identification: Determine the taxonomy layer (1=Core to 4=Advanced).
- Template Authoring: Requires finalized snippet implementations in C++, Python, and TypeScript.
- Unique Handle: A canonical name (lowercase, underscore-separated) for the approach.
Dependencies
src/registry/categories/: The directory where the new category file should be created.ApproachInfoInterface: The TypeScript contract defined insrc/types.ts.
Formal Definition
Let be a new approach to be added to the registry . The registration is an additive transformation: Where .
Operational Behavior
- Intent Selection: Identify a recurring algorithmic pattern (e.g.,
convex_hull) not already in . - Category Mapping: Add the approach to an existing category file (e.g.,
cp_advanced.ts) or create a new one. - Cross-Language Templating: Author the logic for each backend, ensuring that placeholder variables like
{{target}}and{{params.name}}are used correctly for context injection. - Alias Resolution: Add common synonyms (e.g.,
monotone_chainforconvex_hull) to thealiasesarray for a broader semantic reach. - Documentation Update: Author the corresponding Markdown specification in
8_registry_approaches/following the 17-point structural standard.
Implementation Mapping
ARES Registry Addition vs. Final Result
Step 1: Modifying (or Creating) the Category File
Action (src/registry/categories/cp_new.ts):
typescriptimport type { ApproachInfo } from "../../types.js"; export const CpNewApproaches: ApproachInfo[] = [ { name: "new_alg", description: "An example new algorithm.", layer: 4, complexity: "O(log N)", category: "new", cpp_template: "long long res = std::log2({{target}});", // v1.2: use long long py_template: "import math\nres = math.log2({{target}})", ts_template: "const res = Math.log2({{target}});", } ];
Step 2: Registering the Category
Action (src/registry/approach_registry.ts):
You must import your new category and register it in the initializeRegistry() method:
typescriptimport { CpNewApproaches } from "./categories/cp_new.js"; // ... inside initializeRegistry() this.registerAll(CpNewApproaches);
Step 3: Use in ARES
ARES Source:
aresread x as int use new_alg on x
Emitted C++:
cppint n_x; cin >> n_x; long long x; cin >> x; // vector promotion logic (if x was vector) // For simple int x: long long x; cin >> x; long long res = std::log2(x);
Mathematical Model
The total registry surface grows linearly with new additions: Where is the increase in the universal set of reachable intents.
Complexity
- Registration Complexity: to append to the category array.
- Resolution Complexity: average lookup in the registry hash map using the canonical name or alias.
Examples
Defining an Alias:
typescript{ name: "binary_search", aliases: ["lower_bound", "bsearch"], // templates... }
Adding Dependencies:
typescript{ name: "hld", dependencies: ["dfs_sz", "decompose"], // templates... }
Traces
Trace for ApproachRegistry.get("new_alg"):
- Retrieve name from dictionary.
- Follow aliases if applicable (recursive).
- Combine metadata and return the
ApproachInfoobject to the Emitter. - Emitter identifies
{{target}}and replaces it with the bound variable name.
Edge Cases
- Naming Collisions: If a new approach has the same name as an existing one, the first one loaded (alphabetically by category file) is preserved.
- Missing Backend Template: If a template is missing for a target backend, ARES will emit a "NOT_IMPLEMENTED" comment instead of crashing the compiler.
- Recursive Dependencies: If an approach depends on itself, it triggers a 50-step overflow preventer in Phase 6.
Failure Modes
- Syntax Errors in Templates: Will result in non-compilable output code in Phase 9, as ARES does not currently validate the static correctness of the registry snippets themselves.
- Duplicate Aliases: May cause non-deterministic resolution if the same alias scales to multiple canonical names.
Compatibility
- Tools: Any text editor capable of handling TypeScript.
- Registry Loading: Automatically handled by
ApproachRegistryduring Phase 3.
Testing
- Registry Load Test:
tests/registry.test.tsensures that after adding the new category to the indexer,registry.has("new_alg")returns true. - Template Substitution Check: Compiles a dummy script using the new approach and asserts that
{{target}}was correctly replaced.
Related Entities
8_registry_approaches/00_registry.md: Core architecture of the approach system.src/types.ts: The schema to follow.
Source Attribution
- Main Component:
src/registry/approach_registry.ts. - Design Pattern: Strategy Pattern / Template Method.