All the elements of a type that satisfy a predicate.
Subtype p
, usually written { x : α // p x }
or { x // p x }
, contains all elements x : α
for
which p x
is true. Its constructor is a pair of the value and the proof that it satisfies the
predicate. In run-time code, { x : α // p x }
is represented identically to α
.
There is a coercion from { x : α // p x }
to α
, so elements of a subtype may be used where the
underlying type is expected.
Examples:
-
{ n : Nat // n % 2 = 0 }
is the type of even numbers. -
{ xs : Array String // xs.size = 5 }
is the type of arrays with fiveString
s. -
Given
xs : List α
,List { x : α // x ∈ xs }
is the type of lists in which all elements are contained inxs
.
Conventions for notations in identifiers:
-
The recommended spelling of
{ x // p x }
in identifiers issubtype
.
Constructor
Subtype.mk.{u}
Fields
val : α
The value in the underlying type that satisfies the predicate.
property : p self.val
The proof that val
satisfies the predicate p
.