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.

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