How does the compiler determine types in sml

匆匆过客 提交于 2019-11-29 08:19:04
Simon Shine

How does the compiler determine types in sml

Standard ML uses Damas-Hindley-Milner type inference, but there are several algorithms for resolving the most general type, notably Algorithm W. The type-inference rules are explained in e.g.

At what point does 'a and 'b specialize to bool?

The exact point at which 'a and 'b specialize to bool depends on the algorithm used. I will jump ahead and skip some steps where expressions are given types and those types are unified.

When it says if (f a) then a else raise Break, then

  • a : t1
  • f : t1 → bool
  • if ... : t1

And similarly, when it says if not (f a) then a else b, we additionally have that

  • b : t1

Lastly, f (raise Break) handle Break => if ... then a else ... gives us that

  • f (raise Break) : bool
  • t1 = bool, since f (raise Break) and if ... then a else ... must have the same type.

The part you were missing was probably to realize that for x handle ... => y, x and y must have the same type. Otherwise you would have an expression that changes type depending on whether an exception is thrown. That is not possible when types are resolved during compilation and exceptions are thrown at run-time. A practical example where this is relevant is negative tests, where the type on the left side of handle ... is forced to be that of the right side:

val factorial_robust_test = (factorial -1; false)
                            handle Domain => true
                                 | Overflow => false
                                 | _ => false
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!