|
@ -41,15 +41,16 @@ eval :: Term -> ElabM Value |
|
|
eval t = asks (flip eval' t) |
|
|
eval t = asks (flip eval' t) |
|
|
|
|
|
|
|
|
forceIO :: MonadIO m => Value -> m Value |
|
|
forceIO :: MonadIO m => Value -> m Value |
|
|
forceIO mv@(VNe (HMeta (mvCell -> cell)) args) = do |
|
|
|
|
|
|
|
|
forceIO mv@(VNe hd@(HMeta (mvCell -> cell)) args) = do |
|
|
solved <- liftIO $ readIORef cell |
|
|
solved <- liftIO $ readIORef cell |
|
|
case solved of |
|
|
case solved of |
|
|
Just vl -> forceIO $ foldl applProj vl args |
|
|
|
|
|
|
|
|
Just vl -> forceIO (foldl applProj vl args) |
|
|
Nothing -> pure mv |
|
|
Nothing -> pure mv |
|
|
forceIO vl@(VSystem fs) = |
|
|
forceIO vl@(VSystem fs) = |
|
|
case Map.lookup VI1 fs of |
|
|
case Map.lookup VI1 fs of |
|
|
Just x -> forceIO x |
|
|
Just x -> forceIO x |
|
|
Nothing -> pure vl |
|
|
Nothing -> pure vl |
|
|
|
|
|
forceIO (GluedVl _ _ vl) = forceIO vl |
|
|
forceIO (VComp line phi u a0) = comp line <$> forceIO phi <*> pure u <*> pure a0 |
|
|
forceIO (VComp line phi u a0) = comp line <$> forceIO phi <*> pure u <*> pure a0 |
|
|
forceIO x = pure x |
|
|
forceIO x = pure x |
|
|
|
|
|
|
|
@ -74,12 +75,8 @@ zonkIO (VNe hd sp) = do |
|
|
Just vl -> zonkIO $ foldl applProj vl sp' |
|
|
Just vl -> zonkIO $ foldl applProj vl sp' |
|
|
Nothing -> pure $ VNe hd sp' |
|
|
Nothing -> pure $ VNe hd sp' |
|
|
hd -> pure $ VNe hd sp' |
|
|
hd -> pure $ VNe hd sp' |
|
|
where |
|
|
|
|
|
zonkSp (PApp p x) = PApp p <$> zonkIO x |
|
|
|
|
|
zonkSp (PIElim l x y i) = PIElim <$> zonkIO l <*> zonkIO x <*> zonkIO y <*> zonkIO i |
|
|
|
|
|
zonkSp (POuc a phi u) = POuc <$> zonkIO a <*> zonkIO phi <*> zonkIO u |
|
|
|
|
|
zonkSp PProj1 = pure PProj1 |
|
|
|
|
|
zonkSp PProj2 = pure PProj2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
zonkIO (GluedVl h sp vl) = GluedVl h <$> traverse zonkSp sp <*> zonkIO vl |
|
|
|
|
|
|
|
|
zonkIO (VLam p (Closure s k)) = pure $ VLam p (Closure s (zonk . k)) |
|
|
zonkIO (VLam p (Closure s k)) = pure $ VLam p (Closure s (zonk . k)) |
|
|
zonkIO (VPi p d (Closure s k)) = VPi p <$> zonkIO d <*> pure (Closure s (zonk . k)) |
|
|
zonkIO (VPi p d (Closure s k)) = VPi p <$> zonkIO d <*> pure (Closure s (zonk . k)) |
|
@ -124,11 +121,19 @@ zonkIO VTt = pure VTt |
|
|
zonkIO VFf = pure VFf |
|
|
zonkIO VFf = pure VFf |
|
|
zonkIO (VIf a b c d) = elimBool <$> zonkIO a <*> zonkIO b <*> zonkIO c <*> zonkIO d |
|
|
zonkIO (VIf a b c d) = elimBool <$> zonkIO a <*> zonkIO b <*> zonkIO c <*> zonkIO d |
|
|
|
|
|
|
|
|
|
|
|
zonkSp :: Projection -> IO Projection |
|
|
|
|
|
zonkSp (PApp p x) = PApp p <$> zonkIO x |
|
|
|
|
|
zonkSp (PIElim l x y i) = PIElim <$> zonkIO l <*> zonkIO x <*> zonkIO y <*> zonkIO i |
|
|
|
|
|
zonkSp (POuc a phi u) = POuc <$> zonkIO a <*> zonkIO phi <*> zonkIO u |
|
|
|
|
|
zonkSp PProj1 = pure PProj1 |
|
|
|
|
|
zonkSp PProj2 = pure PProj2 |
|
|
|
|
|
|
|
|
mkVSystem :: Map.Map Value Value -> Value |
|
|
mkVSystem :: Map.Map Value Value -> Value |
|
|
mkVSystem map = |
|
|
|
|
|
case Map.lookup VI1 map of |
|
|
|
|
|
|
|
|
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 |
|
|
Just x -> x |
|
|
Nothing -> VSystem (Map.filterWithKey (\k _ -> k /= VI0) map) |
|
|
|
|
|
|
|
|
Nothing -> VSystem (Map.filterWithKey (\k _ -> k /= VI0) map') |
|
|
|
|
|
|
|
|
zonk :: Value -> Value |
|
|
zonk :: Value -> Value |
|
|
zonk = unsafePerformIO . zonkIO |
|
|
zonk = unsafePerformIO . zonkIO |
|
@ -204,7 +209,8 @@ vApp :: HasCallStack => Plicity -> Value -> Value -> Value |
|
|
vApp p (VLam p' k) arg |
|
|
vApp p (VLam p' k) arg |
|
|
| p == p' = clCont 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)) |
|
|
| 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 (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 (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 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)) |
|
|
vApp _ x _ = error $ "can't apply " ++ show (prettyTm (quote x)) |
|
@ -216,6 +222,7 @@ infixl 9 @@ |
|
|
vProj1 :: HasCallStack => Value -> Value |
|
|
vProj1 :: HasCallStack => Value -> Value |
|
|
vProj1 (VPair a _) = a |
|
|
vProj1 (VPair a _) = a |
|
|
vProj1 (VNe h sp) = VNe h (sp Seq.:|> PProj1) |
|
|
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 (VSystem fs) = VSystem (fmap vProj1 fs) |
|
|
vProj1 (VInc (VSigma a _) b c) = VInc a b (vProj1 c) |
|
|
vProj1 (VInc (VSigma a _) b c) = VInc a b (vProj1 c) |
|
|
vProj1 x = error $ "can't proj1 " ++ show (prettyTm (quote x)) |
|
|
vProj1 x = error $ "can't proj1 " ++ show (prettyTm (quote x)) |
|
@ -223,6 +230,7 @@ vProj1 x = error $ "can't proj1 " ++ show (prettyTm (quote x)) |
|
|
vProj2 :: HasCallStack => Value -> Value |
|
|
vProj2 :: HasCallStack => Value -> Value |
|
|
vProj2 (VPair _ b) = b |
|
|
vProj2 (VPair _ b) = b |
|
|
vProj2 (VNe h sp) = VNe h (sp Seq.:|> PProj2) |
|
|
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 (VSystem fs) = VSystem (fmap vProj2 fs) |
|
|
vProj2 (VInc (VSigma _ (Closure _ r)) b c) = VInc (r (vProj1 c)) b (vProj2 c) |
|
|
vProj2 (VInc (VSigma _ (Closure _ r)) b c) = VInc (r (vProj1 c)) b (vProj2 c) |
|
|
vProj2 x = error $ "can't proj2 " ++ show (prettyTm (quote x)) |
|
|
vProj2 x = error $ "can't proj2 " ++ show (prettyTm (quote x)) |
|
@ -239,12 +247,12 @@ unify' topa topb = join $ go <$> forceIO topa <*> forceIO topb where |
|
|
| x == x', length a == length a' = |
|
|
| x == x', length a == length a' = |
|
|
traverse_ (uncurry unify'Spine) (Seq.zip a a') |
|
|
traverse_ (uncurry unify'Spine) (Seq.zip a a') |
|
|
|
|
|
|
|
|
go (VLam p (Closure _ k)) vl = do |
|
|
|
|
|
t <- VVar <$> newName |
|
|
|
|
|
|
|
|
go (VLam p (Closure n k)) vl = do |
|
|
|
|
|
t <- VVar <$> newName' n |
|
|
unify' (k t) (vApp p vl t) |
|
|
unify' (k t) (vApp p vl t) |
|
|
|
|
|
|
|
|
go vl (VLam p (Closure _ k)) = do |
|
|
|
|
|
t <- VVar <$> newName |
|
|
|
|
|
|
|
|
go vl (VLam p (Closure n k)) = do |
|
|
|
|
|
t <- VVar <$> newName' n |
|
|
unify' (vApp p vl t) (k t) |
|
|
unify' (vApp p vl t) (k t) |
|
|
|
|
|
|
|
|
go (VPair a b) vl = unify' a (vProj1 vl) *> unify' b (vProj2 vl) |
|
|
go (VPair a b) vl = unify' a (vProj1 vl) *> unify' b (vProj2 vl) |
|
@ -297,8 +305,8 @@ unify' topa topb = join $ go <$> forceIO topa <*> forceIO topb where |
|
|
go (VComp a phi u a0) (VComp a' phi' u' a0') = |
|
|
go (VComp a phi u a0) (VComp a' phi' u' a0') = |
|
|
traverse_ (uncurry unify') [(a, a'), (phi, phi'), (u, u'), (a0, a0')] |
|
|
traverse_ (uncurry unify') [(a, a'), (phi, phi'), (u, u'), (a0, a0')] |
|
|
|
|
|
|
|
|
go (VGlueTy _ VI1 u _0) rhs = unify' (u @@ VItIsOne) rhs |
|
|
|
|
|
go lhs (VGlueTy _ VI1 u _0) = unify' lhs (u @@ VItIsOne) |
|
|
|
|
|
|
|
|
go (VGlueTy _ (force -> VI1) u _0) rhs = unify' (u @@ VItIsOne) rhs |
|
|
|
|
|
go lhs (VGlueTy _ (force -> VI1) u _0) = unify' lhs (u @@ VItIsOne) |
|
|
|
|
|
|
|
|
go (VGlueTy a phi u a0) (VGlueTy a' phi' u' a0') = |
|
|
go (VGlueTy a phi u a0) (VGlueTy a' phi' u' a0') = |
|
|
traverse_ (uncurry unify') [(a, a'), (phi, phi'), (u, u'), (a0, a0')] |
|
|
traverse_ (uncurry unify') [(a, a'), (phi, phi'), (u, u'), (a0, a0')] |
|
@ -348,7 +356,7 @@ unify' topa topb = join $ go <$> forceIO topa <*> forceIO topb where |
|
|
| otherwise = fail |
|
|
| otherwise = fail |
|
|
|
|
|
|
|
|
unify :: HasCallStack => Value -> Value -> ElabM () |
|
|
unify :: HasCallStack => Value -> Value -> ElabM () |
|
|
unify a b = unify' a b `catchElab` \(_ :: NotEqual) -> liftIO $ throwIO (NotEqual a b) |
|
|
|
|
|
|
|
|
unify a b = unify' a b `catchElab` \(_ :: SomeException) -> liftIO $ throwIO (NotEqual a b) |
|
|
|
|
|
|
|
|
isConvertibleTo :: Value -> Value -> ElabM (Term -> Term) |
|
|
isConvertibleTo :: Value -> Value -> ElabM (Term -> Term) |
|
|
isConvertibleTo a b = isConvertibleTo (force a) (force b) where |
|
|
isConvertibleTo a b = isConvertibleTo (force a) (force b) where |
|
@ -392,6 +400,11 @@ newName = liftIO $ do |
|
|
x <- atomicModifyIORef _nameCounter $ \x -> (x + 1, x + 1) |
|
|
x <- atomicModifyIORef _nameCounter $ \x -> (x + 1, x + 1) |
|
|
pure (Bound (T.pack (show x)) x) |
|
|
pure (Bound (T.pack (show x)) x) |
|
|
|
|
|
|
|
|
|
|
|
newName' :: Name -> ElabM Name |
|
|
|
|
|
newName' n = do |
|
|
|
|
|
~(Bound _ x) <- newName |
|
|
|
|
|
pure (Bound (getNameText n) x) |
|
|
|
|
|
|
|
|
_nameCounter :: IORef Int |
|
|
_nameCounter :: IORef Int |
|
|
_nameCounter = unsafePerformIO $ newIORef 0 |
|
|
_nameCounter = unsafePerformIO $ newIORef 0 |
|
|
{-# NOINLINE _nameCounter #-} |
|
|
{-# NOINLINE _nameCounter #-} |
|
@ -432,6 +445,8 @@ checkScope scope (VNe h sp) = |
|
|
checkProj PProj1 = pure () |
|
|
checkProj PProj1 = pure () |
|
|
checkProj PProj2 = pure () |
|
|
checkProj PProj2 = pure () |
|
|
|
|
|
|
|
|
|
|
|
checkScope scope (GluedVl _ _p vl) = checkScope scope vl |
|
|
|
|
|
|
|
|
checkScope scope (VLam _ (Closure n k)) = |
|
|
checkScope scope (VLam _ (Closure n k)) = |
|
|
checkScope (Set.insert n scope) (k (VVar n)) |
|
|
checkScope (Set.insert n scope) (k (VVar n)) |
|
|
|
|
|
|
|
|