interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tools / gen / abstract / genabs.sh

214 lines · 12175 bytes Raw Blame History
1# tools/gen/abstract/genabs.sh -- shell library of the GEN-3 extractor and verifier (aien-spark/omega#478).
2# Sourced by tools/gen/abstract/extract and tools/gen/abstract/verify, so both run the same checks and write the
3# same receipt bytes. Bash (as tools/exp445 uses), no Python. Deterministic: no clock, no randomness, C locale.
4#
5# ga_init <build dir> <repo root> <work dir> binaries come from the build dir; corpus files from the root
6# Every check calls the repo's own tools: omegese, omega_emit, x445, checker2, ola_runner (through gabs run).
7export LC_ALL=C
8GA_CORPUS=tests/one_layer/generalization/corpus
9GA_HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10GA_PROPERTY_N=256
11
12ga_init() {
13 GA_B="$1" GA_ROOT="$2" GA_W="$3" GA_ERR=""
14 GA_OMEGESE="$GA_B/omegese/omegese" GA_EMIT="$GA_B/omega_emit/omega_emit" GA_X445="$GA_B/omega_emit/x445"
15 GA_CHK="$GA_B/checker2/checker2" GA_RUNNER="$GA_B/one_layer/ola_runner" GA_GABS="$GA_B/gen_abstract/gabs"
16 GA_ORACLE="$GA_B/gen_abstract/oracle" GA_FAMSIG="${GEN_FAMSIG:-$GA_B/gen_abstract/famsig}"
17 local t
18 for t in "$GA_OMEGESE" "$GA_EMIT" "$GA_X445" "$GA_CHK" "$GA_RUNNER" "$GA_GABS" "$GA_ORACLE" "$GA_FAMSIG"; do
19 [ -x "$t" ] || { echo "genabs: missing tool $t (make omegese omega_emit checker2 ola gen-abstract)" >&2; return 2; }
20 done
21 mkdir -p "$GA_W/scratch"
22}
23ga_sha() { sha256sum "$1" | cut -c1-64; }
24ga_shas() { printf '%s' "$1" | sha256sum | cut -c1-64; }
25
26# Tool digests a receipt carries: the sources of the extractor, the verifier, the probe runner and the record
27# validator (a change to any of them changes every receipt).
28ga_tool_lines() {
29 local f
30 for f in extract verify probe genabs.sh gabs.c recheck.c; do printf 'tool tools/gen/abstract/%s %s\n' "$f" "$(ga_sha "$GA_HERE/$f")"; done
31}
32
33# ga_chain <prog.omegese> <base> -- reader chain: omegese emit, omega_emit emit, checker2 check.
34# Writes <base>.json and <base>.ir. On failure returns 1 with GA_ERR set.
35ga_chain() {
36 local out
37 if ! out="$("$GA_OMEGESE" emit "$1" "$2.json" 2>&1)"; then GA_ERR="omegese emit refused: $(printf '%s' "$out" | head -n 1)"; return 1; fi
38 if ! out="$("$GA_EMIT" emit "$2.json" "$2.ir" 2>&1)"; then GA_ERR="omega_emit refused: $(printf '%s' "$out" | head -n 1)"; return 1; fi
39 if ! out="$("$GA_CHK" check "$2.ir" 2>&1)"; then GA_ERR="checker2 refused: $(printf '%s' "$out" | head -n 1)"; return 1; fi
40 case "$out" in checker2=ACCEPT*) ;; *) GA_ERR="checker2: $out"; return 1;; esac
41 return 0
42}
43
44# ga_fp <prog.omegese> <base> -- canonical IR fingerprint (RECORD_SCHEMA.md section 6) on stdout.
45ga_fp() {
46 "$GA_OMEGESE" emit "$1" "$2.fj" > /dev/null 2>&1 || { GA_ERR="omegese emit refused"; return 1; }
47 "$GA_GABS" canon "$2.fj" "$2.cj" > /dev/null 2>&1 || { GA_ERR="gabs canon failed"; return 1; }
48 "$GA_EMIT" emit "$2.cj" "$2.cir" > /dev/null 2>&1 || { GA_ERR="omega_emit refused the canonical program"; return 1; }
49 ga_sha "$2.cir"
50}
51
52# Omegese scalar type -> requirement spelling
53ga_req() {
54 case "$1" in bool) echo bool;; uchar) echo u8;; ushort) echo u16;; uint) echo u32;; ulong) echo u64;;
55 byte) echo i8;; short) echo i16;; int) echo i32;; long) echo i64;; *) return 1;; esac
56}
57
58# ga_params <fn line> -- sets GA_NAME, GA_PN[] (names), GA_PT[] (types; an array is "view uint 4"), GA_RET
59# (Omegese), GA_REQP (params line value, requirement spelling), GA_FLATN (flattened scalar names),
60# GA_FLATT (their requirement types), GA_FLATP (" name type" pairs for a wrapper fn line).
61ga_params() {
62 local w=($1) i=2 t
63 GA_NAME="${w[1]}" GA_PN=() GA_PT=() GA_REQP="" GA_FLATN="" GA_FLATT="" GA_FLATP=""
64 while [ "$i" -lt "${#w[@]}" ] && [ "${w[$i]}" != to ]; do
65 GA_PN+=("${w[$i]}")
66 case "${w[$((i + 1))]}" in
67 view|edit|own)
68 GA_PT+=("${w[$((i + 1))]} ${w[$((i + 2))]} ${w[$((i + 3))]}")
69 t="$(ga_req "${w[$((i + 2))]}")" || return 1
70 GA_REQP+=" ${w[$((i + 1))]} $t ${w[$((i + 3))]}"
71 local j
72 for ((j = 0; j < ${w[$((i + 3))]}; j++)); do
73 GA_FLATN+=" ${w[$i]}_$j" GA_FLATT+=" $t" GA_FLATP+=" ${w[$i]}_$j ${w[$((i + 2))]}"
74 done
75 i=$((i + 4)) ;;
76 *)
77 GA_PT+=("${w[$((i + 1))]}")
78 t="$(ga_req "${w[$((i + 1))]}")" || return 1
79 GA_REQP+=" $t" GA_FLATN+=" ${w[$i]}" GA_FLATT+=" $t" GA_FLATP+=" ${w[$i]} ${w[$((i + 1))]}"
80 i=$((i + 2)) ;;
81 esac
82 done
83 GA_RET="${w[$((i + 1))]}"
84}
85
86# ga_wrappers <helper body> <pre> <post> <out prog> -- the helper, then gw_call (the helper on flattened
87# scalar arguments), gw_pre, gw_post (post with res = the helper's result) and gw_chk (true when pre is false,
88# else post). Arrays are packed from their scalars and freed before each return.
89ga_wrappers() {
90 ga_params "$(head -n 1 "$1")" || { GA_ERR="unknown type in the signature"; return 1; }
91 local pack="" free="" args="" i
92 for i in "${!GA_PN[@]}"; do
93 local tw=(${GA_PT[$i]}) n="${GA_PN[$i]}"
94 if [ "${#tw[@]}" = 3 ]; then
95 [ "${tw[0]}" != own ] || { GA_ERR="own array parameter (v1 checks view and edit only)"; return 1; }
96 pack+="$n = new ${tw[1]} ${tw[2]} 0"$'\n'
97 local j
98 for ((j = 0; j < tw[2]; j++)); do pack+="put $n $j ${n}_$j"$'\n'; done
99 free+="free $n"$'\n'
100 fi
101 args+=" $n"
102 done
103 {
104 cat "$1"
105 printf 'fn gw_call%s to %s\n%sgw_r = %s%s\n%sret gw_r\nend\n' "$GA_FLATP" "$GA_RET" "$pack" "$GA_NAME" "$args" "$free"
106 printf 'fn gw_pre%s to bool\n%sgw_r = bool false\nif %s\ngw_r = true\nend\n%sret gw_r\nend\n' "$GA_FLATP" "$pack" "$2" "$free"
107 printf 'fn gw_post%s to bool\n%sres = %s%s\ngw_r = bool false\nif %s\ngw_r = true\nend\n%sret gw_r\nend\n' "$GA_FLATP" "$pack" "$GA_NAME" "$args" "$3" "$free"
108 printf 'fn gw_chk%s to bool\nif gw_pre%s\nret gw_post%s\nend\nret true\nend\n' "$GA_FLATP" "$GA_FLATN" "$GA_FLATN"
109 } > "$4"
110}
111
112# ga_run <label> <ir> <entry> <ret req> <inputs> <expected> <types...> -- one harness-runner check; appends
113# "<label> inputs=<sha> expected=<sha> <verdict>" to GA_RCPT; returns 1 (GA_ERR) unless ACCEPT.
114ga_run() {
115 local label="$1" ir="$2" entry="$3" ret="$4" in="$5" exp="$6"; shift 6
116 local v; v="$("$GA_GABS" run "$GA_RUNNER" "$GA_W/scratch" "$ir" "$entry" "$ret" "$in" "$exp" "$@" 2>&1)" || true
117 GA_RCPT+="$label inputs=$(ga_sha "$in") expected=$(ga_sha "$exp") $v"$'\n'
118 case "$v" in "ACCEPT n="*) return 0;; *) GA_ERR="$label check failed: $v"; return 1;; esac
119}
120
121# ga_abs_checks <id> <helper body> <spec-like file: desc pre post example limit counter lines> <work prefix>
122# Runs every check of an abstraction and leaves the receipt text in GA_RCPT. Returns 1 with GA_ERR on the first
123# failure. The receipt holds the exact stated lines, so any edit of pre/post/example/counter changes it.
124ga_abs_checks() {
125 local id="$1" body="$2" spec="$3" w="$4" pre post l
126 GA_RCPT="gen-receipt 1"$'\n'"record $id"$'\n'"$(ga_tool_lines)"$'\n'
127 pre="$(sed -n 's/^pre //p' "$spec")" post="$(sed -n 's/^post //p' "$spec")"
128 ga_chain "$body" "$w.h" || return 1
129 GA_RCPT+="helper_ir $(ga_sha "$w.h.ir")"$'\n'"checker2 ACCEPT"$'\n'
130 GA_RCPT+="$(grep -E '^(pre|post|example|counter) ' "$spec")"$'\n'
131 ga_wrappers "$body" "$pre" "$post" "$w.wrap.omegese" || return 1
132 ga_chain "$w.wrap.omegese" "$w.wrap" || { GA_ERR="wrapper program: $GA_ERR (pre or post is not a valid bool value)"; return 1; }
133 local ret; ret="$(ga_req "$GA_RET")"
134 # property: chk on generated inputs must be true everywhere
135 "$GA_GABS" inputs "$GA_PROPERTY_N" $GA_FLATT > "$w.pin" || { GA_ERR="gabs inputs failed"; return 1; }
136 local i; for ((i = 0; i < GA_PROPERTY_N; i++)); do echo 1; done > "$w.pexp"
137 ga_run "property" "$w.wrap.ir" gw_chk bool "$w.pin" "$w.pexp" $GA_FLATT || return 1
138 # the stated example
139 l="$(sed -n 's/^example //p' "$spec")"
140 printf '%s\n' "${l% -> *}" > "$w.ein"; printf '%s\n' "${l##* -> }" > "$w.eexp"
141 ga_run "example" "$w.wrap.ir" gw_call "$ret" "$w.ein" "$w.eexp" $GA_FLATT || return 1
142 # every counterexample: pre is false there, the helper gives the stated result, and post is false for it
143 local k=0
144 while IFS= read -r l; do
145 k=$((k + 1))
146 printf '%s\n' "${l% -> *}" > "$w.cin"
147 printf '0\n' > "$w.c0"
148 ga_run "counter$k-pre-false" "$w.wrap.ir" gw_pre bool "$w.cin" "$w.c0" $GA_FLATT || return 1
149 printf '%s\n' "${l##* -> }" > "$w.cexp"
150 ga_run "counter$k-result" "$w.wrap.ir" gw_call "$ret" "$w.cin" "$w.cexp" $GA_FLATT || return 1
151 case "${l##* -> }" in
152 TRAP*) ;;
153 *) ga_run "counter$k-post-false" "$w.wrap.ir" gw_post bool "$w.cin" "$w.c0" $GA_FLATT || return 1 ;;
154 esac
155 done < <(sed -n 's/^counter //p' "$spec")
156 return 0
157}
158
159# ga_raw_checks <id> <task dir> <work prefix> -- reader chain, x445 check against the requirement, the harness
160# runner on the task's public tests, and the harness runner against the independent C oracle (corpus/oracle.c)
161# on GA_PROPERTY_N generated inputs (`gabs inputs`, the loop count bounded by `oracle --bound`), so a solution
162# that only fits the hand-picked public inputs is not verified. Receipt text in GA_RCPT.
163ga_raw_checks() {
164 local id="$1" d="$2" w="$3" out t
165 GA_RCPT="gen-receipt 1"$'\n'"record $id"$'\n'"$(ga_tool_lines)"$'\n'
166 GA_RCPT+="oracle $GA_CORPUS/oracle.c $(ga_sha "$GA_ROOT/$GA_CORPUS/oracle.c")"$'\n'
167 ga_chain "$d/solution.omegese" "$w.s" || return 1
168 GA_RCPT+="solution_ir $(ga_sha "$w.s.ir")"$'\n'"checker2 ACCEPT"$'\n'
169 out="$("$GA_X445" check oscir "$w.s.ir" "$d/requirement" 2>&1)" || { GA_ERR="x445 check: $out"; return 1; }
170 GA_RCPT+="x445 check oscir $out"$'\n'
171 local p ret entry bound
172 p="$(sed -n 's/^params//p' "$d/requirement")" ret="$(sed -n 's/^ret //p' "$d/requirement")"
173 entry="$(sed -n 's/^entry //p' "$d/requirement")" t="$(basename "$d")"
174 ga_run "tests" "$w.s.ir" "$entry" "$ret" "$d/inputs" "$d/expected" $p || return 1
175 bound="$("$GA_ORACLE" --bound "$t")" || { GA_ERR="oracle has no task $t"; return 1; }
176 "$GA_GABS" inputs "$GA_PROPERTY_N" ${bound:+--max "$bound"} $p > "$w.oin" || { GA_ERR="gabs inputs failed"; return 1; }
177 "$GA_ORACLE" "$t" < "$w.oin" > "$w.oexp" || { GA_ERR="oracle failed on the generated inputs"; return 1; }
178 ga_run "oracle${bound:+ bound=$bound}" "$w.s.ir" "$entry" "$ret" "$w.oin" "$w.oexp" $p || return 1
179 return 0
180}
181
182# ga_scan -- every corpus task: solution digest, whole-solution fingerprint, and each helper (all functions but
183# the last). Writes $GA_W/scan (sorted):
184# S <task> <split corpus|probe> <solution sha256> <solution fingerprint>
185# H <task> <k> <name> <calls|-> <helper fingerprint|-> <helper file> (fingerprint "-" for a non-leaf helper)
186# "split" here is the record meaning (RECORD_SCHEMA.md section 2 row 4): probe when the task is named in
187# corpus/PROBES. It is not the FAMSIG meaning of the `split` column of corpus/META, where every corpus task,
188# probe or not, is `corpus` (the side of the dev/corpus family rule).
189ga_scan() {
190 local root="$GA_ROOT/$GA_CORPUS" t d split fns line k name calls n fp
191 : > "$GA_W/scan.u"
192 for d in "$root"/tasks/*/; do
193 t="$(basename "$d")"
194 split=corpus; awk -v t="$t" '$1 == t { f = 1 } END { exit !f }' "$root/PROBES" && split=probe
195 fp="$(ga_fp "$d/solution.omegese" "$GA_W/sol.$t")" || { echo "genabs: corpus solution $t does not pass the reader chain" >&2; return 1; }
196 echo "S $t $split $(ga_sha "$d/solution.omegese") $fp" >> "$GA_W/scan.u"
197 fns="$("$GA_GABS" split "$d/solution.omegese" "$GA_W/fn.$t")" || { echo "genabs: split $t failed" >&2; return 1; }
198 n="$(printf '%s\n' "$fns" | wc -l)"
199 while read -r _ k name calls _; do
200 [ "$k" -lt $((n - 1)) ] || continue
201 fp=-
202 [ "$calls" != - ] || fp="$(ga_fp "$GA_W/fn.$t.$k.omegese" "$GA_W/hfp.$t.$k")" || { echo "genabs: helper $t/$name does not pass the reader chain" >&2; return 1; }
203 echo "H $t $k $name $calls $fp $GA_W/fn.$t.$k.omegese" >> "$GA_W/scan.u"
204 done <<< "$fns"
205 done
206 sort "$GA_W/scan.u" > "$GA_W/scan"
207}
208
209# ga_meta_token <task> -- family token of the task's META shape (famsig token; the hash is not reimplemented)
210ga_meta_token() {
211 local shape; shape="$(awk -v t="$1" '$1 == t { print $9 }' "$GA_ROOT/$GA_CORPUS/META")"
212 [ -n "$shape" ] || { GA_ERR="no META line for $1"; return 1; }
213 "$GA_FAMSIG" token "$shape"
214}