# Algorithm examples

> This section shows three algorithms, each a protocol instantiated: separate a unit], [author an enforcement] and [verification gate].

Page: PAG · Patterns
Canonical: https://banes-lab.com/pag/patterns#algorithm-examples

This section is stop 29 of 102 in the learning route. Previous: [03 - Genesis stages](https://banes-lab.com/pag/patterns/genesis-stages.md). Next: [05 - Integrating algorithms](https://banes-lab.com/pag/patterns/algorithm-integration.md). It builds on [03 - Genesis stages](https://banes-lab.com/pag/patterns/genesis-stages.md).

This section shows three algorithms, each a protocol instantiated: [D1·a separate a unit](https://banes-lab.com/pag/patterns#algorithm-examples-panel-a), [D1·b author an enforcement](https://banes-lab.com/pag/patterns#algorithm-examples-panel-b) and [D1·c verification gate](https://banes-lab.com/pag/patterns#algorithm-examples-panel-c). In each, the chain is expanded into nodes, every node is headed by its layer, axis, math type and yields and tagged with its [genesis stage](https://banes-lab.com/pag/patterns/genesis-stages.md), every contract reads the previous node's output and ends on a gate that carries evidence, and every placeholder is bound to the task's own nouns. [D1·d beyond the chain](https://banes-lab.com/pag/patterns#algorithm-examples-panel-d) shows what every instance carries beyond its chain, and [D1·e transfers or bound](https://banes-lab.com/pag/patterns#algorithm-examples-panel-e) shows what transfers between instances and what is bound per task.

### Three instances

An algorithm copied from an example keeps the example's nouns and loses the task's. A seam search returns nothing, the node is skipped without an entry, the extraction runs over an empty set, and the count that would have exposed the gap is taken over the wrong population. The chain is the protocol's contract and the gates are how the contract is checked, so the two transfer together and the nouns do not.

For this reason an algorithm is treated as an instance of a protocol, never as a copy of another algorithm. The task's nouns are bound into the protocol's shape, rather than an example being edited until it fits. In practice, the chain is written above the nodes with the reason it was chosen, so a reader sees the protocol before the instance. The chain is expanded into one node per verb, each node is tagged with the stage its verb derives, and each placeholder is bound to a noun from the task, so no placeholder survives into the document. Each node has a contract whose input names the previous output, and it closes on three to five checks that name its output, the evidence that settles each check and the set that at least one check ranged over. A failure is routed to the earliest node that can supply the missing evidence and an unknown to blocked, every write is preceded by a refusal, an empty result is a finding rather than a silent skip, and every command and location is named as a slot the adapter resolves.

To check this, look for a placeholder that survived into the document, a node whose stage disagrees with its verb, a contract whose input names nothing from its predecessor, or a gate whose count is taken over a set smaller than the one it claims. Any of the four marks an instance that was copied rather than bound. A node with two decisions is two nodes, and the examples are not a license to collapse them.

In the first instance, the seam gate reports an empty set of seams as a unit that does not split, because a pass over an empty population measures nothing, and the population shown beside the verdict is what makes that visible. The repair owner on the extraction gate is the seam node, because a bad extraction usually comes from a bad seam.

The second instance is the shape every rule held by attention takes before it can be trusted. The analysis names a shape rather than an instance, because a check that names an instance fails on the next case. The check is proven to fire as described in [the check comes first](https://banes-lab.com/disciplined-methodology/build/the-check-comes-first.md), and a failure there routes back to the node that composed the check, not to the probe; the probe's own write is refused wherever it would land on a real file.

The third instance is appended to every plan rather than chosen. Its gate is the evidence gate described in [from intent to structure](https://banes-lab.com/pag/patterns/intent-to-structure.md), applied to the plan's own reasoning, and its standing line names what moved beneath the plan while it was being checked.

D1·a separate a unit

```pag
# <separate-a-unit> · ANALYZE → FIND → EXTRACT → CREATE → VERIFY
# chosen because the request asks to split a unit that mixes two concerns

# NODE 1 — ANALYZE THE UNIT       [epistemic · analysis · logic · yields: set]
@genesis: difference
CONTRACT:
input:     <unit>
transform: READ_RESOURCE <unit> INTO <source>; ANALYZE_CONTENT <source> AGAINST <responsibilities> INTO <concerns>
output:    <concerns>, each naming the lines that carry it
HANDOFF GATE:
[check] <source> read from <unit> (evidence: the read returned content)
[check] <concerns> holds more than one entry (evidence: a count above one) over: <source> lines measured: <assigned> / <lines>
[check] every <concern> names its lines (evidence: no concern with an empty range)
result: pass → NODE 2 | unread → REPAIR (owner: NODE 1) | unknown → BLOCKED

# NODE 2 — FIND THE SEAMS         [epistemic · ontology · set-theory · yields: set]
@genesis: existence
CONTRACT:
input:     <concerns> from NODE 1
transform: SEARCH_CONTENT <source> FOR <boundaries between concerns> INTO <seams>
output:    <seams>
HANDOFF GATE:
[check] every <seam> lies between two <concerns> (evidence: two concern ids per seam) over: <seams> measured: <between two> / <seams>
[check] no <seam> cuts a single statement (evidence: each seam on a statement boundary)
[check] <seams> is non-empty, or the unit is reported as one that does not split (evidence: a count, or the report)
result: pass → NODE 3 | does not split → TERMINATE | unknown → BLOCKED

# NODE 3 — EXTRACT THE CONCERN    [epistemic · reasoning · graph · yields: edge-list]
@genesis: relation
CONTRACT:
input:     <seams> from NODE 2
transform: EXTRACT_FACTS <concern> FROM <source> INTO <extracted>
preserves: every reference between <extracted> and the rest
output:    <extracted>, and every reference between it and the rest
HANDOFF GATE:
[check] <extracted> carries every line of <concern> (evidence: the line ranges match) over: <concern> lines measured: <carried> / <lines>
[check] <source> minus <extracted> carries the rest (evidence: the two ranges partition the source)
[check] every reference between the two is named (evidence: an edge per reference)
result: pass → NODE 4 | partition broken → REPAIR (owner: NODE 2) | unknown → BLOCKED

# NODE 4 — CREATE THE NEW UNIT    [epistemic · formalisation · computation · yields: artifact]
@genesis: structure
CONTRACT:
input:     <extracted> from NODE 3
transform: COMPOSE_ARTIFACT <new-unit> FROM <extracted> USING <the shape units take here>; PERSIST_ARTIFACT <new-unit> TO <destination>
output:    <new-unit>
freshness: fingerprint(<extracted>) + fingerprint(this document)
HANDOFF GATE:
[check] <new-unit> persisted (evidence: a read of <destination> returns it)
[check] <new-unit> declares what it imports from <unit> (evidence: the import list) over: references measured: <declared> / <references>
[check] <unit> declares what it imports from <new-unit> (evidence: the import list)
refuse: <destination> exists and was not read before PERSIST_ARTIFACT
result: pass → NODE 5 | undeclared import → REPAIR (owner: NODE 4) | unknown → BLOCKED

# NODE 5 — VERIFY THE SPLIT       [evaluative · verification · logic · yields: boolean]
@genesis: constraint
CONTRACT:
input:     <new-unit> from NODE 4
transform: EXECUTE_TOOL {toolchain.verify_command} INTO <verdict>; VALIDATE_ARTIFACT <verdict> AGAINST <green on one full run>
output:    <verdict>
HANDOFF GATE:
[check] <verdict> green on one full run (evidence: the run's own output) over: the run's steps measured: <green> / <steps>
[check] no circular dependency between <unit> and <new-unit> (evidence: the import graph)
[check] every consumer of <unit> resolves (evidence: the typecheck)
refuse: a run that would mutate the tree before EXECUTE_TOOL
standing: moved-set <the files changed since NODE 4>
result: pass → TERMINATE | red → REPAIR (owner: NODE 3) | unknown → BLOCKED
```

D1·b author an enforcement

```pag
# <author-an-enforcement> · ANALYZE → CREATE → LINK → EXECUTE → VERIFY
# chosen because a rule held by attention needs a check that holds it

# NODE 1 — ANALYZE THE SHAPE      [epistemic · analysis · logic · yields: set]
@genesis: difference
CONTRACT:
input:     <the violations seen so far>
transform: ANALYZE_CONTENT <the violations> AGAINST <the shape they share> INTO <shape>
output:    <shape>, with its one fix
HANDOFF GATE:
[check] <shape> names a structure, never an instance (evidence: no vendor, symbol or path in it)
[check] every violation seen is an instance of <shape> (evidence: one match per violation) over: <the violations> measured: <matched> / <violations>
[check] one fix is stated for <shape> (evidence: the fix sentence)
result: pass → NODE 2 | instance named → REPAIR (owner: NODE 1) | unknown → BLOCKED

# NODE 2 — CREATE THE CHECK       [epistemic · formalisation · computation · yields: artifact]
@genesis: structure
CONTRACT:
input:     <shape> from NODE 1
transform: COMPOSE_ARTIFACT <check> FROM <shape> USING <the form checks take here>; PERSIST_ARTIFACT <check> TO {project.rule_home}
output:    <check>
freshness: fingerprint(<shape>) + fingerprint(this document)
HANDOFF GATE:
[check] <check> persisted (evidence: a read of {project.rule_home} returns it)
[check] <check> reports <shape> and states its fix (evidence: its message)
[check] <check> names no vendor, symbol or path (evidence: a scan of its literals) over: its literals measured: <neutral> / <literals>
refuse: {project.rule_home} already holds a check of that name before PERSIST_ARTIFACT
result: pass → NODE 3 | instance literal → REPAIR (owner: NODE 2) | unknown → BLOCKED

# NODE 3 — LINK THE CHECK         [epistemic · reasoning · graph · yields: edge-list]
@genesis: relation
CONTRACT:
input:     <check> from NODE 2
transform: LINK <check> TO <the registry the gate reads>
output:    the registry entry
HANDOFF GATE:
[check] <check> resolves from the registry (evidence: a lookup returns it)
[check] <check> is active (evidence: the entry)
[check] nothing else in the registry changed (evidence: a diff of the entries) over: registry entries measured: <unchanged> / <entries>
result: pass → NODE 4 | unresolved → REPAIR (owner: NODE 3) | unknown → BLOCKED

# NODE 4 — PROVE IT FIRES         [epistemic · formalisation · analysis · yields: procedure]
@genesis: transformation
CONTRACT:
input:     the registry entry from NODE 3
transform: PERSIST_ARTIFACT <a deliberate violation> TO <a probe>; EXECUTE_TOOL {toolchain.lint_command} INTO <report>; RESTORE <a probe>
output:    <report>
HANDOFF GATE:
[check] <report> names <a probe> under <check> (evidence: the report line)
[check] <report> carries the expected message (evidence: the message text)
[check] <a probe> restored (evidence: a read returns the original) over: probes measured: <restored> / <planted>
refuse: <a probe> names a real file before PERSIST_ARTIFACT
result: pass → NODE 5 | silent → REPAIR (owner: NODE 2) | unknown → BLOCKED

# NODE 5 — VERIFY THE TREE        [evaluative · verification · logic · yields: boolean]
@genesis: constraint
CONTRACT:
input:     <report> from NODE 4
transform: EXECUTE_TOOL {toolchain.verify_command} INTO <verdict>; VALIDATE_ARTIFACT <verdict> AGAINST <green on one full run>
output:    <verdict>
HANDOFF GATE:
[check] <verdict> green on one full run (evidence: the run's own output)
[check] every real occurrence of <shape> repaired in this change (evidence: the check reports none) over: occurrences measured: <repaired> / <occurrences>
[check] <check> caught each of them before the repair (evidence: the first run's report)
refuse: a run that would mutate the tree before EXECUTE_TOOL
standing: moved-set <the files changed since NODE 2>
result: pass → TERMINATE | occurrence remains → REPAIR (owner: NODE 1) | unknown → BLOCKED
```

D1·c verification gate

```pag
# <verification-gate> · ANALYZE → VERIFY → REPORT · appended to every plan

# NODE N — VERIFY THE REASONING   [evaluative · verification · logic · yields: boolean]
@genesis: constraint
CONTRACT:
input:     every node above
transform: ANALYZE_CONTENT <every gate above> AGAINST <a check that is a judgement, lacks evidence or lacks a population> INTO <weak-gates>; VALIDATE_ARTIFACT every <claim> IN <the plan> AGAINST <the tree>; REPORT_RESULT <verdict> TO <the parties whose next work it creates>
output:    <verdict>, with every gate, its evidence, and what it reached
HANDOFF GATE:
[check] <weak-gates> is empty (evidence: a count of zero) over: <every gate above> measured: <sound> / <gates>
[check] every <claim> supported by evidence, none by the absence of a contradiction (evidence: an evidence entry per claim)
[check] <verdict> names what the run reached before what it found (evidence: the report's first line)
standing: moved-set <the surfaces re-read since the plan began>
result: pass → TERMINATE | weak gate → REPAIR (owner: the earliest node whose gate is weak) | unknown → BLOCKED
```

D1·d beyond the chain

```mermaid
flowchart TB
chain["A verb chain"]
nodes["Nodes · one per verb, tagged with the stage its verb derives"]
contract["A contract · reads the prior output, yields one record"]
gate["A gate · three to five checks over the node's output, each with its evidence and its set"]
empty["An empty result is a finding · never a skip"]
slot["A command or a location is a slot · the adapter resolves it"]
chain --> nodes --> contract --> gate
gate --> empty
nodes --> slot
```

D1·e transfers or bound

```mermaid
flowchart LR
transfers["Transfers to any task of the shape · the chain, the tags, the contract and gate shape"]
bound["Bound per task · the nouns, the sources, the thresholds"]
instance["An instance"]
example["An example · another task's nouns still inside"]
transfers --> instance
bound --> instance
transfers -. nouns not rebound .-> example
```

## Links to

- [Genesis stages](https://banes-lab.com/pag/patterns/genesis-stages.md)
- [The check comes first](https://banes-lab.com/disciplined-methodology/build/the-check-comes-first.md)
- [From intent to structure](https://banes-lab.com/pag/patterns/intent-to-structure.md)
