在抽象语法树中键入信息

时间:2017-03-06 16:35:37

标签: types abstract-syntax-tree type-inference hindley-milner

抽象语法树中存在哪些类型信息? AST如何用于类型推理?在没有任何节点指示具体类型时,我不明白如何在给定AST的情况下导出类型输入和输出。这些类型是从树结构中推断出来的吗?例如有一堆IfStatement(Statement),所以它可能会返回一个bool?例如,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的类型推理的好的阅读材料。感谢。

1 个答案:

答案 0 :(得分:2)

  

AST如何用于类型推断?

类型推断通过遍历树传播“环境”(将字典名称映射到变量名称(包括函数)到其类型)来将未类型的AST转换为类型的AST。这是通过AST传播到叶子的。

文字整数或字符串的类型为intstring

在环境中查找变量的类型。

函数应用程序f(x)的类型,其中f: a -> bx: ab

其中fun x -> yx: ay: b的{​​{1}}的类型。

a -> b的{​​{1}}处,推论向环境添加绑定let x = y in z并推论y: a的类型(即整个{{1 }}表达式)在新环境的上下文中,以便遇到x: a时可以查找z

以此类推。 Hindley-Milner类型系统更为复杂,因为它包括泛型,但实现仅是获取有关类型变量的一系列约束并解决这些约束以尽可能多地得出类型变量。类型变量通常在let绑定中进行一般化,因此,例如,x: a定义了通用标识函数x

此外,在存在突变的情况下,源自广义类型的类型变量未得到概括,例如,let在OCaml中具有弱多态类型,称为let id x = x,表示“此类型变量只能解析为一种具体类型”,即∀a id: a -> a而不是ref None: '_a

使用'_a∃a的最小ML方言的类型推断少于100行代码,并且在现代计算机上只需合理的实现即可。

您可能会喜欢以下链接:

https://pfudke.wordpress.com/2014/11/20/hindley-milner-type-inference-a-practical-example-2/ http://okmij.org/ftp/ML/generalization.html http://okmij.org/ftp/Haskell/AlgorithmsH.html#teval http://www.cs.cornell.edu/courses/cs6110/2013sp/lectures/lec32-sp13.pdf https://www.doc.ic.ac.uk/~scd/icooolps15_GC.pdf http://janvitek.org/pubs/oopsla17a.pdf http://www.cs.columbia.edu/~sedwards/classes/2016/4115-spring/proposals/JSJS.pdf https://github.com/7sharp9/write-you-an-inference-in-fsharp https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT http://www.elsman.com/pdf/masters.pdf https://courses.cs.washington.edu/courses/csep505/03au/hw/hw1.html http://lucacardelli.name/Papers/BasicTypechecking.pdf http://okmij.org/ftp/ML/generalization.html http://www.smlnj.org/doc/Conversion/types.html