diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 04:01:56 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-28 04:01:56 +0200 |
commit | 0e21fb5be982ed7e48be455f872fb862ef28b895 (patch) | |
tree | e8d99b8a50940c2d886d091fa83de1285b78257e | |
parent | 7e9105793bd0d5ff7197a5860ac5339dea677e0e (diff) |
unify Doc types; better expr. pretty print
-rw-r--r-- | lambdacube-compiler.cabal | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/CoreToIR.hs | 2 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/DesugaredSource.hs | 26 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 14 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 10 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Patterns.hs | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 182 | ||||
-rw-r--r-- | testdata/language-features/basic-values/case05.out | 2 | ||||
-rw-r--r-- | testdata/language-features/basic-values/def03.out | 2 | ||||
-rw-r--r-- | testdata/language-features/basic-values/def07.reject.out | 4 |
10 files changed, 133 insertions, 117 deletions
diff --git a/lambdacube-compiler.cabal b/lambdacube-compiler.cabal index 17c8fd56..a5bd8980 100644 --- a/lambdacube-compiler.cabal +++ b/lambdacube-compiler.cabal | |||
@@ -136,7 +136,7 @@ executable lambdacube-compiler-test-suite | |||
136 | monad-control >= 1.0 && <1.1, | 136 | monad-control >= 1.0 && <1.1, |
137 | optparse-applicative == 0.12.*, | 137 | optparse-applicative == 0.12.*, |
138 | megaparsec >= 4.3.0 && <4.5, | 138 | megaparsec >= 4.3.0 && <4.5, |
139 | wl-pprint >=1.2 && <1.3, | 139 | ansi-wl-pprint >=0.6 && <0.7, |
140 | patience >= 0.1 && < 0.2, | 140 | patience >= 0.1 && < 0.2, |
141 | text >= 1.2 && <1.3, | 141 | text >= 1.2 && <1.3, |
142 | time >= 1.5 && <1.6, | 142 | time >= 1.5 && <1.6, |
@@ -244,7 +244,7 @@ executable lambdacube-compiler-coverage-test-suite | |||
244 | monad-control >= 1.0 && <1.1, | 244 | monad-control >= 1.0 && <1.1, |
245 | optparse-applicative == 0.12.*, | 245 | optparse-applicative == 0.12.*, |
246 | megaparsec >= 4.3.0 && <4.5, | 246 | megaparsec >= 4.3.0 && <4.5, |
247 | wl-pprint >=1.2 && <1.3, | 247 | ansi-wl-pprint >=0.6 && <0.7, |
248 | pretty-show >= 1.6.9, | 248 | pretty-show >= 1.6.9, |
249 | patience >= 0.1 && < 0.2, | 249 | patience >= 0.1 && < 0.2, |
250 | text >= 1.2 && <1.3, | 250 | text >= 1.2 && <1.3, |
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs index 13aac101..d9d31d68 100644 --- a/src/LambdaCube/Compiler/CoreToIR.hs +++ b/src/LambdaCube/Compiler/CoreToIR.hs | |||
@@ -913,7 +913,7 @@ instance Up ExpTV where | |||
913 | closedExp (ExpTV a b cs) = ExpTV (closedExp a) (closedExp b) cs | 913 | closedExp (ExpTV a b cs) = ExpTV (closedExp a) (closedExp b) cs |
914 | 914 | ||
915 | instance PShow ExpTV where | 915 | instance PShow ExpTV where |
916 | pShowPrec p (ExpTV x t _) = pShowPrec p (x, t) | 916 | pShow (ExpTV x t _) = pShow (x, t) |
917 | 917 | ||
918 | isSampler (TyCon n _) = show n == "'Sampler" | 918 | isSampler (TyCon n _) = show n == "'Sampler" |
919 | isSampler _ = False | 919 | isSampler _ = False |
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs index 0511988a..e588403a 100644 --- a/src/LambdaCube/Compiler/DesugaredSource.hs +++ b/src/LambdaCube/Compiler/DesugaredSource.hs | |||
@@ -24,6 +24,7 @@ import qualified Data.Set as Set | |||
24 | import qualified Data.IntMap as IM | 24 | import qualified Data.IntMap as IM |
25 | import Control.Arrow hiding ((<+>)) | 25 | import Control.Arrow hiding ((<+>)) |
26 | import Control.DeepSeq | 26 | import Control.DeepSeq |
27 | import Debug.Trace | ||
27 | 28 | ||
28 | import LambdaCube.Compiler.Utils | 29 | import LambdaCube.Compiler.Utils |
29 | import LambdaCube.Compiler.DeBruijn | 30 | import LambdaCube.Compiler.DeBruijn |
@@ -64,7 +65,7 @@ data SPos = SPos | |||
64 | deriving (Eq, Ord) | 65 | deriving (Eq, Ord) |
65 | 66 | ||
66 | instance PShow SPos where | 67 | instance PShow SPos where |
67 | pShowPrec _ (SPos r c) = pShow r <> ":" <> pShow c | 68 | pShow (SPos r c) = pShow r <> ":" <> pShow c |
68 | 69 | ||
69 | -------------------------------------------------------------------------------- file info | 70 | -------------------------------------------------------------------------------- file info |
70 | 71 | ||
@@ -77,7 +78,7 @@ data FileInfo = FileInfo | |||
77 | instance Eq FileInfo where (==) = (==) `on` fileId | 78 | instance Eq FileInfo where (==) = (==) `on` fileId |
78 | instance Ord FileInfo where compare = compare `on` fileId | 79 | instance Ord FileInfo where compare = compare `on` fileId |
79 | 80 | ||
80 | instance PShow FileInfo where pShowPrec _ = text . filePath | 81 | instance PShow FileInfo where pShow = text . filePath |
81 | instance Show FileInfo where show = ppShow | 82 | instance Show FileInfo where show = ppShow |
82 | 83 | ||
83 | showPos :: FileInfo -> SPos -> Doc | 84 | showPos :: FileInfo -> SPos -> Doc |
@@ -92,7 +93,7 @@ instance NFData Range where | |||
92 | rnf Range{} = () | 93 | rnf Range{} = () |
93 | 94 | ||
94 | -- short version | 95 | -- short version |
95 | instance PShow Range where pShowPrec _ (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e | 96 | instance PShow Range where pShow (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e |
96 | instance Show Range where show = ppShow | 97 | instance Show Range where show = ppShow |
97 | 98 | ||
98 | -- long version | 99 | -- long version |
@@ -131,8 +132,8 @@ instance Monoid SI where | |||
131 | mappend _ r@RangeSI{} = r | 132 | mappend _ r@RangeSI{} = r |
132 | 133 | ||
133 | instance PShow SI where | 134 | instance PShow SI where |
134 | pShowPrec _ (NoSI ds) = hsep $ map text $ Set.toList ds | 135 | pShow (NoSI ds) = hsep $ map text $ Set.toList ds |
135 | pShowPrec _ (RangeSI r) = pShow r | 136 | pShow (RangeSI r) = pShow r |
136 | 137 | ||
137 | -- long version | 138 | -- long version |
138 | showSI x = case sourceInfo x of | 139 | showSI x = case sourceInfo x of |
@@ -155,13 +156,14 @@ pattern SIName si n <- SIName_ si _ n | |||
155 | instance Eq SIName where (==) = (==) `on` sName | 156 | instance Eq SIName where (==) = (==) `on` sName |
156 | instance Ord SIName where compare = compare `on` sName | 157 | instance Ord SIName where compare = compare `on` sName |
157 | instance Show SIName where show = sName | 158 | instance Show SIName where show = sName |
158 | instance PShow SIName where pShowPrec _ = text . sName | 159 | instance PShow SIName where pShow = text . sName |
159 | 160 | ||
160 | sName (SIName _ s) = s | 161 | sName (SIName _ s) = s |
161 | 162 | ||
162 | --appName f (SIName si n) = SIName si $ f n | 163 | --appName f (SIName si n) = SIName si $ f n |
163 | 164 | ||
164 | getFixity (SIName_ _ f _) = fromMaybe (Fixity InfixL 9) f | 165 | getFixity_ (SIName_ _ f _) = f |
166 | --getFixity (SIName_ _ f _) = fromMaybe (Fixity InfixL 9) f | ||
165 | 167 | ||
166 | ------------- | 168 | ------------- |
167 | 169 | ||
@@ -386,13 +388,14 @@ trSExp' :: SExp -> SExp' a | |||
386 | trSExp' = trSExp elimVoid | 388 | trSExp' = trSExp elimVoid |
387 | 389 | ||
388 | instance Up a => PShow (SExp' a) where | 390 | instance Up a => PShow (SExp' a) where |
389 | pShowPrec _ = showDoc_ . sExpDoc | 391 | pShow = sExpDoc |
390 | 392 | ||
391 | sExpDoc :: Up a => SExp' a -> NDoc | 393 | sExpDoc :: Up a => SExp' a -> NDoc |
392 | sExpDoc = \case | 394 | sExpDoc = \case |
393 | SGlobal ns -> shAtom $ sName ns | 395 | SGlobal ns -> shAtom $ sName ns |
394 | SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b) | 396 | SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b) |
395 | TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a) | 397 | TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a) |
398 | SGlobal op `SAppV` a `SAppV` b | Just p <- getFixity_ op -> DOp p (pShow a) (sName op) (pShow b) | ||
396 | SApp h a b -> shApp h (sExpDoc a) (sExpDoc b) | 399 | SApp h a b -> shApp h (sExpDoc a) (sExpDoc b) |
397 | Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t) | 400 | Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t) |
398 | SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b) | 401 | SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b) |
@@ -401,6 +404,9 @@ sExpDoc = \case | |||
401 | SVar _ i -> shVar i | 404 | SVar _ i -> shVar i |
402 | SLit _ l -> shAtom $ show l | 405 | SLit _ l -> shAtom $ show l |
403 | 406 | ||
407 | shApp Visible a b = DApp a b | ||
408 | shApp Hidden a b = DApp a (DOp (Fixity InfixR 20) "@" "" b) | ||
409 | |||
404 | shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b | 410 | shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b |
405 | where | 411 | where |
406 | lam = case h of | 412 | lam = case h of |
@@ -408,7 +414,7 @@ shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b | |||
408 | _ -> shLam' | 414 | _ -> shLam' |
409 | 415 | ||
410 | p = case h of | 416 | p = case h of |
411 | BMeta -> cpar . shAnn ":" True (inBlue' $ DVar 0) | 417 | BMeta -> shAnn ":" True (inBlue' $ DVar 0) |
412 | BLam h -> vpar h | 418 | BLam h -> vpar h |
413 | BPi h -> vpar h | 419 | BPi h -> vpar h |
414 | 420 | ||
@@ -429,7 +435,7 @@ data Stmt | |||
429 | pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") | 435 | pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") |
430 | 436 | ||
431 | instance PShow Stmt where | 437 | instance PShow Stmt where |
432 | pShowPrec p = \case | 438 | pShow = \case |
433 | Let n ty e -> text (sName n) </> "=" <+> maybe (pShow e) (\ty -> pShow e </> "::" <+> pShow ty) ty | 439 | Let n ty e -> text (sName n) </> "=" <+> maybe (pShow e) (\ty -> pShow e </> "::" <+> pShow ty) ty |
434 | Data n ps ty cs -> "data" <+> text (sName n) | 440 | Data n ps ty cs -> "data" <+> text (sName n) |
435 | PrecDef n i -> "precedence" <+> text (sName n) <+> text (show i) | 441 | PrecDef n i -> "precedence" <+> text (sName n) <+> text (show i) |
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 492e9a69..b44d67f2 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -1443,29 +1443,29 @@ joinEnv e1 e2 = do | |||
1443 | 1443 | ||
1444 | downTo n m = map Var [n+m-1, n+m-2..n] | 1444 | downTo n m = map Var [n+m-1, n+m-2..n] |
1445 | 1445 | ||
1446 | tellType si t = tell $ mkInfoItem (sourceInfo si) $ removeEscs $ showDoc $ mkDoc False True (t, TType) | 1446 | tellType si t = tell $ mkInfoItem (sourceInfo si) $ removeEscs $ show $ mkDoc False True (t, TType) |
1447 | 1447 | ||
1448 | 1448 | ||
1449 | -------------------------------------------------------------------------------- pretty print | 1449 | -------------------------------------------------------------------------------- pretty print |
1450 | -- todo: do this via conversion to SExp | 1450 | -- todo: do this via conversion to SExp |
1451 | 1451 | ||
1452 | instance PShow Exp where | 1452 | instance PShow Exp where |
1453 | pShowPrec _ = showDoc_ . mkDoc False False | 1453 | pShow = mkDoc False False |
1454 | 1454 | ||
1455 | instance PShow (CEnv Exp) where | 1455 | instance PShow (CEnv Exp) where |
1456 | pShowPrec _ = showDoc_ . mkDoc False False | 1456 | pShow = mkDoc False False |
1457 | 1457 | ||
1458 | instance PShow Env where | 1458 | instance PShow Env where |
1459 | pShowPrec _ e = showDoc_ $ envDoc e $ epar $ shAtom "<<HERE>>" | 1459 | pShow e = envDoc e $ epar $ shAtom "<<HERE>>" |
1460 | 1460 | ||
1461 | showEnvExp :: Env -> ExpType -> String | 1461 | showEnvExp :: Env -> ExpType -> String |
1462 | showEnvExp e c = showDoc $ envDoc e $ epar $ mkDoc False False c | 1462 | showEnvExp e c = show $ envDoc e $ epar $ mkDoc False False c |
1463 | 1463 | ||
1464 | showEnvSExp :: Up a => Env -> SExp' a -> String | 1464 | showEnvSExp :: Up a => Env -> SExp' a -> String |
1465 | showEnvSExp e c = showDoc $ envDoc e $ epar $ sExpDoc c | 1465 | showEnvSExp e c = show $ envDoc e $ epar $ sExpDoc c |
1466 | 1466 | ||
1467 | showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String | 1467 | showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String |
1468 | showEnvSExpType e c t = showDoc $ envDoc e $ epar $ (shAnn "::" False (sExpDoc c) (mkDoc False False (t, TType))) | 1468 | showEnvSExpType e c t = show $ envDoc e $ epar $ (shAnn "::" False (sExpDoc c) (mkDoc False False (t, TType))) |
1469 | {- | 1469 | {- |
1470 | where | 1470 | where |
1471 | infixl 4 <**> | 1471 | infixl 4 <**> |
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index 49079073..33a24d0a 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -71,7 +71,7 @@ instance Show LCParseError where | |||
71 | show = \case | 71 | show = \case |
72 | MultiplePatternVars xs -> unlines $ "multiple pattern vars:": | 72 | MultiplePatternVars xs -> unlines $ "multiple pattern vars:": |
73 | concat [(sName (head ns) ++ " is defined at"): map showSI ns | ns <- xs] | 73 | concat [(sName (head ns) ++ " is defined at"): map showSI ns | ns <- xs] |
74 | OperatorMismatch op op' -> "Operator precedences don't match:\n" ++ show (getFixity op) ++ " at " ++ showSI op ++ "\n" ++ show (getFixity op') ++ " at " ++ showSI op' | 74 | OperatorMismatch op op' -> "Operator precedences don't match:\n" ++ show (fromJust $ getFixity_ op) ++ " at " ++ showSI op ++ "\n" ++ show (fromJust $ getFixity_ op') ++ " at " ++ showSI op' |
75 | UndefinedConstructor n -> "Constructor " ++ show n ++ " is not defined at " ++ showSI n | 75 | UndefinedConstructor n -> "Constructor " ++ show n ++ " is not defined at " ++ showSI n |
76 | ParseError p -> show p | 76 | ParseError p -> show p |
77 | 77 | ||
@@ -105,7 +105,7 @@ instance Monoid DesugarInfo where | |||
105 | addFixity :: BodyParser SIName -> BodyParser SIName | 105 | addFixity :: BodyParser SIName -> BodyParser SIName |
106 | addFixity p = f <$> asks (fixityMap . desugarInfo) <*> p | 106 | addFixity p = f <$> asks (fixityMap . desugarInfo) <*> p |
107 | where | 107 | where |
108 | f fm sn@(SIName_ si _ n) = SIName_ si (Map.lookup n fm) n | 108 | f fm sn@(SIName_ si _ n) = SIName_ si (Just $ fromMaybe (Fixity InfixL 9) $ Map.lookup n fm) n |
109 | 109 | ||
110 | addConsInfo p = join $ f <$> asks (consMap . desugarInfo) <*> p | 110 | addConsInfo p = join $ f <$> asks (consMap . desugarInfo) <*> p |
111 | where | 111 | where |
@@ -278,7 +278,7 @@ calculatePrecs = go where | |||
278 | waitOp lsec e acc [] = calcPrec' e acc | 278 | waitOp lsec e acc [] = calcPrec' e acc |
279 | waitOp lsec e acc _ = error "impossible @ Parser 488" | 279 | waitOp lsec e acc _ = error "impossible @ Parser 488" |
280 | 280 | ||
281 | calcPrec' e = postponedCheck id . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) getFixity e . reverse | 281 | calcPrec' e = postponedCheck id . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (fromJust . getFixity_) e . reverse |
282 | 282 | ||
283 | generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp) | 283 | generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp) |
284 | generator = do | 284 | generator = do |
@@ -337,7 +337,7 @@ mkPVar s = PVarSimp s | |||
337 | 337 | ||
338 | concatParPats ps = ParPat $ concat [p | ParPat p <- ps] | 338 | concatParPats ps = ParPat $ concat [p | ParPat p <- ps] |
339 | 339 | ||
340 | litP = flip ViewPatSimp cTrue . SAppV (SBuiltin "==") | 340 | litP = flip ViewPatSimp cTrue . SAppV (SGlobal $ SIName_ mempty (Just $ Fixity Infix 4) "==") |
341 | 341 | ||
342 | patlist = commaSep $ setR parsePatAnn | 342 | patlist = commaSep $ setR parsePatAnn |
343 | 343 | ||
@@ -357,7 +357,7 @@ mkTup ps = foldr cHCons cHNil ps | |||
357 | patType p (Wildcard SType) = p | 357 | patType p (Wildcard SType) = p |
358 | patType p t = PatTypeSimp p t | 358 | patType p t = PatTypeSimp p t |
359 | 359 | ||
360 | calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (getFixity . fst) e xs | 360 | calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (fromJust . getFixity_ . fst) e xs |
361 | 361 | ||
362 | longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id) | 362 | longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id) |
363 | 363 | ||
diff --git a/src/LambdaCube/Compiler/Patterns.hs b/src/LambdaCube/Compiler/Patterns.hs index 00626d2e..06036f44 100644 --- a/src/LambdaCube/Compiler/Patterns.hs +++ b/src/LambdaCube/Compiler/Patterns.hs | |||
@@ -54,9 +54,9 @@ pattern ParPat ps <- ParPat_ _ ps | |||
54 | where ParPat ps = ParPat_ (foldMap sourceInfo ps) ps | 54 | where ParPat ps = ParPat_ (foldMap sourceInfo ps) ps |
55 | 55 | ||
56 | instance PShow (Pat_ a) where | 56 | instance PShow (Pat_ a) where |
57 | pShowPrec _ = showDoc_ . patDoc | 57 | pShow = patDoc |
58 | instance PShow (ParPat_ a) where | 58 | instance PShow (ParPat_ a) where |
59 | pShowPrec _ = showDoc_ . parPatDoc | 59 | pShow = parPatDoc |
60 | 60 | ||
61 | 61 | ||
62 | pattern PWildcard si = ParPat_ si [] | 62 | pattern PWildcard si = ParPat_ si [] |
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs index e7c1216f..e94f6e41 100644 --- a/src/LambdaCube/Compiler/Pretty.hs +++ b/src/LambdaCube/Compiler/Pretty.hs | |||
@@ -4,98 +4,74 @@ | |||
4 | {-# LANGUAGE PatternSynonyms #-} | 4 | {-# LANGUAGE PatternSynonyms #-} |
5 | {-# LANGUAGE ViewPatterns #-} | 5 | {-# LANGUAGE ViewPatterns #-} |
6 | {-# LANGUAGE FlexibleContexts #-} | 6 | {-# LANGUAGE FlexibleContexts #-} |
7 | {-# LANGUAGE DeriveFunctor #-} | ||
8 | {-# LANGUAGE DeriveTraversable #-} | ||
9 | {-# LANGUAGE DeriveFoldable #-} | ||
7 | module LambdaCube.Compiler.Pretty | 10 | module LambdaCube.Compiler.Pretty |
8 | ( module LambdaCube.Compiler.Pretty | 11 | ( module LambdaCube.Compiler.Pretty |
9 | , Doc | ||
10 | , (<+>), (</>), (<$$>) | ||
11 | , hsep, hcat, vcat | ||
12 | -- , punctuate | ||
13 | , tupled, braces, parens | ||
14 | , text | ||
15 | , nest | ||
16 | ) where | 12 | ) where |
17 | 13 | ||
18 | import Data.Set (Set) | 14 | import Data.Monoid |
19 | import qualified Data.Set as Set | 15 | import Data.String |
20 | import Data.Map (Map) | 16 | --import qualified Data.Set as Set |
21 | import qualified Data.Map as Map | 17 | --import qualified Data.Map as Map |
22 | import Control.Monad.Except | ||
23 | import Control.Monad.Reader | 18 | import Control.Monad.Reader |
24 | import Control.Monad.State | 19 | import Control.Monad.State |
25 | import Control.Arrow hiding ((<+>)) | 20 | import Control.Arrow hiding ((<+>)) |
26 | import Debug.Trace | 21 | --import Debug.Trace |
27 | 22 | ||
28 | import Text.PrettyPrint.ANSI.Leijen hiding ((<$>)) | 23 | import qualified Text.PrettyPrint.ANSI.Leijen as P |
29 | 24 | ||
30 | import LambdaCube.Compiler.Utils | 25 | import LambdaCube.Compiler.Utils |
31 | 26 | ||
27 | type Doc = NDoc | ||
28 | hsep [] = mempty | ||
29 | hsep xs = foldr1 (<+>) xs | ||
30 | vcat [] = mempty | ||
31 | vcat xs = foldr1 (<$$>) xs | ||
32 | text = DAtom | ||
33 | |||
32 | -------------------------------------------------------------------------------- | 34 | -------------------------------------------------------------------------------- |
33 | 35 | ||
34 | class PShow a where | 36 | class PShow a where |
35 | pShowPrec :: Int -> a -> Doc | 37 | pShow :: a -> NDoc |
36 | 38 | ||
37 | pShow = pShowPrec (-2) | ||
38 | ppShow = show . pShow | 39 | ppShow = show . pShow |
39 | 40 | ||
40 | ppShow' = show | ||
41 | |||
42 | -------------------------------------------------------------------------------- | ||
43 | |||
44 | pParens p x | ||
45 | | p = tupled [x] | ||
46 | | otherwise = x | ||
47 | |||
48 | pOp i j k sep p a b = pParens (p >= i) $ pShowPrec j a <+> sep <+> pShowPrec k b | ||
49 | pOp' i j k sep p a b = pParens (p >= i) $ pShowPrec j a </> sep <+> pShowPrec k b | ||
50 | |||
51 | pInfixl i = pOp i (i-1) i | ||
52 | pInfixr i = pOp i i (i-1) | ||
53 | pInfixr' i = pOp' i i (i-1) | ||
54 | pInfix i = pOp i i i | ||
55 | |||
56 | pTyApp = pInfixl 10 "@" | ||
57 | pApps p x [] = pShowPrec p x | ||
58 | pApps p x xs = pParens (p > 9) $ hsep $ pShowPrec 9 x: map (pShowPrec 10) xs | ||
59 | pApp p a b = pApps p a [b] | ||
60 | |||
61 | showRecord = braces . hsep . punctuate (pShow ',') . map (\(a, b) -> pShow a <> ":" <+> pShow b) | ||
62 | |||
63 | -------------------------------------------------------------------------------- | 41 | -------------------------------------------------------------------------------- |
64 | 42 | ||
65 | instance PShow Bool where | 43 | instance PShow Bool where |
66 | pShowPrec p b = if b then "True" else "False" | 44 | pShow b = if b then "True" else "False" |
67 | 45 | ||
68 | instance (PShow a, PShow b) => PShow (a, b) where | 46 | instance (PShow a, PShow b) => PShow (a, b) where |
69 | pShowPrec p (a, b) = tupled [pShow a, pShow b] | 47 | pShow (a, b) = tupled [pShow a, pShow b] |
70 | 48 | ||
71 | instance (PShow a, PShow b, PShow c) => PShow (a, b, c) where | 49 | instance (PShow a, PShow b, PShow c) => PShow (a, b, c) where |
72 | pShowPrec p (a, b, c) = tupled [pShow a, pShow b, pShow c] | 50 | pShow (a, b, c) = tupled [pShow a, pShow b, pShow c] |
73 | 51 | ||
74 | instance PShow a => PShow [a] where | 52 | instance PShow a => PShow [a] where |
75 | pShowPrec p = brackets . sep . punctuate comma . map pShow | 53 | -- pShow = P.brackets . P.sep . P.punctuate P.comma . map pShow -- TODO |
76 | 54 | ||
77 | instance PShow a => PShow (Maybe a) where | 55 | instance PShow a => PShow (Maybe a) where |
78 | pShowPrec p = \case | 56 | pShow = maybe "Nothing" (("Just" `DApp`) . pShow) |
79 | Nothing -> "Nothing" | ||
80 | Just x -> "Just" <+> pShow x | ||
81 | 57 | ||
82 | instance PShow a => PShow (Set a) where | 58 | --instance PShow a => PShow (Set a) where |
83 | pShowPrec p = pShowPrec p . Set.toList | 59 | -- pShow = pShow . Set.toList |
84 | 60 | ||
85 | instance (PShow s, PShow a) => PShow (Map s a) where | 61 | --instance (PShow s, PShow a) => PShow (Map s a) where |
86 | pShowPrec p = braces . vcat . map (\(k, t) -> pShow k <> colon <+> pShow t) . Map.toList | 62 | -- pShow = braces . vcat . map (\(k, t) -> pShow k <> P.colon <+> pShow t) . Map.toList |
87 | 63 | ||
88 | instance (PShow a, PShow b) => PShow (Either a b) where | 64 | instance (PShow a, PShow b) => PShow (Either a b) where |
89 | pShowPrec p = either (("Left" <+>) . pShow) (("Right" <+>) . pShow) | 65 | pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow) |
90 | 66 | ||
91 | instance PShow Doc where | 67 | instance PShow NDoc where |
92 | pShowPrec p x = x | 68 | pShow x = x |
93 | 69 | ||
94 | instance PShow Int where pShowPrec _ = int | 70 | instance PShow Int where pShow = fromString . show |
95 | instance PShow Integer where pShowPrec _ = integer | 71 | instance PShow Integer where pShow = fromString . show |
96 | instance PShow Double where pShowPrec _ = double | 72 | instance PShow Double where pShow = fromString . show |
97 | instance PShow Char where pShowPrec _ = char | 73 | instance PShow Char where pShow = fromString . show |
98 | instance PShow () where pShowPrec _ _ = "()" | 74 | instance PShow () where pShow _ = "()" |
99 | 75 | ||
100 | --------------------------------------------------------------------------------- | 76 | --------------------------------------------------------------------------------- |
101 | -- TODO: remove | 77 | -- TODO: remove |
@@ -118,7 +94,7 @@ data FixityDir = Infix | InfixL | InfixR | |||
118 | data Fixity = Fixity !FixityDir !Int | 94 | data Fixity = Fixity !FixityDir !Int |
119 | deriving (Eq, Show) | 95 | deriving (Eq, Show) |
120 | 96 | ||
121 | -------------------------------------------------------------------------------- pretty print | 97 | -------------------------------------------------------------------------------- doc data type |
122 | 98 | ||
123 | data NDoc | 99 | data NDoc |
124 | = DAtom String | 100 | = DAtom String |
@@ -128,15 +104,40 @@ data NDoc | |||
128 | | DVar Int | 104 | | DVar Int |
129 | | DFreshName Bool{-False: dummy-} NDoc | 105 | | DFreshName Bool{-False: dummy-} NDoc |
130 | | DUp Int NDoc | 106 | | DUp Int NDoc |
131 | | DColor Color NDoc | 107 | | DDoc (DocOp NDoc) --Color Color NDoc |
132 | -- add wl-pprint combinators as necessary here | 108 | -- add wl-pprint combinators as necessary here |
133 | deriving (Eq) | 109 | deriving (Eq) |
134 | 110 | ||
111 | data DocOp a | ||
112 | = DOColor Color a | ||
113 | | DOHSep a a | ||
114 | | DOHCat a a | ||
115 | | DOSoftSep a a | ||
116 | | DOVCat a a | ||
117 | | DONest Int a | ||
118 | | DOTupled [a] | ||
119 | deriving (Eq, Functor, Foldable, Traversable) | ||
120 | |||
121 | pattern DColor c a = DDoc (DOColor c a) | ||
122 | |||
123 | a <+> b = DDoc $ DOHSep a b | ||
124 | a </> b = DDoc $ DOSoftSep a b | ||
125 | a <$$> b = DDoc $ DOVCat a b | ||
126 | nest n = DDoc . DONest n | ||
127 | tupled = DDoc . DOTupled | ||
128 | |||
129 | instance Monoid NDoc where | ||
130 | mempty = fromString "" | ||
131 | a `mappend` b = DDoc $ DOHCat a b | ||
132 | |||
135 | pattern DParen x = DPar "(" x ")" | 133 | pattern DParen x = DPar "(" x ")" |
136 | pattern DBrace x = DPar "{" x "}" | 134 | pattern DBrace x = DPar "{" x "}" |
137 | pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y | 135 | pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y |
138 | pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y | 136 | pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y |
139 | 137 | ||
138 | braces = DBrace | ||
139 | parens = DParen | ||
140 | |||
140 | data Color = Green | Blue | Underlined | 141 | data Color = Green | Blue | Underlined |
141 | deriving (Eq) | 142 | deriving (Eq) |
142 | 143 | ||
@@ -148,7 +149,7 @@ strip = \case | |||
148 | DColor _ x -> strip x | 149 | DColor _ x -> strip x |
149 | DUp _ x -> strip x | 150 | DUp _ x -> strip x |
150 | DFreshName _ x -> strip x | 151 | DFreshName _ x -> strip x |
151 | x -> x | 152 | x -> x |
152 | 153 | ||
153 | simple x = case strip x of | 154 | simple x = case strip x of |
154 | DAtom{} -> True | 155 | DAtom{} -> True |
@@ -156,12 +157,12 @@ simple x = case strip x of | |||
156 | DPar{} -> True | 157 | DPar{} -> True |
157 | _ -> False | 158 | _ -> False |
158 | 159 | ||
159 | renderDocX :: NDoc -> Doc | 160 | renderDocX :: NDoc -> P.Doc |
160 | renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) . showVars | 161 | renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) . showVars |
161 | where | 162 | where |
162 | showVars x = case x of | 163 | showVars x = case x of |
163 | DAtom s -> pure x | 164 | DAtom s -> pure x |
164 | DColor c x -> DColor c <$> showVars x | 165 | DDoc d -> DDoc <$> traverse showVars d |
165 | DPar l x r -> DPar l <$> showVars x <*> pure r | 166 | DPar l x r -> DPar l <$> showVars x <*> pure r |
166 | DOp pr x s y -> DOp pr <$> showVars x <*> pure s <*> showVars y | 167 | DOp pr x s y -> DOp pr <$> showVars x <*> pure s <*> showVars y |
167 | DVar i -> asks $ DAtom . lookupVarName i | 168 | DVar i -> asks $ DAtom . lookupVarName i |
@@ -180,6 +181,7 @@ renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip ( | |||
180 | DPar l x r -> DPar l (addPar (-20) x) r | 181 | DPar l x r -> DPar l (addPar (-20) x) r |
181 | DOp pr' x s y -> paren $ DOp pr' (addPar (precL pr') x) s (addPar (precR pr') y) | 182 | DOp pr' x s y -> paren $ DOp pr' (addPar (precL pr') x) s (addPar (precR pr') y) |
182 | DLam lam vs arr e -> paren $ DLam lam (addPar 10 <$> vs) arr (addPar (-10) e) | 183 | DLam lam vs arr e -> paren $ DLam lam (addPar 10 <$> vs) arr (addPar (-10) e) |
184 | DDoc d -> DDoc $ addPar (-10) <$> d | ||
183 | where | 185 | where |
184 | paren d | 186 | paren d |
185 | | protect x = DParen d | 187 | | protect x = DParen d |
@@ -199,46 +201,57 @@ renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip ( | |||
199 | precR (Fixity InfixR i) = i | 201 | precR (Fixity InfixR i) = i |
200 | 202 | ||
201 | render x = case x of | 203 | render x = case x of |
202 | DColor c x -> colorFun c $ render x | 204 | DDoc d -> case render <$> d of |
203 | DAtom s -> text s | 205 | DOColor c x -> colorFun c x |
204 | DPar l x r -> text l <> render x <> text r | 206 | DOHSep a b -> a P.<+> b |
207 | DOHCat a b -> a <> b | ||
208 | DOSoftSep a b -> a P.</> b | ||
209 | DOVCat a b -> a P.<$$> b | ||
210 | DONest n a -> P.nest n a | ||
211 | DOTupled a -> P.tupled a | ||
212 | DAtom s -> P.text s | ||
213 | DPar l x r -> P.text l <> render x <> P.text r | ||
205 | DOp _ x s y -> case s of | 214 | DOp _ x s y -> case s of |
206 | "" -> render x <+> render y | 215 | "" -> render x P.<> render y |
207 | _ | simple x && simple y && s /= "," -> render x <> text s <> render y | 216 | " " -> render x P.<+> render y |
208 | | otherwise -> (render x <++> s) <+> render y | 217 | _ | simple x && simple y && s /= "," -> render x <> P.text s <> render y |
209 | DLam lam vs arr e -> text lam <> hsep (render <$> vs) <+> text arr <+> render e | 218 | | otherwise -> (render x <++> s) P.<+> render y |
219 | DLam lam vs arr e -> P.text lam <> P.hsep (render <$> vs) P.<+> P.text arr P.<+> render e | ||
210 | where | 220 | where |
211 | x <++> "," = x <> text "," | 221 | x <++> "," = x <> P.text "," |
212 | x <++> s = x <+> text s | 222 | x <++> s = x P.<+> P.text s |
213 | 223 | ||
214 | colorFun = \case | 224 | colorFun = \case |
215 | Green -> dullgreen | 225 | Green -> P.dullgreen |
216 | Blue -> dullblue | 226 | Blue -> P.dullblue |
217 | Underlined -> underline | 227 | Underlined -> P.underline |
218 | 228 | ||
219 | showDoc :: NDoc -> String | 229 | instance Show NDoc where |
220 | showDoc = show . renderDocX | 230 | show = show . renderDocX |
221 | 231 | ||
222 | showDoc_ :: NDoc -> Doc | 232 | showDoc_ :: NDoc -> P.Doc |
223 | showDoc_ = renderDocX | 233 | showDoc_ = renderDocX |
224 | 234 | ||
225 | shVar = DVar | 235 | shVar = DVar |
226 | 236 | ||
227 | shLet i a b = shLam' (cpar . shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b) | 237 | shLet i a b = shLam' (shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b) |
228 | shLet_ a b = DFreshName True $ shLam' (cpar . shLet' (shVar 0) $ DUp 0 a) b | 238 | shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b |
229 | 239 | ||
230 | ----------------------------------------- | 240 | ----------------------------------------- |
231 | 241 | ||
242 | instance IsString NDoc where | ||
243 | fromString = DAtom | ||
244 | |||
232 | shAtom = DAtom | 245 | shAtom = DAtom |
233 | 246 | ||
234 | shTuple [] = DAtom "()" | 247 | shTuple [] = "()" |
235 | shTuple [x] = DParen $ DParen x | 248 | shTuple [x] = DParen $ DParen x |
236 | shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs | 249 | shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs |
237 | 250 | ||
238 | shAnn _ True x y | strip y == DAtom "Type" = x | 251 | shAnn _ True x y | strip y == "Type" = x |
239 | shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y | 252 | shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y |
240 | 253 | ||
241 | shApp _ x y = DOp (Fixity InfixL 10) x "" y | 254 | pattern DApp x y = DOp (Fixity InfixL 10) x " " y |
242 | 255 | ||
243 | shArr = DArr | 256 | shArr = DArr |
244 | 257 | ||
@@ -252,7 +265,4 @@ getFN a = (0, a) | |||
252 | shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y | 265 | shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y |
253 | shLam' x y = DLam "\\" [x] "->" y | 266 | shLam' x y = DLam "\\" [x] "->" y |
254 | 267 | ||
255 | cpar s | simple s = s | ||
256 | cpar s = DParen s | ||
257 | |||
258 | 268 | ||
diff --git a/testdata/language-features/basic-values/case05.out b/testdata/language-features/basic-values/case05.out index 85e6bced..f4f03c51 100644 --- a/testdata/language-features/basic-values/case05.out +++ b/testdata/language-features/basic-values/case05.out | |||
@@ -11,5 +11,5 @@ Uncovered pattern(s) at testdata/language-features/basic-values/case05.lc:1:16: | |||
11 | value x = case x of | 11 | value x = case x of |
12 | ^ | 12 | ^ |
13 | Missing case(s): | 13 | Missing case(s): |
14 | _ | False <- == (fromInt 1) a_ | 14 | _ | False <- fromInt 1 == a_ |
15 | 15 | ||
diff --git a/testdata/language-features/basic-values/def03.out b/testdata/language-features/basic-values/def03.out index ba33b297..3161d04a 100644 --- a/testdata/language-features/basic-values/def03.out +++ b/testdata/language-features/basic-values/def03.out | |||
@@ -11,5 +11,5 @@ Uncovered pattern(s) at testdata/language-features/basic-values/def03.lc:1:1: | |||
11 | fun 1 = '1' | 11 | fun 1 = '1' |
12 | fun 2 = '2' | 12 | fun 2 = '2' |
13 | Missing case(s): | 13 | Missing case(s): |
14 | _ | False <- == (fromInt 1) a_, False <- == (fromInt 2) a_ | 14 | _ | False <- fromInt 1 == a_, False <- fromInt 2 == a_ |
15 | 15 | ||
diff --git a/testdata/language-features/basic-values/def07.reject.out b/testdata/language-features/basic-values/def07.reject.out index 0ce641f7..e54d61ca 100644 --- a/testdata/language-features/basic-values/def07.reject.out +++ b/testdata/language-features/basic-values/def07.reject.out | |||
@@ -28,11 +28,11 @@ Uncovered pattern(s) at testdata/language-features/basic-values/def07.reject.lc: | |||
28 | fun 1 = '1' | 28 | fun 1 = '1' |
29 | fun 2 = '2' | 29 | fun 2 = '2' |
30 | Missing case(s): | 30 | Missing case(s): |
31 | _ | False <- == (fromInt 1) a_, False <- == (fromInt 2) a_ | 31 | _ | False <- fromInt 1 == a_, False <- fromInt 2 == a_ |
32 | 32 | ||
33 | Uncovered pattern(s) at testdata/language-features/basic-values/def07.reject.lc:3:1: | 33 | Uncovered pattern(s) at testdata/language-features/basic-values/def07.reject.lc:3:1: |
34 | fun2 1 _ = '1' | 34 | fun2 1 _ = '1' |
35 | ^^^^ | 35 | ^^^^ |
36 | Missing case(s): | 36 | Missing case(s): |
37 | _ _ | False <- == (fromInt 1) b_ | 37 | _ _ | False <- fromInt 1 == b_ |
38 | 38 | ||