01_quickstart

quickstart

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++, and python are in your system PATH as per 4_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 N=105N=10^5, ARES will route this to C++ for performance.

powershell
ares 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:

  1. Lexing: Identifies keywords read, use, as.
  2. Parsing: Builds a program AST with two ReadStmt and two UseStmt.
  3. Semantic Analysis: Binds nums as a vector<int>.
  4. Routing: Complexity Profiler sees vector and O(logN)O(\log N) \to Target: C++.
  5. Emission: C++ emitter injects the logic and I/O boilerplate.
  6. Execution: Daemon calls g++ and runs the binary.

Edge Cases

  • Handling No Target: If 42 is not in the array, the binary_search intent in Python might return None or -1, while in C++ it handles it as an iterator check.
  • Vector Sizing: Remember that read nums as vector<int> expects an integer NN followed by NN elements in the input stream.

Testing

  • Quickstart Verification: You can verify your installation by running this tutorial script with the input:
    text
    5 10 20 12 42 5
    Expected Output (if searching 42): The modified prefix sum array.

Related Entities

  • 8_registry_approaches/01_cp_core.md: Deep dive into the prefix_sum intent.
  • 4_setup_and_usage/01_installation.md: If the compiler isn't found.
ARES