15
$\begingroup$

In the init/logic.lean file of the Lean 3 standard library, nonempty and inhabited are defined. It seems like these two classes are very similar - you can instantiate either of them given an element of the type. In what situations do you use one over the other?

$\endgroup$
4
  • 4
    $\begingroup$ I find the Lean terminology confusing. In my world it is: nonempty A means $A \to \emptyset$; inhabited A means $\exists x \in A . \top$; and pointed means $\Sigma_{A : \mathsf{Type}} A$, or in fibered form pointed A means $A$. $\endgroup$ Apr 16, 2022 at 7:25
  • 1
    $\begingroup$ @Andrej Bauer, Lean calls $A → ∅$ is_empty A, which seems like the opposite of the meaning you're suggesting. Do you really use "nonempty" to mean "has no elements"? $\endgroup$
    – Eric
    Apr 16, 2022 at 7:51
  • 2
    $\begingroup$ Ah yes, sorry about that, I've just returned from England. I should have $(A \to \emptyset) \to \emptyset$, or equivalently $\neg\neg \exists x \in A . \top$. $\endgroup$ Apr 16, 2022 at 8:01
  • $\begingroup$ @Guy Coder: the relevant lines of the files you link to (both Lean 3, just different versions of it) are equivalent if not identical, so the distinction isn't important. $\endgroup$
    – Eric
    Apr 16, 2022 at 19:22

1 Answer 1

21
$\begingroup$

The difference between Nonempty and Inhabited is that Nonempty A : Prop but Inhabited A : Sort (max 1 u) (if A : Sort u). This makes Inhabited A isomorphic as a type to A, while Nonempty A is the propositional truncation of A, equivalent to ∃ x : A, True. To extract a value from Inhabited A is Inhabited.default, but extracting a value from a proof of Nonempty A is the axiom of choice and is noncomputable.

Generally, you should use Nonempty A if you only need the "mere fact" that A is not empty, while Inhabited A is used if you need to access a specific default value for totalizing a function or so. Mathlib has a linter to ensure that you don't use Inhabited in place of Nonempty for proving theorems unless you need it in the statement of the theorem.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.