-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: infer
Prop
for inductive
/structure
when defining syntacti…
…c 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
- Loading branch information
Showing
6 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
inductive Term | ||
inductive Term : Type | ||
| id: Term -> Term | ||
|
||
inductive Brx: Term -> Prop | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
inductive E where | ||
inductive E : Type where | ||
| mk : E → E | ||
|
||
inductive F : E → Prop | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
inductive Term | ||
inductive Term : Type | ||
| id2: Term -> Term -> Term | ||
|
||
inductive Brx: Term -> Prop | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/-! | ||
# `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' |