Package net.hydromatic.morel.type
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").
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interfaceTypeSystem.DataTypeFixerReplaces temporary data types with real data types, using the supplied map.static interfaceTypeSystem.ForallHelperProvides access to type variables from within a call toforallType(int, Function).static interfaceTypeSystem.TransactionHolds temporary changes to the type system.private classTypeSystem.TransactionImplImplementation ofTypeSystem.Transaction.private static classTypeSystem.VariableCollector
-
Field Summary
Fields Modifier and Type Field Description (package private) Map<String,Type>internalTypeByNameNameGeneratornameGenerator(package private) Map<Type.Key,Type>typeByKey(package private) Map<String,Type>typeByNameprivate Map<String,Pair<DataType,Type>>typeConstructorByName
-
Constructor Summary
Constructors Constructor Description TypeSystem()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Typeapply(Type type, List<Type> types)Typeapply(Type type, Type... types)static booleanareContiguousIntegers(Iterable<String> strings)Returns whether the collection is ["1", "2", ...BindingbindTyCon(DataType dataType, String tyConName)Creates a binding of a type constructor value.(package private) com.google.common.collect.ImmutableSortedMap<String,Type>copyTypeConstructors(SortedMap<String,Type> typeConstructors, UnaryOperator<Type> transform)(package private) DataTypedataType(String name, Type.Key key, List<? extends Type> types, SortedMap<String,Type> tyCons)List<Type>dataTypes(List<Keys.DataTypeDef> defs)Creates several data types simultaneously.private List<Type>dataTypes(List<Keys.DataTypeDef> defs, TypeSystem.DataTypeFixer fixer)TypedataTypeScheme(String name, List<TypeVar> typeParameters, SortedMap<String,Type> tyCons)Creates a data type scheme: a datatype if there are no type arguments (e.g.TypeensureClosed(Type type)Converts a type into aForallTypeif it has free type variables.FnTypefnType(Type paramType, Type resultType)Creates a function type.TypefnType(Type paramType, Type type1, Type type2, Type... moreTypes)Creates a multi-step function type.TypeforallType(int typeCount, Function<TypeSystem.ForallHelper,Type> builder)Creates a "forall" type.ForallTypeforallType(List<TypeVar> typeVars, Type type)Creates a "for all" type.private TypekeyToType(Type.Key key)Creates a type from a key.ListTypelistType(Type elementType)Creates a list type.Typelookup(String name)Looks up a type by name.TypelookupInternal(String name)Looks up an internal type by name.TypelookupOpt(String name)Looks up a type by name, returning null if not found.Pair<DataType,Type>lookupTyCon(String tyConName)Typeoption(Type type)Creates an "option" type.TyperecordOrScalarType(SortedMap<String,? extends Type> argNameTypes)Creates a record type, or returns a scalar type ifargNameTypeshas one entry.RecordLikeTyperecordType(SortedMap<String,? extends Type> argNameTypes)Creates a record type.voidsetInternal(String name)Converts a regular type to an internal type.private static voidsetTypeConstructors(DataType dataType, com.google.common.collect.ImmutableSortedMap<String,Type> typeConstructors)Sets theDataType.typeConstructorsfield of a newly constructedDataType.TemporaryTypetemporaryType(String name, List<? extends Type> parameterTypes, TypeSystem.Transaction transaction_, boolean withScheme)Creates a temporary type.TypeSystem.Transactiontransaction()RecordLikeTypetupleType(List<? extends Type> argTypes)Creates a tuple type.TupleTypetupleType(Type argType0, Type... argTypes)Creates a tuple type from an array of types.TypetypeFor(Type.Key key)Gets a type that matches a key, creating if necessary.TypeVartypeVariable(int ordinal)Creates a type variable.private List<TypeVar>typeVariables(int size)(package private) static StringBuilderunparse(StringBuilder builder, Type type, int left, int right)(package private) static StringBuilderunparseList(StringBuilder builder, Op op, int left, int right, Collection<? extends Type> argTypes)Typevector(Type type)Creates a "vector" type.private Typewrap(DataType dataType, Type type)
-
-
-
Field Detail
-
nameGenerator
public final NameGenerator nameGenerator
-
-
Method Detail
-
copyTypeConstructors
com.google.common.collect.ImmutableSortedMap<String,Type> copyTypeConstructors(@Nonnull SortedMap<String,Type> typeConstructors, @Nonnull UnaryOperator<Type> transform)
-
bindTyCon
public Binding bindTyCon(DataType dataType, String tyConName)
Creates a binding of a type constructor value.
-
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 asfnType(a, fnType(b, fnType(c, d))), viza → b → c → d.
-
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.
-
dataTypes
public List<Type> dataTypes(List<Keys.DataTypeDef> defs)
Creates several data types simultaneously.
-
setTypeConstructors
private static void setTypeConstructors(DataType dataType, com.google.common.collect.ImmutableSortedMap<String,Type> typeConstructors)
Sets theDataType.typeConstructorsfield of a newly constructedDataType.The field is
public final, which is for the greater good; the assignment occurs immediately after construction, before anyone sees theDataTypeinstance, 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;
-
dataTypes
private List<Type> dataTypes(List<Keys.DataTypeDef> defs, TypeSystem.DataTypeFixer fixer)
-
dataType
DataType dataType(String name, Type.Key key, List<? extends Type> types, SortedMap<String,Type> tyCons)
-
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 ifargNameTypeshas 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].
-
forallType
public Type forallType(int typeCount, Function<TypeSystem.ForallHelper,Type> builder)
Creates a "forall" type.
-
forallType
public ForallType forallType(List<TypeVar> typeVars, Type type)
Creates a "for all" type.
-
unparseList
static StringBuilder unparseList(StringBuilder builder, Op op, int left, int right, Collection<? extends Type> argTypes)
-
unparse
static StringBuilder unparse(StringBuilder builder, Type type, int left, int right)
-
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.)
-
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 aForallTypeif it has free type variables.
-
transaction
public TypeSystem.Transaction transaction()
-
-