一句话:提出线性类型语言 Sparcl,以部分可逆为常态,构造性保证程序可逆性并将双射作为特例。
完全可逆性要求函数为双射,但直接组合双射函数来开发可逆程序过于受限。本文转而研究部分可逆函数:当某些参数被固定后函数变为可逆,例如固定加法的一个操作数,或固定 Huffman 编码中的符号频率信息。作者设计了语言 Sparcl,使部分可逆成为常态、双射成为特例,从而在不牺牲正确性的前提下获得更强表达力。设计难点在于让普通编程部分与可逆部分自由交互,同时由构造保证可逆性。Sparcl 采用线性类型,并提供类型构造子区分可逆计算数据与普通数据。论文给出语法、类型系统与语义,并证明 Sparcl 正确保证程序可逆性;通过前序/中序重建树和 Huffman 编码等例子展示其表达力。
Invertibility is a fundamental concept in computer science, with various manifestations in software development (serializer/deserializer, parser/printer, redo/undo, compressor/decompressor, and so on). Full invertibility necessarily requires bijectivity, but the direct approach of composing bijective functions to develop invertible programs is too restrictive to be useful. In this paper, we take a different approach by focusing on partially-invertible functions—functions that become invertible if some of their arguments are fixed. The simplest example of such is addition, which becomes invertible when fixing one of the operands. More involved examples include entropy-based compression methods (e.g., Huffman coding), which carry the occurrence frequency of input symbols (in certain formats such as Huffman tree), and fixing this frequency information makes the compression methods invertible. We develop a language Sparcl for programming such functions in a natural way, where partial-invertibility is the norm and bijectivity is a special case, hence gaining significant expressiveness without compromising correctness. The challenge in designing such a language is to allow ordinary programming (the “partially” part) to interact with the invertible part freely, and yet guarantee invertibility by construction. The language Sparcl is linear-typed, and has a type constructor to distinguish data that are subject to invertible computation and those that are not. We present the syntax, type system, and semantics of the language, and prove that Sparcl correctly guarantees invertibility for its programs. We demonstrate the expressiveness of Sparcl with examples including tree rebuilding from preorder and inorder traversals and Huffman coding.