summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-28 00:35:01 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-28 00:35:01 +0200
commit61a8efb74a0ca34f73a848f6d9fd018c83dac343 (patch)
tree0ef19f20bfda1faad94693c5ffc4d42ec641ab0f
parent354ab8759fd3879d85e392351bb9ea1aed173e98 (diff)
refactored pretty print framework
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs292
-rw-r--r--src/LambdaCube/Compiler/Infer.hs78
-rw-r--r--src/LambdaCube/Compiler/Parser.hs17
-rw-r--r--src/LambdaCube/Compiler/Patterns.hs10
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs2
-rw-r--r--src/LambdaCube/Compiler/Statements.hs2
-rw-r--r--testdata/instantiate2.out2
-rw-r--r--testdata/typesig.reject.out8
8 files changed, 244 insertions, 167 deletions
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index af85af65..54455fc7 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -14,13 +14,11 @@ import Data.Monoid
14import Data.Maybe 14import Data.Maybe
15import Data.Char 15import Data.Char
16import Data.List 16import Data.List
17import Data.String
18import Data.Function 17import Data.Function
19import Data.Bits 18import Data.Bits
20import qualified Data.Map as Map 19import qualified Data.Map as Map
21import qualified Data.Set as Set 20import qualified Data.Set as Set
22import qualified Data.IntMap as IM 21import qualified Data.IntMap as IM
23import Control.Monad.Except
24import Control.Monad.Reader 22import Control.Monad.Reader
25import Control.Monad.State 23import Control.Monad.State
26import Control.Arrow hiding ((<+>)) 24import Control.Arrow hiding ((<+>))
@@ -28,9 +26,7 @@ import Control.DeepSeq
28 26
29import LambdaCube.Compiler.Utils 27import LambdaCube.Compiler.Utils
30import LambdaCube.Compiler.DeBruijn 28import LambdaCube.Compiler.DeBruijn
31 29import LambdaCube.Compiler.Pretty --hiding (braces, parens)
32import qualified LambdaCube.Compiler.Pretty as Parser
33import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens)
34 30
35-------------------------------------------------------------------------------- simple name 31-------------------------------------------------------------------------------- simple name
36 32
@@ -62,7 +58,7 @@ pattern MatchName cs <- 'm':'a':'t':'c':'h':cs where MatchName cs = "match" ++ c
62data FixityDir = Infix | InfixL | InfixR 58data FixityDir = Infix | InfixL | InfixR
63 deriving (Eq, Show) 59 deriving (Eq, Show)
64 60
65data Fixity = Fixity FixityDir Int 61data Fixity = Fixity !FixityDir !Int
66 deriving (Eq, Show) 62 deriving (Eq, Show)
67 63
68-------------------------------------------------------------------------------- file position 64-------------------------------------------------------------------------------- file position
@@ -91,7 +87,7 @@ instance Ord FileInfo where compare = compare `on` fileId
91instance PShow FileInfo where pShowPrec _ = text . filePath 87instance PShow FileInfo where pShowPrec _ = text . filePath
92instance Show FileInfo where show = ppShow 88instance Show FileInfo where show = ppShow
93 89
94showPos :: FileInfo -> SPos -> Parser.Doc 90showPos :: FileInfo -> SPos -> Doc
95showPos n p = pShow n <> ":" <> pShow p 91showPos n p = pShow n <> ":" <> pShow p
96 92
97-------------------------------------------------------------------------------- range 93-------------------------------------------------------------------------------- range
@@ -107,7 +103,7 @@ instance PShow Range where pShowPrec _ (Range n b e) = pShow n <+> pShow b <> "-
107instance Show Range where show = ppShow 103instance Show Range where show = ppShow
108 104
109-- long version 105-- long version
110showRange :: Range -> Parser.Doc 106showRange :: Range -> Doc
111showRange (Range n p@(SPos r c) (SPos r' c')) = vcat 107showRange (Range n p@(SPos r c) (SPos r' c')) = vcat
112 $ (showPos n p <> ":") 108 $ (showPos n p <> ":")
113 : map text (drop (r - 1) $ take r' $ lines $ fileContent n) 109 : map text (drop (r - 1) $ take r' $ lines $ fileContent n)
@@ -478,126 +474,188 @@ data Extension
478extensionMap :: Map.Map String Extension 474extensionMap :: Map.Map String Extension
479extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] 475extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ]
480 476
481-------------------------------------------------------------------------------- builtin precedences 477-------------------------------------------------------------------------------- pretty print
482 478
483data Prec 479data NDoc
484 = PrecAtom -- ( _ ) ... 480 = DAtom String
485 | PrecAtom' 481 | DOp Fixity NDoc String NDoc
486 | PrecAt -- _@_ {assoc} -- in patterns 482 | DPar String NDoc String
487 | PrecProj -- _ ._ {left} 483 | DLam String [NDoc] String NDoc
488 | PrecSwiz -- _%_ {left} 484 | DVar Int
489 | PrecApp -- _ _ {left} 485 | DFreshName Bool{-False: dummy-} NDoc
490 | PrecOp 486 | DUp Int NDoc
491 | PrecArr -- _ -> _ {right} 487 | DColor Color NDoc
492 | PrecEq -- _ ~ _ 488 -- add wl-pprint combinators as necessary here
493 | PrecAnn -- _ :: _ {right} 489 deriving (Eq)
494 | PrecLet -- _ := _ 490
495 | PrecLam -- \ _ -> _ {right} {accum} 491pattern DParen x = DPar "(" x ")"
496 deriving (Eq, Ord) 492pattern DBrace x = DPar "{" x "}"
497 493pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y
494pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y
495
496data Color = Green | Blue | Underlined
497 deriving (Eq)
498
499inGreen' = DColor Green
500inBlue' = DColor Blue
501epar = DColor Underlined
502
503strip = \case
504 DColor _ x -> strip x
505 DUp _ x -> strip x
506 DFreshName _ x -> strip x
507 x -> x
508
509simple x = case strip x of
510 DAtom{} -> True
511 DVar{} -> True
512 DPar{} -> True
513 _ -> False
514
515renderDocX :: NDoc -> Doc
516renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) . showVars
517 where
518 showVars x = case x of
519 DAtom s -> pure x
520 DColor c x -> DColor c <$> showVars x
521 DPar l x r -> DPar l <$> showVars x <*> pure r
522 DOp pr x s y -> DOp pr <$> showVars x <*> pure s <*> showVars y
523 DVar i -> asks $ DAtom . lookupVarName i
524 -- hack, remove
525 DFreshName True (DArr (DAnn a (DUp 0 b)) y) -> do
526 b' <- showVars b
527 newName $ showVars $ DArr (DAnn a b') y
528 -- hack, remove
529 DFreshName True (DArr (DUp 0 b) y) -> do
530 b' <- showVars b
531 newName $ showVars $ DArr b' y
532 DFreshName True x -> newName $ showVars x
533 DFreshName False x -> local ("?":) $ showVars x
534 DUp i x -> local (dropNth i) $ showVars x
535 DLam lam vs arr e -> DLam lam <$> (mapM showVars vs) <*> pure arr <*> showVars e
536 where
537 lookupVarName i xs | i < length xs && i >= 0 = xs !! i
538 lookupVarName i _ = "V" ++ show i -- TODO: better names here
539
540 addPar :: Int -> NDoc -> NDoc
541 addPar pr x = case x of
542 DColor c x -> DColor c $ addPar pr x
543 DAtom{} -> x
544 DPar l x r -> DPar l (addPar (-20) x) r
545 DOp pr' x s y -> (if simple x' && simple y' && s `notElem` ["", ","] then id else paren) $ DOp pr' x' s y'
546 where
547 x' = addPar (precL pr') x
548 y' = addPar (precR pr') y
549 DLam lam vs arr e -> paren $ DLam lam (addPar 10 <$> vs) arr (addPar (-10) e)
550 where
551 paren d
552 | protect x = DParen d
553 | otherwise = d
554 where
555 protect x = case x of
556 DAtom{} -> False
557 DPar{} -> False
558 DOp (Fixity _ pr') _ _ _ -> pr' < pr
559 DLam{} -> -10 < pr
560 DColor _ x -> protect x
561
562 precL (Fixity Infix i) = i+1
563 precL (Fixity InfixL i) = i
564 precL (Fixity InfixR i) = i+1
565 precR (Fixity Infix i) = i+1
566 precR (Fixity InfixL i) = i+1
567 precR (Fixity InfixR i) = i
568
569 render x = case x of
570 DColor Green x -> text $ inGreen $ show $ render x -- TODO
571 DColor Blue x -> text $ inBlue $ show $ render x -- TODO
572 DColor Underlined x -> text $ underlined $ show $ render x -- TODO
573 DAtom s -> text s
574 DPar l x r -> text l <> render x <> text r
575 DOp _ x s y -> case s of
576 "" -> render x <+> render y
577 _ | simple x && simple y && s /= "," -> render x <> text s <> render y
578 | otherwise -> (render x <++> s) <+> render y
579 DLam lam vs arr e -> text lam <> hsep (render <$> vs) <+> text arr <+> render e
580 where
581 x <++> "," = x <> text ","
582 x <++> s = x <+> text s
583
498instance Up a => PShow (SExp' a) where 584instance Up a => PShow (SExp' a) where
499 pShowPrec _ = showDoc_ . sExpDoc 585 pShowPrec _ = showDoc_ . sExpDoc
500 586
501type Doc = NameDB PrecString
502
503-- name De Bruijn indices 587-- name De Bruijn indices
504type NameDB a = StateT [String] (Reader [String]) a 588type NameDB a = StateT [String] (Reader [String]) a
505 589
506showDoc :: Doc -> String 590showDoc :: NDoc -> String
507showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) 591showDoc = show . renderDocX
508 592
509showDoc_ :: Doc -> Parser.Doc 593showDoc_ :: NDoc -> Doc
510showDoc_ = text . str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) 594showDoc_ = renderDocX
511 595
512sExpDoc :: Up a => SExp' a -> Doc 596sExpDoc :: Up a => SExp' a -> NDoc
513sExpDoc = \case 597sExpDoc = \case
514 SGlobal ns -> pure $ shAtom $ sName ns 598 SGlobal ns -> shAtom $ sName ns
515 SAnn a b -> shAnn ":" False <$> sExpDoc a <*> sExpDoc b 599 SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b)
516 TyType a -> shApp Visible (shAtom "tyType") <$> sExpDoc a 600 TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a)
517 SApp h a b -> shApp h <$> sExpDoc a <*> sExpDoc b 601 SApp h a b -> shApp h (sExpDoc a) (sExpDoc b)
518 Wildcard t -> shAnn ":" True (shAtom "_") <$> sExpDoc t 602 Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t)
519 SBind_ _ h _ a b -> join $ shLam (usedVar 0 b) h <$> sExpDoc a <*> pure (sExpDoc b) 603 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b)
520 SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b) 604 SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b)
521 STyped _{-(e,t)-} -> pure $ shAtom "<<>>" -- todo: expDoc e 605 STyped _{-(e,t)-} -> shAtom "<<>>" -- todo: expDoc e
522 SVar _ i -> shAtom <$> shVar i 606 SVar _ i -> shVar i
523 SLit _ l -> pure $ shAtom $ show l 607 SLit _ l -> shAtom $ show l
524
525shVar i = asks lookupVarName where
526 lookupVarName xs | i < length xs && i >= 0 = xs !! i
527 lookupVarName _ = "V" ++ show i
528
529newName = gets head <* modify tail
530
531shLet i a b = shAtom <$> shVar i >>= \i' -> local (dropNth i) $ shLam' <$> (cpar . shLet' (fmap inBlue i') <$> a) <*> b
532shLet_ a b = newName >>= \i -> shLam' <$> (cpar . shLet' (shAtom i) <$> a) <*> local (i:) b
533shLam usedVar h a b = newName >>= \i ->
534 let lam = case h of
535 BPi _ -> shArr
536 _ -> shLam'
537 p = case h of
538 BMeta -> cpar . shAnn ":" True (shAtom $ inBlue i)
539 BLam h -> vpar h
540 BPi h -> vpar h
541 vpar Hidden = brace . shAnn ":" True (shAtom $ inGreen i)
542 vpar Visible = ann (shAtom $ inGreen i)
543 ann | usedVar = shAnn ":" False
544 | otherwise = const id
545 in lam (p a) <$> local (i:) b
546 608
547----------------------------------------- 609shVar = DVar
610
611newName p = gets head >>= \n -> modify tail >> local (n:) p
548 612
549data PS a = PS Prec a deriving (Functor) 613shLet i a b = shLam' (cpar . shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b)
550type PrecString = PS String 614shLet_ a b = DFreshName True $ shLam' (cpar . shLet' (shVar 0) $ DUp 0 a) b
551 615
552getPrec (PS p _) = p 616shLam usedVar h a b = DFreshName True $ lam (p $ DUp 0 a) b
553prec i s = PS i (s i)
554str (PS _ s) = s
555
556lpar, rpar :: PrecString -> Prec -> String
557lpar (PS i s) j = par (i >. j) s where
558 PrecLam >. i = i > PrecAtom'
559 i >. PrecLam = i >= PrecArr
560 PrecApp >. PrecApp = False
561 i >. j = i >= j
562rpar (PS i s) j = par (i >. j) s where
563 PrecLam >. PrecLam = False
564 PrecLam >. i = i > PrecAtom'
565 PrecArr >. PrecArr = False
566 PrecAnn >. PrecAnn = False
567 i >. j = i >= j
568
569par True s = "(" ++ s ++ ")"
570par False s = s
571
572isAtom = (==PrecAtom) . getPrec
573isAtom' = (<=PrecAtom') . getPrec
574
575shAtom = PS PrecAtom
576shAtom' = PS PrecAtom'
577shTuple xs = prec PrecAtom $ \_ -> case xs of
578 [x] -> "((" ++ str x ++ "))"
579 xs -> "(" ++ intercalate ", " (map str xs) ++ ")"
580shAnn _ True x y | str y `elem` ["Type", inGreen "Type"] = x
581shAnn s simp x y | isAtom x && isAtom y = shAtom' $ str x <> s <> str y
582shAnn s simp x y = prec PrecAnn $ lpar x <> " " <> const s <> " " <> rpar y
583shApp Hidden x y = prec PrecApp $ lpar x <> " " <> const (str $ brace y)
584shApp h x y = prec PrecApp $ lpar x <> " " <> rpar y
585shArr x y | isAtom x && isAtom y = shAtom' $ str x <> "->" <> str y
586shArr x y = prec PrecArr $ lpar x <> " -> " <> rpar y
587shCstr x y | isAtom x && isAtom y = shAtom' $ str x <> "~" <> str y
588shCstr x y = prec PrecEq $ lpar x <> " ~ " <> rpar y
589shLet' x y | isAtom x && isAtom y = shAtom' $ str x <> ":=" <> str y
590shLet' x y = prec PrecLet $ lpar x <> " := " <> rpar y
591shLam' x y | PrecLam <- getPrec y = prec PrecLam $ "\\" <> lpar x <> " " <> pure (dropC $ str y)
592 where 617 where
593 dropC (ESC s (dropC -> x)) = ESC s x 618 lam = case h of
594 dropC (x: xs) = xs 619 BPi{} -> shArr
595shLam' x y | isAtom x && isAtom y = shAtom' $ "\\" <> str x <> "->" <> str y 620 _ -> shLam'
596shLam' x y = prec PrecLam $ "\\" <> lpar x <> " -> " <> rpar y 621
597brace s = shAtom $ "{" <> str s <> "}" 622 p = case h of
598cpar s | isAtom' s = s -- TODO: replace with lpar, rpar 623 BMeta -> cpar . shAnn ":" True (inBlue' $ DVar 0)
599cpar s = shAtom $ par True $ str s 624 BLam h -> vpar h
600epar = fmap underlined 625 BPi h -> vpar h
601 626
602instance IsString (Prec -> String) where fromString = const 627 vpar Hidden = (\p -> DBrace p) . shAnn ":" True (inGreen' $ DVar 0)
628 vpar Visible = ann (inGreen' $ DVar 0)
629
630 ann | usedVar = shAnn ":" False
631 | otherwise = const id
632
633-----------------------------------------
634
635shAtom = DAtom
636
637shTuple [] = DAtom "()"
638shTuple [x] = DParen $ DParen x
639shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs
640
641shAnn _ True x y | strip y == DAtom "Type" = x
642shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y
643
644shApp _ x y = DOp (Fixity InfixL 10) x "" y
645
646shArr = DArr
647
648shCstr x y = DOp (Fixity Infix (-2)) x "~" y
649
650shLet' x y = DOp (Fixity Infix (-4)) x ":=" y
651
652getFN (DFreshName True a) = first (+1) $ getFN a
653getFN a = (0, a)
654
655shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y
656shLam' x y = DLam "\\" [x] "->" y
657
658cpar s | simple s = s
659cpar s = DParen s
660
603 661
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
48import LambdaCube.Compiler.Utils 48import LambdaCube.Compiler.Utils
49import LambdaCube.Compiler.DeBruijn 49import LambdaCube.Compiler.DeBruijn
50import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) 50import LambdaCube.Compiler.Pretty hiding (braces, parens)
51import LambdaCube.Compiler.DesugaredSource hiding (getList) 51import LambdaCube.Compiler.DesugaredSource hiding (getList)
52import LambdaCube.Compiler.Parser (ParseWarning) -- TODO: remove 52import 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
1459instance PShow Env where 1459instance PShow Env where
1460 pShowPrec _ e = showDoc_ $ envDoc e $ pure $ shAtom $ underlined "<<HERE>>" 1460 pShowPrec _ e = showDoc_ $ envDoc e $ shAtom $ underlined "<<HERE>>"
1461 1461
1462showEnvExp :: Env -> ExpType -> String 1462showEnvExp :: Env -> ExpType -> String
1463showEnvExp e c = showDoc $ envDoc e $ epar <$> mkDoc False False c 1463showEnvExp e c = showDoc $ envDoc e $ epar $ mkDoc False False c
1464 1464
1465showEnvSExp :: Up a => Env -> SExp' a -> String 1465showEnvSExp :: Up a => Env -> SExp' a -> String
1466showEnvSExp e c = showDoc $ envDoc e $ epar <$> sExpDoc c 1466showEnvSExp e c = showDoc $ envDoc e $ epar $ sExpDoc c
1467 1467
1468showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String 1468showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String
1469showEnvSExpType e c t = showDoc $ envDoc e $ epar <$> (shAnn "::" False <$> sExpDoc c <**> mkDoc False False (t, TType)) 1469showEnvSExpType 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{-
1476expToSExp :: Exp -> SExp 1478expToSExp :: 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-}
1504envDoc :: Env -> Doc -> Doc 1506envDoc :: Env -> NDoc -> NDoc
1505envDoc x m = case x of 1507envDoc 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
1524class MkDoc a where 1526class MkDoc a where
1525 mkDoc :: Bool {-print reduced-} -> Bool -> a -> Doc 1527 mkDoc :: Bool {-print reduced-} -> Bool -> a -> NDoc
1526 1528
1527instance MkDoc ExpType where 1529instance 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
1530instance MkDoc Exp where 1532instance 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
1548instance MkDoc Neutral where 1550instance 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
1566instance MkDoc (CEnv Exp) where 1568instance 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
1575getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs 1577getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 7dfcd580..8a9e56c1 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -142,6 +142,23 @@ mkDesugarInfo ss = DesugarInfo
142 Data sn _ _ cs -> Set.fromList $ sName sn: map (sName . fst) cs 142 Data sn _ _ cs -> Set.fromList $ sName sn: map (sName . fst) cs
143 PrecDef{} -> mempty 143 PrecDef{} -> mempty
144 144
145-------------------------------------------------------------------------------- precedences -- TODO: remove
146
147data Prec
148 = PrecAtom -- ( _ ) ... -- 20
149 | PrecAtom'
150 | PrecAt -- _@_ {assoc} -- in patterns -- 13
151 | PrecProj -- _ ._ {left} -- 12
152 | PrecSwiz -- _%_ {left} -- 11
153 | PrecApp -- _ _ {left} -- 10
154 | PrecOp -- 0 - 9
155 | PrecArr -- _ -> _ {right} -- -1
156 | PrecEq -- _ ~ _ -- -2
157 | PrecAnn -- _ :: _ {right} -- -3
158 | PrecLet -- _ := _ -- -4
159 | PrecLam -- \ _ -> _ {right} {accum} -- -10
160 -- _ , _ {right} -- -20
161 deriving (Eq, Ord)
145 162
146-------------------------------------------------------------------------------- expression parsing 163-------------------------------------------------------------------------------- expression parsing
147 164
diff --git a/src/LambdaCube/Compiler/Patterns.hs b/src/LambdaCube/Compiler/Patterns.hs
index 0ebb6a97..00626d2e 100644
--- a/src/LambdaCube/Compiler/Patterns.hs
+++ b/src/LambdaCube/Compiler/Patterns.hs
@@ -18,7 +18,7 @@ import Control.Arrow hiding ((<+>))
18 18
19import LambdaCube.Compiler.Utils 19import LambdaCube.Compiler.Utils
20import LambdaCube.Compiler.DeBruijn 20import LambdaCube.Compiler.DeBruijn
21import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) 21import LambdaCube.Compiler.Pretty hiding (braces, parens)
22import LambdaCube.Compiler.DesugaredSource 22import LambdaCube.Compiler.DesugaredSource
23 23
24--------------------------------- 24---------------------------------
@@ -157,13 +157,13 @@ instance SetSourceInfo (Pat_ c) where
157 157
158-------------------------------------------------------------------------------- pretty print 158-------------------------------------------------------------------------------- pretty print
159 159
160patDoc :: Pat_ a -> Doc 160patDoc :: Pat_ a -> NDoc
161patDoc = \case 161patDoc = \case
162 PCon (n, _) _ -> pure $ shAtom $ sName n -- TODO 162 PCon (n, _) _ -> shAtom $ sName n -- TODO
163 163
164parPatDoc :: ParPat_ a -> Doc 164parPatDoc :: ParPat_ a -> NDoc
165parPatDoc = \case 165parPatDoc = \case
166 ParPat [] -> pure $ shAtom "_" 166 ParPat [] -> shAtom "_"
167 ParPat [p] -> patDoc p 167 ParPat [p] -> patDoc p
168 -- TODO 168 -- TODO
169-------------------------------------------------------------------------------- pattern match compilation 169-------------------------------------------------------------------------------- pattern match compilation
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 2fa539a3..e1d41fc1 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -120,7 +120,7 @@ splitESC _ = Nothing
120 120
121withEsc i s = ESC (show i) $ s ++ ESC "" "" 121withEsc i s = ESC (show i) $ s ++ ESC "" ""
122 122
123inGreen = withEsc 32 123inGreen = withEsc 32 -- TODO
124inBlue = withEsc 34 124inBlue = withEsc 34
125inRed = withEsc 31 125inRed = withEsc 31
126underlined = withEsc 47 126underlined = withEsc 47
diff --git a/src/LambdaCube/Compiler/Statements.hs b/src/LambdaCube/Compiler/Statements.hs
index afb5fbc1..fa4f02bc 100644
--- a/src/LambdaCube/Compiler/Statements.hs
+++ b/src/LambdaCube/Compiler/Statements.hs
@@ -19,7 +19,7 @@ import Control.Arrow hiding ((<+>))
19--import Debug.Trace 19--import Debug.Trace
20 20
21import LambdaCube.Compiler.DeBruijn 21import LambdaCube.Compiler.DeBruijn
22import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) 22import LambdaCube.Compiler.Pretty hiding (braces, parens)
23import LambdaCube.Compiler.DesugaredSource 23import LambdaCube.Compiler.DesugaredSource
24import LambdaCube.Compiler.Patterns 24import LambdaCube.Compiler.Patterns
25 25
diff --git a/testdata/instantiate2.out b/testdata/instantiate2.out
index b7d32356..1a2a8631 100644
--- a/testdata/instantiate2.out
+++ b/testdata/instantiate2.out
@@ -1 +1 @@
(+ ('VecS 'Float 2) TT (V2 1.0 2.0) (V2 2.3 3.4), 3.0) \ No newline at end of file (+ ('VecS 'Float 2) TT (V2 1.0 2.0) (V2 2.3 3.4), 3.0) \ No newline at end of file
diff --git a/testdata/typesig.reject.out b/testdata/typesig.reject.out
index 9c2979a6..590a5d21 100644
--- a/testdata/typesig.reject.out
+++ b/testdata/typesig.reject.out
@@ -1,12 +1,12 @@
1focus checkMetas: \a -> (\b:Type -> primFix a b) (\a:=Type c:Type -> <<HERE>>) 1focus checkMetas: \a -> (\b:Type -> primFix a b) (\(a:=Type) -> \c:Type -> <<HERE>>)
2\(a : Type~V0) (b : V1~'X) -> typeAnn V2 (labend X) 2\(a : Type~V0) (b : V1~'X) -> typeAnn V2 (labend X)
3------------ trace 3------------ trace
4'X :: Type 4'X :: Type
5X :: 'X 5X :: 'X
6'XCase :: (b : 'X->Type) -> b X -> d:'X -> b d 6'XCase :: (b : 'X->Type) -> b X -> d:'X -> b d
7match'X :: (b : Type->Type) -> b 'X -> d:Type -> b d -> b d 7match'X :: (b : Type->Type) -> b 'X -> d:Type -> b d -> b d
8!focus checkMetas: \a -> (\b:Type -> primFix a b) (\a:=Type c:Type -> <<HERE>>) 8!focus checkMetas: \a -> (\b:Type -> primFix a b) (\(a:=Type) -> \c:Type -> <<HERE>>)
9\(a : Type~V0) (b : V1~'X) -> typeAnn V2 (labend X) 9\(a : Type~V0) (b : V1~'X) -> typeAnn V2 (labend X)
10------------ tooltips 10------------ tooltips
11testdata/typesig.reject.lc 4:6-4:7 Type 11testdata/typesig.reject.lc 4:6-4:7 Type
12testdata/typesig.reject.lc 4:6-4:11 Type 12testdata/typesig.reject.lc 4:6-4:11 Type