Zulip Chat Archive

Stream: lean4

Topic: lean is clever


Kenny Lau (Oct 21 2025 at 16:03):

I just had a "wow lean is clever" moment when it typechecked my binary search algorithm without me having to specify anything

Kenny Lau (Oct 21 2025 at 16:03):

it automatically figured out to use (hi, hi - lo) as the proof of termination

Aaron Liu (Oct 21 2025 at 16:48):

it just has a list of common termination measures that it tries

Asei Inoue (Oct 22 2025 at 00:36):

@Kenny Lau could you show us a code example?

Kenny Lau (Oct 22 2025 at 09:12):

@Asei Inoue

def binaryRecursion (lo hi : Nat) : Nat :=
  if hi  lo then 0
  else if lo + 1 = hi then 1
  else
    have mi := (lo + hi) / 2
    binaryRecursion lo mi + binaryRecursion mi hi

Asei Inoue (Oct 22 2025 at 09:16):

Thank you

François G. Dorais (Oct 23 2025 at 03:07):

Or avoid reinventing the wheel: Batteries.Data.Nat.Bisect

Kenny Lau (Oct 23 2025 at 06:44):

@François G. Dorais thanks for the pointer,

  1. for some reason the docgen doesn't work
  2. that's locating something using binary search, in particular it has to return the index, it cannot do a binary recursion like the one I've posted above

Joachim Breitner (Oct 24 2025 at 21:36):

Kenny Lau schrieb:

it automatically figured out to use (hi, hi - lo) as the proof of termination

See https://lean-lang.org/doc/reference/latest/Definitions/Recursive-Definitions/#inferring-well-founded-recursion for more details on how Lean managed to be clever

Kenny Lau (Oct 24 2025 at 21:37):

Thanks!


Last updated: Dec 20 2025 at 21:32 UTC