> ## Documentation Index
> Fetch the complete documentation index at: https://74b1afe-archee-studio.manicule.dev/llms.txt
> Use this file to discover all available pages before exploring further.
> For every page in one file, fetch https://74b1afe-archee-studio.manicule.dev/llms-full.txt (large).
> To search and read these docs as tools, connect the MCP server at https://74b1afe-archee-studio.manicule.dev/api/mcp.
> Every agent surface: https://74b1afe-archee-studio.manicule.dev/.well-known/llms-manifest.json

# Sets, views and rules (/arql/rules)

<!-- agent-signals: reading_time_min: 2 · est_tokens: 545 · updated: 2026-12-05 -->
Related: [Facts and relations](/arql/overview.md), [Query reference](/arql/queries.md), [Connect your computer](/cli/connect.md)

> Combine queries with set operations, name a pattern with DEFINE, and keep a check as a RULE.

## Set operations

`UNION`, `INTERSECT` and `EXCEPT` combine queries that return the same columns, left to right, with set semantics. `EXCEPT` keeps the rows of the left query that the right one lacks.

```text
Bearings and seals:
MATCH (p:bearing) RETURN p.id UNION MATCH (p:seal) RETURN p.id

Both coaxial and touching:
MATCH (p)-[:coaxial]-("shaft.II") RETURN p.id INTERSECT MATCH (p)-[:touches]-("shaft.II") RETURN p.id

Shafts with no dimension:
MATCH (p:shaft) RETURN p.id EXCEPT MATCH (:dim)-[:dimensions]->(p:shaft) RETURN p.id
```

## Optional match

`OPTIONAL MATCH` keeps a row when its pattern finds nothing:

```text
MATCH (s:shaft) OPTIONAL MATCH (d:dim)-[:dimensions]->(s) RETURN s.id, count(d) AS dims
```

Shafts without dimensions appear with `dims = 0`.

## DEFINE

`DEFINE` names a pattern you reuse in the same program:

```text
DEFINE mounted_on(p, s) AS MATCH (p)-[:coaxial]-(s:shaft), (p)-[:touches]-(s) RETURN p, s;
MATCH mounted_on(p, s) RETURN s.id, count(p) AS carried
```

Recursion is a monotone closure over relations, capped in depth, with no aggregate or negation inside. A capped result says `truncated: true`.

## RULE

`RULE name AS query` is a check. Each returned row is a violation, and an empty result passes.

```text
RULE seal_on_thread AS MATCH (s:seal)-[:touches]-(t:thread) RETURN s.id, t.id
```

Keep the rules that matter to your design. The same rule runs after every edit, and a policy is a rule over the result.

## Rule packs

A rule pack is a set of `.arql` rules evaluated together. The packs `compat`, `departures`, `map` and `impact` ship with the engine. In `impact`, `touching` is the one-hop set `[:touches|coaxial]` and `impacted` is the two-hop set `[:touches|coaxial*1..2]`. List them with:

```bash
agentcad search 'rule pack'
```

## Use ARQL in a review

1. Export the drawing, or open the `.dxf`.
2. Run the ten common queries, or your own, against it.
3. Turn the query that matters into a `RULE`.
4. After each revision, run the rules again and read the rows.

See [Query reference](/arql/queries) for the grammar.
