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
omega-mv / spec / universal / python-u64-v1.md
| 1 | # Python explicit-mask u64 adapter, profile `python-explicit-u64-pure-v1` (#627) |
| 2 | |
| 3 | Status: experimental, author-tested (TESTED), not QUALIFIED and not merge-approved. Lane Python of |
| 4 | `translation-compose-v1.md` (G2). This file defines the Python member of the S1 family. It adds no |
| 5 | envelope, registry, codec, OSC or checker change. The importer and exporter are handwritten seeded |
| 6 | Omega; Python is used only as the host test oracle and for build/dispatch orchestration. |
| 7 | |
| 8 | ## 1. Unit produced |
| 9 | |
| 10 | | field | value | |
| 11 | |---|---| |
| 12 | | header language / profile / dialect version / runtime / ABI | 3 Python3 / 4 python-explicit-u64-pure-v1 / 1 / 0 / 1 | |
| 13 | | op dialect byte | 3 | |
| 14 | | input (op 9) | type 1, attribute 0, effects 0, alias 0, obligations 1 (input-domain), slot = parameter position | |
| 15 | | constant (op 1) | type 1, attribute 1, obligations 0 | |
| 16 | | add/sub/mul (ops 2/3/4) | type 1, attribute 1 (modulo 2^64), effects 0, alias 0, obligations 5 (input-domain + source-explicit-masking) | |
| 17 | | sidecar | OMSP v1, encoding 1 (strict UTF-8; accepted sources are ASCII), raw source bytes stored exactly | |
| 18 | |
| 19 | Semantics are S1 (`translation-v1.md` section 2): integers modulo 2^64. `N(U) = R(U, C)` |
| 20 | (`translation-compose-v1.md` section 2) of a Python import equals `N` of the C import of the same |
| 21 | expression tree, byte for byte (checked on all 35 F2 positives). |
| 22 | |
| 23 | ## 2. Accepted source |
| 24 | |
| 25 | The whole input is exactly two lines, each ended by LF (0x0A), and nothing after the second LF: |
| 26 | |
| 27 | ``` |
| 28 | def <name>(<p0>, <p1>, ...): |
| 29 | return <expr> |
| 30 | ``` |
| 31 | |
| 32 | - Line 1 starts at byte 0 (no indentation). Tokens: `def`, name, `(`, 0..6 parameter names separated by |
| 33 | `,`, `)`, `:`. Spaces (0x20) may separate tokens and may end the line. No trailing comma. |
| 34 | - Line 2 starts with exactly four spaces, then the token `return`, then `<expr>`; spaces may separate |
| 35 | tokens and may end the line. The indentation rule is exactly four spaces: any other indentation, |
| 36 | a blank line, a one-line suite or any further line is refused (section 4). |
| 37 | - Names (function and parameters): ASCII `[a-z][A-Za-z0-9_]*`, at most 32 bytes, not a Python keyword |
| 38 | or soft keyword (`False None True and as assert async await break class continue def del elif else |
| 39 | except finally for from global if import in is lambda nonlocal not or pass raise return try while with |
| 40 | yield match case type`). Parameters are distinct. Any such function name is accepted (the name is not |
| 41 | part of the semantic bytes). Builtin names (`int`, `print`) are ordinary names here. |
| 42 | |
| 43 | Expression grammar (tokens; Python precedence is respected because every accepted form is fully |
| 44 | parenthesised): |
| 45 | |
| 46 | ``` |
| 47 | expr := operand | masked |
| 48 | masked := "(" operand op operand ")" "&" MASK |
| 49 | operand := param | literal | "(" expr ")" |
| 50 | op := "+" | "-" | "*" |
| 51 | MASK := 0xFFFFFFFFFFFFFFFF (exactly this spelling: lowercase x, 16 uppercase F) |
| 52 | literal := "0" | [1-9][0-9]* (value at most 2^64-1) |
| 53 | ``` |
| 54 | |
| 55 | Every arithmetic result is masked in source, by itself: `(x op y) & 0xFFFFFFFFFFFFFFFF`. A masked form |
| 56 | used as an operand needs its own parentheses, `((a + b) & 0xFFFFFFFFFFFFFFFF) * c`, because `&` binds |
| 57 | more loosely than `+ - *` in Python. At most 128 parentheses may be open at once (CPython's own limit is |
| 58 | 200). Redundant parentheses are allowed (`((a))`). |
| 59 | |
| 60 | Why the mask is mandatory: without it `a + b` is CPython bigint arithmetic (profile 3, |
| 61 | `cpython-int-pure-v1`), not S1. `(a + b + c) & MASK` is also refused, although it has the same value |
| 62 | modulo 2^64, because the profile requires every arithmetic result to be masked explicitly. Masking |
| 63 | a non-arithmetic operand (`a & MASK`), masking twice, or any other mask spelling refuses. |
| 64 | |
| 65 | ## 3. Mapping and spans |
| 66 | |
| 67 | The importer emits, in this order: one input record per parameter (declaration order, slot = position; |
| 68 | unused parameters are kept), then the expression in post-order (left operand, right operand, operation, |
| 69 | as the C importer), then the normalization pair. |
| 70 | |
| 71 | - One masked form `(x op y) & MASK` maps to exactly ONE upper operation (2 add, 3 sub, 4 mul). The mask is |
| 72 | the source-explicit-masking obligation (bit 4), not an operation; there is no record for `&` or for the |
| 73 | mask literal. |
| 74 | - Normalization pair: YES. Like the C importer, every import appends a synthetic `const 0` and |
| 75 | `add(root, that const)`, so the unit is always NORMALIZED (`translation-v1.md` section 4) and |
| 76 | `strip()` removes exactly that pair (`translation-compose-v1.md` section 3). This is required in |
| 77 | any case for `return b` with parameters `a, b`: the last result must be the function result. |
| 78 | - Spans (sidecar, half-open byte offsets): input = the parameter name token in the signature; |
| 79 | constant = its digits; arithmetic = the operator token `+`, `-` or `*` inside the masked form; |
| 80 | synthetic constant and add = `[0,0]`. The parentheses and mask have no span of their own. |
| 81 | |
| 82 | ## 4. Refusals (statuses mirror the C importer) |
| 83 | |
| 84 | Every refusal writes no stdout and one `PY_IMPORT_*` line on stderr. |
| 85 | |
| 86 | | status | name | cases | |
| 87 | |---|---|---| |
| 88 | | 65 | SYNTAX | NUL byte; empty input; missing token; indentation other than def at column 0 and return at exactly four spaces; blank line before the body; missing final LF; expression split over lines; bare `return` | |
| 89 | | 66 | UNSUPPORTED_TYPE | parameter annotation (`a: int`), return annotation (`-> int`). Annotations are refused because `int` is a bigint annotation that Python does not enforce | |
| 90 | | 67 | LIMIT | more than 4096 bytes; more than 6 parameters; more than 64 upper operations including inputs and the pair; more than 128 open parentheses; literal above 2^64-1 | |
| 91 | | 68 | UNSUPPORTED_ARITHMETIC | unmasked `+ - *`; two operations under one mask; any mask other than `& 0xFFFFFFFFFFFFFFFF` after a single operation; `a & MASK`; double mask; masked operand without its own parentheses; `/ // % ** << >> & \| ^ ~`, unary `-`/`+`, comparisons, `@`; hex/octal/binary literals other than the one mask, leading zeros (`00`, `010`), `_` separators, floats, imaginary literals | |
| 92 | | 69 | UNSUPPORTED_CONSTRUCT | decorator; anything but `def` first (`async def`, `class`, assignment, lambda); a statement other than `return` (`if`, `while`, `assert`, `global`, assignment); one-line suite; any line after the body; calls; subscripts; attributes; tuples; `;`; strings; keywords in expressions (`lambda`, `not`, `True`, conditional expressions) | |
| 93 | | 70 | UNKNOWN_IDENTIFIER | a name in the expression that is not a parameter (includes builtins such as `abs`) | |
| 94 | | 71 | UNSUPPORTED_SIGNATURE | default values, `*args`, `**kwargs`, `/` and `*` markers, trailing comma, duplicate parameters, names breaking section 2 (uppercase/underscore first, keyword, longer than 32 bytes) | |
| 95 | | 72 | UNSUPPORTED_SOURCE_FORM | any byte above 0x7E (non-ASCII, so invalid UTF-8 and NFKC-normalised Unicode identifiers such as `a`), tab, CR (CRLF), any other control byte but LF, backslash (explicit line joining), `#` (comments; a comment in the first two lines can be a PEP 263 coding declaration, e.g. `# coding: utf-7`, that changes how CPython decodes the remaining bytes) | |
| 96 | | 77 | IO | read/write failure, closed stdin | |
| 97 | |
| 98 | The public command adds 64 BAD_COMMAND (any argument) and 78 STALE_OR_MISSING_BUILD. |
| 99 | |
| 100 | ## 5. Input-domain obligation |
| 101 | |
| 102 | Python callers can pass negative ints, ints of 2^64 or more, `bool`, or non-ints. S1 inputs are |
| 103 | 0..2^64-1. This domain is obligation 1 (input-domain) on every input and arithmetic record; it is a |
| 104 | caller obligation and is not enforced in the source. Neither the importer nor the exporter accepts or |
| 105 | emits an `assert` or range check: `python -O` removes `assert` statements, so an assert is not an |
| 106 | enforcement, and a guard would be a second statement outside this grammar. Outside the domain the |
| 107 | generated function is not S1 (for example `return a` returns a negative argument unchanged). The tests |
| 108 | call every Python program only with in-domain ints. |
| 109 | |
| 110 | ## 6. Exporter |
| 111 | |
| 112 | `tools/adapters/python-export [NAME]` (Omega source `src/adapters/python-u64-export.om`) reads one envelope |
| 113 | on stdin. The reviewed #621 decoder (`src/source-aware/core.om`, `decode`) is prepended unchanged and |
| 114 | validates the whole envelope, sidecar included. Then the unit must be an S1-family member |
| 115 | (`translation-compose-v1.md` section 1): (language, profile) = (1,1), (2,2) or (3,4), runtime 0, ABI 1, every |
| 116 | record of type 1, effects 0, alias 0, input op 9 attribute 0 obligations 1, constant attribute 1 |
| 117 | obligations 0, add/sub/mul attribute 1 with the member's arithmetic obligations (1, 1, 5), input slots a |
| 118 | permutation of 0..n-1. The source language of the input is ignored; output is generated from the |
| 119 | records only (the sidecar is never read; a semantic-only envelope exports the same bytes). |
| 120 | |
| 121 | Output form (byte-exact, `NAME` defaults to `calc`, LF line ends): |
| 122 | |
| 123 | ``` |
| 124 | def <NAME>(i0, i1, ...): ("def <NAME>():" for 0 inputs; parameter i<s> is slot s) |
| 125 | return <expr> |
| 126 | ``` |
| 127 | |
| 128 | `<expr>`: input slot s is `i<s>`; a constant is its decimal; an operation at the root is |
| 129 | `(<A> <op> <B>) & 0xFFFFFFFFFFFFFFFF`, and as an operand `((<A> <op> <B>) & 0xFFFFFFFFFFFFFFFF)`, with single |
| 130 | spaces. Shared SSA results are expanded as trees. The root follows `translation-v1.md` section 4: the |
| 131 | pre-normalization root for a NORMALIZED unit, the last result otherwise. |
| 132 | |
| 133 | Exporter statuses: 64 bad command or NAME (NAME follows section 2 name rules); 65 malformed envelope |
| 134 | (any decoder refusal other than 68); 67 LIMIT when the output would not re-import (more than 4096 bytes, |
| 135 | more than 128 open parentheses, or more than 64 operations after re-import, counted as inputs plus the |
| 136 | tree-expanded constants and operations plus the pair); 68 not an S1-family unit (profiles 3, 5, 6, ops |
| 137 | 5..8, other types/effects/obligations); 77 IO; public command 78 stale build. With at most 64 records |
| 138 | the operation count is the only limit that can be reached; the other two are checked anyway. |
| 139 | |
| 140 | Round trip: for a unit produced by this importer, `import(export(U))` has the same semantic bytes as U. |
| 141 | For any S1-family unit, `N(strip(import(export(U)))) = N(strip(U))` holds when U is import-shaped |
| 142 | (inputs first in slot order, then the post-order tree, then the pair); otherwise the round trip is |
| 143 | checked by equal evaluation and the identity difference is recorded (`translation-v1.md` section 4). |
| 144 | |
| 145 | ## 7. Build, pins and commands |
| 146 | |
| 147 | Compiler: bridge-seed omc `191b776ef98821597c3e30655da6d680a9b66edbc9ceaca3c39c61ea6df098e9` (source |
| 148 | archive `0fc7d8662cb799e7c206b17c18ea4c3ec2b8fea9`, built by `tools/source-aware/build`), recorded in each |
| 149 | `build.json`. Chosen because it is the pinned compiler of the C importer, the C exporter and the #621 codec: |
| 150 | the vendor SHA256 core and the prepended `core.om` decoder are qualified under it, and the bounded |
| 151 | stdin/stdout interface needs no argv (the exporter name arrives in a one-byte-length frame, as in the C |
| 152 | exporter). The native omc `734f0a39…6b61` is not used. |
| 153 | |
| 154 | Importer = `src/source-aware/vendor/sha256.om` + `src/adapters/python-u64-import.om`; exporter = vendor |
| 155 | SHA256 + `src/source-aware/core.om` + `src/adapters/python-u64-export.om`. Outputs: `build/L627/import/` |
| 156 | and `build/L627/export/` (`program.om`, the ELF, `build.json`). The public commands refuse a changed |
| 157 | source or binary (78). |
| 158 | |
| 159 | ```sh |
| 160 | make -f mk/python-u64.mk test-python-u64 |
| 161 | # SA_SEED_REPO (absolute path; default ~/workspace/omega-bridge) holds bridge commit 0fc7d866. |
| 162 | # The target first builds this tree's #621 codec (mk/source-aware.mk source-aware) and C importer |
| 163 | # (mk/c-u64-import.mk c-u64-import). PY_SA_ROOT (default: this tree) is where `bash tools/source-aware/sa` |
| 164 | # runs; PY_C_IMPORT (default: this tree's build/L589/c-import) is the C importer. |
| 165 | # PY_C_EXPORT=<path>/tools/adapters/c-export runs Python -> C; empty means Python -> C is NOT_RUN. |
| 166 | tools/adapters/python-import < computation.py > unit.upper |
| 167 | tools/adapters/python-export [NAME] < unit.upper > generated.py |
| 168 | ``` |
| 169 | |
| 170 | ## 8. Evidence and limits |
| 171 | |
| 172 | `tests/universal/translation/python_selftest.py` (author self-test, TESTED): |
| 173 | same-language round trip for every F2-python positive and boundary control; the reference codec's |
| 174 | `identity` accepts every imported unit and equals SHA256(`omega.source.semantic.v1` || semantic bytes); |
| 175 | compose section 4.1 identity; the F2 rewrite has the same `N()` as the C import of its F1 source; python3 |
| 176 | runs the original and the exported source on boundary vectors (0, 1, 2, 2^32-1, 2^32, 2^63-1, 2^63, |
| 177 | 2^64-2, 2^64-1) and 64 seeded random vectors per program, compared with a bigint evaluation of the records |
| 178 | (REFERENCE + DIFFERENTIAL). C -> Python on every F1 positive and accepted C boundary control: C importer, |
| 179 | cc-compiled original C, generated Python; R(U, Rust) and R(U, Python) export identically. Python -> C runs |
| 180 | only when a translation-v1 section 6 exporter is given: it accepts the C member only, so the test applies |
| 181 | R(U, C) first. The native-realization leg (section 5 lowering) is NOT_RUN in this lane. Refusal fixtures |
| 182 | check exact statuses. None of this is a proof of equivalence. |
| 183 | |
| 184 | `tests/universal/translation/fixtures-f2-python.json`: the 35 F1 positives rewritten |
| 185 | (`F2-python-F1-Pnn`; F1-P33 drops its C comments and CRLF, which are source decoration), four boundary |
| 186 | controls (exact 4096 bytes, the 61-term chain at 64 operations, 16 and 128 nested parentheses), the |
| 187 | NOT_IN_PROFILE list (`comment-at-capacity`: no comments in this profile) and the refusal fixtures with |
| 188 | their statuses. |
| 189 | |
| 190 | Not covered: Python versions other than the CPython 3 used as oracle, any other Python syntax, bigint or |
| 191 | dynamic profiles (3, 6), runtime input-domain enforcement, independent validation and QUALIFIED status. |