Hostname: page-component-76d6cb85b7-5qg8f Total loading time: 0 Render date: 2026-07-13T00:21:35.743Z Has data issue: false hasContentIssue false

Migrating gradual types

Published online by Cambridge University Press:  06 October 2022

JOHN PETER CAMPORA III
Affiliation:
University of Louisiana at Lafayette, Lafayette, LA 70504, USA (e-mail: petecampora@gmail.com; chen@louisiana.edu)
SHENG CHEN
Affiliation:
University of Louisiana at Lafayette, Lafayette, LA 70504, USA (e-mail: petecampora@gmail.com; chen@louisiana.edu)
MARTIN ERWIG
Affiliation:
Oregon State University, Corvallis, OR 97331, USA (e-mail: erwig@oregonstate.edu; walkiner@oregonstate.edu)
ERIC WALKINGSHAW
Affiliation:
Oregon State University, Corvallis, OR 97331, USA (e-mail: erwig@oregonstate.edu; walkiner@oregonstate.edu)
Rights & Permissions [Opens in a new window]

Abstract

Gradual typing allows programs to enjoy the benefits of both static typing and dynamic typing. While it is often desirable to migrate a program from more dynamically typed to more statically typed or vice versa, gradual typing itself does not provide a way to facilitate this migration. This places the burden on programmers who have to manually add or remove type annotations. Besides the general challenge of adding type annotations to dynamically typed code, there are subtle interactions between these annotations in gradually typed code that exacerbate the situation. For example, to migrate a program to be as static as possible, in general, all possible combinations of adding or removing type annotations from parameters must be tried out and compared. In this paper, we address this problem by developing migrational typing, which efficiently types all possible ways of replacing dynamic types with fully static types for a gradually typed program. The typing result supports automatically migrating a program to be as static as possible or introducing the least number of dynamic types necessary to remove a type error. The approach can be extended to support user-defined criteria about which annotations to modify. We have implemented migrational typing and evaluated it on large programs. The results show that migrational typing scales linearly with the size of the program and takes only 2–4 times longer than plain gradual typing.

Information

Type
Research Article
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution, and reproduction in any medium, provided the original work is properly cited.
Copyright
© The Author(s), 2022. Published by Cambridge University Press
Figure 0

Figure 1. Types for a sample of the migration space for the $\mathtt{rowAtI}$ function. The second column contains a sequence of + and – symbols, indicating whether the $\star$ annotation is kept or removed, respectively, for each of the five parameters annotated with in $\mathtt{rowAtI}$. For example, for program 2, all parameters except $\mathtt{fixed}$ keep their $\star$ annotations. The entries denote that the corresponding program is ill-typed.

Figure 1

Figure 2. Programs explored for searching possible migrations in Migeed & Palsberg (2019) (left) and this work (right). Programs in blue type check and those in red do not type check. The dashed lines in the left subfigure denote that an infinite number of programs were omitted from it.

Figure 2

Figure 3. Programs explored for finding migrations for $\mathtt{rowAtI}$ in our approach. These programs (configurations) constitute the full migration lattice (Takikawa et al.,2016) for the program $\mathtt{rowAtI}$. Each configuration is identified by a sequence of “+/-” signs, with “+” (“-”) indicating that the corresponding $\star$ is kept (removed). A configuration with strictly more “-”s is more precise. We present several lines relating program precision and omit most of them for clarity.

Figure 3

Figure 4. Syntax and type system of ITGL, an implicitly typed gradual language. The operations dom, cod, and $\sqcap$ are undefined for cases that are not listed here.

Figure 4

Figure 5. Variational types, selection, and type equivalence.

Figure 5

Figure 6. Relations between theorems and challenges. The notations in the figure are discussed in Section 3.

Figure 6

Figure 7. Syntax of expressions, types, and environments.

Figure 7

Figure 8. Rules defining type compatibility.

Figure 8

Figure 9. Patterns and pattern-constrained relations and operations.. op can be any unary or binary operation on types. The is defined stipulations in the premise mean that the operations are defined on their input types, as specified in Figure 4. The is defined in the conclusion indicates that the operation can be safely carried out on the migrational type when constricted by ${\pi}$.

Figure 9

Figure 10. Typing rules. The operations dom, cod, and $\sqcap$ are undefined for cases that are not listed here. The process for obtaining $dom_{\pi}$ from dom is detailed in Section 4.3. The operations $cod_{\pi}$ and $\sqcap_{\pi}$ can be obtained similarly through Figure 9.

Figure 10

Figure 11. Constraint generation rules.

Figure 11

Figure 12. Auxiliary constraint generation functions.

Figure 12

Figure 13. A unification algorithm.

Figure 13

Figure 14. An extension to the unification algorithm in Figure 13.

Figure 14

Figure 15. Running time (in seconds) of migrational typing on programs converted from Grift (Kuhlenschmidt et al.,2019). For each row, columns 2 through 4 give the metric of the program, including the number of lines of non-blank code, the number of functions, the number of dynamic parameters, and the number of changes we made to the program. Times are measured on a ThinkPad with 2.4GHz i7-5500U 4-core processor and 8GB memory running GHC 8.0.2 on Ubuntu 16.04. Each time is an average of 10 runs. The symbol – indicates that typing timed out after 1,000 s.

Figure 15

Figure 16. Relations between ratios of typed parameters and migrational typing times for the nbody benchmark.

Submit a response

Discussions

No Discussions have been published for this article.