# A system file

# A system file

One `.phl` file holds one system: its declarations, its sequences, how it
behaves without hardware, and its unit tests. `system("name")` is the first
line.

## The cycle

The system is judged once a **cycle**: every alert, condition and calculated
expression, and the next line of a running sequence. It is 50 Hz unless the
file says otherwise:

```python
system("proof_test", cycle=100 * Hz)
```

`cycle=` is a frequency of at least 1 Hz. An input's silence deadline is
measured in cycles and sample periods ([Inputs](https://phloem.ai/docs/trellis/channels/)), so a
faster cycle notices a silent input sooner. Whether the device can hold the
cycle is checked when you deploy.

## What goes where

| Part of the file | Holds | Page |
|---|---|---|
| Top level, after `system(...)` | `input`, `output`, `condition`, `calculated`, `alert`, `on_shutdown`, `note_when`, `note_period`, `pid` | [Inputs, outputs and wiring](https://phloem.ai/docs/trellis/channels/), [Alerts and shutdown](https://phloem.ai/docs/trellis/alerts/) |
| A function passed to `sequence(...)` | steps, waits, settings, checks, people's gates | [Sequences](https://phloem.ai/docs/trellis/sequences/) |
| A function passed to `simulation(...)` | how each input moves, for running with no hardware | [Test without hardware](https://phloem.ai/docs/trellis/testing/) |
| A function passed to `unit_test(...)` | a test; never deployed | [Test without hardware](https://phloem.ai/docs/trellis/testing/) |
| Any other function | a helper; it takes the words of whatever calls it | — |

A word in the wrong part is a compile error that names the part: `alert` in a
sequence, `set` at the top level outside `on_shutdown`. So a sequence can never
add, remove or loosen what protects the system.

One file compiles to one system program plus one program per sequence, each
with its own hash. Editing only a sequence leaves the system's hash, and its
deployment, as they are.

## Values and units

| Kind | Written | Note |
|---|---|---|
| A quantity | `400 * psi`, `15 * s`, `24 * N * m` | units convert within a dimension (`2 * bar` against a psi input); across dimensions is an error |
| A time | `500 * ms`, `30 * s`, `2 * minute` | every wait, hold and timeout takes one |
| A name | `"pressure held"` | names are unique within their kind, ignoring case |
| Text | `f"temp at {pct}"` | f-strings for formatting |

Units include `Pa` `kPa` `MPa` `bar` `mbar` `psi` `atm` · `K` `degC` `degF` ·
`N` `kN` `lbf` · `kg` `g` `lb` · `m` `cm` `mm` `um` `inch` `ft` · `L` `mL` ·
`mps` `knot` · `g0` (standard gravity; `g` is the gram) · `s` `ms` `us`
`minute` `hour` · `Hz` `kHz` `MHz` `rpm` · `deg` `rad` · `V` `mV` `A` `mA`
`ohm` `kohm` `Mohm` · `W` `kW` `J` · `bit` `kbit` `Mbit` `Gbit` `byte` `kB`
`MB` `GB` (decimal: a GB is 10⁹ bytes; a rate is `Mbit / s`) · `percent`
`microstrain` · `plain` · `px` (a pixel count, never a length) · `dB`.

A channel with no unit — a count, a ratio, a serial, a tap position — is
`plain`: `cycles = input("cycles", plain)`, compared with a bare number
(`cycles > 4000`) and shown with no unit.

`dB` is a level, not an amount: a dB channel is compared with dB
(`snr < 45 * dB`, `nesz > -19 * dB`), and a dB value can be offset by dB.
It is never a plain number or a percent, and it does not multiply or divide
with another unit — `3 * dB / s` is refused.

## Values from another tool

A number your team keeps elsewhere — a requirement's limit, a part's mass —
is written with where it came from:

```python
MAX_MASS = cite("jira:PHL-42", "max_mass_kg", 12.0, rev="r7")
```

`cite(ref, field, value, rev=)` **is** `value`, exactly as written: the device
runs that number offline and looks nothing up. Apply the unit yourself
(`mass < MAX_MASS * kg`). The program keeps the citation, so when the field
changes at its source, [Trace](https://phloem.ai/docs/ai/trace/) flags this system and every run that
judged by it. `rev=` is left out when the source has no revision.

## One syntax

Each idea has exactly one spelling; the compiler refuses the others and shows
the accepted form.

| Rule | Accepted | Refused |
|---|---|---|
| One word per idea | `wait`, `check`, `alert` | `delay`, `expect`, `alarm` |
| The first one or two arguments by position, the rest by keyword | `input("temp", degC, if_silent="warn")` | `input("temp", degC, "warn")` |
| Quantities carry their unit | `400 * psi` | `400` on a psi input |
| Built-in words are not your names | `pressure = input(…)` | `wait = input(…)` |
| No `while`, recursion or lambdas | `for pct in (25, 50, 75):` | `while pressure < 30 * bar:` |
| Other files come in with `include` | `include("lib/valve_check")` | `load(…)` |

A `for` loop runs over fixed values and is unrolled when the file compiles. An
`if` on a live value is allowed in a sequence (a decision, made once when it is
reached) and in a simulation; at the top level, use an `alert` or a `condition`.

## Libraries

Functions and constants several systems share live in a library: a file under
`lib/` whose first line is `library("name")`. A library declares nothing —
anything that can change what hardware does stays in the system file.

```python
library("valve_check")

SETTLE = 2 * s

def cycle_valve(valve, opened):
    set(valve, "open")
    wait_until(opened, timeout=SETTLE)
    set(valve, "closed")
```

```python
system("valves")

valves = include("lib/valve_check")

pressure = input("pressure", bar)
fill = output("fill", states=("closed", "open"), safe="closed", failsafe=500 * ms)

def check_fill():
    valves.cycle_valve(fill, pressure > 1 * bar)
    check("rest pressure", pressure < 0.5 * bar)

sequence("check fill", check_fill)
```

A library's function sees its own file's names, never the caller's. Each
library's hash is part of every program that uses it, so a library edit shows
as a change to the sequences that call it.

## Help in the editor

**Code** completes words, shows each one's signature and an example on hover,
and offers only the words that compile where the cursor is. A word marked
*not compiled yet* — `shared_sequence` — is refused if you write it.

- [Inputs, outputs and wiring](https://phloem.ai/docs/trellis/channels/): Name channels in the file, wire them in the project
- [Sequences](https://phloem.ai/docs/trellis/sequences/): Steps, waits, settings and run fields
