Package net.hydromatic.morel.compile
Class TypeResolver
- java.lang.Object
-
- net.hydromatic.morel.compile.TypeResolver
-
public class TypeResolver extends Object
Resolves 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.DatatypeBindWorkspaceWorkspace used while handling several datatype binds simultaneously.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.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.TypeExceptionError while deducing type.
-
Field Summary
Fields Modifier and Type Field Description private Map<Unifier.Variable,Unifier.Action>actionMap(package private) static StringAPPLY_TY_CON(package private) static StringFN_TY_CON(package private) static StringLIST_TY_CONprivate Map<AstNode,Unifier.Term>mapprivate List<Pair<Unifier.Variable,PrimitiveType>>preferredTypes(package private) static StringRECORD_TY_CONprivate List<TypeResolver.TermVariable>terms(package private) static StringTUPLE_TY_CONprivate TypeSystemtypeSystemprivate Map<String,TypeVar>tyVarMapprivate Unifierunifier
-
Constructor Summary
Constructors Modifier Constructor Description privateTypeResolver(TypeSystem typeSystem)
-
Method Summary
-
-
-
Field Detail
-
typeSystem
private final TypeSystem typeSystem
-
unifier
private final Unifier unifier
-
terms
private final List<TypeResolver.TermVariable> terms
-
map
private final Map<AstNode,Unifier.Term> map
-
actionMap
private final Map<Unifier.Variable,Unifier.Action> actionMap
-
preferredTypes
private final List<Pair<Unifier.Variable,PrimitiveType>> preferredTypes
-
TUPLE_TY_CON
static final String TUPLE_TY_CON
- See Also:
- Constant Field Values
-
LIST_TY_CON
static final String LIST_TY_CON
- See Also:
- Constant Field Values
-
RECORD_TY_CON
static final String RECORD_TY_CON
- See Also:
- Constant Field Values
-
FN_TY_CON
static final String FN_TY_CON
- See Also:
- Constant Field Values
-
APPLY_TY_CON
static final String APPLY_TY_CON
- See Also:
- Constant Field Values
-
-
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.
-
toType
public static Type toType(Ast.Type type, TypeSystem typeSystem)
Converts a type AST to a type.
-
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)
-
deduceStepType
private Pair<TypeResolver.TypeEnv,Unifier.Variable> deduceStepType(TypeResolver.TypeEnv env, Ast.FromStep step, Unifier.Variable v, TypeResolver.TypeEnv env2, Map<Ast.Id,Unifier.Variable> fieldVars, List<Ast.FromStep> fromSteps)
-
validateGroup
private void validateGroup(Ast.Group group)
Validates aGroup. Throws if there are duplicate names among the keys and aggregates.
-
fieldRecord
private Unifier.Term fieldRecord(Map<Ast.Id,Unifier.Variable> fieldVars)
-
record
private Unifier.Term record(NavigableMap<String,? extends Unifier.Term> labelTypes)
-
tuple
private Unifier.Term tuple(List<Unifier.Term> types)
-
deduceRecordSelectorType
private Ast.RecordSelector deduceRecordSelectorType(TypeResolver.TypeEnv env, Unifier.Variable vResult, Unifier.Variable vArg, Ast.RecordSelector recordSelector)
-
fieldList
static List<String> fieldList(Unifier.Sequence sequence)
-
deduceMatchType
private Ast.Match deduceMatchType(TypeResolver.TypeEnv env, Ast.Match match, Map<Ast.IdPat,Unifier.Term> termMap, Unifier.Variable argVariable, Unifier.Variable resultVariable)
-
deduceMatchListType
private List<Ast.Match> deduceMatchListType(TypeResolver.TypeEnv env, List<Ast.Match> matchList, NavigableSet<String> labelNames, Unifier.Variable argVariable, Unifier.Variable resultVariable)
-
deduceValBindType
private AstNode deduceValBindType(TypeResolver.TypeEnv env, Ast.ValBind valBind, Map<Ast.IdPat,Unifier.Term> termMap, Unifier.Variable v, Unifier.Variable vPat)
-
bindAll
private static TypeResolver.TypeEnv bindAll(TypeResolver.TypeEnv env, Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceDeclType
private Ast.Decl deduceDeclType(TypeResolver.TypeEnv env, Ast.Decl node, Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceDataTypeDeclType
private Ast.Decl deduceDataTypeDeclType(TypeResolver.TypeEnv env, Ast.DatatypeDecl datatypeDecl, Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceValDeclType
private Ast.Decl deduceValDeclType(TypeResolver.TypeEnv env, Ast.ValDecl valDecl, Map<Ast.IdPat,Unifier.Term> termMap)
-
deduceDatatypeBindType
private void deduceDatatypeBindType(TypeResolver.TypeEnv env, Ast.DatatypeBind datatypeBind, Map<Ast.IdPat,Unifier.Term> termMap, TypeResolver.DatatypeBindWorkspace w)
-
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)
-
patTuple
private Ast.Pat patTuple(TypeResolver.TypeEnv env, 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, Map<Ast.IdPat,Unifier.Term> termMap, NavigableSet<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.
-
infix
private Ast.Exp infix(TypeResolver.TypeEnv env, Ast.InfixCall call, Unifier.Variable v)
Registers an infix operator.
-
prefix
private Ast.Exp prefix(TypeResolver.TypeEnv env, Ast.PrefixCall call, Unifier.Variable v)
Registers a prefix operator.
-
equiv
private void equiv(Unifier.Term term, Unifier.Variable atom)
-
equiv
private void equiv(Unifier.Term term, Unifier.Term term2)
-
toTerms
private List<Unifier.Term> toTerms(Iterable<? extends Type> types, TypeResolver.Subst subst)
-
toTerm
private Unifier.Term toTerm(PrimitiveType type)
-
toTerm
private Unifier.Term toTerm(Type type, TypeResolver.Subst subst)
-
-