Hint for SML type inference

半城伤御伤魂 提交于 2019-12-02 03:43:35

Let's do this manually, step-by-step:

fun compCompA42 x = comp compA42 x
  1. It's a function, so compCompA42 has type α -> β.
  2. compCompA42's return value must be of the same type as comp compA42 x, i.e. β = typeof(comp compA42 x).
  3. We already now the most general type for comp:

    (a -> b) -> (b -> c) -> a -> c

Now, we need to specialize it for the case when a -> b = typeof(compA42) and (b -> c) = α:

  1. a -> b = typeof(compA42) = (int -> d) -> int -> d. From this equation follows that a = int -> d and b = int -> d.

  2. So, α = b -> c = (int -> d) -> c and β = typeof(comp compA42 x) = a -> c = (int -> d) -> c.

  3. Finally, our most general type for compCompA42 is

    α -> β = ((int -> d) -> c) -> (int -> d) -> c.


Observe that you can always make some SML interpreter (e.g., smlnj) show you types:
- fun compCompA42 x = comp compA42 x;
val compCompA42 = fn : ((int -> 'a) -> 'b) -> (int -> 'a) -> 'b

And it's the same type we've got manually (just rename d to 'a and c to 'b).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!