Lean4
/-- Reorder pi-binders. See doc of `reorderAttr` for the interpretation of the argument -/
def reorderForall (reorder : List (List Nat)) (src : Expr) : MetaM Expr := do
if let some maxReorder := reorder.flatten.max? then
forallBoundedTelescope src (some (maxReorder + 1)) fun xs e => do
if xs.size = maxReorder + 1 then
mkForallFVars (xs.permute! reorder) e
else
throwError "the permutation\n{reorder}\nprovided by the `(reorder := ...)` option is \
out of bounds, the type{(indentExpr src)}\nhas only {xs.size} arguments"
else
return src