Zulip Chat Archive

Stream: mathlib4

Topic: Why doesn't this work for ℝ?


Markus Schmaus (Nov 09 2023 at 08:54):

I'm looking into formalizing the notion that a Float approximates a real number.

For some reason the following code works for , but for some reason it throws an error for .

import Mathlib

inductive IsApproxℚ : Float    Prop :=
  | ofNat (base : ) : IsApproxℚ (Float.ofNat base) base

example : IsApproxℚ (Float.ofNat 1) 1 := by
  apply IsApproxℚ.ofNat

inductive IsApproxℝ : Float    Prop :=
  | ofNat (base : ) : IsApproxℝ (Float.ofNat base) base

example : IsApproxℝ (Float.ofNat 1) 1 := by
  apply IsApproxℝ.ofNat

Why is that? Can I make this work somehow?

Markus Schmaus (Nov 09 2023 at 08:55):

Here is the error message:

tactic 'apply' failed, failed to unify
  IsApproxℝ (Float.ofNat ?base) ↑?base
with
  IsApproxℝ (Float.ofNat 1) 1
 IsApproxℝ (Float.ofNat 1) 1

Damiano Testa (Nov 09 2023 at 09:01):

If you try

  convert IsApproxℝ.ofNat 1

instead of apply, you will see what is the issue that Lean is facing. Essentially, you need to provide a proof that one 1 is equal to another 1!

Markus Schmaus (Nov 09 2023 at 09:16):

That's very useful. This results in two goals, one that 1 = ↑1 and another that is inhabited. Curiously, for 2 it's only necessary to show that is inhabited. Why is this only necessary for , but not for ?

import Mathlib

inductive IsApproxℝ : Float    Prop :=
  | ofNat (base : ) {exact : } : IsApproxℝ (Float.ofNat base) base

example : IsApproxℝ (Float.ofNat 1) 1 := by
  convert IsApproxℝ.ofNat 1
  simp only [Nat.cast_one]
  exact Inhabited.default

example : IsApproxℝ (Float.ofNat 2) 2 := by
  convert IsApproxℝ.ofNat 2
  exact Inhabited.default

Kevin Buzzard (Nov 09 2023 at 09:21):

Probably because in the case of the rationals the proof is rfl and in the case of the reals it's not. Your apply proof only works assuming that the two 1's are equal by definition, so whether it works in practice all depends on the underlying definitions of things, which is sort of random.


Last updated: Dec 20 2023 at 11:08 UTC