# Chapter 15: Building Libraries and Packages

Part I ends here — you now know the full First language: functions, types, generics, interfaces, type classes, dependent types, and refinements. This chapter covers how to **package** that knowledge into libraries others can install, and how to **consume** libraries others have built. First's package manager is called **fir**.

---

## fir — the First package manager

`fir` ships alongside `firstc`. It manages projects, dependencies, and binary artefacts.

```bash
fir new mylib      # create a new library project
fir build          # compile
fir run            # build + run (for executables)
fir add <dep>      # add a dependency
fir install        # install all dependencies listed in fir.json
```

---

## Project layout

```
mylib/
  fir.json         # manifest
  first.lock       # pinned dependency versions (commit)
  src/
    lib.first      # library root module (or main.first for executables)
  lib/             # optional: local modules
  test/
    test_mylib.first
```

### fir.json

```json
{
  "name": "mylib",
  "version": "0.2.0",
  "type": "lib",
  "dependencies": {
    "first-mathlib": {
      "git": "https://github.com/you/first-mathlib",
      "tag": "v0.1.0"
    },
    "my-local-util": {
      "path": "../my-local-util"
    }
  }
}
```

For an executable project, set `"type": "bin"`. For a combined library + test runner, use `"type": "lib"`.

### first.lock

After running `fir install`, a `first.lock` file is generated that pins every dependency to an exact git commit:

```json
{
  "first-mathlib": {
    "git": "https://github.com/you/first-mathlib",
    "tag": "v0.1.0",
    "commit": "a3f9c82e..."
  }
}
```

Commit `first.lock` to source control. This ensures every developer and CI run gets exactly the same dependency versions.

---

## Writing a library

A library exports public API and keeps implementation details private.

```first
// src/lib.first
module mylib;

// Private helper — not exported
function factorial_helper(n: Int, acc: Int) -> Int {
    return if (n <= 1) { acc } else { factorial_helper(n - 1, n * acc) };
}

// Public API
export function factorial(n: Nat) -> Nat {
    return factorial_helper(n, 1);
}

export function choose(n: Nat, k: Nat) -> Nat {
    return factorial(n) / (factorial(k) * factorial(n - k));
}
```

Only `export`ed symbols are visible to importers.

---

## Depending on a library

```first
// your_project/src/main.first
import "mylib";

interaction main() {
    let result = mylib.factorial(10);
    println(toString(result));
}
```

### Local path dependency

During development, before publishing:

```json
{
  "dependencies": {
    "mylib": { "path": "../mylib" }
  }
}
```

### Git dependency

For published libraries, use a git URL with an optional tag or commit:

```json
{
  "dependencies": {
    "first-mathlib": {
      "git": "https://github.com/you/first-mathlib",
      "tag": "v0.3.1"
    }
  }
}
```

You can also pin to an exact commit:

```json
{
  "dependencies": {
    "first-mathlib": {
      "git": "https://github.com/you/first-mathlib",
      "commit": "a3f9c82e"
    }
  }
}
```

---

## Installing dependencies

```bash
fir install
```

`fir install` does the following:
1. Reads `fir.json` and resolves all dependencies
2. For git deps: clones into `~/.fir/cache/<name>/<commit>/` if not already cached
3. For path deps: resolves the local path
4. Writes / updates `first.lock` with pinned commits
5. Writes a `--lib-path` configuration so `firstc` can find each library

---

## Building a library

```bash
fir build
```

This compiles your library to LLVM bitcode. The build artefact goes in `build/`. Other projects that depend on your library use the source directly (not the bitcode) — First currently links at the source level for maximum inlining and cross-module optimisation.

---

## Multi-path projects

If you maintain multiple libraries in a monorepo, use `--lib-path` flags directly:

```bash
firstc main.first \
    --lib-path ./lib \
    --lib-path ~/.fir/cache/first-mathlib/a3f9c82e/lib \
    -o main
```

`fir` does this for you automatically based on `fir.json`.

---

## Publishing a library

Publishing means pushing to a public git repository that others can depend on.

1. Create a repo on GitHub / GitLab / Gitea.
2. Tag a release: `git tag v0.1.0 && git push --tags`
3. Tell users to add it to their `fir.json`:

```json
{
  "dependencies": {
    "mylib": {
      "git": "https://github.com/yourname/mylib",
      "tag": "v0.1.0"
    }
  }
}
```

There is no central registry (like crates.io or npm) yet — the git URL is the address.

---

## Writing proof libraries

Libraries can export theorems as well as functions. This lets you build reusable mathematics:

```first
// mymath/src/lib.first
module mymath;
import "Int";

// A proved theorem — users can apply it in their proofs
export theorem square_nonneg(n: Int) -> Eq(n * n >= 0, true) by {
    ring;
}

// A simp lemma — the simp tactic uses it automatically
@[simp]
export theorem add_zero_right(n: Int) -> Eq(n + 0, n) by {
    ring;
}
```

Users of `mymath` can call `square_nonneg` by name in `exact` and `apply`, or rely on `add_zero_right` being applied automatically by `simp`.

---

## first-mathlib

**first-mathlib** is the community mathematics library for First — analogous to Lean's Mathlib. It lives at `github.com/first-lang/first-mathlib` and contains:

- Hundreds of lemmas about `Nat`, `Int`, `Real`, `Rat`, `Complex`
- `Bool`, `Set<A>`, `List<A>` theorems
- Arithmetic, order theory, field axioms

Add it to your project with:

```json
{
  "dependencies": {
    "first-mathlib": {
      "git": "https://github.com/first-lang/first-mathlib",
      "tag": "v0.1.0"
    }
  }
}
```

Part II of this book covers how to use first-mathlib for theorem proving, and Chapter 23 explains how to contribute new lemmas.

---

## Summary

- `fir.json` — declare project name, type, and dependencies
- `first.lock` — pin exact commits; always commit this file
- `fir install` — clone/cache all deps, write lib-path config
- `fir build` — compile; `fir run` for executables
- `export` controls public API; unexported names are private
- Proof libraries export theorems alongside functions
- **first-mathlib** — the community maths library

This is the end of Part I. Part II begins with **Introduction to Theorem Proving** — using First as a proof assistant.
