Typed Lua
Some random thoughts and ideas about types
Last updated
Was this helpful?
Some random thoughts and ideas about types
Last updated
Was this helpful?
We outline a method to extend the TypeScript system to Lua. However, as TypeScript's subtype semantics do not satisfy completeness (only soundness), we emphasize a method for subtyping that is both sound and complete.
...
In this section, we develop just enough definitions to aid in the implementation of such a system. We will define precisely what it means to be a subtype and show informally that our definition is sensible (i.e., it is consistent with basic subtype rules).
Denote an element β of type β as β. Define a subtype operator β where β to indicate that β is a subtype of β. What does it mean for a type to be the subtype of another type?
Definition 1: The set of all objects is the domain of β notated
Remark: We don't observe types under the framework of type theory. Instead, types are predicates where β iff .
Definition 2a: The set of operations, denoted β, is the set where any element is of type.
Definition 2b: Suppose we have β and . Then iff β for all β. We will not develop this further.
Definition 3: Denote the set of operators that accept a parameter typed β as β. Then we define β as .β
Remark: This is analogous to the subsumption rule in type theory. We'll see later that the sets β are far easier to compute than subtype rules.
Definition 4: Type union is notated β or where
Definition 5: Type intersection is notated β where β
Theorem 1: The following are true about types β
β
β and β implies β
βRemark: This is an important result and shows that function parameters are contravariant while function return types are covariant.
We will not develop these theorems further.
The theorem shows that β behaves as we would expect. The proof is trivial and excluded.
Lemma 1: and β
Let any then if β we have β and if β we have hence . Now the converse. Let any then trivially is true so . Similarly, β and so we're done.
Theorem 2: and ββ
Let any β and WLOG let and construct β as β where so by Definition 2 we have β. Similarly, we have β. Conversely, let any and and WLOG suppose and then we can construct a partial map where
β
and β as β by Lemma 1, concluding this proof.
Corollary 1: If β and β then
By Definition 2, β and β.
By Theorem 2, β, concluding this proof. β
Theorem 3: If β and β then β.
Let any where WLOG .
Constructβ function so for all and some we have .
Since and we can construct partial function so that for all β and some we have β.
Finally, we can construct β such that for all . As β, we have β concluding this proof. β