I am using the Prettier Printer implementation from the contrib
library in Idris
.
When I fold with the |//|
operator on a list of Doc
s, the performance quickly explodes, i.e. the following code doesn't terminate before I lose my patience:
*IdrisFMT\PrettyDocs> :exec toString 0 15 $ fold (|//|) $ map text $ words "this is a long sentence with a lot of words that I can use for testing the performance of the prettier printer implementation. I need a few more words to prove my point, though."
Please note, the above fold is equal to the pre-defined combinator fillCat
.
If I instead use the pre-defined cat
combinator (= group . vcat
), it terminates within a second:
*IdrisFMT\PrettyDocs> :exec toString 0 15 $ cat $ map text $ words "this is a long sentence with a lot of words that I can use for testing the performance of the prettier printer implementation. I need a few more words to prove my point, though."
"this\nis\na\nlong\nsentence\nwith\na\nlot\nof\nwords\nthat\nI\ncan\nuse\nfor\ntesting\nthe\nperformance\nof\nthe\nprettier\nprinter\nimplementation.\nI\nneed\na\nfew\nmore\nwords\nto\nprove\nmy\npoint,\nthough."
The Doc
adt version of cat $ map text $ words "this is a long sentence with a lot of words that I can use for testing the performance of the prettier printer implementation. I need a few more words to prove my point, though."
can be seen here: https://pastebin.com/4AJWcGnD
I understand that the cat
combinator solves a much simpler problem, but I fail to see how fillCat
is so complex as to never terminate.
Could this be caused by a mistake in the implementation, or am I simply constructing too complex a document?
EDIT
The definition of the |//|
operator is found here:
https://github.com/idris-lang/Idris-dev/blob/master/libs/contrib/Text/PrettyPrint/WL/Combinators.idr#L65-L69
||| The document `(x |//| y)` concatenates document `x` and `y` with
||| a 'softbreak' in between. This effectively puts `x` and `y` either
||| right next to each other or underneath each other.
(|//|) : Doc -> Doc -> Doc
(|//|) = concatDoc softBreak
++
has on lists when folded. IIRCfoldl' (++) [] listOfStrings
has a bad performance. Perhapsfold (|//|)
has a similar issue? – SpleenwortIdris
, so I'm not even sure the same holds true here (afaik, strings are not lists inIdris
). – Retroversion|//|
. Also does the same problem happen if you compile instead of execute inside the interpreter? – CalisayafillCat
used directly? – Paolapaolina