[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"YKpUPpnZkG":3},"# Thales\n\nA TypeScript-to-Lean 4 compiler. Thales type-checks a safe subset of\nTypeScript and emits a Lean 4 sidecar alongside the input `.ts` file,\nturning your TypeScript module into a Lean module you can reason\nabout.\n\n**Thales sits on top of strict TypeScript.** Every program Thales\naccepts is also accepted by `tsc --strict` — we don't invent new\nsyntax or reinterpret existing type rules, so your editor tooling,\nIDE integrations, and CI linters keep working. What Thales _does_ is\nfurther restrict TS (rejecting mutation, classes, async, untyped\nescapes, etc.) and enrich selected patterns — nullable unions,\n`@throws`, `@total` — with Lean-visible semantics that TypeScript's\nown type system cannot express. The result: a narrow, disciplined\nsubset of TS whose emitted Lean you can actually reason about.\n\n## A quick taste\n\n```typescript\ntype User = { name: string; age: number };\n\n/** @throws RangeError when age is negative */\nfunction makeUser(name: string, age: number): User {\n  if (age \u003C 0) throw new RangeError('age must be non-negative');\n  return { name, age };\n}\n\ntype NameList = { kind: 'nil' } | { kind: 'cons'; head: User; tail: NameList };\n\n/** @total */\nfunction firstName(xs: NameList): string | null {\n  switch (xs.kind) {\n    case 'nil':\n      return null;\n    case 'cons':\n      return xs.head.name;\n  }\n}\n```\n\nThales type-checks this against a strict subset of TypeScript and\nemits Lean 4 where:\n\n- `makeUser` becomes `def makeUser : String → Int → Except RangeError User`\n  (failure mode visible in the signature; callers must `try`/`catch` or\n  propagate via `@throws`).\n- `firstName` becomes `def firstName : NameList → Option String`\n  (Lean verifies termination from the structural recursion; nullability\n  tracked in the type).\n\n`@throws` and `@total` are mutually exclusive: a `@total` function makes\nthe stronger claim that no failure escapes, so it cannot also declare\none. See [`docs/subset.md`](docs/subset.md#total-and-termination).\n\n## Install\n\n```bash\ngit clone https://github.com/jessealama/thales.git\ncd thales\nlake build thales\n```\n\n## Usage\n\n```bash\n.lake/build/bin/thales foo.ts               # type-check + emit Foo.lean\n.lake/build/bin/thales --no-emit foo.ts     # type-check only\n.lake/build/bin/thales -o \u003Cdir> foo.ts      # emit into \u003Cdir>/Foo.lean\n.lake/build/bin/thales --overwrite foo.ts   # emit, replacing existing Foo.lean\n```\n\n## Headline features\n\n- **`Option` for nullable types.** `T | null` and `T | undefined` map\n  to `Option T`. Narrowing on `=== null` / `!== null` works.\n- **`@throws` for typed exceptions.** Functions that can throw declare\n  their error types in JSDoc; the emitted Lean returns `Except E T`.\n  `try`/`catch` desugars to a `match` on the `Except`. Catches use the\n  standard TS form (`catch (e)` — untyped, as `tsc --strict` requires);\n  Thales infers the caught type from the `try` body.\n- **`@total` for \"always returns a value\" guarantees.** Default emission\n  is `partial def` — non-total recursion is fine. `@total` is a stronger\n  source-level claim: the function terminates (Lean's termination checker\n  must accept it) _and_ no failure escapes (no uncaught `throw`, no\n  uncaught call into a `@throws` callee). It is mutually exclusive with\n  `@throws`; failures of either kind surface as clean diagnostics\n  (TH0066/TH0067/TH0070).\n- **Built-in bounded number types via `@thales/prelude`.**\n  `Integer`, `Natural`, `Byte`, and `Bit` are branded aliases of\n  `number` in TypeScript and Lean Subtypes of `Float` in the emitted\n  Lean. The chain is `Bit ⊆ Byte ⊆ Natural ⊆ Integer ⊆ number`.\n  Numeric literals are checked at compile time (out-of-range →\n  TH0080); assigning a plain `number` without a guard (`isInteger`,\n  `isNatural`, …) or throwing constructor (`asInteger`, `asNatural`,\n  …) is rejected with TH0081. Arithmetic operators always widen to\n  `number`; narrow the result with a guard or constructor if you\n  need the refinement type back. The bounded number types reflect\n  into Lean's `Int`/`Nat` so downstream proofs can reason about\n  safe-integer arithmetic — see\n  [`docs/beyond-typescript.md`](docs/beyond-typescript.md) for the\n  picture of what Thales gives you that TypeScript alone cannot.\n\n## What's in the subset\n\nThales accepts a proper subset of what `tsc --strict` accepts. See\n[`docs/subset.md`](docs/subset.md) for the full contract and\n[`docs/errors.md`](docs/errors.md) for every `TH####` diagnostic code.\nCurrently out: classes, mutation, async, `any`/`unknown`/intersection\ntypes. See [`docs/future.md`](docs/future.md) for the roadmap.\n\n## Generated Lean modules\n\nEvery emitted file opens with `import Thales.TS.Runtime`. The runtime\nis a small Lean module (`Option'`, `Result`, error records,\n`consoleLog` with JS-compatible number printing, array combinators,\n`parseFloat`/`isNaN`) sized to the accepted subset and designed so\nthat the Lean path's stdout matches the VM path byte-for-byte. See\n[`docs/runtime.md`](docs/runtime.md) for the full surface.\n\nThe runtime's bounded-number-type machinery postulates twelve\nIEEE-754 axioms (covering Float ↔ Int boundary behavior, `Float.abs`,\nand `Integer` reflection) that Lean's stdlib does not provide.\nEmitted code that reasons about safe-integer arithmetic ultimately\nrests on these. See [`docs/axioms.md`](docs/axioms.md) for the full\nlist and rationale.\n\n## Testing\n\n```bash\nnpm run conformance:self-test                # harness regression\nnpm run conformance                          # full conformance corpus\nlake build ThalesTest                        # Lean unit tests\n```\n\n## License\n\n[MIT](LICENSE)\n",1780846760339]