# Check and format a program

Find the mistakes in an alchemy program before it runs, and print it in canonical or layout form.

You are writing an alchemy program and want to know it is sound before running it, or to tidy its layout. `check`, `format`, and `canon` read a program without running it.

## Check a program

`dmog check` reads the program, resolves its names, checks its types, and builds its plan. It prints nothing when the program is sound:

```console
$ dmog check table.alc; echo $?
0
```

A program with a mistake fails with the place of the mistake. Write this as `broken.alc`, which uses a definition it never makes:

```alchemy
def export [input]
  table-from-json api-binding input
```

Then check it:

```console
$ dmog check broken.alc; echo $?
{"code":"DSL_TYPE_ERROR","message":"unknown_name: api-binding is not defined","row":2,"col":19,"output":"none"}
2
```

`row` and `col` point at `api-binding`. The word before the first colon of the message, `unknown_name`, is the finer code, which a script can branch on.

## Print the layout form

`dmog format` prints the program in layout form, where indentation stands for parentheses. Here it lays out a program written on one line:

```alchemy
(def export [input] (json input))
```

```console
$ dmog format packed.alc
def export [input]
  json input
```

## Print the canonical form

`dmog canon` prints the program with every parenthesis written out, one definition to a line:

```console
$ dmog canon table.alc
(def column-from-meta [source] (record (entry :label (get "title" source)) (entry :source (as-path (get "path" source)))))
(def api-binding (record (entry :columns (path "response" "metadata" "fields")) (entry :rows (path "response" "payload" "deep" "records" each-index)) (entry :column column-from-meta)))
(def export [input] (table-from-json api-binding input))
```

Two programs with the same canonical form are the same program.

## What to watch for

`format` and `canon` read the program and do not check it. A program whose text does not read, such as one with a line indented three spaces, fails with `DSL_PARSE_ERROR`. A program that reads but names an undefined function still prints, so run `check` as well. Neither command changes the file: redirect the output to save it. The forms themselves are defined in [the alchemy language reference](https://github.com/tabnas/alchemy/blob/main/docs/language.md#canonical-and-layout-form), on GitHub.
