less prototype, less bad code implementation of CCHM 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.

620 lines
22 KiB

  1. {-# LANGUAGE LambdaCase #-}
  2. {-# LANGUAGE BlockArguments #-}
  3. {-# LANGUAGE TupleSections #-}
  4. {-# LANGUAGE DeriveAnyClass #-}
  5. {-# LANGUAGE ScopedTypeVariables #-}
  6. {-# LANGUAGE DerivingStrategies #-}
  7. module Elab where
  8. import Control.Arrow (Arrow(first))
  9. import Control.Monad.Reader
  10. import Control.Exception
  11. import qualified Data.Map.Strict as Map
  12. import qualified Data.Sequence as Seq
  13. import qualified Data.Set as Set
  14. import qualified Data.Text as T
  15. import Data.Traversable
  16. import Data.Text (Text)
  17. import Data.Map (Map)
  18. import Data.Typeable
  19. import Data.Foldable
  20. import Elab.Eval.Formula (possible, truthAssignments)
  21. import Elab.WiredIn
  22. import Elab.Monad
  23. import Elab.Eval
  24. import qualified Presyntax.Presyntax as P
  25. import Prettyprinter
  26. import Syntax.Pretty
  27. import Syntax
  28. infer :: P.Expr -> ElabM (Term, NFType)
  29. infer (P.Span ex a b) = withSpan a b $ infer ex
  30. infer (P.Var t) = do
  31. name <- getNameFor t
  32. nft <- getNfType name
  33. pure (Ref name, nft)
  34. infer (P.App p f x) = do
  35. (f, f_ty) <- infer f
  36. porp <- isPiType p f_ty
  37. case porp of
  38. It'sProd d r w -> do
  39. x <- check x d
  40. x_nf <- eval x
  41. pure (App p (w f) x, r x_nf)
  42. It'sPath li le ri wp -> do
  43. x <- check x VI
  44. x_nf <- eval x
  45. pure (IElim (quote (fun li)) (quote le) (quote ri) (wp f) x, li x_nf)
  46. It'sPartial phi a w -> do
  47. x <- check x (VIsOne phi)
  48. pure (App P.Ex (w f) x, a)
  49. It'sPartialP phi a w -> do
  50. x <- check x (VIsOne phi)
  51. x_nf <- eval x
  52. pure (App P.Ex (w f) x, a @@ x_nf)
  53. infer (P.Proj1 x) = do
  54. (tm, ty) <- infer x
  55. (d, _, wp) <- isSigmaType ty
  56. pure (Proj1 (wp tm), d)
  57. infer (P.Proj2 x) = do
  58. (tm, ty) <- infer x
  59. tm_nf <- eval tm
  60. (_, r, wp) <- isSigmaType ty
  61. pure (Proj2 (wp tm), r (vProj1 tm_nf))
  62. infer exp = do
  63. t <- newMeta VType
  64. tm <- switch $ check exp t
  65. pure (tm, t)
  66. check :: P.Expr -> NFType -> ElabM Term
  67. check (P.Span ex a b) ty = withSpan a b $ check ex ty
  68. check (P.Lam p var body) (VPi p' dom (Closure _ rng)) | p == p' =
  69. assume (Bound var 0) dom $ \name ->
  70. Lam p name <$> check body (rng (VVar name))
  71. check tm (VPi P.Im dom (Closure var rng)) =
  72. assume var dom $ \name ->
  73. Lam P.Im name <$> check tm (rng (VVar name))
  74. check (P.Lam p v b) ty = do
  75. porp <- isPiType p =<< forceIO ty
  76. case porp of
  77. It'sProd d r wp ->
  78. assume (Bound v 0) d $ \name ->
  79. wp . Lam p name <$> check b (r (VVar name))
  80. It'sPath li le ri wp -> do
  81. tm <- assume (Bound v 0) VI $ \var ->
  82. Lam P.Ex var <$> check b (force (li (VVar var)))
  83. tm_nf <- eval tm
  84. unify (tm_nf @@ VI0) le
  85. `catchElab` (throwElab . WhenCheckingEndpoint le ri VI0)
  86. unify (tm_nf @@ VI1) ri
  87. `catchElab` (throwElab . WhenCheckingEndpoint le ri VI1)
  88. pure (wp (PathIntro (quote (fun li)) (quote le) (quote ri) tm))
  89. It'sPartial phi a wp ->
  90. assume (Bound v 0) (VIsOne phi) $ \var ->
  91. wp . Lam p var <$> check b a
  92. It'sPartialP phi a wp ->
  93. assume (Bound v 0) (VIsOne phi) $ \var ->
  94. wp . Lam p var <$> check b (a @@ VVar var)
  95. check (P.Pair a b) ty = do
  96. (d, r, wp) <- isSigmaType =<< forceIO ty
  97. a <- check a d
  98. a_nf <- eval a
  99. b <- check b (r a_nf)
  100. pure (wp (Pair a b))
  101. check (P.Pi p s d r) ty = do
  102. isSort ty
  103. d <- check d ty
  104. d_nf <- eval d
  105. assume (Bound s 0) d_nf \var -> do
  106. r <- check r ty
  107. pure (Pi p var d r)
  108. check (P.Sigma s d r) ty = do
  109. isSort ty
  110. d <- check d ty
  111. d_nf <- eval d
  112. assume (Bound s 0) d_nf \var -> do
  113. r <- check r ty
  114. pure (Sigma var d r)
  115. check (P.Let items body) ty = do
  116. checkLetItems mempty items \decs -> do
  117. body <- check body ty
  118. pure (Let decs body)
  119. check (P.LamSystem bs) ty = do
  120. (extent, dom) <- isPartialType ty
  121. let dom_q = quote dom
  122. eqns <- for (zip [(0 :: Int)..] bs) $ \(n, (formula, rhs)) -> do
  123. phi <- checkFormula (P.condF formula)
  124. rhses <-
  125. case P.condV formula of
  126. Just t -> assume (Bound t 0) (VIsOne phi) $ \var -> do
  127. env <- ask
  128. for (truthAssignments phi (getEnv env)) $ \e -> do
  129. let env' = env{ getEnv = e }
  130. (Just var,) <$> check rhs (eval' env' dom_q)
  131. Nothing -> do
  132. env <- ask
  133. for (truthAssignments phi (getEnv env)) $ \e -> do
  134. let env' = env{ getEnv = e }
  135. (Nothing,) <$> check rhs (eval' env' dom_q)
  136. pure (n, (phi, head rhses))
  137. unify extent (foldl ior VI0 (map (fst . snd) eqns))
  138. for_ eqns $ \(n, (formula, (binding, rhs))) -> do
  139. let
  140. k = case binding of
  141. Just v -> assume v (VIsOne formula) . const
  142. Nothing -> id
  143. k $ for_ eqns $ \(n', (formula', (binding, rhs'))) -> do
  144. let
  145. k = case binding of
  146. Just v -> assume v (VIsOne formula) . const
  147. Nothing -> id
  148. truth = possible mempty (iand formula formula')
  149. add [] = id
  150. add ((~(HVar x), True):xs) = redefine x VI VI1 . add xs
  151. add ((~(HVar x), False):xs) = redefine x VI VI0 . add xs
  152. k $ when ((n /= n') && fst truth) . add (Map.toList (snd truth)) $ do
  153. vl <- eval rhs
  154. vl' <- eval rhs'
  155. unify vl vl'
  156. `withNote` vsep [ pretty "These two cases must agree because they are both possible:"
  157. , indent 2 $ pretty '*' <+> prettyTm (quote formula) <+> operator (pretty "=>") <+> prettyTm rhs
  158. , indent 2 $ pretty '*' <+> prettyTm (quote formula') <+> operator (pretty "=>") <+> prettyTm rhs'
  159. ]
  160. `withNote` (pretty "Consider this face, where both are true:" <+> showFace (snd truth))
  161. name <- newName
  162. let
  163. mkB name (Just v, b) = App P.Ex (Lam P.Ex v b) (Ref name)
  164. mkB _ (Nothing, b) = b
  165. pure (Lam P.Ex name (System (Map.fromList (map (\(_, (x, y)) -> (quote x, mkB name y)) eqns))))
  166. check (P.LamCase pats) ty =
  167. do
  168. porp <- isPiType P.Ex ty
  169. case porp of
  170. It'sProd dom rng wp -> do
  171. name <- newName
  172. let range = Lam P.Ex name (quote (rng (VVar name)))
  173. cases <- checkPatterns range [] pats \partialPats (pat, rhs) -> do
  174. checkPattern pat dom \pat wp boundary pat_nf -> do
  175. rhs <- check rhs (rng pat_nf)
  176. case boundary of
  177. -- If we're checking a higher constructor then we need to
  178. -- compute what the case expression computed so far does
  179. -- with all the faces
  180. -- and make sure that the current case agrees with that
  181. -- boundary
  182. Just boundary -> do
  183. rhs_nf <- eval (wp rhs)
  184. cases <- partialPats
  185. let
  186. (ty, a, b) = case pat_nf of
  187. VNe (HCon ty (ConName _ _ a b)) _ -> (ty, a, b)
  188. VNe (HPCon _ ty (ConName _ _ a b)) _ -> (ty, a, b)
  189. _ -> undefined
  190. dummies <- replicateM ((a + b) - length (getBoundaryNames boundary)) newName
  191. let
  192. base = appDummies (VVar <$> dummies) ty rhs_nf
  193. sys = boundaryFormulas (drop a dummies ++ getBoundaryNames boundary) (getBoundaryMap boundary)
  194. for_ (Map.toList sys) \(formula, side) -> do
  195. let rhs = cases @@ side
  196. for_ (truthAssignments formula mempty) $ \i -> do
  197. let vl = foldl (\v n -> vApp P.Ex v (snd (i Map.! n))) base (getBoundaryNames boundary)
  198. unify vl rhs
  199. `withNote` vcat [ pretty "These must be the same because of the face"
  200. , indent 2 $ prettyTm (quote formula) <+> operator (pretty "=>") <+> prettyTm (quote (zonk side))
  201. ]
  202. `withNote` (pretty "Mandated by the constructor" <+> prettyTm (quote pat_nf))
  203. _ -> pure ()
  204. pure (pat, wp rhs)
  205. let x = wp (Lam P.Ex name (Case range (Ref name) cases))
  206. pure x
  207. _ -> do
  208. dom <- newMeta VTypeω
  209. n <- newName' (Bound (T.singleton 'x') 0)
  210. assume n dom \_ -> do
  211. rng <- newMeta VTypeω
  212. throwElab $ NotEqual (VPi P.Ex dom (Closure n (const rng))) ty
  213. where
  214. checkPatterns _ acc [] _ = pure (reverse acc)
  215. checkPatterns rng acc (x:xs) k = do
  216. n <- newName
  217. (p, t) <- k (eval (Lam P.Ex n (Case rng (Ref n) acc))) x
  218. checkPatterns rng ((p, t):acc) xs k
  219. appDummies (v:vl) (VPi p _ (Closure _ r)) x = appDummies vl (r v) (vApp p x v)
  220. appDummies [] _ x = x
  221. appDummies vs t _ = error (show (vs, t))
  222. boundaryFormulas [] (VSystem fs) = fs
  223. boundaryFormulas (x:xs) k = boundaryFormulas xs $ k @@ VVar x
  224. boundaryFormulas a b = error (show (a, b))
  225. check exp ty = do
  226. (tm, has) <- switch $ infer exp
  227. wp <- isConvertibleTo has ty
  228. pure (wp tm)
  229. checkPattern :: P.Pattern -> NFSort -> (Term -> (Term -> Term) -> Maybe Boundary -> Value -> ElabM a) -> ElabM a
  230. checkPattern (P.PCap var) dom cont = do
  231. name <- asks (Map.lookup var . nameMap)
  232. case name of
  233. Just name@(ConName _ _ skip arity) -> do
  234. unless (arity == 0) $ throwElab $ UnsaturatedCon name
  235. (ty, wp, _) <- instantiate =<< getNfType name
  236. unify ty dom
  237. wrap <- skipLams skip
  238. cont (Con name) wrap Nothing =<< eval (wp (Con name))
  239. Just name -> throwElab $ NotACon name
  240. Nothing -> assume (Bound var 0) dom \name -> cont (Ref name) (Lam P.Ex name) Nothing (VVar name)
  241. checkPattern (P.PCon var args) dom cont =
  242. do
  243. name <- asks (Map.lookup var . nameMap)
  244. case name of
  245. Just name@(ConName _ _ nskip arity) -> do
  246. unless (arity == length args) $ throwElab $ UnsaturatedCon name
  247. (ty, wp, xs) <- instantiate =<< getNfType name
  248. _ <- isConvertibleTo (skipBinders arity ty) dom
  249. skip <- skipLams nskip
  250. t <- asks (Map.lookup name . boundaries)
  251. con <- quote <$> getValue name
  252. bindNames args ty $ \names wrap ->
  253. cont (Con name) (skip . wrap) (instBoundary xs <$> t) =<< eval (foldl (\x y -> App P.Ex x (Ref y)) (wp con) names)
  254. Just name -> throwElab $ NotACon name
  255. _ -> throwElab $ NotInScope (Bound var 0)
  256. where
  257. skipBinders :: Int -> NFType -> NFType
  258. skipBinders 0 t = t
  259. skipBinders n (VPi _ _ (Closure v r)) = skipBinders (n - 1) (r (VVar v))
  260. skipBinders _ _ = error $ "constructor type is wrong?"
  261. bindNames (n:ns) (VPi p d (Closure _ r)) k =
  262. assume (Bound n 0) d \n -> bindNames ns (r (VVar n)) \ns w ->
  263. k (n:ns) (Lam p n . w)
  264. bindNames [] _ k = k [] id
  265. bindNames xs t _ = error $ show (xs, t)
  266. instBoundary :: [Value] -> Boundary -> Boundary
  267. instBoundary metas (Boundary x y) = Boundary x (foldl (vApp P.Ex) y metas)
  268. instantiate :: NFType -> ElabM (NFType, Term -> Term, [Value])
  269. instantiate (VPi P.Im d (Closure _ k)) = do
  270. t <- newMeta d
  271. (ty, w, xs) <- instantiate (k t)
  272. pure (ty, \inner -> w (App P.Im inner (quote t)), t:xs)
  273. instantiate x = pure (x, id, [])
  274. skipLams :: Int -> ElabM (Term -> Term)
  275. skipLams 0 = pure id
  276. skipLams k = do
  277. n <- newName
  278. (Lam P.Im n . ) <$> skipLams (k - 1)
  279. checkLetItems :: Map Text (Maybe NFType) -> [P.LetItem] -> ([(Name, Term, Term)] -> ElabM a) -> ElabM a
  280. checkLetItems _ [] cont = cont []
  281. checkLetItems map (P.LetDecl v t:xs) cont = do
  282. t <- check t VTypeω
  283. t_nf <- eval t
  284. assume (Defined v 0) t_nf \_ ->
  285. checkLetItems (Map.insert v (Just t_nf) map) xs cont
  286. checkLetItems map (P.LetBind name rhs:xs) cont = do
  287. case Map.lookup name map of
  288. Nothing -> do
  289. (tm, ty) <- infer rhs
  290. tm_nf <- eval tm
  291. makeLetDef (Defined name 0) ty tm_nf \name' ->
  292. checkLetItems map xs \xs ->
  293. cont ((name', quote ty, tm):xs)
  294. Just Nothing -> throwElab $ Redefinition (Defined name 0)
  295. Just (Just ty_nf) -> do
  296. rhs <- check rhs ty_nf
  297. rhs_nf <- eval rhs
  298. makeLetDef (Defined name 0) ty_nf rhs_nf \name' ->
  299. checkLetItems (Map.insert name Nothing map) xs \xs ->
  300. cont ((name', quote ty_nf, rhs):xs)
  301. checkFormula :: P.Formula -> ElabM Value
  302. checkFormula P.FTop = pure VI1
  303. checkFormula P.FBot = pure VI0
  304. checkFormula (P.FAnd x y) = iand <$> checkFormula x <*> checkFormula y
  305. checkFormula (P.FOr x y) = ior <$> checkFormula x <*> checkFormula y
  306. checkFormula (P.FIs0 x) = do
  307. nm <- getNameFor x
  308. ty <- getNfType nm
  309. unify ty VI
  310. pure (inot (VVar nm))
  311. checkFormula (P.FIs1 x) = do
  312. nm <- getNameFor x
  313. ty <- getNfType nm
  314. unify ty VI
  315. pure (VVar nm)
  316. isSort :: NFType -> ElabM ()
  317. isSort t = isSort (force t) where
  318. isSort VType = pure ()
  319. isSort VTypeω = pure ()
  320. isSort vt@(VNe HMeta{} _) = unify vt VType
  321. isSort vt = throwElab $ NotEqual vt VType
  322. data ProdOrPath
  323. = It'sProd { prodDmn :: NFType
  324. , prodRng :: NFType -> NFType
  325. , prodWrap :: Term -> Term
  326. }
  327. | It'sPath { pathLine :: NFType -> NFType
  328. , pathLeft :: Value
  329. , pathRight :: Value
  330. , pathWrap :: Term -> Term
  331. }
  332. | It'sPartial { partialExtent :: NFEndp
  333. , partialDomain :: Value
  334. , partialWrap :: Term -> Term
  335. }
  336. | It'sPartialP { partialExtent :: NFEndp
  337. , partialDomain :: Value
  338. , partialWrap :: Term -> Term
  339. }
  340. isPiType :: P.Plicity -> NFType -> ElabM ProdOrPath
  341. isPiType p x = isPiType p (force x) where
  342. isPiType p (VPi p' d (Closure _ k)) | p == p' = pure (It'sProd d k id)
  343. isPiType P.Ex (VPath li le ri) = pure (It'sPath (li @@) le ri id)
  344. isPiType P.Ex (VPartial phi a) = pure (It'sPartial phi a id)
  345. isPiType P.Ex (VPartialP phi a) = pure (It'sPartialP phi a id)
  346. isPiType P.Ex (VPi P.Im d (Closure _ k)) = do
  347. meta <- newMeta d
  348. porp <- isPiType P.Ex (k meta)
  349. pure $ case porp of
  350. It'sProd d r w -> It'sProd d r (\f -> w (App P.Im f (quote meta)))
  351. It'sPath l x y w -> It'sPath l x y (\f -> w (App P.Im f (quote meta)))
  352. It'sPartial phi a w -> It'sPartial phi a (\f -> w (App P.Im f (quote meta)))
  353. It'sPartialP phi a w -> It'sPartialP phi a (\f -> w (App P.Im f (quote meta)))
  354. isPiType p t = do
  355. dom <- newMeta VType
  356. name <- newName
  357. assume name dom $ \name -> do
  358. rng <- newMeta VType
  359. wp <- isConvertibleTo t (VPi p dom (Closure name (const rng)))
  360. pure (It'sProd dom (const rng) wp)
  361. isSigmaType :: NFType -> ElabM (Value, NFType -> NFType, Term -> Term)
  362. isSigmaType t = isSigmaType (force t) where
  363. isSigmaType (VSigma d (Closure _ k)) = pure (d, k, id)
  364. isSigmaType t = do
  365. dom <- newMeta VType
  366. name <- newName
  367. assume name dom $ \name -> do
  368. rng <- newMeta VType
  369. wp <- isConvertibleTo t (VSigma dom (Closure name (const rng)))
  370. pure (dom, const rng, wp)
  371. isPartialType :: NFType -> ElabM (NFEndp, Value)
  372. isPartialType t = isPartialType (force t) where
  373. isPartialType (VPartial phi a) = pure (phi, a)
  374. isPartialType (VPartialP phi a) = pure (phi, a)
  375. isPartialType t = do
  376. phi <- newMeta VI
  377. dom <- newMeta (VPartial phi VType)
  378. unify t (VPartial phi dom)
  379. pure (phi, dom)
  380. checkStatement :: P.Statement -> ElabM a -> ElabM a
  381. checkStatement (P.SpanSt s a b) k = withSpan a b $ checkStatement s k
  382. checkStatement (P.Decl name ty) k = do
  383. ty <- check ty VTypeω
  384. ty_nf <- eval ty
  385. assumes (flip Defined 0 <$> name) ty_nf (const k)
  386. checkStatement (P.Postulate []) k = k
  387. checkStatement (P.Postulate ((name, ty):xs)) k = do
  388. ty <- check ty VTypeω
  389. ty_nf <- eval ty
  390. assume (Defined name 0) ty_nf \name ->
  391. local (\e -> e { definedNames = Set.insert name (definedNames e) }) (checkStatement (P.Postulate xs) k)
  392. checkStatement (P.Defn name rhs) k = do
  393. ty <- asks (Map.lookup name . nameMap)
  394. case ty of
  395. Nothing -> do
  396. (tm, ty) <- infer rhs
  397. tm_nf <- eval tm
  398. makeLetDef (Defined name 0) ty tm_nf (const k)
  399. Just nm -> do
  400. ty_nf <- getNfType nm
  401. t <- asks (Set.member nm . definedNames)
  402. when t $ throwElab (Redefinition (Defined name 0))
  403. rhs <- check rhs ty_nf
  404. rhs_nf <- evalFix (Defined name 0) ty_nf rhs
  405. makeLetDef (Defined name 0) ty_nf rhs_nf $ \name ->
  406. local (\e -> e { definedNames = Set.insert name (definedNames e) }) k
  407. checkStatement (P.Builtin winame var) k = do
  408. wi <-
  409. case Map.lookup winame wiredInNames of
  410. Just wi -> pure wi
  411. _ -> throwElab $ NoSuchPrimitive winame
  412. let
  413. check = do
  414. nm <- getNameFor var
  415. ty <- getNfType nm
  416. unify ty (wiType wi)
  417. `withNote` hsep [ pretty "Previous definition of", pretty nm, pretty "here" ]
  418. `seeAlso` nm
  419. env <- ask
  420. liftIO $
  421. runElab check env `catch` \(_ :: NotInScope) -> pure ()
  422. define (Defined var 0) (wiType wi) (wiValue wi) $ \name ->
  423. local (\e -> e { definedNames = Set.insert name (definedNames e) }) k
  424. checkStatement (P.ReplNf e) k = do
  425. (e, _) <- infer e
  426. e_nf <- eval e
  427. h <- asks commHook
  428. liftIO (h e_nf)
  429. k
  430. checkStatement (P.ReplTy e) k = do
  431. (_, ty) <- infer e
  432. h <- asks commHook
  433. liftIO (h ty)
  434. k
  435. checkStatement (P.Data name tele retk constrs) k =
  436. do
  437. checkTeleRetk True tele retk \kind tele undef -> do
  438. kind_nf <- eval kind
  439. defineInternal (Defined name 0) kind_nf (\name' -> VNe (mkHead name') mempty) \name' ->
  440. checkCons tele (VNe (mkHead name') (Seq.fromList (map makeProj tele))) constrs (local (markAsDef name' . undef) k)
  441. where
  442. makeProj (x, p, _) = PApp p (VVar x)
  443. markAsDef x e = e { definedNames = Set.insert x (definedNames e) }
  444. mkHead name
  445. | any (\case { (_, _, P.Path{}) -> True; _ -> False}) constrs = HData True name
  446. | otherwise = HData False name
  447. checkTeleRetk allKan [] retk cont = do
  448. t <- check retk VTypeω
  449. t_nf <- eval t
  450. when allKan $ unify t_nf VType
  451. cont t [] id
  452. checkTeleRetk allKan ((x, p, t):xs) retk cont = do
  453. (t, ty) <- infer t
  454. _ <- isConvertibleTo ty VTypeω
  455. let
  456. allKan' = case ty of
  457. VType -> allKan
  458. _ -> False
  459. t_nf <- eval t
  460. let rm nm e = e{ nameMap = Map.delete (getNameText nm) (nameMap e), getEnv = Map.delete nm (getEnv e) }
  461. assume (Bound x 0) t_nf $ \nm -> checkTeleRetk allKan' xs retk \k xs w -> cont (Pi p nm t k) ((nm, p, t_nf):xs) (rm nm . w)
  462. checkCons _ _et [] k = k
  463. checkCons n ret ((s, e, P.Point x ty):xs) k = withSpan s e $ do
  464. t <- check ty VTypeω
  465. ty_nf <- eval t
  466. let
  467. (args, ret') = splitPi ty_nf
  468. closed = close n t
  469. n' = map (\(x, _, y) -> (x, P.Im, y)) n
  470. unify ret' ret
  471. closed_nf <- eval closed
  472. defineInternal (ConName x 0 (length n') (length args)) closed_nf (makeCon closed_nf mempty n' args) \_ -> checkCons n ret xs k
  473. checkCons n ret ((s, e, P.Path name indices return faces):xs) k = withSpan s e $ do
  474. (con, closed_nf, value, boundary) <- assumes (flip Bound 0 <$> indices) VI \indices -> do
  475. t <- check return VTypeω
  476. ty_nf <- eval t
  477. let
  478. (args, ret') = splitPi ty_nf
  479. closed = close n (addArgs args (addInterval indices (quote ret')))
  480. n' = map (\(x, _, y) -> (x, P.Im, y)) n
  481. addArgs = flip $ foldr (\(x, p, t) -> Pi p x (quote t))
  482. addInterval = flip $ foldr (\n -> Pi P.Ex n I)
  483. envArgs ((x, _, y):xs) = assume x y . const . envArgs xs
  484. envArgs [] = id
  485. closed_nf <- eval closed
  486. unify ret' ret
  487. faces <- envArgs args $ for faces \(f, t) -> do
  488. phi <- checkFormula f
  489. t <- check t ret
  490. pure (phi, (quote phi, t))
  491. system <- eval $ foldr (\x -> Lam P.Ex x) (System (Map.fromList (map snd faces))) (map (\(x, _, _) -> x) n' ++ map (\(x, _, _) -> x) args ++ indices)
  492. unify (foldr ior VI0 (map fst faces)) (totalProp indices)
  493. `withNote` pretty "The formula determining the endpoints of a higher constructor must be a classical tautology"
  494. pure (ConName name 0 (length n') (length args + length indices), closed_nf, makePCon closed_nf mempty n' args indices system, Boundary indices system)
  495. defineInternal con closed_nf value \name -> addBoundary name boundary $ checkCons n ret xs k
  496. close [] t = t
  497. close ((x, _, y):xs) t = Pi P.Im x (quote y) (close xs t)
  498. splitPi (VPi p y (Closure x k)) = first ((x, p, y):) $ splitPi (k (VVar x))
  499. splitPi t = ([], t)
  500. makeCon cty sp [] [] con = VNe (HCon cty con) sp
  501. makeCon cty sp ((nm, p, _):xs) ys con = VLam p $ Closure nm \a -> makeCon cty (sp Seq.:|> PApp p a) xs ys con
  502. makeCon cty sp [] ((nm, p, _):ys) con = VLam p $ Closure nm \a -> makeCon cty (sp Seq.:|> PApp p a) [] ys con
  503. makePCon cty sp [] [] [] sys con = VNe (HPCon sys cty con) sp
  504. makePCon cty sp ((nm, p, _):xs) ys zs sys con = VLam p $ Closure nm \a -> makePCon cty (sp Seq.:|> PApp p a) xs ys zs (sys @@ a) con
  505. makePCon cty sp [] ((nm, p, _):ys) zs sys con = VLam p $ Closure nm \a -> makePCon cty (sp Seq.:|> PApp p a) [] ys zs (sys @@ a) con
  506. makePCon cty sp [] [] (nm:zs) sys con = VLam P.Ex $ Closure nm \a -> makePCon cty (sp Seq.:|> PApp P.Ex a) [] [] zs (sys @@ a) con
  507. totalProp (x:xs) = ior (inot (VVar x)) (VVar x) `ior` totalProp xs
  508. totalProp [] = VI0
  509. evalFix :: Name -> NFType -> Term -> ElabM Value
  510. evalFix name nft term = do
  511. env <- ask
  512. pure . fix $ \val -> eval' env{ getEnv = Map.insert name (nft, val) (getEnv env) } term
  513. checkProgram :: [P.Statement] -> ElabM a -> ElabM a
  514. checkProgram [] k = k
  515. checkProgram (st:sts) k = checkStatement st $ checkProgram sts k
  516. newtype Redefinition = Redefinition { getRedefName :: Name }
  517. deriving (Show, Typeable, Exception)
  518. data WhenCheckingEndpoint = WhenCheckingEndpoint { leftEndp :: Value, rightEndp :: Value, whichIsWrong :: NFEndp, exc :: SomeException }
  519. deriving (Show, Typeable, Exception)
  520. data UnsaturatedCon = UnsaturatedCon { theConstr :: Name }
  521. deriving (Show, Typeable)
  522. deriving anyclass (Exception)
  523. data NotACon = NotACon { theNotConstr :: Name }
  524. deriving (Show, Typeable)
  525. deriving anyclass (Exception)