Lean4
/-- `permuteExpr op tgt instructions` takes the same input as `rankSums` and returns the
expression obtained from `tgt` by replacing all `old_sum`s by the corresponding `new_sum`.
If there were no required changes, then `permuteExpr` reports this in its second factor. -/
def permuteExpr (tgt : Expr) (instructions : List (Expr × Bool)) : MetaM Expr := do
let permInstructions ← rankSums op tgt instructions
if permInstructions == [] then
throwError"The goal is already in the required form"
let mut permTgt := tgt
for (old, new) in permInstructions do
permTgt := permTgt.replace (if · == old then new else none)
return permTgt