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.

250 lines
7.0 KiB

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