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 | |
parent | 354ab8759fd3879d85e392351bb9ea1aed173e98 (diff) |
refactored pretty print framework
-rw-r--r-- | src/LambdaCube/Compiler/DesugaredSource.hs | 292 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 78 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 17 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Patterns.hs | 10 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 2 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Statements.hs | 2 | ||||
-rw-r--r-- | testdata/instantiate2.out | 2 | ||||
-rw-r--r-- | testdata/typesig.reject.out | 8 |
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 | |||
14 | import Data.Maybe | 14 | import Data.Maybe |
15 | import Data.Char | 15 | import Data.Char |
16 | import Data.List | 16 | import Data.List |
17 | import Data.String | ||
18 | import Data.Function | 17 | import Data.Function |
19 | import Data.Bits | 18 | import Data.Bits |
20 | import qualified Data.Map as Map | 19 | import qualified Data.Map as Map |
21 | import qualified Data.Set as Set | 20 | import qualified Data.Set as Set |
22 | import qualified Data.IntMap as IM | 21 | import qualified Data.IntMap as IM |
23 | import Control.Monad.Except | ||
24 | import Control.Monad.Reader | 22 | import Control.Monad.Reader |
25 | import Control.Monad.State | 23 | import Control.Monad.State |
26 | import Control.Arrow hiding ((<+>)) | 24 | import Control.Arrow hiding ((<+>)) |
@@ -28,9 +26,7 @@ import Control.DeepSeq | |||
28 | 26 | ||
29 | import LambdaCube.Compiler.Utils | 27 | import LambdaCube.Compiler.Utils |
30 | import LambdaCube.Compiler.DeBruijn | 28 | import LambdaCube.Compiler.DeBruijn |
31 | 29 | import LambdaCube.Compiler.Pretty --hiding (braces, parens) | |
32 | import qualified LambdaCube.Compiler.Pretty as Parser | ||
33 | import 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 | |||
62 | data FixityDir = Infix | InfixL | InfixR | 58 | data FixityDir = Infix | InfixL | InfixR |
63 | deriving (Eq, Show) | 59 | deriving (Eq, Show) |
64 | 60 | ||
65 | data Fixity = Fixity FixityDir Int | 61 | data 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 | |||
91 | instance PShow FileInfo where pShowPrec _ = text . filePath | 87 | instance PShow FileInfo where pShowPrec _ = text . filePath |
92 | instance Show FileInfo where show = ppShow | 88 | instance Show FileInfo where show = ppShow |
93 | 89 | ||
94 | showPos :: FileInfo -> SPos -> Parser.Doc | 90 | showPos :: FileInfo -> SPos -> Doc |
95 | showPos n p = pShow n <> ":" <> pShow p | 91 | showPos 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 <> "- | |||
107 | instance Show Range where show = ppShow | 103 | instance Show Range where show = ppShow |
108 | 104 | ||
109 | -- long version | 105 | -- long version |
110 | showRange :: Range -> Parser.Doc | 106 | showRange :: Range -> Doc |
111 | showRange (Range n p@(SPos r c) (SPos r' c')) = vcat | 107 | showRange (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 | |||
478 | extensionMap :: Map.Map String Extension | 474 | extensionMap :: Map.Map String Extension |
479 | extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] | 475 | extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] |
480 | 476 | ||
481 | -------------------------------------------------------------------------------- builtin precedences | 477 | -------------------------------------------------------------------------------- pretty print |
482 | 478 | ||
483 | data Prec | 479 | data 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} | 491 | pattern DParen x = DPar "(" x ")" |
496 | deriving (Eq, Ord) | 492 | pattern DBrace x = DPar "{" x "}" |
497 | 493 | pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y | |
494 | pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y | ||
495 | |||
496 | data Color = Green | Blue | Underlined | ||
497 | deriving (Eq) | ||
498 | |||
499 | inGreen' = DColor Green | ||
500 | inBlue' = DColor Blue | ||
501 | epar = DColor Underlined | ||
502 | |||
503 | strip = \case | ||
504 | DColor _ x -> strip x | ||
505 | DUp _ x -> strip x | ||
506 | DFreshName _ x -> strip x | ||
507 | x -> x | ||
508 | |||
509 | simple x = case strip x of | ||
510 | DAtom{} -> True | ||
511 | DVar{} -> True | ||
512 | DPar{} -> True | ||
513 | _ -> False | ||
514 | |||
515 | renderDocX :: NDoc -> Doc | ||
516 | renderDocX = 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 | |||
498 | instance Up a => PShow (SExp' a) where | 584 | instance Up a => PShow (SExp' a) where |
499 | pShowPrec _ = showDoc_ . sExpDoc | 585 | pShowPrec _ = showDoc_ . sExpDoc |
500 | 586 | ||
501 | type Doc = NameDB PrecString | ||
502 | |||
503 | -- name De Bruijn indices | 587 | -- name De Bruijn indices |
504 | type NameDB a = StateT [String] (Reader [String]) a | 588 | type NameDB a = StateT [String] (Reader [String]) a |
505 | 589 | ||
506 | showDoc :: Doc -> String | 590 | showDoc :: NDoc -> String |
507 | showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) | 591 | showDoc = show . renderDocX |
508 | 592 | ||
509 | showDoc_ :: Doc -> Parser.Doc | 593 | showDoc_ :: NDoc -> Doc |
510 | showDoc_ = text . str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) | 594 | showDoc_ = renderDocX |
511 | 595 | ||
512 | sExpDoc :: Up a => SExp' a -> Doc | 596 | sExpDoc :: Up a => SExp' a -> NDoc |
513 | sExpDoc = \case | 597 | sExpDoc = \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 | |||
525 | shVar i = asks lookupVarName where | ||
526 | lookupVarName xs | i < length xs && i >= 0 = xs !! i | ||
527 | lookupVarName _ = "V" ++ show i | ||
528 | |||
529 | newName = gets head <* modify tail | ||
530 | |||
531 | shLet i a b = shAtom <$> shVar i >>= \i' -> local (dropNth i) $ shLam' <$> (cpar . shLet' (fmap inBlue i') <$> a) <*> b | ||
532 | shLet_ a b = newName >>= \i -> shLam' <$> (cpar . shLet' (shAtom i) <$> a) <*> local (i:) b | ||
533 | shLam 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 | ----------------------------------------- | 609 | shVar = DVar |
610 | |||
611 | newName p = gets head >>= \n -> modify tail >> local (n:) p | ||
548 | 612 | ||
549 | data PS a = PS Prec a deriving (Functor) | 613 | shLet i a b = shLam' (cpar . shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b) |
550 | type PrecString = PS String | 614 | shLet_ a b = DFreshName True $ shLam' (cpar . shLet' (shVar 0) $ DUp 0 a) b |
551 | 615 | ||
552 | getPrec (PS p _) = p | 616 | shLam usedVar h a b = DFreshName True $ lam (p $ DUp 0 a) b |
553 | prec i s = PS i (s i) | ||
554 | str (PS _ s) = s | ||
555 | |||
556 | lpar, rpar :: PrecString -> Prec -> String | ||
557 | lpar (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 | ||
562 | rpar (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 | |||
569 | par True s = "(" ++ s ++ ")" | ||
570 | par False s = s | ||
571 | |||
572 | isAtom = (==PrecAtom) . getPrec | ||
573 | isAtom' = (<=PrecAtom') . getPrec | ||
574 | |||
575 | shAtom = PS PrecAtom | ||
576 | shAtom' = PS PrecAtom' | ||
577 | shTuple xs = prec PrecAtom $ \_ -> case xs of | ||
578 | [x] -> "((" ++ str x ++ "))" | ||
579 | xs -> "(" ++ intercalate ", " (map str xs) ++ ")" | ||
580 | shAnn _ True x y | str y `elem` ["Type", inGreen "Type"] = x | ||
581 | shAnn s simp x y | isAtom x && isAtom y = shAtom' $ str x <> s <> str y | ||
582 | shAnn s simp x y = prec PrecAnn $ lpar x <> " " <> const s <> " " <> rpar y | ||
583 | shApp Hidden x y = prec PrecApp $ lpar x <> " " <> const (str $ brace y) | ||
584 | shApp h x y = prec PrecApp $ lpar x <> " " <> rpar y | ||
585 | shArr x y | isAtom x && isAtom y = shAtom' $ str x <> "->" <> str y | ||
586 | shArr x y = prec PrecArr $ lpar x <> " -> " <> rpar y | ||
587 | shCstr x y | isAtom x && isAtom y = shAtom' $ str x <> "~" <> str y | ||
588 | shCstr x y = prec PrecEq $ lpar x <> " ~ " <> rpar y | ||
589 | shLet' x y | isAtom x && isAtom y = shAtom' $ str x <> ":=" <> str y | ||
590 | shLet' x y = prec PrecLet $ lpar x <> " := " <> rpar y | ||
591 | shLam' 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 |
595 | shLam' x y | isAtom x && isAtom y = shAtom' $ "\\" <> str x <> "->" <> str y | 620 | _ -> shLam' |
596 | shLam' x y = prec PrecLam $ "\\" <> lpar x <> " -> " <> rpar y | 621 | |
597 | brace s = shAtom $ "{" <> str s <> "}" | 622 | p = case h of |
598 | cpar s | isAtom' s = s -- TODO: replace with lpar, rpar | 623 | BMeta -> cpar . shAnn ":" True (inBlue' $ DVar 0) |
599 | cpar s = shAtom $ par True $ str s | 624 | BLam h -> vpar h |
600 | epar = fmap underlined | 625 | BPi h -> vpar h |
601 | 626 | ||
602 | instance 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 | |||
635 | shAtom = DAtom | ||
636 | |||
637 | shTuple [] = DAtom "()" | ||
638 | shTuple [x] = DParen $ DParen x | ||
639 | shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs | ||
640 | |||
641 | shAnn _ True x y | strip y == DAtom "Type" = x | ||
642 | shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y | ||
643 | |||
644 | shApp _ x y = DOp (Fixity InfixL 10) x "" y | ||
645 | |||
646 | shArr = DArr | ||
647 | |||
648 | shCstr x y = DOp (Fixity Infix (-2)) x "~" y | ||
649 | |||
650 | shLet' x y = DOp (Fixity Infix (-4)) x ":=" y | ||
651 | |||
652 | getFN (DFreshName True a) = first (+1) $ getFN a | ||
653 | getFN a = (0, a) | ||
654 | |||
655 | shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y | ||
656 | shLam' x y = DLam "\\" [x] "->" y | ||
657 | |||
658 | cpar s | simple s = s | ||
659 | cpar 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 | ||
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 |
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 | |||
147 | data 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 | ||
19 | import LambdaCube.Compiler.Utils | 19 | import LambdaCube.Compiler.Utils |
20 | import LambdaCube.Compiler.DeBruijn | 20 | import LambdaCube.Compiler.DeBruijn |
21 | import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) | 21 | import LambdaCube.Compiler.Pretty hiding (braces, parens) |
22 | import LambdaCube.Compiler.DesugaredSource | 22 | import 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 | ||
160 | patDoc :: Pat_ a -> Doc | 160 | patDoc :: Pat_ a -> NDoc |
161 | patDoc = \case | 161 | patDoc = \case |
162 | PCon (n, _) _ -> pure $ shAtom $ sName n -- TODO | 162 | PCon (n, _) _ -> shAtom $ sName n -- TODO |
163 | 163 | ||
164 | parPatDoc :: ParPat_ a -> Doc | 164 | parPatDoc :: ParPat_ a -> NDoc |
165 | parPatDoc = \case | 165 | parPatDoc = \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 | ||
121 | withEsc i s = ESC (show i) $ s ++ ESC "" "" | 121 | withEsc i s = ESC (show i) $ s ++ ESC "" "" |
122 | 122 | ||
123 | inGreen = withEsc 32 | 123 | inGreen = withEsc 32 -- TODO |
124 | inBlue = withEsc 34 | 124 | inBlue = withEsc 34 |
125 | inRed = withEsc 31 | 125 | inRed = withEsc 31 |
126 | underlined = withEsc 47 | 126 | underlined = 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 | ||
21 | import LambdaCube.Compiler.DeBruijn | 21 | import LambdaCube.Compiler.DeBruijn |
22 | import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) | 22 | import LambdaCube.Compiler.Pretty hiding (braces, parens) |
23 | import LambdaCube.Compiler.DesugaredSource | 23 | import LambdaCube.Compiler.DesugaredSource |
24 | import LambdaCube.Compiler.Patterns | 24 | import 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 @@ | |||
[32m([32m+ ([32m'VecS 'Float 2[m) [32mTT[m ([32mV2 1.0 2.0[m) ([32mV2 2.3 3.4[m)[m, 3.0)[m \ No newline at end of file | [32m([32m+ [32m('VecS 'Float 2)[m [32mTT[m [32m(V2 1.0 2.0)[m [32m(V2 2.3 3.4)[m[m, 3.0)[m \ 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 @@ | |||
1 | focus checkMetas: \[34ma[m -> ([32m\[32mb[m:Type -> [32mprimFix [32m[32ma[m[m [32m[32mb[m[m[m[m) (\[34ma[m:=[32mType[m [32mc[m:[32mType[m -> [47m<<HERE>>[m) | 1 | focus checkMetas: \[34ma[m -> [32m(\[32mb[m:Type -> [32mprimFix [32m[32ma[m[m [32m[32mb[m[m[m)[m (\([34ma[m:=[32mType[m) -> \[32mc[m:[32mType[m -> [47m<<HERE>>[m) |
2 | [32m\([34ma[m : [32m[32m[32mType[m~[32m[32mV0[m[m[m[m) ([34mb[m : [32m[32m[32m[32mV1[m[m~[32m'X[m[m[m) -> [32m[32mtypeAnn [32m[32mV2[m[m ([32m[32mlabend [32mX[m[m[m)[m[m[m | 2 | [32m\([34ma[m : [32m[32m[32mType[m~[32m[32mV0[m[m[m[m) ([34mb[m : [32m[32m[32m[32mV1[m[m~[32m'X[m[m[m) -> [32m[32mtypeAnn [32m[32mV2[m[m [32m[32m(labend [32mX[m)[m[m[m[m[m |
3 | ------------ trace | 3 | ------------ trace |
4 | 'X :: [32mType[39m[K | 4 | 'X :: [32mType[39m[K |
5 | X :: [32m'X[39m[K | 5 | X :: [32m'X[39m[K |
6 | 'XCase :: [32m(b : 'X->Type) -> b X -> d:'X -> b d[39m[K | 6 | 'XCase :: [32m(b : 'X->Type) -> b X -> d:'X -> b d[39m[K |
7 | match'X :: [32m(b : Type->Type) -> b 'X -> d:Type -> b d -> b d[39m[K | 7 | match'X :: [32m(b : Type->Type) -> b 'X -> d:Type -> b d -> b d[39m[K |
8 | !focus checkMetas: \[34ma[m -> ([32m\[32mb[m:Type -> [32mprimFix [32m[32ma[m[m [32m[32mb[m[m[m[m) (\[34ma[m:=[32mType[m [32mc[m:[32mType[m -> [47m<<HERE>>[m) | 8 | !focus checkMetas: \[34ma[m -> [32m(\[32mb[m:Type -> [32mprimFix [32m[32ma[m[m [32m[32mb[m[m[m)[m (\([34ma[m:=[32mType[m) -> \[32mc[m:[32mType[m -> [47m<<HERE>>[m) |
9 | [32m\([34ma[m : [32m[32m[32mType[m~[32m[32mV0[m[m[m[m) ([34mb[m : [32m[32m[32m[32mV1[m[m~[32m'X[m[m[m) -> [32m[32mtypeAnn [32m[32mV2[m[m ([32m[32mlabend [32mX[m[m[m)[m[m[m | 9 | [32m\([34ma[m : [32m[32m[32mType[m~[32m[32mV0[m[m[m[m) ([34mb[m : [32m[32m[32m[32mV1[m[m~[32m'X[m[m[m) -> [32m[32mtypeAnn [32m[32mV2[m[m [32m[32m(labend [32mX[m)[m[m[m[m[m |
10 | ------------ tooltips | 10 | ------------ tooltips |
11 | testdata/typesig.reject.lc 4:6-4:7 Type | 11 | testdata/typesig.reject.lc 4:6-4:7 Type |
12 | testdata/typesig.reject.lc 4:6-4:11 Type | 12 | testdata/typesig.reject.lc 4:6-4:11 Type |