【发布时间】:2017-07-26 15:08:37
【问题描述】:
抽象语法树中存在哪些类型信息? AST 如何用于类型推断?当没有节点指示具体类型时,我不明白如何在给定 AST 的情况下派生类型输入和输出。是否仅从树结构推断出类型?例如有一堆IfStatement(Statement),所以很可能返回一个布尔值?例如,javalang python 模块使用这些 AST 节点:
CompilationUnit(Node)
Import(Node)
Documented(Node)
Declaration(Node)
Type(Node)
TypeArgument(Node)
TypeParameter(Node)
Annotation(Node)
ElementValuePair(Node)
ElementArrayValue(Node)
ArrayInitializer(Node)
VariableDeclarator(Node)
InferredFormalParameter(Node)
Statement(Node)
SwitchStatementCase(Node)
ForControl(Node)
EnhancedForControl(Node)
Expression(Node)
EnumBody(Node)
VariableDeclaration(Declaration)
FormalParameter(Declaration)
TryResource(Declaration)
CatchClauseParameter(Declaration)
AnnotationMethod(Declaration)
BasicType(Type)
ReferenceType(Type)
TypeDeclaration(Declaration, Documented)
PackageDeclaration(Declaration, Documented)
ConstructorDeclaration(Declaration, Documented)
EnumConstantDeclaration(Declaration, Documented)
ClassDeclaration(TypeDeclaration)
EnumDeclaration(TypeDeclaration)
InterfaceDeclaration(TypeDeclaration)
AnnotationDeclaration(TypeDeclaration)
Member(Documented)
MethodDeclaration(Member, Declaration)
FieldDeclaration(Member, Declaration)
ConstantDeclaration(FieldDeclaration)
LocalVariableDeclaration(VariableDeclaration)
IfStatement(Statement)
WhileStatement(Statement)
DoStatement(Statement)
ForStatement(Statement)
AssertStatement(Statement)
BreakStatement(Statement)
ContinueStatement(Statement)
ReturnStatement(Statement)
ThrowStatement(Statement)
SynchronizedStatement(Statement)
TryStatement(Statement)
SwitchStatement(Statement)
BlockStatement(Statement)
StatementExpression(Statement)
CatchClause(Statement)
Assignment(Expression)
TernaryExpression(Expression)
BinaryOperation(Expression)
Cast(Expression)
MethodReference(Expression)
LambdaExpression(Expression)
Primary(Expression)
ArraySelector(Expression)
Literal(Primary)
This(Primary)
MemberReference(Primary)
Invocation(Primary)
SuperMemberReference(Primary)
ClassReference(Primary)
Creator(Primary)
ExplicitConstructorInvocation(Invocation)
SuperConstructorInvocation(Invocation)
MethodInvocation(Invocation)
SuperMethodInvocation(Invocation)
VoidClassReference(ClassReference)
ArrayCreator(Creator)
ClassCreator(Creator)
InnerClassCreator(Creator)
给定一些玩具代码,它会为函数输出以下 AST:
public class HelloWorld{
public static void main(String args[]){
add(5);
}
public static int add(int x){
return x+0;
}
}
(MethodDeclaration
(FormalParameter
(ReferenceType)
)
(StatementExpression
(MethodInvocation
(Literal)
)
)
)
另外,如果有人能指出一些关于给定 AST 的类型推断的优秀阅读材料。谢谢。
【问题讨论】:
-
尝试使用
Hindley-Milner和AST进行搜索,看看是否能找到所需的内容。我很快找到了Hindley-Milner Type Checking希望对您有所帮助。 -
@GuyCoder 我知道 Hindley-Milner 算法。我还没有找到任何详细的例子来说明如何处理叶子是调用其他函数或 ADT 的 AST。另外,我不明白如果仅给出 AST,如何推断文字的值。如果基本 AST 的叶子以要添加的文字结尾,您如何知道是否添加了浮点数或整数?在我见过的所有示例中,它们都假设您已获得叶文字的值。
-
希望我知道的不仅仅是给 cmets。其他建议 1. 添加标签hindley-milner 以获得更多曝光。 2. 试试Computer Science Stack Exchange site。 3. 我知道很多专家在lambda-the-ultimate 闲逛,但准备好阅读大量理论和参考论文。
-
@GuyCoder 好的。谢谢指点。
-
经典书籍Types and Programming Languages 有一章叫做
Type Reconstruction,这是一组不同的关键词。如果你真的很绝望,我知道一些 ML 方言的专业编译器已经内置了它,但弄清楚它可能需要很长时间,F#,OCaml。我敢打赌,但还没有看过SML/NJ和GHC
标签: types abstract-syntax-tree type-inference hindley-milner