Package net.hydromatic.morel.compile
Class TypeResolver
- java.lang.Object
-
- net.hydromatic.morel.compile.TypeResolver
-
public class TypeResolver extends java.lang.ObjectResolves the type of an expression.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classTypeResolver.BindTypeEnvA type environment that consists of a type environment plus one binding.private static classTypeResolver.EmptySubstEmpty substitution.(package private) static classTypeResolver.EmptyTypeEnvEmpty type environment.private static classTypeResolver.PlusSubstSubstitution that adds one (type, variable) assignment to a parent substitution.static classTypeResolver.ResolvedResult of validating a declaration.private static classTypeResolver.SubstSubstitution.private static classTypeResolver.TermToTypeConverterVisitor that converts type terms into actual types.private static classTypeResolver.TermVariablePair consisting of a term and a variable.(package private) static interfaceTypeResolver.TypeEnvType environment.private classTypeResolver.TypeEnvHolderContains aTypeResolver.TypeEnvand adds to it by callingTypeResolver.TypeEnv.bind(String, Function).static classTypeResolver.TypeMapThe result of type resolution, a map from AST nodes to types.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<Unifier.Variable,Unifier.Action>actionMapprivate static java.lang.StringAPPLY_TY_CONprivate static java.lang.StringFN_TY_CONprivate static java.lang.String[]INT_STRINGSprivate static java.lang.StringLIST_TY_CON(package private) java.util.Map<AstNode,Unifier.Term>mapprivate static java.lang.StringRECORD_TY_CON(package private) java.util.List<TypeResolver.TermVariable>termsprivate static java.lang.StringTUPLE_TY_CON(package private) TypeSystemtypeSystem(package private) java.util.Map<java.lang.String,TypeVar>tyVarMap(package private) Unifierunifier
-
Constructor Summary
Constructors Modifier Constructor Description privateTypeResolver(TypeSystem typeSystem)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static TypeResolver.TypeEnvbindAll(TypeResolver.TypeEnv env, java.util.Map<Ast.IdPat,Unifier.Term> termMap)private Ast.Expcomparison(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v)Registers an infix operator whose type is a given type and whose arguments are the same type.private Ast.ExpdeduceConsType(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v)private voiddeduceDatatypeBindType(TypeResolver.TypeEnv env, Ast.DatatypeBind datatypeBind, java.util.Map<Ast.IdPat,Unifier.Term> termMap)private Ast.DecldeduceDeclType(TypeResolver.TypeEnv env, Ast.Decl node, java.util.Map<Ast.IdPat,Unifier.Term> termMap)private voiddeduceMatchListType(TypeResolver.TypeEnv env, java.util.List<Ast.Match> matchList, java.util.NavigableSet<java.lang.String> labelNames, Unifier.Variable argVariable, Unifier.Variable resultVariable)private AstNodededuceMatchType(TypeResolver.TypeEnv env, Ast.Match match, java.util.Map<Ast.IdPat,Unifier.Term> termMap, Unifier.Variable argVariable, Unifier.Variable resultVariable)private Ast.PatdeducePatType(TypeResolver.TypeEnv env, Ast.Pat pat, java.util.Map<Ast.IdPat,Unifier.Term> termMap, java.util.NavigableSet<java.lang.String> labelNames, Unifier.Variable v)Derives a type term for a pattern, collecting the names of pattern variables.private Ast.RecordSelectordeduceRecordSelectorType(TypeResolver.TypeEnv env, Unifier.Variable vResult, Unifier.Variable vArg, Ast.RecordSelector recordSelector)static TypeResolver.ResolveddeduceType(Environment env, Ast.Decl decl, TypeSystem typeSystem)Deduces the type of a declaration.private Ast.ExpdeduceType(TypeResolver.TypeEnv env, Ast.Exp node, Unifier.Variable v)private TypeResolver.ResolveddeduceType_(Environment env, Ast.Decl decl)private AstNodededuceValBindType(TypeResolver.TypeEnv env, Ast.ValBind valBind, java.util.Map<Ast.IdPat,Unifier.Term> termMap, Unifier.Variable v)private Ast.DecldeduceValDeclType(TypeResolver.TypeEnv env, Ast.ValDecl valDecl, java.util.Map<Ast.IdPat,Unifier.Term> termMap)private voidequiv(Unifier.Term term, Unifier.Term term2)private voidequiv(Unifier.Term term, Unifier.Variable atom)private static java.util.List<java.lang.String>fieldList(Unifier.Sequence sequence)private static Ast.ExpidTuple(java.util.List<java.lang.String> vars)Converts a list of variable names to a variable or tuple.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 Ast.ExpinfixOverloaded(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v, Type defaultType)Registers an infix or prefix operator whose type is the same as its arguments.private booleanisContiguousIntegers(java.util.NavigableSet<java.lang.String> names)Returns whether a collection consists of ["1", "2", ...private Ast.ExpopOverloaded(TypeResolver.TypeEnv env, Ast.Exp call, Unifier.Variable v, Type defaultType)Registers an infix or prefix operator whose type is the same as its arguments.private Ast.PatpatTuple(TypeResolver.TypeEnv env, java.util.List<Ast.Pat> patList)Converts a list of patterns to a singleton pattern or tuple pattern.private Ast.ExpprefixOverloaded(TypeResolver.TypeEnv env, Ast.PrefixCall call, Unifier.Variable v, Type defaultType)Registers an infix or prefix operator whose type is the same as its arguments.private Unifier.Termrecord(java.util.NavigableMap<java.lang.String,Unifier.Term> labelTypes)private <E extends AstNode>
Ereg(E node, Unifier.Variable variable, Unifier.Term term)private static java.lang.Stringstr(int i)Converts an integer to its string representation, using a cached value if possible.private Unifier.TermtoTerm(PrimitiveType type)private Unifier.TermtoTerm(Type type, TypeResolver.Subst subst)private java.util.List<Unifier.Term>toTerms(java.lang.Iterable<? extends Type> types, TypeResolver.Subst subst)private TypetoType(Ast.Type type)private java.util.List<Type>toTypes(java.util.List<Ast.Type> typeList)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.Sequencetuple(java.util.List<Unifier.Term> types)
-
-
-
Field Detail
-
typeSystem
final TypeSystem typeSystem
-
unifier
final Unifier unifier
-
terms
final java.util.List<TypeResolver.TermVariable> terms
-
map
final java.util.Map<AstNode,Unifier.Term> map
-
actionMap
final java.util.Map<Unifier.Variable,Unifier.Action> actionMap
-
tyVarMap
final java.util.Map<java.lang.String,TypeVar> tyVarMap
-
TUPLE_TY_CON
private static final java.lang.String TUPLE_TY_CON
- See Also:
- Constant Field Values
-
LIST_TY_CON
private static final java.lang.String LIST_TY_CON
- See Also:
- Constant Field Values
-
RECORD_TY_CON
private static final java.lang.String RECORD_TY_CON
- See Also:
- Constant Field Values
-
FN_TY_CON
private static final java.lang.String FN_TY_CON
- See Also:
- Constant Field Values
-
APPLY_TY_CON
private static final java.lang.String APPLY_TY_CON
- See Also:
- Constant Field Values
-
INT_STRINGS
private static final java.lang.String[] INT_STRINGS
-
-
Constructor Detail
-
TypeResolver
private TypeResolver(TypeSystem typeSystem)
-
-
Method Detail
-
deduceType
public static TypeResolver.Resolved deduceType(Environment env, Ast.Decl decl, TypeSystem typeSystem)
Deduces the type of a declaration.
-
deduceType_
private TypeResolver.Resolved deduceType_(Environment env, Ast.Decl decl)
-
reg
private <E extends AstNode> E reg(E node, Unifier.Variable variable, Unifier.Term term)
-
deduceType
private Ast.Exp deduceType(TypeResolver.TypeEnv env, Ast.Exp node, Unifier.Variable v)
-
record
private Unifier.Term record(java.util.NavigableMap<java.lang.String,Unifier.Term> labelTypes)
-
tuple
private Unifier.Sequence tuple(java.util.List<Unifier.Term> types)
-
isContiguousIntegers
private boolean isContiguousIntegers(java.util.NavigableSet<java.lang.String> names)
Returns whether a collection consists of ["1", "2", ... "n"].
-
str
private static java.lang.String str(int i)
Converts an integer to its string representation, using a cached value if possible.
-
deduceRecordSelectorType
private Ast.RecordSelector deduceRecordSelectorType(TypeResolver.TypeEnv env, Unifier.Variable vResult, Unifier.Variable vArg, Ast.RecordSelector recordSelector)
-
fieldList
private static java.util.List<java.lang.String> fieldList(Unifier.Sequence sequence)
-
deduceMatchType
private AstNode deduceMatchType(TypeResolver.TypeEnv env, Ast.Match match, java.util.Map<Ast.IdPat,Unifier.Term> termMap, Unifier.Variable argVariable, Unifier.Variable resultVariable)
-
deduceMatchListType
private void deduceMatchListType(TypeResolver.TypeEnv env, java.util.List<Ast.Match> matchList, java.util.NavigableSet<java.lang.String> labelNames, Unifier.Variable argVariable, Unifier.Variable resultVariable)
-
deduceValBindType
private AstNode deduceValBindType(TypeResolver.TypeEnv env, Ast.ValBind valBind, java.util.Map<Ast.IdPat,Unifier.Term> termMap, Unifier.Variable v)
-
bindAll
private static TypeResolver.TypeEnv bindAll(TypeResolver.TypeEnv env, java.util.Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceDeclType
private Ast.Decl deduceDeclType(TypeResolver.TypeEnv env, Ast.Decl node, java.util.Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceValDeclType
private Ast.Decl deduceValDeclType(TypeResolver.TypeEnv env, Ast.ValDecl valDecl, java.util.Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceDatatypeBindType
private void deduceDatatypeBindType(TypeResolver.TypeEnv env, Ast.DatatypeBind datatypeBind, java.util.Map<Ast.IdPat,Unifier.Term> termMap)
-
toValDecl
private Ast.ValDecl toValDecl(TypeResolver.TypeEnv env, Ast.FunDecl funDecl)
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)becomesval rec gcd = fn x => fn y => case (x, y) of (a, 0) => a | (a, b) = gcd b (a mod b).
-
toValBind
private Ast.ValBind toValBind(TypeResolver.TypeEnv env, Ast.FunBind funBind)
-
idTuple
private static Ast.Exp idTuple(java.util.List<java.lang.String> vars)
-
patTuple
private Ast.Pat patTuple(TypeResolver.TypeEnv env, java.util.List<Ast.Pat> patList)
Converts a list of patterns to a singleton pattern or tuple pattern.
-
deducePatType
private Ast.Pat deducePatType(TypeResolver.TypeEnv env, Ast.Pat pat, java.util.Map<Ast.IdPat,Unifier.Term> termMap, java.util.NavigableSet<java.lang.String> labelNames, Unifier.Variable v)
Derives a type term for a pattern, collecting the names of pattern variables.- Parameters:
env- Compile-time environmentpat- Pattern ASTtermMap- Map from names to bound terms, populated by this methodlabelNames- List of names of labels in this pattern and sibling patterns in a|match, or null if not a record patternv- Type variable that this method should equate the type term that it derives for this pattern
-
infix
private Ast.Exp infix(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v, Type type)
Registers an infix operator whose type is a given type.
-
comparison
private Ast.Exp comparison(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v)
Registers an infix operator whose type is a given type and whose arguments are the same type.
-
infixOverloaded
private Ast.Exp infixOverloaded(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v, Type defaultType)
Registers an infix or prefix operator whose type is the same as its arguments.
-
prefixOverloaded
private Ast.Exp prefixOverloaded(TypeResolver.TypeEnv env, Ast.PrefixCall call, Unifier.Variable v, Type defaultType)
Registers an infix or prefix operator whose type is the same as its arguments.
-
opOverloaded
private Ast.Exp opOverloaded(TypeResolver.TypeEnv env, Ast.Exp call, Unifier.Variable v, Type defaultType)
Registers an infix or prefix operator whose type is the same as its arguments.
-
deduceConsType
private Ast.Exp deduceConsType(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v)
-
equiv
private void equiv(Unifier.Term term, Unifier.Variable atom)
-
equiv
private void equiv(Unifier.Term term, Unifier.Term term2)
-
toTerms
private java.util.List<Unifier.Term> toTerms(java.lang.Iterable<? extends Type> types, TypeResolver.Subst subst)
-
toTerm
private Unifier.Term toTerm(PrimitiveType type)
-
toTerm
private Unifier.Term toTerm(Type type, TypeResolver.Subst subst)
-
-