什么是类型状态?
TypeState对于语言设计来说指的是什么?我在一些关于 mozilla 的新语言 Rust 的讨论中看到了它。
What does TypeState refer to in respect to language design? I saw it mentioned in some discussions regarding a new language by mozilla called Rust.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意: Typestate 已从 Rust 中删除,仅留下有限版本(跟踪未初始化的和从变量移动的)。请参阅最后我的注释。
TypeState 背后的动机是类型是不可变的,但是它们的某些属性是动态的(基于每个变量)。
因此,我们的想法是创建关于类型的简单谓词,并使用编译器出于许多其他原因执行的控制流分析,以使用这些谓词静态修饰类型。
这些谓词实际上并没有由编译器本身检查,这可能太繁重,相反编译器会简单地根据图形进行推理。
作为一个简单的示例,您创建一个谓词
even
,如果数字为偶数,则返回true
。现在,您创建两个函数:
halve
,它仅作用于even
数字double
,它接受任何数字,并返回even
号码。请注意,
number
类型未更改,您不会创建evennumber
类型并复制之前作用于number
的所有函数。您只需将number
与名为even
的谓词组合起来即可。现在,让我们构建一些图表:
很简单,不是吗?
当然,当您有多个可能的路径时,它会变得有点复杂:
这表明您根据谓词集进行推理:
这可以通过函数的通用规则来增强:
,Rust 中的 TypeState 构建块:
check
:检查谓词是否成立,如果它没有失败< /code>,否则将谓词添加到谓词集中
请注意,由于 Rust 要求谓词是纯函数,因此如果可以证明谓词有效,则可以消除冗余的
check
调用此时已经成立。Typestate 缺乏的很简单:可组合性。
如果你仔细阅读说明,你会注意到这一点:
这意味着类型的谓词本身是无用的,实用程序来自注释函数。因此,在现有代码库中引入新谓词是一件无聊的事情,因为需要审查和调整现有函数以迎合解释它们是否需要/保留不变量。
当新的谓词出现时,这可能会导致函数以指数速度重复:不幸的是,谓词是不可组合的。他们本来要解决的设计问题(类型的激增,因此功能的激增)似乎没有得到解决。
Note: Typestate was dropped from Rust, only a limited version (tracking uninitialized and moved from variables) is left. See my note at the end.
The motivation behind TypeState is that types are immutable, however some of their properties are dynamic, on a per variable basis.
The idea is therefore to create simple predicates about a type, and use the Control-Flow analysis that the compiler execute for many other reasons to statically decorate the type with those predicates.
Those predicates are not actually checked by the compiler itself, it could be too onerous, instead the compiler will simply reasons in terms of graph.
As a simple example, you create a predicate
even
, which returnstrue
if a number is even.Now, you create two functions:
halve
, which only acts oneven
numbersdouble
, which take any number, and return aneven
number.Note that the type
number
is not changed, you do not create aevennumber
type and duplicate all those functions that previously acted onnumber
. You just composenumber
with a predicate calledeven
.Now, let's build some graphs:
Simple, isn't it ?
Of course it gets a bit more complicated when you have several possible paths:
This shows that you reason in terms of sets of predicates:
This can be augmented by the generic rule of a function:
And thus the building block of TypeState in Rust:
check
: checks that the predicate holds, if it does notfail
, otherwise adds the predicate to set of predicatesNote that since Rust requires that predicates are pure functions, it can eliminate redundant
check
calls if it can prove that the predicate already holds at this point.What Typestate lack is simple: composability.
If you read the description carefully, you will note this:
This means that predicates for a types are useless in themselves, the utility comes from annotating functions. Therefore, introducing a new predicate in an existing codebase is a bore, as the existing functions need be reviewed and tweaked to cater to explain whether or not they need/preserve the invariant.
And this may lead to duplicating functions at an exponential rate when new predicates pop up: predicates are not, unfortunately, composable. The very design issue they were meant to address (proliferation of types, thus functions), does not seem to be addressed.
它基本上是类型的扩展,您不仅检查一般情况下是否允许某些操作,而且还检查特定上下文中是否允许某些操作。所有这些都在编译时完成。
原始论文实际上非常可读。
It's basically an extension of types, where you don't just check whether some operation is allowed in general, but in this specific context. All that at compile time.
The original paper is actually quite readable.
有一个为 Java 编写的类型状态检查器,Adam Warski 的说明页提供了一些有用的信息。我只是自己弄清楚这些材料,但如果您熟悉 QuickCheck for Haskell,QuickCheck 在单子状态中的应用似乎很相似:对状态进行分类并解释当它们通过接口发生突变时它们如何变化。
There's a typestate checker written for Java, and Adam Warski's explanatory page gives some useful information. I'm only just figuring this material out myself, but if you are familiar with QuickCheck for Haskell, the application of QuickCheck to monadic state seems similar: categorise the states and explain how they change when they are mutated through the interface.
Typestate解释为:
Typestate is explained as: