Zulip Chat Archive

Stream: lean4

Topic: import mathlib breaks existing proofs


Jonathan Protzenko (Jan 31 2023 at 21:38):

I have the following definition (simplified):

def USize.checked_rem (n: USize) (m: USize): Option USize :=
  if h: m > 0 then
    .some  n.val % m.val, by
      have h1: m.val < USize.size := m.val.isLt
      have h2: n.val.val % m.val.val < m.val.val := @Nat.mod_lt n.val m.val h
      apply Nat.lt_trans h2 h1
    
  else
    .none

works fine, except if I add import Mathlib at the top of my file, my proof no longer works, and I get:

type mismatch
  m.val.isLt
has type
  m.val < size : Prop
but is expected to have type
  m.val < size : Prop

is there any advice on how to debug this? the reason for importing mathlib was to get the benefits of linarith

Mario Carneiro (Jan 31 2023 at 21:39):

you should mouseover the up arrows to see what the types are

Eric Wieser (Jan 31 2023 at 21:41):

I think the problem is that m.val < USize.size is not well-typed, so Lean tried inserting a coercion first on the right, and if that failed, on the left

Jonathan Protzenko (Jan 31 2023 at 21:42):

ok, there was an implicit .val that Lean was adding

Jonathan Protzenko (Jan 31 2023 at 21:42):

and somehow, import Mathlib made it fail... the fix was to write instead have h1: m.val.val

Jonathan Protzenko (Jan 31 2023 at 21:42):

(and leave the rest unchanged)

Eric Wieser (Jan 31 2023 at 21:42):

If you have mathlib imported, there is a coercion from Nat to Fin n that lean picks before it tries the coercion from Fin n to Nat

Jonathan Protzenko (Jan 31 2023 at 21:43):

ah! that explains a lot... thanks so much

Eric Wieser (Jan 31 2023 at 21:43):

have h1: ↑m.val < USize.size := m.val.isLt or even have h1 := m.val.isLt would be nicer than .val.val

Jonathan Protzenko (Jan 31 2023 at 21:43):

I'm not familiar with the \uparrow notation... need to read about it!

Jonathan Protzenko (Jan 31 2023 at 21:45):

ok that's a nice notation for coercions... and indeed it makes things more pleasant. thanks!


Last updated: Dec 20 2023 at 11:08 UTC