@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 Γ M⇝z: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:az⇝z:a Ty-λ A⇝a:∗B⇝b:∗Γ,x:a⊢E:bΓ⊢(λ(x:A)→E:B):a→b Ty-App-λ Γ⊢E1:a→bΓ⊢E2:aΓ⊢E1(E2):b Eval-App-λ E1⇝(λx→E′):a→bE2⇝v2:a[v2/x]E′⇝v′:bE1(E2)⇝v′:b Ty-λs A⇝a:∗B⇝b:∗⋅⊢(λs(x:A)→E:B):a→sb Ty-App-λs E1⇝(λsx→E′):a→sbE2⇝v2:a⋅⊢[v2/x]E′:b⋅⊢E1(E2):b Eval-App-λs E1⇝(λsx→E′):a→sbE2⇝v2:a[v2/x]E′⇝v′:bE1(E2)⇝v′:b Ty-Λs A⇝a:∗Γ⊢(Λs(x:A)→E:B):∀(x:a)→sB Ty-App-Λs E1⇝(Λsx→E′):∀(x:a)→sBE2⇝v2:a[v2/x]B⇝b:∗⋅⊢[v2/x]E′:b⋅⊢E1(E2):b Eval-App-Λs E1⇝(Λsx→E′):∀(x:a)→sBE2⇝v2:a[v2/x]E′⇝v′: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}.