Typechecker.agda 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. module Typechecker where
  2. data Nat : Set where
  3. zero : Nat
  4. suc : Nat -> Nat
  5. {-# BUILTIN NATURAL Nat #-}
  6. _+_ : Nat -> Nat -> Nat
  7. zero + m = m
  8. suc n + m = suc (n + m)
  9. data Fin : Nat -> Set where
  10. f0 : {n : Nat} -> Fin (suc n)
  11. fs : {n : Nat} -> Fin n -> Fin (suc n)
  12. infixl 30 _::_ _++_
  13. data Vect (A : Set) : Nat -> Set where
  14. ε : Vect A zero
  15. _::_ : {n : Nat} -> Vect A n -> A -> Vect A (suc n)
  16. fs^ : {n : Nat}(m : Nat) -> Fin n -> Fin (m + n)
  17. fs^ zero i = i
  18. fs^ (suc m) i = fs (fs^ m i)
  19. _++_ : {A : Set}{n m : Nat} -> Vect A n -> Vect A m -> Vect A (m + n)
  20. xs ++ ε = xs
  21. xs ++ (ys :: y) = (xs ++ ys) :: y
  22. data Chop {A : Set} : {n : Nat} -> Vect A n -> Fin n -> Set where
  23. chopGlue : {m n : Nat}(xs : Vect A n)(x : A)(ys : Vect A m) ->
  24. Chop (xs :: x ++ ys) (fs^ m f0)
  25. chop : {A : Set}{n : Nat}(xs : Vect A n)(i : Fin n) -> Chop xs i
  26. chop ε ()
  27. chop (xs :: x) f0 = chopGlue xs x ε
  28. chop (xs :: y) (fs i) with chop xs i
  29. chop (.(xs :: x ++ ys) :: y) (fs .(fs^ m f0))
  30. | chopGlue {m} xs x ys = chopGlue xs x (ys :: y)
  31. infixr 40 _⊃_
  32. data Simp : Set where
  33. o : Simp
  34. _⊃_ : Simp -> Simp -> Simp
  35. infix 20 Simp-_
  36. data Simp-_ : Simp -> Set where
  37. neqo : Simp -> Simp -> Simp- o
  38. neq⊃ : {S T : Simp} -> Simp- (S ⊃ T)
  39. neqT : {S T : Simp}(T' : Simp- T) -> Simp- (S ⊃ T)
  40. neqS : {S : Simp}{T₁ : Simp}(S' : Simp- S)(T₂ : Simp) -> Simp- (S ⊃ T₁)
  41. infixl 60 _∖_
  42. _∖_ : (S : Simp) -> Simp- S -> Simp
  43. .o ∖ neqo S T = S ⊃ T
  44. .(_ ⊃ _) ∖ neq⊃ = o
  45. .(S ⊃ T) ∖ neqT {S}{T} T' = S ⊃ T ∖ T'
  46. .(S ⊃ _) ∖ neqS {S} S' T₂ = S ∖ S' ⊃ T₂
  47. data SimpEq? (S : Simp) : Simp -> Set where
  48. simpSame : SimpEq? S S
  49. simpDiff : (T : Simp- S) -> SimpEq? S (S ∖ T)
  50. simpEq? : (S T : Simp) -> SimpEq? S T
  51. simpEq? o o = simpSame
  52. simpEq? o (S ⊃ T) = simpDiff (neqo S T)
  53. simpEq? (S ⊃ T) o = simpDiff neq⊃
  54. simpEq? (S₁ ⊃ T₁) (S₂ ⊃ T₂) with simpEq? S₁ S₂
  55. simpEq? (S ⊃ T₁) (.S ⊃ T₂) | simpSame with simpEq? T₁ T₂
  56. simpEq? (S ⊃ T) (.S ⊃ .T) | simpSame | simpSame = simpSame
  57. simpEq? (S ⊃ T) (.S ⊃ .(T ∖ T')) | simpSame | simpDiff T' = simpDiff (neqT T')
  58. simpEq? (S ⊃ T₁) (.(S ∖ S') ⊃ T₂) | simpDiff S' = simpDiff (neqS S' T₂)
  59. data Term : Nat -> Set where
  60. var : {n : Nat} -> Fin n -> Term n
  61. app : {n : Nat} -> Term n -> Term n -> Term n
  62. lam : {n : Nat} -> Simp -> Term (suc n) -> Term n
  63. data Good : {n : Nat} -> Vect Simp n -> Simp -> Set where
  64. gVar : {n m : Nat}(Γ : Vect Simp n)(T : Simp)(Δ : Vect Simp m) ->
  65. Good (Γ :: T ++ Δ) T
  66. gApp : {n : Nat}{Γ : Vect Simp n}{S T : Simp} ->
  67. Good Γ (S ⊃ T) -> Good Γ S -> Good Γ T
  68. gLam : {n : Nat}{Γ : Vect Simp n}(S : Simp){T : Simp} ->
  69. Good (Γ :: S) T -> Good Γ (S ⊃ T)
  70. g : {n : Nat}{Γ : Vect Simp n}(T : Simp) -> Good Γ T -> Term n
  71. g T (gVar{n}{m} Γ .T Δ) = var (fs^ m f0)
  72. g T (gApp f s) = app (g _ f) (g _ s)
  73. g .(S ⊃ _) (gLam S t) = lam S (g _ t)
  74. data Bad {n : Nat}(Γ : Vect Simp n) : Set where
  75. bNonFun : Good Γ o -> Term n -> Bad Γ
  76. bMismatch : {S T : Simp}(S' : Simp- S) ->
  77. Good Γ (S ⊃ T) -> Good Γ (S ∖ S') -> Bad Γ
  78. bArg : {S T : Simp} -> Good Γ (S ⊃ T) -> Bad Γ -> Bad Γ
  79. bFun : Bad Γ -> Term n -> Bad Γ
  80. bLam : (S : Simp) -> Bad (Γ :: S) -> Bad Γ
  81. b : {n : Nat}{Γ : Vect Simp n} -> Bad Γ -> Term n
  82. b (bNonFun f s) = app (g o f) s
  83. b (bMismatch _ f s) = app (g _ f) (g _ s)
  84. b (bArg f s) = app (g _ f) (b s)
  85. b (bFun f s) = app (b f) s
  86. b (bLam S t) = lam S (b t)
  87. data TypeCheck? {n : Nat}(Γ : Vect Simp n) : Term n -> Set where
  88. good : (T : Simp)(t : Good Γ T) -> TypeCheck? Γ (g T t)
  89. bad : (t : Bad Γ) -> TypeCheck? Γ (b t)
  90. typeCheck? : {n : Nat}(Γ : Vect Simp n)(t : Term n) -> TypeCheck? Γ t
  91. typeCheck? Γ (var i) with chop Γ i
  92. typeCheck? .(Γ :: T ++ Δ) (var ._) | chopGlue Γ T Δ = good T (gVar Γ T Δ)
  93. typeCheck? Γ (app f s) with typeCheck? Γ f
  94. typeCheck? Γ (app .(g (S ⊃ T) f) s) | good (S ⊃ T) f with typeCheck? Γ s
  95. typeCheck? Γ (app .(g (S ⊃ T) f) .(g S' s))
  96. | good (S ⊃ T) f | good S' s with simpEq? S S'
  97. typeCheck? Γ (app .(g (S ⊃ T) f) .(g S s))
  98. | good (S ⊃ T) f | good .S s | simpSame = good T (gApp f s)
  99. typeCheck? Γ (app .(g (S ⊃ T) f) .(g (S ∖ S') s))
  100. | good (S ⊃ T) f | good .(S ∖ S') s | simpDiff S' = bad (bMismatch S' f s)
  101. typeCheck? Γ (app .(g (S ⊃ T) f) .(b s))
  102. | good (S ⊃ T) f | bad s = bad (bArg f s)
  103. typeCheck? Γ (app .(g o f) s) | good o f = bad (bNonFun f s)
  104. typeCheck? Γ (app .(b f) s) | bad f = bad (bFun f s)
  105. typeCheck? Γ (lam S t) with typeCheck? (Γ :: S) t
  106. typeCheck? Γ (lam S .(g T t)) | good T t = good (S ⊃ T) (gLam S t)
  107. typeCheck? Γ (lam S .(b t)) | bad t = bad (bLam S t)