Package net.hydromatic.morel.compile
Class TypeResolver
java.lang.Object
net.hydromatic.morel.compile.TypeResolver
Resolves all the types within an expression.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA type environment that consists of a type environment plus one binding.private static enumprivate static classEmpty substitution.(package private) static enumEmpty type environment.private classType environment based on anEnvironment.(package private) static classVisitor that expands progressive types if they are used in field references.private static classInstance of an overloaded function.private static classWorkspace for converting types to keys.private static classprivate static classSubstitution that adds one (type, variable) assignment to a parent substitution.static classResult of validating a declaration.(package private) static classFactory that always returns a givenUnifier.Term.private static classSubstitution.private static classPair consisting of a term and a variable.private static classOutput of the type resolution of afromstep, and input to the next step.(package private) static interfaceType environment.private classContains aTypeResolver.TypeEnvand adds to it by callingTypeResolver.TypeEnv.bind(String, Kind, Function).static classError while deducing type. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<Unifier.Variable, Unifier.Action> (package private) static final String(package private) static final Stringprivate final List<Unifier.Constraint> (package private) static final String(package private) static final Stringprivate final Map<AstNode, Unifier.Term> (package private) static final Stringprivate final List<TypeResolver.Inst> private final PairList<Unifier.Variable, PrimitiveType> (package private) static final StringA field of this name indicates that a record type is progressive.(package private) static final Stringprivate final PairList<Ast.FromStep, TypeResolver.Triple> private final List<TypeResolver.TermVariable> (package private) static final Stringprivate final TypeSystemprivate final Unifierprivate final List<Consumer<TypeResolver.Resolved>> private final Consumer<CompileException> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateTypeResolver(TypeSystem typeSystem, Consumer<CompileException> warningConsumer) -
Method Summary
Modifier and TypeMethodDescriptionprivate Unifier.SequenceargTerm(Unifier.Term... args) private Unifier.SequencebagTerm(Unifier.Term term) private static TypeResolver.TypeEnvbindAll(TypeResolver.TypeEnv env, PairList<Ast.IdPat, Unifier.Term> termMap) private static voidcheckNoUnresolvedFieldRefs(Ast.Decl decl, TypeMap typeMap) Checks that there are no field references "x.y" or "#y x" where "x" has an unresolved type.private voidconstrain(Unifier.Variable arg, Unifier.Variable result, PairList<Unifier.Term, Unifier.Term> argResults) Registers that a type variable is equivalent to at least one of a list of terms.private Ast.ExpdeduceApplyFnType(TypeResolver.TypeEnv env, Ast.Exp fn, Unifier.Variable vFn, Unifier.Variable vArg, Unifier.Variable vResult) Deduces the datatype of a function being applied to an argument.private Ast.ApplydeduceApplyType(TypeResolver.TypeEnv env, Ast.Apply apply, Unifier.Variable v) private Ast.CasededuceCaseType(TypeResolver.TypeEnv env, Ast.Case case_, Unifier.Variable v) private voiddeduceDatatypeBindType(Ast.DatatypeBind datatypeBind, PairList<String, Type.Key> tyCons) private Ast.DecldeduceDataTypeDeclType(TypeResolver.TypeEnv env, Ast.DatatypeDecl datatypeDecl, PairList<Ast.IdPat, Unifier.Term> termMap) private Ast.DecldeduceDeclType(TypeResolver.TypeEnv env, Ast.Decl node, PairList<Ast.IdPat, Unifier.Term> termMap) private TypeResolver.TriplededuceGroupStepType(TypeResolver.TypeEnv env, Ast.Group group, TypeResolver.Triple p, PairList<Ast.Id, Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps) deduceMatchListType(TypeResolver.TypeEnv env, List<Ast.Match> matchList, NavigableSet<String> labelNames, Unifier.Variable argVariable, Unifier.Variable resultVariable) private Ast.MatchdeduceMatchType(TypeResolver.TypeEnv env, Ast.Match match, BiConsumer<Ast.IdPat, Unifier.Term> termMap, Unifier.Variable argVariable, Unifier.Variable resultVariable) private Ast.DecldeduceOverDeclType(TypeResolver.TypeEnv env, Ast.OverDecl overDecl, PairList<Ast.IdPat, Unifier.Term> termMap) private voiddeducePatType(TypeResolver.TypeEnv env, Ast.Pat pat, Consumer<TypeResolver.PatTerm> termMap, @Nullable NavigableSet<String> labelNames, Unifier.Variable v, UnaryOperator<Unifier.Term> accessor) Derives a type term for a pattern, collecting the names of pattern variables.private Ast.QuerydeduceQueryType(TypeResolver.TypeEnv env, Ast.Query query, Unifier.Variable v) private Ast.RecordSelectordeduceRecordSelectorType(TypeResolver.TypeEnv env, Ast.RecordSelector recordSelector, Unifier.Variable vArg, Unifier.Variable vResult) private TypeResolver.TriplededuceScanStepType(Ast.Scan scan, TypeResolver.Triple p, PairList<Ast.Id, Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps) private TypeResolver.TriplededuceStepType(TypeResolver.TypeEnv env, Ast.FromStep step, TypeResolver.Triple p, PairList<Ast.Id, Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps) static TypeResolver.ResolveddeduceType(Environment env, Ast.Decl decl, TypeSystem typeSystem, Consumer<CompileException> warningConsumer) Deduces the datatype of a declaration.private Ast.ExpdeduceType(TypeResolver.TypeEnv env, Ast.Exp node, Unifier.Variable v) private TypeResolver.ResolveddeduceType_(Environment env, Ast.Decl decl) Deduces the datatype of a declaration.private TypeResolver.ResolveddeduceTypeWithRetries(Environment env, Ast.Decl decl, TypeSystem typeSystem) Deduces the datatype of a declaration, retrying each time a node is expanded.private AstNodededuceValBindType(TypeResolver.TypeEnv env, Ast.ValBind valBind, PairList<Ast.IdPat, Unifier.Term> termMap, Unifier.Variable vPat) private Ast.DecldeduceValDeclType(TypeResolver.TypeEnv env, Ast.ValDecl valDecl, PairList<Ast.IdPat, Unifier.Term> termMap) private Ast.ExpdeduceYieldType(TypeResolver.TypeEnv env, Ast.Exp node, Unifier.Variable v) Deduces ayieldexpression's type.private Unifier.Variableequiv(Unifier.Variable v, Unifier.Term term) Declares that a term is equivalent to a variable.fieldList(Unifier.Sequence sequence) Inverse ofrecordLabel(NavigableSet).private Unifier.VariablefieldVar(PairList<Ast.Id, Unifier.Variable> fieldVars) private Unifier.SequencefnTerm(Unifier.Term arg, Unifier.Term result) private static voidforEachUnresolvedField(Ast.Decl decl, TypeMap typeMap, Consumer<Ast.Apply> variableConsumer, Consumer<Ast.Apply> notRecordTypeConsumer, Consumer<Ast.Apply> noFieldConsumer) private @Nullable TypegetType(TypeResolver.TypeEnv env, Ast.Exp exp) private static Ast.ExpConverts a list of variable names to a variable or tuple.private Ast.Expinfix(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v) Registers an infix operator.private Ast.Expinfix(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v, Type type) Registers an infix operator whose type is a given type.private voidisListIfAllAreLists(List<Unifier.Term> args, Unifier.Variable c, Unifier.Variable v) Adds a constraint that the terms inargsare all a bag or list (ofv); if all are lists thencis a list ofv, otherwisecis a bag ofv.private voidisListIfBothAreLists(Unifier.Term c0, Unifier.Variable v0, Unifier.Term c1, Unifier.Variable v1, Unifier.Variable c, Unifier.Variable v) Adds a constraint thatc0is a bag or list (ofv0), andc1is a bag or list (ofv1); if both are lists thencis a list ofv, otherwisecis a bag ofv.private voidisListOrBagMatchingInput(Unifier.Variable c1, Unifier.Variable v1, Unifier.Variable c2, Unifier.Variable v2) Adds a constraint thatc1is a bag or list ofv1; if it is a list thenc2is a list ofv2, otherwisec2is a bag ofv2.private Unifier.SequencelistTerm(Unifier.Term term) private voidAdds a constraint thatcis a bag or list ofv.private Ast.PatpatTuple(TypeResolver.TypeEnv env, List<Ast.Pat> patList) Converts a list of patterns to a singleton pattern or tuple pattern.private Ast.Expprefix(TypeResolver.TypeEnv env, Ast.PrefixCall call, Unifier.Variable v) Registers a prefix operator.(package private) static StringrecordLabel(NavigableSet<String> labels) Inverse offieldList(Sequence).private Unifier.TermrecordTerm(NavigableMap<String, ? extends Unifier.Term> labelTypes) private <E extends AstNode>
Ereg(E node, Unifier.Term term) Registers that an AST node maps to a type term.private <E extends AstNode>
Ereg(E node, Unifier.Variable variable, Unifier.Term term) Registers that an AST node maps to a type term and is equivalent to a variable.private Unifier.TermtoTerm(PrimitiveType type) private Unifier.TermtoTerm(Type type, TypeResolver.Subst subst) private List<Unifier.Term> toTerms(Collection<? extends Type> types, TypeResolver.Subst subst) private List<Unifier.Term> toTerms(List<? extends Type> types, TypeResolver.Subst subst) static TypetoType(Ast.Type type, TypeSystem typeSystem) Converts a type AST to a type.static Type.KeyConverts a type AST to a type key.private Ast.ValBindtoValBind(TypeResolver.TypeEnv env, Ast.FunBind funBind) private Ast.ValDecltoValDecl(TypeResolver.TypeEnv env, Ast.FunDecl funDecl) Converts a function declaration to a value declaration.private Unifier.VariabletoVariable(Unifier.Term term) Converts a term to a variable.private Unifier.TermtupleTerm(Collection<? extends Unifier.Term> types) private voidvalidateGroup(Ast.Group group) Validates aGroup.private voidvalidateOrder(Ast.Order order) Validates anOrder.
-
Field Details
-
typeSystem
-
warningConsumer
-
stepStack
-
validations
-
unifier
-
terms
-
map
-
actionMap
-
preferredTypes
-
overloads
-
constraints
-
BAG_TY_CON
-
TUPLE_TY_CON
- See Also:
-
ARG_TY_CON
- See Also:
-
OVERLOAD_TY_CON
-
LIST_TY_CON
- See Also:
-
RECORD_TY_CON
- See Also:
-
FN_TY_CON
- See Also:
-
PROGRESSIVE_LABEL
A field of this name indicates that a record type is progressive.- See Also:
-
-
Constructor Details
-
TypeResolver
-
-
Method Details
-
deduceType
public static TypeResolver.Resolved deduceType(Environment env, Ast.Decl decl, TypeSystem typeSystem, Consumer<CompileException> warningConsumer) Deduces the datatype of a declaration. -
toType
Converts a type AST to a type. -
toTypeKey
Converts a type AST to a type key. -
deduceTypeWithRetries
private TypeResolver.Resolved deduceTypeWithRetries(Environment env, Ast.Decl decl, TypeSystem typeSystem) Deduces the datatype of a declaration, retrying each time a node is expanded. -
deduceType_
Deduces the datatype of a declaration. -
checkNoUnresolvedFieldRefs
Checks that there are no field references "x.y" or "#y x" where "x" has an unresolved type. Throws if there are unresolved field references. -
forEachUnresolvedField
-
reg
Registers that an AST node maps to a type term. -
reg
Registers that an AST node maps to a type term and is equivalent to a variable. -
constrain
private void constrain(Unifier.Variable arg, Unifier.Variable result, PairList<Unifier.Term, Unifier.Term> argResults) Registers that a type variable is equivalent to at least one of a list of terms. -
mayBeBagOrList
Adds a constraint thatcis a bag or list ofv. -
isListOrBagMatchingInput
private void isListOrBagMatchingInput(Unifier.Variable c1, Unifier.Variable v1, Unifier.Variable c2, Unifier.Variable v2) Adds a constraint thatc1is a bag or list ofv1; if it is a list thenc2is a list ofv2, otherwisec2is a bag ofv2. -
isListIfBothAreLists
private void isListIfBothAreLists(Unifier.Term c0, Unifier.Variable v0, Unifier.Term c1, Unifier.Variable v1, Unifier.Variable c, Unifier.Variable v) Adds a constraint thatc0is a bag or list (ofv0), andc1is a bag or list (ofv1); if both are lists thencis a list ofv, otherwisecis a bag ofv. -
isListIfAllAreLists
Adds a constraint that the terms inargsare all a bag or list (ofv); if all are lists thencis a list ofv, otherwisecis a bag ofv. -
deduceYieldType
Deduces ayieldexpression's type.Singleton records are treated specially. The type of
yield {x = y}is not a record type but the type ofy. The step has the same effect asyield y, except that it introducesxinto the namespace. -
deduceType
-
deduceQueryType
-
deduceStepType
private TypeResolver.Triple deduceStepType(TypeResolver.TypeEnv env, Ast.FromStep step, TypeResolver.Triple p, PairList<Ast.Id, Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps) -
deduceScanStepType
private TypeResolver.Triple deduceScanStepType(Ast.Scan scan, TypeResolver.Triple p, PairList<Ast.Id, Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps) -
argTerm
-
deduceGroupStepType
private TypeResolver.Triple deduceGroupStepType(TypeResolver.TypeEnv env, Ast.Group group, TypeResolver.Triple p, PairList<Ast.Id, Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps) -
validateGroup
Validates aGroup. Throws if there are duplicate names among the keys and aggregates. -
validateOrder
Validates anOrder. Warns if asked so sort on a record expression where the fields are not in alphabetical order. -
fieldVar
-
deduceApplyType
-
deduceApplyFnType
private Ast.Exp deduceApplyFnType(TypeResolver.TypeEnv env, Ast.Exp fn, Unifier.Variable vFn, Unifier.Variable vArg, Unifier.Variable vResult) Deduces the datatype of a function being applied to an argument. If the function is overloaded, the argument will help us resolve the overloading.- Parameters:
env- Compile-time environmentfn- Function expression (often an identifier)vFn- Variable for the function typevArg- Variable for the argument typevResult- Variable for the result type- Returns:
- the function expression with its type deduced
-
getType
-
deduceRecordSelectorType
private Ast.RecordSelector deduceRecordSelectorType(TypeResolver.TypeEnv env, Ast.RecordSelector recordSelector, Unifier.Variable vArg, Unifier.Variable vResult) -
fieldList
Inverse ofrecordLabel(NavigableSet). -
deduceMatchType
private Ast.Match deduceMatchType(TypeResolver.TypeEnv env, Ast.Match match, BiConsumer<Ast.IdPat, Unifier.Term> termMap, Unifier.Variable argVariable, Unifier.Variable resultVariable) -
deduceCaseType
-
deduceValBindType
private AstNode deduceValBindType(TypeResolver.TypeEnv env, Ast.ValBind valBind, PairList<Ast.IdPat, Unifier.Term> termMap, Unifier.Variable vPat) -
bindAll
private static TypeResolver.TypeEnv bindAll(TypeResolver.TypeEnv env, PairList<Ast.IdPat, Unifier.Term> termMap) -
deduceDeclType
private Ast.Decl deduceDeclType(TypeResolver.TypeEnv env, Ast.Decl node, PairList<Ast.IdPat, Unifier.Term> termMap) -
deduceDataTypeDeclType
private Ast.Decl deduceDataTypeDeclType(TypeResolver.TypeEnv env, Ast.DatatypeDecl datatypeDecl, PairList<Ast.IdPat, Unifier.Term> termMap) -
deduceOverDeclType
private Ast.Decl deduceOverDeclType(TypeResolver.TypeEnv env, Ast.OverDecl overDecl, PairList<Ast.IdPat, Unifier.Term> termMap) -
deduceValDeclType
private Ast.Decl deduceValDeclType(TypeResolver.TypeEnv env, Ast.ValDecl valDecl, PairList<Ast.IdPat, Unifier.Term> termMap) -
deduceDatatypeBindType
private void deduceDatatypeBindType(Ast.DatatypeBind datatypeBind, PairList<String, Type.Key> tyCons) -
toValDecl
Converts a function declaration to a value declaration. In other words,funis syntactic sugar, and this is the de-sugaring machine.For example,
fun inc x = x + 1becomesval rec inc = fn x => x + 1.If there are multiple arguments, there is one
fnfor each argument:fun sum x y = x + ybecomesval rec sum = fn x => fn y => x + y.If there are multiple clauses, we generate
case:fun gcd a 0 = a | gcd a b = gcd b (a mod b)becomes
val rec gcd = fn x => fn y => case (x, y) of (a, 0) => a | (a, b) = gcd b (a mod b) -
toValBind
-
idTuple
-
patTuple
Converts a list of patterns to a singleton pattern or tuple pattern. -
infix
Registers an infix operator whose type is a given type. -
infix
Registers an infix operator. -
prefix
Registers a prefix operator. -
toVariable
Converts a term to a variable. -
equiv
Declares that a term is equivalent to a variable. -
listTerm
-
bagTerm
-
fnTerm
-
tupleTerm
-
toTerms
-
toTerms
-
toTerm
-
toTerm
-