diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-04 08:39:23 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-04 08:39:23 +0200 |
commit | 4425f4c90361927a7a1b839e69b89d56d9b77e8a (patch) | |
tree | 4c59501509aa6e083e2a88f023c8e6b8697944e5 /src | |
parent | e23ea133eb598e822183b44fe97ea0f6bdc4d4c9 (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler.hs | 6 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Core.hs | 36 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/CoreToIR.hs | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/DeBruijn.hs | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 164 |
5 files changed, 112 insertions, 102 deletions
diff --git a/src/LambdaCube/Compiler.hs b/src/LambdaCube/Compiler.hs index bc1f615b..65f9f965 100644 --- a/src/LambdaCube/Compiler.hs +++ b/src/LambdaCube/Compiler.hs | |||
@@ -51,7 +51,7 @@ import LambdaCube.Compiler.CoreToIR | |||
51 | 51 | ||
52 | import LambdaCube.Compiler.Utils | 52 | import LambdaCube.Compiler.Utils |
53 | import LambdaCube.Compiler.DesugaredSource as Exported (FileInfo(..), Range(..), SPos(..), SIName(..), pattern SIName, sName) | 53 | import LambdaCube.Compiler.DesugaredSource as Exported (FileInfo(..), Range(..), SPos(..), SIName(..), pattern SIName, sName) |
54 | import LambdaCube.Compiler.Infer as Exported (Infos, Info(..), listAllInfos, listTypeInfos, listTraceInfos, errorRange, Exp, outputType, boolType, trueExp, unfixlabel) | 54 | import LambdaCube.Compiler.Infer as Exported (Infos, Info(..), listAllInfos, listTypeInfos, listTraceInfos, errorRange, Exp, ExpType(..), outputType, boolType, trueExp, unfixlabel) |
55 | 55 | ||
56 | -- inlcude path for: Builtins, Internals and Prelude | 56 | -- inlcude path for: Builtins, Internals and Prelude |
57 | import Paths_lambdacube_compiler (getDataDir) | 57 | import Paths_lambdacube_compiler (getDataDir) |
@@ -188,7 +188,7 @@ loadModule ex imp mname_ = do | |||
188 | filterImports (ImportJust ns) = (`elem` map sName ns) | 188 | filterImports (ImportJust ns) = (`elem` map sName ns) |
189 | 189 | ||
190 | -- used in runTests | 190 | -- used in runTests |
191 | getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m (Infos, [Stmt]) ((Infos, [Stmt]), Either Doc (FilePath, Either Doc (Exp, Exp))) | 191 | getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m (Infos, [Stmt]) ((Infos, [Stmt]), Either Doc (FilePath, Either Doc ExpType)) |
192 | getDef = getDef_ id | 192 | getDef = getDef_ id |
193 | 193 | ||
194 | getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case | 194 | getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case |
@@ -201,7 +201,7 @@ getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case | |||
201 | Just (e, thy, si) | 201 | Just (e, thy, si) |
202 | | Just False <- (== thy) <$> ty -- TODO: better type comparison | 202 | | Just False <- (== thy) <$> ty -- TODO: better type comparison |
203 | -> Left $ "type of" <+> text d <+> "should be" <+> pShow ty <+> "instead of" <+> pShow thy | 203 | -> Left $ "type of" <+> text d <+> "should be" <+> pShow ty <+> "instead of" <+> pShow thy |
204 | | otherwise -> Right (e, thy) | 204 | | otherwise -> Right (ET e thy) |
205 | Nothing -> Left $ text d <+> "is not found" | 205 | Nothing -> Left $ text d <+> "is not found" |
206 | ) | 206 | ) |
207 | 207 | ||
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs index 42e6307d..5670da79 100644 --- a/src/LambdaCube/Compiler/Core.hs +++ b/src/LambdaCube/Compiler/Core.hs | |||
@@ -87,8 +87,8 @@ upDB x (MaxDB i) = MaxDB $ ad x i where | |||
87 | class HasMaxDB a where | 87 | class HasMaxDB a where |
88 | maxDB_ :: a -> MaxDB | 88 | maxDB_ :: a -> MaxDB |
89 | 89 | ||
90 | instance (HasMaxDB a, HasMaxDB b) => HasMaxDB (a, b) where | 90 | instance HasMaxDB ExpType where |
91 | maxDB_ (a, b) = maxDB_ a <> maxDB_ b | 91 | maxDB_ (ET a b) = maxDB_ a <> maxDB_ b |
92 | 92 | ||
93 | 93 | ||
94 | -------------------------------------------------------------------------------- names | 94 | -------------------------------------------------------------------------------- names |
@@ -217,7 +217,14 @@ data Neutral | |||
217 | -------------------------------------------------------------------------------- auxiliary functions and patterns | 217 | -------------------------------------------------------------------------------- auxiliary functions and patterns |
218 | 218 | ||
219 | type Type = Exp | 219 | type Type = Exp |
220 | type ExpType = (Exp, Type) | 220 | data ExpType = ET {expr :: Exp, ty :: Type} |
221 | deriving (Eq) | ||
222 | |||
223 | instance Rearrange ExpType where | ||
224 | rearrange l f (ET e t) = ET (rearrange l f e) (rearrange l f t) | ||
225 | |||
226 | instance PShow ExpType where pShow (ET x t) = DAnn (pShow x) (pShow t) | ||
227 | |||
221 | type SExp2 = SExp' ExpType | 228 | type SExp2 = SExp' ExpType |
222 | 229 | ||
223 | pattern TType = TType_ CompileTime | 230 | pattern TType = TType_ CompileTime |
@@ -392,6 +399,9 @@ class Subst b a where | |||
392 | --subst :: Subst b a => Int -> MaxDB -> b -> a -> a | 399 | --subst :: Subst b a => Int -> MaxDB -> b -> a -> a |
393 | subst i x a = subst_ i (maxDB_ x) x a | 400 | subst i x a = subst_ i (maxDB_ x) x a |
394 | 401 | ||
402 | instance Subst Exp ExpType where | ||
403 | subst_ i dx x (ET a b) = ET (subst_ i dx x a) (subst_ i dx x b) | ||
404 | |||
395 | instance Subst ExpType SExp2 where | 405 | instance Subst ExpType SExp2 where |
396 | subst_ j _ x = mapS (\_ _ -> error "subst: TODO") (const . SGlobal) f2 0 | 406 | subst_ j _ x = mapS (\_ _ -> error "subst: TODO") (const . SGlobal) f2 0 |
397 | where | 407 | where |
@@ -501,6 +511,10 @@ instance Up Neutral where | |||
501 | App_ a b -> foldVar f i a <> foldVar f i b | 511 | App_ a b -> foldVar f i a <> foldVar f i b |
502 | Fun' _ vs x d -> foldMap (foldVar f i) vs <> foldMap (foldVar f i) x -- <> foldVar f i d | 512 | Fun' _ vs x d -> foldMap (foldVar f i) vs <> foldMap (foldVar f i) x -- <> foldVar f i d |
503 | 513 | ||
514 | instance Up ExpType where | ||
515 | usedVar i (ET a b) = usedVar i a || usedVar i b | ||
516 | foldVar f i (ET a b) = foldVar f i a <> foldVar f i b | ||
517 | |||
504 | instance HasMaxDB Exp where | 518 | instance HasMaxDB Exp where |
505 | maxDB_ = \case | 519 | maxDB_ = \case |
506 | Lam_ c _ -> c | 520 | Lam_ c _ -> c |
@@ -522,9 +536,6 @@ instance HasMaxDB Neutral where | |||
522 | App__ c a b -> c | 536 | App__ c a b -> c |
523 | Fun_ c _ _ _ _ -> c | 537 | Fun_ c _ _ _ _ -> c |
524 | 538 | ||
525 | instance (Subst x a, Subst x b) => Subst x (a, b) where | ||
526 | subst_ i dx x (a, b) = (subst_ i dx x a, subst_ i dx x b) | ||
527 | |||
528 | varType' :: Int -> [Exp] -> Exp | 539 | varType' :: Int -> [Exp] -> Exp |
529 | varType' i vs = vs !! i | 540 | varType' i vs = vs !! i |
530 | 541 | ||
@@ -535,8 +546,11 @@ pattern Closed a <- a where Closed a = closedExp a | |||
535 | class ClosedExp a where | 546 | class ClosedExp a where |
536 | closedExp :: a -> a | 547 | closedExp :: a -> a |
537 | 548 | ||
538 | instance (ClosedExp a, ClosedExp b) => ClosedExp (a, b) where | 549 | instance ClosedExp ExpType where |
539 | closedExp (a, b) = (closedExp a, closedExp b) | 550 | closedExp (ET a b) = ET (closedExp a) (closedExp b) |
551 | |||
552 | --instance (ClosedExp a, ClosedExp b) => ClosedExp (a, b) where | ||
553 | -- closedExp (a, b) = (closedExp a, closedExp b) | ||
540 | 554 | ||
541 | instance ClosedExp Exp where | 555 | instance ClosedExp Exp where |
542 | closedExp = \case | 556 | closedExp = \case |
@@ -569,7 +583,7 @@ class MkDoc a where | |||
569 | mkDoc :: Bool {-print reduced-} -> a -> Doc | 583 | mkDoc :: Bool {-print reduced-} -> a -> Doc |
570 | 584 | ||
571 | instance MkDoc ExpType where | 585 | instance MkDoc ExpType where |
572 | mkDoc pr e = mkDoc pr $ fst e | 586 | mkDoc pr (ET e _) = mkDoc pr e |
573 | 587 | ||
574 | instance MkDoc Exp where | 588 | instance MkDoc Exp where |
575 | mkDoc pr e = green $ f e | 589 | mkDoc pr e = green $ f e |
@@ -595,7 +609,7 @@ instance MkDoc Neutral where | |||
595 | where | 609 | where |
596 | g = mkDoc pr | 610 | g = mkDoc pr |
597 | f = \case | 611 | f = \case |
598 | CstrT' t a b -> shCstr (g (a, t)) (g (b, t)) | 612 | CstrT' t a b -> shCstr (g (ET a t)) (g (ET b t)) |
599 | FL' a | pr -> g a | 613 | FL' a | pr -> g a |
600 | Fun' s vs (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (pShow s) (g <$> xs) | 614 | Fun' s vs (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (pShow s) (g <$> xs) |
601 | Var_ k -> shVar k | 615 | Var_ k -> shVar k |
@@ -799,7 +813,7 @@ instance NType TyCaseFunName where nType (TyCaseFunName _ t) = t | |||
799 | conType (snd . getParams . unfixlabel -> TyCon (TyConName _ _ _ cs _) _) (ConName _ n t) = t --snd $ cs !! n | 813 | conType (snd . getParams . unfixlabel -> TyCon (TyConName _ _ _ cs _) _) (ConName _ n t) = t --snd $ cs !! n |
800 | 814 | ||
801 | mkExpTypes t [] = [] | 815 | mkExpTypes t [] = [] |
802 | mkExpTypes t@(Pi _ a _) (x: xs) = (x, t): mkExpTypes (appTy t x) xs | 816 | mkExpTypes t@(Pi _ a _) (x: xs) = ET x t: mkExpTypes (appTy t x) xs |
803 | 817 | ||
804 | appTy (Pi _ a b) x = subst 0 x b | 818 | appTy (Pi _ a b) x = subst 0 x b |
805 | appTy t x = error $ "appTy: " ++ ppShow t | 819 | appTy t x = error $ "appTy: " ++ ppShow t |
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs index c5497a98..5d8ea414 100644 --- a/src/LambdaCube/Compiler/CoreToIR.hs +++ b/src/LambdaCube/Compiler/CoreToIR.hs | |||
@@ -835,7 +835,7 @@ expOf (ExpTV x _ _) = x | |||
835 | mapVal f (ExpTV a b c) = ExpTV (f a) b c | 835 | mapVal f (ExpTV a b c) = ExpTV (f a) b c |
836 | 836 | ||
837 | toExp :: ExpType -> ExpTV | 837 | toExp :: ExpType -> ExpTV |
838 | toExp (x, xt) = ExpTV x xt [] | 838 | toExp (ET x xt) = ExpTV x xt [] |
839 | 839 | ||
840 | pattern Pi h a b <- (mkPi . mapVal unLab' -> Just (h, a, b)) | 840 | pattern Pi h a b <- (mkPi . mapVal unLab' -> Just (h, a, b)) |
841 | pattern Lam h a b <- (mkLam . mapVal unFunc' -> Just (h, a, b)) | 841 | pattern Lam h a b <- (mkLam . mapVal unFunc' -> Just (h, a, b)) |
@@ -874,7 +874,7 @@ mkApp (ExpTV (Neut (I.App_ a b)) et vs) = Just (ExpTV (Neut a) t vs, head $ chai | |||
874 | mkApp _ = Nothing | 874 | mkApp _ = Nothing |
875 | 875 | ||
876 | mkFunc r@(ExpTV (I.Func (show -> n) def nt xs) ty vs) | all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n | 876 | mkFunc r@(ExpTV (I.Func (show -> n) def nt xs) ty vs) | all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n |
877 | = Just (untick n +++ intercalate "_" (filter (/="TT") $ map (filter isAlphaNum . plainShow) hs), toExp (foldl app_ def hs, foldl appTy nt hs), tyOf r, xs') | 877 | = Just (untick n +++ intercalate "_" (filter (/="TT") $ map (filter isAlphaNum . plainShow) hs), toExp $ ET (foldl app_ def hs) (foldl appTy nt hs), tyOf r, xs') |
878 | where | 878 | where |
879 | a +++ [] = a | 879 | a +++ [] = a |
880 | a +++ b = a ++ "_" ++ b | 880 | a +++ b = a ++ "_" ++ b |
diff --git a/src/LambdaCube/Compiler/DeBruijn.hs b/src/LambdaCube/Compiler/DeBruijn.hs index 83e81188..2bc6cfdd 100644 --- a/src/LambdaCube/Compiler/DeBruijn.hs +++ b/src/LambdaCube/Compiler/DeBruijn.hs | |||
@@ -53,10 +53,6 @@ class Up{-TODO: rename-} a where | |||
53 | usedVar :: Int -> a -> Bool | 53 | usedVar :: Int -> a -> Bool |
54 | usedVar = (getAny .) . foldVar ((Any .) . (==)) | 54 | usedVar = (getAny .) . foldVar ((Any .) . (==)) |
55 | 55 | ||
56 | instance (Up a, Up b) => Up (a, b) where | ||
57 | usedVar i (a, b) = usedVar i a || usedVar i b | ||
58 | foldVar f i (a, b) = foldVar f i a <> foldVar f i b | ||
59 | |||
60 | instance Up Void where | 56 | instance Up Void where |
61 | foldVar _ _ = elimVoid | 57 | foldVar _ _ = elimVoid |
62 | 58 | ||
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 2b962b8d..0faf0b4d 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -17,7 +17,7 @@ | |||
17 | -- {-# OPTIONS_GHC -O0 #-} | 17 | -- {-# OPTIONS_GHC -O0 #-} |
18 | module LambdaCube.Compiler.Infer | 18 | module LambdaCube.Compiler.Infer |
19 | ( SName, Lit(..), Visibility(..) | 19 | ( SName, Lit(..), Visibility(..) |
20 | , Exp (..), Neutral (..), ExpType, GlobalEnv | 20 | , Exp (..), Neutral (..), ExpType(..), GlobalEnv |
21 | , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, app_, pattern TType | 21 | , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, app_, pattern TType |
22 | , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun, pattern ELit, pattern Func, pattern FL, pattern UFL, unFunc_ | 22 | , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun, pattern ELit, pattern Func, pattern FL, pattern UFL, unFunc_ |
23 | , outputType, boolType, trueExp | 23 | , outputType, boolType, trueExp |
@@ -66,9 +66,9 @@ makeCaseFunPars' te n = case neutType' te n of | |||
66 | 66 | ||
67 | varType :: String -> Int -> Env -> (Binder, Exp) | 67 | varType :: String -> Int -> Env -> (Binder, Exp) |
68 | varType err n_ env = f n_ env where | 68 | varType err n_ env = f n_ env where |
69 | f n (EAssign i (x, _) es) = second (subst i x) $ f (if n < i then n else n+1) es | 69 | f n (EAssign i (ET x _) es) = second (subst i x) $ f (if n < i then n else n+1) es |
70 | f n (EBind2 b t es) = if n == 0 then (b, up 1 t) else second (up 1) $ f (n-1) es | 70 | f n (EBind2 b t es) = if n == 0 then (b, up 1 t) else second (up 1) $ f (n-1) es |
71 | f n (ELet2 _ (x, t) es) = if n == 0 then (BLam Visible{-??-}, up 1 t) else second (up 1) $ f (n-1) es | 71 | f n (ELet2 _ (ET x t) es) = if n == 0 then (BLam Visible{-??-}, up 1 t) else second (up 1) $ f (n-1) es |
72 | f n e = either (error $ "varType: " ++ err ++ "\n" ++ show n_ ++ "\n" ++ ppShow env) (f n) $ parent e | 72 | f n e = either (error $ "varType: " ++ err ++ "\n" ++ show n_ ++ "\n" ++ ppShow env) (f n) $ parent e |
73 | 73 | ||
74 | 74 | ||
@@ -86,20 +86,20 @@ showEnvSExp :: (PShow a, Up a) => Env -> SExp' a -> String | |||
86 | showEnvSExp e c = show $ envDoc e $ underline $ pShow c | 86 | showEnvSExp e c = show $ envDoc e $ underline $ pShow c |
87 | 87 | ||
88 | showEnvSExpType :: (PShow a, Up a) => Env -> SExp' a -> Exp -> String | 88 | showEnvSExpType :: (PShow a, Up a) => Env -> SExp' a -> Exp -> String |
89 | showEnvSExpType e c t = show $ envDoc e $ underline $ (shAnn (pShow c) (mkDoc False (t, TType))) | 89 | showEnvSExpType e c t = show $ envDoc e $ underline $ (shAnn (pShow c) (mkDoc False (ET t TType))) |
90 | 90 | ||
91 | envDoc :: Env -> Doc -> Doc | 91 | envDoc :: Env -> Doc -> Doc |
92 | envDoc x m = case x of | 92 | envDoc x m = case x of |
93 | EGlobal{} -> m | 93 | EGlobal{} -> m |
94 | EBind1 _ h ts b -> envDoc ts $ shLam (usedVar 0 b) h m (pShow b) | 94 | EBind1 _ h ts b -> envDoc ts $ shLam (usedVar 0 b) h m (pShow b) |
95 | EBind2 h a ts -> envDoc ts $ shLam True h (mkDoc False (a, TType)) m | 95 | EBind2 h a ts -> envDoc ts $ shLam True h (mkDoc False (ET a TType)) m |
96 | EApp1 _ h ts b -> envDoc ts $ shApp h m (pShow b) | 96 | EApp1 _ h ts b -> envDoc ts $ shApp h m (pShow b) |
97 | EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (text "tyType") m | 97 | EApp2 _ h (ET (Lam (Var 0)) (Pi Visible TType _)) ts -> envDoc ts $ shApp h (text "tyType") m |
98 | EApp2 _ h a ts -> envDoc ts $ shApp h (mkDoc False a) m | 98 | EApp2 _ h a ts -> envDoc ts $ shApp h (mkDoc False a) m |
99 | ELet1 _ ts b -> envDoc ts $ shLet_ m (pShow b) | 99 | ELet1 _ ts b -> envDoc ts $ shLet_ m (pShow b) |
100 | ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False x) m | 100 | ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False x) m |
101 | EAssign i x ts -> envDoc ts $ shLet i (mkDoc False x) m | 101 | EAssign i x ts -> envDoc ts $ shLet i (mkDoc False x) m |
102 | CheckType t ts -> envDoc ts $ shAnn m $ mkDoc False (t, TType) | 102 | CheckType t ts -> envDoc ts $ shAnn m $ mkDoc False (ET t TType) |
103 | CheckIType t ts -> envDoc ts $ shAnn m (text "??") -- mkDoc ts' t | 103 | CheckIType t ts -> envDoc ts $ shAnn m (text "??") -- mkDoc ts' t |
104 | -- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t | 104 | -- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t |
105 | CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m | 105 | CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m |
@@ -113,7 +113,7 @@ instance MkDoc (CEnv Exp) where | |||
113 | f = \case | 113 | f = \case |
114 | MEnd a -> mkDoc pr a | 114 | MEnd a -> mkDoc pr a |
115 | Meta a b -> shLam True BMeta (mkDoc pr a) (f b) | 115 | Meta a b -> shLam True BMeta (mkDoc pr a) (f b) |
116 | Assign i (x, _) e -> shLet i (mkDoc pr x) (f e) | 116 | Assign i (ET x _) e -> shLet i (mkDoc pr x) (f e) |
117 | 117 | ||
118 | 118 | ||
119 | 119 | ||
@@ -145,14 +145,14 @@ instance (Subst Exp a, Rearrange a) => Subst Exp (CEnv a) where | |||
145 | MEnd a -> MEnd $ subst_ i dx x a | 145 | MEnd a -> MEnd $ subst_ i dx x a |
146 | Meta a b -> Meta (subst_ i dx x a) (subst_ (i+1) (upDB 1 dx) (up 1 x) b) | 146 | Meta a b -> Meta (subst_ i dx x a) (subst_ (i+1) (upDB 1 dx) (up 1 x) b) |
147 | Assign j a b | 147 | Assign j a b |
148 | | j > i, Just a' <- down i a -> assign (j-1) a' (subst i (subst (j-1) (fst a') x) b) | 148 | | j > i, Just a' <- down i a -> assign (j-1) a' (subst i (subst (j-1) (expr a') x) b) |
149 | | j > i, Just x' <- down (j-1) x -> assign (j-1) (subst i x' a) (subst i x' b) | 149 | | j > i, Just x' <- down (j-1) x -> assign (j-1) (subst i x' a) (subst i x' b) |
150 | | j < i, Just a' <- down (i-1) a -> assign j a' (subst (i-1) (subst j (fst a') x) b) | 150 | | j < i, Just a' <- down (i-1) a -> assign j a' (subst (i-1) (subst j (expr a') x) b) |
151 | | j < i, Just x' <- down j x -> assign j (subst (i-1) x' a) (subst (i-1) x' b) | 151 | | j < i, Just x' <- down j x -> assign j (subst (i-1) x' a) (subst (i-1) x' b) |
152 | | j == i -> Meta (cstr (snd a) x $ fst a) $ up1_ 0 b | 152 | | j == i -> Meta (cstr (ty a) x $ expr a) $ up1_ 0 b |
153 | 153 | ||
154 | --swapAssign :: (Int -> Exp -> CEnv Exp -> a) -> (Int -> Exp -> CEnv Exp -> a) -> Int -> Exp -> CEnv Exp -> a | 154 | --swapAssign :: (Int -> Exp -> CEnv Exp -> a) -> (Int -> Exp -> CEnv Exp -> a) -> Int -> Exp -> CEnv Exp -> a |
155 | swapAssign _ clet i (Var j, t) b | i > j = clet j (Var (i-1), t) $ subst j (Var (i-1)) $ up1_ i b | 155 | swapAssign _ clet i (ET (Var j) t) b | i > j = clet j (ET (Var (i-1)) t) $ subst j (Var (i-1)) $ up1_ i b |
156 | swapAssign clet _ i a b = clet i a b | 156 | swapAssign clet _ i a b = clet i a b |
157 | 157 | ||
158 | --assign :: Rearrange a => Int -> ExpType -> CEnv a -> CEnv a | 158 | --assign :: Rearrange a => Int -> ExpType -> CEnv a -> CEnv a |
@@ -254,7 +254,7 @@ instance PShow ErrorMsg where | |||
254 | -- inference monad | 254 | -- inference monad |
255 | type IM m = ExceptT ErrorMsg (ReaderT (Extensions, GlobalEnv) (WriterT Infos m)) | 255 | type IM m = ExceptT ErrorMsg (ReaderT (Extensions, GlobalEnv) (WriterT Infos m)) |
256 | 256 | ||
257 | expAndType s (e, t, si) = (e, t) | 257 | expAndType s (e, t, si) = (ET e t) |
258 | 258 | ||
259 | -- todo: do only if NoTypeNamespace extension is not on | 259 | -- todo: do only if NoTypeNamespace extension is not on |
260 | lookupName s@(Ticked s') m = expAndType s <$> (Map.lookup s m `mplus` Map.lookup s' m) | 260 | lookupName s@(Ticked s') m = expAndType s <$> (Map.lookup s m `mplus` Map.lookup s' m) |
@@ -287,8 +287,8 @@ inferN_ tellTrace = infer where | |||
287 | Parens x -> infer te x | 287 | Parens x -> infer te x |
288 | SAnn x t -> checkN (CheckIType x te) t TType | 288 | SAnn x t -> checkN (CheckIType x te) t TType |
289 | SRHS x -> infer (ERHS te) x | 289 | SRHS x -> infer (ERHS te) x |
290 | SVar sn i -> focusTell te exp (Var i, snd $ varType "C2" i te) | 290 | SVar sn i -> focusTell te exp $ ET (Var i) $ snd $ varType "C2" i te |
291 | SLit si l -> focusTell te exp (ELit l, litType l) | 291 | SLit si l -> focusTell te exp $ ET (ELit l) (litType l) |
292 | STyped et -> focusTell' te exp et | 292 | STyped et -> focusTell' te exp et |
293 | SGlobal (SIName si s) -> focusTell te exp =<< getDef te si s | 293 | SGlobal (SIName si s) -> focusTell te exp =<< getDef te si s |
294 | SLet le a b -> infer (ELet1 le te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a) | 294 | SLet le a b -> infer (ELet1 le te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a) |
@@ -301,14 +301,14 @@ inferN_ tellTrace = infer where | |||
301 | checkN_ te (Parens e) t = checkN_ te e t | 301 | checkN_ te (Parens e) t = checkN_ te e t |
302 | checkN_ te e t | 302 | checkN_ te e t |
303 | | x@(SGlobal (sName -> MatchName n)) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` SVar siv v `SAppV` b <- e | 303 | | x@(SGlobal (sName -> MatchName n)) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` SVar siv v `SAppV` b <- e |
304 | = infer te $ x `SAppV` SLam Visible SType (STyped (subst (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` SVar siv v `SAppV` b | 304 | = infer te $ x `SAppV` SLam Visible SType (STyped (ET (subst (v+1) (Var 0) $ up 1 t) TType)) `SAppV` a `SAppV` SVar siv v `SAppV` b |
305 | -- temporal hack | 305 | -- temporal hack |
306 | | x@(SGlobal (sName -> CaseName "'Nat")) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` b `SAppV` SVar siv v <- e | 306 | | x@(SGlobal (sName -> CaseName "'Nat")) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` b `SAppV` SVar siv v <- e |
307 | = infer te $ x `SAppV` SLamV (STyped (substTo (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` b `SAppV` SVar siv v | 307 | = infer te $ x `SAppV` SLamV (STyped (ET (substTo (v+1) (Var 0) $ up 1 t) TType)) `SAppV` a `SAppV` b `SAppV` SVar siv v |
308 | -- temporal hack | 308 | -- temporal hack |
309 | | x@(SGlobal (sName -> CaseName "'VecS")) `SAppV` SLamV (SLamV (Wildcard _)) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v <- e | 309 | | x@(SGlobal (sName -> CaseName "'VecS")) `SAppV` SLamV (SLamV (Wildcard _)) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v <- e |
310 | , TyConN FVecS [_, Var n'] <- snd $ varType "xx" v te | 310 | , TyConN FVecS [_, Var n'] <- snd $ varType "xx" v te |
311 | = infer te $ x `SAppV` SLamV (SLamV (STyped (substTo (n'+2) (Var 1) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v | 311 | = infer te $ x `SAppV` SLamV (SLamV (STyped (ET (substTo (n'+2) (Var 1) $ up 2 t) TType))) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v |
312 | 312 | ||
313 | {- | 313 | {- |
314 | -- temporal hack | 314 | -- temporal hack |
@@ -321,7 +321,7 @@ inferN_ tellTrace = infer where | |||
321 | | SLam h a b <- e, Pi h' x y <- t, h == h' = do | 321 | | SLam h a b <- e, Pi h' x y <- t, h == h' = do |
322 | -- tellType e t | 322 | -- tellType e t |
323 | let same = checkSame te a x | 323 | let same = checkSame te a x |
324 | if same then checkN (EBind2 (BLam h) x te) b y else error $ "checkSame:\n" ++ ppShow a ++ "\nwith\n" ++ showEnvExp te (x, TType) | 324 | if same then checkN (EBind2 (BLam h) x te) b y else error $ "checkSame:\n" ++ ppShow a ++ "\nwith\n" ++ showEnvExp te (ET x TType) |
325 | | Pi Hidden a b <- t = do | 325 | | Pi Hidden a b <- t = do |
326 | bb <- notHiddenLam e | 326 | bb <- notHiddenLam e |
327 | if bb then checkN (EBind2 (BLam Hidden) a te) (up1 e) b | 327 | if bb then checkN (EBind2 (BLam Hidden) a te) (up1 e) b |
@@ -334,7 +334,7 @@ inferN_ tellTrace = infer where | |||
334 | SGlobal (sName -> s) -> do | 334 | SGlobal (sName -> s) -> do |
335 | nv <- asks snd | 335 | nv <- asks snd |
336 | case fromMaybe (error $ "infer: can't find: " ++ s) $ lookupName s nv of | 336 | case fromMaybe (error $ "infer: can't find: " ++ s) $ lookupName s nv of |
337 | (Lam _, Pi Hidden _ _) -> return False | 337 | ET (Lam _) (Pi Hidden _ _) -> return False |
338 | _ -> return True | 338 | _ -> return True |
339 | _ -> return False | 339 | _ -> return False |
340 | {- | 340 | {- |
@@ -347,18 +347,18 @@ inferN_ tellTrace = infer where | |||
347 | checkSame te (Wildcard _) a = True | 347 | checkSame te (Wildcard _) a = True |
348 | checkSame te (SGlobal (sName -> "'Type")) TType = True | 348 | checkSame te (SGlobal (sName -> "'Type")) TType = True |
349 | checkSame te SType TType = True | 349 | checkSame te SType TType = True |
350 | checkSame te (SBind_ _ BMeta _ SType (STyped (Var 0, _))) a = True | 350 | checkSame te (SBind_ _ BMeta _ SType (STyped (ET (Var 0) _))) a = True |
351 | checkSame te a b = error $ "checkSame: " ++ ppShow (a, b) | 351 | checkSame te a b = error $ "checkSame: " ++ ppShow (a, b) |
352 | 352 | ||
353 | hArgs (Pi Hidden _ b) = 1 + hArgs b | 353 | hArgs (Pi Hidden _ b) = 1 + hArgs b |
354 | hArgs _ = 0 | 354 | hArgs _ = 0 |
355 | 355 | ||
356 | focusTell env si eet = tellType si (snd eet) >> focus_ env eet | 356 | focusTell env si eet = tellType si (ty eet) >> focus_ env eet |
357 | focusTell' env si eet = focus_ env eet | 357 | focusTell' env si eet = focus_ env eet |
358 | 358 | ||
359 | focus_ :: Env -> ExpType -> IM m ExpType' | 359 | focus_ :: Env -> ExpType -> IM m ExpType' |
360 | focus_ env eet@(e, et) = tellTrace "focus" (showEnvExp env eet) $ case env of | 360 | focus_ env eet@(ET e et) = tellTrace "focus" (showEnvExp env eet) $ case env of |
361 | ERHS te -> focus_ te (RHS e, et) | 361 | ERHS te -> focus_ te (ET (RHS e) et) |
362 | -- CheckSame x te -> focus_ (EBind2_ (debugSI "focus_ CheckSame") BMeta (cstr x e) te) $ up 1 eet | 362 | -- CheckSame x te -> focus_ (EBind2_ (debugSI "focus_ CheckSame") BMeta (cstr x e) te) $ up 1 eet |
363 | CheckAppType si h t te b -- App1 h (CheckType t te) b | 363 | CheckAppType si h t te b -- App1 h (CheckType t te) b |
364 | | Pi h' x (down 0 -> Just y) <- et, h == h' -> case t of | 364 | | Pi h' x (down 0 -> Just y) <- et, h == h' -> case t of |
@@ -372,8 +372,8 @@ inferN_ tellTrace = infer where | |||
372 | -- | CheckAppType Hidden _ te' _ <- te -> error "ok" | 372 | -- | CheckAppType Hidden _ te' _ <- te -> error "ok" |
373 | | otherwise -> infer (CheckType_ (sourceInfo b) (Var 2) $ cstr' h (up 2 et) (Pi Visible (Var 1) (Var 1)) (up 2 e) $ EBind2_ (sourceInfo b) BMeta TType $ EBind2_ (sourceInfo b) BMeta TType te) (up 3 b) | 373 | | otherwise -> infer (CheckType_ (sourceInfo b) (Var 2) $ cstr' h (up 2 et) (Pi Visible (Var 1) (Var 1)) (up 2 e) $ EBind2_ (sourceInfo b) BMeta TType $ EBind2_ (sourceInfo b) BMeta TType te) (up 3 b) |
374 | where | 374 | where |
375 | cstr' h x y e = EApp2 mempty h (evalCoe (up 1 x) (up 1 y) (Var 0) (up 1 e), up 1 y) . EBind2_ (sourceInfo b) BMeta (cstr TType x y) | 375 | cstr' h x y e = EApp2 mempty h (ET (evalCoe (up 1 x) (up 1 y) (Var 0) (up 1 e)) (up 1 y)) . EBind2_ (sourceInfo b) BMeta (cstr TType x y) |
376 | ELet2 ln (x{-let-}, xt) te -> focus_ te $ subst 0 (mkELet ln x xt){-let-} eet{-in-} | 376 | ELet2 ln (ET x{-let-} xt) te -> focus_ te $ subst 0 (mkELet ln x xt){-let-} eet{-in-} |
377 | CheckIType x te -> checkN te x e | 377 | CheckIType x te -> checkN te x e |
378 | CheckType_ si t te | 378 | CheckType_ si t te |
379 | | hArgs et > hArgs t | 379 | | hArgs et > hArgs t |
@@ -381,10 +381,10 @@ inferN_ tellTrace = infer where | |||
381 | | hArgs et < hArgs t, Pi Hidden t1 t2 <- t | 381 | | hArgs et < hArgs t, Pi Hidden t1 t2 <- t |
382 | -> focus_ (CheckType_ si t2 $ EBind2 (BLam Hidden) t1 te) eet | 382 | -> focus_ (CheckType_ si t2 $ EBind2 (BLam Hidden) t1 te) eet |
383 | | otherwise -> focus_ (EBind2_ si BMeta (cstr TType t et) te) $ up 1 eet | 383 | | otherwise -> focus_ (EBind2_ si BMeta (cstr TType t et) te) $ up 1 eet |
384 | EApp2 si h (a, at) te -> focusTell te si (app_ a e, appTy at e) -- h?? | 384 | EApp2 si h (ET a at) te -> focusTell te si $ ET (app_ a e) (appTy at e) -- h?? |
385 | EBind1 si h te b -> infer (EBind2_ (sourceInfo b) h e te) b | 385 | EBind1 si h te b -> infer (EBind2_ (sourceInfo b) h e te) b |
386 | EBind2_ si (BLam h) a te -> focus_ te $ lamPi h a eet | 386 | EBind2_ si (BLam h) a te -> focus_ te $ lamPi h a eet |
387 | EBind2_ si (BPi h) a te -> focusTell te si (Pi h a e, TType) | 387 | EBind2_ si (BPi h) a te -> focusTell te si $ ET (Pi h a e) TType |
388 | _ -> focus2 env $ MEnd eet | 388 | _ -> focus2 env $ MEnd eet |
389 | 389 | ||
390 | focus2 :: Env -> CEnv ExpType -> IM m ExpType' | 390 | focus2 :: Env -> CEnv ExpType -> IM m ExpType' |
@@ -397,16 +397,16 @@ inferN_ tellTrace = infer where | |||
397 | | Empty msg <- tt -> throwError' $ ETypeError (text msg) si | 397 | | Empty msg <- tt -> throwError' $ ETypeError (text msg) si |
398 | | T2 x y <- tt, let te' = EBind2_ si BMeta (up 1 y) $ EBind2_ si BMeta x te | 398 | | T2 x y <- tt, let te' = EBind2_ si BMeta (up 1 y) $ EBind2_ si BMeta x te |
399 | -> refocus te' $ subst 2 (t2C (Var 1) (Var 0)) $ up 2 eet | 399 | -> refocus te' $ subst 2 (t2C (Var 1) (Var 0)) $ up 2 eet |
400 | | CstrT t a b <- tt, Just r <- cst (a, t) b -> r | 400 | | CstrT t a b <- tt, Just r <- cst (ET a t) b -> r |
401 | | CstrT t a b <- tt, Just r <- cst (b, t) a -> r | 401 | | CstrT t a b <- tt, Just r <- cst (ET b t) a -> r |
402 | | isCstr tt, EBind2 h x te' <- te{-, h /= BMeta todo: remove-}, Just x' <- down 0 tt, x == x' | 402 | | isCstr tt, EBind2 h x te' <- te{-, h /= BMeta todo: remove-}, Just x' <- down 0 tt, x == x' |
403 | -> refocus te $ subst 1 (Var 0) eet | 403 | -> refocus te $ subst 1 (Var 0) eet |
404 | | EBind2_ si' h x te' <- te, h /= BMeta, Just b' <- down 0 tt | 404 | | EBind2_ si' h x te' <- te, h /= BMeta, Just b' <- down 0 tt |
405 | -> refocus (EBind2_ si' h (up 1 x) $ EBind2_ si BMeta b' te') $ subst 2 (Var 0) $ up 1 eet | 405 | -> refocus (EBind2_ si' h (up 1 x) $ EBind2_ si BMeta b' te') $ subst 2 (Var 0) $ up 1 eet |
406 | | ELet2 le (x, xt) te' <- te, Just b' <- down 0 tt | 406 | | ELet2 le x te' <- te, Just b' <- down 0 tt |
407 | -> refocus (ELet2 le (up 1 x, up 1 xt) $ EBind2_ si BMeta b' te') $ subst 2 (Var 0) $ up 1 eet | 407 | -> refocus (ELet2 le (up 1 x) $ EBind2_ si BMeta b' te') $ subst 2 (Var 0) $ up 1 eet |
408 | | EBind1 si h te' x <- te -> refocus (EBind1 si h (EBind2_ si BMeta tt_ te') $ up1_ 1 x) eet | 408 | | EBind1 si h te' x <- te -> refocus (EBind1 si h (EBind2_ si BMeta tt_ te') $ up1_ 1 x) eet |
409 | | ELet1 le te' x <- te, floatLetMeta $ snd $ replaceMetas' $ Meta tt_ $ eet | 409 | | ELet1 le te' x <- te, floatLetMeta $ ty $ replaceMetas' $ Meta tt_ eet |
410 | -> refocus (ELet1 le (EBind2_ si BMeta tt_ te') $ up1_ 1 x) eet | 410 | -> refocus (ELet1 le (EBind2_ si BMeta tt_ te') $ up1_ 1 x) eet |
411 | | CheckAppType si h t te' x <- te -> refocus (CheckAppType si h (up 1 t) (EBind2_ si BMeta tt_ te') $ up1 x) eet | 411 | | CheckAppType si h t te' x <- te -> refocus (CheckAppType si h (up 1 t) (EBind2_ si BMeta tt_ te') $ up1 x) eet |
412 | | EApp1 si h te' x <- te -> refocus (EApp1 si h (EBind2_ si BMeta tt_ te') $ up1 x) eet | 412 | | EApp1 si h te' x <- te -> refocus (EApp1 si h (EBind2_ si BMeta tt_ te') $ up1 x) eet |
@@ -421,23 +421,23 @@ inferN_ tellTrace = infer where | |||
421 | cst x = \case | 421 | cst x = \case |
422 | Var i | fst (varType "X" i te) == BMeta | 422 | Var i | fst (varType "X" i te) == BMeta |
423 | , Just y <- down i x | 423 | , Just y <- down i x |
424 | -> Just $ join swapAssign (\i x -> refocus $ EAssign i x te) i y $ subst 0 {-ReflCstr y-}TT $ subst (i+1) (fst $ up 1 y) eet | 424 | -> Just $ join swapAssign (\i x -> refocus $ EAssign i x te) i y $ subst 0 {-ReflCstr y-}TT $ subst (i+1) (expr $ up 1 y) eet |
425 | _ -> Nothing | 425 | _ -> Nothing |
426 | 426 | ||
427 | EAssign i b te -> case te of | 427 | EAssign i b te -> case te of |
428 | ERHS te' -> refocus' (ERHS $ EAssign i b te') eet | 428 | ERHS te' -> refocus' (ERHS $ EAssign i b te') eet |
429 | EBind2_ si h x te' | i > 0, Just b' <- down 0 b | 429 | EBind2_ si h x te' | i > 0, Just b' <- down 0 b |
430 | -> refocus' (EBind2_ si h (subst (i-1) (fst b') x) (EAssign (i-1) b' te')) eet | 430 | -> refocus' (EBind2_ si h (subst (i-1) (expr b') x) (EAssign (i-1) b' te')) eet |
431 | ELet2 le (x, xt) te' | i > 0, Just b' <- down 0 b | 431 | ELet2 le x te' | i > 0, Just b' <- down 0 b |
432 | -> refocus' (ELet2 le (subst (i-1) (fst b') x, subst (i-1) (fst b') xt) (EAssign (i-1) b' te')) eet | 432 | -> refocus' (ELet2 le (subst (i-1) (expr b') x) (EAssign (i-1) b' te')) eet |
433 | ELet1 le te' x -> refocus' (ELet1 le (EAssign i b te') $ subst (i+1) (up 1 b) x) eet | 433 | ELet1 le te' x -> refocus' (ELet1 le (EAssign i b te') $ subst (i+1) (up 1 b) x) eet |
434 | EBind1 si h te' x -> refocus' (EBind1 si h (EAssign i b te') $ subst (i+1) (up 1 b) x) eet | 434 | EBind1 si h te' x -> refocus' (EBind1 si h (EAssign i b te') $ subst (i+1) (up 1 b) x) eet |
435 | CheckAppType si h t te' x -> refocus' (CheckAppType si h (subst i (fst b) t) (EAssign i b te') $ subst i b x) eet | 435 | CheckAppType si h t te' x -> refocus' (CheckAppType si h (subst i (expr b) t) (EAssign i b te') $ subst i b x) eet |
436 | EApp1 si h te' x -> refocus' (EApp1 si h (EAssign i b te') $ subst i b x) eet | 436 | EApp1 si h te' x -> refocus' (EApp1 si h (EAssign i b te') $ subst i b x) eet |
437 | EApp2 si h x te' -> refocus' (EApp2 si h (subst i (fst b) x) $ EAssign i b te') eet | 437 | EApp2 si h x te' -> refocus' (EApp2 si h (subst i (expr b) x) $ EAssign i b te') eet |
438 | CheckType_ si t te' -> refocus' (CheckType_ si (subst i (fst b) t) $ EAssign i b te') eet | 438 | CheckType_ si t te' -> refocus' (CheckType_ si (subst i (expr b) t) $ EAssign i b te') eet |
439 | EAssign j a te' | i < j | 439 | EAssign j a te' | i < j |
440 | -> refocus' (EAssign (j-1) (subst i (fst b) a) $ EAssign i (up1_ (j-1) b) te') eet | 440 | -> refocus' (EAssign (j-1) (subst i (expr b) a) $ EAssign i (up1_ (j-1) b) te') eet |
441 | t | Just te' <- pull1 i b te -> refocus' te' eet | 441 | t | Just te' <- pull1 i b te -> refocus' te' eet |
442 | | otherwise -> swapAssign (\i x -> focus2 te . Assign i x) (\i x -> refocus' $ EAssign i x te) i b eet | 442 | | otherwise -> swapAssign (\i x -> focus2 te . Assign i x) (\i x -> refocus' $ EAssign i x te) i b eet |
443 | -- todo: CheckSame Exp Env | 443 | -- todo: CheckSame Exp Env |
@@ -446,10 +446,10 @@ inferN_ tellTrace = infer where | |||
446 | 446 | ||
447 | pull1 i b = \case | 447 | pull1 i b = \case |
448 | EBind2_ si h x te | i > 0, Just b' <- down 0 b | 448 | EBind2_ si h x te | i > 0, Just b' <- down 0 b |
449 | -> EBind2_ si h (subst (i-1) (fst b') x) <$> pull1 (i-1) b' te | 449 | -> EBind2_ si h (subst (i-1) (expr b') x) <$> pull1 (i-1) b' te |
450 | EAssign j a te | 450 | EAssign j a te |
451 | | i < j -> EAssign (j-1) (subst i (fst b) a) <$> pull1 i (up1_ (j-1) b) te | 451 | | i < j -> EAssign (j-1) (subst i (expr b) a) <$> pull1 i (up1_ (j-1) b) te |
452 | | j <= i -> EAssign j (subst i (fst b) a) <$> pull1 (i+1) (up1_ j b) te | 452 | | j <= i -> EAssign j (subst i (expr b) a) <$> pull1 (i+1) (up1_ j b) te |
453 | te -> pull i te | 453 | te -> pull i te |
454 | 454 | ||
455 | pull i = \case | 455 | pull i = \case |
@@ -461,7 +461,7 @@ inferN_ tellTrace = infer where | |||
461 | EGlobal{} -> return eet | 461 | EGlobal{} -> return eet |
462 | _ -> case eet of | 462 | _ -> case eet of |
463 | MEnd x -> throwError' $ ErrorMsg $ "focus todo:" <+> pShow x | 463 | MEnd x -> throwError' $ ErrorMsg $ "focus todo:" <+> pShow x |
464 | _ -> throwError' $ ErrorMsg $ "focus checkMetas:" <+> pShow env <$$> pShow (fst <$> eet) | 464 | _ -> throwError' $ ErrorMsg $ "focus checkMetas:" <+> pShow env <$$> pShow (expr <$> eet) |
465 | where | 465 | where |
466 | refocus_ :: (Env -> CEnv ExpType -> IM m ExpType') -> Env -> CEnv ExpType -> IM m ExpType' | 466 | refocus_ :: (Env -> CEnv ExpType -> IM m ExpType') -> Env -> CEnv ExpType -> IM m ExpType' |
467 | refocus_ _ e (MEnd at) = focus_ e at | 467 | refocus_ _ e (MEnd at) = focus_ e at |
@@ -470,11 +470,11 @@ inferN_ tellTrace = infer where | |||
470 | 470 | ||
471 | replaceMetas' = replaceMetas $ lamPi Hidden | 471 | replaceMetas' = replaceMetas $ lamPi Hidden |
472 | 472 | ||
473 | lamPi h = (***) <$> const Lam <*> Pi h | 473 | lamPi h t (ET x y) = ET (Lam x) (Pi h t y) |
474 | 474 | ||
475 | replaceMetas bind = \case | 475 | replaceMetas bind = \case |
476 | Meta a t -> bind a $ replaceMetas bind t | 476 | Meta a t -> bind a $ replaceMetas bind t |
477 | Assign i x t | x' <- up1_ i x -> bind (cstr (snd x') (Var i) $ fst x') . up 1 . up1_ i $ replaceMetas bind t | 477 | Assign i x t | x' <- up1_ i x -> bind (cstr (ty x') (Var i) $ expr x') . up 1 . up1_ i $ replaceMetas bind t |
478 | MEnd t -> t | 478 | MEnd t -> t |
479 | 479 | ||
480 | 480 | ||
@@ -491,7 +491,7 @@ recheck msg e = recheck' msg e | |||
491 | 491 | ||
492 | -- todo: check type also | 492 | -- todo: check type also |
493 | recheck' :: Message -> Env -> ExpType -> ExpType | 493 | recheck' :: Message -> Env -> ExpType -> ExpType |
494 | recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt) | 494 | recheck' msg' e (ET x xt) = ET (recheck_ "main" (checkEnv e) (ET x xt)) xt |
495 | where | 495 | where |
496 | checkEnv = \case | 496 | checkEnv = \case |
497 | e@EGlobal{} -> e | 497 | e@EGlobal{} -> e |
@@ -506,35 +506,35 @@ recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt) | |||
506 | -- CheckSame x e -> CheckSame (recheck'' "env" e x) $ checkEnv e | 506 | -- CheckSame x e -> CheckSame (recheck'' "env" e x) $ checkEnv e |
507 | CheckAppType si h x e y -> CheckAppType si h (checkType e x) (checkEnv e) y | 507 | CheckAppType si h x e y -> CheckAppType si h (checkType e x) (checkEnv e) y |
508 | 508 | ||
509 | recheck'' msg te a@(x, xt) = (recheck_ msg te a, xt) | 509 | recheck'' msg te a@(ET x xt) = ET (recheck_ msg te a) xt |
510 | checkType te e = recheck_ "check" te (e, TType) | 510 | checkType te e = recheck_ "check" te (ET e TType) |
511 | 511 | ||
512 | recheck_ msg te = \case | 512 | recheck_ msg te = \case |
513 | (Var k, zt) -> Var k -- todo: check var type | 513 | ET (Var k) zt -> Var k -- todo: check var type |
514 | (Lam_ md b, Pi h a bt) -> Lam_ md $ recheck_ "9" (EBind2 (BLam h) a te) (b, bt) | 514 | ET (Lam_ md b) (Pi h a bt) -> Lam_ md $ recheck_ "9" (EBind2 (BLam h) a te) (ET b bt) |
515 | (Pi_ md h a b, TType) -> Pi_ md h (checkType te a) $ checkType (EBind2 (BPi h) a te) b | 515 | ET (Pi_ md h a b) TType -> Pi_ md h (checkType te a) $ checkType (EBind2 (BPi h) a te) b |
516 | (ELit l, zt) -> ELit l -- todo: check literal type | 516 | ET (ELit l) zt -> ELit l -- todo: check literal type |
517 | (TType, TType) -> TType | 517 | ET TType TType -> TType |
518 | (Neut (App__ md a b), zt) | 518 | ET (Neut (App__ md a b)) zt |
519 | | (Neut a', at) <- recheck'' "app1" te (Neut a, neutType te a) | 519 | | ET (Neut a') at <- recheck'' "app1" te $ ET (Neut a) (neutType te a) |
520 | -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b] | 520 | -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b] |
521 | (Con_ md s n as, zt) -> checkApps (ppShow s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as | 521 | ET (Con_ md s n as) zt -> checkApps (ppShow s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as |
522 | (TyCon_ md s as, zt) -> checkApps (ppShow s) [] zt (TyCon_ md s) te (nType s) as | 522 | ET (TyCon_ md s as) zt -> checkApps (ppShow s) [] zt (TyCon_ md s) te (nType s) as |
523 | (CaseFun s@(CaseFunName _ t pars) as n, zt) -> checkApps (ppShow s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n]) | 523 | ET (CaseFun s@(CaseFunName _ t pars) as n) zt -> checkApps (ppShow s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n]) |
524 | (TyCaseFun s [m, t, f] n, zt) -> checkApps (ppShow s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f] | 524 | ET (TyCaseFun s [m, t, f] n) zt -> checkApps (ppShow s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f] |
525 | (Neut (Fun_ md f vs@[] a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun_ md f vs (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x | 525 | ET (Neut (Fun_ md f vs@[] a x)) zt -> checkApps "lab" [] zt (\xs -> Neut $ Fun_ md f vs (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x |
526 | -- TODO | 526 | -- TODO |
527 | (r@(Neut (Fun' f vs a x)), zt) -> r | 527 | ET r@(Neut (Fun' f vs a x)) zt -> r |
528 | (RHS x, zt) -> RHS $ recheck_ msg te (x, zt) | 528 | ET (RHS x) zt -> RHS $ recheck_ msg te (ET x zt) |
529 | (Delta, zt) -> Delta | 529 | ET Delta zt -> Delta |
530 | where | 530 | where |
531 | checkApps s acc zt f _ t [] | 531 | checkApps s acc zt f _ t [] |
532 | | t == zt = f $ reverse acc | 532 | | t == zt = f $ reverse acc |
533 | | otherwise = | 533 | | otherwise = |
534 | error $ "checkApps' " ++ s ++ " " ++ msg ++ "\n" ++ showEnvExp te{-todo-} (t, TType) ++ "\n\n" ++ showEnvExp te (zt, TType) | 534 | error $ "checkApps' " ++ s ++ " " ++ msg ++ "\n" ++ showEnvExp te{-todo-} (ET t TType) ++ "\n\n" ++ showEnvExp te (ET zt TType) |
535 | checkApps s acc zt f te t@(unfixlabel -> Pi h x y) (b_: xs) = checkApps (s++"+") (b: acc) zt f te (appTy t b) xs where b = recheck_ "checkApps" te (b_, x) | 535 | checkApps s acc zt f te t@(unfixlabel -> Pi h x y) (b_: xs) = checkApps (s++"+") (b: acc) zt f te (appTy t b) xs where b = recheck_ "checkApps" te (ET b_ x) |
536 | checkApps s acc zt f te t _ = | 536 | checkApps s acc zt f te t _ = |
537 | error $ "checkApps " ++ s ++ " " ++ msg ++ "\n" ++ showEnvExp te{-todo-} (t, TType) ++ "\n\n" ++ showEnvExp e (x, xt) | 537 | error $ "checkApps " ++ s ++ " " ++ msg ++ "\n" ++ showEnvExp te{-todo-} (ET t TType) ++ "\n\n" ++ showEnvExp e (ET x xt) |
538 | 538 | ||
539 | -- Ambiguous: (Int ~ F a) => Int | 539 | -- Ambiguous: (Int ~ F a) => Int |
540 | -- Not ambiguous: (Show a, a ~ F b) => b | 540 | -- Not ambiguous: (Show a, a ~ F b) => b |
@@ -653,12 +653,12 @@ handleStmt = \case | |||
653 | Primitive n t_ -> do | 653 | Primitive n t_ -> do |
654 | t <- inferType $ trSExp' t_ | 654 | t <- inferType $ trSExp' t_ |
655 | tellType (sourceInfo n) t | 655 | tellType (sourceInfo n) t |
656 | addToEnv n $ flip (,) t $ lamify t $ Neut . DFun (FunName' (mkFName n) t) | 656 | addToEnv n $ flip ET t $ lamify t $ Neut . DFun (FunName' (mkFName n) t) |
657 | Let n mt t_ -> do | 657 | Let n mt t_ -> do |
658 | let t__ = maybe id (flip SAnn) mt t_ | 658 | let t__ = maybe id (flip SAnn) mt t_ |
659 | (x, t) <- inferTerm (sName n) $ trSExp' $ addFix n t__ | 659 | ET x t <- inferTerm (sName n) $ trSExp' $ addFix n t__ |
660 | tellType (sourceInfo n) t | 660 | tellType (sourceInfo n) t |
661 | addToEnv n (mkELet n x t, t) | 661 | addToEnv n (ET (mkELet n x t) t) |
662 | {- -- hack | 662 | {- -- hack |
663 | when (snd (getParams t) == TType) $ do | 663 | when (snd (getParams t) == TType) $ do |
664 | let ps' = fst $ getParams t | 664 | let ps' = fst $ getParams t |
@@ -683,15 +683,15 @@ handleStmt = \case | |||
683 | = do | 683 | = do |
684 | cty <- removeHiddenUnit <$> inferType (UncurryS [(Hidden, x) | (Visible, x) <- ps] ct) | 684 | cty <- removeHiddenUnit <$> inferType (UncurryS [(Hidden, x) | (Visible, x) <- ps] ct) |
685 | -- tellType (sourceInfo cn) cty | 685 | -- tellType (sourceInfo cn) cty |
686 | let pars = zipWith (\x -> second $ STyped . flip (,) TType . rUp (1+j) x) [0..] $ drop (length ps) $ fst $ getParams cty | 686 | let pars = zipWith (\x -> second $ STyped . flip ET TType . rUp (1+j) x) [0..] $ drop (length ps) $ fst $ getParams cty |
687 | act = length . fst . getParams $ cty | 687 | act = length . fst . getParams $ cty |
688 | acts = map fst . fst . getParams $ cty | 688 | acts = map fst . fst . getParams $ cty |
689 | conn = ConName (mkFName cn) j cty | 689 | conn = ConName (mkFName cn) j cty |
690 | e <- addToEnv cn (Con conn 0 [], cty) | 690 | e <- addToEnv cn $ ET (Con conn 0 []) cty |
691 | return (e, ((conn, cty) | 691 | return (e, ((conn, cty) |
692 | , UncurryS pars | 692 | , UncurryS pars |
693 | $ foldl SAppV (sVar ".cs" $ j + length pars) $ drop pnum' xs ++ [AppsS (SGlobal cn) (zip acts $ downToS ("a4 " ++ sName cn ++ " " ++ show (length ps)) (j+1+length pars) (length ps) ++ downToS "a5" 0 (act- length ps))] | 693 | $ (foldl SAppV (sVar ".cs" $ j + length pars) $ drop pnum' xs ++ [AppsS (SGlobal cn) (zip acts $ downToS ("a4 " ++ sName cn ++ " " ++ show (length ps)) (j+1+length pars) (length ps) ++ downToS "a5" 0 (act- length ps))] |
694 | )) | 694 | :: SExp2))) |
695 | | otherwise = throwError' $ ErrorMsg "illegal data definition (parameters are not uniform)" -- ++ show (c, cn, take pnum' xs, act) | 695 | | otherwise = throwError' $ ErrorMsg "illegal data definition (parameters are not uniform)" -- ++ show (c, cn, take pnum' xs, act) |
696 | 696 | ||
697 | motive = UncurryS (replicate inum (Visible, Wildcard SType)) $ | 697 | motive = UncurryS (replicate inum (Visible, Wildcard SType)) $ |
@@ -700,7 +700,7 @@ handleStmt = \case | |||
700 | (e1, es, tcn, cfn@(CaseFunName _ ct _), _, _) <- mfix $ \ ~(_, _, _, _, ct', cons') -> do | 700 | (e1, es, tcn, cfn@(CaseFunName _ ct _), _, _) <- mfix $ \ ~(_, _, _, _, ct', cons') -> do |
701 | let cfn = CaseFunName sint ct' $ length ps | 701 | let cfn = CaseFunName sint ct' $ length ps |
702 | let tcn = TyConName sint inum vty (map fst cons') cfn | 702 | let tcn = TyConName sint inum vty (map fst cons') cfn |
703 | e1 <- addToEnv s (TyCon tcn [], vty) | 703 | e1 <- addToEnv s (ET (TyCon tcn []) vty) |
704 | (unzip -> (mconcat -> es, cons)) <- withEnv e1 $ zipWithM mkConstr [0..] cs | 704 | (unzip -> (mconcat -> es, cons)) <- withEnv e1 $ zipWithM mkConstr [0..] cs |
705 | ct <- withEnv (e1 <> es) $ inferType | 705 | ct <- withEnv (e1 <> es) $ inferType |
706 | ( UncurryS | 706 | ( UncurryS |
@@ -714,14 +714,14 @@ handleStmt = \case | |||
714 | ) | 714 | ) |
715 | return (e1, es, tcn, cfn, ct, cons) | 715 | return (e1, es, tcn, cfn, ct, cons) |
716 | 716 | ||
717 | e2 <- addToEnv (SIName (sourceInfo s) $ CaseName (sName s)) (lamify ct $ \xs -> evalCaseFun cfn (init $ drop (length ps) xs) (last xs), ct) | 717 | e2 <- addToEnv (SIName (sourceInfo s) $ CaseName (sName s)) $ ET (lamify ct $ \xs -> evalCaseFun cfn (init $ drop (length ps) xs) (last xs)) ct |
718 | let ps' = fst $ getParams vty | 718 | let ps' = fst $ getParams vty |
719 | t = (TType :~> TType) | 719 | t = (TType :~> TType) |
720 | :~> addParams ps' (Var (length ps') `app_` TyCon tcn (downTo 0 $ length ps')) | 720 | :~> addParams ps' (Var (length ps') `app_` TyCon tcn (downTo 0 $ length ps')) |
721 | :~> TType | 721 | :~> TType |
722 | :~> Var 2 `app_` Var 0 | 722 | :~> Var 2 `app_` Var 0 |
723 | :~> Var 3 `app_` Var 1 | 723 | :~> Var 3 `app_` Var 1 |
724 | e3 <- addToEnv (SIName (sourceInfo s) $ MatchName (sName s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName sint t) [m, tr, f] n, t) | 724 | e3 <- addToEnv (SIName (sourceInfo s) $ MatchName (sName s)) $ ET (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName sint t) [m, tr, f] n) t |
725 | return (e1 <> e2 <> e3 <> es) | 725 | return (e1 <> e2 <> e3 <> es) |
726 | 726 | ||
727 | stmt -> error $ "handleStmt: " ++ ppShow stmt | 727 | stmt -> error $ "handleStmt: " ++ ppShow stmt |
@@ -761,12 +761,12 @@ getApps' = second reverse . run where | |||
761 | 761 | ||
762 | inferTerm :: Monad m => String -> SExp2 -> IM m ExpType | 762 | inferTerm :: Monad m => String -> SExp2 -> IM m ExpType |
763 | inferTerm msg t = | 763 | inferTerm msg t = |
764 | fmap ((closedExp *** closedExp) . recheck msg EGlobal . replaceMetas (lamPi Hidden)) $ inferN EGlobal t | 764 | fmap (closedExp . recheck msg EGlobal . replaceMetas (lamPi Hidden)) $ inferN EGlobal t |
765 | inferType :: Monad m => SExp2 -> IM m Type | 765 | inferType :: Monad m => SExp2 -> IM m Type |
766 | inferType t = fmap (closedExp . fst . recheck "inferType" EGlobal . flip (,) TType . replaceMetas (Pi Hidden) . fmap fst) $ inferN (CheckType_ (debugSI "inferType CheckType_") TType EGlobal) t | 766 | inferType t = fmap (closedExp . expr . recheck "inferType" EGlobal . flip ET TType . replaceMetas (Pi Hidden) . fmap expr) $ inferN (CheckType_ (debugSI "inferType CheckType_") TType EGlobal) t |
767 | 767 | ||
768 | addToEnv :: Monad m => SIName -> ExpType -> IM m GlobalEnv | 768 | addToEnv :: Monad m => SIName -> ExpType -> IM m GlobalEnv |
769 | addToEnv sn@(SIName si s) (x, t) = do | 769 | addToEnv sn@(SIName si s) (ET x t) = do |
770 | -- maybe (pure ()) throwError_ $ ambiguityCheck s t -- TODO | 770 | -- maybe (pure ()) throwError_ $ ambiguityCheck s t -- TODO |
771 | -- b <- asks $ (TraceTypeCheck `elem`) . fst | 771 | -- b <- asks $ (TraceTypeCheck `elem`) . fst |
772 | tell [IType sn t] | 772 | tell [IType sn t] |
@@ -781,6 +781,6 @@ joinEnv e1 e2 = do | |||
781 | 781 | ||
782 | downTo n m = map Var [n+m-1, n+m-2..n] | 782 | downTo n m = map Var [n+m-1, n+m-2..n] |
783 | 783 | ||
784 | tellType si t = tell $ mkInfoItem (sourceInfo si) $ DTypeNamespace True $ mkDoc False (t, TType) | 784 | tellType si t = tell $ mkInfoItem (sourceInfo si) $ DTypeNamespace True $ mkDoc False (ET t TType) |
785 | 785 | ||
786 | 786 | ||