Zulip Chat Archive

Stream: new members

Topic: How to rw `f`to `fun x => f x`?


Adomas Baliuka (Jan 13 2024 at 23:45):

I find myself using rw a lot to go between a function and lambdas. What's the idiomatic way to do this?

import Mathlib
open Real

/- TODO how to not need this lemma? -/
lemma Function.eq_lambda {α β : Type*} (f : α  β) : f = fun x  f x := rfl

-- example use
noncomputable def sin_sin x := sin (sin x)

example : Continuous sin_sin := by
  rw [Function.eq_lambda sin_sin]
  simp [sin_sin]
  apply Continuous.comp' <;> apply continuous_sin

Kevin Buzzard (Jan 13 2024 at 23:48):

Try unfold sin_sin and then continuity?

Adomas Baliuka (Jan 13 2024 at 23:50):

Yes, continuity works. I want to know in general what to do when one has finside some expression and one needs fun x => f x. Or should such situations not happen?

Kyle Miller (Jan 13 2024 at 23:50):

https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Tactic/DefEqTransformations.lean has a number of tactics for transforming expressions in different ways. The eta_expand tactic does that transformation to all under-applied functions.

Kyle Miller (Jan 13 2024 at 23:50):

I'm not sure it's used anywhere in mathlib, so caveat emptor

Adomas Baliuka (Jan 13 2024 at 23:52):

Yes, I was looking for eta_expand. Thanks! Any advice how I could have found it myself?

Kyle Miller (Jan 14 2024 at 00:04):

(1) Guessing it might be called eta_expansion if you happen to know the greek letter associated to this lambda calculus rule, (2) consulting the not-yet-existing list of all the mathlib tactics in the mathlib docs (though someone's generated this in the meantime), (3) asking here and hoping someone familiar with a tactic is around (I wrote this one, so I knew it existed :smile:)

Matt Diamond (Jan 14 2024 at 01:38):

Are there cases where transformations between defeq terms are necessary? I would think definitional equality means X will suffice whenever Y will

Adomas Baliuka (Jan 14 2024 at 01:39):

In my original example, if you remove the eta_expand (or the lemma I made that does the same), the proof no longer works.

Matt Diamond (Jan 14 2024 at 01:44):

Right, I see... I'm surprised that it's necessary there

Matt Diamond (Jan 14 2024 at 01:47):

maybe it's a quirk of the way simp does unfolding... as Kevin pointed out, using unfold directly doesn't require the rewrite

Adomas Baliuka (Jan 14 2024 at 01:47):

Yes, I was wondering what's the point of unfold... I guess it does unfold better than simp

Kyle Miller (Jan 14 2024 at 01:49):

simp only applies equation lemmas, and equation lemmas are generally for fully applied functions. unfold will unfold a definition even if it's not fully applied.

Kyle Miller (Jan 14 2024 at 01:51):

I think there was a suggestion to have simp [!sin_sin] do unfold-style unfolding, but I'm not sure if that's graduated from being a mere idea.

Kyle Miller (Jan 14 2024 at 01:53):

I had this simp problem sort of in mind when I added eta_expand, but it's also there as an inverse to eta_reduce.

Kyle Miller (Jan 14 2024 at 01:56):

I think this sort of example might have been the inspiration:

example (f g :   ) : Continuous (f + g) := by
  eta_expand
  simp only [Pi.add_apply]
  -- ⊢ Continuous fun a ↦ f a + g a
  sorry

Matt Diamond (Jan 14 2024 at 02:11):

@Kyle Miller While we're on the topic of defeq transformations, is there a reason this doesn't seem to work?

import Mathlib.Tactic.DefEqTransformations

example {α : Type*} (y : α) (f : α  α) : True := by
  let q := (fun x => f x) y
  beta_reduce at q -- no change
  trivial

Or am I just misunderstanding how it's meant to be used?

Matt Diamond (Jan 14 2024 at 02:12):

Does it only apply to types rather than terms?

Kyle Miller (Jan 14 2024 at 02:15):

Yeah, only types of local hypotheses, no let values.

Kyle Miller (Jan 14 2024 at 02:16):

You could make such a tactic that transforms the values though.

Kyle Miller (Jan 14 2024 at 02:17):

(Maybe it could be a configuration option to beta_reduce and all the other defeq tactics in that file, if this is useful to do.)

Kevin Buzzard (Jan 14 2024 at 12:20):

Matt Diamond said:

Are there cases where transformations between defeq terms are necessary? I would think definitional equality means X will suffice whenever Y will

Tactics like rw work up to syntactic equality, so sometimes you need to make a definitional change before rewriting.


Last updated: May 02 2025 at 03:31 UTC