键入对统一问题的推断

时间:2008-12-09 13:58:49

标签: 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
                }

可以在统一问题中转移吗?

真的很感激任何帮助!

1 个答案:

答案 0 :(得分:2)

首先,重命名类型变量,以便表达式中的所有变量都不会与键入环境中的变量发生冲突。 (在您的示例中,这已经完成,因为表达式引用{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

第三,在必须保持的类型变量之间生成一组方程。您可以从下往上,从上到下或以其他方式执行此操作。有关您可能想要选择这种或那种方式的详细信息,请参阅Heeren, Bastiaan. Top Quality Type Error Messages. 2005.。这将导致类似:

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

请注意,如果在类型环境中使用相同的函数两次,我们需要更多的新类型变量(在第二步,上面)来统一,以便我们不会意外地使用所有调用cons相同的类型。对不起,我不确定如何更清楚地解释这一部分。在这里我们处于简单的情况,因为hd和cons只使用一次。

第四,统一这些方程,导致(如果我没有犯错误)类似的东西:

a4 = list(int)
a5 = int
a6 = int -> list(int) -> list(int)
a7 = list(int)
a8 = list(int) -> int
a0 = int

高兴,您现在知道原始表达式中每个子表达式的类型。

(公平的警告,在这些事情上,我自己有点像业余爱好者,我可能会在这里做出印刷错误或无意中被骗了。)