suh*_*hwi 5 compiler-construction ocaml haskell functional-programming hindley-milner
I'm making a strongly typed toy functional programming language. It uses the Hindley Milner algorithm as type inference algorithm.
Implementing the algorithm, I have a question on how to infer types of the mutually recursive functions.
let rec f n = if n == 0 then 0 else g (n - 1)
let rec g n = if n == 0 then 0 else f (n - 1)
Run Code Online (Sandbox Code Playgroud)
f and g are mutually recursive functions. Now, when the type checker is inferring the type of function f, it should also be able to infer the type of function g, since it is a subexpression.
But, in that moment, function g is not defined yet. Therefore, the type checker doesn't even know the existence of function g, as well as the type of function g, obviously.
What are some solutions that real world compilers/intepreters use?
在 OCaml 中,相互递归的值由关键字而and不是另一个分隔let rec。当类型系统到达递归定义时,它将所有递归名称添加到环境中,然后像平常一样继续。
更新(感谢 KA Buhr):
完全有可能创建一个具有类型'a('a新鲜)的新变量,然后将其统一。确保在正确的位置概括变量(通常在定义之后)。
| 归档时间: |
|
| 查看次数: |
861 次 |
| 最近记录: |