# The definition A definition of what a patrun matcher answers, in Lean 4, for patterns whose values are exact strings or the star `*`, which takes any value. That is all that Seneca, the main user of patrun, asks of it. The matchers that a matcher can be given besides are outside it for now: globs (#35) and intervals (#36). It says what the right answer is, as relations over the table of patterns that hold data, and not how to compute it. A model computes the answers, and every operation of the model is proved to give the answer the definition gives. An oracle runs the model against the rows that the TypeScript and Go ports share in `test/data/`, and writes more rows for them to run. It needs nothing but Lean 4.24.1 itself, with no Mathlib, and builds in seconds. | File | What it holds | | --- | --- | | `Patrun/Basic.lean` | The definition: patterns, key orders, subjects, matching, the order of `find`, and what `find`, `findexact`, collect, exact collect and `list` answer | | `Patrun/Order.lean` | That a key order is a strict order of all keys, that the order of `find` is strict and total on the patterns that match a subject, so `find` and `findexact` each have one answer, that it puts the narrower of two patterns first, and what a key order decides | | `Patrun/Model.lean` | The model, a key order and a list of the patterns that hold data, with a proof for each operation | | `Oracle.lean` | `oracle check DIR` and `oracle rows` | ## The definition in brief A matcher has a **key order**: the keys that it is told come first, in the order they are to come. Those keys come before every other key, and the others come in the order of their text, as strings of code points. With no keys named, that is the order of all keys. A matcher keeps the key order it was made with. A **pattern** is a list of key/value pairs whose keys ascend strictly in the matcher's key order, so each key appears once, and none of whose keys is the empty string. A pattern added with its keys in any order is stored in that form. The empty string is not a key: pairs that have it make no pattern, and adding them changes nothing. Both ports refuse them with an error. A **subject** gives each key a value or none. A pattern **matches** a subject when the subject has a value for the key of every pair of the pattern, and that value is the pair's, unless the pair's value is the star. A subject may have a value for the empty string. As no pattern has that key, the value makes no difference to `find`, and `findexact` finds nothing for such a subject. The **star** is the pattern value `*`. A pair with it asks only that the subject has a value for the key. Any value will do, the empty string too, but a subject with no value for the key does not match. It is always on: it is no matcher's, and means the same when gex is on. A matcher is a **table**: the data, if any, at each pattern. `add` sets a pattern's data. `add` with no data, and `remove`, leave the pattern with none. The order in which patterns were added makes no difference. **`find`** answers the data of the matching pattern that comes first in this order: more pairs first, where a pair with the star counts as a pair. Among patterns with as many pairs, the one whose keys come first in the key order, compared key by key. Among patterns with the same keys, the one with an exact value at the first key where the other has the star. Two distinct patterns that match one subject always come in one order or the other, since the subject fixes their exact values, so no tie is left to break. **`findexact`** answers the data of the matching pattern that has every key of the subject. If several do, they differ only in where they have the star, and the first of them in the order of `find` is the answer. **collect**, `find(subject, false, true)`, answers the data of every matching pattern, ordered by their keys compared key by key, then an exact value before the star, where a pattern whose pairs begin another's comes first. **exact collect**, `find(subject, true, true)` or Go's `CollectExact`, answers the data of every matching pattern that has every key of the subject, in the same order. Those patterns have the same keys, so for them it is the order of `find`, and the first is what `findexact` answers. **`list(partial)`** answers every pattern that holds data and has every pair of the partial pattern, with its data. A pair of the partial pattern with the star asks only that a pattern has the key. The empty pattern has only the pairs of the partial pattern with none. A partial pattern with the empty string as a key lists nothing. The order is pair by pair: by key in the key order, and then by value, where the values of a key come whole numbers first, in numeric order, then the other values as text, then the star. A whole number is written in digits with no leading zero. So that where a pattern comes can be given as a list of strings, the definition gives each key a **label**: `0` and then as many `1`s as its place for a key that the key order names, and `1` and then the key for any other. Keys come in the order of their labels, and no two keys share one. ## What is proved Each of these holds for every key order. - `KeyOrder.lt_irrefl`, `KeyOrder.lt_trans`, `KeyOrder.lt_or_gt_of_ne`: a key order is a strict order of all keys, and two distinct keys come one way or the other. - `KeyOrder.lt_of_mem_of_not_mem`, `KeyOrder.lt_of_not_mem`, `KeyOrder.lt_nil`: a key that the order names comes before every key that it does not, two keys that it does not name come in the order of their text, and with no keys named that is the order of all keys. - `KeyOrder.label_lt_iff`, `KeyOrder.label_inj`: keys come in the order of their labels, and no two keys share a label. - `Before.irrefl`, `Before.trans`, `Before.asymm`: the order of `find` is strict. - `Before.total`: two distinct patterns that match one subject come in one order or the other. - `Before.of_contains`: a pattern comes before every other pattern that it contains, which is one that has each of its pairs or, for a pair with the star, its key. `Matches.of_contains`: every subject that matches the first matches the second. So the narrower of two patterns always comes first, and the order only chooses between patterns of which neither is the narrower. - `Before.of_key_lt`, `Before.of_exact_star`: between two patterns with as many pairs, the first pair at which they differ decides. If its keys differ, the pattern whose key comes first in the key order is first, which is all that a key order changes. If its key is the same, the pattern with an exact value comes before the one with the star. - `Before.of_first_named`: of two patterns with as many pairs, one that has the key that the key order names first comes before one that lacks it. - `Find.unique`, `FindExact.unique`: each has one answer. - `Find.congr`: the value a subject has for the empty string makes no difference to `find`. - `FindExact.none_of_empty_key`: `findexact` finds nothing for a subject that has a value for the empty string. - `Router.find_spec`, `findExact_spec`, `collect_spec`, `collectExact_spec`, `list_spec`: the model answers as defined, for any valid router. - `Router.add_valid`, `Router.table_add`: `add` and `remove` keep a router valid and change its table as defined. - `Router.add_order`: they leave it the key order it was made with. - `Router.add_refused`: pairs with the empty string as a key leave a router as it was. - `Router.find_eq_of_table_eq`: two routers with the same key order and the same table answer `find` alike, however their patterns were added. The proofs use only Lean's standard axioms. ## How the ports map onto it Both ports store a pattern's values as strings. TypeScript takes a string, a number or a boolean for a value, a number or boolean as the string it converts to, leaves out a key whose value is `null` or `undefined`, and refuses any other value. Go takes `map[string]string`. Data `null` or `undefined` in TypeScript, or `nil` in Go, is no data: an `add` with it leaves the pattern holding nothing, as `remove` does. A pattern or a subject is a map. In Go that is a `map[string]string`. In TypeScript it is an object written as a literal, or one with no prototype, and a pattern's keys are those that `Object.keys` lists. The key order is the `order` option in TypeScript and `Options.Order` in Go: a list of the keys that come first. Both ports refuse one that names a key twice, or names the empty string, when the matcher is made, with a `TypeError` in TypeScript and a panic in Go, and TypeScript refuses one that is not an array of strings. `KeyOrder.Valid` says which key orders a matcher can be made with. The definition gives every list of keys a meaning, the first place of a key counting, and nothing that is proved needs more. For a subject, Go's `map[string]string` is the definition's subject. In TypeScript, a key has a value when it is the subject's own, enumerable or not, and holds a string, a number or a boolean. A number or boolean is matched as the string it converts to. Any other value, such as `null`, an object or an array, is no value, and nothing that the subject inherits is one. The definition has only maps, so it does not say what a port does with anything else. Both ports treat it by what was asked. `add` and `remove` refuse a pattern that is no map, `null` or `nil` among them, with the error they give for the empty string as a key: a `TypeError` in TypeScript, a panic in Go. `find`, `findexact` and collect match nothing for a subject that is no map. `list` takes `null` or `nil` as no partial pattern, and lists nothing for any other value that is no map. ## Running it With Lean 4.24.1 on the `PATH`, or with `elan`, which reads `lean-toolchain`, run this from the repository root: ```sh make spec ``` That checks every proof, checks every row in `test/data/` that the definition covers, and writes `test/data/spec.tsv`. The CI job then fails if that file differs from the committed one. ## The rows `oracle check` reads the rows as the ports' runners do. It skips the sections that turn on intervals, and those that turn on gex and add a glob. A star alone is not a glob, so a section with gex on whose only special value is the star is checked. It checks each `find`, `findexact`, `collect`, `collectexact` and `list` row against the definition. A `refuse` row is an add that both ports must refuse: it checks that the definition cannot take that pattern either, and that it can take the pattern of every `add` and `remove` row. A section is checked for the key order that its options give, and a key order that no matcher can be made with is reported. All 426 of the hand-written rows it covers agree. `oracle rows` writes `test/data/spec.tsv`: 100 sections of patterns and subjects, 4,511 rows to check, with the answers the definition gives. A pattern's value is the star one time in three, and keys and values such as `constructor` and `toString` are among the rest. Subjects and partial patterns now and then have the empty string as a key, and some sections try to add a pattern that has it, which must be refused. The values 2 and 10 are among the rest, which come in one order as numbers and in the other as text. Patterns are added in the order they are drawn, and are left holding nothing both by `remove` and by an `add` with no data, some of them patterns that were never added. Every other section runs with gex on, where the star must mean the same. The first 60 sections draw their patterns at random and have no key order. They seldom have two patterns with as many pairs that match one subject, which is where the order of keys counts. So the other 40 take every pattern from the pairs of one subject, which then matches them all: most of a section's patterns have as many pairs, and some have the same keys and differ in where they have the star. 26 of the 40 run with a key order of up to three keys, and 193 of their rows have another answer if the key order is left out. The rows are generated from a fixed seed, so every run writes the same rows. Both ports run them as they run every file in `test/data/`, and pass them. ## Where the ports stand Both ports now answer as the definition says for everything it covers, and the rows no longer keep clear of anything: they list with pairs beside the empty pattern, remove the empty pattern and patterns that were never added, and give values in every order. Outside the definition: the gex matcher (#35) and the interval matcher (#36), customizers, the second argument of TypeScript's `list`, and `toString`, `toJSON` and `top`. What the ports do with the two matchers no longer depends on the order of adding, which was what kept them out of a definition whose matcher is only the data at each pattern. At one key, an exact value comes before a matcher's value, and a matcher's value before the star. Every glob comes before every interval. Of two globs, or two intervals, the narrower comes first: the one that takes only values that the other takes too. The READMEs give the tests that decide it. Both ports check them over every small glob and interval, 4,958 and 44,671 pairs of which one is the narrower, and `test/data/rank.tsv` has the cases they share. That is a check and not a proof: the proof belongs here, beside `Before.of_contains`, when the two matchers are described. Both ports read a number for an interval alike, as the whole of a value that is a decimal number, and `list` gives the patterns that have a matcher's value.