Quickstart Tutorial: Your First ARES Program
Purpose
This tutorial provides a hands-on introduction to the ARES language. We will build a simple "Competitive Programming" entry point that reads an array of integers, calculates the prefix sums, and finds a target value using binary search. This example demonstrates ARES's core philosophy: translating high-level algorithmic intent into optimized C++ or Python code.
Prerequisites
- Installed ARES: Ensure
node,g++, andpythonare in your system PATH as per4_setup_and_usage/01_installation.md.
Tutorial
1. The ARES Script
Create a new file named sum_search.ares:
ares// 1. Input Section // Reads an integer N, then N elements into nums read nums as vector<int> // 2. High-Level Algorithmic Intent use prefix_sum on nums use binary_search on nums with target=42 // 3. Output Section if result { print "Found target 42!" } else { print "Target 42 not found." }
2. Compilation and Execution
Run the script using ARES. By default, with , ARES will route this to C++ for performance.
powershellares sum_search.ares --exec
3. Understanding the Emission
ARES identifies the prefix_sum and binary_search intents. It doesn't just call a library; it emits the optimized logic directly into the target source.
What ARES generated (C++ snippet): // From prefix_sum template std::vector<long long> pref(nums.size() + 1, 0); for(int i = 0; i < (int)nums.size(); ++i) pref[i+1] = pref[i] + nums[i];
// From binary_search template bool result = std::binary_search(nums.begin(), nums.end(), 42);
> [!NOTE]
> Notice that `prefix_sum` creates a new auxiliary array `pref`, while `binary_search` stores its boolean outcome in the variable `result` (default outcome name).
## Implementation Mapping
### ARES Workflow vs. Developer Experience
| Step | ARES Action | Developer Result |
| :--- | :--- | :--- |
| **Logic** | `use prefix_sum` | $O(N)$ source code injected. |
| **Routing** | Automated Profiling| C++ is chosen for $O(N)$ vector tasks. |
| **Build** | UMR Config | `CMakeLists.txt` created automatically. |
| **Run** | Orchestrator | `run.exe` spawned, input piped. |
## Examples
**Running with Python (Force Target)**:
If you want to use Python's built-in visualization alongside the core logic:
```bash
ares sum_search.ares python --exec
Traces
Trace for ares sum_search.ares --exec:
- Lexing: Identifies keywords
read,use,as. - Parsing: Builds a program AST with two
ReadStmtand twoUseStmt. - Semantic Analysis: Binds
numsas avector<int>. - Routing: Complexity Profiler sees
vectorand Target: C++. - Emission: C++ emitter injects the logic and I/O boilerplate.
- Execution: Daemon calls
g++and runs the binary.
Edge Cases
- Handling No Target: If
42is not in the array, thebinary_searchintent in Python might returnNoneor-1, while in C++ it handles it as an iterator check. - Vector Sizing: Remember that
read nums as vector<int>expects an integer followed by elements in the input stream.
Testing
- Quickstart Verification: You can verify your installation by running this tutorial script with the input:
Expected Output (if searching 42): The modified prefix sum array.text5 10 20 12 42 5
Related Entities
8_registry_approaches/01_cp_core.md: Deep dive into theprefix_sumintent.4_setup_and_usage/01_installation.md: If the compiler isn't found.