summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-04 06:23:15 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-04 06:23:15 +0200
commitec0986584ec645d0649d63803d6be474bbff7f88 (patch)
tree5e521ebe16dce0fe2f2675cec3ff0ac51b5451d3 /src
parent3bf9f9d19378e5e7243c0c951979e734d4c7780a (diff)
refactoring
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Core.hs52
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs2
-rw-r--r--src/LambdaCube/Compiler/Infer.hs10
-rw-r--r--src/LambdaCube/Compiler/Parser.hs228
4 files changed, 153 insertions, 139 deletions
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs
index bacf769b..15a37c0f 100644
--- a/src/LambdaCube/Compiler/Core.hs
+++ b/src/LambdaCube/Compiler/Core.hs
@@ -206,7 +206,7 @@ data Neutral
206 | App__ !MaxDB Neutral Exp 206 | App__ !MaxDB Neutral Exp
207 | CaseFun__ !MaxDB CaseFunName [Exp] Neutral 207 | CaseFun__ !MaxDB CaseFunName [Exp] Neutral
208 | TyCaseFun__ !MaxDB TyCaseFunName [Exp] Neutral 208 | TyCaseFun__ !MaxDB TyCaseFunName [Exp] Neutral
209 | Fun_ !MaxDB FunName [Exp]{-local vars-} !Int{-number of missing parameters-} [Exp]{-given parameters, reversed-} Neutral{-unfolded expression-}{-not neut?-} 209 | Fun_ !MaxDB FunName [Exp]{-local vars-} [Exp]{-given parameters, reversed-} Exp{-unfolded expression-}
210 | RHS_ Exp -- not neut? 210 | RHS_ Exp -- not neut?
211 | Delta (SData ([Exp] -> Exp)) -- not neut? 211 | Delta (SData ([Exp] -> Exp)) -- not neut?
212 212
@@ -225,16 +225,19 @@ infixr 1 :~>
225 225
226pattern NoLE <- (isNoRHS -> True) 226pattern NoLE <- (isNoRHS -> True)
227 227
228isNoRHS (RHS_ _) = False 228isNoRHS (Neut (RHS_ _)) = False
229isNoRHS _ = True 229isNoRHS _ = True
230 230
231pattern 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 231getLams' (Lam e) = first (+1) $ getLams' e
232pattern Fun f i xs n = Fun' f [] i xs n 232getLams' e = (0, e)
233pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) _ (reverse -> b) NoLE)) 233
234pattern Fun' f vs xs n <- Fun_ _ f vs xs n where Fun' f vs xs n = Fun_ (foldMap maxDB_ vs <> foldMap maxDB_ xs {- <> iterateN i lowerDB (maxDB_ n)-}) f vs xs n
235pattern Fun f xs n = Fun' f [] xs n
236pattern UTFun a t b <- (unfixlabel -> Neut (Fun (FunName a _ t) (reverse -> b) NoLE))
234pattern UFunN a b <- UTFun a _ b 237pattern UFunN a b <- UTFun a _ b
235pattern DFun_ fn xs <- Fun fn 0 (reverse -> xs) (Delta _) where 238pattern DFun_ fn xs <- Fun fn (reverse -> xs) (Neut (Delta _)) where
236 DFun_ fn@(FunName n _ _) xs = Fun fn 0 (reverse xs) d where 239 DFun_ fn@(FunName n _ _) xs = Fun fn (reverse xs) d where
237 d = Delta $ SData $ getFunDef n $ \xs -> Neut $ Fun fn 0 (reverse xs) d 240 d = Neut $ Delta $ SData $ getFunDef n $ \xs -> Neut $ Fun fn (reverse xs) d
238pattern TFun' a t b = DFun_ (FunName a Nothing t) b 241pattern TFun' a t b = DFun_ (FunName a Nothing t) b
239pattern TFun a t b = Neut (TFun' a t b) 242pattern TFun a t b = Neut (TFun' a t b)
240 243
@@ -337,12 +340,11 @@ trueExp = EBool True
337pattern RHS x = Neut (RHS_ x) 340pattern RHS x = Neut (RHS_ x)
338 341
339--pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp 342--pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp
340pmLabel' _ (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as 343pmLabel' _ (FunName _ _ _) _ as (Neut (Delta (SData f))) = f $ reverse as
341pmLabel' md f vs i xs (unfixlabel -> Neut y) = Neut $ Fun_ md f vs i xs y 344pmLabel' md f vs xs (unfixlabel -> y) = Neut $ Fun_ md f vs xs y
342pmLabel' _ f _ i xs (unfixlabel -> y) = error $ "pmLabel:\n" ++ ppShow (f, i, length xs) ++ "\n" ++ ppShow y --show (f, i, length xs, y)
343 345
344pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp 346pmLabel :: FunName -> [Exp] -> [Exp] -> Exp -> Exp
345pmLabel f vs i xs e = pmLabel' (foldMap maxDB_ vs <> foldMap maxDB_ xs) f vs (i + numLams e) xs (Neut $ dropLams e) 347pmLabel f vs xs e = pmLabel' (foldMap maxDB_ vs <> foldMap maxDB_ xs) f vs xs e
346 348
347dropLams (unfixlabel -> Lam x) = dropLams x 349dropLams (unfixlabel -> Lam x) = dropLams x
348dropLams (unfixlabel -> Neut x) = x 350dropLams (unfixlabel -> Neut x) = x
@@ -350,12 +352,12 @@ dropLams (unfixlabel -> Neut x) = x
350numLams (unfixlabel -> Lam x) = 1 + numLams x 352numLams (unfixlabel -> Lam x) = 1 + numLams x
351numLams x = 0 353numLams x = 0
352 354
353pattern FL' y <- Fun' f _ 0 xs (RHS_ y) 355pattern FL' y <- Fun' f _ xs (Neut (RHS_ y))
354pattern FL y <- Neut (FL' y) 356pattern FL y <- Neut (FL' y)
355 357
356pattern Func n def ty xs <- (mkFunc -> Just (n, def, ty, xs)) 358pattern Func n def ty xs <- (mkFunc -> Just (n, def, ty, xs))
357 359
358mkFunc (Neut (Fun (FunName n (Just def) ty) 0 xs RHS_{})) | Just def' <- removeLams (length xs) def = Just (n, def', ty, xs) 360mkFunc (Neut (Fun (FunName n (Just def) ty) xs (Neut RHS_{}))) | Just def' <- removeLams (length xs) def = Just (n, def', ty, xs)
359mkFunc _ = Nothing 361mkFunc _ = Nothing
360 362
361removeLams 0 (RHS x) = Just x 363removeLams 0 (RHS x) = Just x
@@ -364,10 +366,10 @@ removeLams _ _ = Nothing
364 366
365pattern UFL y <- (unFunc -> Just y) 367pattern UFL y <- (unFunc -> Just y)
366 368
367unFunc (Neut (Fun' (FunName _ (Just def) _) _ n xs y)) = Just $ iterateN n Lam $ Neut y 369unFunc (Neut (Fun' (FunName _ (Just def) _) _ xs y)) = Just y
368unFunc _ = Nothing 370unFunc _ = Nothing
369 371
370unFunc_ (Neut (Fun' _ _ n xs y)) = Just $ iterateN n Lam $ Neut y 372unFunc_ (Neut (Fun' _ _ xs y)) = Just y
371unFunc_ _ = Nothing 373unFunc_ _ = Nothing
372 374
373unfixlabel (FL y) = unfixlabel y 375unfixlabel (FL y) = unfixlabel y
@@ -406,7 +408,7 @@ instance Eq Exp where
406 _ == _ = False 408 _ == _ = False
407 409
408instance Eq Neutral where 410instance Eq Neutral where
409 Fun' f vs i a _ == Fun' f' vs' i' a' _ = (f, vs, i, a) == (f', vs', i', a') 411 Fun' f vs a _ == Fun' f' vs' a' _ = (f, vs, a) == (f', vs', a')
410 FL' a == a' = a == Neut a' 412 FL' a == a' = a == Neut a'
411 a == FL' a' = Neut a == a' 413 a == FL' a' = Neut a == a'
412 RHS_ a == RHS_ a' = a == a' 414 RHS_ a == RHS_ a' = a == a'
@@ -455,7 +457,7 @@ instance Subst Exp Exp where
455 CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n) 457 CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n)
456 TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n) 458 TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n)
457 App_ a b -> app_ (substNeut a) (f i b) 459 App_ a b -> app_ (substNeut a) (f i b)
458 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 460 Fun_ md fn vs xs v -> pmLabel' (md <> upDB i dx) fn (f i <$> vs) (f i <$> xs) $ f i v
459 RHS_ a -> RHS $ f i a 461 RHS_ a -> RHS $ f i a
460 d@Delta{} -> Neut d 462 d@Delta{} -> Neut d
461 f i e | cmpDB i e = e 463 f i e | cmpDB i e = e
@@ -483,7 +485,7 @@ instance Rearrange Neutral where
483 CaseFun__ md s as ne -> CaseFun__ (upDB_ g i md) s (rearrange i g <$> as) (rearrange i g ne) 485 CaseFun__ md s as ne -> CaseFun__ (upDB_ g i md) s (rearrange i g <$> as) (rearrange i g ne)
484 TyCaseFun__ md s as ne -> TyCaseFun__ (upDB_ g i md) s (rearrange i g <$> as) (rearrange i g ne) 486 TyCaseFun__ md s as ne -> TyCaseFun__ (upDB_ g i md) s (rearrange i g <$> as) (rearrange i g ne)
485 App__ md a b -> App__ (upDB_ g i md) (rearrange i g a) (rearrange i g b) 487 App__ md a b -> App__ (upDB_ g i md) (rearrange i g a) (rearrange i g b)
486 Fun_ md fn vs c x y -> Fun_ (upDB_ g i md) fn (rearrange i g <$> vs) c (rearrange i g <$> x) $ rearrange (i + c) g y 488 Fun_ md fn vs x y -> Fun_ (upDB_ g i md) fn (rearrange i g <$> vs) (rearrange i g <$> x) $ rearrange i g y
487 RHS_ x -> RHS_ $ rearrange i g x 489 RHS_ x -> RHS_ $ rearrange i g x
488 d@Delta{} -> d 490 d@Delta{} -> d
489 491
@@ -497,7 +499,7 @@ instance Up Neutral where
497 CaseFun_ _ as n -> foldMap (foldVar f i) as <> foldVar f i n 499 CaseFun_ _ as n -> foldMap (foldVar f i) as <> foldVar f i n
498 TyCaseFun_ _ as n -> foldMap (foldVar f i) as <> foldVar f i n 500 TyCaseFun_ _ as n -> foldMap (foldVar f i) as <> foldVar f i n
499 App_ a b -> foldVar f i a <> foldVar f i b 501 App_ a b -> foldVar f i a <> foldVar f i b
500 Fun' _ vs j x d -> foldMap (foldVar f i) vs <> foldMap (foldVar f i) x -- <> foldVar f (i+j) d 502 Fun' _ vs x d -> foldMap (foldVar f i) vs <> foldMap (foldVar f i) x -- <> foldVar f i d
501 RHS_ x -> foldVar f i x 503 RHS_ x -> foldVar f i x
502 Delta{} -> mempty 504 Delta{} -> mempty
503 505
@@ -507,7 +509,7 @@ instance HasMaxDB Neutral where
507 CaseFun__ c _ _ _ -> c 509 CaseFun__ c _ _ _ -> c
508 TyCaseFun__ c _ _ _ -> c 510 TyCaseFun__ c _ _ _ -> c
509 App__ c a b -> c 511 App__ c a b -> c
510 Fun_ c _ _ _ _ _ -> c 512 Fun_ c _ _ _ _ -> c
511 RHS_ x -> maxDB_ x 513 RHS_ x -> maxDB_ x
512 Delta{} -> mempty 514 Delta{} -> mempty
513 515
@@ -543,7 +545,7 @@ instance ClosedExp Neutral where
543 CaseFun__ _ a as n -> CaseFun__ mempty a (closedExp <$> as) (closedExp n) 545 CaseFun__ _ a as n -> CaseFun__ mempty a (closedExp <$> as) (closedExp n)
544 TyCaseFun__ _ a as n -> TyCaseFun__ mempty a (closedExp <$> as) (closedExp n) 546 TyCaseFun__ _ a as n -> TyCaseFun__ mempty a (closedExp <$> as) (closedExp n)
545 App__ _ a b -> App__ mempty (closedExp a) (closedExp b) 547 App__ _ a b -> App__ mempty (closedExp a) (closedExp b)
546 Fun_ _ f l i x y -> Fun_ mempty f l i (closedExp <$> x) y 548 Fun_ _ f l x y -> Fun_ mempty f l (closedExp <$> x) y
547 RHS_ a -> RHS_ (closedExp a) 549 RHS_ a -> RHS_ (closedExp a)
548 d@Delta{} -> d 550 d@Delta{} -> d
549 551
@@ -584,7 +586,7 @@ instance MkDoc Neutral where
584 f = \case 586 f = \case
585 CstrT' t a b -> shCstr (g (a, t)) (g (b, t)) 587 CstrT' t a b -> shCstr (g (a, t)) (g (b, t))
586 FL' a | pr -> g a 588 FL' a | pr -> g a
587 Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (pShow s) (g <$> xs) 589 Fun' s vs (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (pShow s) (g <$> xs)
588 Var_ k -> shVar k 590 Var_ k -> shVar k
589 App_ a b -> shApp Visible (g a) (g b) 591 App_ a b -> shApp Visible (g a) (g b)
590 CaseFun_ s xs n -> foldl (shApp Visible) (pShow s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) 592 CaseFun_ s xs n -> foldl (shApp Visible) (pShow s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n])
@@ -769,7 +771,7 @@ app_ (TyCon s xs) a = TyCon s (xs ++ [a])
769app_ (Neut f) a = neutApp f a 771app_ (Neut f) a = neutApp f a
770 where 772 where
771 neutApp (FL' x) a = app_ x a -- ??? 773 neutApp (FL' x) a = app_ x a -- ???
772 neutApp (Fun' f vs i xs e) a | i > 0 = pmLabel f vs (i-1) (a: xs) (subst (i-1) (up (i-1) a) $ Neut e) 774 neutApp (Fun' f vs xs (Lam e)) a = pmLabel f vs (a: xs) (subst 0 a e)
773 neutApp f a = Neut $ App_ f a 775 neutApp f a = Neut $ App_ f a
774 776
775 777
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index bcdd0968..c5497a98 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -864,7 +864,7 @@ mkLam _ = Nothing
864 864
865mkCon (ExpTV (I.Con s n xs) et vs) = Just (untick $ show s, chain vs (conType et s) $ mkConPars n et ++ xs) 865mkCon (ExpTV (I.Con s n xs) et vs) = Just (untick $ show s, chain vs (conType et s) $ mkConPars n et ++ xs)
866mkCon (ExpTV (TyCon s xs) et vs) = Just (untick $ show s, chain vs (nType s) xs) 866mkCon (ExpTV (TyCon s xs) et vs) = Just (untick $ show s, chain vs (nType s) xs)
867mkCon (ExpTV (Neut (I.Fun s i (reverse -> xs) def)) et vs) = Just (untick $ show s, chain vs (nType s) xs) 867mkCon (ExpTV (Neut (I.Fun s (reverse -> xs) def)) et vs) = Just (untick $ show s, chain vs (nType s) xs)
868mkCon (ExpTV (CaseFun s xs n) et vs) = Just (untick $ show s, chain vs (nType s) $ makeCaseFunPars' (mkEnv vs) n ++ xs ++ [Neut n]) 868mkCon (ExpTV (CaseFun s xs n) et vs) = Just (untick $ show s, chain vs (nType s) $ makeCaseFunPars' (mkEnv vs) n ++ xs ++ [Neut n])
869mkCon (ExpTV (TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, Neut n, f]) 869mkCon (ExpTV (TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, Neut n, f])
870mkCon _ = Nothing 870mkCon _ = Nothing
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index ec66c801..6e351410 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -210,14 +210,14 @@ neutType te = \case
210 Var_ i -> snd $ varType "C" i te 210 Var_ i -> snd $ varType "C" i te
211 CaseFun_ s ts n -> appTy (foldl appTy (nType s) $ makeCaseFunPars te n ++ ts) (Neut n) 211 CaseFun_ s ts n -> appTy (foldl appTy (nType s) $ makeCaseFunPars te n ++ ts) (Neut n)
212 TyCaseFun_ s [m, t, f] n -> foldl appTy (nType s) [m, t, Neut n, f] 212 TyCaseFun_ s [m, t, f] n -> foldl appTy (nType s) [m, t, Neut n, f]
213 Fun' s _ _ a _ -> foldlrev appTy (nType s) a 213 Fun' s _ a _ -> foldlrev appTy (nType s) a
214 214
215neutType' te = \case 215neutType' te = \case
216 App_ f x -> appTy (neutType' te f) x 216 App_ f x -> appTy (neutType' te f) x
217 Var_ i -> varType' i te 217 Var_ i -> varType' i te
218 CaseFun_ s ts n -> appTy (foldl appTy (nType s) $ makeCaseFunPars' te n ++ ts) (Neut n) 218 CaseFun_ s ts n -> appTy (foldl appTy (nType s) $ makeCaseFunPars' te n ++ ts) (Neut n)
219 TyCaseFun_ s [m, t, f] n -> foldl appTy (nType s) [m, t, Neut n, f] 219 TyCaseFun_ s [m, t, f] n -> foldl appTy (nType s) [m, t, Neut n, f]
220 Fun' s _ _ a _ -> foldlrev appTy (nType s) a 220 Fun' s _ a _ -> foldlrev appTy (nType s) a
221 221
222-------------------------------------------------------------------------------- error messages 222-------------------------------------------------------------------------------- error messages
223 223
@@ -522,9 +522,9 @@ recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt)
522 (TyCon_ md s as, zt) -> checkApps (ppShow s) [] zt (TyCon_ md s) te (nType s) as 522 (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 (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 (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@[] 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 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
526 -- TODO 526 -- TODO
527 (r@(Neut (Fun' f vs i a x)), zt) -> r 527 (r@(Neut (Fun' f vs a x)), zt) -> r
528 (RHS x, zt) -> RHS $ recheck_ msg te (x, zt) 528 (RHS x, zt) -> RHS $ recheck_ msg te (x, zt)
529 (Neut d@Delta{}, zt) -> Neut d 529 (Neut d@Delta{}, zt) -> Neut d
530 where 530 where
@@ -733,7 +733,7 @@ mkELet n x xt = {-(if null vs then id else trace_ $ "mkELet " ++ show (length vs
733 vs = [Var i | i <- Set.toList $ free x <> free xt] 733 vs = [Var i | i <- Set.toList $ free x <> free xt]
734 fn = FunName (mkFName n) (Just x) xt 734 fn = FunName (mkFName n) (Just x) xt
735 735
736 term = pmLabel fn vs 0 [] $ getFix x 0 736 term = pmLabel fn vs [] $ getFix x 0
737 737
738 getFix (Lam z) i = Lam $ getFix z (i+1) 738 getFix (Lam z) i = Lam $ getFix z (i+1)
739 getFix (TFun FprimFix _ [t, Lam f]) i = subst 0 (foldl app_ term (downTo 0 i)) f 739 getFix (TFun FprimFix _ [t, Lam f]) i = subst 0 (foldl app_ term (downTo 0 i)) f
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 08b066cd..09e9507b 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -87,17 +87,6 @@ trackSI p = do
87 tell $ Right . TrackedCode <$> maybeToList (getRange $ sourceInfo x) 87 tell $ Right . TrackedCode <$> maybeToList (getRange $ sourceInfo x)
88 return x 88 return x
89 89
90-- TODO: filter this in module imports
91data DesugarInfo = DesugarInfo
92 { fixityMap :: Map.Map SName Fixity
93 , consMap :: Map.Map SName ConsInfo
94 , definedSet :: DefinedSet
95 }
96
97instance Monoid DesugarInfo where
98 mempty = DesugarInfo mempty mempty mempty
99 DesugarInfo a b c `mappend` DesugarInfo a' b' c' = DesugarInfo (a <> a') (b <> b') (c <> c')
100
101addFixity :: BodyParser SIName -> BodyParser SIName 90addFixity :: BodyParser SIName -> BodyParser SIName
102addFixity p = f <$> asks (fixityMap . desugarInfo) <*> p 91addFixity p = f <$> asks (fixityMap . desugarInfo) <*> p
103 where 92 where
@@ -125,6 +114,17 @@ addForalls_ s = addForalls . (Set.fromList (sName <$> s) <>) <$> asks (definedSe
125 114
126-------------------------------------------------------------------------------- desugar info 115-------------------------------------------------------------------------------- desugar info
127 116
117-- TODO: filter this in module imports
118data DesugarInfo = DesugarInfo
119 { fixityMap :: Map.Map SName Fixity
120 , consMap :: Map.Map SName ConsInfo
121 , definedSet :: DefinedSet
122 }
123
124instance Monoid DesugarInfo where
125 mempty = DesugarInfo mempty mempty mempty
126 DesugarInfo a b c `mappend` DesugarInfo a' b' c' = DesugarInfo (a <> a') (b <> b') (c <> c')
127
128mkDesugarInfo :: [Stmt] -> DesugarInfo 128mkDesugarInfo :: [Stmt] -> DesugarInfo
129mkDesugarInfo ss = DesugarInfo 129mkDesugarInfo ss = DesugarInfo
130 { fixityMap = Map.fromList $ ("'EqCTt", Infix (-1)): [(sName s, f) | PrecDef s f <- ss] 130 { fixityMap = Map.fromList $ ("'EqCTt", Infix (-1)): [(sName s, f) | PrecDef s f <- ss]
@@ -145,10 +145,11 @@ mkDesugarInfo ss = DesugarInfo
145 145
146-------------------------------------------------------------------------------- expression parsing 146-------------------------------------------------------------------------------- expression parsing
147 147
148hiddenTerm p q = (,) Hidden <$ reservedOp "@" <*> typeNS p <|> (,) Visible <$> q
149
148parseType mb = maybe id option mb (reservedOp "::" *> typeNS (setR parseTermLam)) 150parseType mb = maybe id option mb (reservedOp "::" *> typeNS (setR parseTermLam))
149typedIds f ds mb = (\ns t -> (,) <$> ns <*> pure t) <$> commaSep1 upperLower <*> (deBruijnify (ds :: [SIName]) <$> f (parseType mb))
150 151
151hiddenTerm p q = (,) Hidden <$ reservedOp "@" <*> typeNS p <|> (,) Visible <$> q 152typedIds f ds = (\ns t -> (,) <$> ns <*> pure t) <$> commaSep1 upperLower <*> (deBruijnify (ds :: [SIName]) <$> f (parseType Nothing))
152 153
153telescope mb = fmap mkTelescope $ many $ hiddenTerm 154telescope mb = fmap mkTelescope $ many $ hiddenTerm
154 (typedId <|> maybe empty (tvar . pure) mb) 155 (typedId <|> maybe empty (tvar . pure) mb)
@@ -183,22 +184,71 @@ parseTermLam =
183 identation False $ do 184 identation False $ do
184 (fe, p) <- longPattern 185 (fe, p) <- longPattern
185 (,) p . deBruijnify fe <$> parseRHS "->" 186 (,) p . deBruijnify fe <$> parseRHS "->"
187 where
188 mkIf b t f = SBuiltin "primIfThenElse" `SAppV` b `SAppV` t `SAppV` f
189
190 mkPi Hidden xs b = foldr (sNonDepPi Hidden) b $ getTTuple xs
191 mkPi h a b = sNonDepPi h a b
192
193 sNonDepPi h a b = SPi h a $ up1 b
194
186parseTermAnn = level parseTermOp $ \t -> SAnn t <$> parseType Nothing 195parseTermAnn = level parseTermOp $ \t -> SAnn t <$> parseType Nothing
187parseTermOp = (notOp False <|> notExp) >>= calculatePrecs where 196
197parseTermOp = (notOp False <|> notExp) >>= calculatePrecs
198 where
188 notExp = (++) <$> ope <*> notOp True 199 notExp = (++) <$> ope <*> notOp True
189 notOp x = (++) <$> try_ "expression" ((++) <$> ex parseTermApp <*> option [] ope) <*> notOp True 200 notOp x = (++) <$> try_ "expression" ((++) <$> ex parseTermApp <*> option [] ope) <*> notOp True
190 <|> if x then option [] (try_ "lambda" $ ex parseTermLam) else mzero 201 <|> if x then option [] (try_ "lambda" $ ex parseTermLam) else mzero
191 ope = pure . Left <$> addFixity (rhsOperator <|> appRange (flip SIName "'EqCTt" <$ reservedOp "~")) 202 ope = pure . Left <$> addFixity (rhsOperator <|> appRange (flip SIName "'EqCTt" <$ reservedOp "~"))
192 ex pr = pure . Right <$> setR pr 203 ex pr = pure . Right <$> setR pr
204
205 calculatePrecs :: [Either SIName SExp] -> BodyParser SExp
206 calculatePrecs = go where
207
208 go (Right e: xs) = waitOp False e [] xs
209 go xs@(Left (sName -> "-"): _) = waitOp False (mkLit $ LInt 0) [] xs
210 go (Left op: xs) = Section . SLamV <$> waitExp True (sVar "leftSection" 0) [] op xs
211 go _ = error "impossible @ Parser 479"
212
213 waitExp lsec e acc op (Right t: xs) = waitOp lsec e ((op, if lsec then up 1 t else t): acc) xs
214 waitExp lsec e acc op [] | lsec = fail "two-sided section is not allowed"
215 | otherwise = fmap (Section . SLamV) . calcPrec' e $ (op, sVar "rightSection" 0): map (second (up 1)) acc
216 waitExp _ _ _ _ _ = fail "two operator is not allowed next to each-other"
217
218 waitOp lsec e acc (Left op: xs) = waitExp lsec e acc op xs
219 waitOp lsec e acc [] = calcPrec' e acc
220 waitOp lsec e acc _ = error "impossible @ Parser 488"
221
222 calcPrec' e = postponedCheck id . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (fromJust . getFixity) e . reverse
223
193parseTermApp = 224parseTermApp =
194 AppsS <$> try_ "record" (SGlobal <$> upperCase <* symbol "{") 225 AppsS <$> try_ "record" (SGlobal <$> upperCase <* symbol "{")
195 <*> commaSep ((,) Visible <$ lowerCase{-TODO-} <* reservedOp "=" <*> setR parseTermLam) 226 <*> commaSep ((,) Visible <$ lowerCase{-TODO-} <* reservedOp "=" <*> setR parseTermLam)
196 <* symbol "}" 227 <* symbol "}"
197 <|> AppsS <$> setR parseTermSwiz <*> many (hiddenTerm (setR parseTermSwiz) $ setR parseTermSwiz) 228 <|> AppsS <$> setR parseTermSwiz <*> many (hiddenTerm (setR parseTermSwiz) $ setR parseTermSwiz)
229
198parseTermSwiz = level parseTermProj $ \t -> 230parseTermSwiz = level parseTermProj $ \t ->
199 mkSwizzling t <$> lexeme (try_ "swizzling" $ char '%' *> count' 1 4 (satisfy (`elem` ("xyzwrgba" :: String)))) 231 mkSwizzling t <$> lexeme (try_ "swizzling" $ char '%' *> count' 1 4 (satisfy (`elem` ("xyzwrgba" :: String))))
232 where
233 mkSwizzling term = swizzcall . map (sc . synonym)
234 where
235 swizzcall [] = error "impossible: swizzling parsing returned empty pattern"
236 swizzcall [x] = SBuiltin "swizzscalar" `SAppV` term `SAppV` x
237 swizzcall xs = SBuiltin "swizzvector" `SAppV` term `SAppV` foldl SAppV (SBuiltin $ "V" ++ show (length xs)) xs
238
239 sc c = SBuiltin ['S', c]
240
241 synonym 'r' = 'x'
242 synonym 'g' = 'y'
243 synonym 'b' = 'z'
244 synonym 'a' = 'w'
245 synonym c = c
246
200parseTermProj = level parseTermAtom $ \t -> 247parseTermProj = level parseTermAtom $ \t ->
201 try_ "projection" $ mkProjection t <$ char '.' <*> sepBy1 lowerCase (char '.') 248 try_ "projection" $ mkProjection t <$ char '.' <*> sepBy1 lowerCase (char '.')
249 where
250 mkProjection = foldl $ \exp field -> SBuiltin "project" `SAppV` litString field `SAppV` exp
251
202parseTermAtom = 252parseTermAtom =
203 mkLit <$> try_ "literal" parseLit 253 mkLit <$> try_ "literal" parseLit
204 <|> Wildcard (Wildcard SType) <$ reserved "_" 254 <|> Wildcard (Wildcard SType) <$ reserved "_"
@@ -218,104 +268,73 @@ parseTermAtom =
218 <|> parens (SGlobal <$> try_ "opname" (symbols <* lookAhead (symbol ")")) 268 <|> parens (SGlobal <$> try_ "opname" (symbols <* lookAhead (symbol ")"))
219 <|> mkTuple . tick <$> asks namespace <*> commaSep (setR parseTermLam)) 269 <|> mkTuple . tick <$> asks namespace <*> commaSep (setR parseTermLam))
220 270
221level pr f = pr >>= \t -> option t $ f t 271 mkTuple _ [Section e] = e
272 mkTuple ExpNS [Parens e] = HCons e HNil
273 mkTuple TypeNS [Parens e] = HList $ BCons e BNil
274 mkTuple _ [x] = Parens x
275 mkTuple ExpNS xs = foldr HCons HNil xs
276 mkTuple TypeNS xs = HList $ foldr BCons BNil xs
222 277
278 mkList TypeNS [x] = BList x
279 mkList _ xs = foldr BCons BNil xs
223 280
224mkSwizzling term = swizzcall . map (sc . synonym) 281 -- Creates: RecordCons @[("x", _), ("y", _), ("z", _)] (1.0, 2.0, 3.0)))
225 where 282 mkRecord (unzip -> (names, values))
226 swizzcall [] = error "impossible: swizzling parsing returned empty pattern" 283 = SBuiltin "RecordCons" `SAppH` foldr BCons BNil (mkRecItem <$> names) `SAppV` foldr HCons HNil values
227 swizzcall [x] = SBuiltin "swizzscalar" `SAppV` term `SAppV` x
228 swizzcall xs = SBuiltin "swizzvector" `SAppV` term `SAppV` foldl SAppV (SBuiltin $ "V" ++ show (length xs)) xs
229 284
230 sc c = SBuiltin ['S', c] 285 mkRecItem l = SBuiltin "RecItem" `SAppV` litString l `SAppV` Wildcard SType
231 286
232 synonym 'r' = 'x' 287 mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f
233 synonym 'g' = 'y'
234 synonym 'b' = 'z'
235 synonym 'a' = 'w'
236 synonym c = c
237 288
238mkProjection = foldl $ \exp field -> SBuiltin "project" `SAppV` litString field `SAppV` exp 289 generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp)
290 generator = do
291 (dbs, pat) <- try_ "generator" $ longPattern <* reservedOp "<-"
292 checkPattern dbs
293 exp <- setR parseTermLam
294 return $ \e -> do
295 cf <- runCheck $ compileGuardTree id id (Just $ SIName (sourceInfo pat) "") [(Visible, Wildcard SType)]
296 $ compilePatts [pat] (noGuards $ deBruijnify dbs e) `mappend` noGuards BNil
297 return $ SBuiltin "concatMap" `SAppV` cf `SAppV` exp
239 298
240-- Creates: RecordCons @[("x", _), ("y", _), ("z", _)] (1.0, 2.0, 3.0))) 299 letdecl = (return .) . mkLets <$ reserved "let" <*> (runCheck . compileStmt' =<< valueDef)
241mkRecord (unzip -> (names, values))
242 = SBuiltin "RecordCons" `SAppH` foldr BCons BNil (mkRecItem <$> names) `SAppV` foldr HCons HNil values
243 300
244mkRecItem l = SBuiltin "RecItem" `SAppV` litString l `SAppV` Wildcard SType 301 boolExpression = (\pred e -> return $ SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` BNil) <$> setR parseTermLam
245 302
246litString (SIName si n) = SLit si $ LString n
247 303
248mkTuple _ [Section e] = e 304level pr f = pr >>= \t -> option t $ f t
249mkTuple ExpNS [Parens e] = HCons e HNil
250mkTuple TypeNS [Parens e] = HList $ BCons e BNil
251mkTuple _ [x] = Parens x
252mkTuple ExpNS xs = foldr HCons HNil xs
253mkTuple TypeNS xs = HList $ foldr BCons BNil xs
254 305
255mkList TypeNS [x] = BList x 306litString (SIName si n) = SLit si $ LString n
256mkList _ xs = foldr BCons BNil xs
257 307
258mkLit n@LInt{} = SBuiltin "fromInt" `SAppV` sLit n 308mkLit n@LInt{} = SBuiltin "fromInt" `SAppV` sLit n
259mkLit l = sLit l 309mkLit l = sLit l
260 310
261mkIf b t f = SBuiltin "primIfThenElse" `SAppV` b `SAppV` t `SAppV` f
262
263mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f
264
265calculatePrecs :: [Either SIName SExp] -> BodyParser SExp
266calculatePrecs = go where
267
268 go (Right e: xs) = waitOp False e [] xs
269 go xs@(Left (sName -> "-"): _) = waitOp False (mkLit $ LInt 0) [] xs
270 go (Left op: xs) = Section . SLamV <$> waitExp True (sVar "leftSection" 0) [] op xs
271 go _ = error "impossible @ Parser 479"
272
273 waitExp lsec e acc op (Right t: xs) = waitOp lsec e ((op, if lsec then up 1 t else t): acc) xs
274 waitExp lsec e acc op [] | lsec = fail "two-sided section is not allowed"
275 | otherwise = fmap (Section . SLamV) . calcPrec' e $ (op, sVar "rightSection" 0): map (second (up 1)) acc
276 waitExp _ _ _ _ _ = fail "two operator is not allowed next to each-other"
277
278 waitOp lsec e acc (Left op: xs) = waitExp lsec e acc op xs
279 waitOp lsec e acc [] = calcPrec' e acc
280 waitOp lsec e acc _ = error "impossible @ Parser 488"
281
282 calcPrec' e = postponedCheck id . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (fromJust . getFixity) e . reverse
283
284generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp)
285generator = do
286 (dbs, pat) <- try_ "generator" $ longPattern <* reservedOp "<-"
287 checkPattern dbs
288 exp <- setR parseTermLam
289 return $ \e -> do
290 cf <- runCheck $ compileGuardTree id id (Just $ SIName (sourceInfo pat) "") [(Visible, Wildcard SType)] $ compilePatts [pat] (noGuards $ deBruijnify dbs e) `mappend` noGuards BNil
291 return $ SBuiltin "concatMap" `SAppV` cf `SAppV` exp
292
293letdecl = (return .) . mkLets <$ reserved "let" <*> (runCheck . compileStmt' =<< valueDef)
294
295boolExpression = (\pred e -> return $ SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` BNil) <$> setR parseTermLam
296
297mkPi Hidden xs b = foldr (sNonDepPi Hidden) b $ getTTuple xs
298mkPi h a b = sNonDepPi h a b
299
300sNonDepPi h a b = SPi h a $ up1 b
301
302-------------------------------------------------------------------------------- pattern parsing 311-------------------------------------------------------------------------------- pattern parsing
303 312
304setR p = appRange $ flip setSI <$> p 313setR p = appRange $ flip setSI <$> p
305 314
306--parsePat... :: BodyParser ParPat 315--parsePat... :: BodyParser ParPat
307parsePatAnn = patType <$> setR parsePatArr <*> parseType (Just $ Wildcard SType) 316parsePatAnn = patType <$> setR parsePatOp <*> parseType (Just $ Wildcard SType)
308parsePatArr = parsePatOp 317 where
318 patType p (Wildcard SType) = p
319 patType p t = PatTypeSimp p t
320
309parsePatOp = join $ calculatePatPrecs <$> setR parsePatApp <*> option [] (oper >>= p) 321parsePatOp = join $ calculatePatPrecs <$> setR parsePatApp <*> option [] (oper >>= p)
310 where 322 where
311 oper = addConsInfo $ addFixity colonSymbols 323 oper = addConsInfo $ addFixity colonSymbols
312 p op = do (exp, op') <- try_ "pattern" $ (,) <$> setR parsePatApp <*> oper 324 p op = do (exp, op') <- try_ "pattern" $ (,) <$> setR parsePatApp <*> oper
313 ((op, exp):) <$> p op' 325 ((op, exp):) <$> p op'
314 <|> pure . (,) op <$> setR parsePatAnn 326 <|> pure . (,) op <$> setR parsePatAnn
327
328 calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (fromJust . getFixity . fst) e xs
329
315parsePatApp = 330parsePatApp =
316 PConSimp <$> addConsInfo upperCase_ <*> many (setR parsePatAt) 331 PConSimp <$> addConsInfo upperCase_ <*> many (setR parsePatAt)
317 <|> parsePatAt 332 <|> parsePatAt
333
318parsePatAt = concatParPats <$> sepBy1 (setR parsePatAtom) (reservedOp "@") 334parsePatAt = concatParPats <$> sepBy1 (setR parsePatAtom) (reservedOp "@")
335 where
336 concatParPats ps = ParPat $ concat [p | ParPat p <- ps]
337
319parsePatAtom = 338parsePatAtom =
320 mkLit <$> asks namespace <*> try_ "literal" parseLit 339 mkLit <$> asks namespace <*> try_ "literal" parseLit
321 <|> flip PConSimp [] <$> addConsInfo upperCase_ 340 <|> flip PConSimp [] <$> addConsInfo upperCase_
@@ -331,34 +350,27 @@ parsePatAtom =
331 brackets (mkListPat . tick <$> asks namespace <*> patlist) 350 brackets (mkListPat . tick <$> asks namespace <*> patlist)
332 <|> parens (parseViewPat <|> mkTupPat . tick <$> asks namespace <*> patlist) 351 <|> parens (parseViewPat <|> mkTupPat . tick <$> asks namespace <*> patlist)
333 352
334parseViewPat = ViewPatSimp <$> try_ "view pattern" (setR parseTermOp <* reservedOp "->") <*> setR parsePatAnn 353 mkListPat TypeNS [p] = cList p
335 354 mkListPat ns ps = foldr cCons cNil ps
336mkPVar (SIName si "") = PWildcard si
337mkPVar s = PVarSimp s
338
339concatParPats ps = ParPat $ concat [p | ParPat p <- ps]
340
341litP = flip ViewPatSimp cTrue . SAppV (SGlobal $ SIName_ mempty (Just $ Infix 4) "==")
342 355
343patlist = commaSep $ setR parsePatAnn 356 --mkTupPat :: [Pat] -> Pat
357 mkTupPat TypeNS [PParens x] = mkTTup [x]
358 mkTupPat ns [PParens x] = mkTup [x]
359 mkTupPat _ [x] = PParens x
360 mkTupPat TypeNS ps = mkTTup ps
361 mkTupPat ns ps = mkTup ps
344 362
345mkListPat TypeNS [p] = cList p 363 mkTTup = cHList . mkListPat ExpNS
346mkListPat ns ps = foldr cCons cNil ps 364 mkTup ps = foldr cHCons cHNil ps
347 365
348--mkTupPat :: [Pat] -> Pat 366 parseViewPat = ViewPatSimp <$> try_ "view pattern" (setR parseTermOp <* reservedOp "->") <*> setR parsePatAnn
349mkTupPat TypeNS [PParens x] = mkTTup [x]
350mkTupPat ns [PParens x] = mkTup [x]
351mkTupPat _ [x] = PParens x
352mkTupPat TypeNS ps = mkTTup ps
353mkTupPat ns ps = mkTup ps
354 367
355mkTTup = cHList . mkListPat ExpNS 368 mkPVar (SIName si "") = PWildcard si
356mkTup ps = foldr cHCons cHNil ps 369 mkPVar s = PVarSimp s
357 370
358patType p (Wildcard SType) = p 371 litP = flip ViewPatSimp cTrue . SAppV (SGlobal $ SIName_ mempty (Just $ Infix 4) "==")
359patType p t = PatTypeSimp p t
360 372
361calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (fromJust . getFixity . fst) e xs 373 patlist = commaSep $ setR parsePatAnn
362 374
363longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id) 375longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id)
364 376
@@ -397,7 +409,7 @@ parseDef =
397 , deBruijnify npsd $ foldr (uncurry SPi) (foldl SAppV (SGlobal x) $ SGlobal <$> reverse npsd) ts' 409 , deBruijnify npsd $ foldr (uncurry SPi) (foldl SAppV (SGlobal x) $ SGlobal <$> reverse npsd) ts'
398 ) 410 )
399 (af, cs) <- option (True, []) $ 411 (af, cs) <- option (True, []) $
400 (,) True . map (second $ (,) Nothing) . concat <$ reserved "where" <*> identation True (typedIds id npsd Nothing) 412 (,) True . map (second $ (,) Nothing) . concat <$ reserved "where" <*> identation True (typedIds id npsd)
401 <|> (,) False <$ reservedOp "=" <*> 413 <|> (,) False <$ reservedOp "=" <*>
402 sepBy1 ((,) <$> (addFixity' upperCase <|> parens (addFixity colonSymbols)) 414 sepBy1 ((,) <$> (addFixity' upperCase <|> parens (addFixity colonSymbols))
403 <*> (mkConTy True <$> braces telescopeDataFields <|> mkConTy False <$> telescope Nothing) 415 <*> (mkConTy True <$> braces telescopeDataFields <|> mkConTy False <$> telescope Nothing)
@@ -407,7 +419,7 @@ parseDef =
407 <|> do reserved "class" *> do 419 <|> do reserved "class" *> do
408 x <- typeNS upperCase 420 x <- typeNS upperCase
409 (nps, ts) <- telescope (Just SType) 421 (nps, ts) <- telescope (Just SType)
410 cs <- option [] $ concat <$ reserved "where" <*> identation True (typedIds id nps Nothing) 422 cs <- option [] $ concat <$ reserved "where" <*> identation True (typedIds id nps)
411 return $ pure $ Class x (map snd ts) cs 423 return $ pure $ Class x (map snd ts) cs
412 <|> do reserved "instance" *> do 424 <|> do reserved "instance" *> do
413 typeNS $ do 425 typeNS $ do
@@ -433,7 +445,7 @@ parseDef =
433 runCheck $ fmap Stmt <$> compileStmt (compileGuardTrees id . const Nothing) 445 runCheck $ fmap Stmt <$> compileStmt (compileGuardTrees id . const Nothing)
434 [{-TypeAnn x $ UncurryS ts $ SType-}{-todo-}] 446 [{-TypeAnn x $ UncurryS ts $ SType-}{-todo-}]
435 [funAlt' x ts (map PVarSimp $ reverse nps) $ noGuards rhs] 447 [funAlt' x ts (map PVarSimp $ reverse nps) $ noGuards rhs]
436 <|> do try_ "typed ident" $ map (uncurry TypeAnn) <$> typedIds addForalls' [] Nothing 448 <|> do try_ "typed ident" $ map (uncurry TypeAnn) <$> typedIds addForalls' []
437 <|> fmap . (Stmt .) . flip PrecDef <$> parseFixity <*> commaSep1 rhsOperator 449 <|> fmap . (Stmt .) . flip PrecDef <$> parseFixity <*> commaSep1 rhsOperator
438 <|> pure <$> funAltDef (Just lhsOperator) varId 450 <|> pure <$> funAltDef (Just lhsOperator) varId
439 <|> valueDef 451 <|> valueDef