Transposing an n×m integer array stored on a stack
ProvedNLogN.FW.trWith_specLet be a list of integers on stack XD (, ), and registers (distinct from the scratch stacks 1,2,3) and KBR hold , , . If all entries and have bit length , the program trWith a b replaces by its transpose transposeL n m L (row-major to row-major ) within cost .
Formalization Note. The transpose is implemented by tagging every entry with its target index, a radix sort on the tags, and stripping the tags. transposeL is from Def_IntMul_HvdH_MachineRef. The machine layer: IState HS is a state with stacks of items (Item = integer or mark), an input position and an output; RM programs (straight-line register code RS, loops rep, conditionals, calls of the multiplication procedure through a MulSpec giving its domain and cost) have a functional semantics p.run s, a definedness predicate p.ok M s and a cost p.cost M s; Spec M p s s' c abbreviates p.ok M s ∧ p.run s = s' ∧ p.cost M s ≤ c. ints L / gints U push an integer list / a list of pairs as items (head = top); zsz z is the bit length of . MulBnd M m Φ says the multiplication procedure accepts all pairs of integers of bit length at cost . Register names (GU, CR, KB, XN, …) are fixed stack indices.
-- Harvey–van der Hoeven multitape-TM development (NLogN), generated by skeleton subtraction. import Definitions.Def_IntMul_HvdH_M_Defs4 set_option autoImplicit false set_option relaxedAutoImplicit false set_option maxHeartbeats 1000000 set_option linter.unusedSimpArgs false set_option linter.unusedVariables false open NLogN open NLogN.FW open IntMul IntMul.HvdH NLogN
theorem NLogN.FW.trWith_spec (M : MulSpec) (a b : Fin HS) (ha : a.val < 55) (hb : b.val < 55)
(ha1 : a ≠ 1) (ha2 : a ≠ 2) (hb1 : b ≠ 1) (hb2 : b ≠ 2) (ha3 : a ≠ 3) (hb3 : b ≠ 3)
(n m kb W : ℕ) (L : List ℤ) (Rest La Lb Lk : List Item) (s : IState HS)
(hL : L.length = n * m) (hN : n * m ≤ 2 ^ kb) (hm : 1 ≤ m)
(hX : s.G XD = ints L ++ Rest) (hA : s.G a = .int n :: La) (hB : s.G b = .int m :: Lb)
(hK : s.G KBR = .int kb :: Lk) (hW : Bnd W L) (hnW : zsz (n : ℤ) ≤ W) (hmW : zsz (m : ℤ) ≤ W)
(hkW : zsz (kb : ℤ) ≤ W) :
Spec M (trWith a b) s (s.upd XD (ints (Ref.transposeL n m L) ++ Rest))
(10 ^ 6 * (kb + 1) * (n * m + 1) * 2 * (W + kb + 5) + 20000 * (W + 2)) := by sorry