diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-02-19 12:39:50 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-02-19 12:40:05 +0100 |
commit | a4b6b7bae563d8d2cfdbb5afc3bde594dad7f77b (patch) | |
tree | 23a08e7b5ccb392b0deeb20a6849c7f0eb3b36ed /src/LambdaCube | |
parent | 49b1a36e8bf97a4edd56d7890273566eaa895075 (diff) |
speedup: tweak max de bruijn handling
Diffstat (limited to 'src/LambdaCube')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 39 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Lexer.hs | 6 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 30 |
3 files changed, 45 insertions, 30 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 9afa8a36..13e18411 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -107,7 +107,12 @@ pattern NoLE <- (isNoLabelEnd -> True) | |||
107 | isNoLabelEnd (LabelEnd_ _) = False | 107 | isNoLabelEnd (LabelEnd_ _) = False |
108 | isNoLabelEnd _ = True | 108 | isNoLabelEnd _ = True |
109 | 109 | ||
110 | 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 | 110 | mconcat1 [] = mempty |
111 | mconcat1 [x] = x | ||
112 | mconcat1 xs = notClosed --foldl1 (<>) xs | ||
113 | mconcat1' = mconcat | ||
114 | |||
115 | 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 | ||
111 | pattern Fun f i xs n = Fun' f [] i xs n | 116 | pattern Fun f i xs n = Fun' f [] i xs n |
112 | pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE)) | 117 | pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE)) |
113 | pattern UFunN a b <- UTFun a _ b | 118 | pattern UFunN a b <- UTFun a _ b |
@@ -117,7 +122,7 @@ pattern DFun_ fn xs <- Fun fn 0 (reverse -> xs) (Delta _) where | |||
117 | pattern TFun' a t b = DFun_ (FunName a Nothing t) b | 122 | pattern TFun' a t b = DFun_ (FunName a Nothing t) b |
118 | pattern TFun a t b = Neut (TFun' a t b) | 123 | pattern TFun a t b = Neut (TFun' a t b) |
119 | 124 | ||
120 | pattern CaseFun_ a b c <- CaseFun__ _ a b c where CaseFun_ a b c = CaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c | 125 | pattern CaseFun_ a b c <- CaseFun__ _ a b c where CaseFun_ a b c = CaseFun__ (maxDB_ c <> foldMap maxDB_ b) a b c |
121 | pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c where TyCaseFun_ a b c = TyCaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c | 126 | pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c where TyCaseFun_ a b c = TyCaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c |
122 | pattern App_ a b <- App__ _ a b where App_ a b = App__ (maxDB_ a <> maxDB_ b) a b | 127 | pattern App_ a b <- App__ _ a b where App_ a b = App__ (maxDB_ a <> maxDB_ b) a b |
123 | pattern CaseFun a b c = Neut (CaseFun_ a b c) | 128 | pattern CaseFun a b c = Neut (CaseFun_ a b c) |
@@ -141,7 +146,7 @@ makeCaseFunPars' te n = case neutType' te n of | |||
141 | pattern Closed :: () => Up a => a -> a | 146 | pattern Closed :: () => Up a => a -> a |
142 | pattern Closed a <- a where Closed a = closedExp a | 147 | pattern Closed a <- a where Closed a = closedExp a |
143 | 148 | ||
144 | pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (foldMap maxDB_ y) x n y | 149 | pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (mconcat1 $ map maxDB_ y) x n y |
145 | pattern ConN s a <- Con (ConName s _ _) _ a | 150 | pattern ConN s a <- Con (ConName s _ _) _ a |
146 | pattern ConN' s a <- Con (ConName _ s _) _ a | 151 | pattern ConN' s a <- Con (ConName _ s _) _ a |
147 | tCon s i t a = Con (ConName s i t) 0 a | 152 | tCon s i t a = Con (ConName s i t) 0 a |
@@ -371,7 +376,7 @@ instance Up Neutral where | |||
371 | CaseFun__ md s as ne -> CaseFun__ (upDB n md) s (up_ n i <$> as) (up_ n i ne) | 376 | CaseFun__ md s as ne -> CaseFun__ (upDB n md) s (up_ n i <$> as) (up_ n i ne) |
372 | TyCaseFun__ md s as ne -> TyCaseFun__ (upDB n md) s (up_ n i <$> as) (up_ n i ne) | 377 | TyCaseFun__ md s as ne -> TyCaseFun__ (upDB n md) s (up_ n i <$> as) (up_ n i ne) |
373 | App__ md a b -> App__ (upDB n md) (up_ n i a) (up_ n i b) | 378 | App__ md a b -> App__ (upDB n md) (up_ n i a) (up_ n i b) |
374 | Fun' fn vs c x y -> Fun' fn (up_ n i <$> vs) c (up_ n i <$> x) $ up_ n (i + c) y | 379 | Fun_ md fn vs c x y -> Fun_ (upDB n md) fn (up_ n i <$> vs) c (up_ n i <$> x) $ up_ n (i + c) y |
375 | LabelEnd_ x -> LabelEnd_ $ up_ n i x | 380 | LabelEnd_ x -> LabelEnd_ $ up_ n i x |
376 | d@Delta{} -> d | 381 | d@Delta{} -> d |
377 | 382 | ||
@@ -755,6 +760,8 @@ inferN e s = do | |||
755 | f ITrace{} = False | 760 | f ITrace{} = False |
756 | f _ = True | 761 | f _ = True |
757 | 762 | ||
763 | substTo i x = subst i x . up1_ (i+1) | ||
764 | |||
758 | inferN_ :: forall m . Monad m => (forall a . String -> String -> IM m a -> IM m a) -> Env -> SExp2 -> IM m ExpType' | 765 | inferN_ :: forall m . Monad m => (forall a . String -> String -> IM m a -> IM m a) -> Env -> SExp2 -> IM m ExpType' |
759 | inferN_ tellTrace = infer where | 766 | inferN_ tellTrace = infer where |
760 | 767 | ||
@@ -779,12 +786,12 @@ inferN_ tellTrace = infer where | |||
779 | | x@(SGlobal (si, MatchName n)) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` SVar siv v `SAppV` b <- e | 786 | | x@(SGlobal (si, MatchName n)) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` SVar siv v `SAppV` b <- e |
780 | = infer te $ x `SAppV` SLam Visible SType (STyped mempty (subst (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` SVar siv v `SAppV` b | 787 | = infer te $ x `SAppV` SLam Visible SType (STyped mempty (subst (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` SVar siv v `SAppV` b |
781 | -- temporal hack | 788 | -- temporal hack |
782 | | x@(SGlobal (si, "'NatCase")) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` b `SAppV` SVar siv v <- e | 789 | | x@(SGlobal (si, CaseName "'Nat")) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` b `SAppV` SVar siv v <- e |
783 | = infer te $ x `SAppV` SLamV (STyped mempty (subst (v+1) (Var 0) $ up1_ (v+2) $ up 1 t, TType)) `SAppV` a `SAppV` b `SAppV` SVar siv v | 790 | = infer te $ x `SAppV` SLamV (STyped mempty (substTo (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` b `SAppV` SVar siv v |
784 | -- temporal hack | 791 | -- temporal hack |
785 | | x@(SGlobal (si, "'VecSCase")) `SAppV` SLamV (SLamV (Wildcard _)) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v <- e | 792 | | x@(SGlobal (si, CaseName "'VecS")) `SAppV` SLamV (SLamV (Wildcard _)) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v <- e |
786 | , TVec (Var n') _ <- snd $ varType "xx" v te | 793 | , TyConN "'VecS" [_, Var n'] <- snd $ varType "xx" v te |
787 | = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v | 794 | = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (substTo (n'+2) (Var 1) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v |
788 | 795 | ||
789 | {- | 796 | {- |
790 | -- temporal hack | 797 | -- temporal hack |
@@ -986,18 +993,18 @@ recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt) | |||
986 | 993 | ||
987 | recheck_ msg te = \case | 994 | recheck_ msg te = \case |
988 | (Var k, zt) -> Var k -- todo: check var type | 995 | (Var k, zt) -> Var k -- todo: check var type |
989 | (Lam b, Pi h a bt) -> Lam $ recheck_ "9" (EBind2 (BLam h) a te) (b, bt) | 996 | (Lam_ md b, Pi h a bt) -> Lam_ md $ recheck_ "9" (EBind2 (BLam h) a te) (b, bt) |
990 | (Pi h a b, TType) -> Pi h (checkType te a) $ checkType (EBind2 (BPi h) a te) b | 997 | (Pi_ md h a b, TType) -> Pi_ md h (checkType te a) $ checkType (EBind2 (BPi h) a te) b |
991 | (ELit l, zt) -> ELit l -- todo: check literal type | 998 | (ELit l, zt) -> ELit l -- todo: check literal type |
992 | (TType, TType) -> TType | 999 | (TType, TType) -> TType |
993 | (Neut (App_ a b), zt) | 1000 | (Neut (App__ md a b), zt) |
994 | | (Neut a', at) <- recheck'' "app1" te (Neut a, neutType te a) | 1001 | | (Neut a', at) <- recheck'' "app1" te (Neut a, neutType te a) |
995 | -> checkApps "a" [] zt (Neut . App_ a' . head) te at [b] | 1002 | -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b] |
996 | (Con s n as, zt) -> checkApps (show s) [] zt (Con s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as | 1003 | (Con_ md s n as, zt) -> checkApps (show s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as |
997 | (TyCon s as, zt) -> checkApps (show s) [] zt (TyCon s) te (nType s) as | 1004 | (TyCon_ md s as, zt) -> checkApps (show s) [] zt (TyCon_ md s) te (nType s) as |
998 | (CaseFun s@(CaseFunName _ t pars) as n, zt) -> checkApps (show s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n]) | 1005 | (CaseFun s@(CaseFunName _ t pars) as n, zt) -> checkApps (show s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n]) |
999 | (TyCaseFun s [m, t, f] n, zt) -> checkApps (show s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f] | 1006 | (TyCaseFun s [m, t, f] n, zt) -> checkApps (show s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f] |
1000 | (Neut (Fun' f vs@[] i a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun' f vs i (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x | 1007 | (Neut (Fun_ md f vs@[] i a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun_ md f vs i (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x |
1001 | -- TODO | 1008 | -- TODO |
1002 | (r@(Neut (Fun' f vs i a x)), zt) -> r | 1009 | (r@(Neut (Fun' f vs i a x)), zt) -> r |
1003 | (LabelEnd x, zt) -> LabelEnd $ recheck_ msg te (x, zt) | 1010 | (LabelEnd x, zt) -> LabelEnd $ recheck_ msg te (x, zt) |
diff --git a/src/LambdaCube/Compiler/Lexer.hs b/src/LambdaCube/Compiler/Lexer.hs index 631bbef9..83446799 100644 --- a/src/LambdaCube/Compiler/Lexer.hs +++ b/src/LambdaCube/Compiler/Lexer.hs | |||
@@ -87,7 +87,13 @@ instance Show Lit where | |||
87 | 87 | ||
88 | type SName = String | 88 | type SName = String |
89 | 89 | ||
90 | pattern CaseName :: String -> String | ||
91 | pattern CaseName cs <- (getCaseName -> Just cs) where CaseName = caseName | ||
92 | |||
90 | caseName (c:cs) = toLower c: cs ++ "Case" | 93 | caseName (c:cs) = toLower c: cs ++ "Case" |
94 | getCaseName cs = case splitAt 4 $ reverse cs of | ||
95 | (reverse -> "Case", xs) -> Just $ reverse xs | ||
96 | _ -> Nothing | ||
91 | 97 | ||
92 | pattern MatchName cs <- (getMatchName -> Just cs) where MatchName = matchName | 98 | pattern MatchName cs <- (getMatchName -> Just cs) where MatchName = matchName |
93 | 99 | ||
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index e4d482a8..e64327de 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -15,7 +15,7 @@ module LambdaCube.Compiler.Parser | |||
15 | , pattern SVar, pattern SType, pattern Wildcard, pattern SAppV, pattern SLamV, pattern SAnn | 15 | , pattern SVar, pattern SType, pattern Wildcard, pattern SAppV, pattern SLamV, pattern SAnn |
16 | , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam, pattern Parens | 16 | , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam, pattern Parens |
17 | , pattern TyType, pattern Wildcard_ | 17 | , pattern TyType, pattern Wildcard_ |
18 | , debug, isPi, varDB, lowerDB, justDB, upDB, cmpDB, MaxDB (..), iterateN, traceD | 18 | , debug, isPi, varDB, lowerDB, upDB, notClosed, cmpDB, MaxDB(..), iterateN, traceD |
19 | , parseLC, runDefParser | 19 | , parseLC, runDefParser |
20 | , getParamsS, addParamsS, getApps, apps', downToS, addForalls | 20 | , getParamsS, addParamsS, getApps, apps', downToS, addForalls |
21 | , mkDesugarInfo, joinDesugarInfo | 21 | , mkDesugarInfo, joinDesugarInfo |
@@ -175,28 +175,30 @@ instance SetSourceInfo (SExp' a) where | |||
175 | SGlobal (_, n) -> SGlobal (si, n) | 175 | SGlobal (_, n) -> SGlobal (si, n) |
176 | SLit _ l -> SLit si l | 176 | SLit _ l -> SLit si l |
177 | 177 | ||
178 | -------------------------------------------------------------------------------- low-level toolbox | 178 | -------------------------------------------------------------------------------- De-Bruijn limit |
179 | 179 | ||
180 | newtype MaxDB = MaxDB {getMaxDB :: Maybe Int} | 180 | newtype MaxDB = MaxDB {getMaxDB :: Bool} -- True: closed |
181 | 181 | ||
182 | instance Monoid MaxDB where | 182 | instance Monoid MaxDB where |
183 | mempty = MaxDB Nothing | 183 | mempty = MaxDB True |
184 | MaxDB a `mappend` MaxDB a' = MaxDB $ Just $ max (fromMaybe 0 a) (fromMaybe 0 a') | 184 | MaxDB a `mappend` MaxDB a' = MaxDB $ a && a' -- $ Just $ max (fromMaybe 0 a) (fromMaybe 0 a') |
185 | 185 | ||
186 | instance Show MaxDB where show _ = "MaxDB" | 186 | instance Show MaxDB where show _ = "MaxDB" |
187 | 187 | ||
188 | varDB i = MaxDB $ Just $ i + 1 | 188 | varDB i = MaxDB False |
189 | 189 | ||
190 | lowerDB (MaxDB i) = MaxDB $ (+ (- 1)) <$> i | 190 | lowerDB = id |
191 | justDB (MaxDB i) = MaxDB $ Just $ fromMaybe 0 i | ||
192 | 191 | ||
193 | -- 0 means that no free variable is used | 192 | -- 0 means that no free variable is used |
194 | -- 1 means that only var 0 is used | 193 | -- 1 means that only var 0 is used |
195 | --cmpDB i e = i >= maxDB e | 194 | --cmpDB i e = i >= maxDB e |
196 | cmpDB _ (maxDB_ -> MaxDB x) = isNothing x | 195 | cmpDB _ (maxDB_ -> MaxDB x) = x --isNothing x |
196 | |||
197 | upDB n = id --(MaxDB i) = MaxDB $ (\x -> if x == 0 then x else x+n) $ i | ||
197 | 198 | ||
198 | maxDB = max 0 . fromMaybe 0 . getMaxDB . maxDB_ | 199 | notClosed = MaxDB False |
199 | upDB n (MaxDB i) = MaxDB $ (\x -> if x == 0 then x else x+n) <$> i | 200 | |
201 | -------------------------------------------------------------------------------- low-level toolbox | ||
200 | 202 | ||
201 | class Up a where | 203 | class Up a where |
202 | up_ :: Int -> Int -> a -> a | 204 | up_ :: Int -> Int -> a -> a |
@@ -681,7 +683,7 @@ compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ g | |||
681 | ts@(GuardNode f s _ _: _) -> case Map.lookup s (snd adts) of | 683 | ts@(GuardNode f s _ _: _) -> case Map.lookup s (snd adts) of |
682 | Nothing -> error $ "Constructor is not defined: " ++ s | 684 | Nothing -> error $ "Constructor is not defined: " ++ s |
683 | Just (Left ((casename, inum), cns)) -> | 685 | Just (Left ((casename, inum), cns)) -> |
684 | foldl SAppV (SGlobal (debugSI "compileGuardTree2", casename) `SAppV` iterateN (1 + inum) SLamV (Wildcard SType)) | 686 | foldl SAppV (SGlobal (debugSI "compileGuardTree2", casename) `SAppV` iterateN (1 + inum) SLamV (Wildcard (Wildcard SType))) |
685 | [ iterateN n SLamV $ guardTreeToCases $ Alts $ map (filterGuardTree (up n f) cn 0 n . upGT 0 n) ts | 687 | [ iterateN n SLamV $ guardTreeToCases $ Alts $ map (filterGuardTree (up n f) cn 0 n . upGT 0 n) ts |
686 | | (cn, n) <- cns | 688 | | (cn, n) <- cns |
687 | ] | 689 | ] |
@@ -1002,8 +1004,8 @@ mkDesugarInfo ss = | |||
1002 | where | 1004 | where |
1003 | pars ty = length $ filter ((== Visible) . fst) $ fst $ getParamsS ty -- todo | 1005 | pars ty = length $ filter ((== Visible) . fst) $ fst $ getParamsS ty -- todo |
1004 | 1006 | ||
1005 | hackHList ("HCons", _) = ("HCons", Left (("hlistConsCase", 0), [("HCons", 2)])) | 1007 | hackHList ("HCons", _) = ("HCons", Left (("hlistConsCase", -1), [("HCons", 2)])) |
1006 | hackHList ("HNil", _) = ("HNil", Left (("hlistNilCase", 0), [("HNil", 0)])) | 1008 | hackHList ("HNil", _) = ("HNil", Left (("hlistNilCase", -1), [("HNil", 0)])) |
1007 | hackHList x = x | 1009 | hackHList x = x |
1008 | 1010 | ||
1009 | joinDesugarInfo (fm, cm) (fm', cm') = (Map.union fm fm', Map.union cm cm') | 1011 | joinDesugarInfo (fm, cm) (fm', cm') = (Map.union fm fm', Map.union cm cm') |