From 5595ce07a7548fbc9c8367106a4cd6bcbba5a55c Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 22 Aug 2017 12:42:51 -0400 Subject: [PATCH 01/57] Copy in the Data.Functor.Classes.Pretty module. --- prettyprinter/prettyprinter.cabal | 2 + .../src/Data/Functor/Classes/Pretty.hs | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 prettyprinter/src/Data/Functor/Classes/Pretty.hs diff --git a/prettyprinter/prettyprinter.cabal b/prettyprinter/prettyprinter.cabal index fee98d0c..b851dd94 100644 --- a/prettyprinter/prettyprinter.cabal +++ b/prettyprinter/prettyprinter.cabal @@ -40,6 +40,8 @@ library , Data.Text.Prettyprint.Doc.Symbols.Unicode , Data.Text.Prettyprint.Doc.Symbols.Ascii + , Data.Functor.Classes.Pretty + -- Deprecated , Data.Text.Prettyprint.Doc.Render.ShowS diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs new file mode 100644 index 00000000..9062ea98 --- /dev/null +++ b/prettyprinter/src/Data/Functor/Classes/Pretty.hs @@ -0,0 +1,62 @@ +module Data.Functor.Classes.Pretty +( Pretty1(..) +, Pretty2(..) +, module Pretty +, pretty1 +, Pretty1Of(..) +, Pretty2Of(..) +) where + +import Data.Text.Prettyprint.Doc as Pretty + +class Pretty1 f where + liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann + liftPrettyList :: (a -> Doc ann) -> ([a] -> Doc ann) -> [f a] -> Doc ann + liftPrettyList p pl = list . map (liftPretty p pl) + +class Pretty2 f where + liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann + liftPrettyList2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> [f a b] -> Doc ann + liftPrettyList2 pa pla pb plb = list . map (liftPretty2 pa pla pb plb) + +pretty1 :: (Pretty a, Pretty1 f) => f a -> Doc ann +pretty1 = liftPretty pretty prettyList + +instance Pretty1 [] where + liftPretty _ pl = pl + +instance Pretty2 Either where + liftPretty2 pL _ _ _ (Left l) = pL l + liftPretty2 _ _ pR _ (Right r) = pR r + +instance Pretty l => Pretty1 (Either l) where + liftPretty = liftPretty2 pretty prettyList + +instance Pretty2 (,) where + liftPretty2 pA _ pB _ (a, b) = tupled [ pA a, pB b ] + +instance Pretty a => Pretty1 ((,) a) where + liftPretty = liftPretty2 pretty prettyList + + +newtype Pretty1Of f a = Pretty1Of { unPretty1Of :: f a } + deriving (Eq, Ord, Show) + +instance Pretty1 f => Pretty1 (Pretty1Of f) where + liftPretty p pl = liftPretty p pl . unPretty1Of + +instance (Pretty1 f, Pretty a) => Pretty (Pretty1Of f a) where + pretty = pretty1 + + +newtype Pretty2Of f a b = Pretty2Of { unPretty2Of :: f a b } + deriving (Eq, Ord, Show) + +instance Pretty2 f => Pretty2 (Pretty2Of f) where + liftPretty2 pA plA pB plB = liftPretty2 pA plA pB plB . unPretty2Of + +instance (Pretty2 f, Pretty a) => Pretty1 (Pretty2Of f a) where + liftPretty = liftPretty2 pretty prettyList + +instance (Pretty2 f, Pretty a, Pretty b) => Pretty (Pretty2Of f a b) where + pretty = pretty1 From d3a44cffb796c21963484b435a2589bde4b41037 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 22 Aug 2017 13:31:58 -0400 Subject: [PATCH 02/57] Define generically-derivable Pretty1 instances. --- .../src/Data/Functor/Classes/Pretty.hs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs index 9062ea98..f3f9d25a 100644 --- a/prettyprinter/src/Data/Functor/Classes/Pretty.hs +++ b/prettyprinter/src/Data/Functor/Classes/Pretty.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE FlexibleInstances, TypeOperators #-} module Data.Functor.Classes.Pretty ( Pretty1(..) , Pretty2(..) @@ -8,6 +9,7 @@ module Data.Functor.Classes.Pretty ) where import Data.Text.Prettyprint.Doc as Pretty +import GHC.Generics class Pretty1 f where liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann @@ -60,3 +62,43 @@ instance (Pretty2 f, Pretty a) => Pretty1 (Pretty2Of f a) where instance (Pretty2 f, Pretty a, Pretty b) => Pretty (Pretty2Of f a b) where pretty = pretty1 + + +-- Generics + +class GPretty1 f where + gliftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann + gcollectPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> [Doc ann] + gcollectPretty p pl a = [gliftPretty p pl a] + +instance GPretty1 U1 where + gliftPretty _ _ _ = emptyDoc + +instance GPretty1 Par1 where + gliftPretty p _ (Par1 a) = p a + +instance Pretty c => GPretty1 (K1 i c) where + gliftPretty _ _ (K1 a) = pretty a + +instance Pretty1 f => GPretty1 (Rec1 f) where + gliftPretty p pl (Rec1 a) = liftPretty p pl a + +instance GPretty1 f => GPretty1 (M1 D c f) where + gliftPretty p pl (M1 a) = gliftPretty p pl a + +instance (Constructor c, GPretty1 f) => GPretty1 (M1 C c f) where + gliftPretty p pl m = nest 2 (vsep (pretty (conName m) : gcollectPretty p pl (unM1 m))) + +instance GPretty1 f => GPretty1 (M1 S c f) where + gliftPretty p pl (M1 a) = gliftPretty p pl a + +instance (GPretty1 f, GPretty1 g) => GPretty1 (f :+: g) where + gliftPretty p pl (L1 l) = gliftPretty p pl l + gliftPretty p pl (R1 r) = gliftPretty p pl r + +instance (GPretty1 f, GPretty1 g) => GPretty1 (f :*: g) where + gliftPretty p pl (a :*: b) = gliftPretty p pl a <+> gliftPretty p pl b + gcollectPretty p pl (a :*: b) = gcollectPretty p pl a <> gcollectPretty p pl b + +instance (Pretty1 f, GPretty1 g) => GPretty1 (f :.: g) where + gliftPretty p pl (Comp1 a) = liftPretty (gliftPretty p pl) (list . map (gliftPretty p pl)) a From 00f7bb3e7fe28dd56c663e565d464ffcea4361da Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 22 Aug 2017 13:35:09 -0400 Subject: [PATCH 03/57] Give a default implementation of liftPretty for Generic1 types. --- prettyprinter/src/Data/Functor/Classes/Pretty.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs index f3f9d25a..0496e9d9 100644 --- a/prettyprinter/src/Data/Functor/Classes/Pretty.hs +++ b/prettyprinter/src/Data/Functor/Classes/Pretty.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE FlexibleInstances, TypeOperators #-} +{-# LANGUAGE DefaultSignatures, FlexibleContexts, FlexibleInstances, TypeOperators #-} module Data.Functor.Classes.Pretty ( Pretty1(..) , Pretty2(..) @@ -13,6 +13,9 @@ import GHC.Generics class Pretty1 f where liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann + default liftPretty :: (Generic1 f, GPretty1 (Rep1 f)) => (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann + liftPretty p pl = gliftPretty p pl . from1 + liftPrettyList :: (a -> Doc ann) -> ([a] -> Doc ann) -> [f a] -> Doc ann liftPrettyList p pl = list . map (liftPretty p pl) From c51c52bb02b21102c16b90a5669dfeb00706aaa2 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 22 Aug 2017 13:53:27 -0400 Subject: [PATCH 04/57] Define a Pretty1 instance for NonEmpty. --- prettyprinter/src/Data/Functor/Classes/Pretty.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs index 0496e9d9..dd3aefe2 100644 --- a/prettyprinter/src/Data/Functor/Classes/Pretty.hs +++ b/prettyprinter/src/Data/Functor/Classes/Pretty.hs @@ -8,6 +8,8 @@ module Data.Functor.Classes.Pretty , Pretty2Of(..) ) where +import Data.Foldable (toList) +import Data.List.NonEmpty (NonEmpty(..)) import Data.Text.Prettyprint.Doc as Pretty import GHC.Generics @@ -30,6 +32,9 @@ pretty1 = liftPretty pretty prettyList instance Pretty1 [] where liftPretty _ pl = pl +instance Pretty1 NonEmpty where + liftPretty _ pl = pl . toList + instance Pretty2 Either where liftPretty2 pL _ _ _ (Left l) = pL l liftPretty2 _ _ pR _ (Right r) = pR r From 80823254bbed62f2b145b1ae814bdcf6fd38cccb Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 22 Aug 2017 13:54:19 -0400 Subject: [PATCH 05/57] Define a Pretty1 instance for Maybe. --- prettyprinter/src/Data/Functor/Classes/Pretty.hs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs index dd3aefe2..7b233b8e 100644 --- a/prettyprinter/src/Data/Functor/Classes/Pretty.hs +++ b/prettyprinter/src/Data/Functor/Classes/Pretty.hs @@ -35,6 +35,9 @@ instance Pretty1 [] where instance Pretty1 NonEmpty where liftPretty _ pl = pl . toList +instance Pretty1 Maybe where + liftPretty p _ = maybe emptyDoc p + instance Pretty2 Either where liftPretty2 pL _ _ _ (Left l) = pL l liftPretty2 _ _ pR _ (Right r) = pR r From 2247b13c574b1dc820315aefe456f142b5372319 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 09:48:26 -0400 Subject: [PATCH 06/57] :fire: Pretty1Of & Pretty2Of. --- .../src/Data/Functor/Classes/Pretty.hs | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs index 7b233b8e..c0db370c 100644 --- a/prettyprinter/src/Data/Functor/Classes/Pretty.hs +++ b/prettyprinter/src/Data/Functor/Classes/Pretty.hs @@ -52,29 +52,6 @@ instance Pretty a => Pretty1 ((,) a) where liftPretty = liftPretty2 pretty prettyList -newtype Pretty1Of f a = Pretty1Of { unPretty1Of :: f a } - deriving (Eq, Ord, Show) - -instance Pretty1 f => Pretty1 (Pretty1Of f) where - liftPretty p pl = liftPretty p pl . unPretty1Of - -instance (Pretty1 f, Pretty a) => Pretty (Pretty1Of f a) where - pretty = pretty1 - - -newtype Pretty2Of f a b = Pretty2Of { unPretty2Of :: f a b } - deriving (Eq, Ord, Show) - -instance Pretty2 f => Pretty2 (Pretty2Of f) where - liftPretty2 pA plA pB plB = liftPretty2 pA plA pB plB . unPretty2Of - -instance (Pretty2 f, Pretty a) => Pretty1 (Pretty2Of f a) where - liftPretty = liftPretty2 pretty prettyList - -instance (Pretty2 f, Pretty a, Pretty b) => Pretty (Pretty2Of f a b) where - pretty = pretty1 - - -- Generics class GPretty1 f where From 96e21f9e99699311860f2c3826b055dec54371f9 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 09:57:21 -0400 Subject: [PATCH 07/57] Define Pretty1 & Pretty2 in Data.Text.PrettyPrint.Doc.Internal. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 68318f78..665a6c33 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -353,6 +353,18 @@ instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict instance Pretty Void where pretty = absurd +class Pretty1 f where + liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann + + liftPrettyList :: (a -> Doc ann) -> ([a] -> Doc ann) -> [f a] -> Doc ann + liftPrettyList p pl = list . map (liftPretty p pl) + +class Pretty2 f where + liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann + + liftPrettyList2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> [f a b] -> Doc ann + liftPrettyList2 pa pla pb plb = list . map (liftPretty2 pa pla pb plb) + -- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@. -- From 680d6e24b27ea2904a678230ffc1c09503c5f909 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 09:57:33 -0400 Subject: [PATCH 08/57] :memo: Pretty1 & Pretty2. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 665a6c33..9aa165d6 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -353,12 +353,22 @@ instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict instance Pretty Void where pretty = absurd +-- | Overloaded conversion to 'Doc', lifted to unary type constructors. +-- +-- Laws: +-- +-- 1. output should be pretty. :-) class Pretty1 f where liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann liftPrettyList :: (a -> Doc ann) -> ([a] -> Doc ann) -> [f a] -> Doc ann liftPrettyList p pl = list . map (liftPretty p pl) +-- | Overloaded conversion to 'Doc', lifted to binary type constructors. +-- +-- Laws: +-- +-- 1. output should be pretty. :-) class Pretty2 f where liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann From f8e3343ce46a1c454ae59267e9cc003b43b5bb5b Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:02:04 -0400 Subject: [PATCH 09/57] :fire: liftPrettyList/liftPrettyList2. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 9aa165d6..2504311f 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -361,9 +361,6 @@ instance Pretty Void where pretty = absurd class Pretty1 f where liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann - liftPrettyList :: (a -> Doc ann) -> ([a] -> Doc ann) -> [f a] -> Doc ann - liftPrettyList p pl = list . map (liftPretty p pl) - -- | Overloaded conversion to 'Doc', lifted to binary type constructors. -- -- Laws: @@ -372,9 +369,6 @@ class Pretty1 f where class Pretty2 f where liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann - liftPrettyList2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> [f a b] -> Doc ann - liftPrettyList2 pa pla pb plb = list . map (liftPretty2 pa pla pb plb) - -- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@. -- From 3f65702c39dd7135ad9e9073aec0e47cf9a63ae8 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:02:10 -0400 Subject: [PATCH 10/57] :fire: redundant spacing. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 2504311f..0e528602 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -352,7 +352,6 @@ instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict -- [] instance Pretty Void where pretty = absurd - -- | Overloaded conversion to 'Doc', lifted to unary type constructors. -- -- Laws: @@ -369,7 +368,6 @@ class Pretty1 f where class Pretty2 f where liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann - -- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@. -- -- The string must not contain any newline characters, since this is an From 73dd98cde3c2d3f787cc0957160172fe58da71ae Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:04:16 -0400 Subject: [PATCH 11/57] Reformat liftPretty/liftPretty2. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 0e528602..4a94aebd 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -358,7 +358,10 @@ instance Pretty Void where pretty = absurd -- -- 1. output should be pretty. :-) class Pretty1 f where - liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann + liftPretty :: (a -> Doc ann) + -> ([a] -> Doc ann) + -> f a + -> Doc ann -- | Overloaded conversion to 'Doc', lifted to binary type constructors. -- @@ -366,7 +369,12 @@ class Pretty1 f where -- -- 1. output should be pretty. :-) class Pretty2 f where - liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann + liftPretty2 :: (a -> Doc ann) + -> ([a] -> Doc ann) + -> (b -> Doc ann) + -> ([b] -> Doc ann) + -> f a b + -> Doc ann -- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@. -- From 6d8d0af4058cd55e6fbae06222b6910969407197 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:05:23 -0400 Subject: [PATCH 12/57] Add newlines after class declarations. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 4a94aebd..bdf37f13 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -358,6 +358,7 @@ instance Pretty Void where pretty = absurd -- -- 1. output should be pretty. :-) class Pretty1 f where + liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a @@ -369,6 +370,7 @@ class Pretty1 f where -- -- 1. output should be pretty. :-) class Pretty2 f where + liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) From 7cafb338dd03c6def060bb1edd46b58b93385f86 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:07:55 -0400 Subject: [PATCH 13/57] :memo: liftPretty. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index bdf37f13..8abc4ac3 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -359,6 +359,8 @@ instance Pretty Void where pretty = absurd -- 1. output should be pretty. :-) class Pretty1 f where + -- | >>> liftPretty (\ s -> pretty s <> dot) (Just "hello") + -- hello. liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a From 9579404348820810aa9a222dd1924b9d9425cd59 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:08:54 -0400 Subject: [PATCH 14/57] :fire: Data.Functor.Classes.Pretty. --- prettyprinter/prettyprinter.cabal | 2 - .../src/Data/Functor/Classes/Pretty.hs | 92 ------------------- 2 files changed, 94 deletions(-) delete mode 100644 prettyprinter/src/Data/Functor/Classes/Pretty.hs diff --git a/prettyprinter/prettyprinter.cabal b/prettyprinter/prettyprinter.cabal index b851dd94..fee98d0c 100644 --- a/prettyprinter/prettyprinter.cabal +++ b/prettyprinter/prettyprinter.cabal @@ -40,8 +40,6 @@ library , Data.Text.Prettyprint.Doc.Symbols.Unicode , Data.Text.Prettyprint.Doc.Symbols.Ascii - , Data.Functor.Classes.Pretty - -- Deprecated , Data.Text.Prettyprint.Doc.Render.ShowS diff --git a/prettyprinter/src/Data/Functor/Classes/Pretty.hs b/prettyprinter/src/Data/Functor/Classes/Pretty.hs deleted file mode 100644 index c0db370c..00000000 --- a/prettyprinter/src/Data/Functor/Classes/Pretty.hs +++ /dev/null @@ -1,92 +0,0 @@ -{-# LANGUAGE DefaultSignatures, FlexibleContexts, FlexibleInstances, TypeOperators #-} -module Data.Functor.Classes.Pretty -( Pretty1(..) -, Pretty2(..) -, module Pretty -, pretty1 -, Pretty1Of(..) -, Pretty2Of(..) -) where - -import Data.Foldable (toList) -import Data.List.NonEmpty (NonEmpty(..)) -import Data.Text.Prettyprint.Doc as Pretty -import GHC.Generics - -class Pretty1 f where - liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann - default liftPretty :: (Generic1 f, GPretty1 (Rep1 f)) => (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann - liftPretty p pl = gliftPretty p pl . from1 - - liftPrettyList :: (a -> Doc ann) -> ([a] -> Doc ann) -> [f a] -> Doc ann - liftPrettyList p pl = list . map (liftPretty p pl) - -class Pretty2 f where - liftPretty2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> f a b -> Doc ann - liftPrettyList2 :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) -> [f a b] -> Doc ann - liftPrettyList2 pa pla pb plb = list . map (liftPretty2 pa pla pb plb) - -pretty1 :: (Pretty a, Pretty1 f) => f a -> Doc ann -pretty1 = liftPretty pretty prettyList - -instance Pretty1 [] where - liftPretty _ pl = pl - -instance Pretty1 NonEmpty where - liftPretty _ pl = pl . toList - -instance Pretty1 Maybe where - liftPretty p _ = maybe emptyDoc p - -instance Pretty2 Either where - liftPretty2 pL _ _ _ (Left l) = pL l - liftPretty2 _ _ pR _ (Right r) = pR r - -instance Pretty l => Pretty1 (Either l) where - liftPretty = liftPretty2 pretty prettyList - -instance Pretty2 (,) where - liftPretty2 pA _ pB _ (a, b) = tupled [ pA a, pB b ] - -instance Pretty a => Pretty1 ((,) a) where - liftPretty = liftPretty2 pretty prettyList - - --- Generics - -class GPretty1 f where - gliftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann - gcollectPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> [Doc ann] - gcollectPretty p pl a = [gliftPretty p pl a] - -instance GPretty1 U1 where - gliftPretty _ _ _ = emptyDoc - -instance GPretty1 Par1 where - gliftPretty p _ (Par1 a) = p a - -instance Pretty c => GPretty1 (K1 i c) where - gliftPretty _ _ (K1 a) = pretty a - -instance Pretty1 f => GPretty1 (Rec1 f) where - gliftPretty p pl (Rec1 a) = liftPretty p pl a - -instance GPretty1 f => GPretty1 (M1 D c f) where - gliftPretty p pl (M1 a) = gliftPretty p pl a - -instance (Constructor c, GPretty1 f) => GPretty1 (M1 C c f) where - gliftPretty p pl m = nest 2 (vsep (pretty (conName m) : gcollectPretty p pl (unM1 m))) - -instance GPretty1 f => GPretty1 (M1 S c f) where - gliftPretty p pl (M1 a) = gliftPretty p pl a - -instance (GPretty1 f, GPretty1 g) => GPretty1 (f :+: g) where - gliftPretty p pl (L1 l) = gliftPretty p pl l - gliftPretty p pl (R1 r) = gliftPretty p pl r - -instance (GPretty1 f, GPretty1 g) => GPretty1 (f :*: g) where - gliftPretty p pl (a :*: b) = gliftPretty p pl a <+> gliftPretty p pl b - gcollectPretty p pl (a :*: b) = gcollectPretty p pl a <> gcollectPretty p pl b - -instance (Pretty1 f, GPretty1 g) => GPretty1 (f :.: g) where - gliftPretty p pl (Comp1 a) = liftPretty (gliftPretty p pl) (list . map (gliftPretty p pl)) a From add8798c34d847aadcc1869ff93fd2658e340047 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:17:25 -0400 Subject: [PATCH 15/57] Define a bunch of Pretty1 instances. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 8abc4ac3..e58f8ec4 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -366,6 +366,18 @@ class Pretty1 f where -> f a -> Doc ann +instance Pretty1 [] where + liftPretty _ prettyList' = prettyList' + +instance Pretty1 NonEmpty where + liftPretty _ prettyList' (x:|xs) = prettyList' (x:xs) + +instance Pretty1 Maybe where + liftPretty p _ = maybe emptyDoc p + +instance Pretty a => Pretty1 ((,) a) where + liftPretty pretty2 _ (x1, x2) = tupled [pretty x1, pretty2 x2] + -- | Overloaded conversion to 'Doc', lifted to binary type constructors. -- -- Laws: From ba7ba5650538a9535375852a6b927a22428b9f96 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:17:34 -0400 Subject: [PATCH 16/57] :memo: Pretty1 []. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index e58f8ec4..82f6dcab 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -366,6 +366,8 @@ class Pretty1 f where -> f a -> Doc ann +-- | >>> liftPretty (parens . pretty) [1,2,3] +-- [(1), (2), (3)] instance Pretty1 [] where liftPretty _ prettyList' = prettyList' From 146ab167fb702f44ee4f06ee710ac577787fb9fd Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:17:42 -0400 Subject: [PATCH 17/57] :memo: Pretty1 Maybe. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 82f6dcab..4cc306db 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -374,6 +374,12 @@ instance Pretty1 [] where instance Pretty1 NonEmpty where liftPretty _ prettyList' (x:|xs) = prettyList' (x:xs) +-- | Ignore 'Nothing's, print 'Just' contents. +-- +-- >>> liftPretty (parens . pretty) (Just True) +-- (True) +-- >>> braces (liftPretty (parens . pretty) (Nothing :: Maybe Bool)) +-- {} instance Pretty1 Maybe where liftPretty p _ = maybe emptyDoc p From 12ebec7eb0e9c43acc759d26fb890227d327c4de Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:17:52 -0400 Subject: [PATCH 18/57] Better :memo: for liftPretty. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 4cc306db..e85b9ee0 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -359,8 +359,8 @@ instance Pretty Void where pretty = absurd -- 1. output should be pretty. :-) class Pretty1 f where - -- | >>> liftPretty (\ s -> pretty s <> dot) (Just "hello") - -- hello. + -- | >>> liftPretty (parens . pretty) (Just "hello") + -- (hello) liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a From 8f3c30653c2bf83e22cee645c4a4b6cb90ac882a Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:18:39 -0400 Subject: [PATCH 19/57] Define a Pretty2 instance for pairs. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index e85b9ee0..61425e1b 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -400,6 +400,10 @@ class Pretty2 f where -> f a b -> Doc ann + +instance Pretty2 (,) where + liftPretty2 pretty1 _ pretty2 _ (x1, x2) = tupled [pretty1 x1, pretty2 x2] + -- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@. -- -- The string must not contain any newline characters, since this is an From 223b7af13c0e8f3f2b102f0f34603db8932d8169 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:19:31 -0400 Subject: [PATCH 20/57] :memo: Pretty1 ((,) a). --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 61425e1b..1ef13300 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -383,6 +383,8 @@ instance Pretty1 NonEmpty where instance Pretty1 Maybe where liftPretty p _ = maybe emptyDoc p +-- | >>> liftPretty (parens . pretty) (123, "hello") +-- (123, (hello)) instance Pretty a => Pretty1 ((,) a) where liftPretty pretty2 _ (x1, x2) = tupled [pretty x1, pretty2 x2] From 6280232b84f3b3ee264f5d0ef8ee8dd531e0333c Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:23:10 -0400 Subject: [PATCH 21/57] Include the list parameter in the :memo:s. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 1ef13300..d9809f33 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -359,14 +359,14 @@ instance Pretty Void where pretty = absurd -- 1. output should be pretty. :-) class Pretty1 f where - -- | >>> liftPretty (parens . pretty) (Just "hello") + -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") -- (hello) liftPretty :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann --- | >>> liftPretty (parens . pretty) [1,2,3] +-- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) [1,2,3] -- [(1), (2), (3)] instance Pretty1 [] where liftPretty _ prettyList' = prettyList' @@ -376,14 +376,14 @@ instance Pretty1 NonEmpty where -- | Ignore 'Nothing's, print 'Just' contents. -- --- >>> liftPretty (parens . pretty) (Just True) +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just True) -- (True) --- >>> braces (liftPretty (parens . pretty) (Nothing :: Maybe Bool)) +-- >>> braces (liftPretty (parens . pretty) (list . map (parens . pretty)) (Nothing :: Maybe Bool)) -- {} instance Pretty1 Maybe where liftPretty p _ = maybe emptyDoc p --- | >>> liftPretty (parens . pretty) (123, "hello") +-- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (123, "hello") -- (123, (hello)) instance Pretty a => Pretty1 ((,) a) where liftPretty pretty2 _ (x1, x2) = tupled [pretty x1, pretty2 x2] From 00612b751511ea11cd040afd1a74058b5b5faa43 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:24:30 -0400 Subject: [PATCH 22/57] :memo: liftPretty2. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index d9809f33..5d241e69 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -403,6 +403,8 @@ class Pretty2 f where -> Doc ann +-- | >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (123, "hello") +-- ((123), (hello)) instance Pretty2 (,) where liftPretty2 pretty1 _ pretty2 _ (x1, x2) = tupled [pretty1 x1, pretty2 x2] From 5acdc82ed50b88eda6b73c6a6ef091f7f04d426c Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:25:26 -0400 Subject: [PATCH 23/57] Define a Pretty instance for Either. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 5d241e69..9118e20f 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -328,6 +328,9 @@ instance Pretty a => Pretty (Maybe a) where pretty = maybe mempty pretty prettyList = prettyList . catMaybes +instance (Pretty a, Pretty b) => Pretty (Either a b) where + pretty = either pretty pretty + -- | Automatically converts all newlines to @'line'@. -- -- >>> pretty ("hello\nworld" :: Text) From df988099bd896a92fee09f950a0236e5975ce038 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:26:16 -0400 Subject: [PATCH 24/57] :memo: Pretty (Either a b). --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 9118e20f..5f189d9a 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -328,6 +328,12 @@ instance Pretty a => Pretty (Maybe a) where pretty = maybe mempty pretty prettyList = prettyList . catMaybes +-- | Print 'Left' and 'Right' contents. +-- +-- >>> pretty (Left True) +-- True +-- >>> pretty (Right True) +-- True instance (Pretty a, Pretty b) => Pretty (Either a b) where pretty = either pretty pretty From 4c42bd0adff1b16d6b64c2fdcd248cde01f67182 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:28:25 -0400 Subject: [PATCH 25/57] Mirror the Pretty instance for Maybe. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 5f189d9a..17a80a28 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -390,7 +390,7 @@ instance Pretty1 NonEmpty where -- >>> braces (liftPretty (parens . pretty) (list . map (parens . pretty)) (Nothing :: Maybe Bool)) -- {} instance Pretty1 Maybe where - liftPretty p _ = maybe emptyDoc p + liftPretty prettyJust _ = maybe mempty prettyJust -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (123, "hello") -- (123, (hello)) From fce219b8be2a581d719011ca02dd238536fd53e2 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:31:29 -0400 Subject: [PATCH 26/57] :memo: why Pretty1. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 17a80a28..8b13e8e8 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -363,6 +363,10 @@ instance Pretty Void where pretty = absurd -- | Overloaded conversion to 'Doc', lifted to unary type constructors. -- +-- This is most useful for defining 'Pretty' instances for recursive types, and +-- for efficiently pretty-printing 'Functor's whose definitions of 'fmap' don’t +-- fuse. +-- -- Laws: -- -- 1. output should be pretty. :-) From 85d57e407048b4de35c265ad361287b66c72b1e4 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:31:44 -0400 Subject: [PATCH 27/57] :memo: why Pretty2. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 8b13e8e8..ed372fb6 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -403,6 +403,10 @@ instance Pretty a => Pretty1 ((,) a) where -- | Overloaded conversion to 'Doc', lifted to binary type constructors. -- +-- This is most useful for defining 'Pretty' instances for recursive types, and +-- for efficiently pretty-printing 'Functor's whose definitions of 'fmap' don’t +-- fuse. +-- -- Laws: -- -- 1. output should be pretty. :-) From 9d2f6a8c4ae6c93dac9482a747fd94e2868c5622 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:32:14 -0400 Subject: [PATCH 28/57] Better :memo: for Pretty1 Maybe. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index ed372fb6..d2e43520 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -387,7 +387,7 @@ instance Pretty1 [] where instance Pretty1 NonEmpty where liftPretty _ prettyList' (x:|xs) = prettyList' (x:xs) --- | Ignore 'Nothing's, print 'Just' contents. +-- | Ignore 'Nothing's, print 'Just' contents with the supplied function. -- -- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just True) -- (True) From 943163ad8defbfef77f4db6e41d8c9261272f996 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:33:09 -0400 Subject: [PATCH 29/57] Define Pretty1 & Pretty2 instances for Either. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index d2e43520..3b15f8e9 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -396,6 +396,9 @@ instance Pretty1 NonEmpty where instance Pretty1 Maybe where liftPretty prettyJust _ = maybe mempty prettyJust +instance Pretty a => Pretty1 (Either a) where + liftPretty prettyRight _ = either pretty prettyRight + -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (123, "hello") -- (123, (hello)) instance Pretty a => Pretty1 ((,) a) where @@ -419,6 +422,8 @@ class Pretty2 f where -> f a b -> Doc ann +instance Pretty2 Either where + liftPretty2 prettyLeft _ prettyRight _ = either prettyLeft prettyRight -- | >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (123, "hello") -- ((123), (hello)) From 03bd95286a19e9d80b0562f57ca8f80da529bf03 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:33:15 -0400 Subject: [PATCH 30/57] :memo: Pretty2 Either. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 3b15f8e9..a269f0f9 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -422,6 +422,12 @@ class Pretty2 f where -> f a b -> Doc ann +-- | Print 'Left' and 'Right' contents with the supplied functions. +-- +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Left True) +-- True +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Right True) +-- True instance Pretty2 Either where liftPretty2 prettyLeft _ prettyRight _ = either prettyLeft prettyRight From 566b0dc4609391790f77fc5a5af64e5014e0ea1f Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:34:27 -0400 Subject: [PATCH 31/57] :memo: Pretty1 (Either a). --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index a269f0f9..1f532905 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -396,6 +396,13 @@ instance Pretty1 NonEmpty where instance Pretty1 Maybe where liftPretty prettyJust _ = maybe mempty prettyJust +-- | Print 'Left' contents with 'pretty', and 'Right' contents with the supplied +-- function. +-- +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Left True) +-- True +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Right True) +-- (True) instance Pretty a => Pretty1 (Either a) where liftPretty prettyRight _ = either pretty prettyRight From 2fa9adce1c3d881ca08c84ce965f3e28574f6b21 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:37:19 -0400 Subject: [PATCH 32/57] Better docs for Pretty1/Pretty2. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 1f532905..28deb118 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -363,9 +363,12 @@ instance Pretty Void where pretty = absurd -- | Overloaded conversion to 'Doc', lifted to unary type constructors. -- --- This is most useful for defining 'Pretty' instances for recursive types, and --- for efficiently pretty-printing 'Functor's whose definitions of 'fmap' don’t --- fuse. +-- This is most useful for: +-- 1. defining 'Pretty' instances for recursive types, +-- 2. defining 'Pretty' instances for type constructors without 'Functor' +-- instances, and +-- 3. efficiently pretty-printing type constructors with 'Functor' instances +-- whose 'fmap' traverses the whole structure. -- -- Laws: -- @@ -413,9 +416,12 @@ instance Pretty a => Pretty1 ((,) a) where -- | Overloaded conversion to 'Doc', lifted to binary type constructors. -- --- This is most useful for defining 'Pretty' instances for recursive types, and --- for efficiently pretty-printing 'Functor's whose definitions of 'fmap' don’t --- fuse. +-- This is most useful for: +-- 1. defining 'Pretty' instances for recursive types, +-- 2. defining 'Pretty' instances for type constructors without 'Functor' +-- instances, and +-- 3. efficiently pretty-printing type constructors with 'Functor' instances +-- whose 'fmap' traverses the whole structure. -- -- Laws: -- From 21a3c33579e950d1ba5f13460b943011878f1684 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:37:47 -0400 Subject: [PATCH 33/57] Correct the examples for Pretty2 Either. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 28deb118..ca9aec4f 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -437,10 +437,10 @@ class Pretty2 f where -- | Print 'Left' and 'Right' contents with the supplied functions. -- --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Left True) --- True --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Right True) --- True +-- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Left True) +-- (True) +-- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Right True) +-- (True) instance Pretty2 Either where liftPretty2 prettyLeft _ prettyRight _ = either prettyLeft prettyRight From 605f59a793533eabb8a1ef4b6d826b5672582a4c Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:38:36 -0400 Subject: [PATCH 34/57] Match the formatting style of e.g. 'nest'. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index ca9aec4f..d29d6716 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -377,7 +377,8 @@ class Pretty1 f where -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") -- (hello) - liftPretty :: (a -> Doc ann) + liftPretty + :: (a -> Doc ann) -> ([a] -> Doc ann) -> f a -> Doc ann @@ -428,7 +429,8 @@ instance Pretty a => Pretty1 ((,) a) where -- 1. output should be pretty. :-) class Pretty2 f where - liftPretty2 :: (a -> Doc ann) + liftPretty2 + :: (a -> Doc ann) -> ([a] -> Doc ann) -> (b -> Doc ann) -> ([b] -> Doc ann) From ad6f1719518be86e524b9bf5727aef322f6129a6 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:40:53 -0400 Subject: [PATCH 35/57] :memo: the arguments to liftPretty. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index d29d6716..dd4622ac 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -378,8 +378,8 @@ class Pretty1 f where -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") -- (hello) liftPretty - :: (a -> Doc ann) - -> ([a] -> Doc ann) + :: (a -> Doc ann) -- ^ A function to print a single value. + -> ([a] -> Doc ann) -- ^ A function to print a list. Used for []. -> f a -> Doc ann From 07986b773a658bc626f34e269afd327a6a6f24ec Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:41:16 -0400 Subject: [PATCH 36/57] =?UTF-8?q?Indent,=20don=E2=80=99t=20align.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index dd4622ac..c1567c29 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -378,10 +378,10 @@ class Pretty1 f where -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") -- (hello) liftPretty - :: (a -> Doc ann) -- ^ A function to print a single value. - -> ([a] -> Doc ann) -- ^ A function to print a list. Used for []. - -> f a - -> Doc ann + :: (a -> Doc ann) -- ^ A function to print a single value. + -> ([a] -> Doc ann) -- ^ A function to print a list. Used for []. + -> f a + -> Doc ann -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) [1,2,3] -- [(1), (2), (3)] @@ -430,12 +430,12 @@ instance Pretty a => Pretty1 ((,) a) where class Pretty2 f where liftPretty2 - :: (a -> Doc ann) - -> ([a] -> Doc ann) - -> (b -> Doc ann) - -> ([b] -> Doc ann) - -> f a b - -> Doc ann + :: (a -> Doc ann) + -> ([a] -> Doc ann) + -> (b -> Doc ann) + -> ([b] -> Doc ann) + -> f a b + -> Doc ann -- | Print 'Left' and 'Right' contents with the supplied functions. -- From 1da038bfe72ca45a3551733f8151274971c557dd Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:43:35 -0400 Subject: [PATCH 37/57] :memo: the arguments to liftPretty2. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index c1567c29..dc7bc3f4 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -430,10 +430,10 @@ instance Pretty a => Pretty1 ((,) a) where class Pretty2 f where liftPretty2 - :: (a -> Doc ann) - -> ([a] -> Doc ann) - -> (b -> Doc ann) - -> ([b] -> Doc ann) + :: (a -> Doc ann) -- ^ A function to print a single value of the first parameter. + -> ([a] -> Doc ann) -- ^ A function to print a list of the first parameter. + -> (b -> Doc ann) -- ^ A function to print a single value of the second parameter. + -> ([b] -> Doc ann) -- ^ A function to print a list of the second parameter. -> f a b -> Doc ann From f91663ae99be4474fb05b4276df43ed00da75c46 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:48:48 -0400 Subject: [PATCH 38/57] Disambiguate the types for the doctests. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index dc7bc3f4..c5948e99 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -403,9 +403,9 @@ instance Pretty1 Maybe where -- | Print 'Left' contents with 'pretty', and 'Right' contents with the supplied -- function. -- --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Left True) +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Left True :: Either Bool Bool) -- True --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Right True) +-- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Right True :: Either Bool Bool) -- (True) instance Pretty a => Pretty1 (Either a) where liftPretty prettyRight _ = either pretty prettyRight @@ -439,9 +439,9 @@ class Pretty2 f where -- | Print 'Left' and 'Right' contents with the supplied functions. -- --- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Left True) +-- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Left True :: Either Bool Bool) -- (True) --- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Right True) +-- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Right True :: Either Bool Bool) -- (True) instance Pretty2 Either where liftPretty2 prettyLeft _ prettyRight _ = either prettyLeft prettyRight From ee66301665ddfd7caf183c9591b769af8b67863a Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 10:50:04 -0400 Subject: [PATCH 39/57] Fix the doctests for Pretty (Either a b). --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index c5948e99..9338a0ab 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -330,9 +330,9 @@ instance Pretty a => Pretty (Maybe a) where -- | Print 'Left' and 'Right' contents. -- --- >>> pretty (Left True) +-- >>> pretty (Left True :: Either Bool Bool) -- True --- >>> pretty (Right True) +-- >>> pretty (Right True :: Either Bool Bool) -- True instance (Pretty a, Pretty b) => Pretty (Either a b) where pretty = either pretty pretty From ec0e4825b18b5d43511396b03aac12b388c4ee02 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 23 Aug 2017 11:46:56 -0400 Subject: [PATCH 40/57] Export Pretty1 & Pretty2. --- prettyprinter/src/Data/Text/Prettyprint/Doc.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc.hs index 78661e17..dde12e17 100644 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc.hs @@ -267,6 +267,10 @@ module Data.Text.Prettyprint.Doc ( PageWidth(..), LayoutOptions(..), defaultLayoutOptions, layoutPretty, layoutCompact, layoutSmart, + -- * Lifted classes. + Pretty1(..), + Pretty2(..), + -- * Migration guide -- -- $migration From fc5efc1d5d6c6377861a1f10bb934ef7a6125b49 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Sat, 26 Aug 2017 16:37:45 -0400 Subject: [PATCH 41/57] Reformat the doctests for the Pretty2 instance for Either. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 9338a0ab..41669b50 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -439,9 +439,13 @@ class Pretty2 f where -- | Print 'Left' and 'Right' contents with the supplied functions. -- --- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Left True :: Either Bool Bool) +-- >>> let parenthesized = parens . pretty +-- >>> parenthesizedList = list . map parenthesized +-- >>> liftPretty2 parenthesized parenthesizedList parenthesized parenthesizedList (Left True :: Either Bool Bool) -- (True) --- >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (Right True :: Either Bool Bool) +-- >>> let parenthesized = parens . pretty +-- >>> parenthesizedList = list . map parenthesized +-- >>> liftPretty2 parenthesized parenthesizedList parenthesized parenthesizedList (Right True :: Either Bool Bool) -- (True) instance Pretty2 Either where liftPretty2 prettyLeft _ prettyRight _ = either prettyLeft prettyRight From a60ab84a5ef59f3cbd46af46b7798b6052865602 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Sat, 26 Aug 2017 16:40:04 -0400 Subject: [PATCH 42/57] Give laws relating Pretty2, Pretty1, & Pretty. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 41669b50..71ea9d50 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -372,7 +372,7 @@ instance Pretty Void where pretty = absurd -- -- Laws: -- --- 1. output should be pretty. :-) +-- 1. liftPretty pretty prettyList f = pretty f class Pretty1 f where -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") @@ -426,7 +426,8 @@ instance Pretty a => Pretty1 ((,) a) where -- -- Laws: -- --- 1. output should be pretty. :-) +-- 1. liftPretty2 pretty prettyList p pl f = liftPretty p pl f +-- 2. liftPretty2 pretty prettyList pretty prettyList f = pretty f class Pretty2 f where liftPretty2 From d6c2166af133a9272d01397baecc7c4f5d7f0416 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Sat, 26 Aug 2017 16:51:38 -0400 Subject: [PATCH 43/57] Better formatting of the laws. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 71ea9d50..e938051c 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -372,7 +372,11 @@ instance Pretty Void where pretty = absurd -- -- Laws: -- --- 1. liftPretty pretty prettyList f = pretty f +-- 1. @'Pretty1' f@ and @'Pretty' (f a)@ should result in identical behaviour: +-- +-- @ +-- liftPretty pretty prettyList f = pretty f +-- @ class Pretty1 f where -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") @@ -426,8 +430,12 @@ instance Pretty a => Pretty1 ((,) a) where -- -- Laws: -- --- 1. liftPretty2 pretty prettyList p pl f = liftPretty p pl f --- 2. liftPretty2 pretty prettyList pretty prettyList f = pretty f +-- 1. @'Pretty2' f@, @'Pretty1' (f a)@, and @'Pretty' (f a b)@ should result +-- in identical behaviour: +-- +-- @ +-- liftPretty2 pretty prettyList pretty prettyList f = liftPretty pretty prettyList f = pretty f +-- @ class Pretty2 f where liftPretty2 From 2441649f6b4c15584a99738a3d3a563251f56f75 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Sat, 26 Aug 2017 17:25:54 -0400 Subject: [PATCH 44/57] Fix the doctests. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index e938051c..7e8e90f2 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -449,11 +449,11 @@ class Pretty2 f where -- | Print 'Left' and 'Right' contents with the supplied functions. -- -- >>> let parenthesized = parens . pretty --- >>> parenthesizedList = list . map parenthesized +-- >>> let parenthesizedList = list . map parenthesized -- >>> liftPretty2 parenthesized parenthesizedList parenthesized parenthesizedList (Left True :: Either Bool Bool) -- (True) -- >>> let parenthesized = parens . pretty --- >>> parenthesizedList = list . map parenthesized +-- >>> let parenthesizedList = list . map parenthesized -- >>> liftPretty2 parenthesized parenthesizedList parenthesized parenthesizedList (Right True :: Either Bool Bool) -- (True) instance Pretty2 Either where From 2284f72c53024866f37633392d12fcdaafa4fbc6 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:17:24 -0400 Subject: [PATCH 45/57] :fire: the list parameters to liftPretty & liftPretty2. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 7e8e90f2..1b65783b 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -379,45 +379,44 @@ instance Pretty Void where pretty = absurd -- @ class Pretty1 f where - -- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just "hello") + -- | >>> liftPretty (parens . pretty) (Just "hello") -- (hello) liftPretty - :: (a -> Doc ann) -- ^ A function to print a single value. - -> ([a] -> Doc ann) -- ^ A function to print a list. Used for []. + :: (a -> Doc ann) -- ^ A function to print a value. -> f a -> Doc ann --- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) [1,2,3] +-- | >>> liftPretty (parens . pretty) [1,2,3] -- [(1), (2), (3)] instance Pretty1 [] where - liftPretty _ prettyList' = prettyList' + liftPretty pretty' = list . map pretty' instance Pretty1 NonEmpty where - liftPretty _ prettyList' (x:|xs) = prettyList' (x:xs) + liftPretty pretty' (x:|xs) = liftPretty pretty' (x:xs) -- | Ignore 'Nothing's, print 'Just' contents with the supplied function. -- --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Just True) +-- >>> liftPretty (parens . pretty) (Just True) -- (True) --- >>> braces (liftPretty (parens . pretty) (list . map (parens . pretty)) (Nothing :: Maybe Bool)) +-- >>> braces (liftPretty (parens . pretty) (Nothing :: Maybe Bool)) -- {} instance Pretty1 Maybe where - liftPretty prettyJust _ = maybe mempty prettyJust + liftPretty prettyJust = maybe mempty prettyJust -- | Print 'Left' contents with 'pretty', and 'Right' contents with the supplied -- function. -- --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Left True :: Either Bool Bool) +-- >>> liftPretty (parens . pretty) (Left True :: Either Bool Bool) -- True --- >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (Right True :: Either Bool Bool) +-- >>> liftPretty (parens . pretty) (Right True :: Either Bool Bool) -- (True) instance Pretty a => Pretty1 (Either a) where - liftPretty prettyRight _ = either pretty prettyRight + liftPretty prettyRight = either pretty prettyRight --- | >>> liftPretty (parens . pretty) (list . map (parens . pretty)) (123, "hello") +-- | >>> liftPretty (parens . pretty) (123, "hello") -- (123, (hello)) instance Pretty a => Pretty1 ((,) a) where - liftPretty pretty2 _ (x1, x2) = tupled [pretty x1, pretty2 x2] + liftPretty pretty2 (x1, x2) = tupled [pretty x1, pretty2 x2] -- | Overloaded conversion to 'Doc', lifted to binary type constructors. -- @@ -440,29 +439,24 @@ class Pretty2 f where liftPretty2 :: (a -> Doc ann) -- ^ A function to print a single value of the first parameter. - -> ([a] -> Doc ann) -- ^ A function to print a list of the first parameter. -> (b -> Doc ann) -- ^ A function to print a single value of the second parameter. - -> ([b] -> Doc ann) -- ^ A function to print a list of the second parameter. -> f a b -> Doc ann -- | Print 'Left' and 'Right' contents with the supplied functions. -- -- >>> let parenthesized = parens . pretty --- >>> let parenthesizedList = list . map parenthesized --- >>> liftPretty2 parenthesized parenthesizedList parenthesized parenthesizedList (Left True :: Either Bool Bool) +-- >>> liftPretty2 parenthesized parenthesized (Left True :: Either Bool Bool) -- (True) --- >>> let parenthesized = parens . pretty --- >>> let parenthesizedList = list . map parenthesized --- >>> liftPretty2 parenthesized parenthesizedList parenthesized parenthesizedList (Right True :: Either Bool Bool) +-- >>> liftPretty2 parenthesized parenthesized (Right True :: Either Bool Bool) -- (True) instance Pretty2 Either where - liftPretty2 prettyLeft _ prettyRight _ = either prettyLeft prettyRight + liftPretty2 prettyLeft prettyRight = either prettyLeft prettyRight --- | >>> liftPretty2 (parens . pretty) (list . map (parens . pretty)) (parens . pretty) (list . map (parens . pretty)) (123, "hello") +-- | >>> liftPretty2 (parens . pretty) (parens . pretty) (123, "hello") -- ((123), (hello)) instance Pretty2 (,) where - liftPretty2 pretty1 _ pretty2 _ (x1, x2) = tupled [pretty1 x1, pretty2 x2] + liftPretty2 pretty1 pretty2 (x1, x2) = tupled [pretty1 x1, pretty2 x2] -- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@. -- From d9425eb58c0f393628f94a08d22b7653cf7647d2 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:17:48 -0400 Subject: [PATCH 46/57] Briefer docs for liftPretty2. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 1b65783b..36c9430f 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -438,8 +438,8 @@ instance Pretty a => Pretty1 ((,) a) where class Pretty2 f where liftPretty2 - :: (a -> Doc ann) -- ^ A function to print a single value of the first parameter. - -> (b -> Doc ann) -- ^ A function to print a single value of the second parameter. + :: (a -> Doc ann) -- ^ A function to print a value of the first parameter. + -> (b -> Doc ann) -- ^ A function to print a value of the second parameter. -> f a b -> Doc ann From 5b9f66b8f124404d30878f21d5679ecda10f5545 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:22:07 -0400 Subject: [PATCH 47/57] Correct the law for Pretty1. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 36c9430f..84479235 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -372,10 +372,11 @@ instance Pretty Void where pretty = absurd -- -- Laws: -- --- 1. @'Pretty1' f@ and @'Pretty' (f a)@ should result in identical behaviour: +-- 1. @'Pretty1' f@ and @'Pretty' (f a)@ should result in identical behaviour +-- when @'prettyList' = 'list' . map 'pretty'@ (the default definition): -- -- @ --- liftPretty pretty prettyList f = pretty f +-- liftPretty pretty f = pretty f -- @ class Pretty1 f where From 4dced207f57ca67296f309eb3e600493e5ab1ca3 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:22:23 -0400 Subject: [PATCH 48/57] Note the difference in behaviour between Pretty1 and Pretty for []. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 84479235..cfa58aab 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -378,6 +378,13 @@ instance Pretty Void where pretty = absurd -- @ -- liftPretty pretty f = pretty f -- @ +-- +-- Note that since 'liftPretty' does not receive a function corresponding to +-- 'prettyList', the behaviour will differ for '[]', 'Maybe', and other types +-- whose instances customize 'prettyList': +-- +-- >>> liftPretty pretty "hello" +-- [h, e, l, l, o] class Pretty1 f where -- | >>> liftPretty (parens . pretty) (Just "hello") From 31cf99b6ef94bf031fd8e8f643a20b3b373678f5 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:23:16 -0400 Subject: [PATCH 49/57] Cast for the doctest. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index cfa58aab..82c4fc20 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -383,7 +383,7 @@ instance Pretty Void where pretty = absurd -- 'prettyList', the behaviour will differ for '[]', 'Maybe', and other types -- whose instances customize 'prettyList': -- --- >>> liftPretty pretty "hello" +-- >>> liftPretty pretty ("hello" :: String) -- [h, e, l, l, o] class Pretty1 f where From 03a1ae2dfa0c1f053a03a63749f9db8a399d9046 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:25:08 -0400 Subject: [PATCH 50/57] Clarify the relationship between liftPretty2, liftPretty, and pretty. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 82c4fc20..a59e2df1 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -437,11 +437,12 @@ instance Pretty a => Pretty1 ((,) a) where -- -- Laws: -- --- 1. @'Pretty2' f@, @'Pretty1' (f a)@, and @'Pretty' (f a b)@ should result --- in identical behaviour: +-- 1. @'Pretty2' f@, @'Pretty1' (f a)@, and @'Pretty' (f a b)@ should result in +-- identical behaviour when @'prettyList' = 'list' . map 'pretty'@ (the +-- default definition): -- -- @ --- liftPretty2 pretty prettyList pretty prettyList f = liftPretty pretty prettyList f = pretty f +-- liftPretty2 pretty pretty f = liftPretty pretty f = pretty f -- @ class Pretty2 f where From a1d6c13aa0ea24b2c337ea36d333f73a799de48a Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:26:10 -0400 Subject: [PATCH 51/57] Dedent the param docs. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index a59e2df1..3e1c139b 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -390,7 +390,7 @@ class Pretty1 f where -- | >>> liftPretty (parens . pretty) (Just "hello") -- (hello) liftPretty - :: (a -> Doc ann) -- ^ A function to print a value. + :: (a -> Doc ann) -- ^ A function to print a value. -> f a -> Doc ann @@ -447,8 +447,8 @@ instance Pretty a => Pretty1 ((,) a) where class Pretty2 f where liftPretty2 - :: (a -> Doc ann) -- ^ A function to print a value of the first parameter. - -> (b -> Doc ann) -- ^ A function to print a value of the second parameter. + :: (a -> Doc ann) -- ^ A function to print a value of the first parameter. + -> (b -> Doc ann) -- ^ A function to print a value of the second parameter. -> f a b -> Doc ann From 234154cedd49295c6e6d3f473f6ce2e725b94da4 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:32:31 -0400 Subject: [PATCH 52/57] Better docs for liftPretty. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 3e1c139b..22b80c21 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -387,7 +387,10 @@ instance Pretty Void where pretty = absurd -- [h, e, l, l, o] class Pretty1 f where - -- | >>> liftPretty (parens . pretty) (Just "hello") + -- | Pretty-print a container using the supplied function to print its + -- contents. + -- + -- >>> liftPretty (parens . pretty) (Just "hello") -- (hello) liftPretty :: (a -> Doc ann) -- ^ A function to print a value. From d80a5e593a150fcd1988fb8d39fedee1d14cab50 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:33:10 -0400 Subject: [PATCH 53/57] Docs for liftPretty2. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 22b80c21..6a03c263 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -449,6 +449,11 @@ instance Pretty a => Pretty1 ((,) a) where -- @ class Pretty2 f where + -- | Pretty-print a container using the supplied functions to print its + -- contents. + -- + -- >>> liftPretty2 (parens . pretty) pretty (("hello", 0) :: (String, Int)) + -- ((hello), 0) liftPretty2 :: (a -> Doc ann) -- ^ A function to print a value of the first parameter. -> (b -> Doc ann) -- ^ A function to print a value of the second parameter. From 821a8f5a6f414622c62de37129dfa8534f25144f Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:43:07 -0400 Subject: [PATCH 54/57] Use a longer example to illustrate Pretty1/Pretty for recursive types. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 6a03c263..efe4f4ae 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -385,6 +385,18 @@ instance Pretty Void where pretty = absurd -- -- >>> liftPretty pretty ("hello" :: String) -- [h, e, l, l, o] +-- +-- ==== __Examples__ +-- +-- Using 'Pretty1' to provide a (decidable!) 'Pretty' instance for a recursive +-- type: +-- +-- >>> data Expr a = Plus a a | Times a a | Const Int +-- >>> newtype Fix f = Fix (f (Fix f)) +-- >>> instance Pretty1 Expr where liftPretty p e = case e of { Plus a b -> parens (p a <+> pretty '+' <+> p b) ; Times a b -> p a <+> pretty '*' <+> p b ; Const i -> pretty i } +-- >>> instance Pretty1 f => Pretty (Fix f) where pretty (Fix f) = liftPretty pretty f +-- >>> pretty (Fix (Times (Fix (Plus (Fix (Const 1)) (Fix (Const 2)))) (Fix (Const 3)))) +-- (1 + 2) * 3 class Pretty1 f where -- | Pretty-print a container using the supplied function to print its From 12457a84b555cf33820b2e922516f130366b90df Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:44:15 -0400 Subject: [PATCH 55/57] Spacing. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index efe4f4ae..12371163 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -393,8 +393,10 @@ instance Pretty Void where pretty = absurd -- -- >>> data Expr a = Plus a a | Times a a | Const Int -- >>> newtype Fix f = Fix (f (Fix f)) +-- >>> -- >>> instance Pretty1 Expr where liftPretty p e = case e of { Plus a b -> parens (p a <+> pretty '+' <+> p b) ; Times a b -> p a <+> pretty '*' <+> p b ; Const i -> pretty i } -- >>> instance Pretty1 f => Pretty (Fix f) where pretty (Fix f) = liftPretty pretty f +-- >>> -- >>> pretty (Fix (Times (Fix (Plus (Fix (Const 1)) (Fix (Const 2)))) (Fix (Const 3)))) -- (1 + 2) * 3 class Pretty1 f where From dba8a03bf3320dd0115091c487430f7e76e8af15 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 11:55:46 -0400 Subject: [PATCH 56/57] Split up the instances over multiple lines. --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 12371163..2aaf2584 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -393,10 +393,18 @@ instance Pretty Void where pretty = absurd -- -- >>> data Expr a = Plus a a | Times a a | Const Int -- >>> newtype Fix f = Fix (f (Fix f)) --- >>> --- >>> instance Pretty1 Expr where liftPretty p e = case e of { Plus a b -> parens (p a <+> pretty '+' <+> p b) ; Times a b -> p a <+> pretty '*' <+> p b ; Const i -> pretty i } --- >>> instance Pretty1 f => Pretty (Fix f) where pretty (Fix f) = liftPretty pretty f --- >>> +-- >>> :{ +-- >>> instance Pretty1 Expr where +-- >>> liftPretty p (Plus a b) = parens (p a <+> pretty '+' <+> p b) +-- >>> liftPretty p (Times a b) = p a <+> pretty '*' <+> p b +-- >>> liftPretty _ (Const i) = pretty i +-- >>> :} +-- +-- >>> :{ +-- >>> instance Pretty1 f => Pretty (Fix f) where +-- >>> pretty (Fix f) = liftPretty pretty f +-- >>> :} +-- -- >>> pretty (Fix (Times (Fix (Plus (Fix (Const 1)) (Fix (Const 2)))) (Fix (Const 3)))) -- (1 + 2) * 3 class Pretty1 f where From b36e30677ce7af736edda93b141b7909fa1ebb9a Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 11 Oct 2017 12:03:27 -0400 Subject: [PATCH 57/57] :fire: an exclamation point. --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 2aaf2584..32d999d7 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -388,7 +388,7 @@ instance Pretty Void where pretty = absurd -- -- ==== __Examples__ -- --- Using 'Pretty1' to provide a (decidable!) 'Pretty' instance for a recursive +-- Using 'Pretty1' to provide a (decidable) 'Pretty' instance for a recursive -- type: -- -- >>> data Expr a = Plus a a | Times a a | Const Int