summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-28 04:01:56 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-28 04:01:56 +0200
commit0e21fb5be982ed7e48be455f872fb862ef28b895 (patch)
treee8d99b8a50940c2d886d091fa83de1285b78257e
parent7e9105793bd0d5ff7197a5860ac5339dea677e0e (diff)
unify Doc types; better expr. pretty print
-rw-r--r--lambdacube-compiler.cabal4
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs2
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs26
-rw-r--r--src/LambdaCube/Compiler/Infer.hs14
-rw-r--r--src/LambdaCube/Compiler/Parser.hs10
-rw-r--r--src/LambdaCube/Compiler/Patterns.hs4
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs182
-rw-r--r--testdata/language-features/basic-values/case05.out2
-rw-r--r--testdata/language-features/basic-values/def03.out2
-rw-r--r--testdata/language-features/basic-values/def07.reject.out4
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
915instance PShow ExpTV where 915instance PShow ExpTV where
916 pShowPrec p (ExpTV x t _) = pShowPrec p (x, t) 916 pShow (ExpTV x t _) = pShow (x, t)
917 917
918isSampler (TyCon n _) = show n == "'Sampler" 918isSampler (TyCon n _) = show n == "'Sampler"
919isSampler _ = False 919isSampler _ = 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
24import qualified Data.IntMap as IM 24import qualified Data.IntMap as IM
25import Control.Arrow hiding ((<+>)) 25import Control.Arrow hiding ((<+>))
26import Control.DeepSeq 26import Control.DeepSeq
27import Debug.Trace
27 28
28import LambdaCube.Compiler.Utils 29import LambdaCube.Compiler.Utils
29import LambdaCube.Compiler.DeBruijn 30import LambdaCube.Compiler.DeBruijn
@@ -64,7 +65,7 @@ data SPos = SPos
64 deriving (Eq, Ord) 65 deriving (Eq, Ord)
65 66
66instance PShow SPos where 67instance 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
77instance Eq FileInfo where (==) = (==) `on` fileId 78instance Eq FileInfo where (==) = (==) `on` fileId
78instance Ord FileInfo where compare = compare `on` fileId 79instance Ord FileInfo where compare = compare `on` fileId
79 80
80instance PShow FileInfo where pShowPrec _ = text . filePath 81instance PShow FileInfo where pShow = text . filePath
81instance Show FileInfo where show = ppShow 82instance Show FileInfo where show = ppShow
82 83
83showPos :: FileInfo -> SPos -> Doc 84showPos :: 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
95instance PShow Range where pShowPrec _ (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e 96instance PShow Range where pShow (Range n b e) = pShow n <+> pShow b <> "-" <> pShow e
96instance Show Range where show = ppShow 97instance 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
133instance PShow SI where 134instance 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
138showSI x = case sourceInfo x of 139showSI x = case sourceInfo x of
@@ -155,13 +156,14 @@ pattern SIName si n <- SIName_ si _ n
155instance Eq SIName where (==) = (==) `on` sName 156instance Eq SIName where (==) = (==) `on` sName
156instance Ord SIName where compare = compare `on` sName 157instance Ord SIName where compare = compare `on` sName
157instance Show SIName where show = sName 158instance Show SIName where show = sName
158instance PShow SIName where pShowPrec _ = text . sName 159instance PShow SIName where pShow = text . sName
159 160
160sName (SIName _ s) = s 161sName (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
164getFixity (SIName_ _ f _) = fromMaybe (Fixity InfixL 9) f 165getFixity_ (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
386trSExp' = trSExp elimVoid 388trSExp' = trSExp elimVoid
387 389
388instance Up a => PShow (SExp' a) where 390instance Up a => PShow (SExp' a) where
389 pShowPrec _ = showDoc_ . sExpDoc 391 pShow = sExpDoc
390 392
391sExpDoc :: Up a => SExp' a -> NDoc 393sExpDoc :: Up a => SExp' a -> NDoc
392sExpDoc = \case 394sExpDoc = \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
407shApp Visible a b = DApp a b
408shApp Hidden a b = DApp a (DOp (Fixity InfixR 20) "@" "" b)
409
404shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b 410shLam 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
429pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") 435pattern Primitive n t = Let n (Just t) (SBuiltin "undefined")
430 436
431instance PShow Stmt where 437instance 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
1444downTo n m = map Var [n+m-1, n+m-2..n] 1444downTo n m = map Var [n+m-1, n+m-2..n]
1445 1445
1446tellType si t = tell $ mkInfoItem (sourceInfo si) $ removeEscs $ showDoc $ mkDoc False True (t, TType) 1446tellType 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
1452instance PShow Exp where 1452instance PShow Exp where
1453 pShowPrec _ = showDoc_ . mkDoc False False 1453 pShow = mkDoc False False
1454 1454
1455instance PShow (CEnv Exp) where 1455instance PShow (CEnv Exp) where
1456 pShowPrec _ = showDoc_ . mkDoc False False 1456 pShow = mkDoc False False
1457 1457
1458instance PShow Env where 1458instance PShow Env where
1459 pShowPrec _ e = showDoc_ $ envDoc e $ epar $ shAtom "<<HERE>>" 1459 pShow e = envDoc e $ epar $ shAtom "<<HERE>>"
1460 1460
1461showEnvExp :: Env -> ExpType -> String 1461showEnvExp :: Env -> ExpType -> String
1462showEnvExp e c = showDoc $ envDoc e $ epar $ mkDoc False False c 1462showEnvExp e c = show $ envDoc e $ epar $ mkDoc False False c
1463 1463
1464showEnvSExp :: Up a => Env -> SExp' a -> String 1464showEnvSExp :: Up a => Env -> SExp' a -> String
1465showEnvSExp e c = showDoc $ envDoc e $ epar $ sExpDoc c 1465showEnvSExp e c = show $ envDoc e $ epar $ sExpDoc c
1466 1466
1467showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String 1467showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String
1468showEnvSExpType e c t = showDoc $ envDoc e $ epar $ (shAnn "::" False (sExpDoc c) (mkDoc False False (t, TType))) 1468showEnvSExpType 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
105addFixity :: BodyParser SIName -> BodyParser SIName 105addFixity :: BodyParser SIName -> BodyParser SIName
106addFixity p = f <$> asks (fixityMap . desugarInfo) <*> p 106addFixity 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
110addConsInfo p = join $ f <$> asks (consMap . desugarInfo) <*> p 110addConsInfo 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
283generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp) 283generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp)
284generator = do 284generator = do
@@ -337,7 +337,7 @@ mkPVar s = PVarSimp s
337 337
338concatParPats ps = ParPat $ concat [p | ParPat p <- ps] 338concatParPats ps = ParPat $ concat [p | ParPat p <- ps]
339 339
340litP = flip ViewPatSimp cTrue . SAppV (SBuiltin "==") 340litP = flip ViewPatSimp cTrue . SAppV (SGlobal $ SIName_ mempty (Just $ Fixity Infix 4) "==")
341 341
342patlist = commaSep $ setR parsePatAnn 342patlist = commaSep $ setR parsePatAnn
343 343
@@ -357,7 +357,7 @@ mkTup ps = foldr cHCons cHNil ps
357patType p (Wildcard SType) = p 357patType p (Wildcard SType) = p
358patType p t = PatTypeSimp p t 358patType p t = PatTypeSimp p t
359 359
360calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (getFixity . fst) e xs 360calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (fromJust . getFixity_ . fst) e xs
361 361
362longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id) 362longPattern = 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
56instance PShow (Pat_ a) where 56instance PShow (Pat_ a) where
57 pShowPrec _ = showDoc_ . patDoc 57 pShow = patDoc
58instance PShow (ParPat_ a) where 58instance PShow (ParPat_ a) where
59 pShowPrec _ = showDoc_ . parPatDoc 59 pShow = parPatDoc
60 60
61 61
62pattern PWildcard si = ParPat_ si [] 62pattern 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 #-}
7module LambdaCube.Compiler.Pretty 10module 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
18import Data.Set (Set) 14import Data.Monoid
19import qualified Data.Set as Set 15import Data.String
20import Data.Map (Map) 16--import qualified Data.Set as Set
21import qualified Data.Map as Map 17--import qualified Data.Map as Map
22import Control.Monad.Except
23import Control.Monad.Reader 18import Control.Monad.Reader
24import Control.Monad.State 19import Control.Monad.State
25import Control.Arrow hiding ((<+>)) 20import Control.Arrow hiding ((<+>))
26import Debug.Trace 21--import Debug.Trace
27 22
28import Text.PrettyPrint.ANSI.Leijen hiding ((<$>)) 23import qualified Text.PrettyPrint.ANSI.Leijen as P
29 24
30import LambdaCube.Compiler.Utils 25import LambdaCube.Compiler.Utils
31 26
27type Doc = NDoc
28hsep [] = mempty
29hsep xs = foldr1 (<+>) xs
30vcat [] = mempty
31vcat xs = foldr1 (<$$>) xs
32text = DAtom
33
32-------------------------------------------------------------------------------- 34--------------------------------------------------------------------------------
33 35
34class PShow a where 36class PShow a where
35 pShowPrec :: Int -> a -> Doc 37 pShow :: a -> NDoc
36 38
37pShow = pShowPrec (-2)
38ppShow = show . pShow 39ppShow = show . pShow
39 40
40ppShow' = show
41
42--------------------------------------------------------------------------------
43
44pParens p x
45 | p = tupled [x]
46 | otherwise = x
47
48pOp i j k sep p a b = pParens (p >= i) $ pShowPrec j a <+> sep <+> pShowPrec k b
49pOp' i j k sep p a b = pParens (p >= i) $ pShowPrec j a </> sep <+> pShowPrec k b
50
51pInfixl i = pOp i (i-1) i
52pInfixr i = pOp i i (i-1)
53pInfixr' i = pOp' i i (i-1)
54pInfix i = pOp i i i
55
56pTyApp = pInfixl 10 "@"
57pApps p x [] = pShowPrec p x
58pApps p x xs = pParens (p > 9) $ hsep $ pShowPrec 9 x: map (pShowPrec 10) xs
59pApp p a b = pApps p a [b]
60
61showRecord = braces . hsep . punctuate (pShow ',') . map (\(a, b) -> pShow a <> ":" <+> pShow b)
62
63-------------------------------------------------------------------------------- 41--------------------------------------------------------------------------------
64 42
65instance PShow Bool where 43instance PShow Bool where
66 pShowPrec p b = if b then "True" else "False" 44 pShow b = if b then "True" else "False"
67 45
68instance (PShow a, PShow b) => PShow (a, b) where 46instance (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
71instance (PShow a, PShow b, PShow c) => PShow (a, b, c) where 49instance (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
74instance PShow a => PShow [a] where 52instance 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
77instance PShow a => PShow (Maybe a) where 55instance 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
82instance 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
85instance (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
88instance (PShow a, PShow b) => PShow (Either a b) where 64instance (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
91instance PShow Doc where 67instance PShow NDoc where
92 pShowPrec p x = x 68 pShow x = x
93 69
94instance PShow Int where pShowPrec _ = int 70instance PShow Int where pShow = fromString . show
95instance PShow Integer where pShowPrec _ = integer 71instance PShow Integer where pShow = fromString . show
96instance PShow Double where pShowPrec _ = double 72instance PShow Double where pShow = fromString . show
97instance PShow Char where pShowPrec _ = char 73instance PShow Char where pShow = fromString . show
98instance PShow () where pShowPrec _ _ = "()" 74instance PShow () where pShow _ = "()"
99 75
100--------------------------------------------------------------------------------- 76---------------------------------------------------------------------------------
101-- TODO: remove 77-- TODO: remove
@@ -118,7 +94,7 @@ data FixityDir = Infix | InfixL | InfixR
118data Fixity = Fixity !FixityDir !Int 94data Fixity = Fixity !FixityDir !Int
119 deriving (Eq, Show) 95 deriving (Eq, Show)
120 96
121-------------------------------------------------------------------------------- pretty print 97-------------------------------------------------------------------------------- doc data type
122 98
123data NDoc 99data 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
111data 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
121pattern DColor c a = DDoc (DOColor c a)
122
123a <+> b = DDoc $ DOHSep a b
124a </> b = DDoc $ DOSoftSep a b
125a <$$> b = DDoc $ DOVCat a b
126nest n = DDoc . DONest n
127tupled = DDoc . DOTupled
128
129instance Monoid NDoc where
130 mempty = fromString ""
131 a `mappend` b = DDoc $ DOHCat a b
132
135pattern DParen x = DPar "(" x ")" 133pattern DParen x = DPar "(" x ")"
136pattern DBrace x = DPar "{" x "}" 134pattern DBrace x = DPar "{" x "}"
137pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y 135pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y
138pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y 136pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y
139 137
138braces = DBrace
139parens = DParen
140
140data Color = Green | Blue | Underlined 141data 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
153simple x = case strip x of 154simple 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
159renderDocX :: NDoc -> Doc 160renderDocX :: NDoc -> P.Doc
160renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) . showVars 161renderDocX = 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
219showDoc :: NDoc -> String 229instance Show NDoc where
220showDoc = show . renderDocX 230 show = show . renderDocX
221 231
222showDoc_ :: NDoc -> Doc 232showDoc_ :: NDoc -> P.Doc
223showDoc_ = renderDocX 233showDoc_ = renderDocX
224 234
225shVar = DVar 235shVar = DVar
226 236
227shLet i a b = shLam' (cpar . shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b) 237shLet i a b = shLam' (shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b)
228shLet_ a b = DFreshName True $ shLam' (cpar . shLet' (shVar 0) $ DUp 0 a) b 238shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b
229 239
230----------------------------------------- 240-----------------------------------------
231 241
242instance IsString NDoc where
243 fromString = DAtom
244
232shAtom = DAtom 245shAtom = DAtom
233 246
234shTuple [] = DAtom "()" 247shTuple [] = "()"
235shTuple [x] = DParen $ DParen x 248shTuple [x] = DParen $ DParen x
236shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs 249shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs
237 250
238shAnn _ True x y | strip y == DAtom "Type" = x 251shAnn _ True x y | strip y == "Type" = x
239shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y 252shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y
240 253
241shApp _ x y = DOp (Fixity InfixL 10) x "" y 254pattern DApp x y = DOp (Fixity InfixL 10) x " " y
242 255
243shArr = DArr 256shArr = DArr
244 257
@@ -252,7 +265,4 @@ getFN a = (0, a)
252shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y 265shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y
253shLam' x y = DLam "\\" [x] "->" y 266shLam' x y = DLam "\\" [x] "->" y
254 267
255cpar s | simple s = s
256cpar 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:
11value x = case x of 11value x = case x of
12 ^ 12 ^
13Missing case(s): 13Missing 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:
11fun 1 = '1' 11fun 1 = '1'
12fun 2 = '2' 12fun 2 = '2'
13Missing case(s): 13Missing 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:
28fun 1 = '1' 28fun 1 = '1'
29fun 2 = '2' 29fun 2 = '2'
30Missing case(s): 30Missing case(s):
31 _ | False <- == (fromInt 1) a_, False <- == (fromInt 2) a_ 31 _ | False <- fromInt 1 == a_, False <- fromInt 2 == a_
32 32
33Uncovered pattern(s) at testdata/language-features/basic-values/def07.reject.lc:3:1: 33Uncovered pattern(s) at testdata/language-features/basic-values/def07.reject.lc:3:1:
34fun2 1 _ = '1' 34fun2 1 _ = '1'
35^^^^ 35^^^^
36Missing case(s): 36Missing case(s):
37 _ _ | False <- == (fromInt 1) b_ 37 _ _ | False <- fromInt 1 == b_
38 38