Class TypeSystem


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

      • typeByName

        private final java.util.Map<java.lang.String,​Type> typeByName
      • typeConstructorByName

        private final java.util.Map<java.lang.String,​Pair<DataType,​Type>> typeConstructorByName
    • Constructor Detail

      • TypeSystem

        public TypeSystem()
    • Method Detail

      • lookup

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

        public Type lookupOpt​(java.lang.String name)
        Looks up a type by name, returning null if not found.
      • 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 Type tupleType​(Type... argTypes)
        Creates a tuple type from an array of types.
      • tupleType

        public Type tupleType​(java.util.List<? extends Type> argTypes)
        Creates a tuple type.
      • listType

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

        public DataType dataType​(java.lang.String name,
                                 java.util.List<TypeVar> typeVars,
                                 java.util.Map<java.lang.String,​Type> tyCons)
        Creates a data type.
      • recordType

        public Type recordType​(java.util.SortedMap<java.lang.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

        private boolean areContiguousIntegers​(java.lang.Iterable<java.lang.String> strings)
        Returns whether the collection is ["1", "2", ... n].
      • forallType

        public Type forallType​(java.lang.Iterable<TypeVar> typeVars,
                               Type type)
        Creates a "forall" type.
      • unparseList

        private static java.lang.StringBuilder unparseList​(java.lang.StringBuilder builder,
                                                           Op op,
                                                           int left,
                                                           int right,
                                                           java.util.List<? extends Type> argTypes)
      • unparse

        private static java.lang.StringBuilder unparse​(java.lang.StringBuilder builder,
                                                       Type type,
                                                       int left,
                                                       int right)
      • lookupTyCon

        public Pair<DataType,​Type> lookupTyCon​(java.lang.String tyConName)
      • apply

        public Type apply​(Type type,
                          java.util.List<Type> types)
      • typeVariable

        public TypeVar typeVariable​(int ordinal)
      • ensureClosed

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