@pithlessly · github · λ

The comptime calculus

This is an attempt at formalizing some aspects of Zig’s comptime. We have three types of binders:

Conventions:

We will be simultaneously defining two relations:

ΓM:a M has type a in Γ Mz:a M evaluates to z at type a  \begin{aligned} \boxed{ \Gamma \vdash M : a } & \qquad \text{ $M$ has type $a$ in $\Gamma$ } \\ \boxed{ M \leadsto z : a } & \qquad \text{ $M$ evaluates to $z$ at type $a$ } \end{aligned}

We only evaluate closed terms.

 Eval-Val z:azz:a Ty-λ Aa:Bb:Γ,x:aE:bΓ(λ(x:A)E:B):ab Ty-App-λ ΓE1:abΓE2:aΓE1(E2):b Eval-App-λ E1(λxE):abE2v2:a[v2/x]Ev:bE1(E2)v:b Ty-λs Aa:Bb:(λs(x:A)E:B):asb Ty-App-λs E1(λsxE):asbE2v2:a[v2/x]E:bE1(E2):b Eval-App-λs E1(λsxE):asbE2v2:a[v2/x]Ev:bE1(E2)v:b Ty-Λs Aa:Γ(Λs(x:A)E:B):(x:a)sB Ty-App-Λs E1(ΛsxE):(x:a)sBE2v2:a[v2/x]Bb:[v2/x]E:bE1(E2):b Eval-App-Λs E1(ΛsxE):(x:a)sBE2v2:a[v2/x]Ev:bE1(E2)v:b \begin{aligned} \boxed{\text{ Eval-Val }} & \qquad \frac{ \cdot \vdash z : a }{ z \leadsto z : a } \\\\ \boxed{\text{ Ty-$\lambda$ }} & \qquad \frac{ A \leadsto a : * \qquad B \leadsto b : * \qquad \Gamma, x : a \vdash E : b }{ \Gamma \vdash (\lambda (x : A) \to E : B) : a \to b } \\\\ \boxed{\text{ Ty-App-$\lambda$ }} & \qquad \frac{ \Gamma \vdash E_1 : a \to b \qquad \Gamma \vdash E_2 : a }{ \Gamma \vdash E_1(E_2) : b } \\\\ \boxed{\text{ Eval-App-$\lambda$ }} & \qquad \frac{ E_1 \leadsto (\lambda x \to E') : a \to b \qquad E_2 \leadsto v_2 : a \qquad [v_2 / x]E' \leadsto v' : b }{ E_1(E_2) \leadsto v' : b } \\\\ \boxed{\text{ Ty-$\lambda_s$ }} & \qquad \frac{ A \leadsto a : * \qquad B \leadsto b : * }{ \cdot \vdash (\lambda_s (x : A) \to E : B) : a \to_s b } \\\\ \boxed{\text{ Ty-App-$\lambda_s$ }} & \qquad \frac{ E_1 \leadsto (\lambda_s x \to E') : a \to_s b \qquad E_2 \leadsto v_2 : a \qquad \cdot \vdash [v_2/x] E' : b }{ \cdot \vdash E_1(E_2) : b } \\\\ \boxed{\text{ Eval-App-$\lambda_s$ }} & \qquad \frac{ E_1 \leadsto (\lambda_s x \to E') : a \to_s b \qquad E_2 \leadsto v_2 : a \qquad [v_2/x] E' \leadsto v' : b }{ E_1(E_2) \leadsto v' : b } \\\\ \boxed{\text{ Ty-$\Lambda_s$ }} & \qquad \frac{ A \leadsto a : * }{ \Gamma \vdash (\Lambda_s (x : A) \to E : B) : \forall (x : a) \to_s B } \\\\ \boxed{\text{ Ty-App-$\Lambda_s$ }} & \qquad \frac{ E_1 \leadsto (\Lambda_s x \to E') : \forall (x : a) \to_s B \qquad E_2 \leadsto v_2 : a \qquad [v_2/x] B \leadsto b : * \qquad \cdot \vdash [v_2/x] E' : b }{ \cdot \vdash E_1(E_2) : b } \\\\ \boxed{\text{ Eval-App-$\Lambda_s$ }} & \qquad \frac{ E_1 \leadsto (\Lambda_s x \to E') : \forall (x : a) \to_s B \qquad E_2 \leadsto v_2 : a \qquad [v_2/x] E' \leadsto v' : b }{ E_1(E_2) \leadsto v' : b } \end{aligned}

Note the general trend: as we move from λ\lambda to λs\lambda_s to Λs\Lambda_s, we gradually move more of the premises from Ty-Foo\text{Ty-Foo} to Ty-App-Foo\text{Ty-App-Foo}.