|
|
@ -37,12 +37,11 @@ import System.IO.Unsafe |
|
|
|
|
|
|
|
import {-# SOURCE #-} Elab.WiredIn |
|
|
|
import Data.List (sortOn) |
|
|
|
import Syntax.Subst |
|
|
|
import Data.Map.Strict (Map) |
|
|
|
|
|
|
|
eval :: Term -> ElabM Value |
|
|
|
eval t = asks (flip eval' t) |
|
|
|
|
|
|
|
|
|
|
|
-- everywhere force |
|
|
|
zonkIO :: Value -> IO Value |
|
|
|
zonkIO (VNe hd sp) = do |
|
|
@ -93,7 +92,9 @@ zonkIO (VHComp a b c d) = hComp <$> zonkIO a <*> zonkIO b <*> zonkIO c <*> zonkI |
|
|
|
zonkIO (VGlueTy a phi ty e) = glueType <$> zonkIO a <*> zonkIO phi <*> zonkIO ty <*> zonkIO e |
|
|
|
zonkIO (VGlue a phi ty e t x) = glueElem <$> zonkIO a <*> zonkIO phi <*> zonkIO ty <*> zonkIO e <*> zonkIO t <*> zonkIO x |
|
|
|
zonkIO (VUnglue a phi ty e x) = unglue <$> zonkIO a <*> zonkIO phi <*> zonkIO ty <*> zonkIO e <*> zonkIO x |
|
|
|
zonkIO (VCase t x xs) = VCase <$> zonkIO t <*> zonkIO x <*> pure xs |
|
|
|
zonkIO (VCase env t x xs) = do |
|
|
|
env' <- emptyEnv |
|
|
|
evalCase env'{getEnv = env} . (@@) <$> zonkIO t <*> zonkIO x <*> pure xs |
|
|
|
|
|
|
|
zonkSp :: Projection -> IO Projection |
|
|
|
zonkSp (PApp p x) = PApp p <$> zonkIO x |
|
|
@ -184,7 +185,9 @@ eval' e (Case range sc xs) = evalCase e (eval' e range @@) (force (eval' e sc)) |
|
|
|
evalCase :: ElabEnv -> (Value -> Value) -> Value -> [(Term, Term)] -> Value |
|
|
|
evalCase _ _ sc [] = error $ "unmatched pattern for value: " ++ show (prettyTm (quote sc)) |
|
|
|
|
|
|
|
evalCase env rng (VHComp a phi u a0) cases = |
|
|
|
evalCase env rng (VSystem fs) cases = VSystem (fmap (flip (evalCase env rng) cases) fs) |
|
|
|
|
|
|
|
evalCase env rng (VHComp a phi u a0) cases = |
|
|
|
comp (fun \i -> rng (v i)) phi (system \i is1 -> evalCase env rng (u @@ i @@ is1) cases) |
|
|
|
(VInc (rng a) phi (evalCase env rng (outS a0 phi (u @@ VI0) a0) cases)) |
|
|
|
where |
|
|
@ -200,7 +203,7 @@ evalCase env rng (val@(VNe (HPCon _ _ x) sp)) ((Con x', k):xs) |
|
|
|
| x == x' = foldl applProj (eval' env k) sp |
|
|
|
| otherwise = evalCase env rng val xs |
|
|
|
|
|
|
|
evalCase _ rng sc xs = VCase (fun rng) sc xs |
|
|
|
evalCase env rng sc xs = VCase (getEnv env) (fun rng) sc xs |
|
|
|
|
|
|
|
|
|
|
|
data NotEqual = NotEqual Value Value |
|
|
@ -287,7 +290,7 @@ unify' topa topb = join $ go <$> forceIO topa <*> forceIO topb where |
|
|
|
go (VSystem sys) rhs = goSystem unify' sys rhs |
|
|
|
go rhs (VSystem sys) = goSystem (flip unify') sys rhs |
|
|
|
|
|
|
|
go (VCase _ a b) (VCase _ a' b') = do |
|
|
|
go (VCase _ _ a b) (VCase _ _ a' b') = do |
|
|
|
unify' a a' |
|
|
|
let go a b = join $ unify' <$> eval (snd a) <*> eval (snd b) |
|
|
|
zipWithM_ go (sortOn fst b) (sortOn fst b') |
|
|
@ -465,7 +468,7 @@ checkScope s (VGlueTy a phi ty eq) = traverse_ (checkScope s) [a, phi, ty, eq] |
|
|
|
checkScope s (VGlue a phi ty eq inv x) = traverse_ (checkScope s) [a, phi, ty, eq, inv, x] |
|
|
|
checkScope s (VUnglue a phi ty eq vl) = traverse_ (checkScope s) [a, phi, ty, eq, vl] |
|
|
|
|
|
|
|
checkScope s (VCase _ v _) = checkScope s v |
|
|
|
checkScope s (VCase _ _ v _) = checkScope s v |
|
|
|
|
|
|
|
checkSpine :: Set Name -> Seq Projection -> ElabM [Name] |
|
|
|
checkSpine scope (PApp Ex (VVar n@Bound{}) Seq.:<| xs) |
|
|
@ -479,3 +482,133 @@ newtype NonLinearSpine = NonLinearSpine { getDupeName :: Name } |
|
|
|
|
|
|
|
newtype SpineProjection = SpineProj { getSpineProjection :: Projection } |
|
|
|
deriving (Show, Typeable, Exception) |
|
|
|
|
|
|
|
substituteIO :: Map.Map Name Value -> Value -> IO Value |
|
|
|
substituteIO sub = substituteIO . force where |
|
|
|
substituteIO (VNe hd sp) = do |
|
|
|
sp' <- traverse (substituteSp sub) sp |
|
|
|
case hd of |
|
|
|
HMeta (mvCell -> cell) -> do |
|
|
|
solved <- liftIO $ readIORef cell |
|
|
|
case solved of |
|
|
|
Just vl -> substituteIO $ foldl applProj vl sp' |
|
|
|
Nothing -> pure $ VNe hd sp' |
|
|
|
HVar v -> |
|
|
|
case Map.lookup v sub of |
|
|
|
Just vl -> substituteIO $ foldl applProj vl sp' |
|
|
|
Nothing -> pure $ VNe hd sp' |
|
|
|
hd -> pure $ VNe hd sp' |
|
|
|
|
|
|
|
substituteIO (GluedVl h sp vl) = GluedVl h <$> traverse (substituteSp sub) sp <*> substituteIO vl |
|
|
|
|
|
|
|
substituteIO (VLam p (Closure s k)) = pure $ VLam p (Closure s (substitute (Map.delete s sub) . k)) |
|
|
|
substituteIO (VPi p d (Closure s k)) = VPi p <$> substituteIO d <*> pure (Closure s (substitute (Map.delete s sub) . k)) |
|
|
|
substituteIO (VSigma d (Closure s k)) = VSigma <$> substituteIO d <*> pure (Closure s (substitute (Map.delete s sub) . k)) |
|
|
|
substituteIO (VPair a b) = VPair <$> substituteIO a <*> substituteIO b |
|
|
|
|
|
|
|
substituteIO (VPath line x y) = VPath <$> substituteIO line <*> substituteIO x <*> substituteIO y |
|
|
|
substituteIO (VLine line x y f) = VLine <$> substituteIO line <*> substituteIO x <*> substituteIO y <*> substituteIO f |
|
|
|
|
|
|
|
-- Sorts |
|
|
|
substituteIO VType = pure VType |
|
|
|
substituteIO VTypeω = pure VTypeω |
|
|
|
|
|
|
|
substituteIO VI = pure VI |
|
|
|
substituteIO VI0 = pure VI0 |
|
|
|
substituteIO VI1 = pure VI1 |
|
|
|
|
|
|
|
substituteIO (VIAnd x y) = iand <$> substituteIO x <*> substituteIO y |
|
|
|
substituteIO (VIOr x y) = ior <$> substituteIO x <*> substituteIO y |
|
|
|
substituteIO (VINot x) = inot <$> substituteIO x |
|
|
|
|
|
|
|
substituteIO (VIsOne x) = VIsOne <$> substituteIO x |
|
|
|
substituteIO VItIsOne = pure VItIsOne |
|
|
|
|
|
|
|
substituteIO (VPartial x y) = VPartial <$> substituteIO x <*> substituteIO y |
|
|
|
substituteIO (VPartialP x y) = VPartialP <$> substituteIO x <*> substituteIO y |
|
|
|
substituteIO (VSystem fs) = do |
|
|
|
t <- for (Map.toList fs) $ \(a, b) -> (,) <$> substituteIO a <*> substituteIO b |
|
|
|
pure (mkVSystem (Map.fromList t)) |
|
|
|
substituteIO (VSub a b c) = VSub <$> substituteIO a <*> substituteIO b <*> substituteIO c |
|
|
|
substituteIO (VInc a b c) = VInc <$> substituteIO a <*> substituteIO b <*> substituteIO c |
|
|
|
substituteIO (VComp a b c d) = comp <$> substituteIO a <*> substituteIO b <*> substituteIO c <*> substituteIO d |
|
|
|
substituteIO (VHComp a b c d) = hComp <$> substituteIO a <*> substituteIO b <*> substituteIO c <*> substituteIO d |
|
|
|
|
|
|
|
substituteIO (VGlueTy a phi ty e) = glueType <$> substituteIO a <*> substituteIO phi <*> substituteIO ty <*> substituteIO e |
|
|
|
substituteIO (VGlue a phi ty e t x) = glueElem <$> substituteIO a <*> substituteIO phi <*> substituteIO ty <*> substituteIO e <*> substituteIO t <*> substituteIO x |
|
|
|
substituteIO (VUnglue a phi ty e x) = unglue <$> substituteIO a <*> substituteIO phi <*> substituteIO ty <*> substituteIO e <*> substituteIO x |
|
|
|
substituteIO (VCase env t x xs) = VCase env <$> substituteIO t <*> substituteIO x <*> pure xs |
|
|
|
|
|
|
|
substitute :: Map Name Value -> Value -> Value |
|
|
|
substitute sub = unsafePerformIO . substituteIO sub |
|
|
|
|
|
|
|
substituteSp :: Map Name Value -> Projection -> IO Projection |
|
|
|
substituteSp sub (PApp p x) = PApp p <$> substituteIO sub x |
|
|
|
substituteSp sub (PIElim l x y i) = PIElim <$> substituteIO sub l <*> substituteIO sub x <*> substituteIO sub y <*> substituteIO sub i |
|
|
|
substituteSp sub (POuc a phi u) = POuc <$> substituteIO sub a <*> substituteIO sub phi <*> substituteIO sub u |
|
|
|
substituteSp _ PProj1 = pure PProj1 |
|
|
|
substituteSp _ PProj2 = pure PProj2 |
|
|
|
|
|
|
|
mkVSystem :: Map.Map Value Value -> Value |
|
|
|
mkVSystem vals = |
|
|
|
let map' = Map.fromList (map (\(a, b) -> (force a, b)) (Map.toList vals)) in |
|
|
|
case Map.lookup VI1 map' of |
|
|
|
Just x -> x |
|
|
|
Nothing -> VSystem (Map.filterWithKey (\k _ -> k /= VI0) map') |
|
|
|
|
|
|
|
forceIO :: MonadIO m => Value -> m Value |
|
|
|
forceIO mv@(VNe (HMeta (mvCell -> cell)) args) = do |
|
|
|
solved <- liftIO $ readIORef cell |
|
|
|
case solved of |
|
|
|
Just vl -> forceIO (foldl applProj vl args) |
|
|
|
Nothing -> pure mv |
|
|
|
forceIO vl@(VSystem fs) = |
|
|
|
case Map.lookup VI1 fs of |
|
|
|
Just x -> forceIO x |
|
|
|
Nothing -> pure vl |
|
|
|
forceIO (GluedVl _ _ vl) = forceIO vl |
|
|
|
forceIO (VComp line phi u a0) = comp <$> forceIO line <*> forceIO phi <*> pure u <*> pure a0 |
|
|
|
forceIO (VCase env rng v vs) = do |
|
|
|
env' <- liftIO emptyEnv |
|
|
|
evalCase env'{getEnv=env} . (@@) <$> forceIO rng <*> forceIO v <*> pure vs |
|
|
|
forceIO x = pure x |
|
|
|
|
|
|
|
applProj :: Value -> Projection -> Value |
|
|
|
applProj fun (PApp p arg) = vApp p fun arg |
|
|
|
applProj fun (PIElim l x y i) = ielim l x y fun i |
|
|
|
applProj fun (POuc a phi u) = outS a phi u fun |
|
|
|
applProj fun PProj1 = vProj1 fun |
|
|
|
applProj fun PProj2 = vProj2 fun |
|
|
|
|
|
|
|
force :: Value -> Value |
|
|
|
force = unsafePerformIO . forceIO |
|
|
|
|
|
|
|
vApp :: HasCallStack => Plicity -> Value -> Value -> Value |
|
|
|
vApp p (VLam p' k) arg |
|
|
|
| p == p' = clCont k arg |
|
|
|
| otherwise = error $ "wrong plicity " ++ show p ++ " vs " ++ show p' ++ " in app " ++ show (App p (quote (VLam p' k)) (quote arg)) |
|
|
|
vApp p (VNe h sp) arg = VNe h (sp Seq.:|> PApp p arg) |
|
|
|
vApp p (GluedVl h sp vl) arg = GluedVl h (sp Seq.:|> PApp p arg) (vApp p vl arg) |
|
|
|
vApp p (VSystem fs) arg = VSystem (fmap (flip (vApp p) arg) fs) |
|
|
|
vApp p (VInc (VPi _ _ (Closure _ r)) phi f) arg = VInc (r (vApp p f arg)) phi (vApp p f arg) |
|
|
|
vApp _ x _ = error $ "can't apply " ++ show (prettyTm (quote x)) |
|
|
|
|
|
|
|
(@@) :: HasCallStack => Value -> Value -> Value |
|
|
|
(@@) = vApp Ex |
|
|
|
infixl 9 @@ |
|
|
|
|
|
|
|
vProj1 :: HasCallStack => Value -> Value |
|
|
|
vProj1 (VPair a _) = a |
|
|
|
vProj1 (VNe h sp) = VNe h (sp Seq.:|> PProj1) |
|
|
|
vProj1 (GluedVl h sp vl) = GluedVl h (sp Seq.:|> PProj1) (vProj1 vl) |
|
|
|
vProj1 (VSystem fs) = VSystem (fmap vProj1 fs) |
|
|
|
vProj1 (VInc (VSigma a _) b c) = VInc a b (vProj1 c) |
|
|
|
vProj1 x = error $ "can't proj1 " ++ show (prettyTm (quote x)) |
|
|
|
|
|
|
|
vProj2 :: HasCallStack => Value -> Value |
|
|
|
vProj2 (VPair _ b) = b |
|
|
|
vProj2 (VNe h sp) = VNe h (sp Seq.:|> PProj2) |
|
|
|
vProj2 (GluedVl h sp vl) = GluedVl h (sp Seq.:|> PProj2) (vProj2 vl) |
|
|
|
vProj2 (VSystem fs) = VSystem (fmap vProj2 fs) |
|
|
|
vProj2 (VInc (VSigma _ (Closure _ r)) b c) = VInc (r (vProj1 c)) b (vProj2 c) |
|
|
|
vProj2 x = error $ "can't proj2 " ++ show (prettyTm (quote x)) |