Zulip Chat Archive

Stream: lean4

Topic: Proofs for List.diff


Marcus Rossel (Apr 01 2024 at 09:36):

The following function obviously terminates, right?

import Std.Data.List.Lemmas

opaque p : α  Bool

def f [DecidableEq α] [DecidableEq β] (l : List (α × β)) : List (α × β) :=
  let m := l.filter p
  if m.isEmpty then [] else f (l.diff m)

I encourage anyone to try to prove termination by l.length.

Hints

I found it surprisingly hard to prove this, as many theorems about List.diff require instances of DecidableEq, whereas the function itself only requires BEq. As a result, in my solution I ended up having to specify explicit instances for List.diff and List.erase. Is this just a hole in the API for List.diff, or is there some other problem here?

My Solution

François G. Dorais (Apr 01 2024 at 10:36):

std4#716


Last updated: May 02 2025 at 03:31 UTC