The Difference Between Types and Sets
The type system in the Sym application does not assume that types are sets. Types are just names in the Sym type system. The relationship between two types is defined by declaring one type to be a subtype of another. The type system consists of a list of types and a list of subtype relationships between the types.
The type system in Sym has the following properties:
- Types are objects defined in the Types namespace. Only those objects can appear in the type definition of an object.
-
The type system includes the concept of a subtype. To say that A is a subtype of B means that any term of type A can appear in a context where a term of type B is expected.
- All types are subtypes of Object and Null is a subtype of every type.
- Type A is a subtype of type B if A is defined to be a subtype of B in the Subtypes function. The Subtypes function returns a list of all of the type relationships that have been defined (excluding the relationships with Object and Null, which are assumed).
- The type intersection and type union of any two types exists and is unique. Both the type intersection and type union are defined by the subtype relation.
The Subtype Relationship
Subtype relationships are required to satisfy some limiting conditions. They can't be circular for example. The must also be defined so that the type intersection and type union of any two types is unique.
If you attempt to declare a subtype relationship that violates these limiting conditions, an error is added to the error list during the build process. See Validation in the Build Process for details
Type Intersections and Type Unions
The type union of type A and type B is the "smallest" type that has both A and B as subtypes. If C and D are two types, C is "smaller" than D if C is a subtype of D.
The type intersection of type A and type B is the "largest" type that is a subtype of both A and B. If C and D are two types, C is "larger" than D if D is a subtype of C.
The type intersection and type union are both required to be unique. We evaluate the type intersection by making a list of the types that are subtypes of both type A and type B and taking the "largest" type from that list. This requires that if C and D and both subtypes of both type A and type B, either type C must be a subtype of type D or type D must be a subtype of type C.
The type union is defined similarly, in this case we make a list of all of the types that have A and B and subtypes and take the "smallest" type from that list.
The differences between types and sets
The several differences between types and sets that should be noted:
- The type union and type intersection are not necessarily the same as the union and intersection of the corresponding sets. For example, suppose that our type system consists of Bool, Integer, and Color along with Object and Null and there are no subtype relationships defined. The type union of Bool and Integer is Object, as are the type union Bool and Color and Integer and Color. The set union of
- The bottom type, defined to be Null in the type system in Sym, is not empty as would necessarily be the case if the bottom type were identified with the empty set. Objects can have the Null type and there are a number of objects that are typed as Null, for example the Null object (in the code namespace), the Exception, Break and Return objects are also Null typed. The "flow of control" objects will normally be typed as Null.
- Types are not assumed to have with members. When you create a type, it's not necessary to define the "members" of the type.