Types are conceptually thought of as sets of Solid Language Values.
A type union is thought of as a set union (containing values “in” either set).
A type intersection is thought of as a set intersection (containing values “in” both sets).
When a type ‹S› is a subtype of a type ‹T›, it’s conceptually understood that the values “in” ‹S› are also “in” ‹T›.
Types that are equal are thought of as “containing” the same values.
The concepts above are merely conceptual aids for the programmer. The word “in” is in scare quotes, because types are not actually implemented as sets “containing” values — this would be impossible to achieve. (For example, the int type would need to contain 216 values, and the String type would need to contain literally an infinite number of values.) Rather, as an implementation, it’s more appropriate to define an algorithm that determines whether a given value ‹x› is assignable to a given type ‹T›. When it is, we say ‹x› is “of type” ‹T›, and ‹x› is conceptually understood as “contained in” ‹T›.
The following abstract algorithms must be defined:
[x] Check whether a value x is of typeT, or assignable to type T.
(If T is a class and x is an object, saying x is “of type” T is equivalent to saying x is an instance of T.)
[x] Check whether a type S is a subtype of type T, i.e. all the values of type S are also of type T.
[x] Check whether types T and U are equal, i.e. they are subtypes of each other.
[ ] Check whether types T and U are disjoint, i.e. they do not have any overlap, i.e. no value is of type T and of type U.
[x] Create a type intersectionT & U containing the values that are assignable to both T and U.
[x] Create a type unionT | U containing any value that is assignable to either T or U.
[x] Define an empty type / bottom type, call it never (TBD), to which no value or expression is assignable.
[x] Define a universal type / top type, call it unknown, to which any value or expression is assignable.
[x] Define an Object type, obj, to which any value is assignable.
For this version, unknown is equivalent to obj, since every expression has a value. In future versions, not every expression will have a value, and so there will be a type void, to which no values but to which some expressions are assignable. Therefore expressions assignable to void would be assignable to unknown but not to obj.
Type Laws
The following is a list of facts about types in general. The metavariables ‹T›, ‹A›, ‹B›, ‹C›, and ‹D› denote placeholders for Solid Language Types and do not refer to real variables or real types. For brevity, angle quotes will be omitted. In the notation below, let <: be an unofficial symbol denoting “is subtype of”, and let && and || denote “and” and “or” respectively.
For any type ‹T›,
T & unknown equals T. (Identity Element, Intersection)
T | never equals T. (Identity Element, Union)
T & never equals never. (Absorption Element, Intersection)
T | unknown equals unknown. (Absorption Element, Union)
never is always a subtype of T.
T is always a subtype of unknown.
For any types ‹A› and ‹B›,
A & B equals B & A.
(intersection is commutative)
A | B equals B | A.
(union is commutative)
For any types ‹A› and ‹B›,
A & B is a subtype of each of A and B.
A and B are each subtypes of A | B.
(It follows that A & B is always a subtype of A | B.)
A <: B if and only if A & B equals A.
A <: B if and only if A | B equals B.
For any types ‹A›, ‹B›, and ‹C›,
(A & B) & C equals A & (B & C).
(intersection is associative)
(A | B) | C equals A | (B | C).
(union is associative)
A | (B & C) equals (A | B) & (A | C).
(union is distributive over intersection)
A & (B | C) equals (A & B) | (A & C).
(intersection is distributive over union)
For any types ‹A›, ‹B›, and ‹C›,
A <: A.
(subtype is reflexive)
If A <: B && B <: A, then A equals B.
(subtype is anti-symmetric)
If A <: B && B <: C, then A <: C.
(subtype is transitive)
For any types ‹A›, ‹B›, ‹C›, and ‹D›,
A <: C && A <: D if and only if A <: C & D.
(subtype is left-factorable under conjunction and left-distributive over intersection)
If A <: C || A <: D then A <: C | D, but not the converse.
(subtype is left-factorable under disjunction)
A <: C && B <: C if and only if A | B <: C
(subtype is right-antifactorable under conjunction and right-antidistributive over union)
If A <: C || B <: C then A & B <: C, but not the converse.
(subtype is right-antifactorable under disjunction)
Types are conceptually thought of as sets of Solid Language Values.
‹S›
is a subtype of a type‹T›
, it’s conceptually understood that the values “in”‹S›
are also “in”‹T›
.The concepts above are merely conceptual aids for the programmer. The word “in” is in scare quotes, because types are not actually implemented as sets “containing” values — this would be impossible to achieve. (For example, the
int
type would need to contain 216 values, and theString
type would need to contain literally an infinite number of values.) Rather, as an implementation, it’s more appropriate to define an algorithm that determines whether a given value‹x›
is assignable to a given type‹T›
. When it is, we say‹x›
is “of type”‹T›
, and‹x›
is conceptually understood as “contained in”‹T›
.The following abstract algorithms must be defined:
x
is of typeT
, or assignable to typeT
. (IfT
is a class andx
is an object, sayingx
is “of type”T
is equivalent to sayingx
is an instance ofT
.)S
is a subtype of typeT
, i.e. all the values of typeS
are also of typeT
.T
andU
are equal, i.e. they are subtypes of each other.T
andU
are disjoint, i.e. they do not have any overlap, i.e. no value is of typeT
and of typeU
.T & U
containing the values that are assignable to bothT
andU
.T | U
containing any value that is assignable to eitherT
orU
.never
(TBD), to which no value or expression is assignable.unknown
, to which any value or expression is assignable.obj
, to which any value is assignable.For this version,
unknown
is equivalent toobj
, since every expression has a value. In future versions, not every expression will have a value, and so there will be a typevoid
, to which no values but to which some expressions are assignable. Therefore expressions assignable tovoid
would be assignable tounknown
but not toobj
.Type Laws
The following is a list of facts about types in general. The metavariables
‹T›
,‹A›
,‹B›
,‹C›
, and‹D›
denote placeholders for Solid Language Types and do not refer to real variables or real types. For brevity, angle quotes will be omitted. In the notation below, let<:
be an unofficial symbol denoting “is subtype of”, and let&&
and||
denote “and” and “or” respectively.For any type
‹T›
,T & unknown
equalsT
. (Identity Element, Intersection)T | never
equalsT
. (Identity Element, Union)T & never
equalsnever
. (Absorption Element, Intersection)T | unknown
equalsunknown
. (Absorption Element, Union)never
is always a subtype ofT
.T
is always a subtype ofunknown
.For any types
‹A›
and‹B›
,A & B
equalsB & A
. (intersection is commutative)A | B
equalsB | A
. (union is commutative)For any types
‹A›
and‹B›
,A & B
is a subtype of each ofA
andB
.A
andB
are each subtypes ofA | B
.A & B
is always a subtype ofA | B
.)A <: B
if and only ifA & B
equalsA
.A <: B
if and only ifA | B
equalsB
.For any types
‹A›
,‹B›
, and‹C›
,(A & B) & C
equalsA & (B & C)
. (intersection is associative)(A | B) | C
equalsA | (B | C)
. (union is associative)A | (B & C)
equals(A | B) & (A | C)
. (union is distributive over intersection)A & (B | C)
equals(A & B) | (A & C)
. (intersection is distributive over union)For any types
‹A›
,‹B›
, and‹C›
,A <: A
. (subtype is reflexive)A <: B && B <: A
, thenA
equalsB
. (subtype is anti-symmetric)A <: B && B <: C
, thenA <: C
. (subtype is transitive)For any types
‹A›
,‹B›
,‹C›
, and‹D›
,A <: C && A <: D
if and only ifA <: C & D
. (subtype is left-factorable under conjunction and left-distributive over intersection)A <: C || A <: D
thenA <: C | D
, but not the converse. (subtype is left-factorable under disjunction)A <: C && B <: C
if and only ifA | B <: C
(subtype is right-antifactorable under conjunction and right-antidistributive over union)A <: C || B <: C
thenA & B <: C
, but not the converse. (subtype is right-antifactorable under disjunction)