Conversation
|
Before merging this I would like to figure out how pipelines are impacted depending on whether they use |
| FlatAlt a b -> case changesUponFlattening b of | ||
| Flattened b' -> Union b' a | ||
| AlreadyFlat -> Union b a | ||
| NeverFlat -> a |
There was a problem hiding this comment.
I should add some documentation that explains why Union and FlatAlt are treated specially.
|
Here's a slightly different take that should be pretty fast with --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs
+++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs
@@ -518,11 +518,16 @@ hardline = Line
-- See 'vcat', 'line', or 'flatAlt' for examples that are related, or make good
-- use of it.
group :: Doc ann -> Doc ann
--- See note [Group: special flattening]
-group x = case changesUponFlattening x of
- Flattened x' -> Union x' x
- AlreadyFlat -> x
- NeverFlat -> x
+-- See note [Group: special flattening] -- TODO
+group x = case x of
+ Union{} -> x
+ FlatAlt a b -> Union (flat b) a
+ _ -> Union (flat x) x
+ where
+ flat a = case changesUponFlattening a of
+ Flattened a' -> a'
+ AlreadyFlat -> a
+ NeverFlat -> Fail
-- Note [Group: special flattening]
--
@@ -563,6 +568,7 @@ changesUponFlattening :: Doc ann -> FlattenResult (Doc ann)
changesUponFlattening = \doc -> case doc of
FlatAlt _ y -> Flattened (flatten y)
Line -> NeverFlat
+ Union Fail _ -> NeverFlat
Union x _ -> Flattened x
Nest i x -> fmap (Nest i) (changesUponFlattening x)
Annotated ann x -> fmap (Annotated ann) (changesUponFlattening x)
@@ -1823,6 +1829,7 @@ layoutWadlerLeijen
-> SimpleDocStream ann -- ^ Choice A. Invariant: first lines should not be longer than B's.
-> SimpleDocStream ann -- ^ Choice B.
-> SimpleDocStream ann -- ^ Choice A if it fits, otherwise B.
+ selectNicer _ _ SFail y = y
selectNicer lineIndent currentColumn x y = case pWidth of
AvailablePerLine lineLength ribbonFraction
| fits pWidth minNestingLevel availableWidth x -> xThat turns out to slow down prettyprinting in Right now I believe it's better to optimize for |
9a02f22 to
ded11b5
Compare
|
For the record, this is the new output for the motivating examples from #115 and #120: > diag $ group (flatAlt "Default" "Fallback")
Union ( Text 8 "Fallback" ) ( Text 7 "Default" )
> diag $ group . group . group $ "x" <> line
Union
( Cat ( Char 'x' ) ( Char ' ' ) )
( Cat ( Char 'x' )
( FlatAlt Line ( Char ' ' ) )
)For the second example, we might still want to try pushing the |
This results in a significant speedup in the usual dhall benchmark.
Includes #123.
Closes #115.