diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-02-19 22:50:16 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-02-19 22:50:16 +0100 |
commit | e0ec0a953292f9613f7ec65bcc78c6b3044c04c3 (patch) | |
tree | 7a055cfc61e569e6b3bc19857f955ea9334dedf4 /src/LambdaCube | |
parent | a287d5a79406ac9042c5ca9bbd6b52b192ca1eee (diff) |
refactoring
Diffstat (limited to 'src/LambdaCube')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index c593f607..cd90acc2 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -17,7 +17,7 @@ | |||
17 | module LambdaCube.Compiler.Infer | 17 | module LambdaCube.Compiler.Infer |
18 | ( Binder (..), SName, Lit(..), Visibility(..) | 18 | ( Binder (..), SName, Lit(..), Visibility(..) |
19 | , Exp (..), Neutral (..), ExpType, GlobalEnv | 19 | , Exp (..), Neutral (..), ExpType, GlobalEnv |
20 | , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_ | 20 | , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, app_ |
21 | , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun, pattern ELit, pattern Func, pattern LabelEnd, pattern FL, pattern UFL, unFunc_ | 21 | , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun, pattern ELit, pattern Func, pattern LabelEnd, pattern FL, pattern UFL, unFunc_ |
22 | , outputType, boolType, trueExp | 22 | , outputType, boolType, trueExp |
23 | , down, Subst (..), free | 23 | , down, Subst (..), free |
@@ -26,7 +26,7 @@ module LambdaCube.Compiler.Infer | |||
26 | , Info(..), Infos, listAllInfos, listTypeInfos, listTraceInfos | 26 | , Info(..), Infos, listAllInfos, listTypeInfos, listTraceInfos |
27 | , PolyEnv(..), joinPolyEnvs, filterPolyEnv, inference_ | 27 | , PolyEnv(..), joinPolyEnvs, filterPolyEnv, inference_ |
28 | , nType, conType, neutType, neutType', appTy, mkConPars, makeCaseFunPars, makeCaseFunPars' | 28 | , nType, conType, neutType, neutType', appTy, mkConPars, makeCaseFunPars, makeCaseFunPars' |
29 | , MaxDB(..), unfixlabel | 29 | , MaxDB, unfixlabel |
30 | , ErrorMsg, showError | 30 | , ErrorMsg, showError |
31 | , FName (..) | 31 | , FName (..) |
32 | ) where | 32 | ) where |
@@ -55,20 +55,20 @@ import LambdaCube.Compiler.Parser | |||
55 | data Exp | 55 | data Exp |
56 | = TType | 56 | = TType |
57 | | ELit_ Lit | 57 | | ELit_ Lit |
58 | | Con_ MaxDB ConName !Int{-number of ereased arguments applied-} [Exp] | 58 | | Con_ !MaxDB ConName !Int{-number of ereased arguments applied-} [Exp] |
59 | | TyCon_ MaxDB TyConName [Exp] | 59 | | TyCon_ !MaxDB TyConName [Exp] |
60 | | Pi_ MaxDB Visibility Exp Exp | 60 | | Pi_ !MaxDB Visibility Exp Exp |
61 | | Lam_ MaxDB Exp | 61 | | Lam_ !MaxDB Exp |
62 | | Neut Neutral | 62 | | Neut Neutral |
63 | deriving (Show) | 63 | deriving (Show) |
64 | 64 | ||
65 | pattern ELit a <- (unfixlabel -> ELit_ a) where ELit = ELit_ | 65 | pattern ELit a <- (unfixlabel -> ELit_ a) where ELit = ELit_ |
66 | 66 | ||
67 | data Neutral | 67 | data Neutral |
68 | = Fun_ MaxDB FunName [Exp]{-local vars-} !Int{-number of missing parameters-} [Exp]{-given parameters, reversed-} Neutral{-unfolded expression-}{-not neut?-} | 68 | = Fun_ !MaxDB FunName [Exp]{-local vars-} !Int{-number of missing parameters-} [Exp]{-given parameters, reversed-} Neutral{-unfolded expression-}{-not neut?-} |
69 | | CaseFun__ MaxDB CaseFunName [Exp] Neutral | 69 | | CaseFun__ !MaxDB CaseFunName [Exp] Neutral |
70 | | TyCaseFun__ MaxDB TyCaseFunName [Exp] Neutral | 70 | | TyCaseFun__ !MaxDB TyCaseFunName [Exp] Neutral |
71 | | App__ MaxDB Neutral Exp | 71 | | App__ !MaxDB Neutral Exp |
72 | | Var_ !Int -- De Bruijn variable | 72 | | Var_ !Int -- De Bruijn variable |
73 | | LabelEnd_ Exp -- not neut? | 73 | | LabelEnd_ Exp -- not neut? |
74 | | Delta (SData ([Exp] -> Exp)) -- not neut? | 74 | | Delta (SData ([Exp] -> Exp)) -- not neut? |
@@ -203,11 +203,7 @@ pattern NoLE <- (isNoLabelEnd -> True) | |||
203 | isNoLabelEnd (LabelEnd_ _) = False | 203 | isNoLabelEnd (LabelEnd_ _) = False |
204 | isNoLabelEnd _ = True | 204 | isNoLabelEnd _ = True |
205 | 205 | ||
206 | mconcat1 [] = mempty | 206 | pattern Fun' f vs i xs n <- Fun_ _ f vs i xs n where Fun' f vs i xs n = Fun_ (foldMap maxDB_ vs <> foldMap maxDB_ xs {- <> iterateN i lowerDB (maxDB_ n)-}) f vs i xs n |
207 | mconcat1 [x] = x | ||
208 | mconcat1 xs = notClosed --foldl1 (<>) xs | ||
209 | |||
210 | pattern Fun' f vs i xs n <- Fun_ _ f vs i xs n where Fun' f vs i xs n = Fun_ (mconcat1 $ map maxDB_ vs ++ map maxDB_ xs {- <> iterateN i lowerDB (maxDB_ n)-}) f vs i xs n | ||
211 | pattern Fun f i xs n = Fun' f [] i xs n | 207 | pattern Fun f i xs n = Fun' f [] i xs n |
212 | pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE)) | 208 | pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE)) |
213 | pattern UFunN a b <- UTFun a _ b | 209 | pattern UFunN a b <- UTFun a _ b |
@@ -241,7 +237,7 @@ makeCaseFunPars' te n = case neutType' te n of | |||
241 | pattern Closed :: () => Up a => a -> a | 237 | pattern Closed :: () => Up a => a -> a |
242 | pattern Closed a <- a where Closed a = closedExp a | 238 | pattern Closed a <- a where Closed a = closedExp a |
243 | 239 | ||
244 | pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (mconcat1 $ map maxDB_ y) x n y | 240 | pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (foldMap maxDB_ y) x n y |
245 | pattern ConN s a <- Con (ConName s _ _) _ a | 241 | pattern ConN s a <- Con (ConName s _ _) _ a |
246 | pattern ConN' s a <- Con (ConName _ s _) _ a | 242 | pattern ConN' s a <- Con (ConName _ s _) _ a |
247 | tCon s i t a = Con (ConName s i t) 0 a | 243 | tCon s i t a = Con (ConName s i t) 0 a |
@@ -325,13 +321,13 @@ trueExp = EBool True | |||
325 | 321 | ||
326 | pattern LabelEnd x = Neut (LabelEnd_ x) | 322 | pattern LabelEnd x = Neut (LabelEnd_ x) |
327 | 323 | ||
328 | pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp | 324 | --pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp |
329 | pmLabel' (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as | 325 | pmLabel' _ (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as |
330 | pmLabel' f vs i xs (unfixlabel -> Neut y) = Neut $ Fun' f vs i xs y | 326 | pmLabel' md f vs i xs (unfixlabel -> Neut y) = Neut $ Fun_ md f vs i xs y |
331 | pmLabel' f _ i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y) | 327 | pmLabel' _ f _ i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y) |
332 | 328 | ||
333 | pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp | 329 | pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp |
334 | pmLabel f vs i xs e = pmLabel' f vs (i + numLams e) xs (Neut $ dropLams e) | 330 | pmLabel f vs i xs e = pmLabel' (foldMap maxDB_ vs <> foldMap maxDB_ xs) f vs (i + numLams e) xs (Neut $ dropLams e) |
335 | 331 | ||
336 | dropLams (unfixlabel -> Lam x) = dropLams x | 332 | dropLams (unfixlabel -> Lam x) = dropLams x |
337 | dropLams (unfixlabel -> Neut x) = x | 333 | dropLams (unfixlabel -> Neut x) = x |
@@ -443,6 +439,7 @@ instance Up Exp where | |||
443 | instance Subst Exp Exp where | 439 | instance Subst Exp Exp where |
444 | subst i0 x = f i0 | 440 | subst i0 x = f i0 |
445 | where | 441 | where |
442 | dx = maxDB_ x | ||
446 | f i (Neut n) = substNeut n | 443 | f i (Neut n) = substNeut n |
447 | where | 444 | where |
448 | substNeut e | cmpDB i e = Neut e | 445 | substNeut e | cmpDB i e = Neut e |
@@ -451,15 +448,15 @@ instance Subst Exp Exp where | |||
451 | CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n) | 448 | CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n) |
452 | TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n) | 449 | TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n) |
453 | App_ a b -> app_ (substNeut a) (f i b) | 450 | App_ a b -> app_ (substNeut a) (f i b) |
454 | Fun' fn vs c xs v -> pmLabel' fn (f i <$> vs) c (f i <$> xs) $ f (i + c) $ Neut v | 451 | Fun_ md fn vs c xs v -> pmLabel' (md <> upDB i dx) fn (f i <$> vs) c (f i <$> xs) $ f (i + c) $ Neut v |
455 | LabelEnd_ a -> LabelEnd $ f i a | 452 | LabelEnd_ a -> LabelEnd $ f i a |
456 | d@Delta{} -> Neut d | 453 | d@Delta{} -> Neut d |
457 | f i e | cmpDB i e = e | 454 | f i e | cmpDB i e = e |
458 | f i e = case e of | 455 | f i e = case e of |
459 | Lam b -> Lam (f (i+1) b) | 456 | Lam_ md b -> Lam_ (md <> upDB i dx) (f (i+1) b) |
460 | Con s n as -> Con s n $ f i <$> as | 457 | Con_ md s n as -> Con_ (md <> upDB i dx) s n $ f i <$> as |
461 | Pi h a b -> Pi h (f i a) (f (i+1) b) | 458 | Pi_ md h a b -> Pi_ (md <> upDB i dx) h (f i a) (f (i+1) b) |
462 | TyCon s as -> TyCon s $ f i <$> as | 459 | TyCon_ md s as -> TyCon_ (md <> upDB i dx) s $ f i <$> as |
463 | 460 | ||
464 | instance Up Neutral where | 461 | instance Up Neutral where |
465 | 462 | ||
@@ -697,6 +694,7 @@ data CEnv a | |||
697 | deriving (Show, Functor) | 694 | deriving (Show, Functor) |
698 | 695 | ||
699 | instance (Subst Exp a, Up a) => Up (CEnv a) where | 696 | instance (Subst Exp a, Up a) => Up (CEnv a) where |
697 | up_ n i = iterateN n $ up1_ i | ||
700 | up1_ i = \case | 698 | up1_ i = \case |
701 | MEnd a -> MEnd $ up1_ i a | 699 | MEnd a -> MEnd $ up1_ i a |
702 | Meta a b -> Meta (up1_ i a) (up1_ (i+1) b) | 700 | Meta a b -> Meta (up1_ i a) (up1_ (i+1) b) |
@@ -1245,6 +1243,7 @@ handleStmt defs = \case | |||
1245 | vty <- inferType $ addParamsS ps t_ | 1243 | vty <- inferType $ addParamsS ps t_ |
1246 | tellType (fst s) vty | 1244 | tellType (fst s) vty |
1247 | let | 1245 | let |
1246 | sint = cFName modn 2 s | ||
1248 | pnum' = length $ filter ((== Visible) . fst) ps | 1247 | pnum' = length $ filter ((== Visible) . fst) ps |
1249 | inum = arity vty - length ps | 1248 | inum = arity vty - length ps |
1250 | 1249 | ||
@@ -1270,8 +1269,8 @@ handleStmt defs = \case | |||
1270 | SPi Visible (apps' (SGlobal s) $ zip (map fst ps) (downToS "a6" inum $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS "a7" 0 inum)) SType | 1269 | SPi Visible (apps' (SGlobal s) $ zip (map fst ps) (downToS "a6" inum $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS "a7" 0 inum)) SType |
1271 | 1270 | ||
1272 | (e1, es, tcn, cfn@(CaseFunName _ ct _), _, _) <- mfix $ \ ~(_, _, _, _, ct', cons') -> do | 1271 | (e1, es, tcn, cfn@(CaseFunName _ ct _), _, _) <- mfix $ \ ~(_, _, _, _, ct', cons') -> do |
1273 | let cfn = CaseFunName (cFName modn 2 s) ct' $ length ps | 1272 | let cfn = CaseFunName sint ct' $ length ps |
1274 | let tcn = TyConName (cFName modn 3 s) inum vty (map fst cons') cfn | 1273 | let tcn = TyConName sint inum vty (map fst cons') cfn |
1275 | e1 <- addToEnv s (TyCon tcn [], vty) | 1274 | e1 <- addToEnv s (TyCon tcn [], vty) |
1276 | (unzip -> (mconcat -> es, cons)) <- withEnv e1 $ zipWithM mkConstr [0..] cs | 1275 | (unzip -> (mconcat -> es, cons)) <- withEnv e1 $ zipWithM mkConstr [0..] cs |
1277 | ct <- withEnv (e1 <> es) $ inferType | 1276 | ct <- withEnv (e1 <> es) $ inferType |
@@ -1293,7 +1292,7 @@ handleStmt defs = \case | |||
1293 | :~> TType | 1292 | :~> TType |
1294 | :~> Var 2 `app_` Var 0 | 1293 | :~> Var 2 `app_` Var 0 |
1295 | :~> Var 3 `app_` Var 1 | 1294 | :~> Var 3 `app_` Var 1 |
1296 | e3 <- addToEnv (fst s, MatchName (snd s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName (cFName modn 4 s) t) [m, tr, f] n, t) | 1295 | e3 <- addToEnv (fst s, MatchName (snd s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName sint t) [m, tr, f] n, t) |
1297 | return (e1 <> e2 <> e3 <> es) | 1296 | return (e1 <> e2 <> e3 <> es) |
1298 | 1297 | ||
1299 | stmt -> error $ "handleStmt: " ++ show stmt | 1298 | stmt -> error $ "handleStmt: " ++ show stmt |