diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 00:35:01 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 00:35:01 +0200 |
commit | 61a8efb74a0ca34f73a848f6d9fd018c83dac343 (patch) | |
tree | 0ef19f20bfda1faad94693c5ffc4d42ec641ab0f /src/LambdaCube/Compiler/Infer.hs | |
parent | 354ab8759fd3879d85e392351bb9ea1aed173e98 (diff) |
refactored pretty print framework
Diffstat (limited to 'src/LambdaCube/Compiler/Infer.hs')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 78 |
1 files changed, 40 insertions, 38 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 8b3b5c12..0015e364 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -47,7 +47,7 @@ import Control.DeepSeq | |||
47 | 47 | ||
48 | import LambdaCube.Compiler.Utils | 48 | import LambdaCube.Compiler.Utils |
49 | import LambdaCube.Compiler.DeBruijn | 49 | import LambdaCube.Compiler.DeBruijn |
50 | import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) | 50 | import LambdaCube.Compiler.Pretty hiding (braces, parens) |
51 | import LambdaCube.Compiler.DesugaredSource hiding (getList) | 51 | import LambdaCube.Compiler.DesugaredSource hiding (getList) |
52 | import LambdaCube.Compiler.Parser (ParseWarning) -- TODO: remove | 52 | import LambdaCube.Compiler.Parser (ParseWarning) -- TODO: remove |
53 | 53 | ||
@@ -1457,20 +1457,22 @@ instance PShow (CEnv Exp) where | |||
1457 | pShowPrec _ = showDoc_ . mkDoc False False | 1457 | pShowPrec _ = showDoc_ . mkDoc False False |
1458 | 1458 | ||
1459 | instance PShow Env where | 1459 | instance PShow Env where |
1460 | pShowPrec _ e = showDoc_ $ envDoc e $ pure $ shAtom $ underlined "<<HERE>>" | 1460 | pShowPrec _ e = showDoc_ $ envDoc e $ shAtom $ underlined "<<HERE>>" |
1461 | 1461 | ||
1462 | showEnvExp :: Env -> ExpType -> String | 1462 | showEnvExp :: Env -> ExpType -> String |
1463 | showEnvExp e c = showDoc $ envDoc e $ epar <$> mkDoc False False c | 1463 | showEnvExp e c = showDoc $ envDoc e $ epar $ mkDoc False False c |
1464 | 1464 | ||
1465 | showEnvSExp :: Up a => Env -> SExp' a -> String | 1465 | showEnvSExp :: Up a => Env -> SExp' a -> String |
1466 | showEnvSExp e c = showDoc $ envDoc e $ epar <$> sExpDoc c | 1466 | showEnvSExp e c = showDoc $ envDoc e $ epar $ sExpDoc c |
1467 | 1467 | ||
1468 | showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String | 1468 | showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String |
1469 | showEnvSExpType e c t = showDoc $ envDoc e $ epar <$> (shAnn "::" False <$> sExpDoc c <**> mkDoc False False (t, TType)) | 1469 | showEnvSExpType e c t = showDoc $ envDoc e $ epar $ (shAnn "::" False (sExpDoc c) (mkDoc False False (t, TType))) |
1470 | {- | ||
1470 | where | 1471 | where |
1471 | infixl 4 <**> | 1472 | infixl 4 <**> |
1472 | (<**>) :: NameDB (a -> b) -> NameDB a -> NameDB b | 1473 | (<**>) :: NameDB (a -> b) -> NameDB a -> NameDB b |
1473 | a <**> b = get >>= \s -> lift $ evalStateT a s <*> evalStateT b s | 1474 | a <**> b = get >>= \s -> lift $ evalStateT a s <*> evalStateT b s |
1475 | -} | ||
1474 | 1476 | ||
1475 | {- | 1477 | {- |
1476 | expToSExp :: Exp -> SExp | 1478 | expToSExp :: Exp -> SExp |
@@ -1501,75 +1503,75 @@ nameSExp = \case | |||
1501 | STyped_ (e, _) -> nameSExp $ expToSExp e -- todo: mark boundary | 1503 | STyped_ (e, _) -> nameSExp $ expToSExp e -- todo: mark boundary |
1502 | SVar i -> SGlobal <$> shVar i | 1504 | SVar i -> SGlobal <$> shVar i |
1503 | -} | 1505 | -} |
1504 | envDoc :: Env -> Doc -> Doc | 1506 | envDoc :: Env -> NDoc -> NDoc |
1505 | envDoc x m = case x of | 1507 | envDoc x m = case x of |
1506 | EGlobal{} -> m | 1508 | EGlobal{} -> m |
1507 | EBind1 _ h ts b -> envDoc ts $ join $ shLam (usedVar 0 b) h <$> m <*> pure (sExpDoc b) | 1509 | EBind1 _ h ts b -> envDoc ts $ shLam (usedVar 0 b) h m (sExpDoc b) |
1508 | EBind2 h a ts -> envDoc ts $ join $ shLam True h <$> mkDoc False ts' (a, TType) <*> pure m | 1510 | EBind2 h a ts -> envDoc ts $ shLam True h (mkDoc False ts' (a, TType)) m |
1509 | EApp1 _ h ts b -> envDoc ts $ shApp h <$> m <*> sExpDoc b | 1511 | EApp1 _ h ts b -> envDoc ts $ shApp h m (sExpDoc b) |
1510 | EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (shAtom "tyType") <$> m | 1512 | EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (shAtom "tyType") m |
1511 | EApp2 _ h a ts -> envDoc ts $ shApp h <$> mkDoc False ts' a <*> m | 1513 | EApp2 _ h a ts -> envDoc ts $ shApp h (mkDoc False ts' a) m |
1512 | ELet1 _ ts b -> envDoc ts $ shLet_ m (sExpDoc b) | 1514 | ELet1 _ ts b -> envDoc ts $ shLet_ m (sExpDoc b) |
1513 | ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False ts' x) m | 1515 | ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False ts' x) m |
1514 | EAssign i x ts -> envDoc ts $ shLet i (mkDoc False ts' x) m | 1516 | EAssign i x ts -> envDoc ts $ shLet i (mkDoc False ts' x) m |
1515 | CheckType t ts -> envDoc ts $ shAnn ":" False <$> m <*> mkDoc False ts' (t, TType) | 1517 | CheckType t ts -> envDoc ts $ shAnn ":" False m $ mkDoc False ts' (t, TType) |
1516 | CheckIType t ts -> envDoc ts $ shAnn ":" False <$> m <*> pure (shAtom "??") -- mkDoc ts' t | 1518 | CheckIType t ts -> envDoc ts $ shAnn ":" False m (shAtom "??") -- mkDoc ts' t |
1517 | -- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t | 1519 | -- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t |
1518 | CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m | 1520 | CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m |
1519 | ELabelEnd ts -> envDoc ts $ shApp Visible (shAtom "labEnd") <$> m | 1521 | ELabelEnd ts -> envDoc ts $ shApp Visible (shAtom "labEnd") m |
1520 | x -> error $ "envDoc: " ++ show x | 1522 | x -> error $ "envDoc: " ++ show x |
1521 | where | 1523 | where |
1522 | ts' = False | 1524 | ts' = False |
1523 | 1525 | ||
1524 | class MkDoc a where | 1526 | class MkDoc a where |
1525 | mkDoc :: Bool {-print reduced-} -> Bool -> a -> Doc | 1527 | mkDoc :: Bool {-print reduced-} -> Bool -> a -> NDoc |
1526 | 1528 | ||
1527 | instance MkDoc ExpType where | 1529 | instance MkDoc ExpType where |
1528 | mkDoc pr ts e = mkDoc pr ts $ fst e | 1530 | mkDoc pr ts e = mkDoc pr ts $ fst e |
1529 | 1531 | ||
1530 | instance MkDoc Exp where | 1532 | instance MkDoc Exp where |
1531 | mkDoc pr ts e = fmap inGreen <$> f e | 1533 | mkDoc pr ts e = inGreen' $ f e |
1532 | where | 1534 | where |
1533 | f = \case | 1535 | f = \case |
1534 | -- Lam h a b -> join $ shLam (usedVar 0 b) (BLam h) <$> f a <*> pure (f b) | 1536 | -- Lam h a b -> join $ shLam (usedVar 0 b) (BLam h) <$> f a <*> pure (f b) |
1535 | Lam b -> join $ shLam True (BLam Visible) <$> f TType{-todo!-} <*> pure (f b) | 1537 | Lam b -> shLam True (BLam Visible) (f TType{-todo!-}) (f b) |
1536 | Pi h a b -> join $ shLam (usedVar 0 b) (BPi h) <$> f a <*> pure (f b) | 1538 | Pi h a b -> shLam (usedVar 0 b) (BPi h) (f a) (f b) |
1537 | ENat' n -> pure $ shAtom $ show n | 1539 | ENat' n -> shAtom $ show n |
1538 | (getTTup -> Just xs) -> shTuple <$> mapM f xs | 1540 | (getTTup -> Just xs) -> shTuple $ f <$> xs |
1539 | (getTup -> Just xs) -> shTuple <$> mapM f xs | 1541 | (getTup -> Just xs) -> shTuple $ f <$> xs |
1540 | Con s _ xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs | 1542 | Con s _ xs -> foldl (shApp Visible) (shAtom_ $ show s) (f <$> xs) |
1541 | TyConN s xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs | 1543 | TyConN s xs -> foldl (shApp Visible) (shAtom_ $ show s) (f <$> xs) |
1542 | TType -> pure $ shAtom "Type" | 1544 | TType -> shAtom "Type" |
1543 | ELit l -> pure $ shAtom $ show l | 1545 | ELit l -> shAtom $ show l |
1544 | Neut x -> mkDoc pr ts x | 1546 | Neut x -> mkDoc pr ts x |
1545 | 1547 | ||
1546 | shAtom_ = shAtom . if ts then switchTick else id | 1548 | shAtom_ = shAtom . if ts then switchTick else id |
1547 | 1549 | ||
1548 | instance MkDoc Neutral where | 1550 | instance MkDoc Neutral where |
1549 | mkDoc pr ts e = fmap inGreen <$> f e | 1551 | mkDoc pr ts e = inGreen' $ f e |
1550 | where | 1552 | where |
1551 | g = mkDoc pr ts | 1553 | g = mkDoc pr ts |
1552 | f = \case | 1554 | f = \case |
1553 | CstrT' t a b -> shCstr <$> g (a, t) <*> g (b, t) | 1555 | CstrT' t a b -> shCstr (g (a, t)) (g (b, t)) |
1554 | FL' a | pr -> g a | 1556 | FL' a | pr -> g a |
1555 | Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g xs | 1557 | Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (shAtom_ $ show s) (g <$> xs) |
1556 | Var_ k -> shAtom <$> shVar k | 1558 | Var_ k -> shVar k |
1557 | App_ a b -> shApp Visible <$> g a <*> g b | 1559 | App_ a b -> shApp Visible (g a) (g b) |
1558 | CaseFun_ s xs n -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g ({-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) | 1560 | CaseFun_ s xs n -> foldl (shApp Visible) (shAtom_ $ show s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) |
1559 | TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g (mkExpTypes (nType s) [m, t, Neut n, f]) | 1561 | TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (shAtom_ $ show s) (g <$> mkExpTypes (nType s) [m, t, Neut n, f]) |
1560 | TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" | 1562 | TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" |
1561 | LabelEnd_ x -> shApp Visible (shAtom $ "labend") <$> g x | 1563 | LabelEnd_ x -> shApp Visible (shAtom "labend") (g x) |
1562 | Delta{} -> return $ shAtom "^delta" | 1564 | Delta{} -> shAtom "^delta" |
1563 | 1565 | ||
1564 | shAtom_ = shAtom . if ts then switchTick else id | 1566 | shAtom_ = shAtom . if ts then switchTick else id |
1565 | 1567 | ||
1566 | instance MkDoc (CEnv Exp) where | 1568 | instance MkDoc (CEnv Exp) where |
1567 | mkDoc pr ts e = fmap inGreen <$> f e | 1569 | mkDoc pr ts e = inGreen' $ f e |
1568 | where | 1570 | where |
1569 | f :: CEnv Exp -> Doc | 1571 | f :: CEnv Exp -> NDoc |
1570 | f = \case | 1572 | f = \case |
1571 | MEnd a -> mkDoc pr ts a | 1573 | MEnd a -> mkDoc pr ts a |
1572 | Meta a b -> join $ shLam True BMeta <$> mkDoc pr ts a <*> pure (f b) | 1574 | Meta a b -> shLam True BMeta (mkDoc pr ts a) (f b) |
1573 | Assign i (x, _) e -> shLet i (mkDoc pr ts x) (f e) | 1575 | Assign i (x, _) e -> shLet i (mkDoc pr ts x) (f e) |
1574 | 1576 | ||
1575 | getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs | 1577 | getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs |