/-! # RNAcube: machine-checked uniqueness theorem and bit-flip lemma Bases are indexed U = 0, C = 1, A = 2, G = 3. The standard genetic code is given as a list of 64 meaning codes (21 meanings, stop included), indexed by the standard address 4*b1 + 16*b2 + b3. -/ set_option maxRecDepth 100000 def code : List Nat := [5, 5, 10, 10, 10, 10, 10, 10, 8, 8, 8, 11, 18, 18, 18, 18, 16, 16, 16, 16, 13, 13, 13, 13, 17, 17, 17, 17, 1, 1, 1, 1, 20, 20, 0, 0, 7, 7, 14, 14, 12, 12, 9, 9, 3, 3, 4, 4, 2, 2, 0, 19, 15, 15, 15, 15, 16, 16, 15, 15, 6, 6, 6, 6] def stdAA (i : Nat) : Nat := code.getD i 99 theorem stop_code : stdAA 34 = 0 := by decide +kernel /-- All permutations of a list. -/ def perms : List Nat → List (List Nat) | [] => [[]] | x :: xs => (perms xs).flatMap fun p => (List.range (p.length + 1)).map fun i => p.take i ++ x :: p.drop i /-- The 24 labelings: L[b] is the value given to base b. -/ def labelings : List (List Nat) := perms [0, 1, 2, 3] /-- The 6 orders of the weights {1, 4, 16} on (pos1, pos2, pos3). -/ def weightOrders : List (Nat × Nat × Nat) := [(4, 16, 1), (16, 4, 1), (1, 4, 16), (1, 16, 4), (4, 1, 16), (16, 1, 4)] /-- Position of x in L (portable replacement for List.indexOf, renamed idxOf in newer Lean). -/ def idx (L : List Nat) (x : Nat) : Nat := match L with | [] => 0 | y :: ys => if y = x then 0 else idx ys x + 1 /-- Meaning at address a under weights w and labeling L. -/ def meaningAt (L : List Nat) (w : Nat × Nat × Nat) (a : Nat) : Nat := let (w1, w2, w3) := w let b1 := idx L ((a / w1) % 4) let b2 := idx L ((a / w2) % 4) let b3 := idx L ((a / w3) % 4) stdAA (4 * b1 + 16 * b2 + b3) /-- Stop is meaning code 0. -/ def stop : Nat := 0 /-- Fragments: the number of unbroken runs into which the 20 amino-acid families break along addresses 0..63 (stop excluded; a run starts wherever an amino acid differs from the meaning one address below). -/ def frags (L : List Nat) (w : Nat × Nat × Nat) : Nat := ((List.range 64).filter fun i => meaningAt L w i != stop && (i == 0 || meaningAt L w i != meaningAt L w (i - 1))).length def frameworks : List ((Nat × Nat × Nat) × List Nat) := weightOrders.flatMap fun w => labelings.map fun L => (w, L) theorem frameworks_count : frameworks.length = 144 := by decide +kernel /-- Premise (ii): no framework scores below 22. -/ theorem min_is_22 : frameworks.all (fun f => decide (frags f.2 f.1 ≥ 22)) = true := by decide +kernel /-- Exactly two frameworks reach 22: UCAG and its mirror GACU, both on [4, 16, 1]. -/ theorem only_two : (frameworks.filter (fun f => frags f.2 f.1 == 22)).length = 2 ∧ frags [0, 1, 2, 3] (4, 16, 1) = 22 ∧ frags [3, 2, 1, 0] (4, 16, 1) = 22 := by decide +kernel /-- Premise (iii): with U at 0, only UCAG on [4, 16, 1] remains. -/ theorem unique_with_U_zero : frameworks.filter (fun f => frags f.2 f.1 == 22 && f.2.head? == some 0) = [((4, 16, 1), [0, 1, 2, 3])] := by decide +kernel /-- Next best score is 23. -/ theorem next_best_23 : frameworks.all (fun f => frags f.2 f.1 == 22 || frags f.2 f.1 ≥ 23) = true ∧ frameworks.any (fun f => frags f.2 f.1 == 23) = true := by decide +kernel /-! ## Bit-flip lemma (in the UCAG [4,16,1] frame, address bits = physical bits) -/ def flipKeep (g : Nat) : Nat := ((List.range 64).filter fun c => stdAA c == stdAA (c ^^^ g)).length theorem lemma_wobble : flipKeep 1 = 60 := by decide +kernel theorem lemma_others : (List.range 62).all (fun i => decide (flipKeep (i + 2) ≤ 34)) = true := by decide +kernel /-! ## Premise (i): a one-to-one weighted address onto 0..63 forces {1, 4, 16} -/ def addrs (w1 w2 w3 : Nat) : List Nat := (List.range 4).flatMap fun d1 => (List.range 4).flatMap fun d2 => (List.range 4).map fun d3 => w1 * d1 + w2 * d2 + w3 * d3 /-- The 64 addresses are exactly 0..63. -/ def onto64 (w1 w2 w3 : Nat) : Bool := (addrs w1 w2 w3).all (· < 64) && (List.range 64).all fun a => (addrs w1 w2 w3).contains a def isPerm1416 (w1 w2 w3 : Nat) : Bool := [(1,4,16),(1,16,4),(4,1,16),(4,16,1),(16,1,4),(16,4,1)].contains (w1, w2, w3) theorem weights_bounded (w1 w2 w3 : Nat) (h : onto64 w1 w2 w3 = true) : w1 ≤ 21 ∧ w2 ≤ 21 ∧ w3 ≤ 21 := by simp only [onto64, Bool.and_eq_true, List.all_eq_true] at h have h1 := h.1 (w1 * 3) (by simp [addrs]; exact ⟨3, by omega, 0, by omega, 0, by omega, by omega⟩) have h2 := h.1 (w2 * 3) (by simp [addrs]; exact ⟨0, by omega, 3, by omega, 0, by omega, by omega⟩) have h3 := h.1 (w3 * 3) (by simp [addrs]; exact ⟨0, by omega, 0, by omega, 3, by omega, by omega⟩) simp at h1 h2 h3 omega def boundedCheck : Bool := (List.range 22).all fun w1 => (List.range 22).all fun w2 => (List.range 22).all fun w3 => onto64 w1 w2 w3 == isPerm1416 w1 w2 w3 theorem bounded_check : boundedCheck = true := by native_decide theorem premise_i (w1 w2 w3 : Nat) (h : onto64 w1 w2 w3 = true) : isPerm1416 w1 w2 w3 = true := by have hb := weights_bounded w1 w2 w3 h have hc := bounded_check simp only [boundedCheck, List.all_eq_true, List.mem_range, beq_iff_eq] at hc rw [← hc w1 (by omega) w2 (by omega) w3 (by omega)] exact h #print axioms min_is_22 #print axioms unique_with_U_zero #print axioms lemma_others #print axioms premise_i /-! ## Visible results (place the cursor on each line to see its value) -/ #eval frags [0, 1, 2, 3] (4, 16, 1) -- 22: UCAG on [4, 16, 1] #eval (frameworks.map (fun f => frags f.2 f.1)).foldl min 99 -- 22: the minimum over all 144 #eval (frameworks.filter (fun f => frags f.2 f.1 == 22)) -- the only two: UCAG and GACU on [4, 16, 1] #eval ((frameworks.map (fun f => frags f.2 f.1)).filter (· > 22)).foldl min 99 -- 23: next best #eval flipKeep 1 -- 60: the wobble flip #eval (List.range 62).map (fun i => flipKeep (i + 2)) -- every other flip, all ≤ 34