Zulip Chat Archive

Stream: mathlib4

Topic: Issue in !4#2702 (Topology.UniformSpace.CompareReals)


Arien Malec (Mar 07 2023 at 20:22):

What's the fix for this?:

synthesized type class instance is not definitionally equal to expression inferred by typing rules, synthesized
  PseudoMetricSpace.toUniformSpace
inferred
  AbsoluteValue.uniformSpace AbsoluteValue.abs

Maybe hide the mistaken instance?

Arien Malec (Mar 07 2023 at 20:29):

Nope:

attribute [-instance] PseudoMetricSpace.toUniformSpace
attribute [-instance] PseudoEMetricSpace.toUniformSpace

changes the message to 'failed to synthesize instance
UniformSpace ℚ'

Eric Wieser (Mar 07 2023 at 20:29):

Can you add a PR comment on the relevant line of !4#2702?

Arien Malec (Mar 07 2023 at 20:41):

Done

Arien Malec (Mar 07 2023 at 20:44):

import Mathlib.Topology.UniformSpace.AbstractCompletion
import Mathlib.Algebra.Order.AbsoluteValue
import Mathlib.Topology.UniformSpace.AbsoluteValue
import Mathlib.Topology.Instances.Rat

def rationalCauSeqPkg : @AbstractCompletion  <| (@AbsoluteValue.abs  _).uniformSpace
    where

demonstrates the issue

Eric Wieser (Mar 07 2023 at 20:45):

You can't use where for constructors relating to non-canonical instances

Eric Wieser (Mar 07 2023 at 20:46):

You have to use @AbstractCompletion.mk, and pass (_) as the instance argument

Eric Wieser (Mar 07 2023 at 20:48):

Lean3 was happy to use unification here, Lean4 will use typeclass search (and then complain that it doesn't unify), unless you force it to not be able to use typeclass search

Arien Malec (Mar 07 2023 at 21:09):

def rationalCauSeqPkg : AbstractCompletion  := @AbstractCompletion.mk
  (space := )
  (coe := (RatCast.ratCast :   ))
  (uniformStruct := by infer_instance)
  (complete := by infer_instance)
  (separation := by infer_instance)
  (uniformInducing := Rat.uniformEmbedding_coe_real.toUniformInducing)
  (dense := Rat.denseEmbedding_coe_real.dense)

Arien Malec (Mar 07 2023 at 21:10):

Wanted the instance named...

Eric Wieser (Mar 07 2023 at 21:35):

I think for now you just have to use underscores...

Arien Malec (Mar 07 2023 at 23:19):

Lean yells if you do that

failed to infer definition type
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed

Arien Malec (Mar 07 2023 at 23:26):

Now the def compiles, has the expected type, but Lean yells later on when it's used. I think it's an implicit issue, but can't figure out how to supply implicits with dotted notation.

Arien Malec (Mar 07 2023 at 23:52):

Got it, and it doesn't help. This is the exact analogue of the type on the Lean 3 side:

noncomputable def compareEquiv : Bourbakiℝ ≃ᵤ  :=
  @AbstractCompletion.uniformContinuous_compareEquiv Q uniformSpace bourbakiPkg rationalCauSeqPkg

And errors on rationalCauSeqPkg with:

argument
  rationalCauSeqPkg
has type
  @AbstractCompletion  PseudoMetricSpace.toUniformSpace : Type 1
but is expected to have type
  @AbstractCompletion Q uniformSpace : Type 1

Arien Malec (Mar 08 2023 at 00:03):

Getting one step closer:

def rationalCauSeqPkg : (@AbstractCompletion  uniformSpace) := AbstractCompletion.mk
  (space := )
  (coe := (RatCast.ratCast :   ))
  (uniformStruct := by infer_instance)
  (complete := by infer_instance)
  (separation := by infer_instance)
  (uniformInducing := Rat.uniformEmbedding_coe_real.toUniformInducing)
  (dense := Rat.denseEmbedding_coe_real.dense)

fixes the typing errors later down, but fails on the uniformInducing and dense steps, the former with our friend:

synthesized type class instance is not definitionally equal to expression inferred by typing rules, synthesized
  uniformSpace
inferred
  PseudoMetricSpace.toUniformSpace

The latter with the very confusing

synthesized type class instance is not definitionally equal to expression inferred by typing rules, synthesized
  toTopologicalSpace
inferred
  toTopologicalSpace

Arien Malec (Mar 08 2023 at 01:25):

I've gone so far as doing:

(uniformInducing := @UniformEmbedding.toUniformInducing ℚ ℝ uniformSpace _ RatCast.ratCast Rat.uniformEmbedding_coe_real)

which still gives me...

pplication type mismatch
  Rat.uniformEmbedding_coe_real.toUniformInducing
argument
  Rat.uniformEmbedding_coe_real
has type
  @UniformEmbedding   PseudoMetricSpace.toUniformSpace PseudoMetricSpace.toUniformSpace RatCast.ratCast : Prop
but is expected to have type
  @UniformEmbedding   uniformSpace PseudoMetricSpace.toUniformSpace RatCast.ratCast : Prop
````

Arien Malec (Mar 08 2023 at 01:26):

When I turn pp.all true

I get as the type of Rat.uniformEmbedding_coe_real

Rat.uniformEmbedding_coe_real : @UniformEmbedding.{0, 0} Rat Real (@PseudoMetricSpace.toUniformSpace.{0} Rat (@MetricSpace.toPseudoMetricSpace.{0} Rat Rat.instMetricSpaceRat)) (@PseudoMetricSpace.toUniformSpace.{0} Real Real.pseudoMetricSpace) (@RatCast.ratCast.{0} Real Real.ratCast)

Arien Malec (Mar 08 2023 at 02:13):

The types check out the same on the Lean3 side but it doesn't seem to cause trouble there,

Arien Malec (Mar 08 2023 at 02:26):

The toTopologicalSpace issue is similar under the microscope of pp.all true

I don't understand the machinery well enough here, but is there a coercion between PseudoMetricSpace.toUniformSpace and ℝ.uniformSpace wanting?

Arien Malec (Mar 08 2023 at 04:19):

Or something something defeq?

Kevin Buzzard (Mar 08 2023 at 14:13):

Arien Malec said:

I've gone so far as doing:

(uniformInducing := @UniformEmbedding.toUniformInducing ℚ ℝ uniformSpace _ RatCast.ratCast Rat.uniformEmbedding_coe_real)

which still gives me...

Application type mismatch
  Rat.uniformEmbedding_coe_real.toUniformInducing
argument
  Rat.uniformEmbedding_coe_real
has type
  @UniformEmbedding   PseudoMetricSpace.toUniformSpace PseudoMetricSpace.toUniformSpace RatCast.ratCast : Prop
but is expected to have type
  @UniformEmbedding   uniformSpace PseudoMetricSpace.toUniformSpace RatCast.ratCast : Prop

Are these terms defeq? If so it's lean's problem, if not it's our problem.

Eric Wieser (Mar 08 2023 at 15:16):

Arien Malec said:

Lean yells if you do that

failed to infer definition type
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed

The underscores belong after the := not before

Arien Malec (Mar 08 2023 at 16:04):

That leads to a different set of issues.

Arien Malec (Mar 08 2023 at 16:08):

I note that right before this definition we have:

theorem Rat.uniformSpace_eq : (AbsoluteValue.abs : AbsoluteValue ℚ ℚ).uniformSpace = PseudoMetricSpace.toUniformSpace which answers the defeq question

Patrick Massot (Mar 08 2023 at 16:10):

I'm really sorry I don't have time to help with this. I will have time this week-end if you are still stuck by then.

Eric Wieser (Mar 08 2023 at 16:12):

Arien Malec said:

That leads to a different set of issues.

I think these issues are the ones you should address. This isn't an unexpected instance diamond or anything, this is just an unusual theorem which mathport doesn't port correctly due to a conscious decision by lean4 to make you spell things the long/ugly way

Eric Wieser (Mar 08 2023 at 16:12):

We've hit this problem in multiple places before

Arien Malec (Mar 08 2023 at 16:30):

The issues are worse to be clear. The definition doesn’t compile & doesn’t typecheck in use. I think I just need to tell Lean about the defeq in proof.

Eric Wieser (Mar 08 2023 at 16:32):

They're not defeq and that's the point of the lemmas I think

Eric Wieser (Mar 08 2023 at 16:40):

I pushed a fix; the solution was to not change the statement of the def, and do roughly what I described above

Eric Wieser (Mar 08 2023 at 16:48):

Thanks for finding a better way to force unification than using @foo _ _ (_) x for instance arguments! It seems that using @foo (x := x) has the same effect.

Eric Wieser (Mar 08 2023 at 16:50):

(as a reminder, @foo _ _ _ x is not the same as @foo _ _ (_) x when it comes to elaborating the last underscore)

Arien Malec (Mar 08 2023 at 16:54):

Eric Wieser said:

Thanks for finding a better way to force unification than using @foo _ _ (_) x for instance arguments! It seems that using @foo (x := x) has the same effect.

I just borrowed the usual lawful monad fixes. Even a blind squirrel finds a nut.


Last updated: Dec 20 2023 at 11:08 UTC