haskell-prettyprinter / haskell-prettyprinter/prettyprinter

Choice between non-flat documents

Open
#152 3 comments 1 reaction 0 assignees View on GitHub
Question or comment
Dominant language
Haskell
Stars
309
Forks
43
Avg merge
21h 25m
Merged PRs (30d)
1

Description

How do I construct a document that renders as

```
abcd
efgh
```

when the page is wide enough, or alternatively as

```
ab
cd
ef
gh
```

?

Using the public API I would try this:

```haskell
x = "abcd" <> hardline <> "efgh"
y = concatWith (\a b -> a <> hardline <> b) ["ab", "cd", "ef", "gh"]
doc = group (flatAlt y x)
```

But this will always render `y`:

```
> putDoc doc
ab
cd
ef
gh
```

…because `group` attempts and fails to flatten the preferred version:

https://github.com/quchen/prettyprinter/blob/57ced7bf35ef581a247d79a4a1b803b384845cc4/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs#L580-L585

https://github.com/quchen/prettyprinter/blob/57ced7bf35ef581a247d79a4a1b803b384845cc4/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs#L627-L628

https://github.com/quchen/prettyprinter/blob/57ced7bf35ef581a247d79a4a1b803b384845cc4/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs#L653-L657

This is an interesting difference to `ansi-wl-pprint` which [doesn't](https://github.com/ekmett/ansi-wl-pprint/blob/1d56efff10d30810ac58779f3e7edfa573f9add8/Text/PrettyPrint/ANSI/Leijen/Internal.hs#L727-L753) attempt to flatten the "nice" `FlatAlt` branch.

If I resort to the internals, it's easy:

```haskell
doc' = Union x y
```

```
> putDocW 4 doc'
abcd
efgh
```

```
> putDocW 2 doc'
ab
cd
ef
gh
```

Is there a different way to achieve this with the public API? Is this simply a problem that doesn't arise in the "real world"?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.