Typed Lua
Some random thoughts and ideas about types
Last updated
Some random thoughts and ideas about types
Last updated
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.