summaryrefslogtreecommitdiff
path: root/src/LambdaCube/Compiler
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-02-09 11:08:02 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-02-09 11:08:02 +0100
commit4494f61a35a16258abd5ae9cea50ef728fdc0254 (patch)
tree11ffc2ff441fc8ce8428415707ed2f1d69e0f347 /src/LambdaCube/Compiler
parente69e759ac57615db361ce38793518ebdef3538a5 (diff)
first try to tie fix better in reduction; reduction is slowed down now
Diffstat (limited to 'src/LambdaCube/Compiler')
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs41
-rw-r--r--src/LambdaCube/Compiler/Infer.hs95
-rw-r--r--src/LambdaCube/Compiler/Parser.hs16
3 files changed, 83 insertions, 69 deletions
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index 2a234820..ac12004b 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -39,6 +39,7 @@ import qualified LambdaCube.Linear as IR
39import LambdaCube.Compiler.Pretty hiding (parens) 39import LambdaCube.Compiler.Pretty hiding (parens)
40import qualified LambdaCube.Compiler.Infer as I 40import qualified LambdaCube.Compiler.Infer as I
41import LambdaCube.Compiler.Infer (SName, Lit(..), Visibility(..)) 41import LambdaCube.Compiler.Infer (SName, Lit(..), Visibility(..))
42import LambdaCube.Compiler.Parser (iterateN)
42 43
43import Data.Version 44import Data.Version
44import Paths_lambdacube_compiler (version) 45import Paths_lambdacube_compiler (version)
@@ -614,7 +615,7 @@ genVertexGLSL backend rp@(etaRed -> ELam is s) ints e@(etaRed -> ELam i o) = sec
614 tell ["gl_PointSize = " <> show (genGLSLSubst (Map.fromList $ zip (streamInput is) $ map (\(_,_,var) -> var) out) s) <> ";"] 615 tell ["gl_PointSize = " <> show (genGLSLSubst (Map.fromList $ zip (streamInput is) $ map (\(_,_,var) -> var) out) s) <> ";"]
615 tell ["}"] 616 tell ["}"]
616 return (input,out) 617 return (input,out)
617genVertexGLSL _ _ _ e = error $ "genVertexGLSL: " ++ ppShow e 618genVertexGLSL _ _ _ e = error $ "genVertexGLSL: " ++ show e --ppShow e
618 619
619genGLSL :: Exp -> String 620genGLSL :: Exp -> String
620genGLSL e = show $ genGLSLSubst mempty e 621genGLSL e = show $ genGLSLSubst mempty e
@@ -844,7 +845,7 @@ data Exp_ a
844 | Lam_ Visibility Pat a a 845 | Lam_ Visibility Pat a a
845 | Con_ SName a [a] 846 | Con_ SName a [a]
846 | ELit_ Lit 847 | ELit_ Lit
847 | Fun_ SName a [a] 848 | Fun_ SName a [a] (Maybe a)
848 | App_ a a 849 | App_ a a
849 | Var_ SName a 850 | Var_ SName a
850 | TType_ 851 | TType_
@@ -872,7 +873,7 @@ pattern Pi h n a b = Exp (Pi_ h n a b)
872pattern Lam h n a b = Exp (Lam_ h n a b) 873pattern Lam h n a b = Exp (Lam_ h n a b)
873pattern Con n a b = Exp (Con_ (UntickName n) a b) 874pattern Con n a b = Exp (Con_ (UntickName n) a b)
874pattern ELit a = Exp (ELit_ a) 875pattern ELit a = Exp (ELit_ a)
875pattern Fun n a b = Exp (Fun_ (UntickName n) a b) 876pattern Fun n a b md = Exp (Fun_ (UntickName n) a b md)
876pattern EApp a b = Exp (App_ a b) 877pattern EApp a b = Exp (App_ a b)
877pattern Var a b = Exp (Var_ a b) 878pattern Var a b = Exp (Var_ a b)
878pattern TType = Exp TType_ 879pattern TType = Exp TType_
@@ -910,24 +911,30 @@ toExp = flip runReader [] . flip evalStateT freshTypeVars . f_
910 I.Pi b x yt -> newName >>= \n -> do 911 I.Pi b x yt -> newName >>= \n -> do
911 t <- f_ (x, I.TType) 912 t <- f_ (x, I.TType)
912 Lam b (PVar t n) t <$> local ((Var n t, x):) (f_ (y, yt)) 913 Lam b (PVar t n) t <$> local ((Var n t, x):) (f_ (y, yt))
913 I.Con s n xs -> Con (show s) <$> f_ (t, I.TType) <*> chain [] t (I.mkConPars n et ++ xs) 914 I.Con s n xs -> Con (show s) <$> f_ (t, I.TType) <*> chain "con" [] t (I.mkConPars n et ++ xs)
914 where t = I.conType et s 915 where t = I.conType et s
915 I.TyCon s xs -> Con (show s) <$> f_ (I.nType s, I.TType) <*> chain [] (I.nType s) xs 916 I.TyCon s xs -> Con (show s) <$> f_ (I.nType s, I.TType) <*> chain "tycon" [] (I.nType s) xs
916 I.Neut (I.Fun s _ xs _) -> Fun (show s) <$> f_ (I.nType s, I.TType) <*> chain [] (I.nType s) xs 917 I.Neut (I.Fun s i xs def) -> Fun (show s) <$> f_ (I.nType s, I.TType) <*> chain "fun" [] (I.nType s) xs <*> mkDef i def et
917 I.CaseFun s xs n -> asks makeTE >>= \te -> Fun (show s) <$> f_ (I.nType s, I.TType) <*> chain [] (I.nType s) (I.makeCaseFunPars te n ++ xs ++ [I.Neut n]) 918 I.CaseFun s xs n -> asks makeTE >>= \te -> Fun (show s) <$> f_ (I.nType s, I.TType) <*> chain "case" [] (I.nType s) (I.makeCaseFunPars te n ++ xs ++ [I.Neut n]) <*> pure Nothing
919 I.TyCaseFun s [m, t, f] n -> asks makeTE >>= \te -> Fun (show s) <$> f_ (I.nType s, I.TType) <*> chain "tycase" [] (I.nType s) ([m, t, I.Neut n, f]) <*> pure Nothing
918 I.Neut (I.App_ a b) -> asks makeTE >>= \te -> do 920 I.Neut (I.App_ a b) -> asks makeTE >>= \te -> do
919 let t = I.neutType te a 921 let t = I.neutType te a
920 app' <$> f_ (I.Neut a, t) <*> (head <$> chain [] t [b]) 922 app' <$> f_ (I.Neut a, t) <*> (head <$> chain "app" [] t [b])
921 I.ELit l -> pure $ ELit l 923 I.ELit l -> pure $ ELit l
922 I.TType -> pure TType 924 I.TType -> pure TType
923 I.FixLabel_ _ _ x -> f_ (x, et) 925 I.FixLabel_ _ _ _ x -> f_ (x, et)
924-- I.LabelEnd x -> f x -- not possible 926 I.LabelEnd x -> f_ (x, et)
925 z -> error $ "toExp: " ++ show z 927 z -> error $ "toExp: " ++ show z
926 928
927 chain acc t [] = return $ reverse acc 929 mkDef i I.Delta _ = return Nothing
928 chain acc t@(I.Pi b at y) (a: as) = do 930 mkDef i _ _ = return Nothing
931-- mkDef i def et = Just <$> f_ (iterateN i I.Lam $ I.Neut def, et)
932
933 chain e acc t [] = return $ reverse acc
934 chain e acc t@({-I.unfixlabel -> -} I.Pi b at y) (a: as) = do
929 a' <- f_ (a, at) 935 a' <- f_ (a, at)
930 chain (a': acc) (I.appTy t a) as 936 chain e (a': acc) (I.appTy t a) as
937 chain e acc t _ = error $ "chain: " ++ e ++ show t
931 938
932 xs !!! i | i < 0 || i >= length xs = error $ show xs ++ " !! " ++ show i 939 xs !!! i | i < 0 || i >= length xs = error $ show xs ++ " !! " ++ show i
933 xs !!! i = xs !! i 940 xs !!! i = xs !! i
@@ -943,7 +950,7 @@ freeVars = \case
943 Var n _ -> Set.singleton n 950 Var n _ -> Set.singleton n
944 Con _ _ xs -> mconcat $ map freeVars xs 951 Con _ _ xs -> mconcat $ map freeVars xs
945 ELit _ -> mempty 952 ELit _ -> mempty
946 Fun _ _ xs -> mconcat $ map freeVars xs 953 Fun _ _ xs md -> foldMap freeVars xs <> foldMap freeVars md
947 EApp a b -> freeVars a <> freeVars b 954 EApp a b -> freeVars a <> freeVars b
948 Pi _ n a b -> freeVars a <> Set.delete n (freeVars b) 955 Pi _ n a b -> freeVars a <> Set.delete n (freeVars b)
949 Lam _ n a b -> freeVars a <> foldr Set.delete (freeVars b) (patVars n) 956 Lam _ n a b -> freeVars a <> foldr Set.delete (freeVars b) (patVars n)
@@ -959,7 +966,7 @@ tyOf = \case
959 Var _ t -> t 966 Var _ t -> t
960 Pi{} -> TType 967 Pi{} -> TType
961 Con _ t xs -> foldl app t xs 968 Con _ t xs -> foldl app t xs
962 Fun _ t xs -> foldl app t xs 969 Fun _ t xs _ -> foldl app t xs
963 ELit l -> toExp (I.litType l, I.TType) 970 ELit l -> toExp (I.litType l, I.TType)
964 TType -> TType 971 TType -> TType
965 ELet a b c -> tyOf $ EApp (ELam a c) b 972 ELet a b c -> tyOf $ EApp (ELam a c) b
@@ -974,7 +981,7 @@ substE n x = \case
974 Pi h n' a b -> Pi h n' (substE n x a) (substE n x b) 981 Pi h n' a b -> Pi h n' (substE n x a) (substE n x b)
975 Lam h n' a b -> Lam h n' (substE n x a) $ if n `elem` patVars n' then b else substE n x b 982 Lam h n' a b -> Lam h n' (substE n x a) $ if n `elem` patVars n' then b else substE n x b
976 Con n' cn xs -> Con n' cn (map (substE n x) xs) 983 Con n' cn xs -> Con n' cn (map (substE n x) xs)
977 Fun n' cn xs -> Fun n' cn (map (substE n x) xs) 984 Fun n' cn xs md -> Fun n' cn (substE n x <$> xs) (substE n x <$> md)
978 TType -> TType 985 TType -> TType
979 EApp a b -> app' (substE n x a) (substE n x b) 986 EApp a b -> app' (substE n x a) (substE n x b)
980 x@ELit{} -> x 987 x@ELit{} -> x
@@ -1046,7 +1053,7 @@ idFun t = Lam Visible (PVar t n) t (Var n t) where n = "id"
1046pattern a :~> b = Pi Visible "" a b 1053pattern a :~> b = Pi Visible "" a b
1047infixr 1 :~> 1054infixr 1 :~>
1048 1055
1049pattern PrimN n xs <- Fun n t (filterRelevant (n, 0) t -> xs) where PrimN n xs = Fun n (builtinType n) xs 1056pattern PrimN n xs <- Fun n t (filterRelevant (n, 0) t -> xs) _ where PrimN n xs = Fun n (builtinType n) xs Nothing
1050pattern Prim0 n = PrimN n [] 1057pattern Prim0 n = PrimN n []
1051pattern Prim1 n a = PrimN n [a] 1058pattern Prim1 n a = PrimN n [a]
1052pattern Prim2 n a b = PrimN n [a, b] 1059pattern Prim2 n a b = PrimN n [a, b]
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index a7389691..b74f978f 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -16,8 +16,8 @@
16-- {-# OPTIONS_GHC -O0 #-} 16-- {-# OPTIONS_GHC -O0 #-}
17module LambdaCube.Compiler.Infer 17module LambdaCube.Compiler.Infer
18 ( Binder (..), SName, Lit(..), Visibility(..), Export(..), Module(..) 18 ( Binder (..), SName, Lit(..), Visibility(..), Export(..), Module(..)
19 , Exp (..), 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_, pattern LabelEnd
21 , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun 21 , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun
22 , outputType, boolType, trueExp 22 , outputType, boolType, trueExp
23 , down 23 , down
@@ -27,7 +27,7 @@ module LambdaCube.Compiler.Infer
27 , ImportItems (..) 27 , ImportItems (..)
28 , SI(..), Range(..) 28 , SI(..), Range(..)
29 , nType, conType, neutType, appTy, mkConPars, makeCaseFunPars 29 , nType, conType, neutType, appTy, mkConPars, makeCaseFunPars
30 , MaxDB(..) 30 , MaxDB(..), unfixlabel
31 ) where 31 ) where
32import Data.Monoid 32import Data.Monoid
33import Data.Maybe 33import Data.Maybe
@@ -56,9 +56,11 @@ data Exp
56 | Pi_ MaxDB Visibility Exp Exp 56 | Pi_ MaxDB Visibility Exp Exp
57 | Lam_ MaxDB Exp 57 | Lam_ MaxDB Exp
58 | Neut Neutral 58 | Neut Neutral
59 | FixLabel_ FunName [Exp] Exp{-unfolded expression-} 59 | FixLabel_ FunName [Exp] Exp{-definition-} Exp{-cached unfolded expression-}
60 deriving (Show) 60 deriving (Show)
61 61
62pattern FixLabel f xs e <- FixLabel_ f xs e _ where FixLabel f xs e = let x = FixLabel_ f xs e (subst 0 x e) in x
63
62data Neutral 64data Neutral
63 = Fun_ MaxDB FunName !Int{-number of missing parameters-} [Exp]{-given parameters-} Neutral{-unfolded expression-} 65 = Fun_ MaxDB FunName !Int{-number of missing parameters-} [Exp]{-given parameters-} Neutral{-unfolded expression-}
64 | CaseFun__ MaxDB CaseFunName [Exp] Neutral 66 | CaseFun__ MaxDB CaseFunName [Exp] Neutral
@@ -102,7 +104,7 @@ instance Eq TyCaseFunName where TyCaseFunName n _ == TyCaseFunName n' _ = n == n
102infixl 2 `App`, `app_` 104infixl 2 `App`, `app_`
103infixr 1 :~> 105infixr 1 :~>
104 106
105pattern Fun f i xs n <- Fun_ _ f i xs n where Fun f i xs n = Fun_ (foldMap maxDB_ xs {- <> iterateN i lowerDB (maxDB_ n)-}) f i xs n 107pattern Fun f i xs n <- Fun_ _ f i xs n where Fun f i xs n = Fun_ (foldMap maxDB_ xs <> iterateN i lowerDB (maxDB_ n)) f i xs n
106pattern UFunN a b <- Neut (Fun (FunName a _) _ b _) 108pattern UFunN a b <- Neut (Fun (FunName a _) _ b _)
107pattern UTFun a t b <- Neut (Fun (FunName a t) _ b _) 109pattern UTFun a t b <- Neut (Fun (FunName a t) _ b _)
108pattern DFun_ fn xs = Fun fn 0 xs Delta 110pattern DFun_ fn xs = Fun fn 0 xs Delta
@@ -238,7 +240,7 @@ outputType = TOutput
238boolType = TBool 240boolType = TBool
239trueExp = EBool True 241trueExp = EBool True
240 242
241-------------------------------------------------------------------------------- FixLabel handling 243-------------------------------------------------------------------------------- label handling
242 244
243pattern LabelEnd x = Neut (LabelEnd_ x) 245pattern LabelEnd x = Neut (LabelEnd_ x)
244 246
@@ -249,15 +251,15 @@ pmLabel' f i xs (unfixlabel -> Neut y) = Neut $ Fun f i xs y
249pmLabel' f i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y) 251pmLabel' f i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y)
250 252
251pmLabel :: FunName -> Int -> [Exp] -> Exp -> Exp 253pmLabel :: FunName -> Int -> [Exp] -> Exp -> Exp
252pmLabel f i xs e = pmLabel' f (i + numLams e') xs (Neut $ dropLams e') where e' = unfixlabel e 254pmLabel f i xs e = pmLabel' f (i + numLams e) xs (Neut $ dropLams e)
253 255
254dropLams (Lam x) = dropLams x 256dropLams (unfixlabel -> Lam x) = dropLams x
255dropLams (Neut x) = x 257dropLams (unfixlabel -> Neut x) = x
256 258
257numLams (Lam x) = 1 + numLams x 259numLams (unfixlabel -> Lam x) = 1 + numLams x
258numLams x = 0 260numLams x = 0
259 261
260unfixlabel (FixLabel_ _ _ a) = unfixlabel a 262unfixlabel (FixLabel_ _ _ _ a) = unfixlabel a
261unfixlabel a = a 263unfixlabel a = a
262 264
263unlabelend (LabelEnd a) = unlabelend a 265unlabelend (LabelEnd a) = unlabelend a
@@ -274,9 +276,9 @@ down t x | used t x = Nothing
274 | otherwise = Just $ subst t (error "impossible: down" :: Exp) x 276 | otherwise = Just $ subst t (error "impossible: down" :: Exp) x
275 277
276instance Eq Exp where 278instance Eq Exp where
277 FixLabel_ f a _ == FixLabel_ f' a' _ = (f, a) == (f', a') 279 FixLabel_ f a _ _ == FixLabel_ f' a' _ _ = (f, a) == (f', a')
278 FixLabel_ _ _ a == a' = a == a' 280 FixLabel_ _ _ _ a == a' = a == a'
279 a == FixLabel_ _ _ a' = a == a' 281 a == FixLabel_ _ _ _ a' = a == a'
280 LabelEnd a == a' = a == a' 282 LabelEnd a == a' = a == a'
281 a == LabelEnd a' = a == a' 283 a == LabelEnd a' = a == a'
282 Lam a == Lam a' = a == a' 284 Lam a == Lam a' = a == a'
@@ -289,41 +291,34 @@ instance Eq Exp where
289 _ == _ = False 291 _ == _ = False
290 292
291instance Eq Neutral where 293instance Eq Neutral where
292 Fun f i a _ == Fun f' i' a' _ = (f, i, a) == (f', i', a') 294 Fun f i a _ == Fun f' i' a' _ = (f, i, a) == (f', i', a') -- TODO: compare by definition / compare by id
293 CaseFun_ a b c == CaseFun_ a' b' c' = (a, b, c) == (a', b', c') 295 CaseFun_ a b c == CaseFun_ a' b' c' = (a, b, c) == (a', b', c')
294 TyCaseFun_ a b c == TyCaseFun_ a' b' c' = (a, b, c) == (a', b', c') 296 TyCaseFun_ a b c == TyCaseFun_ a' b' c' = (a, b, c) == (a', b', c')
295 App_ a b == App_ a' b' = (a, b) == (a', b') 297 App_ a b == App_ a' b' = (a, b) == (a', b')
296 Var_ a == Var_ a' = a == a' 298 Var_ a == Var_ a' = a == a'
297 _ == _ = False 299 _ == _ = False
298 300
299isClosed (maxDB_ -> MaxDB x) = isNothing x 301free x | cmpDB 0 x = mempty
300
301-- 0 means that no free variable is used
302-- 1 means that only var 0 is used
303maxDB = max 0 . fromMaybe 0 . getMaxDB . maxDB_
304upDB n (MaxDB i) = MaxDB $ (\x -> if x == 0 then x else x+n) <$> i
305
306free x | isClosed x = mempty
307free x = fold (\i k -> Set.fromList [k - i | k >= i]) 0 x 302free x = fold (\i k -> Set.fromList [k - i | k >= i]) 0 x
308 303
309instance Up Exp where 304instance Up Exp where
310 up_ 0 = \_ e -> e 305 up_ 0 = \_ e -> e
311 up_ n = f where 306 up_ n = f where
312 f i e | isClosed e = e 307 f i e | cmpDB i e = e
313 f i e = case e of 308 f i e = case e of
314 Lam_ md b -> Lam_ (upDB n md) (f (i+1) b) 309 Lam_ md b -> Lam_ (upDB n md) (f (i+1) b)
315 Pi_ md h a b -> Pi_ (upDB n md) h (f i a) (f (i+1) b) 310 Pi_ md h a b -> Pi_ (upDB n md) h (f i a) (f (i+1) b)
316 Con_ md s pn as -> Con_ (upDB n md) s pn $ map (f i) as 311 Con_ md s pn as -> Con_ (upDB n md) s pn $ map (f i) as
317 TyCon_ md s as -> TyCon_ (upDB n md) s $ map (f i) as 312 TyCon_ md s as -> TyCon_ (upDB n md) s $ map (f i) as
318 Neut x -> Neut $ up_ n i x 313 Neut x -> Neut $ up_ n i x
319 FixLabel_ fn xs y -> FixLabel_ fn (f i <$> xs) $ f i y 314 FixLabel_ fn xs y u -> FixLabel_ fn (f i <$> xs) (f (i+1) y) (f i u)
320 315
321 used i e 316 used i e
322 | i >= maxDB e = False 317 | cmpDB i e = False
323 | otherwise = ((getAny .) . fold ((Any .) . (==))) i e 318 | otherwise = ((getAny .) . fold ((Any .) . (==))) i e
324 319
325 fold f i = \case 320 fold f i = \case
326 FixLabel_ _ _ x -> fold f i x 321 FixLabel_ _ x y _ -> foldMap (fold f i) x -- <> fold f (i+1) y
327 Lam b -> {-fold f i t <> todo: explain why this is not needed -} fold f (i+1) b 322 Lam b -> {-fold f i t <> todo: explain why this is not needed -} fold f (i+1) b
328 Pi _ a b -> fold f i a <> fold f (i+1) b 323 Pi _ a b -> fold f i a <> fold f (i+1) b
329 Con _ _ as -> foldMap (fold f i) as 324 Con _ _ as -> foldMap (fold f i) as
@@ -339,7 +334,7 @@ instance Up Exp where
339 TyCon_ c _ _ -> c 334 TyCon_ c _ _ -> c
340 335
341 Neut x -> maxDB_ x 336 Neut x -> maxDB_ x
342 FixLabel_ f x y -> foldMap maxDB_ x <> maxDB_ y 337 FixLabel_ f x y _ -> foldMap maxDB_ x -- <> lowerDB (maxDB_ y)
343 TType -> mempty 338 TType -> mempty
344 ELit _ -> mempty 339 ELit _ -> mempty
345 340
@@ -349,7 +344,7 @@ instance Up Exp where
349 Con_ _ a b c -> Con_ mempty a b (closedExp <$> c) 344 Con_ _ a b c -> Con_ mempty a b (closedExp <$> c)
350 TyCon_ _ a b -> TyCon_ mempty a (closedExp <$> b) 345 TyCon_ _ a b -> TyCon_ mempty a (closedExp <$> b)
351 Neut a -> Neut $ closedExp a 346 Neut a -> Neut $ closedExp a
352 FixLabel_ fn xs b -> FixLabel_ fn (closedExp <$> xs) (closedExp b) 347 FixLabel_ fn xs b u -> FixLabel_ fn (closedExp <$> xs) b u
353 e@TType{} -> e 348 e@TType{} -> e
354 e@ELit{} -> e 349 e@ELit{} -> e
355 350
@@ -358,7 +353,7 @@ instance Subst Exp Exp where
358 where 353 where
359 f i (Neut n) = substNeut n 354 f i (Neut n) = substNeut n
360 where 355 where
361 substNeut e | isClosed e = Neut e 356 substNeut e | cmpDB i e = Neut e
362 substNeut e = case e of 357 substNeut e = case e of
363 Var_ k -> case compare k i of GT -> Var $ k - 1; LT -> Var k; EQ -> up (i - i0) x 358 Var_ k -> case compare k i of GT -> Var $ k - 1; LT -> Var k; EQ -> up (i - i0) x
364 CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n) 359 CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n)
@@ -367,9 +362,9 @@ instance Subst Exp Exp where
367 Fun fn c xs v -> pmLabel' fn c (f i <$> xs) $ f (i + c) $ Neut v 362 Fun fn c xs v -> pmLabel' fn c (f i <$> xs) $ f (i + c) $ Neut v
368 LabelEnd_ a -> LabelEnd $ f i a 363 LabelEnd_ a -> LabelEnd $ f i a
369 Delta -> Neut Delta 364 Delta -> Neut Delta
370 f i e | {-i >= maxDB e-} isClosed e = e 365 f i e | cmpDB i e = e
371 f i e = case e of 366 f i e = case e of
372 FixLabel_ fn z v -> FixLabel_ fn (f i <$> z) $ f i v 367 FixLabel_ fn z v u -> FixLabel_ fn (f i <$> z) (f (i+1) v) (f i u)
373 Lam b -> Lam (f (i+1) b) 368 Lam b -> Lam (f (i+1) b)
374 Con s n as -> Con s n $ f i <$> as 369 Con s n as -> Con s n $ f i <$> as
375 Pi h a b -> Pi h (f i a) (f (i+1) b) 370 Pi h a b -> Pi h (f i a) (f (i+1) b)
@@ -379,7 +374,7 @@ instance Up Neutral where
379 374
380 up_ 0 = \_ e -> e 375 up_ 0 = \_ e -> e
381 up_ n = f where 376 up_ n = f where
382 f i e | isClosed e = e 377 f i e | cmpDB i e = e
383 f i e = case e of 378 f i e = case e of
384 Var_ k -> Var_ $ if k >= i then k+n else k 379 Var_ k -> Var_ $ if k >= i then k+n else k
385 CaseFun__ md s as ne -> CaseFun__ (upDB n md) s (up_ n i <$> as) (up_ n i ne) 380 CaseFun__ md s as ne -> CaseFun__ (upDB n md) s (up_ n i <$> as) (up_ n i ne)
@@ -390,7 +385,7 @@ instance Up Neutral where
390 Delta -> Delta 385 Delta -> Delta
391 386
392 used i e 387 used i e
393 | i >= maxDB e = False 388 | cmpDB i e = False
394 | otherwise = ((getAny .) . fold ((Any .) . (==))) i e 389 | otherwise = ((getAny .) . fold ((Any .) . (==))) i e
395 390
396 fold f i = \case 391 fold f i = \case
@@ -398,7 +393,7 @@ instance Up Neutral where
398 CaseFun_ _ as n -> foldMap (fold f i) as <> fold f i n 393 CaseFun_ _ as n -> foldMap (fold f i) as <> fold f i n
399 TyCaseFun_ _ as n -> foldMap (fold f i) as <> fold f i n 394 TyCaseFun_ _ as n -> foldMap (fold f i) as <> fold f i n
400 App_ a b -> fold f i a <> fold f i b 395 App_ a b -> fold f i a <> fold f i b
401 Fun _ _ x _ -> foldMap (fold f i) x 396 Fun _ j x d -> foldMap (fold f i) x <> fold f (i+j) d
402 LabelEnd_ x -> fold f i x 397 LabelEnd_ x -> fold f i x
403 Delta -> mempty 398 Delta -> mempty
404 399
@@ -439,11 +434,11 @@ evalCaseFun a ps (Con n@(ConName _ i _) _ vs)
439 xs !!! i = xs !! i 434 xs !!! i = xs !! i
440 435
441evalCaseFun a b (Neut c) = CaseFun a b c 436evalCaseFun a b (Neut c) = CaseFun a b c
442evalCaseFun a b (FixLabel_ _ _ c) = evalCaseFun a b c 437evalCaseFun a b (FixLabel_ _ _ _ c) = evalCaseFun a b c
443evalCaseFun a b x = error $ "evalCaseFun: " ++ show (a, x) 438evalCaseFun a b x = error $ "evalCaseFun: " ++ show (a, x)
444 439
445evalTyCaseFun a b (Neut c) = TyCaseFun a b c 440evalTyCaseFun a b (Neut c) = TyCaseFun a b c
446evalTyCaseFun a b (FixLabel_ _ _ c) = evalTyCaseFun a b c 441evalTyCaseFun a b (FixLabel_ _ _ _ c) = evalTyCaseFun a b c
447evalTyCaseFun (TyCaseFunName "match'Type" ty) [_, t, f] TType = t 442evalTyCaseFun (TyCaseFunName "match'Type" ty) [_, t, f] TType = t
448evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (TyCon (TyConName n' _ _ _ _) vs) | n == n' = foldl app_ t vs 443evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (TyCon (TyConName n' _ _ _ _) vs) | n == n' = foldl app_ t vs
449--evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (DFun (FunName n' _) vs) | n == n' = foldl app_ t vs -- hack 444--evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (DFun (FunName n' _) vs) | n == n' = foldl app_ t vs -- hack
@@ -522,8 +517,8 @@ cstr = f []
522 f _ _ a a' | a == a' = Unit 517 f _ _ a a' | a == a' = Unit
523 f ns typ (LabelEnd a) a' = f ns typ a a' 518 f ns typ (LabelEnd a) a' = f ns typ a a'
524 f ns typ a (LabelEnd a') = f ns typ a a' 519 f ns typ a (LabelEnd a') = f ns typ a a'
525 f ns typ (FixLabel_ _ _ a) a' = f ns typ a a' 520 f ns typ (FixLabel_ _ _ _ a) a' = f ns typ a a'
526 f ns typ a (FixLabel_ _ _ a') = f ns typ a a' 521 f ns typ a (FixLabel_ _ _ _ a') = f ns typ a a'
527 f ns typ (Con a n xs) (Con a' n' xs') | a == a' && n == n' && length xs == length xs' = 522 f ns typ (Con a n xs) (Con a' n' xs') | a == a' && n == n' && length xs == length xs' =
528 if null xs then Unit else ff ns (foldl appTy (conType typ a) $ mkConPars n typ) $ zip xs xs' 523 if null xs then Unit else ff ns (foldl appTy (conType typ a) $ mkConPars n typ) $ zip xs xs'
529 f ns typ (TyCon a xs) (TyCon a' xs') | a == a' && length xs == length xs' = 524 f ns typ (TyCon a xs) (TyCon a' xs') | a == a' && length xs == length xs' =
@@ -612,7 +607,7 @@ app_ :: Exp -> Exp -> Exp
612app_ (Lam x) a = subst 0 a x 607app_ (Lam x) a = subst 0 a x
613app_ (Con s n xs) a = if n < conParams s then Con s (n+1) xs else Con s n (xs ++ [a]) 608app_ (Con s n xs) a = if n < conParams s then Con s (n+1) xs else Con s n (xs ++ [a])
614app_ (TyCon s xs) a = TyCon s (xs ++ [a]) 609app_ (TyCon s xs) a = TyCon s (xs ++ [a])
615app_ (FixLabel_ f xs e) a = FixLabel_ f (xs ++ [a]) $ app_ e a 610app_ (FixLabel_ f xs e u) a = FixLabel_ f (xs ++ [a]) (app_ e $ up1 a) (app_ u a)
616app_ (Neut f) a = neutApp f a 611app_ (Neut f) a = neutApp f a
617 612
618neutApp (Fun f i xs e) a | i > 0 = pmLabel f (i-1) (xs ++ [a]) (subst (i-1) (up (i-1) a) $ Neut e) 613neutApp (Fun f i xs e) a | i > 0 = pmLabel f (i-1) (xs ++ [a]) (subst (i-1) (up (i-1) a) $ Neut e)
@@ -724,6 +719,9 @@ neutType te = \case
724 TyCaseFun_ s [m, t, f] n -> foldl appTy (nType s) [m, t, Neut n, f] 719 TyCaseFun_ s [m, t, f] n -> foldl appTy (nType s) [m, t, Neut n, f]
725 Fun s _ a _ -> foldl appTy (nType s) a 720 Fun s _ a _ -> foldl appTy (nType s) a
726 721
722mkExpTypes t [] = []
723mkExpTypes t@(Pi _ a _) (x: xs) = (x, t): mkExpTypes (appTy t x) xs
724
727appTy (Pi _ a b) x = subst 0 x b 725appTy (Pi _ a b) x = subst 0 x b
728appTy t x = error $ "appTy: " ++ show t 726appTy t x = error $ "appTy: " ++ show t
729 727
@@ -972,8 +970,8 @@ recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt)
972 (TyCon s as, zt) -> checkApps (show s) [] zt (TyCon s) te (nType s) as 970 (TyCon s as, zt) -> checkApps (show s) [] zt (TyCon s) te (nType s) as
973 (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]) 971 (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])
974 (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] 972 (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]
975 (FixLabel_ f a x, zt) -> checkApps "fixlab" [] zt (\xs -> FixLabel_ f xs x) te (nType f) a 973 (FixLabel_ f a x u, zt) -> checkApps "fixlab" [] zt (\xs -> FixLabel_ f xs x u) te (nType f) a -- TODO: recheck x
976 (Neut (Fun f i a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun f i xs x) te (nType f) a 974 (Neut (Fun f i a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun f i xs x) te (nType f) a -- TODO: recheck x
977 (LabelEnd x, zt) -> LabelEnd $ recheck_ msg te (x, zt) 975 (LabelEnd x, zt) -> LabelEnd $ recheck_ msg te (x, zt)
978 (Neut Delta, zt) -> Neut Delta 976 (Neut Delta, zt) -> Neut Delta
979 where 977 where
@@ -1170,7 +1168,7 @@ mkELet (True, n) x t{-type of x-} = term
1170 fn = FunName (snd n) t 1168 fn = FunName (snd n) t
1171 1169
1172 par (Lam z) i = Lam $ par z (i+1) 1170 par (Lam z) i = Lam $ par z (i+1)
1173 par (FunN "primFix" [_, f]) i = f `app_` FixLabel_ fn (downTo 0 i) (foldl app_ term $ downTo 0 i) 1171 par (FunN "primFix" [_, f@(Lam f')]) i = f `app_` FixLabel fn (downTo 0 i) (pmLabel fn 0 (downTo 1 i) f')
1174 par x _ = x 1172 par x _ = x
1175 1173
1176removeHiddenUnit (Pi Hidden Unit (down 0 -> Just t)) = removeHiddenUnit t 1174removeHiddenUnit (Pi Hidden Unit (down 0 -> Just t)) = removeHiddenUnit t
@@ -1327,7 +1325,7 @@ instance MkDoc Exp where
1327 mkDoc ts e = fmap inGreen <$> f e 1325 mkDoc ts e = fmap inGreen <$> f e
1328 where 1326 where
1329 f = \case 1327 f = \case
1330 FixLabel_ _ _ x -> f x 1328 FixLabel_ _ _ _ x -> f x
1331 Neut x -> mkDoc ts x 1329 Neut x -> mkDoc ts x
1332-- Lam h a b -> join $ shLam (used 0 b) (BLam h) <$> f a <*> pure (f b) 1330-- Lam h a b -> join $ shLam (used 0 b) (BLam h) <$> f a <*> pure (f b)
1333 Lam b -> join $ shLam True (BLam Visible) <$> f TType{-todo-} <*> pure (f b) 1331 Lam b -> join $ shLam True (BLam Visible) <$> f TType{-todo-} <*> pure (f b)
@@ -1346,12 +1344,13 @@ instance MkDoc Neutral where
1346 where 1344 where
1347 g = mkDoc ts 1345 g = mkDoc ts
1348 f = \case 1346 f = \case
1349 CstrT' TType a b -> shCstr <$> g a <*> g b 1347 CstrT' t a b -> shCstr <$> g (a, t) <*> g (b, t)
1350 Fun s i xs _ -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g xs 1348 Fun s i (mkExpTypes (nType s) -> xs) _ -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g xs
1351 Var_ k -> shAtom <$> shVar k 1349 Var_ k -> shAtom <$> shVar k
1352 App_ a b -> shApp Visible <$> g a <*> g b 1350 App_ a b -> shApp Visible <$> g a <*> g b
1353 CaseFun_ s xs n -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g (xs ++ [Neut n]) 1351 CaseFun_ s xs n -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g ({-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n])
1354 TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g [m, t, Neut n, f] 1352 TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g (mkExpTypes (nType s) [m, t, Neut n, f])
1353 Delta -> return $ shAtom "^delta"
1355 1354
1356 shAtom_ = shAtom . if ts then switchTick else id 1355 shAtom_ = shAtom . if ts then switchTick else id
1357 1356
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 639b7d76..30739342 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 16 , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam
17 , pattern TyType, pattern Wildcard_ 17 , pattern TyType, pattern Wildcard_
18 , debug, LI, isPi, varDB, lowerDB, MaxDB (..), iterateN, traceD, parseLC 18 , debug, LI, isPi, varDB, lowerDB, justDB, upDB, cmpDB, MaxDB (..), iterateN, traceD, parseLC
19 , getParamsS, addParamsS, getApps, apps', downToS, addForalls 19 , getParamsS, addParamsS, getApps, apps', downToS, addForalls
20 , mkDesugarInfo, joinDesugarInfo 20 , mkDesugarInfo, joinDesugarInfo
21 , throwErrorTCM, ErrorMsg(..), ErrorT 21 , throwErrorTCM, ErrorMsg(..), ErrorT
@@ -164,8 +164,6 @@ getApps = second reverse . run where
164 164
165downToS n m = map (SVar (debugSI "20", ".ds")) [n+m-1, n+m-2..n] 165downToS n m = map (SVar (debugSI "20", ".ds")) [n+m-1, n+m-2..n]
166 166
167xSLabelEnd = id --SLabelEnd
168
169instance SourceInfo (SExp' a) where 167instance SourceInfo (SExp' a) where
170 sourceInfo = \case 168 sourceInfo = \case
171 SGlobal (si, _) -> si 169 SGlobal (si, _) -> si
@@ -199,7 +197,15 @@ instance Show MaxDB where show _ = "MaxDB"
199varDB i = MaxDB $ Just $ i + 1 197varDB i = MaxDB $ Just $ i + 1
200 198
201lowerDB (MaxDB i) = MaxDB $ (+ (- 1)) <$> i 199lowerDB (MaxDB i) = MaxDB $ (+ (- 1)) <$> i
202--lowerDB' l (MaxDB i) = MaxDB $ Just $ 1 + max l (fromMaybe 0 i) 200justDB (MaxDB i) = MaxDB $ Just $ fromMaybe 0 i
201
202-- 0 means that no free variable is used
203-- 1 means that only var 0 is used
204--cmpDB i e = i >= maxDB e
205cmpDB _ (maxDB_ -> MaxDB x) = isNothing x
206
207maxDB = max 0 . fromMaybe 0 . getMaxDB . maxDB_
208upDB n (MaxDB i) = MaxDB $ (\x -> if x == 0 then x else x+n) <$> i
203 209
204class Up a where 210class Up a where
205 up_ :: Int -> Int -> a -> a 211 up_ :: Int -> Int -> a -> a
@@ -872,6 +878,8 @@ mkLets a ds = mkLets' a ds . sortDefs ds where
872 = SLet (False, n) (maybe id (flip SAnn . addForalls {-todo-}[] []) mt x) (substSG0 n $ mkLets' True ge ds e) 878 = SLet (False, n) (maybe id (flip SAnn . addForalls {-todo-}[] []) mt x) (substSG0 n $ mkLets' True ge ds e)
873 mkLets' _ _ (x: ds) e = error $ "mkLets: " ++ show x 879 mkLets' _ _ (x: ds) e = error $ "mkLets: " ++ show x
874 880
881xSLabelEnd = id --SLabelEnd
882
875addForalls :: Up a => Extensions -> [SName] -> SExp' a -> SExp' a 883addForalls :: Up a => Extensions -> [SName] -> SExp' a -> SExp' a
876addForalls exs defined x = foldl f x [v | v@(_, vh:_) <- reverse $ freeS x, snd v `notElem'` ("fromInt"{-todo: remove-}: defined), isLower vh || NoConstructorNamespace `elem` exs] 884addForalls exs defined x = foldl f x [v | v@(_, vh:_) <- reverse $ freeS x, snd v `notElem'` ("fromInt"{-todo: remove-}: defined), isLower vh || NoConstructorNamespace `elem` exs]
877 where 885 where