如何使这些动态类型函数类型安全?

时间:2021-07-11 16:01:21

Is there any programming language (or type system) in which you could express the following Python-functions in a statically typed and type-safe way (without having to use casts, runtime-checks etc)?

是否有任何编程语言(或类型系统)可以在静态类型和类型安全的方式(无需使用强制转换、运行时检查等)中表达以下python函数?

#1:

# 1:

# My function - What would its type be? 
def Apply(x):
    return x(x)

# Example usage
print Apply(lambda _: 42)

#2:

# 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()

3 个解决方案

#1


4  

1# This is not typeable with a finite type. This means that very few (if any) programming languages will be able to type this.

1#这不是一个有限类型的类型。这意味着只有很少的(如果有的话)编程语言能够输入这个。

However, as you have demonstrated, there is a specific type for x that allows the function to be typed:

但是,正如您所演示的,x有一个特定的类型,允许对函数进行类型化:

x :: t -> B

Where B is some concrete type. This results in apply being typed as:

B是具体的类型。这导致应用被打印为:

apply :: (t -> B) -> B

Note that Hindley-Milner will not derive this type.

注意,Hindley-Milner不会派生这种类型。

2# This is easy to represent in Haskell (left as an exercise to the reader...)

2#这很容易在Haskell中表示(留给读者作为练习…)

#2


2  

I found a Haskell solution for #1 using Rank-N-Types (just for GHCi)

我为#1找到了一个Haskell解决方案,它使用的是rank - n类型(仅用于GHCi)

{-# LANGUAGE RankNTypes #-}
apply :: (forall a . a -> r) -> r
apply x = x x

apply $ const 42 -- Yields 42

#3


0  

When it comes to example #1, you would have to specify the return type of Apply(), and then all functions x that you pass also must return this. Most statically typed languages would not be able to do that safely without checks, as the x function you pass in can return whatever.

当涉及到示例#1时,您必须指定Apply()的返回类型,然后传递的所有函数x也必须返回这个类型。大多数静态类型的语言在没有检查的情况下都无法安全地完成这一任务,因为您传入的x函数可以返回任何内容。

In example #2 the type of the iterator objects are that they are iterators. If you mean what they return, they return iterators. I don't see why that wouldn't be possible in a static system, but maybe I'm missing something.

在示例#2中,迭代器对象的类型是它们是迭代器。如果您的意思是它们返回什么,它们返回迭代器。我不明白为什么在静态系统中这是不可能的,但也许我漏掉了什么。

#1


4  

1# This is not typeable with a finite type. This means that very few (if any) programming languages will be able to type this.

1#这不是一个有限类型的类型。这意味着只有很少的(如果有的话)编程语言能够输入这个。

However, as you have demonstrated, there is a specific type for x that allows the function to be typed:

但是,正如您所演示的,x有一个特定的类型,允许对函数进行类型化:

x :: t -> B

Where B is some concrete type. This results in apply being typed as:

B是具体的类型。这导致应用被打印为:

apply :: (t -> B) -> B

Note that Hindley-Milner will not derive this type.

注意,Hindley-Milner不会派生这种类型。

2# This is easy to represent in Haskell (left as an exercise to the reader...)

2#这很容易在Haskell中表示(留给读者作为练习…)

#2


2  

I found a Haskell solution for #1 using Rank-N-Types (just for GHCi)

我为#1找到了一个Haskell解决方案,它使用的是rank - n类型(仅用于GHCi)

{-# LANGUAGE RankNTypes #-}
apply :: (forall a . a -> r) -> r
apply x = x x

apply $ const 42 -- Yields 42

#3


0  

When it comes to example #1, you would have to specify the return type of Apply(), and then all functions x that you pass also must return this. Most statically typed languages would not be able to do that safely without checks, as the x function you pass in can return whatever.

当涉及到示例#1时,您必须指定Apply()的返回类型,然后传递的所有函数x也必须返回这个类型。大多数静态类型的语言在没有检查的情况下都无法安全地完成这一任务,因为您传入的x函数可以返回任何内容。

In example #2 the type of the iterator objects are that they are iterators. If you mean what they return, they return iterators. I don't see why that wouldn't be possible in a static system, but maybe I'm missing something.

在示例#2中,迭代器对象的类型是它们是迭代器。如果您的意思是它们返回什么,它们返回迭代器。我不明白为什么在静态系统中这是不可能的,但也许我漏掉了什么。