- Literal transliteration of `Statement.closedForm` plus an equivalent summarised implementation - Borrowed input keys (`std::string_view` / `std::span`) and owned result keys (`std::string`) - Golden-vector test, differential/safety fuzzers, and a nanobench comparison The literal and summarised implementations agree on 40,000 Lean golden vectors, 18,971 sorted corpus cases, and the readable fuzzer; the raw fuzzer runs both implementations under ASan/UBSan. `build/` and the generated `oracle/golden.txt` are ignored.
123 lines
3.7 KiB
Lean4
123 lines
3.7 KiB
Lean4
import Statement
|
||
|
||
/-!
|
||
Deterministic golden-vector generator for the C++ translation of
|
||
`Statement.closedForm`.
|
||
|
||
Run with:
|
||
|
||
lake env lean --run cpp/oracle/gen.lean > cpp/oracle/golden.txt
|
||
|
||
Each output line is
|
||
|
||
<beginKey> <beginOffset> <endKey> <endOffset> <reverse> <more> <n> <key>... => <outBegin> <outEnd>
|
||
|
||
where a key is lowercase hex, `-` is the empty key, and `T` is `⊤`.
|
||
-/
|
||
|
||
open Statement
|
||
|
||
def hexDigit (n : Nat) : Char :=
|
||
if n < 10 then Char.ofNat (48 + n) else Char.ofNat (87 + n)
|
||
|
||
def hexByte (b : Byte) : String :=
|
||
let n := b.val
|
||
String.ofList [hexDigit (n / 16), hexDigit (n % 16)]
|
||
|
||
def encKey : WithTop Key → String
|
||
| none => "T"
|
||
| some k => if k.isEmpty then "-" else String.join (k.map hexByte)
|
||
|
||
def encRes (res : Result) : String :=
|
||
let more := if res.more then "1" else "0"
|
||
let ks := res.keys.map (fun k => if k.isEmpty then "-" else String.join (k.map hexByte))
|
||
more ++ " " ++ toString res.keys.length ++ " " ++ String.intercalate " " ks
|
||
|
||
def encCase (r : RangeRead) (res : Result) : String :=
|
||
let out := closedForm r res
|
||
encKey r.begin.key ++ " " ++ toString r.begin.offset ++ " " ++
|
||
encKey r.end_.key ++ " " ++ toString r.end_.offset ++ " " ++
|
||
(if r.reverse then "1" else "0") ++ " " ++ encRes res ++ " => " ++
|
||
encKey out.begin ++ " " ++ encKey out.end_
|
||
|
||
def key (xs : List Nat) : Key := xs.map (fun n => Fin.ofNat 256 n)
|
||
|
||
/-- Keys chosen to exercise prefixes, zero bytes, ordering, and the empty key. -/
|
||
def keyUniverse : List Key :=
|
||
[ key [], key [0], key [0, 0], key [0, 1], key [0, 2], key [1], key [1, 0],
|
||
key [1, 1], key [2], key [0, 0, 0], key [255], key [0, 1, 0], key [3, 0] ]
|
||
|
||
def keyChoices : List (WithTop Key) :=
|
||
(⊤ : WithTop Key) :: keyUniverse.map fun k => (k : WithTop Key)
|
||
|
||
def offsets : List Int :=
|
||
[-256, -10, -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 10, 100, 255, 256]
|
||
|
||
def nth {α : Type} (l : List α) (i : Nat) (d : α) : α :=
|
||
l.getD (i % l.length) d
|
||
|
||
def step (s : Nat) : Nat := (s * 1103515245 + 12345) % 2147483648
|
||
|
||
/-- A weighted byte: the boundary values are common, the rest is uniform. -/
|
||
def weightedByte (s : Nat) : Byte :=
|
||
let r := step s % 256
|
||
if r < 24 then 0
|
||
else if r < 40 then 1
|
||
else if r < 56 then 2
|
||
else if r < 72 then 255
|
||
else Fin.ofNat 256 r
|
||
|
||
/-- A random key of length `0..8` over all 256 byte values. -/
|
||
def randomKey (s : Nat) : Key :=
|
||
let len := s % 9
|
||
(List.range len).map fun i => weightedByte (s + i)
|
||
|
||
structure Gen where
|
||
r : RangeRead
|
||
res : Result
|
||
|
||
def mkCase (s : Nat) : Gen :=
|
||
let s1 := step s
|
||
let s2 := step s1
|
||
let s3 := step s2
|
||
let s4 := step s3
|
||
let s5 := step s4
|
||
let s6 := step s5
|
||
let s7 := step s6
|
||
let r : RangeRead :=
|
||
{ begin :=
|
||
{ key := nth keyChoices s1 (⊤ : WithTop Key)
|
||
offset := nth offsets s2 0 }
|
||
end_ :=
|
||
{ key := nth keyChoices s3 (⊤ : WithTop Key)
|
||
offset := nth offsets s4 0 }
|
||
reverse := s5 % 2 = 1 }
|
||
let mode := s6 % 6
|
||
let resKeys : List Key :=
|
||
match mode with
|
||
| 0 => []
|
||
| 1 => [randomKey s7]
|
||
| 2 =>
|
||
let start := s7 % keyUniverse.length
|
||
(keyUniverse.drop start).take (1 + (step s7) % 4)
|
||
| 3 =>
|
||
let start := s7 % keyUniverse.length
|
||
((keyUniverse.drop start).take (1 + (step s7) % 4)).reverse
|
||
| 4 =>
|
||
[randomKey s7, randomKey (step s7), randomKey (step (step s7))]
|
||
| _ =>
|
||
let a := randomKey s7
|
||
[a, a, randomKey (step s7)]
|
||
let more := s6 % 3 = 0
|
||
{ r := r, res := { keys := resKeys, more := more } }
|
||
|
||
def main : IO Unit := do
|
||
let n := 40000
|
||
let mut s := 1234567
|
||
let mut out : Array String := #[]
|
||
for _ in List.range n do
|
||
s := step s
|
||
let g := mkCase s
|
||
out := out.push (encCase g.r g.res)
|
||
IO.println (String.intercalate "\n" out.toList)
|