Dar*_*rio 9 python language-agnostic type-theory
是否有任何编程语言(或类型系统),您可以在其中以静态类型和类型安全的方式表达以下Python函数(不必使用强制转换,运行时检查等)?
#1:
# My function - What would its type be?
def Apply(x):
return x(x)
# Example usage
print Apply(lambda _: 42)
Run Code Online (Sandbox Code Playgroud)
#2:
white = None
black = None
def White():
for x in xrange(1, 10):
print ("White move #%s" % x)
yield black
def Black():
for x in xrange(1, 10):
print ("Black move #%s" % x)
yield white
white = White()
black = Black()
# What would the type of the iterator objects be?
for it in white:
it = it.next()
Run Code Online (Sandbox Code Playgroud)
1# 这不能用有限类型来输入。这意味着很少(如果有的话)编程语言能够输入此内容。
然而,正如您所演示的,x 有一个特定的类型,允许输入该函数:
x :: t -> B
Run Code Online (Sandbox Code Playgroud)
哪里B有具体类型。这会导致apply输入为:
apply :: (t -> B) -> B
Run Code Online (Sandbox Code Playgroud)
请注意,Hindley-Milner 不会派生此类型。
2# 这很容易用 Haskell 来表示(留给读者作为练习......)