|
@ -4,20 +4,27 @@ |
|
|
{-# LANGUAGE LambdaCase #-} |
|
|
{-# LANGUAGE LambdaCase #-} |
|
|
module Eval where |
|
|
module Eval where |
|
|
|
|
|
|
|
|
import Syntax |
|
|
|
|
|
|
|
|
import Control.Exception |
|
|
|
|
|
|
|
|
import qualified Data.Map.Strict as Map |
|
|
import qualified Data.Map.Strict as Map |
|
|
import Data.Foldable |
|
|
import Data.Foldable |
|
|
import Control.Exception |
|
|
|
|
|
import Data.Typeable |
|
|
import Data.Typeable |
|
|
import System.IO.Unsafe (unsafePerformIO) |
|
|
|
|
|
import Data.IORef |
|
|
import Data.IORef |
|
|
import Systems |
|
|
|
|
|
import Presyntax (Formula) |
|
|
|
|
|
import qualified Presyntax as P |
|
|
|
|
|
import Data.Maybe |
|
|
import Data.Maybe |
|
|
|
|
|
|
|
|
import Debug.Trace |
|
|
import Debug.Trace |
|
|
|
|
|
|
|
|
import GHC.Stack |
|
|
import GHC.Stack |
|
|
|
|
|
|
|
|
|
|
|
import qualified Presyntax as P |
|
|
|
|
|
import Presyntax (Formula) |
|
|
|
|
|
|
|
|
|
|
|
import Syntax |
|
|
|
|
|
|
|
|
|
|
|
import System.IO.Unsafe (unsafePerformIO) |
|
|
|
|
|
|
|
|
|
|
|
import Systems |
|
|
|
|
|
|
|
|
iand :: Value -> Value -> Value |
|
|
iand :: Value -> Value -> Value |
|
|
iand = \case |
|
|
iand = \case |
|
|
VI1 -> id |
|
|
VI1 -> id |
|
@ -82,16 +89,12 @@ pathp env p x y (VNe hd sp) i = |
|
|
pathp env p x y (VOfSub _ _ _ v) i = pathp env p x y v i |
|
|
pathp env p x y (VOfSub _ _ _ v) i = pathp env p x y v i |
|
|
|
|
|
|
|
|
comp :: Env -> Value -> Formula -> Value -> Value -> Value |
|
|
comp :: Env -> Value -> Formula -> Value -> Value -> Value |
|
|
comp env a@(VLam ivar VI fam) phi u a0 = go (fam (VVar "woopsie")) phi u a0 where |
|
|
|
|
|
i = VVar ivar |
|
|
|
|
|
|
|
|
comp env a@(VLam ivar VI fam) phi u a0 = glue $ go (fam (VVar "woopsie")) phi u a0 where |
|
|
stuck :: Value |
|
|
stuck :: Value |
|
|
stuck = maybeAddEq $ VComp a (toValue phi) u a0 |
|
|
|
|
|
|
|
|
stuck = VComp a (toValue phi) u a0 |
|
|
|
|
|
|
|
|
maybeAddEq :: Value -> Value |
|
|
|
|
|
maybeAddEq = |
|
|
|
|
|
if phi == P.Top |
|
|
|
|
|
then flip VEqGlued (u @@ VI1) |
|
|
|
|
|
else id |
|
|
|
|
|
|
|
|
glue :: Value -> Value |
|
|
|
|
|
glue = VOfSub (fam VI1) (toValue' env phi) (u @@ VI1) |
|
|
|
|
|
|
|
|
go :: HasCallStack => Value -> Formula -> Value -> Value -> Value |
|
|
go :: HasCallStack => Value -> Formula -> Value -> Value -> Value |
|
|
go VPi{} phi u a0 = |
|
|
go VPi{} phi u a0 = |
|
@ -106,7 +109,7 @@ comp env a@(VLam ivar VI fam) phi u a0 = go (fam (VVar "woopsie")) phi u a0 wher |
|
|
comp env |
|
|
comp env |
|
|
(VLam ivar VI (\i -> rng i (ybar i arg))) |
|
|
(VLam ivar VI (\i -> rng i (ybar i arg))) |
|
|
phi |
|
|
phi |
|
|
(VLam "i" VI \i -> mapVSystem (u @@ i) (@@ ybar i arg)) |
|
|
|
|
|
|
|
|
(VLam "i" VI \i -> mapVSystem (u @@ i) (@@ ybar i arg)) |
|
|
(a0 @@ ybar VI0 arg) |
|
|
(a0 @@ ybar VI0 arg) |
|
|
|
|
|
|
|
|
go VSigma{} phi u a0 = |
|
|
go VSigma{} phi u a0 = |
|
@ -146,7 +149,7 @@ comp env a@(VLam ivar VI fam) phi u a0 = go (fam (VVar "woopsie")) phi u a0 wher |
|
|
(pathp env (ai0 @@ VI0) u0 v0 p0 j) |
|
|
(pathp env (ai0 @@ VI0) u0 v0 p0 j) |
|
|
|
|
|
|
|
|
go a P.Top u a0 = u @@ VI1 |
|
|
go a P.Top u a0 = u @@ VI1 |
|
|
go a phi u a0 = maybeAddEq stuck |
|
|
|
|
|
|
|
|
go a phi u a0 = stuck |
|
|
|
|
|
|
|
|
comp env va phi u a0 = |
|
|
comp env va phi u a0 = |
|
|
if phi == P.Top |
|
|
if phi == P.Top |
|
@ -171,21 +174,16 @@ mapVSystem (VSystem ss) f = VSystem (mapSystem ss f) |
|
|
mapVSystem x f = f x |
|
|
mapVSystem x f = f x |
|
|
|
|
|
|
|
|
evalSystem :: Env -> Map.Map Formula Term -> Value |
|
|
evalSystem :: Env -> Map.Map Formula Term -> Value |
|
|
evalSystem env face = mk . Map.mapMaybeWithKey go $ face where |
|
|
|
|
|
go :: Formula -> Term -> Maybe Value |
|
|
|
|
|
|
|
|
evalSystem env face = mk . Map.fromList . mapMaybe (uncurry go) . Map.toList $ face where |
|
|
|
|
|
go :: Formula -> Term -> Maybe (Formula, Value) |
|
|
go face tm |
|
|
go face tm |
|
|
| VI0 <- toValue' env face = Nothing |
|
|
| VI0 <- toValue' env face = Nothing |
|
|
| otherwise = Just (eval env tm) |
|
|
|
|
|
|
|
|
| otherwise = Just (evalF env face, eval env tm) |
|
|
|
|
|
|
|
|
differsFromEnv :: String -> Bool -> Bool |
|
|
|
|
|
differsFromEnv x True = |
|
|
|
|
|
case Map.lookup x (names env) of |
|
|
|
|
|
Just (VI, VI0) -> True |
|
|
|
|
|
_ -> False |
|
|
|
|
|
differsFromEnv x False = |
|
|
|
|
|
case Map.lookup x (names env) of |
|
|
|
|
|
Just (VI, VI1) -> True |
|
|
|
|
|
_ -> False |
|
|
|
|
|
|
|
|
evalF env tm = |
|
|
|
|
|
case toFormula (toValue' env tm) of |
|
|
|
|
|
Just f -> f |
|
|
|
|
|
Nothing -> error $ "eval turned formula into non formula" |
|
|
|
|
|
|
|
|
mk x = case Map.toList x of |
|
|
mk x = case Map.toList x of |
|
|
[(_, x)] -> x |
|
|
[(_, x)] -> x |
|
@ -222,6 +220,7 @@ eval env = \case |
|
|
Proj2 y -> proj2 (eval env y) |
|
|
Proj2 y -> proj2 (eval env y) |
|
|
|
|
|
|
|
|
Type -> VType |
|
|
Type -> VType |
|
|
|
|
|
Typeω -> VTypeω |
|
|
|
|
|
|
|
|
I -> VI |
|
|
I -> VI |
|
|
I0 -> VI0 |
|
|
I0 -> VI0 |
|
@ -236,9 +235,9 @@ eval env = \case |
|
|
Sub p x y -> VSub (eval env p) (eval env x) (eval env y) |
|
|
Sub p x y -> VSub (eval env p) (eval env x) (eval env y) |
|
|
InclSub a phi u a0 -> VOfSub (eval env a) (eval env phi) (eval env u) (eval env a0) |
|
|
InclSub a phi u a0 -> VOfSub (eval env a) (eval env phi) (eval env u) (eval env a0) |
|
|
|
|
|
|
|
|
IAnd x y -> iand (eval env x) (eval env y) |
|
|
|
|
|
IOr x y -> ior (eval env x) (eval env y) |
|
|
|
|
|
INot x -> inot (eval env x) |
|
|
|
|
|
|
|
|
IAnd x y -> iand (eval env x) (eval env y) |
|
|
|
|
|
IOr x y -> ior (eval env x) (eval env y) |
|
|
|
|
|
INot x -> inot (eval env x) |
|
|
|
|
|
|
|
|
Comp a phi u a0 -> |
|
|
Comp a phi u a0 -> |
|
|
case reduceCube env (eval env phi) of |
|
|
case reduceCube env (eval env phi) of |
|
@ -256,14 +255,18 @@ data UnifyError |
|
|
| NotSort Value |
|
|
| NotSort Value |
|
|
deriving (Show, Typeable, Exception) |
|
|
deriving (Show, Typeable, Exception) |
|
|
|
|
|
|
|
|
unify :: Env -> Value -> Value -> IO () |
|
|
|
|
|
|
|
|
unify :: HasCallStack => Env -> Value -> Value -> IO () |
|
|
unify env (VEqGlued a b) c = |
|
|
unify env (VEqGlued a b) c = |
|
|
unify env a c `catch` \e -> const (unify env b c) (e :: UnifyError) |
|
|
unify env a c `catch` \e -> const (unify env b c) (e :: UnifyError) |
|
|
unify env c (VEqGlued a b) = |
|
|
unify env c (VEqGlued a b) = |
|
|
unify env c a `catch` \e -> const (unify env c b) (e :: UnifyError) |
|
|
unify env c a `catch` \e -> const (unify env c b) (e :: UnifyError) |
|
|
|
|
|
|
|
|
unify env (VLine a x y f) e = unify env (f (VVar "i")) (pathp env a x y e (VVar "i")) |
|
|
|
|
|
unify env e (VLine a x y f) = unify env (f (VVar "i")) (pathp env a x y e (VVar "i")) |
|
|
|
|
|
|
|
|
unify env (VLine a x y f) e = |
|
|
|
|
|
let env' = env { names = Map.insert "i" (VI, VVar "i") (names env) } |
|
|
|
|
|
in unify env' (f (VVar "i")) (pathp env' a x y e (VVar "i")) |
|
|
|
|
|
unify env e (VLine a x y f) = |
|
|
|
|
|
let env' = env { names = Map.insert "i" (VI, VVar "i") (names env) } |
|
|
|
|
|
in unify env' (f (VVar "i")) (pathp env' a x y e (VVar "i")) |
|
|
|
|
|
|
|
|
unify env (VPartial r b) (VPartial r' b') = do |
|
|
unify env (VPartial r b) (VPartial r' b') = do |
|
|
unify env b b' |
|
|
unify env b b' |
|
@ -303,6 +306,7 @@ unify env vl1@(VNe x sp) vl2@(VNe y sp') |
|
|
unifySp (PPathP _a _x _y i) (PPathP _a' _x' _y' i') = unify env i i' |
|
|
unifySp (PPathP _a _x _y i) (PPathP _a' _x' _y' i') = unify env i i' |
|
|
unifySp PProj1 PProj1 = pure () |
|
|
unifySp PProj1 PProj1 = pure () |
|
|
unifySp PProj2 PProj2 = pure () |
|
|
unifySp PProj2 PProj2 = pure () |
|
|
|
|
|
unifySp _ _ = throwIO $ Mismatch vl1 vl2 |
|
|
|
|
|
|
|
|
unify env (VLam x _ k) e = unify env (k (VVar x)) (e @@ VVar x) |
|
|
unify env (VLam x _ k) e = unify env (k (VVar x)) (e @@ VVar x) |
|
|
unify env e (VLam x _ k) = unify env (e @@ VVar x) (k (VVar x)) |
|
|
unify env e (VLam x _ k) = unify env (e @@ VVar x) (k (VVar x)) |
|
@ -321,13 +325,28 @@ unify env VI VI = pure () |
|
|
|
|
|
|
|
|
unify env (VPair a b) (VPair c d) = unify env a c *> unify env b d |
|
|
unify env (VPair a b) (VPair c d) = unify env a c *> unify env b d |
|
|
unify env (VPath a x y) (VPath a' x' y') = unify env a a' *> unify env x x' *> unify env y y' |
|
|
unify env (VPath a x y) (VPath a' x' y') = unify env a a' *> unify env x x' *> unify env y y' |
|
|
|
|
|
unify env (VComp a phi u a0) (VComp a' phi' u' a0') = |
|
|
|
|
|
traverse_ (uncurry (unify env)) |
|
|
|
|
|
[ (a, a') |
|
|
|
|
|
, (phi, phi') |
|
|
|
|
|
, (u, u') |
|
|
|
|
|
, (a0, a0') |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
unify env (VComp a (reduceCube env -> Just P.Top) u a0) vl = |
|
|
|
|
|
unify env (u @@ VI1) vl |
|
|
|
|
|
|
|
|
|
|
|
unify env vl (VComp a (reduceCube env -> Just P.Top) u a0) = |
|
|
|
|
|
unify env (u @@ VI1) vl |
|
|
|
|
|
|
|
|
unify env (VSystem fs) vl |
|
|
unify env (VSystem fs) vl |
|
|
| ((_, vl'):_) <- Map.toList (Map.filterWithKey (\f _ -> isTrue (toValue' env f)) (getSystem fs)) |
|
|
|
|
|
|
|
|
| ((_, vl'):_) <- |
|
|
|
|
|
Map.toList (Map.filterWithKey (\f _ -> isTrue (toValue' env f)) (getSystem fs)) |
|
|
= unify env vl' vl |
|
|
= unify env vl' vl |
|
|
|
|
|
|
|
|
unify env vl (VSystem fs) |
|
|
unify env vl (VSystem fs) |
|
|
| ((_, vl'):_) <- Map.toList (Map.filterWithKey (\f _ -> isTrue (toValue' env f)) (getSystem fs)) |
|
|
|
|
|
|
|
|
| ((_, vl'):_) <- |
|
|
|
|
|
Map.toList (Map.filterWithKey (\f _ -> isTrue (toValue' env f)) (getSystem fs)) |
|
|
= unify env vl' vl |
|
|
= unify env vl' vl |
|
|
|
|
|
|
|
|
unify env VType VTypeω = pure () |
|
|
unify env VType VTypeω = pure () |
|
@ -350,7 +369,7 @@ reduceCube env x = fmap (toDNF . simplify) (toFormula x) where |
|
|
case Map.lookup x (names env) of |
|
|
case Map.lookup x (names env) of |
|
|
Just (VI, VI1) -> P.Top |
|
|
Just (VI, VI1) -> P.Top |
|
|
Just (VI, VI0) -> P.Bot |
|
|
Just (VI, VI0) -> P.Bot |
|
|
_ -> P.Is0 x |
|
|
|
|
|
|
|
|
_ -> P.Is1 x |
|
|
simplify (P.And x y) = P.And (simplify x) (simplify y) |
|
|
simplify (P.And x y) = P.And (simplify x) (simplify y) |
|
|
simplify (P.Or x y) = P.Or (simplify x) (simplify y) |
|
|
simplify (P.Or x y) = P.Or (simplify x) (simplify y) |
|
|
simplify x = x |
|
|
simplify x = x |
|
@ -362,10 +381,10 @@ sameCube env x y = |
|
|
_ -> Nothing |
|
|
_ -> Nothing |
|
|
|
|
|
|
|
|
toFormula :: Value -> Maybe Formula |
|
|
toFormula :: Value -> Maybe Formula |
|
|
toFormula VI0 = Just P.Bot |
|
|
|
|
|
toFormula VI1 = Just P.Top |
|
|
|
|
|
|
|
|
toFormula VI0 = Just P.Bot |
|
|
|
|
|
toFormula VI1 = Just P.Top |
|
|
toFormula (VNe x []) = Just (P.Is1 x) |
|
|
toFormula (VNe x []) = Just (P.Is1 x) |
|
|
toFormula (VINot f) = notFormula <$> toFormula f |
|
|
|
|
|
|
|
|
toFormula (VINot f) = notFormula <$> toFormula f |
|
|
toFormula (VIAnd x y) = do |
|
|
toFormula (VIAnd x y) = do |
|
|
s <- toFormula y |
|
|
s <- toFormula y |
|
|
t <- toFormula x |
|
|
t <- toFormula x |
|
@ -442,7 +461,7 @@ toValue (P.Or x y) = toValue x `ior` toValue y |
|
|
toValue (P.Is0 x) = inot (VVar x) |
|
|
toValue (P.Is0 x) = inot (VVar x) |
|
|
toValue (P.Is1 x) = VVar x |
|
|
toValue (P.Is1 x) = VVar x |
|
|
|
|
|
|
|
|
toValue' :: Env -> Formula -> Value |
|
|
|
|
|
|
|
|
toValue' :: HasCallStack => Env -> Formula -> Value |
|
|
toValue' env P.Top = VI1 |
|
|
toValue' env P.Top = VI1 |
|
|
toValue' env P.Bot = VI0 |
|
|
toValue' env P.Bot = VI0 |
|
|
toValue' env (P.And x y) = toValue x `iand` toValue y |
|
|
toValue' env (P.And x y) = toValue x `iand` toValue y |
|
@ -452,7 +471,7 @@ toValue' env (P.Is0 x) = |
|
|
Just (VI, VI0) -> VI1 |
|
|
Just (VI, VI0) -> VI1 |
|
|
Just (VI, VI1) -> VI0 |
|
|
Just (VI, VI1) -> VI0 |
|
|
Just (VI, x) -> inot x |
|
|
Just (VI, x) -> inot x |
|
|
_ -> error $ "type error in toValue'" |
|
|
|
|
|
|
|
|
vl -> error $ "type error in toValue' " ++ x ++ ": " ++ show vl |
|
|
toValue' env (P.Is1 x) = |
|
|
toValue' env (P.Is1 x) = |
|
|
case Map.lookup x (names env) of |
|
|
case Map.lookup x (names env) of |
|
|
Just (VI, x) -> x |
|
|
Just (VI, x) -> x |
|
|