lean4-htt/tests/lean/run/860.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

31 lines
991 B
Text

def evenq (n: Nat) : Bool := Nat.mod n 2 = 0
private theorem pack_loop_terminates : (n : Nat) → n / 2 < n.succ
| 0 => by decide
| 1 => by decide
| n+2 => by
rw [Nat.div_eq]
split
· rw [Nat.add_sub_self_right]
have := pack_loop_terminates n
calc n/2 + 1 < Nat.succ n + 1 := Nat.add_le_add_right this 1
_ < Nat.succ (n + 2) := Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.lt_succ_self _))
· apply Nat.zero_lt_succ
def pack (n: Nat) : List Nat :=
let rec
loop (n : Nat) (acc : Nat) (accs: List Nat) : List Nat :=
let next (n: Nat) := n / 2;
match n with
| Nat.zero => List.cons acc accs
| n+1 => match evenq n with
| true => loop (next n) 0 (List.cons acc accs)
| false => loop (next n) (acc+1) accs
termination_by n
decreasing_by all_goals
simp [invImage, InvImage, Prod.lex, sizeOfWFRel, measure, Nat.lt_wfRel]
apply pack_loop_terminates
loop n 0 []
#eval pack 27