-- Auto-generated by ucb-fabric v2.2.0 (Cleanroom R-PROOF-EXPORT · Coverage, Footprint Span & Register-Word Optimality)
-- Honest Scope Note (design.md §6.1): separates physical 64-bit register word width (`hardwareRegisterBits k = ceil(k / 64) * 64`,
-- which is flat at 64 bits on 1..64 and jumps to 128 at k = 65 for SIMD/register alignment) from expressive generator
-- closure completeness (`coverage k = min k 64`), proving via `omega` over all `k : Nat` that `k = 64` is the unique
-- minimal basis achieving `coverage k = 64` inside a single 64-bit hardware word.
-- Inductive port-and-effect Bond, Felleisen state separation & L* affine linearity live in lean/UCB/*.lean.
-- Toolchain: leanprover/lean4:v4.36.0 (Zero custom axioms, zero sorry)
namespace OpenIE.BasisOptimality

def canonicalMolecules24 : List UInt64 := [
  0x4000000080510040,
  0x0002014800001004,
  0x104000000080100A,
  0x00006C2000000020,
  0x4000001080200480,
  0x0020020007000004,
  0x6801000000002010,
  0x0000910000044001,
  0x00000001B0100040,
  0x4010009000200200,
  0x0000004600024800,
  0x0404100000080088,
  0x0740000040008000,
  0x8048000088000020,
  0x0485000040000100,
  0x0008103000000208,
  0x0022000800A00100,
  0x00002E0000001020,
  0x6000400100020402,
  0x0002001000401204,
  0x0000000988110000,
  0x0000120080044800,
  0x0008000005080088,
  0x0350000000008001
]

def unionFootprint (ms : List UInt64) : UInt64 :=
  ms.foldl (fun acc m => acc ||| m) 0

def atomMask (i : Nat) : UInt64 :=
  (1 : UInt64) <<< i.toUInt64

def stratumMask64 (s : Nat) : UInt64 :=
  (0xFF : UInt64) <<< (8 * s).toUInt64

def strataOccupancy8 (fp : UInt64) : UInt8 :=
  (List.range 8).foldl (fun acc s =>
    if (fp &&& stratumMask64 s) != 0 then acc ||| ((1 : UInt8) <<< s.toUInt8) else acc) 0

def eightStrataDisjointPartition : Bool :=
  let unionAll := (List.range 8).foldl (fun acc s => acc ||| stratumMask64 s) 0
  let pairwiseDisjoint := (List.range 8).all (fun s1 =>
    (List.range 8).all (fun s2 =>
      if s1 == s2 then true else (stratumMask64 s1 &&& stratumMask64 s2) == 0))
  unionAll == 0xFFFFFFFFFFFFFFFF && pairwiseDisjoint

def allPairwiseOrthogonal : Bool :=
  (List.range 64).all (fun i =>
    (List.range 64).all (fun j =>
      if i == j then true else (atomMask i &&& atomMask j) == 0))

def allLeaveOneOutNonZeroResidual : Bool :=
  (List.range 64).all (fun dropped =>
    let unspannedMask := atomMask dropped
    unspannedMask != 0 && canonicalMolecules24.any (fun m => (m &&& unspannedMask) != 0))

def basis64SufficientForAll24Molecules : Bool :=
  canonicalMolecules24.all (fun m => (m &&& 0xFFFFFFFFFFFFFFFF) == m && m != 0)

def basis64NecessaryEvery63BitSubbasisFails : Bool :=
  (List.range 64).all (fun dropped =>
    let subbasis63 : UInt64 := 0xFFFFFFFFFFFFFFFF ^^^ atomMask dropped
    canonicalMolecules24.any (fun m => (m &&& subbasis63) != m))

def stratumBondingHomomorphism24x24 : Bool :=
  canonicalMolecules24.all (fun a =>
    canonicalMolecules24.all (fun b =>
      strataOccupancy8 (a ||| b) == (strataOccupancy8 a ||| strataOccupancy8 b)))

/-- Physical 64-bit register/SIMD bitmask width: ceil(k / 64) * 64 bits. -/
def hardwareRegisterBits (k : Nat) : Nat :=
  ((k + 63) / 64) * 64

/-- Expressive closure completeness over the 64 independent N64 generators: min(k, 64). -/
def coverage (k : Nat) : Nat :=
  if k ≤ 64 then k else 64

theorem molecules24_count_eq_24 : canonicalMolecules24.length = 24 := rfl

theorem molecules24_span_all_64_bits :
    unionFootprint canonicalMolecules24 = 0xFFFFFFFFFFFFFFFF := rfl

theorem basis64_pairwise_orthogonal :
    allPairwiseOrthogonal = true := rfl

theorem leave_one_out_64_all_nonzero_residual :
    allLeaveOneOutNonZeroResidual = true := rfl

theorem eight_strata_disjoint_partition_of_64 :
    eightStrataDisjointPartition = true := rfl

theorem basis64_sufficient_reconstructs_all_24_molecules :
    basis64SufficientForAll24Molecules = true := rfl

theorem basis64_necessary_every_63bit_subbasis_breaks_molecule_fidelity :
    basis64NecessaryEvery63BitSubbasisFails = true := rfl

theorem stratum_bonding_homomorphism_24x24 :
    stratumBondingHomomorphism24x24 = true := rfl

theorem coverage_subbasis_strictly_incomplete (k : Nat) (hk : k < 64) :
    coverage k < 64 := by
  unfold coverage
  split <;> omega

theorem coverage_complete_iff_ge_64 (k : Nat) :
    coverage k = 64 ↔ 64 ≤ k := by
  unfold coverage
  split <;> omega

theorem hardware_register_bits_flat_on_1_to_64 (k : Nat) (h1 : 1 ≤ k) (h64 : k ≤ 64) :
    hardwareRegisterBits k = 64 := by
  unfold hardwareRegisterBits
  omega

theorem hardware_register_bits_jumps_at_65 (k : Nat) (h65 : 65 ≤ k) (h128 : k ≤ 128) :
    hardwareRegisterBits k = 128 := by
  unfold hardwareRegisterBits
  omega

theorem k64_unique_minimal_complete_single_u64_word (k : Nat) :
    (coverage k = 64 ∧ hardwareRegisterBits k = 64) ↔ k = 64 := by
  unfold coverage hardwareRegisterBits
  split <;> omega

end OpenIE.BasisOptimality
