Files
conflict-range/cpp/oracle/gen.lean
T
andrew 273ad72f0c Polish release docs and add CI checks
- Correct stale references and headline-path wording in READMEs and Lean docstrings
- Document build steps, comparator requirements, sweep scope, and coverage
- Unify Apache headers across Lean and C++ sources
- Add a bwrap-free axiom audit for the headline theorems
- Add a Gitea workflow running lake build, the axiom audit, C++ tests, and coverage
- Match LLVM coverage tool versions to the compiler and recreate stale build dirs
2026-11-17 21:29:16 -05:00

129 lines
3.9 KiB
Lean4
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/-
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: FormalConflictRange contributors
-/
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)