summaryrefslogtreecommitdiff
path: root/src/LambdaCube
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-02-19 22:50:16 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-02-19 22:50:16 +0100
commite0ec0a953292f9613f7ec65bcc78c6b3044c04c3 (patch)
tree7a055cfc61e569e6b3bc19857f955ea9334dedf4 /src/LambdaCube
parenta287d5a79406ac9042c5ca9bbd6b52b192ca1eee (diff)
refactoring
Diffstat (limited to 'src/LambdaCube')
-rw-r--r--src/LambdaCube/Compiler/Infer.hs57
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 @@
17module LambdaCube.Compiler.Infer 17module 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
55data Exp 55data 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
65pattern ELit a <- (unfixlabel -> ELit_ a) where ELit = ELit_ 65pattern ELit a <- (unfixlabel -> ELit_ a) where ELit = ELit_
66 66
67data Neutral 67data 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)
203isNoLabelEnd (LabelEnd_ _) = False 203isNoLabelEnd (LabelEnd_ _) = False
204isNoLabelEnd _ = True 204isNoLabelEnd _ = True
205 205
206mconcat1 [] = mempty 206pattern 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
207mconcat1 [x] = x
208mconcat1 xs = notClosed --foldl1 (<>) xs
209
210pattern 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
211pattern Fun f i xs n = Fun' f [] i xs n 207pattern Fun f i xs n = Fun' f [] i xs n
212pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE)) 208pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE))
213pattern UFunN a b <- UTFun a _ b 209pattern UFunN a b <- UTFun a _ b
@@ -241,7 +237,7 @@ makeCaseFunPars' te n = case neutType' te n of
241pattern Closed :: () => Up a => a -> a 237pattern Closed :: () => Up a => a -> a
242pattern Closed a <- a where Closed a = closedExp a 238pattern Closed a <- a where Closed a = closedExp a
243 239
244pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (mconcat1 $ map maxDB_ y) x n y 240pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (foldMap maxDB_ y) x n y
245pattern ConN s a <- Con (ConName s _ _) _ a 241pattern ConN s a <- Con (ConName s _ _) _ a
246pattern ConN' s a <- Con (ConName _ s _) _ a 242pattern ConN' s a <- Con (ConName _ s _) _ a
247tCon s i t a = Con (ConName s i t) 0 a 243tCon s i t a = Con (ConName s i t) 0 a
@@ -325,13 +321,13 @@ trueExp = EBool True
325 321
326pattern LabelEnd x = Neut (LabelEnd_ x) 322pattern LabelEnd x = Neut (LabelEnd_ x)
327 323
328pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp 324--pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp
329pmLabel' (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as 325pmLabel' _ (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as
330pmLabel' f vs i xs (unfixlabel -> Neut y) = Neut $ Fun' f vs i xs y 326pmLabel' md f vs i xs (unfixlabel -> Neut y) = Neut $ Fun_ md f vs i xs y
331pmLabel' f _ i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y) 327pmLabel' _ f _ i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y)
332 328
333pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp 329pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp
334pmLabel f vs i xs e = pmLabel' f vs (i + numLams e) xs (Neut $ dropLams e) 330pmLabel f vs i xs e = pmLabel' (foldMap maxDB_ vs <> foldMap maxDB_ xs) f vs (i + numLams e) xs (Neut $ dropLams e)
335 331
336dropLams (unfixlabel -> Lam x) = dropLams x 332dropLams (unfixlabel -> Lam x) = dropLams x
337dropLams (unfixlabel -> Neut x) = x 333dropLams (unfixlabel -> Neut x) = x
@@ -443,6 +439,7 @@ instance Up Exp where
443instance Subst Exp Exp where 439instance 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
464instance Up Neutral where 461instance Up Neutral where
465 462
@@ -697,6 +694,7 @@ data CEnv a
697 deriving (Show, Functor) 694 deriving (Show, Functor)
698 695
699instance (Subst Exp a, Up a) => Up (CEnv a) where 696instance (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