Zulip Chat Archive

Stream: mathlib4

Topic: Data.Fin.Basic (mathlib4#1084)


Eric Wieser (Jan 05 2023 at 22:03):

We could stick that on docs4#instOfNat I guess

Eric Wieser (Jan 05 2023 at 22:05):

Unfortunately Data.Fin.Basic is now full of workarounds for what we had before, so it's a bit tricky for me to test out my change

Eric Wieser (Jan 05 2023 at 22:20):

Eric Wieser said:

Is that true? Lean 3 didn't use mod here so it was irrelevant how it was defined

I misdiagnosed this; the cause is that in Lean3, this is true:

example (n) : nat.mod 0 n = 0 := rfl

but in Lean4, it's not

example : Nat.mod 0 n = 0 := rfl

What prevents us using the Lean3 definition of mod here?

Eric Wieser (Jan 05 2023 at 22:21):

For comparison, the two definitions are (docs#nat.mod)

protected def mod_core (y : ) :     
| 0 x := x
| (fuel+1) x := if h : 0 < y  y  x then mod_core fuel (x - y) else x

protected def mod (x y : ) :  :=
nat.mod_core y x x

and docs4#Nat.mod

protected def mod (x y : @& Nat) : Nat :=
  if 0 < y  y  x then
    Nat.mod (x - y) y
  else
    x
decreasing_by apply div_rec_lemma; assumption

Kevin Buzzard (Jan 05 2023 at 22:23):

docs4#Nat.mod -- bleurgh it's in core

Eric Wieser (Jan 05 2023 at 22:24):

Yeah, so I guess my question should have been "would MSR care if we pushed for the definition we used to have?"

Eric Wieser (Jan 05 2023 at 22:24):

Because if we have the old definition, then Fin.val 0 = 0 is true by rfl

Mario Carneiro (Jan 05 2023 at 22:32):

Actually we used to have the lean 4 definition in lean 3 (definition by well founded recursion), it was refactored in community lean because the fuel based definition evaluates by rfl and in decidable instances

Eric Wieser (Jan 05 2023 at 22:33):

Is that an argument for doing the same in Lean4, or is the claim that Lean4 achieves those goals without the refactor?

Mario Carneiro (Jan 05 2023 at 22:34):

lean4 does not achieve those goals without the refactor, but also refactoring everything to avoid well founded recursion is not a sustainable solution

Mario Carneiro (Jan 05 2023 at 22:34):

div/mod just happen to be especially important definitions

Mario Carneiro (Jan 05 2023 at 22:35):

I think we would rather have a solution which allows well founded recursions to compute by rfl, without unfolding proofs

Eric Wieser (Jan 05 2023 at 22:35):

Is there any disadvantage to the Lean3 spelling?

Mario Carneiro (Jan 05 2023 at 22:36):

it's not as good for the compiler, although in this particular case that's probably not a problem since the definition is overridden anyway

Kevin Buzzard (Jan 05 2023 at 22:36):

right now the main disadvantage seems to be that it's not the same as the lean 4 spelling. What is the motivation behind changing the definition?

Mario Carneiro (Jan 05 2023 at 22:36):

you don't want the compiled code to actually have a fuel variable

Eric Wieser (Jan 05 2023 at 22:36):

Kevin, are you asking what my motivation is for changing it back or Leo's for the version that's different from Lean3?

Kevin Buzzard (Jan 05 2023 at 22:38):

I guess I'm asking why Leo changed it but I guess actually he didn't change it, we changed it.

Eric Wieser (Jan 05 2023 at 22:42):

I think the only difficulty in changing the definition In Lean4 to match Lean3 is we have to reprove docs4#Nat.mod_eq; and that's only difficult because I've never written a Lean4 proof before

Kevin Buzzard (Jan 05 2023 at 22:45):

What does the @& Nat mean in the definition? Is this to do with @[extern]?

Mario Carneiro (Jan 05 2023 at 22:45):

it's a borrow annotation, these are only relevant for @[extern]s

Henrik Böving (Jan 05 2023 at 22:46):

It does not make any type theoretic difference, it's just meta data in an Expr tree

Mario Carneiro (Jan 05 2023 at 22:48):

Eric Wieser said:

I think the only difficulty in changing the definition In Lean4 to match Lean3 is we have to reprove docs4#Nat.mod_eq; and that's only difficult because I've never written a Lean4 proof before

Isn't that proof in lean 3 already? You should be able to use the mathported lean 3 proof

Eric Wieser (Jan 05 2023 at 22:48):

docs#nat.mod_eq doesn't exist

Mario Carneiro (Jan 05 2023 at 22:48):

docs#nat.mod_def

Mario Carneiro (Jan 05 2023 at 22:49):

I wonder if we can make a align#nat.mod_def linkifier

Eric Wieser (Jan 05 2023 at 22:50):

How can I find the mathport output? Based on what you said in the meeting, mathport removes its own output once someone ports it

Mario Carneiro (Jan 05 2023 at 22:50):

it's in the mathlib3port repo

Eric Wieser (Jan 05 2023 at 22:51):

Do I have to go back to an old commit?

Eric Wieser (Jan 05 2023 at 22:51):

Or does "mathport output" not mean the same thing as "stuff in mathlib3port"?

Mario Carneiro (Jan 05 2023 at 22:52):

I'm confused by the question. mathport output means stuff in mathlib3port, but you will see the #prints in new (post-port) commits, not old ones

Eric Wieser (Jan 05 2023 at 22:54):

Either way, I found the code at https://github.com/leanprover-community/lean3port/blob/master/Leanbin/Init/Data/Nat/Lemmas.lean#L1318-L1345

Eric Wieser (Jan 05 2023 at 22:54):

The · syntax is unfortunately causing trouble

Mario Carneiro (Jan 05 2023 at 22:57):

what trouble?

Eric Wieser (Jan 05 2023 at 22:58):

I get "expected command" if I paste that output in the file in coer

Eric Wieser (Jan 05 2023 at 22:58):

I would guess it's using syntax that isn't available yet there

Mario Carneiro (Jan 05 2023 at 23:01):

the dot syntax is available in core, but induction' isn't

Mario Carneiro (Jan 05 2023 at 23:01):

everything else looks ok

Notification Bot (Jan 05 2023 at 23:54):

24 messages were moved from this topic to #mathlib4 > Nat.mod 0 n = 0 no longer true by rfl by Eric Wieser.

Johan Commelin (Jan 06 2023 at 10:38):

This file is now error free, except for the last 10 lines:

-- Note that here we are disabling the "safety" of reflected, to allow us to reuse `nat.mk_numeral`.
-- The usual way to provide the required `reflected` instance would be via rewriting to prove that
-- the expression we use here is equivalent.
attribute [local semireducible] reflected

unsafe instance reflect :  n, has_reflect (Fin n)
  | 0 => finZeroElim
  | n + 1 =>
    nat.mk_numeral q(Fin n.succ) q((by infer_instance : Zero (Fin n.succ)))
        q((by infer_instance : One (Fin n.succ))) q((by infer_instance : Add (Fin n.succ))) 
      Fin.val
#align fin.reflect fin.reflect

Johan Commelin (Jan 06 2023 at 10:38):

#lookup3 has_reflect doesn't give me output. What is the Lean 4 equivalent of has_reflect?

Mario Carneiro (Jan 06 2023 at 10:40):

meta code = ignore

Mario Carneiro (Jan 06 2023 at 10:40):

just remove it

Mario Carneiro (Jan 06 2023 at 10:41):

(we should #noalign has_reflect to make this clearer)

Johan Commelin (Jan 06 2023 at 10:43):

Where should we #noalign ?

Mario Carneiro (Jan 06 2023 at 10:44):

same place as the lean 3 definition

Johan Commelin (Jan 06 2023 at 10:44):

which is core, I guess?

Mario Carneiro (Jan 06 2023 at 10:44):

Somehow Mathlib.Init is really hard for people to discover

Mario Carneiro (Jan 06 2023 at 10:45):

actually it's probably in the meta code in init so there is probably a heading for it in Mathlib.Init.Align

Eric Wieser (Jan 06 2023 at 10:47):

Is the convention to #noalign Fin.reflect too?

Mario Carneiro (Jan 06 2023 at 10:50):

instances are usually not aligned unless they are referred to by name in lean 3

Eric Wieser (Jan 06 2023 at 10:54):

The reflect instances seem to always be given explicit names

Eric Wieser (Jan 06 2023 at 10:55):

If we don't align instances, doesn't that mean we can't use mathport to check if we ported them?

Mario Carneiro (Jan 06 2023 at 10:58):

mathport currently can't provide #align statements for instances even if it wanted to because it doesn't know the instance naming convention

Eric Wieser (Jan 06 2023 at 11:02):

Doesn't understand the mathlib3 or mathlib4 conventions?

Eric Wieser (Jan 06 2023 at 11:02):

Presumably it has access to the mathlib3 name by inspection?

Mario Carneiro (Jan 06 2023 at 11:03):

it doesn't

Mario Carneiro (Jan 06 2023 at 11:04):

it's part of a general class of issues due to the fact that we don't know for a given syntax which declarations correspond to it, unless we have something like an identifier foo in def foo to give us a hint where to look

Mario Carneiro (Jan 06 2023 at 11:05):

I'd like to fix this by adding some landmarks in the tlean file corresponding to the beginning of a new command

Mario Carneiro (Jan 06 2023 at 11:06):

so that we can say that everything between one landmark and the next is "owned" by that command and synport can combine that with the AST info to list all the #align commands needed

Mario Carneiro (Jan 06 2023 at 11:06):

that will fix to_additive, simps, alias and lots of other commands that add declarations in a weird way

Johan Commelin (Jan 06 2023 at 11:28):

There are four simpNF linting errors left.

Johan Commelin (Jan 06 2023 at 11:29):

With two, the linter claimes simp can prove this. But if you try by simp then it actually fails.

Kevin Buzzard (Jan 06 2023 at 11:32):

Can we minimise and open a std4 issue if there is not already an issue? I remember writing one at some point but then not tracking it

Johan Commelin (Jan 06 2023 at 11:37):

I dunno how to do that. If some expert can take a look at these final four errors, that would be great.

Johan Commelin (Jan 06 2023 at 14:57):

Is there any reason to keep the AdHoc version around:
https://github.com/leanprover-community/mathlib4/pull/1084/files#diff-c18192a96f99c01756605bb84a74796b3842d271b2afed5aa23f62a241853dc9

Johan Commelin (Jan 06 2023 at 14:57):

I would be happy to ditch it.

Johan Commelin (Jan 06 2023 at 14:59):

cc @Ruben Van de Velde @Mario Carneiro

Ruben Van de Velde (Jan 06 2023 at 15:02):

I have no opinion

Johan Commelin (Jan 06 2023 at 15:02):

I think you created the file. So if you don't care, let's remove it.

Ruben Van de Velde (Jan 06 2023 at 15:09):

Looks like it dates back to mathlib4#84, but Scott moved some things there in my mathlib4#729

Johan Commelin (Jan 06 2023 at 15:10):

Well, if someone needs it, we know where to find it.

Johan Commelin (Jan 06 2023 at 15:10):

There's a weird CI build failure: https://github.com/leanprover-community/mathlib4/actions/runs/3856247402/jobs/6572230703

I can't reproduce it locally

Johan Commelin (Jan 06 2023 at 15:12):

Ooh, actually I can reproduce. But I'm very surprised by the fact that this PR breaks Mathlib.Testing.SlimCheck.Sampleable

Ruben Van de Velde (Jan 06 2023 at 15:14):

Maybe that depended on the ad hoc port?

Johan Commelin (Jan 06 2023 at 15:14):

In a pretty bad way, if that's the case

Johan Commelin (Jan 06 2023 at 15:16):

Mathlib.Mathport.Syntax is also broken, it seems

Johan Commelin (Jan 06 2023 at 15:18):

Ooh, maybe it isn't, and the output in the logs was just garbled up

Johan Commelin (Jan 06 2023 at 15:40):

I have to stop for a bit. If someone else wants to fix these, that would be great.

Johan Commelin (Jan 06 2023 at 15:41):

The broken files seem to be

  • Mathlib.Testing.SlimCheck.Sampleable
  • Mathlib.Data.UInt

Johan Commelin (Jan 07 2023 at 09:28):

The AdHoc port contains

variables [Nonempty (Fin n)]

instance : AddMonoidWithOne (Fin n) where
  __ := inferInstanceAs (AddCommMonoid (Fin n))
  natCast n := Fin.ofNat' n Fin.size_positive'
  natCast_zero := rfl
  natCast_succ _ := Fin.ofNat'_succ

whereas the ported files has

instance : AddMonoidWithOne (Fin (n + 1)) :=
  { Fin.addCommMonoid n with
    one := 1
    natCast := Fin.ofNat
    natCast_zero := rfl
    natCast_succ := fun _ => eq_of_veq (add_mod _ _ _) }

Johan Commelin (Jan 07 2023 at 09:28):

This causes trouble in Mathlib.Data.UInt.

Johan Commelin (Jan 07 2023 at 09:28):

@Mario Carneiro @Scott Morrison what do we do?

Ruben Van de Velde (Jan 07 2023 at 09:29):

Didn't the ad hoc port mention this in a comment?

Johan Commelin (Jan 07 2023 at 09:29):

Ooh, maybe it did...

Mario Carneiro (Jan 07 2023 at 09:30):

the adhoc port version seems more flexible. Don't we have something like this in data.zmod?

Mario Carneiro (Jan 07 2023 at 09:30):

could we maybe try a backport?

Ruben Van de Velde (Jan 07 2023 at 09:31):

Alternatively, do we need the uint code right now?

Mario Carneiro (Jan 07 2023 at 09:32):

We don't (for mathlib), but it is likely to move to std so we should try not to make any decisions mutually exclusive with it

Johan Commelin (Jan 07 2023 at 09:32):

Ok, I'll switch to the nonempty instance and add a porting note.

Johan Commelin (Jan 07 2023 at 09:58):

I've pulled in some of the stuff from the AdHoc port

Johan Commelin (Jan 07 2023 at 09:59):

But Data.UInt still has errors

genIntDeclars UInt8
genIntDeclars UInt16
genIntDeclars UInt32
genIntDeclars UInt64
genIntDeclars USize

Johan Commelin (Jan 07 2023 at 09:59):

This is some macro with pretty mysterious error reporting.

Johan Commelin (Jan 07 2023 at 10:00):

@Mario Carneiro Could you please take a look? I have no clue at all what we want this file to look like.

Mario Carneiro (Jan 07 2023 at 10:02):

I believe the macro is just a fancy copy-paste which proves the same theorems for each size of integer

Mario Carneiro (Jan 07 2023 at 10:05):

a trick to get better error messages is to find/replace $typeName with UInt8 and then remove the syntax quotation and macro declaration to make it just a bunch of regular definitions

Mario Carneiro (Jan 07 2023 at 10:09):

I can't really help with the error message without more context though:

-- set_option hygiene false
-- /-- `genIntDeclars UInt8` generates a `CommRing UInt8` instance.  -/
-- local macro "genIntDeclars" typeName:ident : command => do
--   `(
    namespace UInt8
      instance : Inhabited (Fin size) where
        default := Fin.ofNat' 0 size_positive

      instance : AddSemigroup UInt8 where
        add_assoc := fun _ _ _  by rw [AddSemigroup.add_assoc _ _ _]

      instance : AddCommSemigroup UInt8 where
        add_comm := fun _ _  by rw [AddCommSemigroup.add_comm _ _]

      instance : Semigroup UInt8 where
        mul_assoc := fun _ _ _  by rw [Semigroup.mul_assoc _ _ _]

we get some errors here saying it can't find ?a + ?b + ?c, I guess some typeclasses have been redefined or something

Mario Carneiro (Jan 07 2023 at 10:21):

I got everything working by using a less weird proof for those instances, the only thing that is still broken is here:

      instance : Ring UInt8 where
        sub_eq_add_neg := fun _ _  congrArg mk (sub_eq_add_neg _ _)
        add_left_neg := fun a  by apply eq_of_val_eq; simp [neg_def, add_def, zero_def]
        intCast n := Int.cast n
        intCast_ofNat _ := rfl
        intCast_negSucc _ := rfl

the ⟨Int.cast n⟩ doesn't work because it is looking for IntCast (Fin size) where size is UInt8.size, i.e. 256

Mario Carneiro (Jan 07 2023 at 10:22):

Previously this worked because of a previous instance of Inhabited (Fin size) together with the instance you mentioned above

Johan Commelin (Jan 07 2023 at 10:22):

So maybe we need to grab even more from the AdHoc port?

Mario Carneiro (Jan 07 2023 at 10:22):

I'm not sure what you did exactly to the instances, but we need to know that Fin n is a ring when n > 0

Mario Carneiro (Jan 07 2023 at 10:23):

we need to spell n > 0 somehow but it can't be n := n' + 1 because we want to use things like UInt8.size which are not syntactically of that form

Mario Carneiro (Jan 07 2023 at 10:24):

The way that was used in the adhoc file was Nonempty (Fin n)

Mario Carneiro (Jan 07 2023 at 10:24):

we can also use Fact (0 < n) once that is ported

Johan Commelin (Jan 07 2023 at 10:25):

I think I did CommSemiRing

Johan Commelin (Jan 07 2023 at 10:25):

So that will explain the failure.

Mario Carneiro (Jan 07 2023 at 10:27):

I pushed my fixes for everything other than the missing Ring (Fin n) instance

Chris Bailey (Jan 07 2023 at 10:28):

Johan Commelin said:

Mario Carneiro Could you please take a look? I have no clue at all what we want this file to look like.

I added the macro after doing the original version of Fin some time ago when a lot of things were still in the experimentation phase. The convenience factor was that you wouldn't have to track down every integer size when there was a change/experiment implicating one of the items in the macro or its dependencies.

FWIW I agree with Mario that once you guys are relatively happy with the interface for the UInt types, the UX is probably nicer if there are individual non-macro definitions. Rust left these kinds of macro invocations in the source and it is frustrating sometimes to be directed to that in the docs.

Mario Carneiro (Jan 07 2023 at 10:29):

I was thinking the macro could be improved by doing it all in one command, something like run_cmd for typeName in [`UInt8, ...] do elabCommand `(...) since I think the error messages will go to the right place in that case

Mario Carneiro (Jan 07 2023 at 10:30):

or we can declare a fancier macro-macro to fix the error messages

Mario Carneiro (Jan 07 2023 at 10:30):

in principle there isn't anything stopping this kind of pattern from being just as usable as any other command

Mario Carneiro (Jan 07 2023 at 10:32):

it's just not worth investing too much on a macro that is used only once

Chris Bailey (Jan 07 2023 at 10:32):

I'll look into improving the macro.

Mario Carneiro (Jan 07 2023 at 10:37):

Another related work is @[to_additive], which is also doing a similar copy paste job. We could have a version of to_additive which replaces UInt8 stuff by UIntN

Johan Commelin (Jan 07 2023 at 10:42):

Hurray! Errors are gone

Johan Commelin (Jan 07 2023 at 11:53):

CI gives a green :check:

Eric Wieser (Jan 07 2023 at 12:02):

Does that adhoc port AddMonoidWirhOne instance create a diamond with the OfNat instance in core?

Johan Commelin (Jan 07 2023 at 13:28):

@Eric Wieser I have absolutely no clue.

Eric Wieser (Jan 08 2023 at 00:55):

Mario Carneiro said:

could we maybe try a backport?

I really think we should do this, rather than doing the refactor in the port and hoping that it won't cause us trouble later. If we do all the hard work refactoring in Lean3, then mathport will do all the lean4 work for us. If we do the refactoring in Lean4, we won't know whether it works everywhere until the port is done

Johan Commelin (Jan 08 2023 at 06:29):

I'm not yet convinced that we should switch to Nonempty (Fin n) in the first place.
@Mario Carneiro Why is the ad hoc port of Data/UInt.lean doing something different from the Lean 3 version of the file?

Mario Carneiro (Jan 08 2023 at 06:29):

again, if we only have an instance of Fin (n + 1) then Fin size doesn't work

Mario Carneiro (Jan 08 2023 at 06:29):

Data.UInt is a new file, not a port

Mario Carneiro (Jan 08 2023 at 06:30):

UInt8 didn't exist in lean 3

Mario Carneiro (Jan 08 2023 at 06:32):

I'm not saying we need to use Nonempty (Fin n), but we need something isomorphic to it: Inhabited (Fin n), Fact (0 < n), NeZero n are all possible alternatives

Mario Carneiro (Jan 08 2023 at 06:32):

that is, a typeclass on n that says it is not zero

Mario Carneiro (Jan 08 2023 at 06:35):

otherwise you get things like this (using the lean 4 core instance, which is defined for Fin (n+1)):

#check (1 : Fin UInt8.size)
-- failed to synthesize instance
--   OfNat (Fin UInt8.size) 1

example : (1 : UInt8) = 1 := rfl
-- failed to synthesize instance
--   OfNat (Fin UInt8.size) 1

Eric Wieser (Jan 08 2023 at 10:09):

Can we delete UInt and add it back (along with the needed refactor) after the port? It sounds like the ad hoc stuff just gets in the way of porting here

Mario Carneiro (Jan 08 2023 at 10:20):

I don't think this is a constructive approach to new lean 4 features. This is like saying we can't be bothered to implement ByteString theorems because it wasn't there in lean 3

Mario Carneiro (Jan 08 2023 at 10:21):

If anything, the file is a candidate to move to std, but that doesn't solve any of the problems you are talking about and I don't want mathlib to make it impossible to have this file

Mario Carneiro (Jan 08 2023 at 10:22):

this is not an ad hoc port, it is a new file

Eric Wieser (Jan 08 2023 at 10:32):

I don't think we should spend any time thinking about ByteString theorems until the port is done

Mario Carneiro (Jan 08 2023 at 10:37):

I don't think it is mathlib's call to say what other projects are doing

Mario Carneiro (Jan 08 2023 at 10:38):

you are talking about restricting not just mathlib but also core/std here

Mario Carneiro (Jan 08 2023 at 10:39):

you may as well say lean 4 itself is getting in the way of the port. Which I suppose is true but overcoming those hurdles is exactly the point of porting

Mario Carneiro (Jan 08 2023 at 10:41):

We have new primitives now. We need to deal with it

Eric Wieser (Jan 08 2023 at 11:17):

I think the thing that I don't get here is why mathlib4 during its porting stage needs to provide any theorems about UintXX

Eric Wieser (Jan 08 2023 at 11:18):

Generalizing things in a way that works with new Lean4 primitives (from core / std) is great, but if those primitives aren't used anywhere in mathlib then why bother solving that before the port is done?

Mario Carneiro (Jan 08 2023 at 12:49):

I am totally fine with moving this file out of mathlib. But if it moves to std we'll have the same theorems and the same instances causing the same problems. Maybe it will be easier to handle the problem if it is considered "out of our hands" in that way, but it's mostly just a shell game. But I don't want us to drop the theorems without replacement or moving them somewhere upstream, and I don't want std picking up said theorems to make it incompatible with mathlib, so in the end we still have to deal with the fallout.

Eric Wieser (Jan 08 2023 at 13:06):

in the end we still have to deal with the fallout.

I completely agree; I just think mid-port is not the time to do this because it risks creating fallout for the >80% of mathlib that we haven't tried to port yet

Johan Commelin (Jan 09 2023 at 08:46):

Is there anyone who has the time and energy to attempt to backport this refactor of Fin n to mathlib3?

Chris Hughes (Jan 09 2023 at 08:51):

I'll give it a go. What do I have to do?

Mario Carneiro (Jan 09 2023 at 09:08):

The instances for Fin n being a ring or having numerals etc should depend on an instance of ne_zero n, rather than being hard-coded to Fin (n+1)

Mario Carneiro (Jan 09 2023 at 09:09):

I think the relevant code is partly in core which is one of the reasons it wasn't refactored earlier

Johan Commelin (Jan 09 2023 at 09:14):

Since ne_zero is not available in core, I think we can also use nonempty (fin n).

Johan Commelin (Jan 09 2023 at 09:14):

That shouldn't make too much of a difference.

Eric Wieser (Jan 09 2023 at 09:16):

One thing to note is that the it sounds like in Lean 4 we're going to end up with a Fin n.succ instance in core, and a Fin n instance in mathlib

Eric Wieser (Jan 09 2023 at 09:17):

I think they'll be defeq so this isn't an issue

Eric Wieser (Jan 09 2023 at 09:17):

But it means that we don't need to backport to lean 3 core, only to mathlib 3

Chris Hughes (Jan 09 2023 at 09:28):

Could I just add the ne_zero instance in mathlib and make sure it's defeq?

Chris Hughes (Jan 09 2023 at 09:28):

And leave core alone?

Johan Commelin (Jan 09 2023 at 09:32):

I think that's what Eric is suggesting.

Eric Wieser (Jan 09 2023 at 10:17):

The only slight downside of that approach is that the lemmas in core like docs#fin.val_zero refer to the weaker instance. I think we ignore those lemmas anyway though.

Chris Hughes (Jan 09 2023 at 11:05):

https://github.com/leanprover-community/mathlib/pull/18107

Johan Commelin (Jan 09 2023 at 11:08):

@Chris Hughes Thanks a lot! Did you already test locally whether it compiles?

Chris Hughes (Jan 09 2023 at 11:08):

No

Johan Commelin (Jan 09 2023 at 11:08):

Also, can't you golf one_mul using mul_comm?

Chris Hughes (Jan 09 2023 at 11:09):

I know the data/fin folder compiles though

Yakov Pechersky (Jan 09 2023 at 12:32):

A while back, I started this refactor, branch#pechersky/fin-nontrivial

Chris Hughes (Jan 09 2023 at 13:31):

The PR now builds locally

Eric Wieser (Jan 09 2023 at 13:37):

Yakov Pechersky said:

A while back, I started this refactor, branch#pechersky/fin-nontrivial

Here's the previous discussion

Eric Wieser (Jan 09 2023 at 13:38):

I had vague recollections of objections, but it seems that all of them are resolved by docs#ne_zero now existing

Chris Hughes (Jan 09 2023 at 15:46):

I've almost fixed norm_num to fix the tests that are breaking #18107. However one of the tests uses guard_target which tests alpha equivalence of expressions so it is a very tough test to pass because all the instances have to be equal up to alpha equivalence. Is there a more sensible version of guard_target?

Eric Wieser (Jan 09 2023 at 15:58):

Would de-instancing the core instances (either in data/fin/basic.lean or in the test) help with that?

Chris Hughes (Jan 09 2023 at 16:06):

Not really. There was a difference in the ne_zero instance for some numeral as well.

Chris Hughes (Jan 09 2023 at 16:07):

I just made a guard_target\_2 that unfolded reducibles.

Mario Carneiro (Jan 09 2023 at 16:49):

guard_target is configurable to use either syntactic equality, alpha equiv, reducible defeq, or regular defeq. If it is not reasonable to ask for alpha equiv then you should just downgrade the test to whnfR

Mario Carneiro (Jan 09 2023 at 16:50):

(Oh, is this lean 3 you are talking about? In lean 3 there are other variants of guard_target which offer these different matching behaviors)

Chris Hughes (Jan 09 2023 at 17:31):

I couldn't find the one with the behaviour I wanted

Mario Carneiro (Jan 09 2023 at 17:35):

You probably want either guard_target_mod_implicit or guard_target'

Chris Hughes (Jan 09 2023 at 17:41):

I think guard_target_mod_implicit is the closest I guess.

Chris Hughes (Jan 09 2023 at 17:43):

By the way, how does expression matching work on instances. Will it match if it's a different path to the same instance as long as its defeq or something close to that?

Eric Wieser (Jan 09 2023 at 17:43):

IIRC expression matching matches the exact term

Chris Hughes (Jan 09 2023 at 17:43):

Or they can be unified I suppose

Eric Wieser (Jan 09 2023 at 17:43):

So instances are never something you should match on

Chris Hughes (Jan 09 2023 at 17:43):

Then this norm_fin is kind of flaky

Eric Wieser (Jan 09 2023 at 17:44):

What I'd recommend is matching with a wildcard %%inst, then docs#unify inst with the expected instance to check it's a sensible one

Eric Wieser (Jan 09 2023 at 17:44):

(or maybe docs#is_defeq, I don't remember the difference)

Mario Carneiro (Jan 09 2023 at 17:46):

In lean 3 there are a fair number of tactics that match on instances, to decide whether to do the nat thing or the int thing. I'm not sure it's worth changing them at this point

Eric Wieser (Jan 09 2023 at 17:47):

Can you point to one?

Mario Carneiro (Jan 09 2023 at 17:49):

src#tactic.norm_fin.match_fin is probably what Chris is talking about

Mario Carneiro (Jan 09 2023 at 17:50):

src#tactic.norm_num.eval_cast

Eric Wieser (Jan 09 2023 at 17:50):

That's going to fail if the term is instead fin.add_monoid_with_one.to_has_one, right?

Eric Wieser (Jan 09 2023 at 17:50):

Or does norm_num canonize the instances first?

Mario Carneiro (Jan 09 2023 at 17:51):

yes, it will fail

Mario Carneiro (Jan 09 2023 at 17:52):

I'm not saying this is great, I'm saying that lean 4 fixes this issue so let's just not worry about it as long as mathlib isn't broken

Eric Wieser (Jan 09 2023 at 17:54):

Yep, here's a repro:

import tactic.norm_fin
import data.zmod.defs

def foo (α) [has_lt α] [add_comm_monoid_with_one α] := (1 : α) < 2

example : foo (fin 3) :=
begin
  rw foo,
  norm_num -- fail
end

example : (1 : fin 3) < 2 :=
by norm_num -- ok

Johan Commelin (Jan 10 2023 at 07:04):

#18107 is now broken by a deterministic timeout in ring_theory/adjoin_root

Yuyang Zhao (Jan 10 2023 at 09:58):

Seems @[simps] of docs#adjoin_root.power_basis_aux' should be removed.

Johan Commelin (Jan 10 2023 at 10:01):

Seems like a legit use of simps to me? But you determined that it is causing the timeout?

Johan Commelin (Jan 10 2023 at 10:01):

cc @Chris Hughes

Yuyang Zhao (Jan 10 2023 at 10:04):

There is no @[simps] for docs#adjoin_root.power_basis_aux.

Johan Commelin (Jan 10 2023 at 10:05):

Yeah, but looking at the definition, it doesn't look like it would create nice simp-lemmas.

Johan Commelin (Jan 10 2023 at 10:05):

So to me it seems reasonable that one has simps and the other does not.

Yuyang Zhao (Jan 10 2023 at 10:19):

It does cause the timeout and seems the generated lemmas are not used, however. I'm not sure if there is a better solution.

Johan Commelin (Jan 10 2023 at 10:20):

Chris moves the simps to after the defn. Let's see if that helps.

Yuyang Zhao (Jan 10 2023 at 10:26):

https://github.com/leanprover-community/mathlib/compare/fin_refactor...fin_refactor2?expand=1
Also I tried to use ne_zero n for fin.last as well, but seems it may not a good idea. fin.last is used a lot in mathlib. I'm not sure if this change helps.

Eric Rodriguez (Jan 10 2023 at 10:39):

The power_basis simps can be quite slow, I've sometimes written them by hand instead of using simps for this sad reason :/

Eric Wieser (Jan 10 2023 at 11:39):

Yuyang Zhao said:

https://github.com/leanprover-community/mathlib/compare/fin_refactor...fin_refactor2?expand=1
Also I tried to use ne_zero n for fin.last as well, but seems it may not a good idea. fin.last is used a lot in mathlib. I'm not sure if this change helps.

I don't think this is a clear win; coe (fin last n) = n is currently true by rfl, which is nice

Eric Wieser (Jan 10 2023 at 11:40):

We can always revisit it after the port

Eric Wieser (Jan 11 2023 at 12:43):

Mario Carneiro said:

In lean 3 there are a fair number of tactics that match on instances, to decide whether to do the nat thing or the int thing. I'm not sure it's worth changing them at this point

Fixed in #18129 for norm_num and abel.

Johan Commelin (Jan 12 2023 at 12:50):

@Chris Hughes bumped this PR to match the backports that he did in mathlib 3.

Johan Commelin (Jan 12 2023 at 12:50):

Who wants to review it? (I worked on this PR quite a bit, so I think I shouldn't be the main reviewer.)

Chris Hughes (Jan 12 2023 at 12:50):

The one issue is a breakage in Data.UInt

Chris Hughes (Jan 12 2023 at 12:51):

There were changed to this file in master and the PR branch, so I accepted the changes in master but this broke everything. Maybe someone who understands this file can have a look, or I'll have a proper go after lunch

Chris Hughes (Jan 12 2023 at 14:15):

I think this should be ready now

Eric Wieser (Jan 12 2023 at 14:17):

I've added a dummy commit so that we can compare again with the mathport output

Eric Wieser (Jan 12 2023 at 14:32):

Does it makes sense to temporarily change the base commit such that review can be against that diff?

Johan Commelin (Jan 12 2023 at 14:34):

We're reviewing it right now.

Johan Commelin (Jan 12 2023 at 14:34):

Let's do a final review against your dummy commit after that.

Reid Barton (Jan 12 2023 at 14:50):

I just finished a review with a bunch of naming fixes, now lgtm (though I haven't been following this thread closely)

Johan Commelin (Jan 12 2023 at 14:52):

We're heading to some seminar. So if someone else wants to do the final review, please go ahead.

Jihoon Hyun (Jan 12 2023 at 15:15):

There are some fin which should be modified to Fin in comments.

Jihoon Hyun (Jan 12 2023 at 15:18):

I will directly fix those and give a commit if nobody's working on it

Reid Barton (Jan 12 2023 at 15:30):

Yes please go ahead.

Jihoon Hyun (Jan 12 2023 at 15:32):

Actually I already committed the fix. Now comment looks good to me, except at line 35 I missed one nat..

Reid Barton (Jan 12 2023 at 15:36):

I fixed that one and some other occurrences of nat.

Jihoon Hyun (Jan 12 2023 at 15:38):

Should cast_nat_eq_last be cast_Nat_eq_last?

Reid Barton (Jan 12 2023 at 15:40):

No, in a snake_case_name, the individual components always start with a lowercase letter

Jihoon Hyun (Jan 12 2023 at 15:41):

Then I guess the comments are now all good!

Eric Wieser (Jan 12 2023 at 15:42):

I'll have another look over the diff with mathport

Eric Wieser (Jan 12 2023 at 15:42):

Are we ready to swap the diffbase so that we can more accurately review against that?

Eric Wieser (Jan 12 2023 at 15:44):

I'm going to assume yes, based on @Johan Commelin's comment above. Hopefully it's easy to switch back anyway.

Johan Commelin (Jan 12 2023 at 15:59):

Yes, I think you can

Eric Wieser (Jan 12 2023 at 15:59):

Done already and left some comments

Eric Wieser (Jan 12 2023 at 16:00):

The add hoc port seems to have made a bunch of other refactors re moving lemmas around, and I think we should probably discard them to avoid more confusion in docstream files

Eric Wieser (Jan 12 2023 at 16:01):

For instance, docs#fin.comm_semiring lives in data.zmod.defs in mathlib, but in data.fin.basic in the ad-hoc port

Eric Wieser (Jan 12 2023 at 17:34):

I've spotted another ad-hoc refactor that we should probably backport

Eric Wieser (Jan 12 2023 at 17:34):

The adhoc docs#fin.add_comm_monoid implements a custom nsmul that is not defeq to the one in mathlib3

Johan Commelin (Jan 12 2023 at 18:35):

That's probably easier to fix on the mathlib4 side, right?

Johan Commelin (Jan 12 2023 at 18:36):

Also, we can move the port of fin.comm_semiring out of Data.Fin.Basic into Data.Zmod.AdHocDefs, and then import that file in Data.UInt.

Johan Commelin (Jan 12 2023 at 18:36):

That way we don't have to change mathlib3 right now.

Eric Wieser (Jan 12 2023 at 18:39):

I'm done pushing changes for now

Eric Wieser (Jan 12 2023 at 18:39):

That's probably easier to fix on the mathlib4 side, right?

You mean the nsmul change? Yes, we can almost certainly just revert it

Eric Wieser (Jan 12 2023 at 18:40):

I imagine a lot of ad-hoc things diverge from mathlib3 because someone said "I have a clean slate, let's try something different"

Eric Wieser (Jan 12 2023 at 18:40):

Which was fine when mathlib4 was ad-hoc, but is in my opinion a bad idea now we're trying to faithfully port mathlib3.

Eric Wieser (Jan 12 2023 at 18:41):

So I'd be very tempted to just rename all ad-hoc files to Some/FileAdHoc.lean so that this doesn't happen with other files, assuming there are still adhoc ports left

Eric Wieser (Jan 12 2023 at 18:45):

Johan Commelin said:

Also, we can move the port of fin.comm_semiring out of Data.Fin.Basic into Data.Zmod.AdHocDefs, and then import that file in Data.UInt.

I moved these, but then discovered Data.UInt doesn't even use these instances! Should we rewrite Data.Uint so that it does, write a TODO comment, or just drop them?

Johan Commelin (Jan 12 2023 at 19:03):

@Eric Wieser So, do you think it is merge-ready now?

Eric Wieser (Jan 12 2023 at 19:03):

Was the verdict that I should revert the ad-hoc nsmul refactor, rather than ask someone to backport it?

Johan Commelin (Jan 12 2023 at 19:04):

Yes, that sounds good to me

Eric Wieser (Jan 12 2023 at 19:04):

(note also that we definitely should not merge without first switching the base branch back!)

Eric Wieser (Jan 12 2023 at 19:16):

Johan Commelin said:

Yes, that sounds good to me

Done

Eric Wieser (Jan 12 2023 at 19:17):

Just this comment left of mine to resolve

Eric Wieser (Jan 12 2023 at 19:18):

I think someone else should:

  • Do a final pass over the diff looking for accidental changes (implicit/explicit arguments, switches from n.succ to n+1). This would be a lot easier if someone could run mathport's oneshot.
  • change the base commit
  • merge

Johan Commelin (Jan 12 2023 at 19:28):

Can we just rewrite history, and make your current base commit part of the history of the original PR?

Johan Commelin (Jan 12 2023 at 19:29):

I think it makes a lot of sense to preserve that as part of the PR history

Eric Wieser (Jan 12 2023 at 19:33):

Changing the base doesn't change the history

Eric Wieser (Jan 12 2023 at 19:33):

And the current base commit is already in the history

Eric Wieser (Jan 12 2023 at 19:34):

Github is happy to show a diff, as in this comment; it's just not possible to comment on that diff, which is why I wanted to change the base. We could put that link the PR header

Eric Wieser (Jan 12 2023 at 19:36):

Johan Commelin said:

I think it makes a lot of sense to preserve that as part of the PR history

I do wonder if we want a mechanism such that we can do this for all PRs easily

Johan Commelin (Jan 12 2023 at 19:44):

Eric Wieser said:

Just this comment left of mine to resolve

@Mario Carneiro If this comment needs action, I'll make a seperate PR to fix it.

Johan Commelin (Jan 12 2023 at 19:44):

I've kicked the PR on the queue

Johan Commelin (Jan 12 2023 at 19:45):

Ooh snap. We need to update Mathlib.lean

Eric Wieser (Jan 12 2023 at 19:50):

There was also this comment but it's minor

Johan Commelin (Jan 13 2023 at 06:56):

mathlib4#1521 fixes the ofNat thing


Last updated: Dec 20 2023 at 11:08 UTC