lean4-htt/tests/lean/run/2690.lean
Kyle Miller a35e6f4af7
feat: infer Prop for inductive/structure when defining syntactic subsingletons (#5517)
A `Prop`-valued inductive type is a syntactic subsingleton if it has at
most one constructor and all the arguments to the constructor are in
`Prop`. Such types have large elimination, so they could be defined in
`Type` or `Prop` without any trouble, though users tend to expect that
such types define a `Prop` and need to learn to insert `: Prop`.

Currently, the default universe for types is `Type`. This PR adds a
heuristic: if a type is a syntactic subsingleton with exactly one
constructor, and the constructor has at least one parameter, then the
`inductive` command will prefer creating a `Prop` instead of a `Type`.
For `structure`, we ask for at least one field.

More generally, for mutual inductives, each type needs to be a syntactic
subsingleton, at least one type must have one constructor, and at least
one constructor must have at least one parameter. The motivation for
this restriction is that every inductive type starts with a zero
constructors and each constructor starts with zero fields, and
stubbed-out types shouldn't be `Prop`.

Thanks to @arthur-adjedj for the investigation in #2695 and to @digama0
for formulating the heuristic.

Closes #2690
2024-10-08 22:39:38 +00:00

111 lines
2.2 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-!
# `Prop`-valued `inductive`/`structure` by default
When the inductive types are syntactic subsingletons and could be `Prop`,
they may as well be `Prop`.
Issue: https://github.com/leanprover/lean4/issues/2690
-/
/-!
Subsingleton, but no constructors. Type.
-/
inductive I0 where
/-- info: I0 : Type -/
#guard_msgs in #check I0
/-!
One constructor, has a Prop parameter. Prop.
-/
inductive I1 where
| a (h : True)
/-- info: I1 : Prop -/
#guard_msgs in #check I1
/-!
One constructor, no constructor parameters. Type.
-/
inductive I2 where
| a
/-- info: I2 : Type -/
#guard_msgs in #check I2
inductive I2' (_ : Nat) where
| a
/-- info: I2' : Nat → Type -/
#guard_msgs in #check I2'
/-!
Two constructors. Type
-/
inductive I3 where
| a | b
/-- info: I3 : Type -/
#guard_msgs in #check I3
/-!
Mutually inductives, both syntactic subsingletons,
even if one doesn't have a constructor,
and one has no parameters.
-/
mutual
inductive C1 where
inductive C2 where
| a (h : True)
inductive C3 where
| b
end
/-- info: C1 : Prop -/
#guard_msgs in #check C1
/-- info: C2 : Prop -/
#guard_msgs in #check C2
/-- info: C3 : Prop -/
#guard_msgs in #check C3
/-!
Type parameter (promoted from index), still Prop.
-/
inductive D : Nat → Sort _ where
| a (h : n = n) : D n
/-- info: D : Nat → Prop -/
#guard_msgs in #check D
/-!
Structure with no fields, Type.
-/
structure S1 where
/-- info: S1 : Type -/
#guard_msgs in #check S1
/-!
Structure with a Prop field, Prop.
-/
structure S2 where
h : True
/-- info: S2 : Prop -/
#guard_msgs in #check S2
/-!
Structure with parameter and a Prop field, Prop.
-/
structure S3 (α : Type) where
h : ∀ a : α, a = a
/-- info: S3 (α : Type) : Prop -/
#guard_msgs in #check S3
/-!
Verify: `Decidable` is a `Type`.
-/
class inductive Decidable' (p : Prop) where
| isFalse (h : Not p) : Decidable' p
| isTrue (h : p) : Decidable' p
/-- info: Decidable' (p : Prop) : Type -/
#guard_msgs in #check Decidable'
/-!
Verify: `WellFounded` is a `Prop`.
-/
inductive WellFounded' {α : Sort u} (r : αα → Prop) where
| intro (h : ∀ a, Acc r a) : WellFounded' r
/-- info: WellFounded'.{u} {α : Sort u} (r : αα → Prop) : Prop -/
#guard_msgs in #check WellFounded'