Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/semhash/README.md # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / semhash / README.md` 133 lines ยท 7809 bytes Raw Blame History Rendered 1 # seed/semhash: meaning hash and semantic diff for Omega units (M1) 2 3 `semhash.om` is a seeded-Omega tool. It gives every function of a `.om` unit a hash of what the function 4 means, not how it is spelled. Renaming locals or parameters, adding or removing notes, blank lines or 5 spacing, and reordering functions do not change a function's hash. The tool errs toward CHANGED: when 6 it cannot prove that a spelling is meaning-free, it keeps the spelling verbatim, so the change shows 7 up as CHANGED rather than SAME. 8 9 The design is close to Unison's content-addressed definitions. A call is hashed as the callee's 10 hash (Merkle), so a change in a leaf reaches every transitive caller, and a function-name-only rename 11 keeps its hash. 12 13 ## Build 14 15 `semhash.om` has no `om2` line. It is compiled after the plain-function form of `om/sha256/sha256-om2.om`: 16 17 ```sh 18 mkdir -p build 19 { sed 's/^export fn /fn /' om/sha256/sha256-om2.om; cat seed/semhash/semhash.om; } > build/semhash.full.om \ 20 && $OMC < build/semhash.full.om > build/semhash && chmod +x build/semhash 21 ``` 22 23 `$OMC` is the pinned seed compiler. `check.sh` pins sha256 24 ``26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991``. A missing or different compiler 25 makes it print `SEMHASH NOT_RUN` and exit 3. 26 27 ## Modes 28 29 | command | output | exit | 30 | --- | --- | --- | 31 | `semhash < u.om` or `semhash hash < u.om` | `FN name hex line=N[ verbatim][ cycle]` per function, then `UNIT hex fns=n` | 0 | 32 | `semhash canon < u.om` | the unit with locals renamed by first occurrence (`v0 v1 ...`, or the first of `w y z q` whose letter-digit names are unused), with notes and blank lines dropped and tokens joined by one space | 0 | 33 | `semhash diff a.om b.om` | `SAME`, `CHANGED` or `REMOVED name`, `RENAMED a->b`, `ADDED name`, then `EXPORTS CHANGED` and `ENTRY CHANGED a->b` if they apply, then `DIFF SAME` or `DIFF CHANGED n=K` | 0 same, 1 changed | 34 | `semhash keywords` | the keyword table (`word code`) the tool classifies with | 0 | 35 | `semhash dups < records` | records are hash-mode `FN` lines with a unit path in front (`PATH FN name hex line=N...`). Every `SKIP ...` line is echoed. Then, for each set of two or more records with the same 64-digit hash, `DUP hex n=K` followed by ` PATH name line=N...` per member, in input order; last `DUPS groups=G fns=F records=R skipped=S` | 0 | 36 37 The other exit codes are: 38 39 - 2: a usage error or an unreadable diff file. 40 - 3: refused input, with the reason on stderr. The causes are: 41 - a CR byte; 42 - an input over 8 MiB; 43 - a control byte outside a note; 44 - an unterminated text, fn or layout block; 45 - a token starting with `$`, `#` or `@`; 46 - a duplicate fn name; 47 - a capacity overflow; 48 - no free canon letter; 49 - canon of an `om2 fmt` unit. 50 - 91-94: internal errors. 51 52 ## Duplicate-function report (#827 M1) 53 54 `sh seed/semhash/dups.sh SEMHASH [-i INDEX] [DIR...]` hashes every `.om` unit under the given 55 directories (default `om seed`, from the repository root, in `LC_ALL=C` path order), puts each 56 unit's path in front of its `FN` lines and pipes them to `SEMHASH dups`. A unit that semhash 57 refuses becomes `SKIP path rc=N reason`. With `-i INDEX` the records themselves, which form the 58 function-level meaning-hash index (one `PATH FN name hex line=N...` line per function), are also 59 written to `INDEX`. Equal hashes mean equal meaning as far as this tool can prove it (see the next 60 two sections), so a group is a set of functions that one shared definition could replace. 61 The grouping is in `semhash.om`; the script only lists files and runs the tool. 62 63 At omega-mv ``d3d1b1bc`` the report over `om/` and `seed/` is `dups/report-`d3d1b1bc`.txt`: 64 `DUPS groups=449 fns=1542 records=6959 skipped=17`. The 17 skipped units are the 65 deliberately malformed compiler fixtures (CR bytes, unterminated blocks) and 66 `dups/refused.om`. The largest group is 66 copies of `fn main to int` / `ret 0`. Many groups are 67 test fixtures that repeat a helper on purpose; others, such as `inb`, `obflush`, `ob1` and 68 `hexval` in `om/aarch64`, `om/codec`, `om/evidence`, `om/gpumath` and `om/tls`, are library code 69 that could share one unit. 70 71 ## What a function hash covers 72 73 - **The profile header**: the exact `om2 ...` or `ir` lead line, or `om1`. 74 - **The function's tokens**: 75 - parameters and locals become `$k`, numbered by first occurrence; 76 - each callee becomes `#`, and a self call becomes `@self`; 77 - every other token is kept as written. 78 - **Declarations**: every declaration the function uses, followed transitively. These are global, 79 text, const, struct, layout, abstract and holds. 80 - **Unrecognised top-level lines**: every one of them. 81 - **Mutual recursion**: functions in one call cycle are hashed as a group. Each member keeps its own 82 name and the names of the other members, and gets ` cycle` on its FN line. 83 - **The unit hash**: covers the header and every item in source order. Reordering therefore keeps 84 every function hash and changes the unit hash. 85 86 ## Not normalised (renaming these reports CHANGED) 87 88 The following are not normalised: 89 90 - Function names inside a call cycle. Because of this, RENAMED is not detected for cycle members. 91 - Global, text, const, struct, field, layout and abstract names. 92 - Locals spelled like any struct or layout field name. `get c fa` reads field `fa` when `fa` is a 93 layout field, so these locals are never renamed. 94 - Locals longer than 8 bytes. 95 - Contextual names (`requires ensures invariant result`). 96 - Every local of a function whose parameter list the tool cannot parse. That function is marked 97 ` verbatim`. 98 - Parentheses and grouping, number spelling (`10` vs `0xa`), and the operand order of commutative 99 operators. 100 101 Changing the profile line changes every function hash. A change to a declaration that no function 102 uses does not show as CHANGED for any function, but it does change the unit hash. Function order 103 matters only through the `ENTRY` and `EXPORTS` lines and the unit hash. 104 105 ## Check 106 107 Run `sh seed/semhash/check.sh` from anywhere. It makes these checks: 108 109 1. **Keyword parity**: the tool's table must equal the two `word:code` lists of `seed/omc/genkw.awk` plus the 110 `fn kw2` table of `seed/omc/omc.om`, decoded in sh. 111 2. **Oracle**: for every unit omc accepts, `omc(canon(x))` must be byte-identical to `omc(x)`. 112 - The units are rung4, contracts, omc tests, om2, host tests, the json compositions, blake3, 113 sha256, leakscan, the tool itself and the cases. 114 - For a unit omc refuses, the canon form must stay refused. 115 - `om2 fmt` units are skipped, because their output is the formatted source text. 116 3. **Cases**: `cases/*.om` are diffed against `cases/base.om`, and stdout and the exit code are 117 compared with `cases/*.exp`. 118 - `inv_*` cases must be all SAME. 119 - `neg_*` cases are the negative controls, which must report CHANGED. 120 - `ren_fn` must report `RENAMED`. 121 4. **Unit hashes**: they must be equal for alpha renaming and for notes and spacing, and differ for 122 reordering. 123 5. **Refusals and usage**: the CR input, reserved-byte input and unknown-mode cases are checked. 124 6. **Mutant**: a copy of the tool with `while` dropped from its table must fail both keyword parity 125 and the oracle (`mutant killed`). 126 7. **Dups**: `dups.sh` over `dups/` (`a.om`, `b.om`, `refused.om`) must print `dups/dups.exp` 127 exactly: `a.om sq` and `b.om sq2` (a renamed parameter) group, `inc` and `inc2` (a different 128 constant) do not, and `refused.om` is a SKIP. Control: the same records with one hash digit 129 changed past the first 16 must give `groups=0`, so grouping compares the whole hash, not the key. 130 131 `cases` in the PASS line counts the oracle-identical units, the non-control cases and the dups 132 check. `controls` counts the `neg_*` cases, the three refusal and usage checks, the killed mutant 133 and the dups control.