the-sett / elm-pretty-printer Goto Github PK
View Code? Open in Web Editor NEWLicense: BSD 3-Clause "New" or "Revised" License
License: BSD 3-Clause "New" or "Revised" License
Using a thunk \() -> ...
may result in slow javascript code.
Instead do:
type Doc
= Empty
| Concatenate Doc Doc
| Nest Int Doc
| Flatten Doc -- Flatten the doc later.
... -- More constructors
flatten : Doc -> Doc
flatten doc =
case doc of
Concatenate doc1 doc2 ->
Concatenate (Flatten doc1) (Flatten doc2)
Nest i doc1 ->
Nest i (Flatten doc1)
... -- More cases but they were already tail recursive.
someFunOverDocs doc =
case someDoc of
Flatten doc -> someFunOverDocs (flatten doc) -- lazily apply the flatten.
.... -- Other cases as normal.
Now that flatten
has been made lazy, the best
function could be optimised to incrementally check the line fit without completely flattening the doc.
Should be able to reject large expressions fitted onto a single line sooner that way.
It doesn't look trivial to implement this optimization though, so I'll just leave it in this issue for now until it seems worth trying to implement it.
When laying out a long list and choosing between putting it on a single line versus splitting into multiple lines, the flatten
function is used to do the flattening onto a single line. This function is not tail-recursive so will overflow the stack when too many docs are being Concatenate
d together.
The part that is not TR is:
flatten : Doc -> Doc
flatten doc =
case doc of
Concatenate doc1 doc2 ->
Concatenate (flatten doc1) (flatten doc2)
Nest i doc1 ->
Nest i (flatten doc1)
Hi @rupertlssmith, for my port of the Elm compiler from Haskell to Elm I need a function like
setTag : t -> Doc t -> Doc t
which sets the tag for all the strings in an already assembled document.
If you like, I could create a pull request.
Instead of or additionally to this function I could implement a function like
updateTag : (Maybe t -> Maybe t) -> Doc t -> Doc t
or even
updateTag : (String -> Maybe t -> Maybe t) -> Doc t -> Doc t
which would give more control over the tag replacement, but I'm not sure whether something like this would be needed.
What do you think?
I am making a pretty printer for a GHC core AST and thought that making the space between terms in an application a softline would be a good idea. However, it seems that the deepest nested softline is favoured to become a newline.
$wtest :: ByteArray# -> Int# -> Int# -> Int#
$wtest ww ww ww = let end = +# ww ww
in case $wcaseConvert upperMapping type (\i -> case i of
{ I# ipv -> case >=# ipv end of
{ 1 -> Done type type
lwild -> case indexWord16Array# ww ipv of
{ r# -> case geWord16# r# 55296 of
{ 1 -> case leWord16# r# 56319 of
{ 1 -> case indexWord16Array# ww (+# ipv 1) of
{ r#1 -> Yield type type (C# (chr# (+# 9216 (+#
(uncheckedIShiftL#
(-#
(word2Int#
(word16ToWord#
r#))
55296) 10)
(word2Int#
(word16ToWord#
r#1))))))
(I# (+# ipv 2))
}
lwild2 -> Yield type type (C# (chr# (word2Int# (word16ToWord#
r#)))) (I# (+#
ipv
1))
}
lwild1 -> Yield type type (C# (chr# (word2Int# (word16ToWord# r#))))
(I# (+# ipv 1))
}
}
Is there a way to give the first softline precedent for considering as a newline?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.