Typed Lua

Some random thoughts and ideas about types

1. Preface

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.

...

2. Subtyping using operators

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 e e​ of type τ\tau​ as e:τe:\tau​. Define a subtype operator \le​ where στ\sigma\le\tau​ to indicate that σ\sigma​ is a subtype of τ\tau​. What does it mean for a type to be the subtype of another type?

Definition 1: The set of all objects x:τx:\tau is the domain of τ\tau​ notated [ ⁣[τ] ⁣][\![\tau]\!]

Remark: We don't observe types under the framework of type theory. Instead, types are predicates where x:τx:\tau​ iff x[ ⁣[τ] ⁣]x\in[\![\tau]\!].

Definition 2a: The set of operations, denoted Ω\Omega​, is the set where any element is of typeoΩ:n[ ⁣[τi] ⁣]m[ ⁣[σi] ⁣]o\in\Omega:\prod_n[\![\tau_i]\!]\to\prod_m[\![\sigma_i]\!].

Definition 2b: Suppose we have o:[ ⁣[ai] ⁣][ ⁣[bi] ⁣]Ωo:\prod [\![a_i]\!]\to\prod [\![b_i]\!]\in\Omega​ and o:[ ⁣[ai] ⁣][ ⁣[bi] ⁣]Ωo':\prod [\![a_i']\!]\to\prod [\![b_i']\!]\in\Omega. Then o=oo=o' iff o  v=o  vo\;v=o'\;v​ for all v:ai[ ⁣[ai] ⁣]v:\prod a_i\in\prod[\![a_i]\!]​. We will not develop this further.

Definition 3: Denote the set of operators that accept a parameter typed aa​ as Ωa\Omega_a​. Then we define aba\le b​ as ΩaΩb\Omega_a\subseteq\Omega_b.​

Remark: This is analogous to the subsumption rule in type theory. We'll see later that the sets Ωa\Omega_a​ are far easier to compute than subtype rules.

Definition 4: Type union is notated a+ba+b​ or a    ba\;|\;b where x:a+b    (x:a)(x:b)x:a+b \iff (x:a)\lor (x:b)

Definition 5: Type intersection is notated aba\cdot b​ where x:ab    (x:a)(x:b)x:a\cdot b\iff (x:a)\land(x:b)

Theorem 1: The following are true about types a,b,ca,b,c

  1. aaa\le a

  2. aba\le b​ and bcb\le c​ implies aca\le c

The theorem shows that \le​ behaves as we would expect. The proof is trivial and excluded.

Lemma 1: [ ⁣[a    b] ⁣]=[ ⁣[a] ⁣][ ⁣[b] ⁣][\![a\;|\;b]\!] = [\![a]\!]\cup [\![b]\!] and [ ⁣[ab] ⁣]=[ ⁣[a] ⁣][ ⁣[b] ⁣][\![a\cdot b]\!] = [\![a]\!]\cap [\![b]\!]

Proof

First:

Let any x:a    b[ ⁣[a    b] ⁣]x:a\;|\;b \in[\![a\;|\;b]\!] then if x:ax:a​ we have x[ ⁣[a] ⁣]x\in[\![a]\!]​ and if x:bx:b​ we have x[ ⁣[b] ⁣]x\in[\![b]\!]hence x[ ⁣[a] ⁣][ ⁣[b] ⁣]x\in[\![a]\!]\cup[\![b]\!]. Now the converse. Let any x:a[ ⁣[a] ⁣]x:a\in [\![a]\!] then trivially x:a    bx:a\;|\;b is true so x[ ⁣[a    b] ⁣]x\in[\![a\;|\;b]\!]. Similarly, x[ ⁣[b] ⁣]    x[ ⁣[a    b] ⁣]x\in[\![b]\!]\implies x\in[\![a\;|\;b]\!]​ and so we're done. \Box

Second: Proceed similarly to above.​

Theorem 2: Ωa    b=ΩaΩb\Omega_{a\;|\;b}=\Omega_a\cap\Omega_b and ​Ωab=ΩaΩb\Omega_{a\cdot b}=\Omega_a\cup\Omega_b

Proof

First:

Let any ϕΩa    b\phi\in\Omega_{a\;|\;b}​ and WLOG let ϕ:[ ⁣[a    b] ⁣]×UV\phi:[\![a\;|\;b]\!]\times U\to V and construct ϕ1:[ ⁣[a] ⁣]×UV\phi_1:[\![a]\!]\times U\to V​ as ϕ1:x×yϕ(x×y)\phi_1: x\times y\mapsto \phi(x\times y)​ where x[ ⁣[a] ⁣]x\in[\![a]\!] so by Definition 2 we have ΩaΩa    b\Omega_a\subseteq\Omega_{a\;|\;b}​. Similarly, we have ΩbΩa    b\Omega_b\subseteq\Omega_{a\;|\;b}​. Conversely, let any ϕ1Ωa\phi_1\in\Omega_a and ϕ2Ωb\phi_2\in\Omega_b and WLOG suppose ϕ1:[ ⁣[a] ⁣]×U1V1\phi_1:[\![a]\!]\times U_1\to V_1 and ϕ2:[ ⁣[b] ⁣]×U2V2\phi_2:[\![b]\!]\times U_2\to V_2 then we can construct a partial map ϕ:([ ⁣[a] ⁣][ ⁣[b] ⁣])×(U1U2)(V1V2)\phi:([\![a]\!]\cup [\![b]\!])\times (U_1\cup U_2)\rightharpoonup (V_1\cup V_2) where

ϕ:x×y{ϕ1(x×y)if x[ ⁣[a] ⁣]yU1ϕ2(x×y)if x[ ⁣[b] ⁣]yU2\phi: x\times y\mapsto\begin{cases}\phi_1(x\times y) &\text{if $x\in[\![a]\!]$, $y\in U_1$}\\\phi_2(x\times y)&\text{if $x\in[\![b]\!]$, $y\in U_2$}\end{cases}

and ϕΩa    b\phi\in \Omega_{a\;|\;b}​ as [ ⁣[a] ⁣][ ⁣[b] ⁣]=[ ⁣[a    b] ⁣][\![a]\!]\cup [\![b]\!] = [\![a\;|\;b]\!]​ by Lemma 1, concluding this proof. \Box

Second: Proceed similarly to above.​

Corollary 1: If aca\le c​ and bdb\le d​ then a+bc+da+b\le c+d

Proof

By Definition 2, ΩaΩc\Omega_a\subseteq\Omega_c​ and ΩbΩd\Omega_b\subseteq\Omega_d​.

By Theorem 2, Ωa+b=ΩaΩbΩcΩd=Ωc+d\Omega_{a+b} = \Omega_a\cap\Omega_b\subseteq\Omega_c\cap\Omega_d=\Omega_{c+d}​, concluding this proof. \Box

Theorem 3: If cac\le a​ and bdb\le d​ then abcda\to b\le c\to d​.

Remark: This is an important result and shows that function parameters are contravariant while function return types are covariant.

Proof

Let any ϕ1Ωab\phi_1\in\Omega_{a\to b} where WLOG ϕ1:[ ⁣[ab] ⁣]×U1V=ϕ1:Ωa×U1V\phi_1:[\![a\to b]\!]\times U_1\to V = \phi_1:\Omega_a\times U_1\to V.

Construct​ function φx1:[ ⁣[b] ⁣]×U2V\varphi_x^1: [\![b]\!]\times U_2\to V so for all x[ ⁣[c] ⁣]x\in[\![c]\!] and some fΩcΩaf\in\Omega_c\subseteq\Omega_a we have φx1(f(x),)=ϕ1(f,)\varphi^1_x(f(x),\cdots)=\phi_1(f, \cdots).

Since ΩbΩd\Omega_b\subseteq\Omega_d and ϕx1Ωb\phi^1_x\in\Omega_b we can construct partial function φx2:[ ⁣[d] ⁣]×U2VΩd\varphi_x^2:[\![d]\!]\times U_2\rightharpoonup V \in\Omega_d so that for all x[ ⁣[c] ⁣]x\in[\![c]\!]​ and some fΩcf\in\Omega_c we have φx2(f(x),)=φx1(f(x),)\varphi_x^2(f(x),\cdots)=\varphi_x^1(f(x),\cdots)​.

Finally, we can construct ϕ2:[ ⁣[cd] ⁣]×U3V\phi_2:[\![c\to d]\!]\times U_3\to V​ such that ϕ2=φx2=φx1=ϕ1\phi_2=\varphi_x^2=\varphi_x^1=\phi_1 for all x[ ⁣[c] ⁣]x\in[\![c]\!]. As ϕ2Ωcd\phi_2\in\Omega_{c\to d}​, we have ΩabΩcd\Omega_{a\to b}\subseteq\Omega_{c\to d}​ concluding this proof. \Box

We will not develop these theorems further.

3. Subtyping as a SAT problem

4. ​Implementation

5. Further work and limitations

Last updated

Was this helpful?