lean4-htt/tests/lean/decreasing_by.lean
Joachim Breitner b5122b6a7b feat: per-function termination hints
This change

 * moves `termination_by` and `decreasing_by` next to the function they
   apply to
 * simplify the syntax of `termination_by`
 * apply the `decreasing_by` goal to all goals at once, for better
   interactive use.

See the section in `RELEASES.md` for more details and migration advise.

This is a hard breaking change, requiring developers to touch every
`termination_by` in their code base. We decided to still do it as a
hard-breaking change, because supporting both old and new syntax at the
same time would be non-trivial, and not save that much. Moreover, this
requires changes to some metaprograms that developers might have
written, and supporting both syntaxes at the same time would make
_their_ migration harder.
2024-01-10 17:27:35 +01:00

138 lines
3 KiB
Text

/-!
Various tests about `decreasing_by`.
-/
-- For concise recursive definition that need well-founded recursion
-- and `decreasing_by` tactics that would fail if run on the subgoal
opaque dec1 : Nat → Nat
axiom dec1_lt (n : Nat) : dec1 n < n
opaque dec2 : Nat → Nat
axiom dec2_lt (n : Nat) : dec2 n < n
def simple (n : Nat) := n + simple (dec1 n)
decreasing_by apply dec1_lt
namespace Ex1
-- Multiple goals, explicit termination_By
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100
termination_by (n, m)
decreasing_by
· simp_wf
apply Prod.Lex.right
apply dec2_lt
· simp_wf
apply Prod.Lex.left
apply dec1_lt
end Ex1
namespace Ex2
-- Multiple goals, no termination_By
-- This does *not* work, because GuessLex does not pass multiple goals to the tactic.
-- so this tactic script fails.
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100 -- Error
decreasing_by
· simp_wf
apply Prod.Lex.right
apply dec2_lt
· simp_wf
apply Prod.Lex.left
apply dec1_lt
end Ex2
namespace Ex3
-- Using `all_goals`, explicit termination_By
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100
termination_by (n, m)
decreasing_by all_goals
simp_wf
first
| apply Prod.Lex.right
apply dec2_lt
| apply Prod.Lex.left
apply dec1_lt
end Ex3
namespace Ex4
-- Multiple goals, no termination_By
-- This does work, because the tactic is flexible enough
-- (Not a recommended way; complex `decrasing_by` should go along with `termination_by`.)
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100 -- Error
decreasing_by all_goals
simp_wf
first
| apply Prod.Lex.right
apply dec2_lt
| apply Prod.Lex.left
apply dec1_lt
end Ex4
namespace Ex5
-- Empty proof. Produces parse error and unsolved goals.
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100
termination_by n m => (n, m)
decreasing_by -- Error
end Ex5
namespace Ex6
-- Incomplete tactic
-- Unsolved goals reported
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100
termination_by (n, m)
decreasing_by apply id -- Error
end Ex6
namespace Ex7
-- Incomplete tactic, no termination_by
-- Shows guess-lex matrix
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100 -- Error
decreasing_by apply id
end Ex7
namespace Ex8
-- tactic solving just one goal
-- unsolved goals
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100
termination_by (n, m)
decreasing_by -- Error
· simp_wf
apply Prod.Lex.right
apply dec2_lt
end Ex8
namespace Ex9
-- Incomplete tactic, no termination_by
-- Shows guess-lex matrix
def foo (n m : Nat) : Nat := foo n (dec2 m) + foo (dec1 n) 100 -- Error
decreasing_by
· simp_wf
apply Prod.Lex.right
apply dec2_lt
end Ex9
namespace Ex10
-- This checks that guess-lex does not run tactics in “recover” mode.
-- (If it would it would produce the wrong termination order and then we should see errors)
def foo (n m : Nat) : Nat := foo (n - 1) (dec2 m)
decreasing_by all_goals
· simp_wf
apply dec2_lt
end Ex10