Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types 抽象语法树中的类型信息_Types_Abstract Syntax Tree_Type Inference_Hindley Milner - Fatal编程技术网

Types 抽象语法树中的类型信息

Types 抽象语法树中的类型信息,types,abstract-syntax-tree,type-inference,hindley-milner,Types,Abstract Syntax Tree,Type Inference,Hindley Milner,抽象语法树中存在哪些类型信息?AST如何用于类型推断?我不明白在没有任何节点指示具体类型的情况下,如何在给定AST的情况下派生类型输入和输出。是否仅从树结构推断类型?e、 g.有一堆IfStatement(Statement),所以很可能返回bool?例如,javalang python模块使用以下AST节点: CompilationUnit(Node) Import(Node) Documented(Node) Declaration(Node) Type(Node) TypeArgument

抽象语法树中存在哪些类型信息?AST如何用于类型推断?我不明白在没有任何节点指示具体类型的情况下,如何在给定AST的情况下派生类型输入和输出。是否仅从树结构推断类型?e、 g.有一堆
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类型推断的好阅读材料。谢谢

AST如何用于类型推断

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

文本整数或字符串的类型为
int
string

在环境中查找变量的类型

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

funx->y
的类型,其中
x:a
y:b
a->b

在z中的a
let x=y,其中
y:a
,推理将绑定
x:a
添加到环境中,并在新环境的上下文中推断
z
的类型(这是整个
let
表达式的类型),以便在遇到
x
时可以查找
x:a

等等。Hindley-Milner类型系统更为复杂,因为它包含泛型,但实现只不过是获得一系列关于类型变量的约束,并解决这些约束以尽可能多地计算出类型变量。类型变量通常在
let
绑定处通用,因此,例如,
let id x=x
定义了一个通用标识函数
∀a id:a->a

此外,在存在变异的情况下,源于扩展类型的类型变量不会泛化,因此,例如,
ref None:“'u a
具有弱多态类型,在OCaml中表示为
”,意思是“该类型变量只能解析为一个具体类型”,即
∃a
而不是
∀x

具有
int
fun
的最小ML方言的类型推断不到100行代码,并且在现代计算机上,一个合理的实现是快速的

您可能会喜欢以下链接:


尝试使用
Hindley Milner
AST
搜索,看看是否找到了所需内容。“我很快就找到了帮助我的希望。”GuyCoder我知道Hindley-Milner算法。我还没有找到任何关于如何处理AST的详细示例,其中叶子是对其他函数或ADT的调用。此外,我不明白如果只给出AST,如何推断文本的值。如果基本AST的叶子以要添加的文本结尾,您如何知道是否添加了浮点或整数?在我所看到的所有例子中,他们都假设你得到了叶文字的值。但愿我知道的比仅仅给出注释更多。其他建议1。在SO处添加更多曝光的标签。2.试试这个。3.我知道很多专家都会去,但要准备好大量的理论和参考文献。@GuyCoder Ok。谢谢你的指点。这本经典的书有一章叫做
类型重构
,这是一组不同的关键词。如果你真的很绝望,我知道有几个专业的ML方言编译器都内置了它,但要想弄清楚它可能需要相当长的时间。我敢打赌,但还没看,而且