Zulip Chat Archive

Stream: mathlib4

Topic: apply doesn't like `fun x => _`


Johan Commelin (Feb 03 2023 at 07:12):

Is this expected/known behaviour?

def foo : Nat  Nat  Nat  Nat := by
  apply fun x y  _
  /-
  Where are my `x` and `y`?
  -------------------------
  Goal state:
  ```
  case y
  ⊢ ?m.153 ?x
  case x
  ⊢ ?m.145
  ⊢ Sort ?u.144
  ⊢ ?m.145 → Sort ?u.147
  ⊢ (x : ?m.145) → ?m.153 x → Nat → Nat → Nat → Nat
  ```
  -/

Johan Commelin (Feb 03 2023 at 07:13):

This is one hiccup in the output of mathport.

Mario Carneiro (Feb 03 2023 at 07:14):

this is known behavior

Mario Carneiro (Feb 03 2023 at 07:15):

it seems that lean 4 generalizes metavariables / forgets what was introduced and what wasn't, esp. when using _

Johan Commelin (Feb 03 2023 at 07:16):

(And using ?_ doesn't help either.)

Johan Commelin (Feb 03 2023 at 07:16):

Is it considered a bug or a feature?

Mario Carneiro (Feb 03 2023 at 07:16):

it works for me with ?_

Mario Carneiro (Feb 03 2023 at 07:17):

I think it is a bug

Johan Commelin (Feb 03 2023 at 07:17):

Ooh, right! I do get an x and y. But Lean has no clue at all about the types.

Johan Commelin (Feb 03 2023 at 07:17):

case y
 ?m.19 ?x
case x
 ?m.11
 Sort ?u.10
 ?m.11  Sort ?u.13
x: ?m.11
y: ?m.19 x
 Nat  Nat  Nat  Nat

Mario Carneiro (Feb 03 2023 at 07:17):

but it could be the consequence of some hard to change design things

Mario Carneiro (Feb 03 2023 at 07:18):

yeah that is expected, it did refine (fun x y ↦ ?_) _ _ because why not

Johan Commelin (Feb 03 2023 at 07:19):

apply should become an agda-style macro that replaces itself with refine and a suitable extra number of underscores.

Mario Carneiro (Feb 03 2023 at 07:19):

it did exactly that

Mario Carneiro (Feb 03 2023 at 07:19):

2 is suitable too

Johan Commelin (Feb 03 2023 at 07:19):

No, I mean an editor-level macro.

Mario Carneiro (Feb 03 2023 at 07:20):

aha

Johan Commelin (Feb 03 2023 at 07:20):

We should ban apply from checked-in source code.

Mario Carneiro (Feb 03 2023 at 07:20):

show_term could be a code action for any tactic

Johan Commelin (Feb 03 2023 at 07:21):

also that

Mario Carneiro (Feb 03 2023 at 07:21):

there are more occurrences of apply than refine in mathlib4

Mario Carneiro (Feb 03 2023 at 07:26):

If you found this in mathlib btw this is a weird way to spell intros x y

Siddhartha Gadgil (Feb 03 2023 at 07:26):

As an unskilled tactic user, let me say apply is far easier for me to understand. Also it is one step away from a declarative proof, essentially := my_theorem _ with unification.

Johan Commelin (Feb 03 2023 at 07:26):

It was highly minimized

Johan Commelin (Feb 03 2023 at 07:27):

@Siddhartha Gadgil refine is what you say. apply does more magic.

Johan Commelin (Feb 03 2023 at 07:27):

@Mario Carneiro The actual use case was more like

apply my_lemma x y z (fun a b => _) h1 h2

Mario Carneiro (Feb 03 2023 at 07:27):

ok so using ?_ should do what you want there

Mario Carneiro (Feb 03 2023 at 07:28):

mathport isn't good at inserting ?_ which is why it uses refine'

Johan Commelin (Feb 03 2023 at 07:28):

Maybe unless you need an unknown extra number of ?_ at the end of the apply call as well?

Johan Commelin (Feb 03 2023 at 07:29):

Because otherwise it might still mess up the goal state, like in the MWE

Mario Carneiro (Feb 03 2023 at 07:29):

is that a response to me or siddhartha?

Johan Commelin (Feb 03 2023 at 07:29):

To you

Johan Commelin (Feb 03 2023 at 07:30):

Mario Carneiro said:

yeah that is expected, it did refine (fun x y ↦ ?_) _ _ because why not

You might be bitten by a variant of this, unexpectedly, I guess

Johan Commelin (Feb 03 2023 at 07:30):

Where "You" means "me" and not you

Mario Carneiro (Feb 03 2023 at 07:30):

I won't be bit by it because I never use apply except as a golfed version of refine

Johan Commelin (Feb 03 2023 at 07:31):

"Lean does not unexpectedly bite Mario. Mario unexpectedly bites Lean."

Mario Carneiro (Feb 03 2023 at 07:31):

Does lean 3 really not introduce _ in the apply fun x y ↦ _ example?

Mario Carneiro (Feb 03 2023 at 07:32):

maybe this is an unintended consequence of the apply bug

Mario Carneiro (Feb 03 2023 at 07:32):

it seems like weird enough code that I think it shouldn't occur in the wild

Reid Barton (Feb 03 2023 at 07:33):

Maybe this makes more sense in context but apply (fun x y => _) seems like really weird style unless you intended what Lean 4 does

Reid Barton (Feb 03 2023 at 07:35):

Did whoever wrote that mean refine instead of apply and just forget?

Johan Commelin (Feb 03 2023 at 07:37):

probably

Johan Commelin (Feb 03 2023 at 07:37):

I think there are plenty of examples in mathlib where apply is used, even if refine makes more sense in both a technical and semantical sense.

Mario Carneiro (Feb 03 2023 at 07:38):

that wouldn't be hard to lint

Mario Carneiro (Feb 03 2023 at 07:39):

"used apply where exact/refine would work"

Mario Carneiro (Feb 03 2023 at 07:39):

for the case where zero _ are inserted

Johan Commelin (Feb 03 2023 at 07:40):

I guess in 99% of the cases, that would also be a semantic improvement?

Mario Carneiro (Feb 03 2023 at 07:41):

it could cause an elaboration change, usually for the better

Mario Carneiro (Feb 03 2023 at 07:42):

I wish we had something like rust clippy though for these kind of borderline pedantic lints

Ruben Van de Velde (Feb 03 2023 at 07:44):

I vaguely recall that there were cases in mathlib 3 where apply was a significant speedup over exact or term mode proofs

Mario Carneiro (Feb 03 2023 at 07:46):

we also have exact (e :) for when we want to actively forget about the expected type when elaborating e, which roughly corresponds to what apply e does in the 0-_ case

Yaël Dillies (Feb 03 2023 at 07:51):

Mario Carneiro said:

it could cause an elaboration change, usually for the better

I recently had a case where I needed the apply elaboration order (due to Lean trying to pick up a typeclass assumption throguh TC inference rather than unification), hence I introduced a "no _ inserted"-apply call.

Mario Carneiro (Feb 03 2023 at 07:51):

yep that's what (e :) is for

Mario Carneiro (Feb 03 2023 at 07:52):

I think it is more explicit that you are doing funny business

Yaël Dillies (Feb 03 2023 at 07:52):

I didn't even know that was correct syntax. I thought you had to do exact (e : _)

Mario Carneiro (Feb 03 2023 at 07:52):

that's the lean 3 syntax

Yaël Dillies (Feb 03 2023 at 07:52):

Mario Carneiro said:

I think it is more explicit that you are doing funny business

In that case I left a note in the proof anyway

Mario Carneiro (Feb 03 2023 at 07:53):

it doesn't have the desired effect in lean 4 so (e :) is the replacement

Yaël Dillies (Feb 03 2023 at 07:53):

Oh okay!

Mario Carneiro (Feb 03 2023 at 07:54):

Yaël Dillies said:

Mario Carneiro said:

it could cause an elaboration change, usually for the better

I recently had a case where I needed the apply elaboration order (due to Lean trying to pick up a typeclass assumption throguh TC inference rather than unification), hence I introduced a "no _ inserted"-apply call.

(although I think that trick won't work in lean 4 anyway, since it doesn't like to get typeclasses by unification)


Last updated: Dec 20 2023 at 11:08 UTC