Prototype, extremely bad code implementation of CCHM Cubical Type Theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
9.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. {-# LANGUAGE ViewPatterns #-}
  2. {-# LANGUAGE LambdaCase #-}
  3. {-# LANGUAGE PatternSynonyms #-}
  4. module Syntax where
  5. import qualified Data.Map.Strict as Map
  6. import qualified Data.Set as Set
  7. import Data.Map.Strict (Map)
  8. import Data.Set (Set)
  9. import Presyntax (Formula)
  10. import Systems
  11. import Text.Show (showListWith)
  12. type Space = Term
  13. data Term
  14. = Var String
  15. | App Term Term
  16. | Lam String Term Term
  17. | Let String Term Term Term
  18. | Pi String Term Term
  19. | Type | Typeω
  20. | I
  21. | I0 | I1
  22. | IAnd Term Term
  23. | IOr Term Term
  24. | INot Term
  25. | Path Space Term Term
  26. | PathI Space Term Term String Term
  27. | PathP Space Term Term Term Term
  28. | Sigma String Term Term
  29. | Pair Term Term
  30. | Proj1 Term
  31. | Proj2 Term
  32. | System (System Term)
  33. | Partial Term Term
  34. | PartialP Term Term
  35. | Comp Term Term Term Term
  36. | Sub Term Term Term
  37. | InclSub Term Term Term Term
  38. | GlueTy
  39. Term -- Base type
  40. Term -- Extent
  41. Term -- Partial types
  42. Term -- Partial equivs
  43. | Glue
  44. Term -- Base
  45. Term -- Extent
  46. Term -- Partial types
  47. Term -- Partial equivs
  48. Term -- t
  49. Term -- a
  50. | Unglue
  51. Term -- Base
  52. Term -- Extent
  53. Term -- Partial types
  54. Term -- Partial equivs
  55. Term -- glued value
  56. | Bool
  57. | Tt | Ff
  58. | If Term Term Term Term
  59. deriving (Eq, Ord)
  60. instance Show Term where
  61. showsPrec p =
  62. \case
  63. Var s -> showString s
  64. System fs -> showListWith showPE (Map.toList (getSystem fs))
  65. -- ew
  66. App (App (App (Lam _ _ (Lam _ _ (Lam _ _ Path{}))) a) x) y -> showsPrec p (Path a x y)
  67. App (App (App (Lam _ _ (Lam _ _ (Lam _ _ Sub{}))) a) x) y -> showsPrec p (Sub a x y)
  68. App (App (Lam _ _ (Lam _ _ Partial{})) phi) r -> showsPrec p (Partial phi r)
  69. App (App (Lam _ _ (Lam _ _ PartialP{})) phi) r -> showsPrec p (PartialP phi r)
  70. App (App (App (App (Lam _ _ (Lam _ _ (Lam _ _ (Lam _ _ Comp{})))) a) phi) u) a0 ->
  71. showsPrec p (Comp a phi u a0)
  72. App (App (App (App (Lam _ _ (Lam _ _ (Lam _ _ (Lam _ _ GlueTy{})))) a) phi) u) a0 ->
  73. showsPrec p (GlueTy a phi u a0)
  74. App f x -> showParen (p >= app_prec) $
  75. showsPrec fun_prec f
  76. . showChar ' '
  77. . showsPrec app_prec x
  78. Lam s t b ->
  79. let
  80. getLams (Lam s _ b) =
  81. let (as, b') = getLams b
  82. in (s:as, b')
  83. getLams (PathI _a _x _y s b) =
  84. let (as, b') = getLams b
  85. in (("(" ++ s ++ " : I)"):as, b')
  86. getLams t = ([], t)
  87. (args, bd) = getLams (Lam s t b)
  88. in showParen (p >= fun_prec) $
  89. showString ("λ " ++ unwords args ++ " -> ")
  90. . shows bd
  91. Let s t d b -> showParen (p > fun_prec) $
  92. showString "let\n "
  93. . showString s
  94. . showString " : "
  95. . shows t
  96. . showString " = "
  97. . shows d
  98. . showString " in "
  99. . shows b
  100. Pi "_" d r ->
  101. showParen (p >= domain_prec) $
  102. showsPrec domain_prec d
  103. . showString " -> "
  104. . shows r
  105. Pi v d r -> showParen (p >= domain_prec) $
  106. let
  107. showBinder (Pi "_" d r) =
  108. showsPrec domain_prec d
  109. . showString " -> "
  110. . shows r
  111. showBinder (Pi n d r) =
  112. let
  113. arr = case r of
  114. Pi n _ _ | n /= "_" -> " "
  115. _ -> " -> "
  116. in
  117. showParen True (showString n . showString " : " . shows d)
  118. . showString arr
  119. . showBinder r
  120. showBinder t = shows t
  121. in showBinder (Pi v d r)
  122. Type -> showString "Type"
  123. Typeω -> showString "Typeω"
  124. I -> showChar 'I'
  125. I0 -> showString "i0"
  126. I1 -> showString "i1"
  127. IAnd i j -> showParen (p >= and_prec) $
  128. showsPrec or_prec i
  129. . showString " && "
  130. . showsPrec or_prec j
  131. IOr i j -> showParen (p >= or_prec) $
  132. showsPrec app_prec i
  133. . showString " || "
  134. . showsPrec app_prec j
  135. INot s -> showChar '~' . showsPrec p s
  136. Path a x y -> showApps p (Var "Path") [a, x, y]
  137. Sub a x y -> showApps p (Var "Sub") [a, x, y]
  138. InclSub _a _phi _u0 a0 -> showsPrec p a0
  139. GlueTy a phi t e -> showApps p (Var "Glue") [a, phi, t, e]
  140. Glue base phi ty te t a ->
  141. showApps p (Var "glue") [phi, t, a]
  142. Unglue base phi ty te a ->
  143. showApps p (Var "unglue") [a]
  144. Partial r a -> showApps p (Var "Partial") [r, a]
  145. PartialP r a -> showApps p (Var "PartialP") [r, a]
  146. Comp a phi u a0 -> showApps p (Var "comp") [a, phi, u, a0]
  147. If _ Ff Tt d -> showApps p (Var "not") [d]
  148. If _ b c d -> showApps p (Var "if") [b, c, d]
  149. Bool -> showString "Bool"
  150. Tt -> showString "tt"
  151. Ff -> showString "ff"
  152. PathI a x y s b -> showParen (p >= fun_prec) $
  153. showString ("λ " ++ s ++ " -> ")
  154. . shows b
  155. PathP _a _x _y f i -> showsPrec p (App f i)
  156. Pair a b -> showParen True $
  157. shows a
  158. . showString ", "
  159. . shows b
  160. Proj1 b -> showsPrec p b . showString ".1"
  161. Proj2 b -> showsPrec p b . showString ".1"
  162. Sigma v d r ->
  163. showParen (p >= app_prec) $
  164. showParen True (showString v . showString " : " . shows d)
  165. . showString " × "
  166. . shows r
  167. where
  168. app_prec = 6
  169. domain_prec = 5
  170. and_prec = 4
  171. or_prec = 3
  172. fun_prec = 1
  173. showApps :: Int -> Term -> [Term] -> ShowS
  174. showApps p f xs = showsPrec p (foldl App f xs)
  175. showPE :: (Formula, Term) -> String -> String
  176. showPE (f, t) = shows f . showString " -> " . shows t
  177. data Value
  178. = VNe String [Proj]
  179. | VLam String Value (Value -> Value)
  180. | VPi String Value (Value -> Value)
  181. | VType | VTypeω
  182. | VI | VI0 | VI1
  183. | VEqGlued Value Value -- e which is def. eq. to e'
  184. | VPair Value Value
  185. | VSigma String Value (Value -> Value)
  186. | VLine Value Value Value (Value -> Value)
  187. -- (λ i → ...) : Path A x y
  188. -- order: A x y k
  189. | VSystem (System Value)
  190. | VOfSub Value Value Value Value
  191. | VIAnd Value Value
  192. | VIOr Value Value
  193. | VINot Value
  194. | VPath Value Value Value
  195. | VSub Value Value Value
  196. | VPartial Value Value
  197. | VPartialP Value Value
  198. | VComp Value Value Value Value
  199. | VGlue Value Value Value Value
  200. | VGlueIntro Value Value Value Value Value Value
  201. | VGlueElim Value Value Value Value Value
  202. | VBool
  203. | VTrue
  204. | VFalse
  205. | VIf Value Value Value Value
  206. data Proj
  207. = PApp Value
  208. | PPathP Value Value Value Value
  209. -- a x y i
  210. | PProj1
  211. | PProj2
  212. pattern VVar :: String -> Value
  213. pattern VVar x = VNe x []
  214. quote :: Value -> Term
  215. quote = go mempty where
  216. go :: Set String -> Value -> Term
  217. go scope (VNe hd spine) = foldl (goSpine scope) (Var hd) (reverse spine)
  218. go scope (VLam s a k) =
  219. let n = rename s scope
  220. in Lam n (go scope a) (go (Set.insert n scope) (k (VVar n)))
  221. go scope (VPi s d r) =
  222. let n = rename s scope
  223. in Pi n (go scope d) (go (Set.insert n scope) (r (VVar n)))
  224. go scope (VSigma s d r) =
  225. let n = rename s scope
  226. in Sigma n (go scope d) (go (Set.insert n scope) (r (VVar n)))
  227. go _ VType = Type
  228. go _ VI0 = I0
  229. go _ VI1 = I1
  230. go _ VI = I
  231. go _ VTypeω = Typeω
  232. go scope (VIAnd x y) = IAnd (go scope x) (go scope y)
  233. go scope (VIOr x y) = IOr (go scope x) (go scope y)
  234. go scope (VINot x) = INot (go scope x)
  235. go scope (VPath a x y) = Path (go scope a) (go scope x) (go scope y)
  236. go scope (VSub a x y) = Sub (go scope a) (go scope x) (go scope y)
  237. go scope (VPartial r a) = Partial (go scope r) (go scope a)
  238. go scope (VPartialP r a) = PartialP (go scope r) (go scope a)
  239. go scope (VComp a b c d) = Comp (go scope a) (go scope b) (go scope c) (go scope d)
  240. go scope (VEqGlued e _) = go scope e
  241. go scope (VPair a b) = Pair (go scope a) (go scope b)
  242. go scope (VLine a x y k) =
  243. let n = rename "i" scope
  244. in PathI (go scope a) (go scope x) (go scope y) n (go (Set.insert n scope) (k (VVar n)))
  245. go scope (VSystem (FMap fs)) = System (FMap (fmap (go scope) fs))
  246. go scope (VOfSub _ _ _ x) = go scope x
  247. go scope (VGlue a phi tys eqvs) = GlueTy (go scope a) (go scope phi) (go scope tys) (go scope eqvs)
  248. go scope (VGlueIntro base phi tys eqvs t a) =
  249. Glue (go scope base) (go scope phi) (go scope tys) (go scope eqvs) (go scope t) (go scope a)
  250. go scope (VGlueElim base phi tys eqvs a) =
  251. Unglue (go scope base) (go scope phi) (go scope tys) (go scope eqvs) (go scope a)
  252. go scope VBool = Bool
  253. go scope VTrue = Tt
  254. go scope VFalse = Ff
  255. go scope (VIf a b c d) = If (go scope a) (go scope b) (go scope c) (go scope d)
  256. goSpine :: Set String -> Term -> Proj -> Term
  257. goSpine scope t (PApp x) = App t (go scope x)
  258. goSpine scope t (PPathP a x y i) = PathP (go scope a) (go scope x) (go scope y) t (go scope i)
  259. goSpine scope t PProj1 = Proj1 t
  260. goSpine scope t PProj2 = Proj2 t
  261. rename :: String -> Set String -> String
  262. rename x s
  263. | x == "_" = x
  264. | x `Set.member` s = rename (x ++ "'") s
  265. | otherwise = x
  266. instance Show Value where
  267. showsPrec p = showsPrec p . quote
  268. data Env =
  269. Env { names :: Map String (Value, Value)
  270. }
  271. deriving (Show)