Class TypeSystem

java.lang.Object
net.hydromatic.morel.type.TypeSystem

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

  • Constructor Details

    • TypeSystem

      public TypeSystem()
  • Method Details

    • bindTyCon

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

      private Type wrap(DataType dataType, Type type)
    • 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.
    • typesFor

      public List<Type> typesFor(Iterable<? extends Type.Key> keys)
      Converts a list of keys to a list of types.
    • typesFor

      public SortedMap<String,Type> typesFor(Map<String,? extends Type.Key> keys)
      Converts a map of keys to a map of types.
    • 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.
    • tupleType

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

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

      public List<Type> dataTypes(List<Keys.DataTypeKey> keys)
      Creates several data types simultaneously.
    • dataType

      DataType dataType(String name, List<? extends Type> argumentTypes, SortedMap<String,Type.Key> tyCons)
      Creates an algebraic type.

      Parameter types is empty unless this is a type scheme. For example,

      • datatype 'a option = NONE | SOME of 'a has parameter types and argument types ['a], type constructors [NONE: dummy, SOME: 'a];
      • int option has empty parameter types, argument types [int], type constructors [NONE: dummy, SOME: int];
      • datatype color = RED | GREEN has empty parameter types and argument types, type constructors [RED: dummy, GREEN: dummy].
      Parameters:
      name - Name (e.g. "option")
      argumentTypes - Argument types
      tyCons - Type constructors
    • 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> parameters, SortedMap<String,Type.Key> 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].
    • progressiveRecordType

      public ProgressiveRecordType progressiveRecordType(SortedMap<String,? extends Type> argNameTypes)
      Creates a progressive record type.
    • forallType

      public Type forallType(int typeCount, Function<TypeSystem.ForallHelper,Type> builder)
      Creates a "forall" type.
    • forallType

      public ForallType forallType(int typeCount, Type type)
      Creates a "for all" type.
    • unparseList

      static StringBuilder unparseList(StringBuilder builder, Op op, int left, int right, Collection<? extends Type.Key> argTypes)
    • unparse

      static StringBuilder unparse(StringBuilder builder, Type.Key type, int left, int right)
    • typeVariables

      public List<TypeVar> typeVariables(int size)
    • lookupTyCon

      public Pair<DataType,Type.Key> lookupTyCon(String tyConName)
    • apply

      public Type apply(Type type, Type... types)
    • apply

      public Type apply(Type type, List<Type> types)
    • typeVariable

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

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

      "option(type)" is shorthand for "apply(lookup("option"), type)".

    • vector

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

      "vector(type)" is shorthand for "apply(lookup("vector"), type)".

    • ensureClosed

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