# Chapter 1: Getting Started

## Introduction to First

First is a **functional-first** programming language with strong, static typing and a clear split between pure functions and side-effecting **interactions**. You write interactions when you need I/O, mutable state, or other effects; the rest of your code stays pure. The compiler targets native code via LLVM and comes with a small runtime and standard library. The **fir** tool (included with the compiler) lets you create projects, build, and run them with simple commands.

One honest thing to say up-front: **functional programming type errors can be cryptic.** A message like `type mismatch: got Vec<Int, 2>, expected Vec<Int, 3>` is precise, but it doesn't tell you *which field* set the expectation or *what you need to change*. This gets harder as you move into dependent types, Sigma types, and refinement predicates. First ships a first-of-its-kind solution:

> **`firstc --ai-errors`** — pass this flag and the compiler calls an LLM (Claude, GPT-4o, DeepSeek, or Kimi — whichever key you have) to translate every type error into a plain-English sentence that explains what went wrong and what to change.

No other compiled language ships this natively in its CLI. It is opt-in, provider-agnostic, and falls back gracefully when no API key is present. Full details are in the [AI error explanations](#ai-powered-error-explanations) section below.

This chapter gets you set up: installing First on macOS or Linux, creating a “Hello, World” project, and using **print** and **println** to produce output.

---

## Installing First

First is available as a pre-built binary for **macOS** and **Linux** (x86\_64 and arm64/aarch64).

### macOS

Install via [Homebrew](https://brew.sh):

```bash
brew tap akkeshavan/first
brew install first-compiler
```

Verify:

```bash
firstc --help
fir --help
```

### Linux

**Quick install** — paste this into your terminal:

```bash
curl -sSfL https://raw.githubusercontent.com/akkeshavan/first/main/scripts/install-linux.sh | bash
```

This script auto-detects your architecture (x86\_64 or aarch64), downloads the correct tarball from [GitHub Releases](https://github.com/akkeshavan/first/releases), installs `firstc` and the runtime to `/usr/local` (or `~/.local` if you don't have sudo), and adds the bin directory to your PATH.

Restart your terminal, then verify:

```bash
firstc --help
fir --help
```

**Manual install** — if you prefer to inspect the tarball yourself:

```bash
# Replace x86_64 with aarch64 for ARM
curl -LO https://github.com/akkeshavan/first/releases/download/v0.1.0/first-compiler-0.1.0-Linux-x86_64.tar.gz
tar -xzf first-compiler-0.1.0-Linux-x86_64.tar.gz
sudo cp -R first-compiler-0.1.0-Linux-x86_64/bin/. /usr/local/bin/
sudo cp -R first-compiler-0.1.0-Linux-x86_64/lib/. /usr/local/lib/
```

**Debian / Ubuntu — `.deb` package:**

```bash
# Replace x86_64 with aarch64 for ARM
curl -LO https://github.com/akkeshavan/first/releases/download/v0.1.0/first-compiler-0.1.0-Linux-x86_64.deb
sudo dpkg -i first-compiler-0.1.0-Linux-x86_64.deb
```

> Need a specific version? Set `FIRST_VERSION` before running the install script:
> ```bash
> FIRST_VERSION=0.1.0 curl -sSfL https://raw.githubusercontent.com/akkeshavan/first/main/scripts/install-linux.sh | bash
> ```

### Windows

Native Windows binaries are not yet available. Install [WSL 2](https://learn.microsoft.com/en-us/windows/wsl/install) (Windows Subsystem for Linux), choose Ubuntu 22.04 or later from the Microsoft Store, then follow the Linux instructions above inside the WSL terminal.

### Updating

Re-run the same install command to update to a newer release. On macOS: `brew upgrade first-compiler`.

---

## Creating your first Hello project

After installing, create a new project anywhere on your machine.

1. **Create a directory and initialize a project**

   ```bash
   mkdir hello-first
   cd hello-first
   ```

2. **Create a new First project named `hello`**

   ```bash
   fir new hello
   ```

   This creates **examples/chapter-01-hello/** with:

   - **fir.json** – project name and entry point
   - **src/main.first** – main source file
   - **.gitignore** – ignores `build/` and similar

3. **Build the project**

   ```bash
   cd hello
   fir build
   ```

   This compiles **src/main.first** and produces an executable in **hello/build/**.

4. **Run the program**

   ```bash
   fir run
   ```

   **fir run** builds if needed, then runs the executable. You should see:

   ```
   Hello from First!
   ```

The source for this program is **examples/chapter-01-hello/src/main.first**. You can edit it and run `fir run` again to see changes.

---

## print and println

First uses two main functions for writing text to the terminal:

- **print(** *string* **)** – Writes the string to stdout with **no** newline at the end.
- **println(** *string* **)** – Writes the string to stdout and then a newline.

Both take a single string. String literals use double quotes and support escape sequences such as `\n` (newline) and `\t` (tab).

**Example: print**

```first
interaction main() -> Unit {
    print("Hello, ");
    print("World!");
}
```

Output: `Hello, World!` (no newline at the end).

**Example: println**

```first
interaction main() -> Unit {
    println("Hello, World!");
}
```

Output:

```
Hello, World!
```

**Example: newlines and tabs**

```first
interaction main() -> Unit {
    println("Line 1");
    print("Line 2\n");
    print("Col1\tCol2\n");
}
```

Output:

```
Line 1
Line 2
Col1    Col2
```

A full runnable example is in **examples/chapter-01-print-and-println**. From the repo root:

```bash
cd examples/chapter-01-print-and-println
fir run
```

You can also try the snippets above in **examples/chapter-01-hello** by editing **src/main.first** and running `fir run`.

---

## AI-powered error explanations

Functional programming type errors are intentionally precise — the compiler tells you exactly which types don’t match. But precision is not the same as clarity. As your code grows to use generics, dependent types, and refinement predicates, the raw error message increasingly requires you to already understand the type system to decode it.

First addresses this directly with `--ai-errors`. When the flag is set, the compiler calls an LLM after type-checking, sends the full error context (message, source line, file location), and appends a plain-English explanation as a `help:` note under each error.

### A concrete example

Suppose you write a dependent record where the Vec length doesn’t match the declared field:

```first
type Frame = {
    length: Int,
    payload: Vec<Int, length>
};

interaction main() -> Unit {
    let f: Frame = { length: 3, payload: [1, 2] };
}
```

Without `--ai-errors`, the compiler says:

```
error [main.first]:7:20: field ‘payload’: Vec length mismatch — got 2, expected 3
     7 |     let f: Frame = { length: 3, payload: [1, 2] };
      |                    ^
```

With `--ai-errors`:

```
error [main.first]:7:20: field ‘payload’: Vec length mismatch — got 2, expected 3
     7 |     let f: Frame = { length: 3, payload: [1, 2] };
      |                    ^
  help: [anthropic] The payload field must have exactly 3 elements because length is 3; add a third element to the array.
```

The raw error is still there — the AI explanation is additive, never a replacement. If the API call fails for any reason, compilation continues normally.

### Supported providers

First auto-detects your API key and picks a provider automatically. Priority order:

| Provider | Env var | Default model |
|----------|---------|---------------|
| Anthropic (Claude) | `ANTHROPIC_API_KEY` | `claude-haiku-4-5-20251001` |
| OpenAI | `OPENAI_API_KEY` | `gpt-4o-mini` |
| DeepSeek | `DEEPSEEK_API_KEY` | `deepseek-chat` |
| Kimi (Moonshot) | `MOONSHOT_API_KEY` | `moonshot-v1-8k` |

You only need **one** key. The compiler uses whichever it finds first (in the order above).

### Usage

```bash
# auto-detect provider from env
firstc --ai-errors -o out myfile.first

# explicit provider
firstc --ai-provider openai -o out myfile.first

# explicit provider + custom model
firstc --ai-provider deepseek --ai-model deepseek-reasoner -o out myfile.first
```

### Fallback behaviour

- **No key set** — prints a one-line note telling you which env vars to set, then shows normal errors.
- **Network failure / API error** — silently falls back to normal errors; compilation result is unaffected.
- **libcurl not available at build time** — `--ai-errors` is accepted but does nothing; the compiler was built without HTTP support.

This makes `--ai-errors` safe to leave on in a team environment: developers without an API key see standard output, those with a key see enhanced output.

---

## What’s next

- Change **examples/chapter-01-hello/src/main.first** to use `println` instead of `print` and add more `print`/`println` calls.
- Explore other examples under **examples/** (each subdirectory is a fir project).
- Continue with the next chapters for types, functions, and more of the language.
