Lambda 统一问题的类型推理

Lambda 统一问题的类型推理,lambda,type-inference,unification,Lambda,Type Inference,Unification,有人知道类型推断问题是如何解决的吗 E > hd (cons 1 nil) : α0 使用打字环境 E={ hd : list(α1 ) → α1 , cons : α2 → list(α2 ) → list(α2 ), nil : list(α3 ), 1 : int }

有人知道类型推断问题是如何解决的吗

E > hd (cons 1 nil) : α0
使用打字环境

                E={
                   hd : list(α1 ) → α1 ,
                   cons : α2 → list(α2 ) → list(α2 ),
                   nil : list(α3 ),
                   1 : int
                }
可以在统一问题中转移吗


任何帮助都将不胜感激

首先,重命名类型变量,使表达式中的变量不会与类型环境中的变量冲突。(在您的示例中,这已经完成,因为表达式引用了{a0},而键入环境引用了{a1、a2、a3}

第二,使用新的类型变量,为表达式中的每个子表达式创建一个类型变量,生成如下内容:

nil : a4
1 : a5
cons : a6
(cons 1 nil) : a7
hd : a8
hd (cons 1 nil) : a0 // already had a type variable
a4 = list(a3) // = E(nil)
a5 = int // = E(1)
a6 = a2 -> list(a2) -> list(a2) // = E(cons)

// now it gets tricky, since we need to deal with application for the first time
a5 = a2 // first actual param of cons matches first formal param of cons
a4 = list(a2) // second actual param of cons matches type of second formal param of cons
a7 = list(a2) // result of (cons 1 nil) matches result type of cons with 2 params

a8 = list(a1) -> a1 // = E(hd)    

// now the application of hd
a7 = list(a1) // first actual param of hd matches type of first formal param of hd
a0 = a1 // result of hd (cons 1 nil) matches result type of hd with 1 param
a4 = list(int)
a5 = int
a6 = int -> list(int) -> list(int)
a7 = list(int)
a8 = list(int) -> int
a0 = int
第三,在必须保持的类型变量之间生成一组方程式。您可以从下至上、从上到下或以其他方式执行此操作。有关为什么要选择一种或另一种方式的详细信息,请参阅。这将导致以下结果:

nil : a4
1 : a5
cons : a6
(cons 1 nil) : a7
hd : a8
hd (cons 1 nil) : a0 // already had a type variable
a4 = list(a3) // = E(nil)
a5 = int // = E(1)
a6 = a2 -> list(a2) -> list(a2) // = E(cons)

// now it gets tricky, since we need to deal with application for the first time
a5 = a2 // first actual param of cons matches first formal param of cons
a4 = list(a2) // second actual param of cons matches type of second formal param of cons
a7 = list(a2) // result of (cons 1 nil) matches result type of cons with 2 params

a8 = list(a1) -> a1 // = E(hd)    

// now the application of hd
a7 = list(a1) // first actual param of hd matches type of first formal param of hd
a0 = a1 // result of hd (cons 1 nil) matches result type of hd with 1 param
a4 = list(int)
a5 = int
a6 = int -> list(int) -> list(int)
a7 = list(int)
a8 = list(int) -> int
a0 = int
请注意,如果从类型环境中使用相同的函数两次,我们将需要更多的新类型变量(在上面的第二步中)为了与统一,这样我们就不会意外地使所有对cons的调用使用相同的类型。我不知道如何更清楚地解释这一部分,抱歉。这里是简单的例子,因为hd和cons都只使用一次

第四,统一这些方程,得出(如果我没有犯错误的话)如下结果:

nil : a4
1 : a5
cons : a6
(cons 1 nil) : a7
hd : a8
hd (cons 1 nil) : a0 // already had a type variable
a4 = list(a3) // = E(nil)
a5 = int // = E(1)
a6 = a2 -> list(a2) -> list(a2) // = E(cons)

// now it gets tricky, since we need to deal with application for the first time
a5 = a2 // first actual param of cons matches first formal param of cons
a4 = list(a2) // second actual param of cons matches type of second formal param of cons
a7 = list(a2) // result of (cons 1 nil) matches result type of cons with 2 params

a8 = list(a1) -> a1 // = E(hd)    

// now the application of hd
a7 = list(a1) // first actual param of hd matches type of first formal param of hd
a0 = a1 // result of hd (cons 1 nil) matches result type of hd with 1 param
a4 = list(int)
a5 = int
a6 = int -> list(int) -> list(int)
a7 = list(int)
a8 = list(int) -> int
a0 = int
高兴吧,您现在知道了原始表达式中每个子表达式的类型

(值得注意的是,在这些问题上,我自己也算是个业余爱好者,我很可能在这里的某个地方犯了印刷错误或无意中作弊。)