Skip to content
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

feat: add inductive.autoPromoteIndices option #3590

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Lean/Elab/Inductive.lean
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ open Meta
builtin_initialize
registerTraceClass `Elab.inductive

register_builtin_option inductive.autoPromoteIndices : Bool := {
defValue := true
descr := "Promote indices to parameters in inductive types whenever possible."
}

def checkValidInductiveModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do
if modifiers.isNoncomputable then
throwError "invalid use of 'noncomputable' in inductive declaration"
Expand Down Expand Up @@ -714,10 +719,12 @@ private def isDomainDefEq (arrowType : Expr) (type : Expr) : MetaM Bool := do
Convert fixed indices to parameters.
-/
private partial def fixedIndicesToParams (numParams : Nat) (indTypes : Array InductiveType) (indFVars : Array Expr) : MetaM Nat := do
if !inductive.autoPromoteIndices.get (← getOptions) then
return numParams
let masks ← indTypes.mapM (computeFixedIndexBitMask numParams · indFVars)
trace[Elab.inductive] "masks: {masks}"
if masks.all fun mask => !mask.contains true then
return numParams
trace[Elab.inductive] "masks: {masks}"
-- We process just a non-fixed prefix of the indices for now. Reason: we don't want to change the order.
-- TODO: extend it in the future. For example, it should be reasonable to change
-- the order of indices generated by the auto implicit feature.
Expand Down
27 changes: 27 additions & 0 deletions tests/lean/run/3458_1.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/- Ensure that turning off `autoPromoteIndices` stops indices
from being promoted to parameters in inductive types.
Because free variables aren't allowed in parameters of nested appearances,
it is sometimes desirable for fixed indices to not be promoted to parameters.
See `IInterp`.
-/

set_option autoImplicit false
universe u v w

structure ISignature (I : Type u) : Type (max u v + 1) where
symbols : I → Type v
indices : {i : I} → symbols i → List I

inductive All {I : Type u} (P : I → Type v) : List I → Type (max u v) where
| nil : All P []
| cons {x xs}: P x → All P xs → All P (x :: xs)

set_option inductive.autoPromoteIndices false

inductive IInterp {I} (ζ : ISignature.{u,v} I) (P : I → Type w) : I → Type (max u v w) where
| mk :{i : I} → (s : ζ.symbols i) → All P (ζ.indices s) → IInterp ζ P i

notation:max "I⟦ " ζ " ⟧" => IInterp ζ

inductive Iμ {I : Type u} (ζ : ISignature.{u,v} I) : I → Type (max u v) where
| mk : (i : I) → I⟦ ζ ⟧ (Iμ ζ) i → Iμ ζ i
Loading