feat: adjust deriving Inhabited to use structure field defaults#9815
feat: adjust deriving Inhabited to use structure field defaults#9815kmill merged 8 commits intoleanprover:masterfrom
deriving Inhabited to use structure field defaults#9815Conversation
|
Reference manual CI status:
|
|
Mathlib CI status (docs):
|
|
The strategy doesn't work in the prelude, since Best would be a handler that doesn't go through This is also currently just a problem with prelude, and we could write those Inhabited instance by hand. |
This PR changes the deriving handler for `Inhabited` instances so that for structures it uses each fields' default values, if possible. For structure type `S`, the handler uses `by refine' {.. : S} <;> exact default` to construct the default value. A limitation to this is that if there are any stuck defaults due to cycles, they use `default`.
Closes leanprover#9463
|
Mathlib CI status (docs):
|
|
Mathlib CI status (docs):
|
|
Mathlib CI status (docs):
|
|
Mathlib CI status (docs):
|
| deriving Inhabited | ||
|
|
||
| instance : Inhabited TacticParsedSnapshot where | ||
| default := { toSnapshot := default, stx := default, finished := default } |
There was a problem hiding this comment.
@Kha Language.Snapshot uses decl_name% to fill desc, so without the override here (and in Snapshot), you get "Lean.Elab.Tactic.instInhabitedTacticParsedSnapshot" as the desc. This appears in the output of the snapshotTree.lean test, because the default value is actually used somewhere.
A few questions for you:
- Is going through this effort to have a blank
descworth it, or would it be ok to letdescbe"Lean.Elab.Tactic.instInhabitedTacticParsedSnapshot"? - Notice I have to do
toSnapshot := defaultto inherit theInhabitedinstance. I'm concerned about this, since it might invite mistakes. It suggests that I should add an "elaborate an Inhabited instance mode" to the structure instance elaborator, where it would try inheriting parentdefaultvalues, similar to what it does for parent classes.
i. This would be more engineering effort... Maybe this PR leaves things better than they were and no need to chase perfection?
ii. Or would it be better to have a mode to turn off autoParams, which would fix the immediate issue?
iii. Or is it already perfect as it is?
The overarching question is "are you concerned about this instance needing to be written, or does it look fine?"
|
Mathlib CI status (docs):
|
…nstances This PR makes the `deriving Inhabited` handler for `structure`s be able to inherit `Inhabited` instances from structure parents, using the same mechanism as for class parents. This fixes a regression introduced by #9815, which lost the ability to apply `Inhabited` instances for parents represented as subobject fields. With this PR, now it works for all parents. Implementation detail: adds `struct_inst_default%` for synthesizing a structure default value using `Inhabited` instances for parents and fields. Closes #13372
…nstances (#13395) This PR makes the `deriving Inhabited` handler for `structure`s be able to inherit `Inhabited` instances from structure parents, using the same mechanism as for class parents. This fixes a regression introduced by #9815, which lost the ability to apply `Inhabited` instances for parents represented as subobject fields. With this PR, now it works for all parents in the hierarchy. Implementation detail: adds `struct_inst_default%` for synthesizing a structure default value using `Inhabited` instances for parents and fields. Closes #13372
) This PR changes the `Inhabited` deriving handler for `structure` types to use default field values when present; this ensures that `{}` and `default` are interchangeable when all fields have default values. The handler effectively uses `by refine' {..} <;> exact default` to construct the inhabitant. (Note: when default field values cannot be resolved, they are ignored, as usual for ellipsis mode.) Implementation note: the handler now constructs the `Expr` directly and adds it to the environment, though the `instance` is still added using `elabCommand`. Closes #9463
…nstances (#13395) This PR makes the `deriving Inhabited` handler for `structure`s be able to inherit `Inhabited` instances from structure parents, using the same mechanism as for class parents. This fixes a regression introduced by #9815, which lost the ability to apply `Inhabited` instances for parents represented as subobject fields. With this PR, now it works for all parents in the hierarchy. Implementation detail: adds `struct_inst_default%` for synthesizing a structure default value using `Inhabited` instances for parents and fields. Closes #13372
This PR changes the
Inhabitedderiving handler forstructuretypes to use default field values when present; this ensures that{}anddefaultare interchangeable when all fields have default values. The handler effectively usesby refine' {..} <;> exact defaultto construct the inhabitant. (Note: when default field values cannot be resolved, they are ignored, as usual for ellipsis mode.)Implementation note: the handler now constructs the
Exprdirectly and adds it to the environment, though theinstanceis still added usingelabCommand.Closes #9463