8000 feat: use `nondep` flag in `Expr.letE` and `LocalContext.ldecl` by kmill · Pull Request #8804 · leanprover/lean4 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: use nondep flag in Expr.letE and LocalContext.ldecl #8804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 22, 2025

Conversation

kmill
Copy link
Collaborator
@kmill kmill commented Jun 16, 2025

This PR implements first-class support for nondependent let expressions in the elaborator; recall that a let expression let x : t := v; b is called nondependent if fun x : t => b typechecks, and the notation for a nondependent let expression is have x := v; b. Previously we encoded have using the letFun function, but now we make use of the nondep flag in the Expr.letE constructor for the encoding. This has been given full support throughout the metaprogramming interface and the elaborator. Key changes to the metaprogramming interface:

  • Local context ldecls with nondep := true are generally treated as cdecls. This is because in the body of a have expression the variable is opaque. Functions like LocalDecl.isLet by default return false for nondependent ldecls. In the rare case where it is needed, they take an additional optional allowNondep : Bool flag (defaults to false) if the variable is being processed in a context where the value is relevant.
  • Functions such as mkLetFVars by default generalize nondependent let variables and create lambda expressions for them. The generalizeNondepLet flag (default true) can be set to false if have expressions should be produced instead. Breaking change: Uses of letLambdaTelescope/mkLetFVars need to use generalizeNondepLet := false. See the next item.
  • There are now some mapping functions to make telescoping operations more convenient. See mapLetTelescope and mapLambdaLetTelescope. There is also mapLetDecl as a counterpart to withLetDecl for creating let/have expressions.
  • Important note about the generalizeNondepLet flag: it should only be used for variables in a local context that the metaprogram "owns". Since nondependent let variables are treated as constants in most cases, the value field might refer to variables that do not exist, if for example those variables were cleared or reverted. Using mapLetDecl is always fine.
  • The simplifier will cache its let dependence calculations in the nondep field of let expressions.
  • The intro tactic still produces dependent local variables. Given that the simplifier will transform lets into haves, it would be surprising if that would prevent intro from creating a local variable whose value cannot be used.

Note that nondependence of lets is not checked by the kernel. To external checker authors: If the elaborator gets the nondep flag wrong, we consider this to be an elaborator error. Feel free to typecheck letE n t v b true as if it were app (lam n t b default) v and please report issues.

This PR follows up from #8751, which made sure the nondep flag was preserved in the C++ interface.

@kmill kmill added the changelog-language Language features, tactics, and metaprograms label Jun 16, 2025
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Jun 16, 2025
@leanprover-community-bot
Copy link
Collaborator
leanprover-community-bot commented Jun 16, 2025

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase e2a947c2e6871f406b965bf1c66ef72e0fdc0599 --onto 957b904ef9fd11ed9cb49f12fcc9d7b989c01900. You can force Mathlib CI using the force-mathlib-ci label. (2025-06-16 00:42:59)
  • 💥 Mathlib branch lean-pr-testing-8804 build failed against this PR. (2025-06-18 18:06:15) View Log
  • 💥 Mathlib branch lean-pr-testing-8804 build failed against this PR. (2025-06-18 21:01:12) View Log
  • 💥 Mathlib branch lean-pr-testing-8804 build failed against this PR. (2025-06-18 21:33:00) View Log
  • 💥 Mathlib branch lean-pr-testing-8804 build failed against this PR. (2025-06-18 22:23:04) View Log
  • 💥 Mathlib branch lean-pr-testing-8804 build failed against this PR. (2025-06-18 23:38:18) View Log
  • ✅ Mathlib branch lean-pr-testing-8804 has successfully built against this PR. (2025-06-19 00:21:50) View Log
  • ✅ Mathlib branch lean-pr-testing-8804 has successfully built against this PR. (2025-06-19 06:27:40) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 2ebc001dd1ce72c134abfe79859780e71df7e3a8 --onto db499e96aac8ad654c8ed5ab40c4e6885d38c9a1. You can force Mathlib CI using the force-mathlib-ci label. (2025-06-22 22:06:10)

@kmill kmill force-pushed the kmill_ldecl_nondep branch from 5a5650a to 0b63061 Compare June 18, 2025 17:30
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Jun 18, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jun 18, 2025
@leanprover-community-bot leanprover-community-bot added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Jun 18, 2025
kmill added a commit to leanprover-community/aesop that referenced this pull request Jun 20, 2025
This PR makes changes for compatibility with leanprover/lean4#8804.

The key difference is that `nondep := true` should be treated as a cdecl. The `LocalDecl.isLet` will return false for such ldecls.
let result ← if useLetExpr then
withLetDecl id.getId (kind := kind) type val fun x => do
let result ←
withLetDecl id.getId (kind := kind) type val (nondep := !useLetExpr) fun x => do
addLocalVarInfo id x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. @kmill No action needed here. I am just marking non trivial changes that I have already checked.

if !zetaDeltaFVarIds.contains fvarId then
/- Non-dependent let-decl. See comment at src/Lean/Meta/Closure.lean -/
if nondep || !zetaDeltaFVarIds.contains fvarId then
/- Nondependent let-decl. See comment at src/Lean/Meta/Closure.lean -/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmill This is an optimization, correct? I am assuming that if fvarId is in zetaDeltaFVarIds and nondep is true, then the nondep annotation is incorrect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, if nondep is true then it is not possible for zetaDeltaFVarIds to contain fvarId. This is just a small optimization.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment mentioning this invariant.

| Expr.letE n type val body nondep =>
-- Turn `have`s into `let`s if they are not propositions.
let nondep ← pure nondep <&&> not <$> Meta.isProp type
mapLetDecl n (← loop below type) (← loop below val) (nondep := nondep) (usedLetOnly := false) fun x => do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remind me of the rationale for turning have to let in structural recursion, Kyle?

@@ -110,14 +110,68 @@ builtin_dsimproc paramMatcher (_) := fun e => do
let matcherApp' := { matcherApp with discrs := discrs', alts := alts' }
return .continue <| matcherApp'.toExpr

/-- `let x := (wfParam e); body[x] ==> let x := e; body[wfParam y] -/
private def anyLetValueIsWfParam (e : Expr) : Bool :=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nomeata I am assuming you are happy with these changes. I am asking because you own this file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks good!

Instead, `revert x` *must* produce the goal `⊢ ∀ x : α, b`.
Furthermore, given a goal `⊢ have x : α := v; b`, the `intro x` tactic should yield a *dependent* `ldecl`,
since users expect to be able to make use of the value of `x`,
and also the value creates a hidden source of dependencies on other local variables.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmill You justified well the design decisions, but it would be great to include a note saying how one gets a nondep ldecl into the local context x : α := v (nondep) ⊢ b since intro will introduce a dependent one for have.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extended the note

since users expect to be able to make use of the value of `x`,
and also the value creates a hidden source of dependencies on other local variables.
- Also: `value` might not be type correct. Metaprograms may decide to pretend that all `nondep := true`
`ldecl`s are `cdecl`s (for example, when reverting variables). As a consequence, nondep `ldecl`s may
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a debugging flag to force Lean to type check the value at functions such as withLetDecl?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep this in mind. Thinking about possible metaprogramming mistakes, maybe mkBinding could check that the value's fvars are all in context?

def setValue : LocalDecl → Expr → LocalDecl
| ldecl idx id n t _ nd k, v => ldecl idx id n t v nd k
| d, _ => d

/-- Sets the `nondep` flag of an `ldecl`, otherwise returns `cdecl`s unchanged. -/
def setNondep : LocalDecl → Bool → LocalDecl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmill Should we add a note saying that it is the caller responsibility to ensure dep => nondep transitions are correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some guidelines to the docstring

-/
def lambdaLetTelescope (e : Expr) (k : Array Expr → Expr → n α) (cleanupAnnotations := false) : n α :=
map2MetaM (fun k => lambdaTelescopeImp e true .none k (cleanupAnnotations := cleanupAnnotations)) k
def lambdaLetTelescope (e : Expr) (k : Array Expr → Expr → n α) (cleanupAnnotations := false) (preserveNondepLet := true) : n α :=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current master, lambdaLetTelescope stops at have. For example, the following example prints 1.

import Lean
def c : Nat → Nat → Bool := fun x => have y := 1; fun z => x == y + z
open Lean Meta in
run_meta do
  let decl ← getConstInfo ``c
  lambdaLetTelescope decl.value! fun xs _ => IO.println xs.size  

I am not defending the current behavior.
The comment above suggests that this example will print 3 if preserveNondepLet := false. What happens if preserveNondepLet := true. I am assuming it will "consume" the have and mark it as nondep in the local context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, now haves are always consumed and that prints 3. Setting preserveNondepLet controls whether the nondep flag will be cleared. I've added this to the tests:

def c : Nat → Nat → Bool := fun x => have y := 1; fun z => x == y + z
/--
info: #[false, true, false]
#[false, false, false]
-/
#guard_msgs in
open Lean Meta in
run_meta do
  let decl ← getConstInfo ``c
  lambdaLetTelescope decl.value! fun xs _ => do
    IO.println <| ← xs.mapM fun x => return (← x.fvarId!.getDecl).isNondep
  lambdaLetTelescope decl.value! (preserveNondepLet := false) fun xs _ => do
    IO.println <| ← xs.mapM fun x => return (← x.fvarId!.getDecl).isNondep


See also `mapLetTelescope` for entering and rebuilding the telescope.
-/
def letTelescope (e : Expr) (k : Array Expr → Expr → n α) (cleanupAnnotations := false) (preserveNondepLet := true) (nondepLetOnly := false) : n α :=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming there is no way to consume only dependent lets.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, there's no way to do that at the moment (I haven't needed it yet).

let zetaDeltaFVarIds ← getZetaDeltaFVarIds
if !zetaDeltaFVarIds.contains fvarId then
if nondep || !zetaDeltaFVarIds.contains fvarId then
/- Non-dependent let-decl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as comment above. I am assuming this is an optimization.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment about the invariant

@@ -728,7 +728,7 @@ mutual
else
let lctx := ctxMeta.lctx
match lctx.findFVar? fvar with
| some (.ldecl (value := v) ..) => check v
| some (.ldecl (nondep := false) (value := v) ..) => check v
| _ =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmill Could you please document this change? We need a why? and example showing why the previous behavior was incorrect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an explanation and example.

@@ -917,7 +917,7 @@ unsafe def checkImpl
| .fvar fvarId .. =>
if mvarDecl.lctx.contains fvarId then
return true
if let some (LocalDecl.ldecl ..) := lctx.find? fvarId then
if let some (LocalDecl.ldecl (nondep := false) ..) := lctx.find? fvarId then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. See previous comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment pointing to the new comment in CheckAssignment.checkFVar.

@@ -379,13 +379,13 @@ partial def foldAndCollect (oldIH newIH : FVarId) (isRecCall : Expr → Option E
let body' ← foldAndCollect oldIH newIH isRecCall (body.instantiate1 x)
mkForallFVars #[x] body'

| .letE n t v b _ =>
| .letE n t v b nondep =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nomeata Are you happy with the changes to this file?

mkLetFun decl.toExpr decl.value e
def mkLetDecls (decls : Array LocalDecl') (e : Expr) : Expr :=
decls.foldr (init := e) fun { decl, isLet } e =>
Expr.letE decl.userName decl.type decl.value (e.abstract #[decl.toExpr]) (nondep := !isLet)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

withLocalDeclD n t fun x => do
let bx := b.instantiate1 x
if nondep then
let bxType ← whnf (← inferType bx)
if (← dependsOn bxType x.fvarId!) then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmill Could you please add a comment here explaining this part? We will forget.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an explanation.

By the way, I have a followup PR that intends to replace this code. Brief preview: if simp comes across a nondep := false let, then it can run letToHave on it (which is slightly cheaper than isTypeCorrect, and it addresses the "TODO" comment in this function, by handling all lets simultaneously), and otherwise we can do the nondepDepVar/nondep determination on an entire telescope at once, using code similar to what you wrote for letFun.

@@ -721,8 +729,8 @@ def simpApp (e : Expr) : SimpM Result := do
if isOfNatNatLit e || isOfScientificLit e || isCharLit e then
-- Recall that we fold "orphan" kernel Nat literals `n` into `OfNat.ofNat n`
return { expr := e }
4D32 else if isNonDepLetFun e then
simpNonDepLetFun e
else if let some (args, n, t, v, b) := e.letFunAppArgs? then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmill Comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment/explanation that this piece of code is in flux. (It should be addressed in time for the next release, so hopefully this comment is short-lived!)

Copy link
Collaborator
@nomeata nomeata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leo asked me to give this a read-through, too.

High-level thought: I’m a bit worried about the complexities around ldecl behaving one way or another depending on their flag and various flags on the functions acting on them. If it wasn’t for backwards compat, would we do it this way, or would entering a have just leave a cdecl in the context, and leave it to whoever rebuilds the expression to remember to use a nondep expression? But you probably considered that design and chose this one for a reason.

In a few cases my brain has trouble keeping up with an optional boolean flag that is called non-something, too much negation and boolean blindness. In terms of API design, at least for those functions where one flag is common and the other is very rare and possibly dangerous or at least hairy to get right, if it’d be better to have separate functions with more speaking names.

| Expr.letE n type val body nondep =>
-- Turn `have`s into `let`s if they are not propositions.
let nondep ← pure nondep <&&> not <$> Meta.isProp type
mapLetDecl n (← loop below type) (← loop below val) (nondep := nondep) (usedLetOnly := false) fun x => do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remind me of the rationale for turning have to let in structural recursion, Kyle?

withLetDecl n (← loop type) (← loop val) fun x => do
mkLetFVars #[x] (← loop (body.instantiate1 x))
| Expr.letE n type val body nondep =>
mapLetDecl n (← loop type) (← loop val) (nondep := nondep) fun x => do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the mapLetDecl combinator to capture that idioms here, I think I wanted something like that before. Maybe later we’ll want it for mapForall etc. as well. (And then generalize the result from Expr to a HasExpr type class so that we can return (Bool, Expr) or (Expr, Expr) … :-))

@@ -110,14 +110,68 @@ builtin_dsimproc paramMatcher (_) := fun e => do
let matcherApp' := { matcherApp with discrs := discrs', alts := alts' }
return .continue <| matcherApp'.toExpr

/-- `let x := (wfParam e); body[x] ==> let x := e; body[wfParam y] -/
private def anyLetValueIsWfParam (e : Expr) : Bool :=
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks good!

Comment on lines 94 to 103
/-- Returns true if this is an `ldecl`. If `allowNondep` is false (the default), then requires that `nondep` be false. -/
def isLet : LocalDecl → (allowNondep : Bool := false) → Bool
| cdecl .., _ => false
| ldecl (nondep := false) .., _ => true
| ldecl (nondep := true) .., allowNondep => allowNondep
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only use of the optional parameter is through isLetVar, and there it is isn’t used, it seems.

I wonder if we get easier to understand code if isLet and isLetVar doesn’t take an optional parameter, and we have separate functions isLetOrHave and isLetOrHaveVar for what’s now (allowNonDep := true). Especially if we don’t expect meta-code to be “dynamically generic” in the allowNonDep flag.

I guess we’d also need letOrHaveValue?. Up to you.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, had to step away before your last message — I was going to say that I'll review this part of the API again after a couple PRs.

Any thoughts about generalizeNondepLet? I'm wondering if I got the polarity wrong on that one, but I couldn't think of anything else to call it. At least it's used infrequently enough that it wouldn't be hard to change it later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to think when I read it, but then concluded that the name is correct 🤷🏻

Comment on lines 185 to 188
- If the declaration is for a variable whose dependencies are not completely under the caller's control,
then setting `nondep := false` must not be done.
- Even if the caller does have complete control, setting `nondep := false` should not be done.
The cautions about caches and metavariables still apply.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like there isn’t a need to even support nondep := false, if it shouldn't be done. Do we need it?

Although, the only use case I see in this diff is

          -- Clear the flag if it's not a prop.
          let decl' := decl.setNondep <| ← pure decl.isNondep <&&> Meta.isProp decl.type

And it seems that this may do a (nondep := true)(nondep := false) transition… if this is something that shouldn’t be done, do we need a comment there why we do it?

I wonder again if the code was clearer if we have separate setNondep and clearNonDep functions, and write it like this:

          -- Clear the flag if it's not a prop.
          if (← Meta.isProp decl.type) the
            decl := decl.clearNondep

assuming we can afford the Meta.isProp check, or

          -- Clear the flag if it's not a prop.
          if (← decl.isNondep <&&> Meta.isProp decl.type) the
            decl := decl.clearNondep

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to clarify this docstring.

I was trying to use "must not" vs "should not" technically, but that's too confusing. There's an example of ok nondep := false usage and why, taken from the WF changes.

@kmill
Copy link
Collaborator Author
kmill commented Jun 22, 2025

@nomeata

High-level thought: I’m a bit worried about the complexities around ldecl behaving one way or another depending on their flag and various flags on the functions acting on them. If it wasn’t for backwards compat, would we do it this way, or would entering a have just leave a cdecl in the context, and leave it to whoever rebuilds the expression to remember to use a nondep expression? But you probably considered that design and chose this one for a reason.

It's reasonable to worry about whether backwards compatibility was a consideration, but I tried to make the design be from first principles. A case study we have is what it's been like to work with letFun, where you have to enter it as a cdecl and then awkwardly apply the letfun constructor at the correct points in a telescope. In an earlier version of letToHave, I had to have a completely custom telescoping function that recorded the have values in a separate data structure. Being able to record the values in the local context reduces the complexity (plus now everything that uses lambdaLetTelescope automatically can enter haves, saving some incidental quadratic complexity). Note that mapLetTelescope does not do everything that you might want with a let telescope, and having a convenient place for sticking the value so that mkLetFVars "just works" is very convenient (modulo needing to set the appropriate flag!)

There seems to be something fundamental here with there being variables where in different situations the value is either known or not known, and I wish I had some more theory to support it. That lack of supporting theory is a weakness of this design in my opinion.

Another design idea I had was to add an hdecl constructor. There were a few reasons I didn't:

  • There's a nice parallel between the nondep flag of Expr.letE and the nondep flag of ldecl.
  • Adding a constructor would need some more expr.h changes.
  • There is enough nondep flag manipulation around where I thought it was clear that requiring code that switches between ldecl and hdecl as necessary would be more complex.

In any case, so long as metaprograms use LocalDecl.isLet, LocalDecl.value?, and other accessors, they don't need to worry about cdecl vs ldecl. It's just "does it have a value (that I can use)?" so long as you don't set any of the flags.

In a few cases my brain has trouble keeping up with an optional boolean flag that is called non-something, too much negation and boolean blindness.

It seems like a negation, but I think in practice "nondependent" is a positive quality. It could have been called "simple"/"fun"/"have"/etc. The default value for the flag is nondep := false, and that's the safe default. Every have in theory is still type correct as a let. The flag is enabled only when a have is safe, and we can talk about clearing the flag, which goes back to the less-conservative expression.

In terms of API design, at least for those functions where one flag is common and the other is very rare and possibly dangerous or at least hairy to get right, if it’d be better to have separate functions with more speaking names.

Yes, this is the purpose of the map* functions. The generalizeNondepLets flag is rather low-level, and I hope it's rare that anyone will need it. There was only one instance of it being needed in mathlib/batteries/aesop/qq (see funprop in https://github.com/leanprover-community/mathlib4/pull/26132/files), and even in that case I think it's just a matter of augmenting the telescoping API with a version that can map a function on types/values before they are added to the local context. (I think that's out of scope for this PR.)

Something that helped me feel like the approach in the PR was on the right track is that none of the uses of the LocalDecl accessors outside of the core algorithms need to adjust the allowNondep argument. (Going back to the point of reasoning about negations, I made this flag be default-false because true means that you are going out of your way to enable accessing normally inaccessible values.)

Remind me of the rationale for turning have to let in structural recursion, Kyle?

I'll check this one again. I know there was a motivating test case, but I don't remember if it was (1) because of changes to simp in this PR, which caches nondependence in the letE expression or (2) if it is necessary once letToHave is being universally applied in a future PR.

(I'll respond to your other comments directly.)

@nomeata
Copy link
Collaborator
nomeata commented Jun 22, 2025

Thanks, that's convincing.

Something that helped me feel like the approach in the PR was on the right track is that none of the uses of the LocalDecl accessors outside of the core algorithms need to adjust the allowNondep argument.

Hm, but isn't that even more reason to make it not look as if allowNondep := true is “equally valid” as the default? If you want users who don't think hard to use the safe default, my sense is that that's achieved more likely if the advanced option is a separate function.

But we are entering superficial bike shedding territory, and it's not like the rest of Lean's API is designed with psychological nudges in mind, so it's ok as is.

@digama0
Copy link
Collaborator
digama0 commented Jun 22, 2025

What's the situation around kernel changes here? Should the kernel now treat nondep := true lets like have? When entering binders should it use ldecl or cdecl?

@kmill
Copy link
Collaborator Author
kmill commented Jun 22, 2025

@digama0 Quoting the PR summary: "Note that nondependence of lets is not checked by the kernel." For the time being, we'll rely on the elaborator typechecker to spot-check nondep usage. (Or, possibly, in some contexts such as proofs we might have the elaborator use a letFun/beta-redex encoding to keep the variable from being unfolded during checking, for performance.)

If you want to implement checking it in an external checker, then I think there are two main options: either convert letE n t v b true to app (lam n t b default) v when you come across it, or, if you're using LocalContext logic, create a cdecl for the fvar when checking b.

kmill added 5 commits June 22, 2025 14:26
This PR implements first-class support for nondependent let expressions in the elaborator; recall that a let expression `let x := v; b` is called *nondependent* if `fun x => b` typechecks, and the notation for a nondependent let expression is `have x := v; b`. Previously we encoded `have` using the `letFun` function, but now we make use of the `nondep` flag in the `Expr.letE` constructor for the encoding. This has been fully supported throughout the metaprogramming interface and the elaborator.

Note that nondependence of lets is not checked by the kernel.

Follows up from leanprover#8751, which made sure the nondep flag was preserved in the C++ interface.
@kmill kmill force-pushed the kmill_ldecl_nondep branch from 8c4d1cf to 2f02cad Compare June 22, 2025 21:40
@kmill kmill added this pull request to the merge queue Jun 22, 2025
Merged via the queue into leanprover:master with commit 02c8c2f Jun 22, 2025
15 checks passed
wkrozowski pushed a commit to wkrozowski/lean4 that referenced this pull request Jun 24, 2025
…prover#8804)

This PR implements first-class support for nondependent let expressions
in the elaborator; recall that a let expression `let x : t := v; b` is
called *nondependent* if `fun x : t => b` typechecks, and the notation
for a nondependent let expression is `have x := v; b`. Previously we
encoded `have` using the `letFun` function, but now we make use of the
`nondep` flag in the `Expr.letE` constructor for the encoding. This has
been given full support throughout the metaprogramming interface and the
elaborator. Key changes to the metaprogramming interface:
- Local context `ldecl`s with `nondep := true` are generally treated as
`cdecl`s. This is because in the body of a `have` expression the
variable is opaque. Functions like `LocalDecl.isLet` by default return
`false` for nondependent `ldecl`s. In the rare case where it is needed,
they take an additional optional `allowNondep : Bool` flag (defaults to
`false`) if the variable is being processed in a context where the value
is relevant.
- Functions such as `mkLetFVars` by default generalize nondependent let
variables and create lambda expressions for them. The
`generalizeNondepLet` flag (default true) can be set to false if `have`
expressions should be produced instead. **Breaking change:** Uses of
`letLambdaTelescope`/`mkLetFVars` need to use `generalizeNondepLet :=
false`. See the next item.
- There are now some mapping functions to make telescoping operations
more convenient. See `mapLetTelescope` and `mapLambdaLetTelescope`.
There is also `mapLetDecl` as a counterpart to `withLetDecl` for
creating `let`/`have` expressions.
- Important note about the `generalizeNondepLet` flag: it should only be
used for variables in a local context that the metaprogram "owns". Since
nondependent let variables are treated as constants in most cases, the
`value` field might refer to variables that do not exist, if for example
those variables were cleared or reverted. Using `mapLetDecl` is always
fine.
- The simplifier will cache its let dependence calculations in the
nondep field of let expressions.
- The `intro` tactic still produces *dependent* local variables. Given
that the simplifier will transform lets into haves, it would be
surprising if that would prevent `intro` from creating a local variable
whose value cannot be used.

Note that nondependence of lets is not checked by the kernel. To
external checker authors: If the elaborator gets the nondep flag wrong,
we consider this to be an elaborator error. Feel free to typecheck `letE
n t v b true` as if it were `app (lam n t b default) v` and please
report issues.

This PR follows up from leanprover#8751, which made sure the nondep flag was
preserved in the C++ interface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features, tactics, and metaprograms toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0