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.
Similar content being viewed by others
Notes
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.
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.
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.
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.
References
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)
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)
Caml mode. http://www.emacswiki.org/emacs/CamlMode
Content assist. http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/editors_contentassist.htm
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)
Eclipse FP. http://eclipsefp.sourceforge.net/
EmacsWiki: Auto complete. http://www.emacswiki.org/emacs/AutoComplete
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)
Gvero, T., Kuncak, V., Piskac, R.: Code completion using quantitative type inhabitation. Technical Report EPFL-REPORT-170040, Ecole Polytechnique Federale de Lausanne (2011)
Haack, C., Wells, J.B.: Type error slicing in implicitly typed higher-order languages. Sci. Comput. Program. 50, 189–224 (2004)
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)
Java development environment for Emacs. http://jdee.sourceforge.net/
Katayama, S.: Systematic search for lambda expressions. In: Trends in Functional Programming, pp. 111–126 (2005)
Leksah. http://leksah.org/
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)
Milner, R.: A theory of type polymorphism in programming. J. Comput. Syst. Sci. 17(3), 348–375 (1978)
Milner, R., Tofte, M., Harper, R., MacQueen, D.: The Definition of Standard ML (Revised). MIT Press, Cambridge (1997)
Mitchell, J.C.: Foundations for Programming Languages. MIT Press, Cambridge (1996)
OCaml Development Tools. http://ocamldt.free.fr/
OCamlSpotter. http://jun.furuse.info/hacks/ocamlspotter/
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)
Pierce, B.C., Turner, D.N.: Local type inference. ACM Trans. Program. Lang. Syst. 22(1), 1–44 (2000)
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)
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)
Robinson, J.A.: A machine-oriented logic based on the resolution principle. J. ACM 12(1), 23–41 (1965)
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)
Tuareg mode. http://www-rocq.inria.fr/~acohen/tuareg/index.html.en
TypeRex. http://www.typerex.org/
Wright, A.: Simple imperative polymorphism. LISP Symb. Comput. 8, 343–355 (1995)
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
Corresponding author
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,a∈X}.
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 Γ 1▷M:τ 1 is an instance of another type judgment Γ 2▷M:τ 2 when there exists a type substitution S such that Γ 1=S(Γ 2) and τ 1=S(τ 2). We also call Γ 2▷M:τ 2 is more general than Γ 1▷M:τ 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 R′ such 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).
Lemma 7
If Γ▷D:(τ,x), then ∃M∈fill (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.
Before proving Lemma 7, we prove another lemma which is used in the proof of Lemma 7.
Lemma 8
If Γ▷M:τ 1→τ 2, then ∃M 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.
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 x∈fill(␣,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 M∈fill(D,x), S(Γ)▷M:S(τ) for some S. Here let τ=τ 1→⋯→τ n (n≥1) and τ′=τ k →⋯→τ n ∈rs(τ) (1≤k≤n). By applying Lemma 8 repeatedly to the type judgment S(Γ)▷M:S(τ), we obtain S′(Γ)▷M M 1…M k−1:S′(τ′) for some S′. Since M∈fill(D,x), by the definition of fill, it follows that M M 1…M k−1∈fill(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 M∈fill(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 M∈fill(D,x), by the definition of fill, we obtain λy.M∈fill(λ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 1∈fill(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 1∈fill(D,x), by the definition of fill, we obtain M M 1∈fill(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 1∈fill(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 y∉FV(M 2). Then we can freely add any binding of the variable y to the type environment in the above type judgment as follows.
By applying the type substitution S 2 to the type judgment S 1(Γ)▷M 1:S 1(τ 1), we obtain
By applying the typing rule (let) to these two type judgments, we obtain
Since M 1∈fill(D,x), by the definition of fill, we obtain let y=M 1 in M 2 end∈fill(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.
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 1∈fill(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 y∉FV(M 1) and hence M 1[M/y]=M 1. So the following type judgment holds.
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.
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.
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
Since \(M_{1}' \in\mbox{\textit{fill}}(D,x)\), by the definition of fill, we obtain
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 1∈fill(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 y. M 1:S 1(τ). Since M 1∈fill(D,x), by the definition of fill, we obtain fix y. M 1∈fill(fix y.D,x). □
Now we state another property, which will be used for proving the completeness property (Property 2).
Lemma 10
If M∈fill(D,x) and Γ▷M:τ, then Γ▷D:(τ,x).
Proof
We prove by induction on the structure of D.
Case (D=␣): Assume M∈fill(␣,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
M∈app M 1 for some M 1∈fill(D 1,x). Let M=M 1 M 2⋯M n . Now Γ▷M 1 M 2⋯M 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 M∈fill(λy.D 1.x) and Γ▷M:τ. Since
M=λy.M 1 for some M 1∈fill(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 M∈fill(M 1 D 2,x) and Γ▷M:τ. Since
M=M 1 M 2 for some M 2∈fill(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 M∈fill(let y=D 1 in [ ] end,x) and Γ▷M:τ. Since
M=let y=M 1 in M 2 end for some M 1∈fill(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 M∈fill(let y=M 1 in D 2 end,x) and Γ▷M:τ. Since
M=let y=M 1 in M 2 end for some M 2∈fill(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 M∈fill(fix y.D 1,x) and Γ▷M:τ. Since
M=fix y.M 1 for some M 1∈fill(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 M∈fill(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.
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 x∈dom(Γ). 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.
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.
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.
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 Γ 1▷D:(τ 1,x) is an instance of another type judgment Γ 2▷D:(τ 2,x) when there exists a type substitution S such that Γ 1=S(Γ 2) and τ 1=S(τ 2). We also call Γ 2▷D:(τ 2,x) is more general than Γ 1▷D:(τ 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 x∈dom(Γ′)=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
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
Here β 1,…,β n were fresh, we obtain
Here we compute .
Since x∈dom(Γ), 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.
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}^{*})\).
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.
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})\).
Here we notice that Γ′{z:τ 0} is an instance of Γ{z:α} since Γ′=S(Γ) and α is fresh. In an equation,
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
Now we check that Γ′▷λz.D 1:(τ 0→τ 1, x) 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})\).
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
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.
By Lemma 6, S 3k is a most general unifier of S 2k (τ 1) and τ 2k →α k . So for some S 4,
Here we define S 5 as follows.
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})\).
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.
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})\).
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
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
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})\).
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.
Since S 2k is a most general unifier, there exists some R′ such that
Here we define S 3 as follows.
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 , x∈dom(Γ ␣), 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 y∈dom(Γ). Further assume x∈dom(Γ ␣) 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 x∈dom(Γ ␣) 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 x∈dom(Γ ␣) 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 x∈dom(Γ ␣)=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}'\).
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 R∘S 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.
So we obtain
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 x∈dom(Γ ␣) 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 , x∈dom(Γ ␣), 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.
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.
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 , x∈dom(Γ ␣), 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 R∘S 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 , x∈dom(Γ ␣), , 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
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
Published:
Issue Date:
DOI: https://doi.org/10.1007/s10990-013-9095-x