# Why Sakura Lang

URL: /docs/why-sakura-lang
Markdown: /docs/why-sakura-lang.md

> Why agent-written code is hard to review, how putting the facts in the signature helps, and how Sakura Lang compares with languages you know.

AI agents write more and more code. Writing code is no longer the hard part. Trusting it is.

## The problem [#the-problem]

Reviewing every line does not scale when an agent writes thousands of lines an hour. Reviewers
skim. Skimming only works if the important facts are easy to find, and in most languages they
are not:

- **Exceptions.** Any call can throw, and the signature does not say what.
- **Null.** A value that may be missing looks the same as one that is always there.
- **Unlimited access.** Any function in any dependency can open a socket, read an environment
  variable or write a file. Supply-chain attacks rely on this.
- **Hidden side effects.** A function called `format_price` might call a currency API.
  Nothing in its signature tells you.

Agents make these problems worse. To make a test pass, an agent might add a network call to a
helper, swallow an exception, or paste a request parameter into a SQL string. Each change looks
small and harmless in a diff.

## The answer: the signature is the contract [#the-answer-the-signature-is-the-contract]

In Sakura Lang, a function's signature lists what it takes, what it returns, everything it may
do, every error it may fail with, and what it promises about its result:

This is a signature only, not a whole program.

```sakura fragment
fn signup(email: Email) -> User ! {Users, Clock, Random, Fail<SignupError>}
  ensures result.email == email
```

- `! {Users, Clock, Random, …}` lists the function's **effects**: what it does besides
  returning a value.
- `Fail<SignupError>` lists the errors it can fail with.
- `ensures` is a **contract**: a promise about the result, checked every time the function
  returns.

If the body does something the signature does not list, the code does not compile. So you
review the signatures, which are short and rarely change. The compiler checks the bodies, which
are long and change all the time.

When an agent's change needs a new effect, the signature has to change, and a changed signature
is exactly what a reviewer looks for. The compiler never applies a fix by itself if that fix
would widen access or weaken a contract.

## Compared with languages you know [#compared-with-languages-you-know]

Each of these languages is excellent at what it was built for. None was built for this
workflow. The [prior art](/docs/prior-art) page goes through each in detail.

| Language | What it does well | What Sakura Lang changes |
| --- | --- | --- |
| **TypeScript** | Huge ecosystem; agents write it well. | Exceptions are untyped, any function can do any IO, and `any` is one keystroke away. |
| **Python** | Readable; great for scripts and data. | Types are optional, and errors and IO are invisible in signatures. |
| **Rust** | Fast and memory safe, with a strict, helpful compiler. | No lifetimes or borrow checker to satisfy, and IO is tracked in signatures. |
| **Go** | Small, consistent, one toolchain, one format. | Errors cannot be ignored, there is no nil, and code reaches the network only if its signature says so. |
| **Effect-TS** | Typed errors and requirements in TypeScript. | The same model, built into the language, so there is no way around it. |

Sakura Lang also borrows from research languages: effect rows from Koka, plain handlers from
Unison, and platform-owned access from Roc. It adds contracts, permission checks and
machine-readable tooling for agents.

## Design principles [#design-principles]

**The signature is the contract.** Every top-level function has a full signature. Only local
variables have inferred types.

**One way to do each thing.** One function syntax, one way to handle errors (`try`/`catch`),
one way to install a handler (`with`), one way to say "maybe missing" (`Option`), one
formatter. No operator overloading, implicit conversions, macros or inheritance.

**Leaving something out has one meaning.** A function without `!` is pure. A missing `match`
case, a missing effect or an unhandled error is a compile error, never a silent default.

**Errors are an API.** Every compiler error has a stable code, exact positions and suggested
fixes that say how big a change they make. See [Diagnostics](/docs/agents/diagnostics).

**The compiler owns memory.** You never write lifetimes or reference counts. In return, values
cannot be shared and changed at the same time, and files and connections close at the end of a
scope.

**Honest guarantees.** The compiler never says "verified" for code that only compiled. Every
claim has one of four levels:

| Level | Meaning | Where it comes from |
| --- | --- | --- |
| `proved` | Holds on every run, under the stated assumptions. | The type checker, and contracts that `sakura verify` proved. |
| `runtime_checked` | A violation is caught when it happens and stops the program. | Contracts, refinement types and checked arithmetic. |
| `tested` | Held on the inputs the tests tried. | `sakura test`. |
| `trusted` | Assumed, not checked. | Foreign calls (`@external`), host handlers, `@trust` sites, the toolchain itself. |

The type checker proves that every effect has a handler, every error is handled or declared,
every `match` covers every case, no value is null, no list index goes unchecked, and a secret
is never printed. It does not prove that your business logic is right, that a loop ends, or
that your specification says what you meant.

## What the language leaves out for now [#what-the-language-leaves-out-for-now]

- Handlers that resume more than once, or first-class continuations.
- `async`/`await`. Any function may wait; the handler decides.
- User-defined traits or interfaces. A fixed set (`Eq`, `Ord`, `Hash`, `Show`, `Schema`) is
  derived for you.
- Advanced type features: higher-rank types, GADTs, type-level computation.
- Information-flow control. `Secret<T>` stops accidental leaks, not deliberate ones.
- Automatic proofs that a loop ends.
- Durable workflows, a distributed runtime or a UI engine. These are planned as later
  packages.
- A tracing garbage collector on WebAssembly or native.

## Next [#next]

Try the [Quickstart](/docs/quickstart), or start the [Language Tour](/docs/tour/basics).

Details for contributors: [design §1](https://github.com/icantcodefyi/sakuralang/blob/main/docs/design.md)
states these principles in full.
