|
@ -6,6 +6,7 @@ |
|
|
{-# LANGUAGE TupleSections #-} |
|
|
{-# LANGUAGE TupleSections #-} |
|
|
module Elab.Eval where |
|
|
module Elab.Eval where |
|
|
|
|
|
|
|
|
|
|
|
import Control.Arrow (Arrow(second)) |
|
|
import Control.Monad.Reader |
|
|
import Control.Monad.Reader |
|
|
import Control.Exception |
|
|
import Control.Exception |
|
|
|
|
|
|
|
@ -51,7 +52,7 @@ forceIO vl@(VSystem fs) = |
|
|
Just x -> forceIO x |
|
|
Just x -> forceIO x |
|
|
Nothing -> pure vl |
|
|
Nothing -> pure vl |
|
|
forceIO (GluedVl _ _ vl) = forceIO 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 <$> forceIO line <*> forceIO phi <*> pure u <*> pure a0 |
|
|
forceIO x = pure x |
|
|
forceIO x = pure x |
|
|
|
|
|
|
|
|
applProj :: Value -> Projection -> Value |
|
|
applProj :: Value -> Projection -> Value |
|
@ -99,8 +100,6 @@ zonkIO (VIOr x y) = ior <$> zonkIO x <*> zonkIO y |
|
|
zonkIO (VINot x) = inot <$> zonkIO x |
|
|
zonkIO (VINot x) = inot <$> zonkIO x |
|
|
|
|
|
|
|
|
zonkIO (VIsOne x) = VIsOne <$> zonkIO x |
|
|
zonkIO (VIsOne x) = VIsOne <$> zonkIO x |
|
|
zonkIO (VIsOne1 x) = VIsOne1 <$> zonkIO x |
|
|
|
|
|
zonkIO (VIsOne2 x) = VIsOne2 <$> zonkIO x |
|
|
|
|
|
zonkIO VItIsOne = pure VItIsOne |
|
|
zonkIO VItIsOne = pure VItIsOne |
|
|
|
|
|
|
|
|
zonkIO (VPartial x y) = VPartial <$> zonkIO x <*> zonkIO y |
|
|
zonkIO (VPartial x y) = VPartial <$> zonkIO x <*> zonkIO y |
|
@ -115,11 +114,7 @@ zonkIO (VComp a b c d) = comp <$> zonkIO a <*> zonkIO b <*> zonkIO c <*> zonkIO |
|
|
zonkIO (VGlueTy a phi ty e) = glueType <$> zonkIO a <*> zonkIO phi <*> zonkIO ty <*> zonkIO e |
|
|
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 (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 (VUnglue a phi ty e x) = unglue <$> zonkIO a <*> zonkIO phi <*> zonkIO ty <*> zonkIO e <*> zonkIO x |
|
|
|
|
|
|
|
|
zonkIO VBool = pure VBool |
|
|
|
|
|
zonkIO VTt = pure VTt |
|
|
|
|
|
zonkIO VFf = pure VFf |
|
|
|
|
|
zonkIO (VIf a b c d) = elimBool <$> zonkIO a <*> zonkIO b <*> zonkIO c <*> zonkIO d |
|
|
|
|
|
|
|
|
zonkIO (VCase x xs) = VCase <$> zonkIO x <*> traverse (\(x, y) -> (x,) <$> zonkIO y) xs |
|
|
|
|
|
|
|
|
zonkSp :: Projection -> IO Projection |
|
|
zonkSp :: Projection -> IO Projection |
|
|
zonkSp (PApp p x) = PApp p <$> zonkIO x |
|
|
zonkSp (PApp p x) = PApp p <$> zonkIO x |
|
@ -143,6 +138,13 @@ eval' env (Ref x) = |
|
|
case Map.lookup x (getEnv env) of |
|
|
case Map.lookup x (getEnv env) of |
|
|
Just (_, vl) -> vl |
|
|
Just (_, vl) -> vl |
|
|
_ -> VNe (HVar x) mempty |
|
|
_ -> VNe (HVar x) mempty |
|
|
|
|
|
|
|
|
|
|
|
eval' env (Con x) = |
|
|
|
|
|
case Map.lookup x (getEnv env) of |
|
|
|
|
|
Just (ty, _) -> VNe (HCon ty x) mempty |
|
|
|
|
|
Nothing -> error $ "constructor " ++ show x ++ " has no type in scope" |
|
|
|
|
|
|
|
|
|
|
|
eval' _ (Data x) = VNe (HData x) mempty |
|
|
eval' env (App p f x) = vApp p (eval' env f) (eval' env x) |
|
|
eval' env (App p f x) = vApp p (eval' env f) (eval' env x) |
|
|
|
|
|
|
|
|
eval' env (Lam p s t) = |
|
|
eval' env (Lam p s t) = |
|
@ -179,8 +181,6 @@ eval' e (IElim l x y f i) = ielim (eval' e l) (eval' e x) (eval' e y) (eval' e f |
|
|
eval' e (PathIntro p x y f) = VLine (eval' e p) (eval' e x) (eval' e y) (eval' e f) |
|
|
eval' e (PathIntro p x y f) = VLine (eval' e p) (eval' e x) (eval' e y) (eval' e f) |
|
|
|
|
|
|
|
|
eval' e (IsOne i) = VIsOne (eval' e i) |
|
|
eval' e (IsOne i) = VIsOne (eval' e i) |
|
|
eval' e (IsOne1 i) = VIsOne1 (eval' e i) |
|
|
|
|
|
eval' e (IsOne2 i) = VIsOne2 (eval' e i) |
|
|
|
|
|
eval' _ ItIsOne = VItIsOne |
|
|
eval' _ ItIsOne = VItIsOne |
|
|
|
|
|
|
|
|
eval' e (Partial x y) = VPartial (eval' e x) (eval' e y) |
|
|
eval' e (Partial x y) = VPartial (eval' e x) (eval' e y) |
|
@ -200,10 +200,15 @@ eval' e (Let ns x) = |
|
|
let env' = foldl (\newe (n, ty, x) -> newe { getEnv = Map.insert n (eval' newe ty, eval' newe x) (getEnv newe) }) e ns |
|
|
let env' = foldl (\newe (n, ty, x) -> newe { getEnv = Map.insert n (eval' newe ty, eval' newe x) (getEnv newe) }) e ns |
|
|
in eval' env' x |
|
|
in eval' env' x |
|
|
|
|
|
|
|
|
eval' e (If a b c d) = elimBool (eval' e a) (eval' e b) (eval' e c) (eval' e d) |
|
|
|
|
|
eval' _ Bool = VBool |
|
|
|
|
|
eval' _ Tt = VTt |
|
|
|
|
|
eval' _ Ff = VFf |
|
|
|
|
|
|
|
|
eval' e (Case sc xs) = evalCase e (eval' e sc) xs |
|
|
|
|
|
|
|
|
|
|
|
evalCase :: ElabEnv -> Value -> [(Term, Term)] -> Value |
|
|
|
|
|
evalCase _ sc [] = error $ "unmatched pattern for value: " ++ show (prettyTm (quote sc)) |
|
|
|
|
|
evalCase env sc ((Ref _, k):_) = eval' env k @@ sc |
|
|
|
|
|
evalCase env (force -> val@(VNe (HCon _ x) sp)) ((Con x', k):xs) |
|
|
|
|
|
| x == x' = foldl applProj (eval' env k) sp |
|
|
|
|
|
| otherwise = evalCase env val xs |
|
|
|
|
|
evalCase env sc xs = VCase sc (map (second (eval' env)) xs) |
|
|
|
|
|
|
|
|
vApp :: HasCallStack => Plicity -> Value -> Value -> Value |
|
|
vApp :: HasCallStack => Plicity -> Value -> Value -> Value |
|
|
vApp p (VLam p' k) arg |
|
|
vApp p (VLam p' k) arg |
|
@ -291,10 +296,6 @@ unify' topa topb = join $ go <$> forceIO topa <*> forceIO topb where |
|
|
-- IsOne is proof-irrelevant: |
|
|
-- IsOne is proof-irrelevant: |
|
|
go VItIsOne _ = pure () |
|
|
go VItIsOne _ = pure () |
|
|
go _ VItIsOne = pure () |
|
|
go _ VItIsOne = pure () |
|
|
go VIsOne1{} _ = pure () |
|
|
|
|
|
go _ VIsOne1{} = pure () |
|
|
|
|
|
go VIsOne2{} _ = pure () |
|
|
|
|
|
go _ VIsOne2{} = pure () |
|
|
|
|
|
|
|
|
|
|
|
go (VPartial phi r) (VPartial phi' r') = unify' phi phi' *> unify' r r' |
|
|
go (VPartial phi r) (VPartial phi' r') = unify' phi phi' *> unify' r r' |
|
|
go (VPartialP phi r) (VPartialP phi' r') = unify' phi phi' *> unify' r r' |
|
|
go (VPartialP phi r) (VPartialP phi' r') = unify' phi phi' *> unify' r r' |
|
@ -317,10 +318,6 @@ unify' topa topb = join $ go <$> forceIO topa <*> forceIO topb where |
|
|
go (VSystem sys) rhs = goSystem unify' sys rhs |
|
|
go (VSystem sys) rhs = goSystem unify' sys rhs |
|
|
go rhs (VSystem sys) = goSystem (flip unify') sys rhs |
|
|
go rhs (VSystem sys) = goSystem (flip unify') sys rhs |
|
|
|
|
|
|
|
|
go VTt VTt = pure () |
|
|
|
|
|
go VFf VFf = pure () |
|
|
|
|
|
go VBool VBool = pure () |
|
|
|
|
|
|
|
|
|
|
|
go x y |
|
|
go x y |
|
|
| x == y = pure () |
|
|
| x == y = pure () |
|
|
| otherwise = |
|
|
| otherwise = |
|
@ -356,7 +353,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` \(_ :: SomeException) -> 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 |
|
@ -436,7 +433,9 @@ checkScope scope (VNe h sp) = |
|
|
unless (v `Set.member` scope) . throwElab $ |
|
|
unless (v `Set.member` scope) . throwElab $ |
|
|
NotInScope v |
|
|
NotInScope v |
|
|
HVar{} -> pure () |
|
|
HVar{} -> pure () |
|
|
|
|
|
HCon{} -> pure () |
|
|
HMeta{} -> pure () |
|
|
HMeta{} -> pure () |
|
|
|
|
|
HData{} -> pure () |
|
|
traverse_ checkProj sp |
|
|
traverse_ checkProj sp |
|
|
where |
|
|
where |
|
|
checkProj (PApp _ t) = checkScope scope t |
|
|
checkProj (PApp _ t) = checkScope scope t |
|
@ -475,8 +474,6 @@ checkScope s (VPath line a b) = traverse_ (checkScope s) [line, a, b] |
|
|
checkScope s (VLine _ _ _ line) = checkScope s line |
|
|
checkScope s (VLine _ _ _ line) = checkScope s line |
|
|
|
|
|
|
|
|
checkScope s (VIsOne x) = checkScope s x |
|
|
checkScope s (VIsOne x) = checkScope s x |
|
|
checkScope s (VIsOne1 x) = checkScope s x |
|
|
|
|
|
checkScope s (VIsOne2 x) = checkScope s x |
|
|
|
|
|
checkScope _ VItIsOne = pure () |
|
|
checkScope _ VItIsOne = pure () |
|
|
|
|
|
|
|
|
checkScope s (VPartial x y) = traverse_ (checkScope s) [x, y] |
|
|
checkScope s (VPartial x y) = traverse_ (checkScope s) [x, y] |
|
@ -492,10 +489,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 (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 (VUnglue a phi ty eq vl) = traverse_ (checkScope s) [a, phi, ty, eq, vl] |
|
|
|
|
|
|
|
|
checkScope s (VIf a b c d) = traverse_ (checkScope s) [a, b, c, d] |
|
|
|
|
|
checkScope _ VBool = pure () |
|
|
|
|
|
checkScope _ VTt = pure () |
|
|
|
|
|
checkScope _ VFf = pure () |
|
|
|
|
|
|
|
|
checkScope s (VCase v xs) = checkScope s v *> traverse_ (checkScope s . snd) xs |
|
|
|
|
|
|
|
|
checkSpine :: Set Name -> Seq Projection -> ElabM [Name] |
|
|
checkSpine :: Set Name -> Seq Projection -> ElabM [Name] |
|
|
checkSpine scope (PApp Ex (VVar n@Bound{}) Seq.:<| xs) |
|
|
checkSpine scope (PApp Ex (VVar n@Bound{}) Seq.:<| xs) |
|
|