我目前正在尝试使用OCaml语言实现类型推理算法(统一算法)。我遇到了一些执行方面的困难,希望有人能给我一些帮助。
让我提供一些我正在尝试实现的背景信息。
[(TypeVar "t1", TypeFunc (TypeVar "t2", TypeVar "t3"))]
这个
(type * type) list
t1
映射到的函数
t2 -> t3
.
[(TypeVar "t1", TypeFunc (TypeVar "t1", TypeVar "t3"))]
这会给我们一个错误,因为
t1 = t1 -> t3
下面是我试图实现的实际OCaml函数,以捕捉这一矛盾:
let contradiction_check (a, t) =
List.exists (fun (x, _) -> x = a) t;;
let t1 = TypeVar "t1";;
let t2 = TypeFunc (TypeVar "t2", TypeVar "t3");;
这段代码的问题首先是
t2
[(TypeVar "t1", TypeFunc (TypeVar "t2", TypeVar "t3"))]
并检查元组的左侧是否出现在右侧。
我想我的具体问题是:是否有可能实现
List.exists
作为元组的版本?我试过手动编写函数,但似乎比我最初想象的要复杂。
[(TypeVar "t1", TypeFunc (TypeFunc (TypeVar "t2", TypeVar "t3"),
TypeFunc (TypeVar "t1", TypeVar "t4")))]
(** t1 = (t2 -> t3) -> (t1 -> t4) **)
欢迎任何反馈。谢谢您。