我是 SML 的新手,我正在尝试练习 SML 类型参考。我正在尝试演绎以下类型:
a)fun add42 x =x+42
b)fun comp F G = let fun C x = G(F(x)) in C end
c)fun compA42 x = comp add42 x
d)val foo = compA42 add42
e)fun compCompA42 x = comp compA42 x
我认为前四个的解决方案是:
a)int->int
b)(a->b)->(b->c)->a->c
c)(int->a)->int->a
d)int->int
但我对最后一个有点困惑。
有没有提示扣除最后一个类型?
多谢。
让我们一步一步手动执行此操作:
fun compCompA42 x = comp compA42 x
- 这是一个函数,所以
compCompA42
有类型α -> β
.
-
compCompA42
的返回值必须与以下类型相同comp compA42 x
, i.e. β = typeof(comp compA42 x)
.
-
我们现在已经有了最通用的类型comp
:
(a -> b) -> (b -> c) -> a -> c
现在,我们需要专门针对以下情况:a -> b = typeof(compA42)
and (b -> c) = α
:
a -> b = typeof(compA42) = (int -> d) -> int -> d
。从这个方程可以得出a = int -> d
and b = int -> d
.
So, α = b -> c = (int -> d) -> c
and β = typeof(comp compA42 x) = a -> c = (int -> d) -> c
.
-
最后,我们最通用的类型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
它与我们手动获得的类型相同(只需重命名d
to 'a
and c
to 'b
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)