larrytheliquid
June 03, 2015
280

June 03, 2015

## Transcript

1. ### Expressionless Weak-Head Normal Forms Larry Diehl Portland State University June

3rd, 2015 Wednesday, June 3, 15
2. ### Motivation • Dependent Type Checking • Formal implementation of an

“efﬁcient” dependent type checker with grammatically-enforced partial correctness and an independence property. With liberty and justice for all. Wednesday, June 3, 15
3. ### Outline • Why DT checking is tricky • Normalization using

• Only Expressions • directly to NF • passing through WHNF • Only Normal forms • via hereditary substitution • An environment machine (closure-based) • Expressions and dependent WHNFs • Independent WHNFs Wednesday, June 3, 15
4. ### Legend Values depend on the expression monarchy :( Values are

independent! Hey, I’ve seen this before... Gotta go fast! Wednesday, June 3, 15
5. ### Pro Tip • Pay special attention to what • `is-a-constructor

• mostly used for syntax • is-a-function • mostly used for semantics Wednesday, June 3, 15

7. ### Type Checking with Redexes in Type and Term one :

if true then ℕ else ⊥ one = (λ x ! x) suc zero Wednesday, June 3, 15
8. ### Type Checking with Redexes in Term One : Set One

= if true then ℕ else ⊥ one : One one = (λ x ! x) suc zero Wednesday, June 3, 15
9. ### Type Checking with Redexes in Term One : Set One

= if true then ℕ else ⊥ one : ℕ one = (λ x ! x) suc zero Wednesday, June 3, 15
10. ### infer Γ (f `∙ a) = infer Γ a >>=

λ A ! infer Γ f >>= λ { (`Π A' B) ! if A == A' then return (B ∙ a) else nothing ; _ ! nothing } Wednesday, June 3, 15
11. ### infer : ∀{γ} ! Ctx γ ! Exp γ !

Maybe (Exp γ) infer Γ (f `∙ a) = infer Γ a >>= λ A ! infer Γ f >>= λ { (`Π A' B) ! if A == A' then return (B ∙ a) else nothing ; _ ! nothing } Wednesday, June 3, 15

13. ### data Exp (γ : ℕ) : Set where `Type :

Exp γ `Π : (A : Exp γ)(B : Exp (suc γ)) ! Exp γ `λ : (b : Exp (suc γ)) ! Exp γ `var : (i : Var γ) ! Exp γ _`∙_ : (f : Exp γ)(a : Exp γ) ! Exp γ Wednesday, June 3, 15
14. ### data Exp (γ : ℕ) : Set where `Type :

Exp γ `Π : (A : Exp γ)(B : Bind Exp γ) ! Exp γ `λ : (b : Bind Exp γ) ! Exp γ `var : (i : Var γ) ! Exp γ _`∙_ : (f : Exp γ)(a : Exp γ) ! Exp γ Wednesday, June 3, 15
15. ### record Bind (A : ℕ ! Set) (γ : ℕ)

: Set where inductive constructor `∣_∣ field val : A (suc γ) Wednesday, June 3, 15
16. ### norm : ∀{γ} ! Exp γ ! Exp γ norm

`Type = `Type norm (`Π A B) = `Π (norm A) `∣ norm B ∣ norm (`λ b) = `λ `∣ norm b ∣ norm (`var i) = `var i norm (f `∙ a) = norm f ∙ norm a _∙_ : ∀{γ} ! Exp γ ! Exp γ ! Exp γ `λ `∣ b ∣ ∙ a = norm (sub a b) f ∙ a = f `∙ a Wednesday, June 3, 15
17. ### norm : ∀{γ} ! Exp γ ! Exp γ norm

`Type = `Type norm (`Π A B) = `Π (norm A) (normᴮ B) norm (`λ b) = `λ (normᴮ b) norm (`var i) = `var i norm (f `∙ a) = norm f ∙ norm a normᴮ : ∀{γ} ! Bind Exp γ ! Bind Exp γ normᴮ `∣ b ∣ = `∣ norm b ∣ _∙_ : ∀{γ} ! Exp γ ! Exp γ ! Exp γ `λ b ∙ a = b ∙ᴮ a f ∙ a = f `∙ a _∙ᴮ_ : ∀{γ} ! Bind Exp γ ! Exp γ ! Exp γ `∣ b ∣ ∙ᴮ a = norm (sub a b) Wednesday, June 3, 15

19. ### data Nf (γ : ℕ) : Set where `Type :

Nf γ `Π : (A : Nf γ) (B : Bind Nf γ) ! Nf γ `λ : (b : Bind Nf γ) ! Nf γ `[_] : Ne γ ! Nf γ data Ne (γ : ℕ) : Set where `var : (i : Var γ) ! Ne γ _`∙_ : (f : Ne γ) (a : Nf γ) ! Ne γ Wednesday, June 3, 15
20. ### Env : ℕ ! ℕ ! Set Env φ γ

= Vec (Nf φ) γ _`!_ : ∀{γ} (A B : Nf γ) ! Nf γ A `! B = `Π A `∣ wkn B ∣ lift : ∀{φ γ} ! Env φ γ ! Env (suc φ) (suc γ) lift σ = `x 0 ∷ map wkn σ Wednesday, June 3, 15
21. ### hsub : ∀{φ γ} ! Env φ γ ! Nf

γ ! Nf φ hsub σ `Type = `Type hsub σ (`Π A B) = `Π (hsub σ A) (hsubᴮ σ B) hsub σ (`λ b) = `λ (hsubᴮ σ b) hsub σ `[ a ] = hsubᴺ σ a hsubᴺ : ∀{φ γ} ! Env φ γ ! Ne γ ! Nf φ hsubᴺ σ (`var i) = lookup i σ hsubᴺ σ (f `∙ a) = hsubᴺ σ f ∙ hsub σ a Wednesday, June 3, 15
22. ### hsubᴮ : ∀{φ γ} ! Env φ γ ! Bind

Nf γ ! Bind Nf φ hsubᴮ σ `∣ b ∣ = `∣ hsub (lift σ) b ∣ _∙_ : ∀{γ} ! Nf γ ! Nf γ ! Nf γ `λ b ∙ a = b ∙ᴷ a `[ f ] ∙ a = `[ f `∙ a ] f ∙ a = undefined _∙ᴷ_ : ∀{γ} ! Bind Nf γ ! Nf γ ! Nf γ `∣ b ∣ ∙ᴷ a = hsub (a ∷ idEnv) b Wednesday, June 3, 15

3, 15

15

15
27. ### data Exp (γ : ℕ) : Set where `λ :

(b : Bind Exp γ) ! Exp γ `var : (i : Var γ) ! Exp γ _`∙_ : (f : Exp γ) (a : Exp γ) ! Exp γ Wednesday, June 3, 15
28. ### Pi : Nf 0 Pi = `Π `Type `∣ `x

0 `! `Type ∣ `! `Type Π' : Nf 0 Π' = `λ `∣ `λ `∣ `Π (`x 1) `∣ `[ `xᴺ 1 `∙ `x 0 ] ∣ ∣ ∣ Prim : ℕ Prim = 2 prim : Env 0 Prim prim = Π' ∷ `Type ∷ [] Wednesday, June 3, 15
29. ### norm : ∀{γ} ! Exp γ ! Nf γ norm

(`λ b) = `λ (normᴮ b) norm (`var i) = `[ `var i ] norm (f `∙ a) = norm f ∙ norm a normᴮ : ∀{γ} ! Bind Exp γ ! Bind Nf γ normᴮ `∣ b ∣ = `∣ norm b ∣ prim-norm : Exp Prim ! Nf 0 prim-norm = hsub prim ∘ norm Wednesday, June 3, 15

31. ### data Exp (γ : ℕ) : Set where `Type :

Exp γ `Π : (A : Exp γ) (B : Bind Exp γ) ! Exp γ `λ : (b : Bind Exp γ) ! Exp γ `var : (i : Var γ) ! Exp γ _`∙_ : (f : Exp γ) (a : Exp γ) ! Exp γ Wednesday, June 3, 15
32. ### wh-norm : ∀{γ} ! Exp γ ! Exp γ wh-norm

`Type = `Type wh-norm (`Π A B) = `Π (wh-norm A) (wh-normᴮ B) wh-norm (`λ b) = `λ (wh-normᴮ b) wh-norm (`var i) = `var i wh-norm (f `∙ a) = wh-norm f ∙ wh-norm a Wednesday, June 3, 15
33. ### wh-normᴮ : ∀{γ} ! Bind Exp γ ! Bind Exp

γ wh-normᴮ b = b _∙_ : ∀{γ} ! Exp γ ! Exp γ ! Exp γ `λ b ∙ a = b ∙ᴮ a f ∙ a = f `∙ a _∙ᴮ_ : ∀{γ} ! Bind Exp γ ! Exp γ ! Exp γ `∣ b ∣ ∙ᴮ a = wh-norm (sub a b) Wednesday, June 3, 15
34. ### -- WHNF as input force : ∀{γ} ! Exp γ

! Exp γ force `Type = `Type force (`Π A B) = `Π (force A) (forceᴮ B) force (`λ b) = `λ (forceᴮ b) force (`var i) = `var i force (f `∙ a) = force f `∙ force a forceᴮ : ∀{γ} ! Bind Exp γ ! Bind Exp γ forceᴮ `∣ b ∣ = `∣ force (wh-norm b) ∣ norm : ∀{γ} ! Exp γ ! Exp γ norm = force ∘ wh-norm Wednesday, June 3, 15
35. ### Normalizing using an Environment Machine* via Dependent WHNFs * Such

as Krivine’s machine, Felleisen et al.’s CEK machine, and Leroy’s Zinc abstract machine. Wednesday, June 3, 15
36. ### data Exp (γ : ℕ) : Set where `Type :

Exp γ `Π : (A : Exp γ) (B : Bind Exp γ) ! Exp γ `λ : (b : Bind Exp γ) ! Exp γ `var : (i : Var γ) ! Exp γ _`∙_ : (f : Exp γ) (a : Exp γ) ! Exp γ Wednesday, June 3, 15
37. ### data Wh (γ : ℕ) : Set where `Type :

Wh γ `Π : (A : Wh γ){δ:ℕ} (σ : Env γ δ)(B : Exp (suc δ)) ! Wh γ `λ : {δ:ℕ} (σ : Env γ δ)(b : Exp (suc δ)) ! Wh γ `[_] : Nu γ ! Wh γ data Nu (γ : ℕ) : Set where `var : (i : Var γ) ! Nu γ _`∙_ : (f : Nu γ) (a : Wh γ) ! Nu γ Wednesday, June 3, 15
38. ### data Wh (γ : ℕ) : Set where `Type :

Wh γ `Π : (A : Wh γ) (B : Close Wh Exp γ) ! Wh γ `λ : (b : Close Wh Exp γ) ! Wh γ `[_] : Nu γ ! Wh γ data Nu (γ : ℕ) : Set where `var : (i : Var γ) ! Nu γ _`∙_ : (f : Nu γ) (a : Wh γ) ! Nu γ Wednesday, June 3, 15
39. ### record Bind (A : ℕ ! Set) (γ : ℕ)

: Set where inductive constructor `∣_∣ field val : A (suc γ) Wednesday, June 3, 15
40. ### record Close (A B : ℕ ! Set) (γ :

ℕ) : Set where inductive constructor _`/_ field {scope} : ℕ env : Vec (A γ) scope val : B (suc scope) Wednesday, June 3, 15
41. ### Env : ℕ ! ℕ ! Set Env φ γ

= Vec (Wh φ) γ _`!_ : ∀{γ} (A : Wh γ) (B : Exp γ) ! Wh γ A `! B = `Π A ∣ wkn B ∣ ∣_∣ : ∀{γ} ! Exp (suc γ) ! Close Wh Exp γ ∣ a ∣ = idEnv `/ a Wednesday, June 3, 15
42. ### eval : ∀{φ γ} ! Env φ γ ! Exp

γ ! Wh φ eval σ `Type = `Type eval σ (`Π A B) = `Π (eval σ A) (evalᴷ σ B) eval σ (`λ b) = `λ (evalᴷ σ b) eval σ (`var i) = lookup i σ eval σ (f `∙ a) = eval σ f ∙ eval σ a Wednesday, June 3, 15
43. ### evalᴷ : ∀{φ γ} ! Env φ γ ! Bind

Exp γ ! Close Wh Exp φ evalᴷ σ `∣ b ∣ = σ `/ b _∙_ : ∀{γ} ! Wh γ ! Wh γ ! Wh γ `λ b ∙ a = b ∙ᴷ a `[ f ] ∙ a = `[ f `∙ a ] f ∙ a = undefined _∙ᴷ_ : ∀{γ} ! Close Wh Exp γ ! Wh γ ! Wh γ (σ `/ b) ∙ᴷ a = eval (a ∷ σ) b Wednesday, June 3, 15
44. ### data Nf (γ : ℕ) : Set where `Type :

Nf γ `Π : (A : Nf γ) (B : Bind Nf γ) ! Nf γ `λ : (b : Bind Nf γ) ! Nf γ `[_] : Ne γ ! Nf γ data Ne (γ : ℕ) : Set where `var : (i : Var γ) ! Ne γ _`∙_ : (f : Ne γ) (a : Nf γ) ! Ne γ Wednesday, June 3, 15
45. ### force : ∀{γ} ! Wh γ ! Nf γ force

`Type = `Type force (`Π A B) = `Π (force A) (forceᴷ B) force (`λ b) = `λ (forceᴷ b) force `[ a ] = `[ forceᴺ a ] forceᴺ : ∀{γ} ! Nu γ ! Ne γ forceᴺ (`var i) = `var i forceᴺ (f `∙ a) = forceᴺ f `∙ force a Wednesday, June 3, 15
46. ### forceᴷ : ∀{γ} ! Close Wh Exp γ ! Bind

Nf γ forceᴷ b = `∣ force (! b) ∣ !_ : ∀{γ} ! Close Wh Exp γ ! Wh (suc γ) ! (σ `/ b) = eval (lift σ) b wh-norm : ∀{γ} ! Exp γ ! Wh γ wh-norm = eval idEnv norm : ∀{γ} ! Exp γ ! Nf γ norm = force ∘ wh-norm Wednesday, June 3, 15
47. ### Normalizing using an Environment Machine* via Independent WHNFs * Such

as Krivine’s machine, Felleisen et al.’s CEK machine, and Leroy’s Zinc abstract machine. Wednesday, June 3, 15
48. ### data Wh (γ : ℕ) : Set where `Type :

Wh γ `Π : (A : Wh γ) (B : Close Wh Wh γ) ! Wh γ `λ : (b : Close Wh Wh γ) ! Wh γ `[_] : Nu γ ! Wh γ data Nu (γ : ℕ) : Set where `var : (i : Var γ) ! Nu γ _`∙_ : (f : Nu γ) (a : Wh γ) ! Nu γ Wednesday, June 3, 15
49. ### Env : ℕ ! ℕ ! Set Env φ γ

= Vec (Wh φ) γ _`!_ : ∀{γ} (A B : Wh γ) ! Wh γ A `! B = `Π A ∣ wkn B ∣ ∣_∣ : ∀{γ} ! Wh (suc γ) ! Close Wh Wh γ ∣ a ∣ = idEnv `/ a Wednesday, June 3, 15
50. ### wh-hsub : ∀{φ γ} ! Env φ γ ! Wh

γ ! Wh φ wh-hsub σ `Type = `Type wh-hsub σ (`Π A B) = `Π (wh-hsub σ A) (wh-hsubᴷ σ B) wh-hsub σ (`λ b) = `λ (wh-hsubᴷ σ b) wh-hsub σ `[ a ] = wh-hsubᴺ σ a wh-hsubᴺ : ∀{φ γ} ! Env φ γ ! Nu γ ! Wh φ wh-hsubᴺ σ (`var i) = lookup i σ wh-hsubᴺ σ (f `∙ a) = wh-hsubᴺ σ f ∙ wh-hsub σ a Wednesday, June 3, 15
51. ### wh-hsubᴷ : ∀{φ γ} ! Env φ γ ! Close

Wh Wh γ ! Close Wh Wh φ wh-hsubᴷ σ (ρ `/ b) = map (wh-hsub σ) ρ `/ b _∙_ : ∀{γ} ! Wh γ ! Wh γ ! Wh γ `λ b ∙ a = b ∙ᴷ a `[ f ] ∙ a = `[ f `∙ a ] f ∙ a = undefined _∙ᴷ_ : ∀{γ} ! Close Wh Wh γ ! Wh γ ! Wh γ (σ `/ b) ∙ᴷ a = wh-hsub (a ∷ σ) b Wednesday, June 3, 15
52. ### data Nf (γ : ℕ) : Set where `Type :

Nf γ `Π : (A : Nf γ) (B : Bind Nf γ) ! Nf γ `λ : (b : Bind Nf γ) ! Nf γ `[_] : Ne γ ! Nf γ data Ne (γ : ℕ) : Set where `var : (i : Var γ) ! Ne γ _`∙_ : (f : Ne γ) (a : Nf γ) ! Ne γ Wednesday, June 3, 15
53. ### force : ∀{γ} ! Wh γ ! Nf γ force

`Type = `Type force (`Π A B) = `Π (force A) (forceᴷ B) force (`λ b) = `λ (forceᴷ b) force `[ a ] = `[ forceᴺ a ] forceᴺ : ∀{γ} ! Nu γ ! Ne γ forceᴺ (`var i) = `var i forceᴺ (f `∙ a) = forceᴺ f `∙ force a Wednesday, June 3, 15
54. ### forceᴷ : ∀{γ} ! Close Wh Wh γ ! Bind

Nf γ forceᴷ b = `∣ force (! b) ∣ !_ : ∀{γ} ! Close Wh Wh γ ! Wh (suc γ) ! (σ `/ b) = wh-hsub (lift σ) b Wednesday, June 3, 15
55. ### data Exp (γ : ℕ) : Set where `λ :

(b : Bind Exp γ) ! Exp γ `var : (i : Var γ) ! Exp γ _`∙_ : (f : Exp γ) (a : Exp γ) ! Exp γ Wednesday, June 3, 15
56. ### Pi : Wh 0 Pi = `Π `Type `∣ `x

0 `! `Type ∣ `! `Type Π' : Wh 0 Π' = `λ `∣ `λ `∣ `Π (`x 1) `∣ `[ `xᴺ 1 `∙ `x 0 ] ∣ ∣ ∣ Prim : ℕ Prim = 2 prim : Env 0 Prim prim = Π' ∷ `Type ∷ [] Wednesday, June 3, 15
57. ### wh-norm : ∀{γ} ! Exp γ ! Wh γ wh-norm

(`λ b) = `λ (wh-normᴮ b) wh-norm (`var i) = `[ `var i ] wh-norm (f `∙ a) = wh-norm f ∙ wh-norm a wh-normᴮ : ∀{γ} ! Bind Exp γ ! Close Wh Wh γ wh-normᴮ `∣ b ∣ = ∣ wh-norm b ∣ Wednesday, June 3, 15
58. ### prim-wh-norm : Exp Prim ! Wh 0 prim-wh-norm = wh-hsub

prim ∘ wh-norm norm : ∀{γ} ! Exp γ ! Nf γ norm = force ∘ wh-norm prim-norm : Exp Prim ! Nf 0 prim-norm = force ∘ prim-wh-norm Wednesday, June 3, 15

60. ### infer : ∀{γ} ! Ctx γ ! Exp γ !

Maybe (Wh γ) infer Γ (f `∙ a) = infer Γ a >>= λ A ! infer Γ f >>= λ { (`Π A' B) ! if A ≈ A' then return (B ∙ᴷ wh-norm a) else nothing ; _ ! nothing } Wednesday, June 3, 15
61. ### _≈_ : ∀{γ} ! Wh γ ! Wh γ !

Bool `Type ≈ `Type = true `Π A₁ B₁ ≈ `Π A₂ B₂ = A₁ ≈ A₂ ∧ B₁ ≈ᴷ B₂ `λ b₁ ≈ `λ b₂ = b₁ ≈ᴷ b₂ `[ a₁ ] ≈ `[ a₂ ] = a₁ ≈ᴺ a₂ _ ≈ _ = false _≈ᴺ_ : ∀{γ} ! Nu γ ! Nu γ ! Bool (f₁ `∙ a₁) ≈ᴺ (f₂ `∙ a₂) = (f₁ ≈ᴺ f₂) ∧ a₁ ≈ a₂ `var i ≈ᴺ `var j = i ==ᴿ j _ ≈ᴺ _ = false Wednesday, June 3, 15
62. ### _≈ᴷ_ : ∀{γ} ! Close Wh Wh γ ! Close

Wh Wh γ ! Bool b₁ ≈ᴷ b₂ = b₁ == b₂ ∨ (! b₁) ≈ (! b₂) !_ : ∀{γ} ! Close Wh Wh γ ! Wh (suc γ) ! (σ `/ b) = wh-hsub (lift σ) b Wednesday, June 3, 15
63. ### POPL Goal • All partial functions formalized as relations •

Realizability semantics for independent WHNFs • Termination proof for wh-hsub • Decidability proof for κ-conversion • Completed above for Gödel’s System T • Currently extending to MLTT Wednesday, June 3, 15