NBE.agda 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. -- NBE for Gödel System T
  2. module NBE where
  3. module Prelude where
  4. -- Zero and One -----------------------------------------------------------
  5. data Zero : Set where
  6. data One : Set where
  7. unit : One
  8. -- Natural numbers --------------------------------------------------------
  9. data Nat : Set where
  10. zero : Nat
  11. suc : Nat -> Nat
  12. (+) : Nat -> Nat -> Nat
  13. zero + m = m
  14. suc n + m = suc (n + m)
  15. -- Props ------------------------------------------------------------------
  16. data True : Prop where
  17. tt : True
  18. data False : Prop where
  19. postulate
  20. falseE : (A:Set) -> False -> A
  21. infix 3 /\
  22. data (/\)(P Q:Prop) : Prop where
  23. andI : P -> Q -> P /\ Q
  24. module Fin where
  25. open Prelude
  26. -- Finite sets ------------------------------------------------------------
  27. data Suc (A:Set) : Set where
  28. fzero_ : Suc A
  29. fsuc_ : A -> Suc A
  30. mutual
  31. data Fin (n:Nat) : Set where
  32. finI : Fin_ n -> Fin n
  33. Fin_ : Nat -> Set
  34. Fin_ zero = Zero
  35. Fin_ (suc n) = Suc (Fin n)
  36. fzero : {n:Nat} -> Fin (suc n)
  37. fzero = finI fzero_
  38. fsuc : {n:Nat} -> Fin n -> Fin (suc n)
  39. fsuc i = finI (fsuc_ i)
  40. finE : {n:Nat} -> Fin n -> Fin_ n
  41. finE (finI i) = i
  42. module Vec where
  43. open Prelude
  44. open Fin
  45. infixr 15 ::
  46. -- Vectors ----------------------------------------------------------------
  47. data Nil : Set where
  48. nil_ : Nil
  49. data Cons (A As:Set) : Set where
  50. cons_ : A -> As -> Cons A As
  51. mutual
  52. data Vec (A:Set)(n:Nat) : Set where
  53. vecI : Vec_ A n -> Vec A n
  54. Vec_ : Set -> Nat -> Set
  55. Vec_ A zero = Nil
  56. Vec_ A (suc n) = Cons A (Vec A n)
  57. nil : {A:Set} -> Vec A zero
  58. nil = vecI nil_
  59. (::) : {A:Set} -> {n:Nat} -> A -> Vec A n -> Vec A (suc n)
  60. x :: xs = vecI (cons_ x xs)
  61. vecE : {A:Set} -> {n:Nat} -> Vec A n -> Vec_ A n
  62. vecE (vecI xs) = xs
  63. vec : {A:Set} -> (n:Nat) -> A -> Vec A n
  64. vec zero _ = nil
  65. vec (suc n) x = x :: vec n x
  66. map : {n:Nat} -> {A B:Set} -> (A -> B) -> Vec A n -> Vec B n
  67. map {zero} f (vecI nil_) = nil
  68. map {suc n} f (vecI (cons_ x xs)) = f x :: map f xs
  69. (!) : {n:Nat} -> {A:Set} -> Vec A n -> Fin n -> A
  70. (!) {suc n} (vecI (cons_ x _ )) (finI fzero_) = x
  71. (!) {suc n} (vecI (cons_ _ xs)) (finI (fsuc_ i)) = xs ! i
  72. tabulate : {n:Nat} -> {A:Set} -> (Fin n -> A) -> Vec A n
  73. tabulate {zero} f = nil
  74. tabulate {suc n} f = f fzero :: tabulate (\x -> f (fsuc x))
  75. module Syntax where
  76. open Prelude
  77. open Fin
  78. -- Types ------------------------------------------------------------------
  79. infixr 8 =>
  80. data Type : Set where
  81. nat : Type
  82. (=>) : Type -> Type -> Type
  83. -- Terms ------------------------------------------------------------------
  84. data Term (n:Nat) : Set where
  85. eZero : Term n
  86. eSuc : Term n
  87. eApp : Term n -> Term n -> Term n
  88. eLam : Term (suc n) -> Term n
  89. eVar : Fin n -> Term n
  90. module NormalForms where
  91. open Prelude
  92. open Syntax
  93. open Fin
  94. mutual
  95. -- Normal terms -----------------------------------------------------------
  96. data Normal (n:Nat) : Set where
  97. nZero : Normal n
  98. nSuc : Normal n -> Normal n
  99. nLam : Normal (suc n) -> Normal n
  100. nNeutral : Neutral n -> Normal n
  101. nStuck : Normal n -- type error
  102. -- Neutral terms ----------------------------------------------------------
  103. data Neutral (n:Nat) : Set where
  104. uVar : Fin n -> Neutral n
  105. uApp : Neutral n -> Normal n -> Neutral n
  106. nVar : {n:Nat} -> Fin n -> Normal n
  107. nVar i = nNeutral (uVar i)
  108. nApp : {n:Nat} -> Neutral n -> Normal n -> Normal n
  109. nApp u n = nNeutral (uApp u n)
  110. module Rename where
  111. open Prelude
  112. open Fin
  113. open Vec
  114. open NormalForms
  115. -- Renamings --------------------------------------------------------------
  116. Ren : Nat -> Nat -> Set
  117. Ren m n = Vec (Fin n) m
  118. id : {n:Nat} -> Ren n n
  119. id = tabulate (\i -> i)
  120. compose : {l m n:Nat} -> Ren m n -> Ren l m -> Ren l n
  121. compose {l}{m}{n} ρ γ = map (\i -> ρ ! i) γ
  122. lift : {m n:Nat} -> Ren m n -> Ren (suc m) (suc n)
  123. lift ρ = fzero :: map fsuc ρ
  124. mutual
  125. rename : {m n:Nat} -> Ren m n -> Normal m -> Normal n
  126. rename ρ nZero = nZero
  127. rename ρ (nSuc n) = nSuc (rename ρ n)
  128. rename ρ (nLam n) = nLam (rename (lift ρ) n)
  129. rename ρ (nNeutral u) = nNeutral (renameNe ρ u)
  130. rename ρ nStuck = nStuck
  131. renameNe : {m n:Nat} -> Ren m n -> Neutral m -> Neutral n
  132. renameNe ρ (uVar i) = uVar (ρ ! i)
  133. renameNe ρ (uApp u n) = uApp (renameNe ρ u) (rename ρ n)
  134. up : {n:Nat} -> Ren n (suc n)
  135. up = map fsuc id
  136. module Subst where
  137. open Prelude
  138. open Fin
  139. open Vec
  140. open NormalForms
  141. open Rename using (Ren; rename; up)
  142. -- Substitutions ----------------------------------------------------------
  143. Sub : Nat -> Nat -> Set
  144. Sub m n = Vec (Normal n) m
  145. id : {n:Nat} -> Sub n n
  146. id = tabulate nVar
  147. ren2sub : {m n:Nat} -> Ren m n -> Sub m n
  148. ren2sub ρ = map nVar ρ
  149. lift : {m n:Nat} -> Sub m n -> Sub (suc m) (suc n)
  150. lift σ = nVar fzero :: map (rename up) σ
  151. mutual
  152. app : {n:Nat} -> Normal n -> Normal n -> Normal n
  153. app nZero _ = nStuck
  154. app (nSuc _) _ = nStuck
  155. app nStuck _ = nStuck
  156. app (nLam u) v = subst (v :: id) u
  157. app (nNeutral n) v = nApp n v
  158. subst : {m n:Nat} -> Sub m n -> Normal m -> Normal n
  159. subst σ nZero = nZero
  160. subst σ (nSuc v) = nSuc (subst σ v)
  161. subst σ (nLam v) = nLam (subst (lift σ) v)
  162. subst σ (nNeutral n) = substNe σ n
  163. subst σ nStuck = nStuck
  164. substNe : {m n:Nat} -> Sub m n -> Neutral m -> Normal n
  165. substNe σ (uVar i) = σ ! i
  166. substNe σ (uApp n v) = substNe σ n `app` subst σ v
  167. compose : {l m n:Nat} -> Sub m n -> Sub l m -> Sub l n
  168. compose σ δ = map (subst σ) δ
  169. module TypeSystem where
  170. open Prelude
  171. open Fin
  172. open Vec
  173. open Syntax
  174. mutual
  175. EqType : Type -> Type -> Prop
  176. EqType nat nat = True
  177. EqType (τ => τ') (σ => σ') = τ == σ /\ τ' == σ'
  178. EqType _ _ = False
  179. infix 5 ==
  180. data (==) (τ0 τ1:Type) : Prop where
  181. eqTypeI : EqType τ0 τ1 -> τ0 == τ1
  182. eqSubst : {σ τ:Type} -> (C:Type -> Set) -> σ == τ -> C τ -> C σ
  183. eqSubst {nat}{nat} C _ x = x
  184. eqSubst {σ => τ}{σ' => τ'} C (eqTypeI (andI eqσ eqτ)) x =
  185. eqSubst (\μ -> C (μ => τ)) eqσ (
  186. eqSubst (\η -> C (σ' => η)) eqτ x
  187. )
  188. Context : Nat -> Set
  189. Context n = Vec Type n
  190. mutual
  191. HasType : {n:Nat} -> Context n -> Term n -> Type -> Set
  192. HasType Γ eZero τ = ZeroType Γ τ
  193. HasType Γ eSuc τ = SucType Γ τ
  194. HasType Γ (eVar i) τ = VarType Γ i τ
  195. HasType Γ (eApp e1 e2) τ = AppType Γ e1 e2 τ
  196. HasType Γ (eLam e) τ = LamType Γ e τ
  197. data ZeroType {n:Nat}(Γ:Context n)(τ:Type) : Set where
  198. zeroType : τ == nat -> HasType Γ eZero τ
  199. data SucType {n:Nat}(Γ:Context n)(τ:Type) : Set where
  200. sucType : τ == (nat => nat) -> HasType Γ eSuc τ
  201. data VarType {n:Nat}(Γ:Context n)(i:Fin n)(τ:Type) : Set where
  202. varType : τ == (Γ ! i) -> HasType Γ (eVar i) τ
  203. data AppType {n:Nat}(Γ:Context n)(e1 e2:Term n)(τ:Type) : Set where
  204. appType : (σ:Type) -> HasType Γ e1 (σ => τ) -> HasType Γ e2 σ -> HasType Γ (eApp e1 e2) τ
  205. data LamType {n:Nat}(Γ:Context n)(e:Term (suc n))(τ:Type) : Set where
  206. lamType : (τ0 τ1:Type) -> τ == (τ0 => τ1) -> HasType (τ0 :: Γ) e τ1 -> HasType Γ (eLam e) τ
  207. module NBE where
  208. open Prelude
  209. open Syntax
  210. open Fin
  211. open Vec
  212. open TypeSystem
  213. mutual
  214. D_ : Nat -> Type -> Set
  215. D_ n nat = NatD n
  216. D_ n (σ => τ) = FunD n σ τ
  217. data D (n:Nat)(τ:Type) : Set where
  218. dI : D_ n τ -> D n τ
  219. data NatD (n:Nat) : Set where
  220. zeroD_ : D_ n nat
  221. sucD_ : D n nat -> D_ n nat
  222. neD_ : Term n -> D_ n nat
  223. -- Will this pass the positivity check?
  224. data FunD (n:Nat)(σ τ:Type) : Set where
  225. lamD_ : (D n σ -> D n τ) -> D_ n (σ => τ)
  226. zeroD : {n:Nat} -> D n nat
  227. zeroD = dI zeroD_
  228. sucD : {n:Nat} -> D n nat -> D n nat
  229. sucD d = dI (sucD_ d)
  230. neD : {n:Nat} -> Term n -> D n nat
  231. neD t = dI (neD_ t)
  232. lamD : {n:Nat} -> {σ τ:Type} -> (D n σ -> D n τ) -> D n (σ => τ)
  233. lamD f = dI (lamD_ f)
  234. coerce : {n:Nat} -> {τ0 τ1:Type} -> τ0 == τ1 -> D n τ1 -> D n τ0
  235. coerce {n} = eqSubst (D n)
  236. mutual
  237. down : {τ:Type} -> {n:Nat} -> D n τ -> Term n
  238. down {σ => τ} (dI (lamD_ f)) = eLam (down {τ} ?) -- doesn't work!
  239. down {nat} (dI zeroD_) = eZero
  240. down {nat} (dI (sucD_ d)) = eSuc `eApp` down d
  241. down {nat} (dI (neD_ t)) = t
  242. up : {n:Nat} -> (τ:Type) -> Term n -> D n τ
  243. up (σ => τ) t = lamD (\d -> up τ (t `eApp` down d))
  244. up nat t = neD t
  245. Valuation : {m:Nat} -> Nat -> Context m -> Set
  246. Valuation {zero} n _ = Nil
  247. Valuation {suc m} n (vecI (cons_ τ Γ)) = Cons (D n τ) (Valuation n Γ)
  248. (!!) : {m n:Nat} -> {Γ:Context m} -> Valuation n Γ -> (i:Fin m) -> D n (Γ ! i)
  249. (!!) {suc _} {_} {vecI (cons_ _ _)} (cons_ v ξ) (finI fzero_) = v
  250. (!!) {suc _} {_} {vecI (cons_ _ _)} (cons_ v ξ) (finI (fsuc_ i)) = ξ !! i
  251. ext : {m n:Nat} -> {τ:Type} -> {Γ:Context m} -> Valuation n Γ -> D n τ -> Valuation n (τ :: Γ)
  252. ext ξ v = cons_ v ξ
  253. app : {σ τ:Type} -> {n:Nat} -> D n (σ => τ) -> D n σ -> D n τ
  254. --app (dI (lamD_ f)) d = f d
  255. app (lamD f) d = f d
  256. eval : {n:Nat} -> {Γ:Context n} -> (e:Term n) -> (τ:Type) -> HasType Γ e τ -> Valuation n Γ -> D n τ
  257. eval (eVar i) τ (varType eq) ξ = coerce eq (ξ !! i)
  258. eval (eApp r s) τ (appType σ dr ds) ξ = eval r (σ => τ) dr ξ `app` eval s σ ds ξ
  259. eval (eLam r) τ (lamType τ0 τ1 eq dr) ξ = coerce eq (lamD (\d -> ?)) -- doesn't work either
  260. eval eZero τ (zeroType eq) ξ = coerce eq zeroD
  261. eval eSuc τ (sucType eq) ξ = coerce eq (lamD sucD)
  262. module Eval where
  263. open Prelude
  264. open Fin
  265. open Vec
  266. open Syntax
  267. open NormalForms
  268. open Rename using (up; rename)
  269. open Subst
  270. open TypeSystem
  271. eval : {n:Nat} -> (Γ:Context n) -> (e:Term n) -> (τ:Type) -> HasType Γ e τ -> Normal n
  272. eval Γ eZero τ _ = nZero
  273. eval Γ eSuc τ _ = nLam (nSuc (nVar fzero))
  274. eval Γ (eVar i) τ _ = nVar i
  275. eval Γ (eApp e1 e2) τ (appType σ d1 d2) = eval Γ e1 (σ => τ) d1 `app` eval Γ e2 σ d2
  276. eval Γ (eLam e) τ (lamType τ0 τ1 _ d) = nLam (eval (τ0 :: Γ) e τ1 d)
  277. open Prelude
  278. open Fin
  279. open Vec
  280. open Syntax