Ruby库的类型级计算
Type-Level Computations for Ruby Libraries
摘要:
许多研究人员已经探索了将静态类型引入动态语言的方法。然而,到目前为止,当类型依赖于值时,这样的系统不够精确,这通常发生在使用某些Ruby库时。例如,Ruby on Rails中数据库查询的类型安全性取决于查询中使用的表名和列名。为了解决这个问题,我们引入了CompRDL,这是一个Ruby的类型系统,它允许库方法类型签名包含类型级计算(或简称comp类型)。与表和列名的单例类型相结合,comp类型让我们可以为数据库查询方法提供类型签名,这些方法计算表的模式以产生非常精确的类型信息。哈希、数组和字符串库的Comp类型还可以提高精度,从而减少类型转换的需要。我们形式化了CompRDL,并证明了它的类型系统的声音。CompRDL插入运行时检查,以确保库方法遵守其计算类型,而不是对具有comp类型的库方法体进行类型检查(这些方法可能包括本机代码或复杂)。我们通过为几个Ruby核心库和数据库查询API编写带有类型级计算的注释来评估CompRDL。然后,我们使用这些注释来检查两个流行的Ruby库和四个Ruby on Rails Web应用程序的类型。我们发现注释相对紧凑,可以在我们的主题程序中成功地对132个方法进行类型检查。此外,使用类型级别的计算允许我们检查更多的表达属性,与没有类型级别的计算相比,手动插入的转换更少。在这个过程中,我们发现了两个类型错误和一个文档错误,开发人员已经确认了这一点。因此,我们相信CompRDL是将精确的静态类型检查引入动态语言的重要一步。
英文摘要:
Many researchers have explored ways to bring static typing to dynamic languages. However, to date, such systems are not precise enough when types depend on values, which often arises when using certain Ruby libraries. For example, the type safety of a database query in Ruby on Rails depends on the table and column names used in the query. To address this issue, we introduce CompRDL, a type system for Ruby that allows library method type signatures to include type-level computations (or comp types for short). Combined with singleton types for table and column names, comp types let us give database query methods type signatures that compute a table's schema to yield very precise type information. Comp types for hash, array, and string libraries can also increase precision and thereby reduce the need for type casts. We formalize CompRDL and prove its type system sound. Rather than type check the bodies of library methods with comp types---those methods may include native code or be complex---CompRDL inserts run-time checks to ensure library methods abide by their computed types. We evaluated CompRDL by writing annotations with type-level computations for several Ruby core libraries and database query APIs. We then used those annotations to type check two popular Ruby libraries and four Ruby on Rails web apps. We found the annotations were relatively compact and could successfully type check 132 methods across our subject programs. Moreover, the use of type-level computations allowed us to check more expressive properties, with fewer manually inserted casts, than was possible without type-level computations. In the process, we found two type errors and a documentation error that were confirmed by the developers. Thus, we believe CompRDL is an important step forward in bringing precise static type checking to dynamic languages.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。