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 / trit / proof / difftest.sh

71 lines · 3416 bytes Raw Blame History
1#!/bin/sh
2# difftest.sh -- Lean model (TritSignal.lean, namespace TritBin) vs trit.bin on N generated cases.
3# Usage: BIN=... N=600 SEED=1 sh tools/trit/proof/difftest.sh (needs python3 and lean)
4# Both sides read the same fixture directories; stdout, stderr and exit status must match exactly.
5set -eu
6HERE=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
7BIN=${BIN:-build/trit/trit}; case $BIN in /*) ;; *) BIN=$(pwd)/$BIN ;; esac
8N=${N:-600}; SEED=${SEED:-1}; LEAN=${LEAN:-lean}
9W=${W:-${TMPDIR:-/tmp}/trit-diff.$$}
10rm -rf "$W"; mkdir -p "$W/cases"
11python3 "$HERE/gen.py" "$W/cases" "$N" "$SEED" "$HERE/../../codebook/codebook.tsv"
12i=0
13: > "$W/bin.out"
14while [ "$i" -lt "$N" ]; do
15 D="$W/cases/$i"
16 set +e; "$BIN" "$D" a > "$W/o" 2> "$W/e"; rc=$?; set -e
17 printf '%s\t%s\t%s\n' "$rc" "$(awk '{printf "%s\\n", $0}' "$W/o")" "$(awk '{printf "%s\\n", $0}' "$W/e")" >> "$W/bin.out"
18 i=$((i+1))
19done
20{ cat "$HERE/TritSignal.lean"; cat <<LEAN
21
22def readOpt (p : System.FilePath) : IO (Option (List Char)) := do
23 if (← p.pathExists) then
24 let b ← IO.FS.readBinFile p
25 return some (b.data.toList.map fun x => Char.ofNat x.toNat)
26 else return none
27
28def escNl (s : String) : String := String.join (s.toList.map fun c => if c == '\n' then "\\\\n" else String.singleton c)
29
30def runDir (d : System.FilePath) : IO String := do
31 let inbox := d / "inbox"
32 let ents ← inbox.readDir
33 let ents := ents.toList
34 let allow := ents.filterMap fun e =>
35 let f := e.fileName
36 if f.endsWith ".md" then some (String.ofList (f.toList.take (f.length - 3))) else none
37 let cbf ← readOpt (d / "codebook" / "codebook.tsv")
38 let kf ← readOpt (d / "codebook" / "key")
39 let codesOf (cb : List Char) : List String :=
40 (((String.ofList cb).splitOn "\n").filter (fun x => x != "" && !x.startsWith "#")).map (fun x => (x.splitOn "\t").headD "")
41 let vc : Option TritBin.VC := match cbf, kf with
42 | some cb, some k => some (TritBin.VC.mk (TritBin.sha256 cb) (codesOf cb) (TritBin.toB k))
43 | _, _ => none
44 let mut ctrs : List (String × List Char) := []
45 for e in ents do
46 let f := e.fileName
47 if f.startsWith "a." && f.endsWith ".ctr" then
48 if let some c ← readOpt e.path then ctrs := ctrs ++ [(String.ofList ((f.toList.drop 2).take (f.length - 6)), c)]
49 let fc : String → Nat × String := fun s => TritBin.rdctf (ctrs.lookup s)
50 let nm ← readOpt (inbox / "a.md")
51 let em ← readOpt (inbox / "a.emergency.md")
52 let nc ← readOpt (inbox / "a.md.cur")
53 let ec ← readOpt (inbox / "a.emergency.md.cur")
54 let (o, e, rc) := TritBin.runBot vc fc allow nm em nc ec
55 return toString rc ++ "\t" ++ escNl o ++ "\t" ++ escNl e
56
57#eval show IO Unit from do
58 for i in [0:$N] do
59 IO.println (← runDir ("$W/cases" / toString i))
60LEAN
61} > "$W/Diff.lean"
62"$LEAN" "$W/Diff.lean" | grep -v "axioms" > "$W/lean.out"
63if cmp -s "$W/bin.out" "$W/lean.out"; then
64 printf 'DIFF agree=%s/%s seed=%s (stdout, stderr, status) lean-sha256=%s\n' "$N" "$N" "$SEED" "$(sha256sum < "$W/lean.out" | cut -c1-16)"
65 awk -F'\t' '{c[$1]++; if ($2 ~ /^a 2/) t2++; else if ($2 ~ /^a 1/) t1++; if ($3 != "") r++} END {printf "MIX exit0=%d exit1=%d trit1=%d trit2=%d with-stderr=%d\n", c[0], c[1], t1, t2, r}' "$W/bin.out"
66else
67 n=$(diff "$W/bin.out" "$W/lean.out" | grep -c '^<' || true)
68 printf 'DIFF disagree=%s of %s seed=%s; see %s\n' "$n" "$N" "$SEED" "$W"
69 diff "$W/bin.out" "$W/lean.out" | head -20
70 exit 1
71fi