Class TypeSystem


  • public class TypeSystem
    extends Object
    A table that contains all types in use, indexed by their description (e.g. "int -> int").
    • Constructor Detail

      • TypeSystem

        public TypeSystem()
    • Method Detail

      • bindTyCon

        public Binding bindTyCon​(DataType dataType,
                                 String tyConName)
        Creates a binding of a type constructor value.
      • lookupInternal

        public Type lookupInternal​(String name)
        Looks up an internal type by name.
      • lookup

        public Type lookup​(String name)
        Looks up a type by name.
      • lookupOpt

        public Type lookupOpt​(String name)
        Looks up a type by name, returning null if not found.
      • typeFor

        public Type typeFor​(Type.Key key)
        Gets a type that matches a key, creating if necessary.
      • keyToType

        private Type keyToType​(Type.Key key)
        Creates a type from a key.
      • fnType

        public Type fnType​(Type paramType,
                           Type type1,
                           Type type2,
                           Type... moreTypes)
        Creates a multi-step function type.

        For example, fnType(a, b, c, d) returns the same as fnType(a, fnType(b, fnType(c, d))), viz a → b → c → d.

      • fnType

        public FnType fnType​(Type paramType,
                             Type resultType)
        Creates a function type.
      • tupleType

        public TupleType tupleType​(Type argType0,
                                   Type... argTypes)
        Creates a tuple type from an array of types.
      • listType

        public ListType listType​(Type elementType)
        Creates a list type.
      • setTypeConstructors

        private static void setTypeConstructors​(DataType dataType,
                                                com.google.common.collect.ImmutableSortedMap<String,​Type> typeConstructors)
        Sets the DataType.typeConstructors field of a newly constructed DataType.

        The field is public final, which is for the greater good; the assignment occurs immediately after construction, before anyone sees the DataType instance, so is safe. We would have assigned the field in the constructor if we did not have to deal with datatypes that refer to each other, like this:

        
         datatype 'a tree = Empty | Node of 'a * 'a forest
         and      'a forest = Nil | Cons of 'a tree * 'a forest;
         
      • setInternal

        public void setInternal​(String name)
        Converts a regular type to an internal type. Throws if the type is not known.
      • dataTypeScheme

        public Type dataTypeScheme​(String name,
                                   List<TypeVar> typeParameters,
                                   SortedMap<String,​Type> tyCons)
        Creates a data type scheme: a datatype if there are no type arguments (e.g. "ordering"), or a forall type if there are type arguments (e.g. "forall 'a . 'a option").
      • recordOrScalarType

        public Type recordOrScalarType​(SortedMap<String,​? extends Type> argNameTypes)
        Creates a record type, or returns a scalar type if argNameTypes has one entry.
      • recordType

        public RecordLikeType recordType​(SortedMap<String,​? extends Type> argNameTypes)
        Creates a record type. (Or a tuple type if the fields are named "1", "2" etc.; or "unit" if the field list is empty.)
      • areContiguousIntegers

        public static boolean areContiguousIntegers​(Iterable<String> strings)
        Returns whether the collection is ["1", "2", ... n].
      • temporaryType

        public TemporaryType temporaryType​(String name,
                                           List<? extends Type> parameterTypes,
                                           TypeSystem.Transaction transaction_,
                                           boolean withScheme)
        Creates a temporary type.

        (Temporary types exist for a brief period while defining a recursive datatype.)

      • typeVariables

        private List<TypeVar> typeVariables​(int size)
      • typeVariable

        public TypeVar typeVariable​(int ordinal)
        Creates a type variable.
      • option

        public Type option​(Type type)
        Creates an "option" type.

        "option(type)" is short-hand for "apply(lookup("option"), type)".

      • vector

        public Type vector​(Type type)
        Creates a "vector" type.

        "vector(type)" is short-hand for "apply(lookup("vector"), type)".

      • ensureClosed

        public Type ensureClosed​(Type type)
        Converts a type into a ForallType if it has free type variables.