Skip to main content
Log in

An approach to completing variable names for implicitly typed functional languages

Higher-Order and Symbolic Computation

Abstract

This paper presents an approach to completing variable names when writing programs in an implicitly typed functional language. As a first step toward developing practical systems, we considered a simple case: up to the cursor position the program text is given completely. With this assumption we specify a variable completion problem for an implicitly typed core functional language with let-polymorphism, and show an algorithm for solving the problem. Based on the algorithm we have implemented a variable name completion system for the language as an Emacs-mode.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Institutional subscriptions

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Fig. 5
Fig. 6
Fig. 7
Fig. 8

Notes

  1. Although our current system does not deal with programs with structures (or modules), here we use this example since it is a typical case where filtering with types effectively reduces the number of candidates.

  2. Our previous paper [9] specified the problem without using type substitution. That specification was only applicable in the cases where FTV(Γ)=∅, which cases typically occur when Γ is the type environment constructed by processing declarations in libraries.

  3. The algorithm \(\mathcal{V}\) is slightly changed from the one in our previous paper [9], where substitutions were not applied to C i ’s. Corresponding to this change, Algorithm 1 is changed so that substitutions are not applied to the types obtained by \(\mathcal{V}\). This is just for making the form of the algorithm be suited to the inductive proof in Appendices A and B.

  4. The function mono is not exactly a function since there is some nondeterminism in generating fresh type variables. We introduce this notation just for simplifying the presentation. We are not rigorous about the generation of fresh type variables in this paper.

  5. Our previous paper [9] stated these properties without using type substitution. In this paper we added type substitution, corresponding to the modification of the problem specification in Sect. 2.

References

  1. Acar, U.A.: Self-adjusting computation (an overview). In: Proceedings of the 2009 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, PEPM ’09, Savannah, GA, USA, pp. 1–6. ACM Press, New York (2009)

    Google Scholar 

  2. Aditya, S., Nikhil, R.S.: Incremental polymorphism. In: Proceedings of the 5th ACM Conference on Functional Programming Languages and Computer Architecture, Cambridge, USA, pp. 379–405 (1991)

    Chapter  Google Scholar 

  3. Caml mode. http://www.emacswiki.org/emacs/CamlMode

  4. Content assist. http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/editors_contentassist.htm

  5. Di Cosmo, R.: Type isomorphisms in a type-assignment framework. In: Proceedings of the 19th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL ’92, Albuquerque, New Mexico, pp. 200–210. ACM Press, New York (1992)

    Chapter  Google Scholar 

  6. Djinn. http://www.augustsson.net/Darcs/Djinn/

  7. Eclipse FP. http://eclipsefp.sourceforge.net/

  8. EmacsWiki: Auto complete. http://www.emacswiki.org/emacs/AutoComplete

  9. Goto, T., Sasano, I.: An approach to completing variable names for implicitly typed functional languages. In: Proceedings of the ACM SIGPLAN 2012 Workshop on Partial Evaluation and Program Manipulation, PEPM ’12, Philadelphia, Pennsylvania, USA, pp. 131–140. ACM Press, New York (2012)

    Chapter  Google Scholar 

  10. Gvero, T., Kuncak, V., Piskac, R.: Code completion using quantitative type inhabitation. Technical Report EPFL-REPORT-170040, Ecole Polytechnique Federale de Lausanne (2011)

  11. Haack, C., Wells, J.B.: Type error slicing in implicitly typed higher-order languages. Sci. Comput. Program. 50, 189–224 (2004)

    Article  MATH  MathSciNet  Google Scholar 

  12. Hashimoto, M.: First-class contexts in ML. In: Asian Computing Science Conference. Lecture Notes in Computer Science, vol. 1538, pp. 206–223. Springer, Berlin (1998)

    Chapter  Google Scholar 

  13. Java development environment for Emacs. http://jdee.sourceforge.net/

  14. Katayama, S.: Systematic search for lambda expressions. In: Trends in Functional Programming, pp. 111–126 (2005)

    Google Scholar 

  15. Leksah. http://leksah.org/

  16. Lerner, B., Flower, M., Grossman, D., Chambers, C.: Searching for type-error messages. In: PLDI ’07: Proceedings of the 2007 ACM SIGPLAN Conference on Programming Language Design and Implementation, San Diego, California, USA. ACM Press, New York (2007)

    Google Scholar 

  17. Milner, R.: A theory of type polymorphism in programming. J. Comput. Syst. Sci. 17(3), 348–375 (1978)

    Article  MATH  MathSciNet  Google Scholar 

  18. Milner, R., Tofte, M., Harper, R., MacQueen, D.: The Definition of Standard ML (Revised). MIT Press, Cambridge (1997)

    Google Scholar 

  19. Mitchell, J.C.: Foundations for Programming Languages. MIT Press, Cambridge (1996)

    Google Scholar 

  20. OcaIDE. http://www.algo-prog.info/ocaide/

  21. OCaml Development Tools. http://ocamldt.free.fr/

  22. OCamlSpotter. http://jun.furuse.info/hacks/ocamlspotter/

  23. Perelman, D., Gulwani, S., Ball, T., Grossman, D.: Type-directed completion of partial expressions. In: Proceedings of the 33rd ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI ’12, Beijing, China, pp. 275–286. ACM Press, New York (2012)

    Chapter  Google Scholar 

  24. Pierce, B.C., Turner, D.N.: Local type inference. ACM Trans. Program. Lang. Syst. 22(1), 1–44 (2000)

    Article  Google Scholar 

  25. Rittri, M.: Using types as search keys in function libraries. In: Proceedings of the Fourth International Conference on Functional Programming Languages and Computer Architecture, FPCA ’89, Imperial College, London, United Kingdom, pp. 174–183. ACM Press, New York (1989)

    Chapter  Google Scholar 

  26. Robbes, R., Lanza, M.: How program history can improve code completion. In: Proceedings of the 2008 23rd IEEE/ACM International Conference on Automated Software Engineering, ASE ’08, Washington, DC, USA, pp. 317–326. IEEE Comput. Soc., Washington (2008)

    Chapter  Google Scholar 

  27. Robinson, J.A.: A machine-oriented logic based on the resolution principle. J. ACM 12(1), 23–41 (1965)

    Article  MATH  Google Scholar 

  28. Runciman, C., Toyn, I.: Retrieving re-usable software components by polymorphic type. In: Proceedings of the Fourth International Conference on Functional Programming Languages and Computer Architecture, FPCA ’89, Imperial College, London, United Kingdom, pp. 166–173. ACM Press, New York (1989)

    Chapter  Google Scholar 

  29. Tuareg mode. http://www-rocq.inria.fr/~acohen/tuareg/index.html.en

  30. TypeRex. http://www.typerex.org/

  31. Wright, A.: Simple imperative polymorphism. LISP Symb. Comput. 8, 343–355 (1995)

    Article  Google Scholar 

Download references

Acknowledgements

We would like to thank the anonymous reviewers of PEPM 2012 and HOSC for many helpful comments and for letting us know EPFL technical reports and the paper in ASE 2008. We would also like to thank Oleg Kiselyov, an editor of HOSC, for providing an example used in Sect. 3.1 and for showing us that GHC retains information of parentheses in abstract syntax trees. We would also like to thank Julia Lawall for proofreading the paper.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Isao Sasano.

Additional information

This paper is an extended version of our paper [9]. A main difference is in that proofs for some properties are added. As another difference, small modification is made on the problem specification.

This work was carried out in part when Goto was at Shibaura Institute of Technology. He has since moved to Yahoo Japan Corporation.

Appendices

Appendix A: Preliminaries for proving the properties

In the appendix we prove Property 1 for Algorithm 1. In this section we first describe some definitions and lemmas used in the proof. We have not yet fully proved Property 2. Nevertheless we divide Property 2 into three statements and prove two of them.

In this appendix, we assume the bound variable convention, that is, that all the bound variables are different from each other and from all the free variables.

We also show Algorithm 2, which is an unoptimized version of Algorithm 1, and prove the two properties for Algorithm 2, which makes it easier to read the proof in the next section.

1.1 A.1 Some definitions about types, type environments, and type judgments

Here we give some definitions about types, type environments, and type judgments.

Definition 1

Given a function f, which is a set of pairs, and a set X, f| X represents the function {(a,b) | (a,b)∈f,aX}.

A type substitution is a function from type variables to types. So for a type substitution S and a set of type variables X, we write S| X for representing a type substitution that is obtained from S by restricting the domain to X. Note that type substitutions are naturally extended to functions that take types or type environments, as we wrote in Sect. 5.

We generalize the instance relation ≤ between types that we defined in Sect. 2.

Definition 2

For a type σ 1=∀α 1α n .τ 1 and a type σ 2=∀β 1β m .τ 2, σ 2 is said to be an instance of σ 1, which is written as σ 2σ 1, when {β 1,…,β m }∩FTV(σ 1)=∅ and there exists a type substitution S such that dom(S)={α 1,…,α n } and τ 2=S(τ 1).

This relation is used for proving the completeness of \(\mathcal{V}\) (and \({\mathcal{W}}\)).

The following defines a relation between type environments.

Definition 3

We call a type environment Γ 1 is an instance of another type environment Γ 2 when there exists a type substitution S such that Γ 1=S(Γ 2).

Some articles define this relation more generally so that Γ 1 can have extra variables in its domain, but we do not since it is not necessary in this paper.

The following defines a relation between type judgments.

Definition 4

We call a type judgment Γ 1M:τ 1 is an instance of another type judgment Γ 2M:τ 2 when there exists a type substitution S such that Γ 1=S(Γ 2) and τ 1=S(τ 2). We also call Γ 2M:τ 2 is more general than Γ 1M:τ 1 in this case.

1.2 A.2 Some known properties about type judgments, type substitutions, and the type inference algorithm \({\mathcal{W}}\)

The following two properties are about type judgments and type substitutions.

Lemma 1

If ΓM:τ then for any type substitution S, S(Γ)▷M:S(τ).

Lemma 2

If Γ{x:σ 1}▷M:τ and σ 1σ 2, then Γ{x:σ 2}▷M:τ.

The following property is about the types and type substitutions.

Lemma 3

If (FTV(τ)∖FTV(Γ))∩dom(S)=∅, then S(Cls(τ,Γ))=Cls(S(τ),S(Γ)).

The following property is the soundness of the type inference algorithm \({\mathcal{W}}\).

Lemma 4

(Soundness of the type inference algorithm \({\mathcal{W}}\))

If \((S,\tau) = {\mathcal{W}}(\varGamma,M)\) then S(Γ)▷M:τ.

Note that when we write \((S,\tau) = {\mathcal{W}}(\varGamma,M)\), it says that \({\mathcal{W}}(\varGamma,M)\) succeeds and results in (S,τ).

The following property is the completeness of the type inference algorithm \({\mathcal{W}}\).

Lemma 5

(Completeness of the type inference algorithm \({\mathcal{W}}\))

If Γ′▷M:τ and Γis an instance of Γ, then \((S_{1},\tau_{1}) = {\mathcal{W}}(\varGamma,M)\) and Γ′▷M:τ is an instance of S 1(Γ)▷M:τ 1.

The following property holds for the unification algorithm \(\mathcal{U}\).

Lemma 6

For any monomorphic types τ 1 and τ 2, \(\mathcal{U}(\tau_{1},\tau_{2})\) returns a most general unifiers of them when they are unifiable. That is, when there exist S such that S(τ 1)=S(τ 2), \(\mathcal{U}(\tau_{1},\tau_{2})\) succeeds and returns a substitution R, for which there exists a substitution Rsuch that S=R′∘R.

We use these lemmas in the following arguments.

1.3 A.3 A type system for D

In order to prove Property 1 and 2 we construct a type system for D in Fig. 9. This type system reflects our intention behind the algorithm \({\mathcal{V}}\). In this type system a type judgment has the form of ΓD:(τ,x). This type judgment indicates that we can construct a term M from D by replacing ␣ by a variable x and filling dummy nodes [ ] and marked nodes by using some terms M 1,… such that the constructed term M have a type S(τ) under a type environment S(Γ) for some type substitution S. We state this property as the following lemma, which will be used for proving the soundness property (Property 1).

Fig. 9
figure 9

A type system for D

Lemma 7

If ΓD:(τ,x), thenMfill (D,x), ∃S, S(Γ)▷M:S(τ).

The function fill is defined in Fig. 10 together with the function app which makes function applications by generating argument terms. Note that the function app returns an infinite sets of terms and hence the function fill may return an infinite sets of terms, but they are only used in proofs.

Fig. 10
figure 10

Filling function fill

Before proving Lemma 7, we prove another lemma which is used in the proof of Lemma 7.

Lemma 8

If ΓM:τ 1τ 2, thenM 1, ∃S, S(Γ)▷M M 1:S(τ 2).

We prove this lemma by using the following lemma.

Lemma 9

Γ, ∀τ, ∃M, ∃S, S(Γ)▷M:S(τ).

Proof

We prove this lemma by giving a function which takes a pair (Γ,τ) of a type environment and a type and returns a pair (M,S) of a term and a type substitution such that S(Γ)▷M:S(τ). We define such a function G as follows.

$$\begin{array}{l} G(\varGamma, \mathit{int}) = (1, \emptyset)\\ G(\varGamma, \alpha) = \bigl(1, \{\alpha\mapsto \mathit{int}\}\bigr)\\ G(\varGamma, \tau_1 \rightarrow\tau_2) = \mathrm{let}~(M,S) = G(\varGamma,\tau_2)~\mathrm{in}~ (\lambda x. M, S) \quad\mbox{($x$~is a fresh variable)} \end{array} $$

Although there are many other ways to define such functions, we gave a simple one here. Proof for G to satisfy the condition is done by induction on τ, which we omit here. □

We prove Lemma 8 by using Lemma 9.

Proof

Let (M 1,S)=G(Γ,τ 1). By Lemma 9, S(Γ)▷M 1:S(τ 1). By applying S to the type judgment ΓM:τ 1τ 2, we obtain S(Γ)▷M:S(τ 1)→S(τ 2). By applying the typing rule (app) to these two type judgments we obtain S(Γ)▷M M 1:S(τ 2). □

We are now ready to prove Lemma 7.

Proof

We prove by induction on the structure of derivations of ΓD:(τ,x). We proceed by cases on the final typing rule used in the derivation.

Case (D-cursor): The type judgment must be in the form of Γ{x:σ}▷␣:(τ,x) and it must be satisfied that τσ. Then by the typing rule (var), we obtain Γ{x:σ}▷x:τ. Since xfill(␣,x)={x}, this case is completed by taking S to be the empty (identity) type substitution.

Case (D-mark): The type judgment must be in the form of ΓD :(τ′,x) and it must be satisfied that ΓD:(τ,x) and τ′∈rs(τ) for some τ. By induction hypothesis, for some Mfill(D,x), S(Γ)▷M:S(τ) for some S. Here let τ=τ 1→⋯→τ n  (n≥1) and τ′=τ k →⋯→τ n rs(τ) (1≤kn). By applying Lemma 8 repeatedly to the type judgment S(Γ)▷M:S(τ), we obtain S′(Γ)▷M M 1M k−1:S′(τ′) for some S′. Since Mfill(D,x), by the definition of fill, it follows that M M 1M k−1fill(D∗,x).

Case (D-abs): The type judgment must be in the form of Γλy.D:(τ 1τ,x) and it must be satisfied that Γ{y:τ 1}▷D:(τ,x). By induction hypothesis with the type environment Γ{y:τ 1}, for some Mfill(D,x), S(Γ{y:τ 1})▷M:S(τ) for some S. Since S(Γ{y:τ 1})=S(Γ){y:S(τ 1)}, by applying the typing rule (abs), we obtain S(Γ)▷λy.M:S(τ 1)→S(τ). Since Mfill(D,x), by the definition of fill, we obtain λy.Mfill(λy.D,x). Since S(τ 1)→S(τ)=S(τ 1τ), this case is completed.

Case (D-app): The type judgment must be in the form of ΓM D:(τ 2,x) and it must be satisfied that ΓM:τ 1τ 2 and ΓD:(τ 1,x). By induction hypothesis, for some M 1fill(D,x), S(Γ)▷M 1:S(τ 1) for some S. By applying the type substitution S to the type judgment ΓM:τ 1τ 2 we obtain S(Γ)▷M:S(τ 1)→S(τ 2). By the typing rule (app) we obtain S(Γ)▷M M 1:S(τ 2). Since M 1fill(D,x), by the definition of fill, we obtain M M 1fill(M D,x).

Case (D-let1): The type judgment must be in the form of Γlet y=D in [ ] end:(τ 2,x) and it must be satisfied that ΓD:(τ 1,x) for some τ 1. By induction hypothesis, for some M 1fill(D,x) and some S 1, S 1(Γ)▷M 1:S 1(τ 1).

Now we apply Lemma 9 to the type environment S 1(Γ) and the type S 1(τ 2). Then for some M 2 and S 2, S 2(S 1(Γ))▷M 2:S 2(S 1(τ 2)). Here, by considering the function G, we can construct M 2 without using the variable y. So we assume yFV(M 2). Then we can freely add any binding of the variable y to the type environment in the above type judgment as follows.

$$S_2\bigl(S_1(\varGamma)\bigr)\bigl\{y : Cls \bigl(S_2\bigl(S_1(\varGamma)\bigr), S_2 \bigl(S_1(\tau_2)\bigr)\bigr)\bigr\} \triangleright M_2 : S_2\bigl(S_1(\tau_2)\bigr) $$

By applying the type substitution S 2 to the type judgment S 1(Γ)▷M 1:S 1(τ 1), we obtain

$$S_2\bigl(S_1(\varGamma)\bigr) \triangleright M_1 : S_2\bigl(S_1(\tau_1)\bigr). $$

By applying the typing rule (let) to these two type judgments, we obtain

$$S_2\bigl(S_1(\varGamma)\bigr) \triangleright\mathbf{let}~y=M_1~\mathbf{in}~M_2~\mathbf{end} : S_2 \bigl(S_1(\tau_2)\bigr). $$

Since M 1fill(D,x), by the definition of fill, we obtain let y=M 1 in M 2 endfill(let y=D in [ ] end,x).

Case (D-let2): In the type system in Fig. 9, we can replace the typing rule (D-let2) by the following typing rule, which results in a type system equivalent to the type system in Fig. 9.

$$\mbox{(D-let$2^{'}$)}\ \ \frac{\varGamma\triangleright M : \tau_1 \quad\varGamma\triangleright D[M/y] : (\tau_2,x)}{\varGamma\triangleright\mathbf{let}~y = M~\mathbf{in}~D~\mathbf{end}: (\tau_2,x)} $$

We do not prove this, which is analogous to the counterpart in the type system in Fig. 1. So only in the proof of this lemma we assume the type system in Fig. 9 is replaced by the new type system with (D-let2) being replaced by (D-let2′).

Case (D-let2′): The type judgment must be in the form of Γlet y=M in D end:(τ 2,x) and it must be satisfied that ΓM:τ 1 and ΓD[M/y]:(τ 2,x) for some τ 1. By induction hypothesis, for some M 1fill(D[M/y],x) and some S 1, S 1(Γ)▷M 1:S 1(τ 2). By considering the definition of G, we can construct this M 1 without using y such that yFV(M 1) and hence M 1[M/y]=M 1. So the following type judgment holds.

$$S_1(\varGamma) \triangleright M_1[M/y] : S_1(\tau_2) $$

Now we reconstruct M 1 by replacing back all the occurrences of M’s that we replaced y with to y, which we let \(M_{1}'\). Then the following type judgment holds.

$$S_1(\varGamma) \triangleright M_1'[M/y] : S_1(\tau_2) $$

Note that \(M_{1}' \in\mbox{\textit{fill}}(D,x)\).

Only in the proof of this lemma we assume the type system in Fig. 1 had been defined with (let) being replaced by the following typing rule.

$$\mbox{(let$^{'}$)}\ \ \frac{\varGamma\triangleright M_1 : \tau_1 ~~ \varGamma\triangleright M_2[M_1/y] : \tau_2}{\varGamma \triangleright\mathbf{let}~ y = M_1 \mathbf{in}~ M_2 ~\mathbf{end}: \tau_2} $$

The obtained type system is equivalent to the original one, which is a well known result [19]. By Lemma 1, applying S 1 to the type judgment ΓM:τ 1 results in S 1(Γ)▷M:S 1(τ 1). Then by applying (let′) to the two type judgments we obtain

$$S_1(\varGamma) \triangleright\mathbf{let}~y=M~\mathbf{in}~M_1'~\mathbf{end}:S_1(\tau_2). $$

Since \(M_{1}' \in\mbox{\textit{fill}}(D,x)\), by the definition of fill, we obtain

$$\mathbf{let}~y=M~\mathbf{in}~M_1'~\mathbf{end} \in \mbox{ \textit{fill}}(\mathbf{let}~y=M~\mathbf{in}~D~\mathbf{end},x). $$

Case (D-fix): The type judgment must be in the form of Γfix y.D:(τ,x) and it must be satisfied that Γ{y:τ}▷D:(τ,x). By induction hypothesis, for some M 1fill(D,x) and some S 1, S 1(Γ{y:τ})▷M 1:S 1(τ). Since S 1(Γ{y:τ})=S 1(Γ){y:S 1(τ)}, we obtain S 1(Γ){y:S 1(τ)})▷M 1:S 1(τ). Then by applying the typing rule (fix) we obtain S 1(Γ)▷fix yM 1:S 1(τ). Since M 1fill(D,x), by the definition of fill, we obtain fix yM 1fill(fix y.D,x). □

Now we state another property, which will be used for proving the completeness property (Property 2).

Lemma 10

If Mfill(D,x) and ΓM:τ, then ΓD:(τ,x).

Proof

We prove by induction on the structure of D.

Case (D=␣): Assume Mfill(␣,x) and ΓM:τ. Since fill(␣,x)={x}, it is suffice to consider the case of M=x. Now Γx:τ. Then it must be satisfied that mono(Γ(x))=τ and hence τΓ(x). By the typing rule (D-cursor), we obtain Γ▷␣:(τ,x).

Case (\(D=D_{1}^{*}\)): Assume \(M \in\mbox{\textit{fill}}(D_{1}^{*},x)\) and ΓM:τ. Since

$$\mbox{\textit{fill}}\bigl(D_1^*,x\bigr) = \mathrm{let}~ms = \mbox{ \textit{fill}}(D_1,x)~\mathrm{in}~ \bigcup_{M \in ms} \mathit{app}~M, $$

Mapp M 1 for some M 1fill(D 1,x). Let M=M 1 M 2M n . Now ΓM 1 M 2M n :τ, so it must be satisfied that ΓM 1:τ 1 for some τ 1 with τrs(τ 1). By induction hypothesis, ΓD 1:(τ 1,x). By applying the typing rule (D-mark) we obtain \(\varGamma\triangleright D_{1}^{*} : (\tau, x)\).

Case (D=λy.D 1): Assume Mfill(λy.D 1.x) and ΓM:τ. Since

$$\mbox{\textit{fill}}(\lambda y.D_1,x)=\bigl\{\lambda y.M_1~|~M_1\in\mbox {\textit{fill}}(D_1,x) \bigr\}, $$

M=λy.M 1 for some M 1fill(D 1,x). Now Γλy.M 1:τ, so it must be satisfied that τ=τ 0τ 1 and Γ{y:τ 0}▷M 1:τ 1 for some τ 0 and τ 1. By induction hypothesis, Γ{y:τ 0}▷D 1:(τ 1,x). By applying the typing rule (D-abs) we obtain Γλy.D 1:(τ 0τ 1,x).

Case (D=M 1 D 2): Assume Mfill(M 1 D 2,x) and ΓM:τ. Since

$$\mbox{\textit{fill}}(M_1~D_2,~x) = \bigl \{M_1~M_2~|~M_2 \in\mbox{\textit {fill}}(D_2,x)\bigr\}, $$

M=M 1 M 2 for some M 2fill(D 2,x). Now ΓM 1 M 2:τ, so it must be satisfied that ΓM 1:τ 2τ and ΓM 2:τ 2 for some τ 2. By induction hypothesis, we obtain ΓD 2:(τ 2,x). By the typing rule (D-app), we obtain ΓM 1 D 2:(τ,x).

Case (D=let y=D 1 in [ ] end): Assume Mfill(let y=D 1 in [ ] end,x) and ΓM:τ. Since

$$\mbox{\textit{fill}}\bigl(\mathbf{let}~y=D_1~\mathbf{in}~[\,]~\mathbf{end},~x \bigr) = \bigl\{\mathbf{let}~y=M_1~\mathbf{in}~M_2~\mathbf{end}~|~ M_1 \in\mbox{\textit{fill}}(D_1,x), \mbox{$M_{2}$ is a term}\bigr\}, $$

M=let y=M 1 in M 2 end for some M 1fill(D 1,x) and M 2. Now Γlet y=M 1 in M 2 end:τ, so it must be satisfied that ΓM 1:τ 1 and Γ{y:Cls(Γ,τ 1)}▷M 2:τ for some τ 1. By induction hypothesis we obtain ΓD 1:(τ 1,x). By applying the typing rule (D-let1) we obtain Γlet y=D 1 in [ ] end:(τ,x).

Case (D=let y=M 1 in D 2 end): Assume Mfill(let y=M 1 in D 2 end,x) and ΓM:τ. Since

$$\mbox{\textit{fill}}(\mathbf{let}~y=M_1~\mathbf{in}~D_2~\mathbf{end},~x) = \bigl\{\mathbf{let}~y=M_1~\mathbf{in}~M_2~\mathbf{end}~|~ M_2 \in\mbox{\textit{fill}}(D_2,x)\bigr\}, $$

M=let y=M 1 in M 2 end for some M 2fill(D 2,x). Now Γlet y=M 1 in M 2 end:τ, so it must be satisfied that ΓM 1:τ 1 and Γ{y:Cls(Γ,τ 1)}▷M 2:τ for some τ 1. By induction hypothesis with the type environment Γ{y:Cls(Γ,τ 1)}, we obtain Γ{y:Cls(Γ,τ 1)}▷D 2:(τ,x). By applying the typing rule (D-let2) we obtain Γlet y=M 1 in D 2 end:(τ,x).

Case (D=fix y.D 1): Assume Mfill(fix y.D 1,x) and ΓM:τ. Since

$$\mbox{\textit{fill}}(\mathbf{fix}~y.D_1,x) = \bigl\{(\mathbf{fix}~y.M_1,x)~|~ M_1 \in\mbox{\textit{fill}}(D_1,x) \bigr\}, $$

M=fix y.M 1 for some M 1fill(D 1,x). Now Γfix y.M 1:τ, so it must be satisfied that Γ{y:τ}▷M 1:τ. By induction hypothesis with the type environment Γ{y:τ}, we obtain Γ{y:τ}▷D 1:(τ,x). By applying (D-fix) we obtain Γfix y.D 1:(τ,x). □

1.4 A.4 A property concerning cmp, fill, and pre

We use the following property concerning cmp, fill, and pre in the following.

Lemma 11

If D=cmp P then Mfill(D,x)⟺(P,x)∈pre M.

We omit the proof, which is fairly straightforward.

1.5 A.5 An unoptimized algorithm and its properties

Here we show an unoptimized algorithm and prove its properties.

Algorithm 2

Let s be the spelling of the cursor node ␣ s in P. Then compute the set V of variables as follows.

$$V = \bigl\{~x~|~ (S, \tau, x) \in\mathcal{V}'(\varGamma,~ \mathit{cmp}~P), s \in\mbox{\textit{prefixes}}~x~\bigr\} $$

This algorithm uses the algorithm \(\mathcal{V}'\) instead of the algorithm \({\mathcal{V}}\). The definition of \(\mathcal{V}'\) is obtained by modifying the definition of \({\mathcal{V}}\) in the case of cursor node ␣ as follows.

In the case of cursor, \(\mathcal{V}'\) directly returns variables that are within their scope in the third elements of the triples of the result set. In the other cases all the occurrences of \({\mathcal{V}}\) are replaced by \(\mathcal{V}'\) and all the occurrences of C’s (and ones with substitutions applied) are replaced by x’s. As is also the case for the algorithm \({\mathcal{V}}\), even when some unification fails, the entire algorithm \(\mathcal{V}'\) does not fail, which is not explicitly expressed in the definition of \(\mathcal{V}'\).

In the following we prove two properties that characterizes the algorithm \(\mathcal{V}'\). One is the soundness of \(\mathcal{V}'\), which is used for proving the soundness of Algorithm 2 (Property 1) and the other is the completeness of \(\mathcal{V}'\), which is used for proving the completeness of Algorithm 2 (Property 2).

Lemma 12

(Soundness of the algorithm \(\mathcal{V}'\))

If \((S,\tau,x) \in\mathcal{V}'(\varGamma,D)\), then S(Γ)▷D:(τ,x).

This lemma has the counterpart in the type system in Fig. 1, which is Lemma 4 shown in a previous subsection. In the proof we will use the following lemma.

Lemma 13

If ΓD:(τ,x) then for any type substitution S, S(Γ)▷D:(S(τ),x).

Proof for Lemma 13 is similar to the proof of Lemma 1, so we omit the proof.

Now we prove Lemma 12, which is done similarly to the proof of Lemma 4 as follows.

Proof

We prove by induction on the structure of D.

Case (D=␣): Assume . By the definition of \(\mathcal{V}'\), S=∅ and τ=mono(Γ(x)) for some xdom(Γ). By the typing rule (D-cursor) we obtain Γ▷␣:(τ,x).

Case (\(D=D_{1}^{*}\)): Assume \((S,\tau,x)\in\mathcal{V}'(\varGamma,~D_{1}^{*})\). By the definition of \(\mathcal{V}'\), for some τ 1, we have \((S,\tau_{1},x)\in\mathcal{V}'(\varGamma,D_{1})\) and τrs(τ 1). By induction hypothesis, S(Γ)▷D 1:(τ 1,x). Since τrs(τ 1), by the typing rule (D-mark), we obtain \(S(\varGamma) \triangleright D_{1}^{*} : (\tau,x)\).

Case (D=λy.D 1): Assume \((S,\tau,x)\in\mathcal{V}'(\varGamma,~\lambda y.D_{1})\). By the definition of \(\mathcal{V}'\), for some τ k , it is satisfied that \((S,\tau_{k},x)\in\mathcal{V}'(\varGamma\{y:\alpha\},D_{1})\) (α fresh) and τ=S(α)→τ k . By induction hypothesis we obtain S(Γ{y:α})▷D 1:(τ k ,x). Since S(Γ{y:α})=S(Γ){y:S(α)}, by applying the typing rule (abs), we obtain S(Γ)▷λy.D 1:(S(α)→τ k ,x).

Case (D=M 1 D 2): Assume \((S,\tau,x)\in\mathcal{V}'(\varGamma,~M_{1}~D_{2})\). By the definition of \(\mathcal{V}'\), for some S 1, τ 1, S 2k , τ 2k , and S 3k , it is satisfied that \((S_{1},\tau_{1})={\mathcal{W}}(\varGamma,M_{1})\), \((S_{2k},\tau_{2k},x)\in\mathcal{V}'(S_{1}(\varGamma),D_{2})\), \(S_{3k}=\mathcal{U}(S_{2k}(\tau_{1}),\tau_{2k}\rightarrow\alpha_{k})\) (α k fresh), S=S 3k S 2k S 1, and τ=S 3k (α k ). By induction hypothesis we obtain S 2k (S 1(Γ))▷D 2:(τ 2k ,x). By Lemma 13 we obtain S 3k (S 2k (S 1(Γ)))▷D 2:(S 3k (τ 2k ), x).

By the way, since \((S_{1},\tau_{1})={\mathcal{W}}(\varGamma,M_{1})\), by Lemma 4 we obtain S 1(Γ)▷M 1:τ 1. By Lemma 1, by applying S 3k S 2k to this type judgment, we obtain S 3k (S 2k (S 1(Γ)))▷M 1:S 3k (S 2k (τ 1)). Since S 3k is a unifier of S 2k (τ 1) and τ 2k α k , the following equations hold.

$$S_{3k}\bigl(S_{2k}(\tau_1)\bigr) = S_{3k}(\tau_{2k}\rightarrow\alpha_k) = S_{3k}(\tau_{2k}) \rightarrow S_{3k}( \alpha_k) $$

So we obtain S 3k (S 2k (S 1(Γ)))▷M 1:S 3k (τ 2k )→S 3k (α k ). By the typing rule (D-app) we obtain S 3k (S 2k (S 1(Γ)))▷M 1 D 2:(S 3k (α k ), x).

Case (D=let y=D 1 in [ ] end): Assume \((S,\tau,x)\in\mathcal{V}'(\varGamma,~\mathbf{let}~y=D_{1}~\mathbf{in}~[\,]~\mathbf{end})\). Here τ is actually some type variable but the fact is not used in the argument. By the definition of \(\mathcal{V}'\), for some τ k , it is satisfied that \((S,\tau_{k},x)\in\mathcal{V}'(\varGamma,D_{1})\). By induction hypothesis we obtain S(Γ)▷D 1:(τ k ,x). By applying the typing rule (D-let1) we obtain S(Γ)▷let y=D 1 in [ ] end:(τ′,x) for any τ′. So by taking τ′ to be τ we obtain S(Γ)▷let y=D 1 in [ ] end:(τ,x).

Case (D=let y=M 1 in D 2 end): Assume \((S,\tau,x)\in\mathcal{V}'(\varGamma,~\mathbf{let}~y=M_{1}~\mathbf{in}~D_{2}~\mathbf{end})\). Then by the definition of \(\mathcal{V}'\), for some S 1, S 2k , τ 1, and σ, it is satisfied that \((S_{1},\tau_{1}) = {\mathcal{W}}(\varGamma,M_{1})\), σ=Cls(τ 1,S 1(Γ)), \((S_{2k},\tau,x)\in\mathcal{V}'(S_{1}(\varGamma)\{x:\sigma\},D_{2})\), and S=S 2k S 1. By induction hypothesis we obtain S 2k (S 1(Γ){x:σ})▷D 2:(τ,x). Since S 2k (S 1(Γ){y:σ})=S 2k (S 1(Γ)){y:S 2k (σ)}, we obtain S 2k (S 1(Γ)){x:S 2k (σ)}▷D 2:(τ,x). Since \((S_{1},\tau_{1}) = {\mathcal{W}}(\varGamma,M_{1})\), by Lemma 4 we obtain S 1(Γ)▷M 1:τ 1.

Here we define the following type substitution.

$$S_2' = S_{2k}|_{dom(S_{2k})\setminus(FTV(\tau_1)\setminus FTV(S_1(\varGamma)))} $$

Then \(S_{2}'(S_{1}(\varGamma))=S_{2k}(S_{1}(\varGamma))\). By Lemma 1, by applying \(S_{2}'\) to the type judgment S 1(Γ)▷M 1:τ 1, we obtain \(S_{2}'(S_{1}(\varGamma))\triangleright M_{1}:S_{2}'(\tau_{1})\). Since \((FTV(\tau_{1})\setminus FTV(S_{1}(\varGamma)))\cap dom(S_{2}')=\emptyset\), by Lemma 3 the following equations hold.

$$\begin{aligned} S_{2k}(\sigma) =& S_2'(\sigma) \\ =& S_2'\bigl(Cls\bigl(\tau_1,S_1( \varGamma)\bigr)\bigr) \\ =& Cls\bigl(S_2'(\tau_1),S_2' \bigl(S_1(\varGamma)\bigr)\bigr) \\ =& Cls\bigl(S_2'(\tau_1),S_{2k} \bigl(S_1(\varGamma)\bigr)\bigr) \end{aligned}$$

So by the typing rule (D-let2) we obtain S 2k (S 1(Γ))▷let y=M 1 in D 2 end:(τ,x).

Case (D=fix y.D 1): Assume \((S,\tau,x)\in\mathcal{V}'(\varGamma,~\mathbf{fix}~y.D_{1})\). Then by the definition of \(\mathcal{V}'\), for some S 1k , τ k , it is satisfied that \((S_{1k},\tau_{k},x)\in\mathcal{V}'(\varGamma\{y:\alpha\},D_{1})\) (α fresh), \(S_{2k}=\mathcal{U}(\tau_{k},S_{1k}(\alpha))\), S=S 2k S 1k , and τ=S 2k (τ k ). By induction hypothesis we obtain S 1k (Γ{y:α})▷D 1:(τ k ,x). Since \(S_{2k}=\mathcal{U}(\tau_{k},S_{1k}(\alpha))\), by Lemma 6, we obtain S 2k (τ k )=S 2k (S 1k (α)).

By Lemma 13, applying S 2k to S 1k (Γ{y:α})▷D 1:(τ k x), we obtain S 2k (S 1k (Γ{y:α}))▷D 1:(S 2k (τ k ), x). Since S 2k (S 1k (Γ{y:α})=S 2k (S 1k )(Γ{y:S 2k (S 1k (α))}) and S 2k (τ k )=S 2k (S 1k (α)), we obtain S 2k (S 1k (Γ)){y:S 2k (τ k )})▷D 1:(S 2k (τ k ), x). By the typing rule (D-fix) we obtain S 2k (S 1k (Γ))▷fix y.D 1:(S 2k (τ k ), x). □

Now we show the second lemma. Before that, we define a relation between type judgments for D, similarly to Definition 4.

Definition 5

We call a type judgment Γ 1D:(τ 1,x) is an instance of another type judgment Γ 2D:(τ 2,x) when there exists a type substitution S such that Γ 1=S(Γ 2) and τ 1=S(τ 2). We also call Γ 2D:(τ 2,x) is more general than Γ 1D:(τ 1,x) in this case.

We also show a lemma, which is a counterpart of Lemma 2.

Lemma 14

If Γ{x:σ 1}▷D:(τ,x) and σ 1σ 2, then Γ{x:σ 2}▷D:(τ,x).

We omit the proof, which is fairly straightforward.

Now we show the second lemma.

Lemma 15

(Completeness of the algorithm \(\mathcal{V}'\))

If Γ′▷D:(τ,x) and Γis an instance of Γ, then there exist S 0 and τ 0 such that \((S_{0},\tau_{0},x) \in\mathcal{V}'(\varGamma,D)\) and Γ′▷D:(τ,x) is an instance of S 0(Γ)▷D:(τ 0,x).

Proof

We prove by induction on the structure of D.

Case (D=␣): Assume Γ′▷␣:(τ,x) and Γ′=S(Γ) for some S. From the definition of the type system for D, it must be satisfied that xdom(Γ′)=dom(Γ) and τΓ′(x). Let Γ(x)=∀α 1α n .τ 0. Then Γ′(x)=S(Γ(x))=S(∀α 1α n .τ 0). By renaming to avoid capturing type variables, we obtain

$$S (\forall\alpha_1\ldots\alpha_n.\tau_0) = \forall\beta_1\ldots\beta_n.\tau_0' $$

for some fresh type variables β 1β n , where \(\tau_{0}'=[\beta_{1}/\alpha_{1}, \ldots, \beta_{n}/\alpha_{n}](\tau_{0})\). Since \(\tau\leq\varGamma'(x) =\forall\beta_{1}\ldots\beta_{n}.\tau_{0}'\), there exists some types τ 1,…,τ n such that

$$\tau= [\tau_1/\beta_1,\ldots,\tau_n/ \beta_n]\bigl(S\bigl(\tau_0'\bigr)\bigr). $$

Here β 1,…,β n were fresh, we obtain

$$\begin{aligned} \tau =& [\tau_1/\beta_1,\ldots,\tau_n/ \beta_n]\bigl(S\bigl(\tau_0'\bigr)\bigr) \\ =& S\bigl([\tau_1/\beta_1,\ldots,\tau_n/ \beta_n]\bigl(\tau_0'\bigr)\bigr) \\ =& S\bigl([\tau_1/\beta_1,\ldots,\tau_n/ \beta_n]\bigl([\beta_1/\alpha _1,\ldots, \beta_n/\alpha_n](\tau_0)\bigr)\bigr) \\ =& S\bigl([\tau_1/\alpha_1,\ldots,\tau_n/ \alpha_n](\tau_0)\bigr). \end{aligned}$$

Here we compute .

Since xdom(Γ), let S 0=∅ and \(\tau_{0}=mono(\varGamma(x))=[\alpha_{1}'/\alpha_{1},\ldots,\alpha _{n}'/\alpha_{n}]\tau_{0}\) with some fresh type variables \(\alpha_{1}',\ldots,\alpha_{n}'\). Now we check that Γ′▷␣:(τ,x) is an instance of . Let \(R=S \circ[\tau_{1}/\alpha_{1}',\ldots,\tau_{n}/\alpha_{n}']\). Since \(\alpha_{1}',\ldots,\alpha_{n}'\) were fresh, R(Γ)=S(Γ). Then we obtain Γ′=S(Γ)=R(Γ)=R(S 0(Γ)). The last equation holds since S 0 is an identity type substitution. Another thing to show is \(\tau=R([\alpha_{1}'/\alpha_{1},\ldots,\alpha _{n}'/\alpha_{n}]\tau_{0})\), which holds as follows.

$$\begin{aligned} R\bigl(\bigl[\alpha_1'/\alpha_1,\ldots, \alpha_n'/\alpha_n\bigr]\tau_0 \bigr) =& S\bigl(\bigl[\tau_1/\alpha_1', \ldots,\tau_n/\alpha_n'\bigr] \bigl(\bigl[ \alpha_1'/\alpha_1,\ldots, \alpha_n'/\alpha_n\bigr](\tau_0) \bigr)\bigr) \\ =& S\bigl([\tau_1/\alpha_1,\ldots,\tau_n/ \alpha_n](\tau_0)\bigr) \\ =& \tau \end{aligned}$$

Case (\(D=D_{1}^{*}\)): Assume \(\varGamma' \triangleright D_{1}^{*} : (\tau,x)\) and Γ′=S(Γ) for some S. From the definition of the type system for D, it must be satisfied that S(Γ)▷D 1:(τ 1,x) and τrs(τ 1) for some τ 1. By induction hypothesis, there exist S 0, τ 0, and R such that \((S_{0}, \tau_{0}, x)\in\mathcal{V}'(\varGamma,D_{1})\), S(Γ)=R(S 0(Γ)), and τ 1=R(τ 0). Now we compute \(\mathcal{V}'(\varGamma,~D_{1}^{*})\).

$$\mathcal{V}'\bigl(\varGamma,D_1^*\bigr) = \bigl\{ \bigl(S,\tau',x\bigr)~|~(S,\tau,x) \in\mathcal{V}'( \varGamma,D_1), \tau' \in rs(\tau )\bigr\} $$

So it follows that \((S_{0},\tau_{0}',x) \in\mathcal{V}'(\varGamma,D_{1}^{*})\) for any \(\tau_{0}' \in rs (\tau_{0})\). By the way the following equation holds.

$$\bigl\{R\bigl(\tau_0'\bigr)~|~\tau_0' \in rs(\tau_0)\bigr\} = rs \bigl(R(\tau_0)\bigr) $$

Since τrs(τ 1)=rs(R(τ 0)), we can select \(\tau_{0}'\) so that \(R(\tau_{0}')=\tau\).

Now we check that \(\varGamma' \triangleright D_{1}^{*} : (\tau,x)\) is an instance of \(S_{0}(\varGamma) \triangleright D_{1}^{*} : (\tau_{0}',x)\), which holds since Γ′=S(Γ)=R(S 0(Γ)) and \(\tau=R(\tau_{0}')\) as we wrote above.

Case (D=λz.D 1): Assume Γ′▷λz.D 1:(τ,x) and Γ′=S(Γ) for some S. From the definition of the type system for D, it must be satisfied that τ=τ 0τ 1 and Γ′{z:τ 0}▷D 1:(τ 1,x) for some τ 0 and τ 1.

Now we compute \(\mathcal{V}'(\varGamma,~\lambda z.D_{1})\).

$$\begin{aligned} \mathcal{V}'(\varGamma,\lambda z.D_1) =& \mathrm{let}~ \bigl\{(S_1,\tau_1,x_1),\ldots,(S_i, \tau_i,x_i)\bigr\} = \mathcal{V}'\bigl( \varGamma\{z:\alpha\},D_1\bigr) \quad(\alpha~\mathrm{fresh}) \\ & \mathrm{in}~ \bigl\{\bigl(S_j,S_j(\alpha)\rightarrow \tau_j,x_j\bigr)~|~j\in\{1,\ldots,i\}\bigr\} \end{aligned}$$

Here we notice that Γ′{z:τ 0} is an instance of Γ{z:α} since Γ′=S(Γ) and α is fresh. In an equation,

$$\varGamma'\{z:\tau_0\} = \bigl(S \cup\{\alpha\mapsto \tau_0\}\bigr) \bigl(\varGamma\{ z:\alpha\}\bigr). $$

By induction hypothesis there exist S 0 and \(\tau_{0}'\) such that \((S_{0},\tau_{0}',x)\in\mathcal{V}'(\varGamma\{z:\alpha\},D_{1})\) and Γ′{z:τ 0}▷D 1:(τ 1,x) is an instance of \(S_{0}(\varGamma\{z:\alpha\}) \triangleright D_{1}:(\tau_{0}',x)\). So we obtain τ 0=R(S 0(α)), Γ′=R(S 0(Γ)), and \(\tau_{1}=R(\tau_{0}')\) for some type substitution R.

Since \((S_{0},\tau_{0}',x) \in\mathcal{V}'(\varGamma\{z:\alpha\},D_{1})\), by looking the above computation of \(\mathcal{V}'(\varGamma,\lambda z.D_{1})\), we obtain

$$\bigl(S_0,S_0(\alpha)\rightarrow\tau_0',x \bigr) \in\mathcal{V}'(\varGamma ,\lambda z.D_1). $$

Now we check that Γ′▷λz.D 1:(τ 0τ 1x) is an instance of \(S_{0}(\varGamma)\triangleright\lambda z.D_{1}:(S_{0}(\alpha )\rightarrow\tau _{0}',~x)\), which holds since τ 0=R(S 0(α)), \(\tau_{1}=R(\tau_{0}')\), and Γ′=R(S 0(Γ)), as we have written above.

Case (D=M 1 D 2): Assume Γ′▷M 1 D 2:(τ,x) and Γ′=S(Γ) for some S. By the definition of the type system for D it must be satisfied that S(Γ)▷M 1:τ 2τ and S(Γ)▷D 2:(τ 2,x) for some τ 2.

Now we compute \(\mathcal{V}'(\varGamma,~M_{1}~D_{2})\).

$$\begin{aligned} \mathcal{V}'(\varGamma,M_1~D_2) =& \mathrm{let}~ (S_1,\tau_1)={\mathcal{W}}( \varGamma,M_1) \\ &\phantom{\mathrm{let}~} \bigl\{(S_{21}, \tau_{21}, x_{21}),\ldots, (S_{2i}, \tau_{2i}, x_{2i}) \bigr\} = \mathcal{V}'\bigl(S_1(\varGamma),D_2 \bigr) \\ &\phantom{\mathrm{let}~} S_{3j}=\mathcal{U}\bigl(S_{2j}( \tau_1), \tau_{2j}\rightarrow\alpha_j\bigr) \quad(\alpha_j~\mathrm{fresh})~\bigl(j \in\{1,\ldots, i\}\bigr) \\ & \mathrm{in}~ \bigl\{\bigl(S_{3j}\circ S_{2j} \circ S_1, S_{3j}(\alpha_j),x_{2j}\bigr) ~|~ j \in\{1,\ldots,i\}\bigr\} \end{aligned}$$

Here, by the completeness of the algorithm \({\mathcal{W}}\) (Lemma 5), S 1(Γ)▷M 1:τ 1 is more general than S(Γ)▷M 1:τ 2τ. So it follows that \(S(\varGamma)=S_{1}' (S_{1}(\varGamma))\) and \(\tau _{2}\rightarrow \tau=S_{1}'(\tau_{1})\) for some \(S_{1}'\). Then we obtain

$$S_1'\bigl(S_1(\varGamma)\bigr) \triangleright D_2 : (\tau_2, x). $$

By induction hypothesis, for some \((S_{2k},\tau_{2k},x_{2k})\in\mathcal{V}'(S_{1}(\varGamma),D_{2})\), it is satisfied that x=x 2k and \(S_{1}'(S_{1}(\varGamma)) \triangleright D_{2} : (\tau_{2}, x)\) is an instance of S 2k (S 1(Γ))▷D 2:(τ 2k ,x). So it follows that \(S_{1}'(S_{1}(\varGamma))=R(S_{2j}(S_{1}(\varGamma)))\) and τ 2=R(τ 2k ) for some R.

Now as is also the case for the proof of completeness of \({\mathcal{W}}\), the two types S 2k (τ 1) and τ 2k α k are unified to \(S_{1}'(\tau_{1})\). So by Lemma 6, the unification \(\mathcal{U}(S_{2k}(\tau_{1}), \tau_{2k}\rightarrow \alpha_{k})\) succeeds. Then the following equation holds.

$$S_{3k}\bigl(S_{2k}(\tau_1)\bigr)=S_{3k}( \tau_{2k})\rightarrow S_{3k}(\alpha_k) $$

By Lemma 6, S 3k is a most general unifier of S 2k (τ 1) and τ 2k α k . So for some S 4,

$$S_4\bigl(S_{3k}\bigl(S_{2k}(\tau_1) \bigr)\bigr)= S_4\bigl(S_{3k}(\tau_{2k})\bigr) \rightarrow S_4\bigl(S_{3k}(\alpha_k)\bigr)= S_1'(\tau_1). $$

Here we define S 5 as follows.

$$S_5(\alpha) = \left \{ \begin{array} {l@{\quad}l} S_4(\alpha) & \alpha\in FTV(S_{3k}(S_{2k}( \tau_1))) \\ R(\alpha) & \mathrm{otherwise} \end{array} \right . $$

Then it is satisfied that S 5(S 3k (S 2k (S 1(Γ))))=Γ′ and S 5(S 3k (α k ))=τ. So it follows that Γ′▷M 1 D 2:(τ,x) is an instance of S 3k (S 2k (S 1(Γ)))▷M 1 D 2:(S 3k (α k ),x).

Case (D=let y=D 0 in [ ] end): Assume Γ′▷let y=D 0 in [ ] end:(τ,x) and Γ′=S(Γ) for some S. By the definition of the type system for D it must be satisfied that Γ′▷D 0:(τ 0,x) for some τ 0.

Now we compute \(\mathcal{V}'(\varGamma,~\mathbf{let}~y=D_{0}~\mathbf{in}~[\, ]~\mathbf{end})\).

$$\begin{aligned} \mathcal{V}'\bigl(\varGamma,~\mathbf{let}~y=D_0~ \mathbf{in}~[\,]~\mathbf{end}\bigr) =& \mathrm{let}~ \bigl\{(S_1, \tau_1,x_1),\ldots,(S_i,\tau_i,x_i) \bigr\}=\mathcal{V}'(\varGamma ,D_0) \\ & \mathrm{in}~ \bigl\{(S_j,\alpha_j,x_j)~|~j \in\{1,\ldots,i\}\bigr\} \quad(\alpha_j~\mathrm{fresh}) \end{aligned}$$

By induction hypothesis, for some \((S_{k},\tau_{j},x_{k})\in\mathcal {V}'(\varGamma,D_{0})\), it is satisfied that x=x k and Γ′▷D 0:(τ 0,x) is an instance of S k (Γ)▷D 1:(τ k ,x). So it follows that Γ′=R(S k (Γ)) and τ 0=R(τ k ) for some R.

Now we check that Γ′▷let y=D 0 in [ ] end:(τ,x) is an instance of S k (Γ)▷let y=D 0 in [ ] end:(α k ,x). We make the following type substitution R′ here.

$$R' = R \cup\{\alpha_k \mapsto\tau\} $$

Then since α k was fresh, it is satisfied that Γ′=R′(S k (Γ)) and τ=R′(α k ).

Case (D=let y=M 1 in D 1 end): Assume Γ′▷let y=M 1 in D 1 end:(τ,x) and Γ′=S(Γ) for some S. By the definition of the type system for D it must be satisfied that \(\varGamma'\triangleright M_{1}:\tau_{1}'\) and \(\varGamma'\{y:Cls(\varGamma',\tau_{1}')\}\triangleright D_{1}:(\tau, x)\) for some \(\tau_{1}'\).

Now we compute \(\mathcal{V}'(\varGamma,~\mathbf{let}~y=M_{1}~\mathbf{in}~D_{1}~\mathbf{end})\).

$$\begin{aligned} \mathcal{V}'(\varGamma,~\mathbf{let}~y=M_1~ \mathbf{in}~D_1~\mathbf{end}) =& \mathrm{let}~ (S_1, \tau_1)={\mathcal{W}}(\varGamma,M_1) \\ & \phantom{\mathrm{let}~} \bigl\{(S_{21},\tau_{21},x_{21}), \ldots,(S_{2i},\tau_{2i},x_{2i})\bigr\}= \\ &\phantom{\mathrm{let}~}\qquad\mathcal{V}'\bigl(S_1( \varGamma)\bigl\{y:Cls\bigl(S_1(\varGamma),\tau_1\bigr) \bigr\},D_1\bigr) \\ & \mathrm{in}~ \bigl\{(S_{2j}\circ S_1, \tau_{2j},x_{2j})~|~j\in\{1,\ldots,i\}\bigr\} \end{aligned}$$

By the completeness of the algorithm \({\mathcal{W}}\) (Lemma 5), S 1(Γ)▷M 1:τ 1 is more general than \(\varGamma' \triangleright M_{1} : \tau_{1}'\). So it follows that \(\varGamma'=S_{1}'(S_{1}(\varGamma))\) and \(\tau_{1}'=S_{1}'(\tau_{1})\) for some \(S_{1}'\).

Here let \(\{\alpha_{1},\ldots,\alpha_{n}\}=FTV(\tau_{1}')\setminus FTV(\varGamma')\) and {β 1,…,β m }=FTV(τ 1)∖FTV(S 1(Γ)). Then it follows that \(Cls(\varGamma',\tau_{1}') = \forall\alpha_{1}\ldots\alpha_{n}.\tau_{1}'\) and Cls(S 1(Γ),τ 1)=∀β 1β m .τ 1.

Since \(\{\alpha_{1},\ldots,\alpha_{n}\} \subseteq FTV(S_{1}'\{\beta_{1},\ldots,\beta_{m}\})\), it is satisfied that

$$Cls\bigl(\varGamma',\tau_1'\bigr) = \forall\alpha_1\ldots\alpha_n.\tau_1' \leq S_1'(\forall\beta_1\ldots \beta_m.\tau_1) =Cls\bigl(S_1(\varGamma), \tau_1\bigr). $$

By Lemma 14, we obtain \(S_{1}'(S_{1}(\varGamma))\{y:S_{1}'(\forall\beta_{1}\ldots\beta_{m}.\tau_{1})\} \triangleright D_{1}:(\tau,x)\). So we obtain

$$S_1'\bigl(S_1(\varGamma)\bigl\{y:Cls \bigl(S_1(\varGamma),\tau_1\bigr)\bigr\}\bigr) \triangleright D_1:(\tau,x) $$

By induction hypothesis, for some \((S_{2k},\tau_{2k},x_{2k})\in \mathcal{V}'(S_{1}(\varGamma)\{y:Cls(S_{1}(\varGamma),\tau_{1})\},D_{1})\), it is satisfied that x=x 2k and \(S_{1}'(S_{1}(\varGamma)\{y:Cls(S_{1}(\varGamma),\tau_{1})\})\triangleright D_{1}:(\tau,x)\) is an instance of S 2k (S 1(Γ){y:Cls(S 1(Γ),τ 1)}):D 1:(τ 2k ,x). So it follows that \(S_{1}'(S_{1}(\varGamma)\{y:Cls(S_{1}(\varGamma),\tau_{1})\}) = R(S_{2k}(S_{1}(\varGamma)\{y:Cls(S_{1}(\varGamma),\tau_{1})\}))\) and τ=R(τ 2k ) for some R. So \(R(S_{2k}(S_{1}(\varGamma)))=S_{1}'(S_{1}(\varGamma))=\varGamma'\).

Now we check that Γ′▷let y=M 1 in D 1 end:(τ,x) is an instance of S 2k (S 1(Γ))▷let y=M 1 in D 1 end:(τ 2k ,x), which holds since R(S 2k (S 1(Γ)))=Γ′ and τ=R(τ 2k ) as we wrote above.

Case (D=fix y.D 1): Assume Γ′▷fix y.D 1:(τ,x) and Γ′=S(Γ) for some S. By the definition of the type system for D it must be satisfied that Γ′{y:τ}▷D 1:(τ,x).

Now we compute \(\mathcal{V}'(\varGamma,~\mathbf{fix}~y.D_{1})\).

$$\begin{aligned} \mathcal{V}'(\varGamma,~\mathbf{fix}~y.D_1) =& \mathrm{let}~ \bigl\{(S_{11},\tau_{11},x_{11}), \ldots,(S_{1i},\tau_{1i},x_{1i})\bigr\} = \mathcal{V}'\bigl(\varGamma\{y:\alpha\},D_1\bigr) \quad( \alpha~\mathrm{fresh}) \\ & \phantom{\mathrm{let}~} S_{2j}=\mathcal{U}\bigl( \tau_{1j},S_{1j}(\alpha)\bigr) \quad\bigl(j\in\{1,\ldots,i\} \bigr) \\ & \mathrm{in}~ \bigl\{\bigl(S_{2j}\circ S_{1j},S_{2j}( \tau_{1j}),x_{2j}\bigr)~|~j\in\{1,\ldots,i\} \bigr\} \end{aligned}$$

Here we notice that Γ′{y:τ} is an instance of Γ{y:α} by the type substitution S∪{ατ} since Γ′=S(Γ) and α is fresh. By induction hypothesis, for some \((S_{1k},\tau_{1k},x_{1k})\in \mathcal{V}'(\varGamma\{y:\alpha\},D_{1})\), it is satisfied that x=x 1k and Γ′{y:τ}▷D 1:(τ,x) is an instance of S 1k (Γ{y:α})▷D 1:(τ 1k ,x). So it follows that Γ′{y:τ}=R(S 1k (Γ{y:α})) and τ=R(τ 1k ) for some R. From the first equation we also obtain τ=R(S 1k (α)), Γ′=R(S 1k (Γ)), and R(τ 1k )=R(S 1k (α)). So τ 1k and S 1k (α) are unified to τ and hence the unification \(\mathcal{U}(\tau_{1k},S_{1k}(\alpha))\) succeeds. Since \(S_{2k}=\mathcal{U}(\tau_{1k},S_{1k}(\alpha))\) the following equation holds.

$$S_{2k}(\tau_{1k})=S_{2k}\bigl(S_{1k}( \alpha)\bigr). $$

Since S 2k is a most general unifier, there exists some R′ such that

$$R'\bigl(S_{2k}(\tau_{1k})\bigr)=R' \bigl(S_{2k}\bigl(S_{1k}(\alpha)\bigr)\bigr)=\tau. $$

Here we define S 3 as follows.

$$S_3(\alpha) = \left \{ \begin{array} {l@{\quad}l} R'(\alpha) & \alpha\in FTV\bigl(S_{2k}(\tau_{1k}) \bigr) \\ R(\alpha) & \mathrm{otherwise} \end{array} \right . $$

Then it is satisfied that S 3(S 2k (S 1k (Γ)))=Γ′ and S 3(S 2k (τ 1k ))=τ. So it follows that Γ′▷fix y.D 1:(τ,x) is an instance of S 2k (S 1k (Γ))▷fix y.D 1:(S 2k (τ 1k ),x). □

Property 1 for Algorithm 2 is an immediate consequence of Lemma 7, 11, and 12.

Property 2 for Algorithm 2 is an immediate consequence of Lemma 10, 11, and 15.

Appendix B: Proof of Property 1 for Algorithm 1

In this section we prove Property 1 for Algorithm 1, and give a statement concerning Property 2.

Firstly we prove the following lemma.

Lemma 16

(Soundness of the algorithm \(\mathcal{V}\))

If , xdom(Γ ), and , then R(S(Γ))▷D:(R(τ),x).

Proof

We prove by induction on the structure of D.

Case (D=␣): Assume . By the definition of \(\mathcal{V}\), we have S=∅, Γ =mono(Γ), τ =τ, and τ=g(arity(Γ(y))) for some ydom(Γ). Further assume xdom(Γ ) and . Then by Lemma 6 it is satisfied that R(Γ (x))=R(τ ). Since Γ =mono(Γ) and τ =τ, we obtain R(mono(Γ(x)))=R(τ). Then it is satisfied that R(τ)≤R(Γ(x)). By the typing rule (D-cursor) we obtain R(Γ)▷␣:(R(τ),x).

Case (\(D=D_{1}^{*}\)): Assume . By the definition of \(\mathcal{V}\), for some τ 1, we have and τrs(τ 1). Further assume xdom(Γ ) and . By induction hypothesis R(S(Γ))▷D 1:(R(τ 1),x). Since τrs(τ 1), R(τ)∈rs(R(τ 1)). So by the typing rule (D-mark) we obtain \(R(S(\varGamma)) \triangleright D_{1}^{*} : (R(\tau),x)\).

Case (D=λy.D 1): Assume . By the definition of \(\mathcal{V}\), for some τ k , we have (α fresh) and τ=S(α)→τ k . Further assume xdom(Γ ) and . By induction hypothesis, R(S(Γ{y:α}))▷D 1:(R(τ k ),x). Since R(S(Γ{y:α}))=R(S k (Γ)){y:R(S(α))}, by the typing rule (D-abs) we obtain R(S(Γ))▷λy.D 1:(R(S(α))→R(τ k ),x). Note that R(S(α))→R(τ k )=R(S(α)→τ k )=R(τ).

Case (D=M 1 D 2): Assume . By the definition of \(\mathcal{V}\), for some S 1, τ 1, S 2k , τ 2k , and S 3k , it is satisfied that \((S_{1},\tau_{1})={\mathcal{W}}(\varGamma,M_{1})\), , \(S_{3k}=\mathcal{U}(S_{2k}(\tau_{1}),\tau_{2k}\rightarrow\alpha_{k})\) (α k fresh), S=S 3k S 2k S 1, Γ =S 3k (Γ ␣2k ), τ =S 3k (τ ␣2k ), and τ=S 3k (α k ). Further assume xdom(Γ )=dom(Γ ␣2k ) and . Then by Lemma 6, we obtain R(Γ (x))=R(τ ). Since Γ =S 3k (Γ ␣2k ) and τ =S 3k (τ ␣2k ), we obtain R(S 3k (Γ ␣2k (x)))=R(S 3k (τ ␣2k )). This equation indicates Γ ␣2k (x) and τ ␣2k are unifiable, so succeeds, whose result we let be R 2k . Since R 2k is a most general unifier of Γ ␣2k (x) and τ ␣2k , the following equation holds for some \(R_{2k}'\).

$$R \circ S_{3k} = R_{2k}' \circ R_{2k} $$

By induction hypothesis, R 2k (S 2k (S 1(Γ)))▷D 2:(R 2k (τ 2k ),x). By Lemma 1, by applying \(R_{2k}'\) to this type judgment, we obtain \(R_{2k}'(R_{2k}(S_{2k}(S_{1}(\varGamma))))\triangleright D_{2}: (R_{2k}'(R_{2k}(\tau _{2k})),x)\). By the equation \(R \circ S_{3k} = R_{2k}' \circ R_{2k}\), we obtain R(S 3k (S 2k (S 1(Γ))))▷D 2:(R(S 3k (τ 2k )),x).

By the way, since \((S_{1},\tau_{1})={\mathcal{W}}(\varGamma,M_{1})\), by Lemma 4 we obtain S 1(Γ)▷M 1:τ 1. By Lemma 1, applying RS 3k S 2k to this judgment we obtain R(S 3k (S 2k (S 1(Γ))))▷M 1:R(S 3k (S 2k (τ 1))). Since \(S_{3k}=\mathcal{U}(S_{2k}(\tau_{1}),\tau_{2k}\rightarrow\alpha_{k})\), by Lemma 6 we have S 3k (S 2k (τ 1))=S 3k (τ 2k α k ). So the following equation hold.

$$R\bigl(S_{3k}\bigl(S_{2k}(\tau_1)\bigr)\bigr) = R\bigl(S_{3k}(\tau_{2k}\rightarrow\alpha_k) \bigr) = R\bigl(S_{3k}(\tau_{2k})\bigr)\rightarrow R \bigl(S_{3k}(\alpha_k)\bigr) $$

So we obtain

$$R\bigl(S_{3k}\bigl(S_{2k}\bigl(S_1(\varGamma) \bigr)\bigr)\bigr)\triangleright M_1:R\bigl(S_{3k}(\tau _{2k})\bigr)\rightarrow R\bigl(S_{3k}(\alpha_k) \bigr). $$

By the typing rule (D-app) we obtain R(S 3k (S 2k (S 1(Γ))))▷M 1 D 2:(R(S 3k (α k )),x). Since S=S 3k S 2k S 1 and τ=S 3k (α k ), we obtain R(S(Γ))▷M 1 D 2:(R(τ),x).

Case (D=let y=D in [ ] end): Assume . Here τ is actually some type variable but the fact is not used in the argument. By the definition of \(\mathcal{V}\), for some τ k , it is satisfied that . Further assume xdom(Γ ) and .

By induction hypothesis we obtain R(S(Γ))▷D 1:(R(τ k ),x). By applying the typing rule (D-let1) we obtain R(S(Γ))▷let y=D 1 in [ ] end:(τ′,x) for any τ′. So by taking τ′ to be τ we obtain R(S(Γ))▷let y=D 1 in [ ] end:(τ,x).

Case (D=let y=M 1 in D 2 end): Assume , xdom(Γ ), and . By the definition of \(\mathcal{V}\), for some S 1, S 2k , τ 1, and σ, it is satisfied that \((S_{1},\tau_{1}) = {\mathcal{W}}(\varGamma,M_{1})\), σ=Cls(τ 1,S 1(Γ)), , and S=S 2k S 1. By induction hypothesis we obtain R(S 2k (S 1(Γ){x:σ}))▷D 2:(R(τ),x). Since R(S 2k (S 1(Γ){y:σ}))=R(S 2k (S 1(Γ))){y:R(S 2k (σ))}, we obtain R(S 2k (S 1(Γ))){x:R(S 2k (σ))}▷D 2:(R(τ),x). Since \((S_{1},\tau_{1}) = {\mathcal{W}}(\varGamma,M_{1})\), by Lemma 4 we obtain S 1(Γ)▷M 1:τ 1.

Here we define the following type substitution.

$$R' = (R\circ S_{2k})|_{dom(R\circ S_{2k})\setminus(FTV(\tau _1)\setminus FTV(S_1(\varGamma)))} $$

Then R′(S 1(Γ))=R(S 2k (S 1(Γ))). By Lemma 1, by applying R′ to the type judgment S 1(Γ)▷M 1:τ 1, we obtain R′(S 1(Γ))▷M 1:R′(τ 1). Since (FTV(τ 1)∖FTV(S 1(Γ)))∩dom(R′)=∅, by Lemma 3 the following equations hold.

$$\begin{aligned} R\bigl(S_{2k}(\sigma)\bigr) =& R'(\sigma) \\ =& R'\bigl(Cls\bigl(\tau_1,S_1(\varGamma) \bigr)\bigr) \\ =& Cls\bigl(R'(\tau_1),R' \bigl(S_1(\varGamma)\bigr)\bigr) \end{aligned}$$

So by the typing rule (D-let2) we obtain R′(S 1(Γ))▷let y=M 1 in D 2 end:(τ,x). Since R′(S 1(Γ))=R(S 2k (S 1(Γ))), we obtain R(S 2k (S 1(Γ)))▷let y=M 1 in D 2 end:(τ,x). Since S=S 2k S 1 we obtain R(S(Γ))▷let y=M 1 in D 2 end:(τ,x).

Case (D=fix y.D 1): Assume , xdom(Γ ), and . By the definition of \(\mathcal{V}\), for some S 1k , τ k , it is satisfied that (α fresh), \(S_{2k}=\mathcal{U}(\tau_{k},S_{1k}(\alpha))\), S=S 2k S 1k , τ=S 2k (τ k ), Γ =S 2k (Γ k ), and τ =S 2k (τ k ). By induction hypothesis we obtain R(S 1k (Γ{y:α}))▷D 1:(R(τ k ),x). Since \(S_{2k}=\mathcal{U}(\tau_{k},S_{1k}(\alpha))\), Lemma 6, we obtain S 2k (τ k )=S 2k (S 1k (α)). By applying R to the both side of this equation we obtain R(S 2k (τ k ))=R(S 2k (S 1k (α))).

By Lemma 13, applying RS 2k to S 1k (Γ{y:α})▷D 1:(τ k x), we obtain R(S 2k (S 1k (Γ{y:α})))▷D 1:(R(S 2k (τ k )), x). Since R(S 2k (S 1k (Γ{y:α})))=R(S 2k (S 1k (Γ))){y:R(S 2k (S 1k (α)))} and R(S 2k (τ k ))=R(S 2k (S 1k (α))), we obtain R(S 2k (S 1k (Γ))){y:R(S 2k (τ k ))}▷D 1:(R(S 2k (τ k )), x). By the typing rule (D-fix) we obtain R(S 2k (S 1k (Γ)))▷fix y.D 1:(R(S 2k (τ k )), x). Since S=S 2k S 1k and S 2k (τ k )=τ, we obtain R(S(Γ))▷fix y.D 1:(R(τ), x). □

Property 1 for Algorithm 1 is an immediate consequence of Lemma 7, 11, and 16.

Now we give a statement of the completeness of the algorithm \(\mathcal{V}\).

Conjecture 1

(Completeness of the algorithm \(\mathcal{V}\))

If Γ′▷D:(τ,x) and Γis an instance of Γ, then there exist S 0, τ 0, Γ , τ , and R such that , xdom(Γ ), , and Γ′▷D:(τ,x) is an instance of R(S 0(Γ))▷D:(R(τ 0),x).

Property 2 for Algorithm 1 is an immediate consequence of Lemma 10, 11, and Conjecture 1.

Rights and permissions

Reprints and permissions

About this article

Cite this article

Sasano, I., Goto, T. An approach to completing variable names for implicitly typed functional languages. Higher-Order Symb Comput 25, 127–163 (2012). https://doi.org/10.1007/s10990-013-9095-x

Download citation

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s10990-013-9095-x

Keywords

Navigation