diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-04-18 15:21:54 +0200 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-04-18 15:21:54 +0200 |
commit | 3b65c82ce70a4ebac78bd7281d7f712fdb2d8667 (patch) | |
tree | fb4d00abb5c3a36c0abee1497921f17626c8f1d4 /src | |
parent | 0d1219cbd364e35eb242771f5c47c421f422e009 (diff) |
can reduce when pretty print if necessary
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 2fbb88da..09137a59 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -29,6 +29,7 @@ module LambdaCube.Compiler.Infer | |||
29 | , MaxDB, unfixlabel | 29 | , MaxDB, unfixlabel |
30 | , ErrorMsg, errorRange | 30 | , ErrorMsg, errorRange |
31 | , FName (..) | 31 | , FName (..) |
32 | , MkDoc (..) | ||
32 | ) where | 33 | ) where |
33 | 34 | ||
34 | import Data.Monoid | 35 | import Data.Monoid |
@@ -1431,29 +1432,29 @@ defined' = Map.keys | |||
1431 | -- todo: proper handling of implicit foralls | 1432 | -- todo: proper handling of implicit foralls |
1432 | addF = asks $ \(exs, ge) -> addForalls exs $ defined' ge | 1433 | addF = asks $ \(exs, ge) -> addForalls exs $ defined' ge |
1433 | 1434 | ||
1434 | tellType si t = tell $ mkInfoItem (sourceInfo si) $ removeEscs $ showDoc $ mkDoc True (t, TType) | 1435 | tellType si t = tell $ mkInfoItem (sourceInfo si) $ removeEscs $ showDoc $ mkDoc False True (t, TType) |
1435 | 1436 | ||
1436 | 1437 | ||
1437 | -------------------------------------------------------------------------------- pretty print | 1438 | -------------------------------------------------------------------------------- pretty print |
1438 | -- todo: do this via conversion to SExp | 1439 | -- todo: do this via conversion to SExp |
1439 | 1440 | ||
1440 | instance PShow Exp where | 1441 | instance PShow Exp where |
1441 | pShowPrec _ = showDoc_ . mkDoc False | 1442 | pShowPrec _ = showDoc_ . mkDoc False False |
1442 | 1443 | ||
1443 | instance PShow (CEnv Exp) where | 1444 | instance PShow (CEnv Exp) where |
1444 | pShowPrec _ = showDoc_ . mkDoc False | 1445 | pShowPrec _ = showDoc_ . mkDoc False False |
1445 | 1446 | ||
1446 | instance PShow Env where | 1447 | instance PShow Env where |
1447 | pShowPrec _ e = showDoc_ $ envDoc e $ pure $ shAtom $ underlined "<<HERE>>" | 1448 | pShowPrec _ e = showDoc_ $ envDoc e $ pure $ shAtom $ underlined "<<HERE>>" |
1448 | 1449 | ||
1449 | showEnvExp :: Env -> ExpType -> String | 1450 | showEnvExp :: Env -> ExpType -> String |
1450 | showEnvExp e c = showDoc $ envDoc e $ epar <$> mkDoc False c | 1451 | showEnvExp e c = showDoc $ envDoc e $ epar <$> mkDoc False False c |
1451 | 1452 | ||
1452 | showEnvSExp :: Up a => Env -> SExp' a -> String | 1453 | showEnvSExp :: Up a => Env -> SExp' a -> String |
1453 | showEnvSExp e c = showDoc $ envDoc e $ epar <$> sExpDoc c | 1454 | showEnvSExp e c = showDoc $ envDoc e $ epar <$> sExpDoc c |
1454 | 1455 | ||
1455 | showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String | 1456 | showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String |
1456 | showEnvSExpType e c t = showDoc $ envDoc e $ epar <$> (shAnn "::" False <$> sExpDoc c <**> mkDoc False (t, TType)) | 1457 | showEnvSExpType e c t = showDoc $ envDoc e $ epar <$> (shAnn "::" False <$> sExpDoc c <**> mkDoc False False (t, TType)) |
1457 | where | 1458 | where |
1458 | infixl 4 <**> | 1459 | infixl 4 <**> |
1459 | (<**>) :: NameDB (a -> b) -> NameDB a -> NameDB b | 1460 | (<**>) :: NameDB (a -> b) -> NameDB a -> NameDB b |
@@ -1492,14 +1493,14 @@ envDoc :: Env -> Doc -> Doc | |||
1492 | envDoc x m = case x of | 1493 | envDoc x m = case x of |
1493 | EGlobal{} -> m | 1494 | EGlobal{} -> m |
1494 | EBind1 _ h ts b -> envDoc ts $ join $ shLam (used 0 b) h <$> m <*> pure (sExpDoc b) | 1495 | EBind1 _ h ts b -> envDoc ts $ join $ shLam (used 0 b) h <$> m <*> pure (sExpDoc b) |
1495 | EBind2 h a ts -> envDoc ts $ join $ shLam True h <$> mkDoc ts' (a, TType) <*> pure m | 1496 | EBind2 h a ts -> envDoc ts $ join $ shLam True h <$> mkDoc False ts' (a, TType) <*> pure m |
1496 | EApp1 _ h ts b -> envDoc ts $ shApp h <$> m <*> sExpDoc b | 1497 | EApp1 _ h ts b -> envDoc ts $ shApp h <$> m <*> sExpDoc b |
1497 | EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (shAtom "tyType") <$> m | 1498 | EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (shAtom "tyType") <$> m |
1498 | EApp2 _ h a ts -> envDoc ts $ shApp h <$> mkDoc ts' a <*> m | 1499 | EApp2 _ h a ts -> envDoc ts $ shApp h <$> mkDoc False ts' a <*> m |
1499 | ELet1 _ ts b -> envDoc ts $ shLet_ m (sExpDoc b) | 1500 | ELet1 _ ts b -> envDoc ts $ shLet_ m (sExpDoc b) |
1500 | ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc ts' x) m | 1501 | ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False ts' x) m |
1501 | EAssign i x ts -> envDoc ts $ shLet i (mkDoc ts' x) m | 1502 | EAssign i x ts -> envDoc ts $ shLet i (mkDoc False ts' x) m |
1502 | CheckType t ts -> envDoc ts $ shAnn ":" False <$> m <*> mkDoc ts' (t, TType) | 1503 | CheckType t ts -> envDoc ts $ shAnn ":" False <$> m <*> mkDoc False ts' (t, TType) |
1503 | CheckIType t ts -> envDoc ts $ shAnn ":" False <$> m <*> pure (shAtom "??") -- mkDoc ts' t | 1504 | CheckIType t ts -> envDoc ts $ shAnn ":" False <$> m <*> pure (shAtom "??") -- mkDoc ts' t |
1504 | -- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t | 1505 | -- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t |
1505 | CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m | 1506 | CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m |
@@ -1509,13 +1510,13 @@ envDoc x m = case x of | |||
1509 | ts' = False | 1510 | ts' = False |
1510 | 1511 | ||
1511 | class MkDoc a where | 1512 | class MkDoc a where |
1512 | mkDoc :: Bool -> a -> Doc | 1513 | mkDoc :: Bool {-print reduced-} -> Bool -> a -> Doc |
1513 | 1514 | ||
1514 | instance MkDoc ExpType where | 1515 | instance MkDoc ExpType where |
1515 | mkDoc ts e = mkDoc ts $ fst e | 1516 | mkDoc pr ts e = mkDoc pr ts $ fst e |
1516 | 1517 | ||
1517 | instance MkDoc Exp where | 1518 | instance MkDoc Exp where |
1518 | mkDoc ts e = fmap inGreen <$> f e | 1519 | mkDoc pr ts e = fmap inGreen <$> f e |
1519 | where | 1520 | where |
1520 | f = \case | 1521 | f = \case |
1521 | -- Lam h a b -> join $ shLam (used 0 b) (BLam h) <$> f a <*> pure (f b) | 1522 | -- Lam h a b -> join $ shLam (used 0 b) (BLam h) <$> f a <*> pure (f b) |
@@ -1528,16 +1529,17 @@ instance MkDoc Exp where | |||
1528 | TyConN s xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs | 1529 | TyConN s xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs |
1529 | TType -> pure $ shAtom "Type" | 1530 | TType -> pure $ shAtom "Type" |
1530 | ELit l -> pure $ shAtom $ show l | 1531 | ELit l -> pure $ shAtom $ show l |
1531 | Neut x -> mkDoc ts x | 1532 | Neut x -> mkDoc pr ts x |
1532 | 1533 | ||
1533 | shAtom_ = shAtom . if ts then switchTick else id | 1534 | shAtom_ = shAtom . if ts then switchTick else id |
1534 | 1535 | ||
1535 | instance MkDoc Neutral where | 1536 | instance MkDoc Neutral where |
1536 | mkDoc ts e = fmap inGreen <$> f e | 1537 | mkDoc pr ts e = fmap inGreen <$> f e |
1537 | where | 1538 | where |
1538 | g = mkDoc ts | 1539 | g = mkDoc pr ts |
1539 | f = \case | 1540 | f = \case |
1540 | CstrT' t a b -> shCstr <$> g (a, t) <*> g (b, t) | 1541 | CstrT' t a b -> shCstr <$> g (a, t) <*> g (b, t) |
1542 | FL' a | pr -> g a | ||
1541 | Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g xs | 1543 | Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM g xs |
1542 | Var_ k -> shAtom <$> shVar k | 1544 | Var_ k -> shAtom <$> shVar k |
1543 | App_ a b -> shApp Visible <$> g a <*> g b | 1545 | App_ a b -> shApp Visible <$> g a <*> g b |
@@ -1550,13 +1552,13 @@ instance MkDoc Neutral where | |||
1550 | shAtom_ = shAtom . if ts then switchTick else id | 1552 | shAtom_ = shAtom . if ts then switchTick else id |
1551 | 1553 | ||
1552 | instance MkDoc (CEnv Exp) where | 1554 | instance MkDoc (CEnv Exp) where |
1553 | mkDoc ts e = fmap inGreen <$> f e | 1555 | mkDoc pr ts e = fmap inGreen <$> f e |
1554 | where | 1556 | where |
1555 | f :: CEnv Exp -> Doc | 1557 | f :: CEnv Exp -> Doc |
1556 | f = \case | 1558 | f = \case |
1557 | MEnd a -> mkDoc ts a | 1559 | MEnd a -> mkDoc pr ts a |
1558 | Meta a b -> join $ shLam True BMeta <$> mkDoc ts a <*> pure (f b) | 1560 | Meta a b -> join $ shLam True BMeta <$> mkDoc pr ts a <*> pure (f b) |
1559 | Assign i (x, _) e -> shLet i (mkDoc ts x) (f e) | 1561 | Assign i (x, _) e -> shLet i (mkDoc pr ts x) (f e) |
1560 | 1562 | ||
1561 | getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs | 1563 | getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs |
1562 | getTup (unfixlabel -> ConN FHNil []) = Just [] | 1564 | getTup (unfixlabel -> ConN FHNil []) = Just [] |