[Haskell-cafe] Pretty-printing haskell source code - could the line breaking be improved?

David Fox dsf at seereason.com
Mon Apr 18 18:56:05 UTC 2016


When I pretty print a certain deleration (with default width) I get this:

          upeekRow _unv (_xconc@(ImageSize {})) = Node (upeekCons idPath
Nothing) (concatMap (\f -> forestMap (liftPeek f) (map (\x' -> Node
(upeekCons idPath (Just (u x' :: Univ)) :: UPeek Univ


                                      Dimension) []) (toListOf (toLens (f
idPath) . ulens' (Proxy :: Proxy Univ)) _xconc :: [Dimension])))
[UPath_ImageSize_dim] ++ (concatMap (\f -> forestMap (liftPeek f) (map (\x'
-> Node (upeekCons idPath (Just (u x' :: Univ)) :: UPeek Univ





                                            Double) []) (toListOf (toLens
(f idPath) . ulens' (Proxy :: Proxy Univ)) _xconc :: [Double])))
[UPath_ImageSize_size] ++ (concatMap (\f -> forestMap (liftPeek f) (map
(\x' -> Node (upeekCons idPath (Just (u x' :: Univ)) :: UPeek Univ








                                             Units) []) (toListOf (toLens
(f idPath) . ulens' (Proxy :: Proxy Univ)) _xconc :: [Units])))
[UPath_ImageSize_units] ++ [])))

However, what I would like to see is something closer to this:

          upeekRow _unv (_xconc@(ImageSize {})) =
              Node (upeekCons idPath Nothing)
                   (concatMap (\f -> forestMap (liftPeek f) (map (\x' ->
Node (upeekCons idPath (Just (u x' :: Univ)) :: UPeek Univ Dimension) [])
                                                                 (toListOf
(toLens (f idPath) . ulens' (Proxy :: Proxy Univ)) _xconc :: [Dimension])))
                              [UPath_ImageSize_dim] ++
                    (concatMap (\f -> forestMap (liftPeek f) (map (\x' ->
Node (upeekCons idPath (Just (u x' :: Univ)) :: UPeek Univ Double) [])
                                                                  (toListOf
(toLens (f idPath) . ulens' (Proxy :: Proxy Univ)) _xconc :: [Double])))
                               [UPath_ImageSize_size] ++
                     (concatMap (\f -> forestMap (liftPeek f) (map (\x' ->
Node (upeekCons idPath (Just (u x' :: Univ)) :: UPeek Univ Units) [])

 (toListOf (toLens (f idPath) . ulens' (Proxy :: Proxy Univ)) _xconc ::
[Units])))
                                [UPath_ImageSize_units] ++ [])))

So I guess what I'm seeing is that as it is traversing the syntax tree when
it finds it is too far to the right it just inserts a line break - but
since it is so far down the tree it only buys it a few spaces, whereas if
it had backtracked a few levels up the tree the new position would be much
further to the left.  Could anyone point me towards the code that
implements this decision making?  I'm pretty sure it is in the display
function Text.PrettyPrint.Annotated.HughesPJ, but the code there doesn't
make much sense to me.  Any thoughts would be appreciated.

David Fox
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160418/9e62c576/attachment.html>


More information about the Haskell-Cafe mailing list