iPTR: Learning a representation for interactive program translation retrieval

Program translation contributes to many real world scenarios , such as porting codebases written in an obsolete or deprecated language to a modern one or reimplementing existing projects in one ’ s preferred programming language . Existing data-driven approaches either require large amounts of training data or neglect significant characteristics of programs . In this paper , we present IPTR for interactive code translation retrieval from Big Code . IPTR uses a novel code representation technique that encodes structural characteristics of a program and a predictive transformation technique to transform the representation into the target programming language . The transformed representation is used for code retrieval from Big Code . With our succinct representation , the user can easily update and correct the returned results to improve the retrieval process . Our experiments show that IPTR outperforms supervised baselines in terms of program accuracy . 1 INTRODUCTION . Numerous programs are being developed and released online . To port codebases written in obsolete or deprecated languages to a modern one ( Lachaux et al. , 2020 ) , or to further study , reproduce and apply them on various platforms , these programs require corresponding versions in different languages . In cases when developers do not make the translation efforts themselves , third-party users have to manually translate the software to their needed language , which is time consuming and error prone because they have to be the expert in both languages . Also , hard-wired cross-language compilers still require heavy human intervention for adaptation and are limited between some specified types of programming . In this paper , we discuss the potentials of data-driven methods that exploit existing big code resources to support code translation . The abundance of open source programs on the internet provides opportunities for new applications , such as workflow generation ( Derakhshan et al. , 2020 ) , data preparation ( Yan & He , 2020 ) , and transformation retrieval ( Yan & He , 2018 ) . Code translation is another application that is gaining attention ( Lachaux et al. , 2020 ) . Data-driven program translation . Inspired by natural language translation , one line of approaches trains a translation model from large amounts of code data either in a supervised ( Nguyen et al. , 2013 ; 2015 ; Chen et al. , 2018 ) or weakly-supervised fashion ( Lachaux et al. , 2020 ) . Supervised approaches require a parallel dataset to train the translation model . In parallel datasets , programs in different languages are considered to be “ semantically aligned ” . Obtaining the parallel datasets in programming languages is hard because the translations have to be handwritten most of the time . Besides massive human efforts , it is also a tricky problem to extract general textual features that apply to every programming language . A recent weakly-supervised method ( Lachaux et al. , 2020 ) pretrains the translation model on the task of denoising randomly corrupted programs and optimizes the model through back-translation . However , this method still relies on high-quality training data . Further , all these approaches directly reuse NLP approaches that neglect the special features of programming languages . Another potential approach is to use a retrieval system to obtain translation candidates directly from Big Code , which refers to well-maintained program repositories . However , existing code retrieval systems , such as Sourcerer ( Linstead et al. , 2009 ) , lack the proper capabilities for code-to-code search and cross-language code retrieval . These methods ask users to give feedback on several preset metrics and questions ( Wang et al. , 2014 ; Dietrich et al. , 2013 ; Martie et al. , 2015 ; Sivaraman et al. , 2019 ) . None of these methods is tailored to cross-language retrieval . In this paper , we propose an interactive program translation retrieval system IPTR based on a novel and generalizable code representation that retains important code properties . The representation not only encodes textual features but structural features that can be generalized across all imperative programming languages . We further propose a query transformation model based on autoencoders to transform the input program representation to a representation that has properties of the target language . Due to the succinct form of our code representation , IPTR can adapt the original query based on user annotations . This methodology can compete with existing statistical translation models that require a large amount of training data . In short , we make the following main contributions : • We propose IPTR , an interactive cross-language code retrieval system with a program feature representation that additionally encodes code structure properties . • We further propose a novel query transformation model that learns a refined code representation in the target language before using it for retrieval . This model can be trained in an unsupervised way but also improved through active learning . • Based on our succinct code representation , we propose a user feedback mechanism that enables IPTR to successively improve its results . 2 SYSTEM OVERVIEW . We propose IPTR , an interactive cross language code retrieval system that supports program translation on multiple programming languages . Problem Definition . Given a piece of source program Ps written in language Ls , a selected target language Lt , and a large program repository Dp = { P1 , P2 , ... , Pn } , the goal is to find the best possible translation Pt of Ps in Lt from Dp . The problem is to design an effective program feature representation that generalizes to many languages and can be updated through user feedback . Our solution : IPTR The workflow of IPTR is shown in Figure 1 . IPTR first constructs a succinct but informative feature representation for input programs ( Section 3.1 ) . Since the target is to identify a similar program in the target language , IPTR then applies a query transformation model ( QTM ) to transform this representation into an estimated feature representation of the translation ( Section 3 ) . The transformation model is trained in an unsupervised manner but can also be updated dynamically through active learning ( Section 3.2.2 ) . Finally , this new representation will be used as a query to retrieve the program that has similar features from the database . In addition , as an interactive system , IPTR allows the user to give feedback on the retrieved translation ( Section 4 ) . The user can either accept the result or make corrections . Based on our structured and informative feature representation , IPTR can easily and quickly adapt the query based on raw user corrections . Then with the new query , it may identify a more appropriate translation candidate in the second retrieval attempt . 3 PROGRAM REPRESENTATION . To retrieve a promising program translation from a large code database , IPTR needs an effective and efficient query . Directly retrieving based on raw code is impractical . In contrast to existing methods that generate queries based on either keywords ( Linstead et al. , 2009 ) or preset metrics and questions ( Martie et al. , 2015 ) , IPTR generates a feature representation that effectively combines structural properties of the program and textual features . It further uses a query transformation model ( QTM ) to generate features in the target language . 3.1 BASIC ENCODING OF PROGRAM STRUCTURE AND TEXT . Due to the special and non-trival structure of programming languages compared to natural languages , we take both structural and textual features into consideration . The structural features of a program can be represented by its syntax tree where each tree node denotes a code construct . One can also use control flow graph ( CFG ) that captures the dependence between code basic blocks and procedures to represent the code behavior . However , our goal is to support program translation for any granularity of program , code behavior is hard to measure when the code fragment is not a complete code block . Further CFG is much more difficult to construct than syntax trees . As syntactical similarity also plays a significant role , we pick syntax trees as the basis of our representation to also capture the low-level syntactic structure within code blocks . One can also combine CFG and syntax trees to reserve more information . However this would trade-off the simplicity of the retrieval query . We show that with analysis of the static AST features we already achieve high program accuracy . The syntax tree can be either a concrete syntax tree ( CST ) or an abstract syntax tree ( AST ) ( Alon et al. , 2018 ; 2019 ; Chen et al. , 2018 ) . A CST depicts nodes with complete structural information , such as all the tokens in the code while the AST is more abstract and only displays structural or content-related details . For more details on CSTs and ASTs , we provide examples in the Appendix A.2 . As the CST is quite verbose and the AST does not generalize to multiple languages , we fall back on the low-level CST as a basis , and take the philosophy of AST as an inspiration to construct a unified abstract representation . Specifically , IPTR first simplifies the CST by removing semantically repetitive nodes such as equalityExpression and == and intermediate nodes and generates a more simplified but still informative syntax tree . As shown in Figure 2 , a piece of Javascript code is first converted into a simplified syntax tree ( details of simplifying CST are shown in Appendix A.2 ) . Inspired by prior work ( Alon et al. , 2018 ) , IPTR further simplifies it by extracting a set of onedimensional paths that connect the program elements of the two-dimensional tree . Our method abstracts these paths by only keeping the three nodes that enclose the most critical information on a path : for each pair of leaf-nodes in the CST , IPTR keeps the nodes with their values and the root node of the statement . It drops all other intermediate nodes on this path . The extracted abstract paths of the JavaScript Program example are also shown in Figure 2 . By simply matching nodes with similar names from different languages , IPTR can classify these paths into different types and generalize them to multiple programming languages . Thus , the structural feature of a program can be succinctly represented by the different types of paths p1 , p2 , ... , pj it contains . To additionally incorporate textual features , IPTR treats extracted paths as plain text and keeps all the text tokens t1 , t2 , ... , tk appearing in each extracted path to generate text features . Finally , we can use these tokens ( textual features ) together with different types of paths ( structural features ) illustrated above as feature elements of programs . For each input program , we generate a feature vector consisting of the feature element frequencies in that program . Let f be the occurrence frequency of feature elements , then the final feature representation will be [ fe1 , fe2 , ... , fen ] = [ fp1 , fp2 , ... , fpnp , ft1 , ft2 , ... , ftnt ] . In our experiment , the number of different path types is about 5,000 on average for each language pair . The number of tokens can be restricted by a hyper-parameter max vocab to trade off effectiveness and efficiency . Our default setting is 10,000 . These elements can also be used as index keys to filter the database , and by calculating the similarity of feature vectors IPTR can retrieve the most similar program in target language , which is the translation candidate . Noted that we simplify the feature representation presented here due to space limitation . In actual IPTR , we also consider the dependencies between structural and textual features . In contrast to existing work ( Cheng et al. , 2016 ; 2017 ) that suggests to extract all the text from a program and treat it in isolation , IPTR considers textual features in strong dependency with the structural features to leverage more context from the structure . For the sake of simplification and to avoid computation overhead , IPTR only processes text that appears in the extracted paths . In this way , IPTR can run an efficient hierarchical retrieval mechanism : first calculating the structural similarity , then comparing the textual similarity in each common path type . We show the details of our comprehensive feature representation in Appendix A.3 .

Summary: This work proposes a retrieval-based approach for program translation. Existing ML/DL models for program translation typically design a decoder to directly generate the code in the target language. On the contrary, this work designs iPTR, which first computes a feature representation of the target code, then retrieves the most similar code in a large code corpus. Specifically, iPTR includes a query transformation model (QTM), which generates the feature representation of the target code, given the feature representation of the source code as the input. The feature representation includes the information of tokens in the code, and the paths in the syntax trees. The idea of using the paths is similar to the code2vec paper. QTM could be trained without parallel data between source and target codes. Specifically, encoders and decoders of different programming languages could be trained in a similar way to the training of autoencoders. This idea is similar to TransCoder. Meanwhile, they show that iPTR could be trained with active learning and user feedback, where they use active learning to acquire limited parallel training data, and user feedbacks are corrections of the wrong output code. They compare their approach to existing ML/DL program translation models, as well as code retrieval systems. They show that iPTR performs better than other baselines, even without active learning and feedbacks. Not surprisingly, active learning and incorporating user feedbacks further improve the performance.