New patches: [fillNB bug, lazy vcat benedikt.huber@gmail.com**20080624113715] { hunk ./Text/PrettyPrint/HughesPJ.hs 410 + +** because of law n6, t2 only holds if x doesn't +** start with `nest'. + hunk ./Text/PrettyPrint/HughesPJ.hs 429 - (text s <> x) $$ y = text s <> ((text "" <> x)) $$ - nest (-length s) y) + (text s <> x) $$ y = text s <> ((text "" <> x) $$ + nest (-length s) y) hunk ./Text/PrettyPrint/HughesPJ.hs 490 +-- lazy list versions +hcat = reduceAB . foldr (beside_' False) empty +hsep = reduceAB . foldr (beside_' True) empty +vcat = reduceAB . foldr (above_' True) empty hunk ./Text/PrettyPrint/HughesPJ.hs 495 -hcat = foldr (<>) empty -hsep = foldr (<+>) empty -vcat = foldr ($$) empty +beside_' :: Bool -> Doc -> Doc -> Doc +beside_' _ p Empty = p +beside_' g p q = Beside p g q + +above_' :: Bool -> Doc -> Doc -> Doc +above_' _ p Empty = p +above_' g p q = Above p g q + +reduceAB :: Doc -> Doc +reduceAB (Above Empty _ q) = q +reduceAB (Beside Empty _ q) = q +reduceAB doc = doc hunk ./Text/PrettyPrint/HughesPJ.hs 556 - * The arugment of @TextBeside@ is never @Nest@. + * The argument of @TextBeside@ is never @Nest@. hunk ./Text/PrettyPrint/HughesPJ.hs 564 - * The right argument of a union cannot be equivalent to the empty set - (@NoDoc@). If the left argument of a union is equivalent to the - empty set (@NoDoc@), then the @NoDoc@ appears in the first line. + * A @NoDoc@ may only appear on the first line of the left argument of an + union. Therefore, the right argument of an union can never be equivalent + to the empty set (@NoDoc@). hunk ./Text/PrettyPrint/HughesPJ.hs 578 - -- Arg of a NilAbove is always an RDoc -nilAbove_ :: Doc -> Doc +-- Invariant: Args to the 4 functions below are always RDocs +nilAbove_ :: RDoc -> RDoc hunk ./Text/PrettyPrint/HughesPJ.hs 583 -textBeside_ :: TextDetails -> Int -> Doc -> Doc +textBeside_ :: TextDetails -> Int -> RDoc -> RDoc hunk ./Text/PrettyPrint/HughesPJ.hs 586 - -- Arg of Nest is always an RDoc -nest_ :: Int -> Doc -> Doc +nest_ :: Int -> RDoc -> RDoc hunk ./Text/PrettyPrint/HughesPJ.hs 589 - -- Args of union are always RDocs -union_ :: Doc -> Doc -> Doc +union_ :: RDoc -> RDoc -> RDoc hunk ./Text/PrettyPrint/HughesPJ.hs 765 - nilAboveNest False k (reduceDoc (vcat ys)) + nilAboveNest True k (reduceDoc (vcat ys)) hunk ./Text/PrettyPrint/HughesPJ.hs 810 -fillNB g Empty k (y:ys) = nilBeside g (fill1 g (oneLiner (reduceDoc y)) k1 ys) +fillNB g Empty k (Empty:ys) = fillNB g Empty k ys +fillNB g Empty k (y:ys) = fillNBE g k y ys +fillNB g p k ys = fill1 g p k ys + +fillNBE g k y ys = nilBeside g (fill1 g (oneLiner (reduceDoc y)) k1 ys) hunk ./Text/PrettyPrint/HughesPJ.hs 816 - nilAboveNest False k (fill g (y:ys)) + nilAboveNest True k (fill g (y:ys)) hunk ./Text/PrettyPrint/HughesPJ.hs 821 -fillNB g p k ys = fill1 g p k ys - - hunk ./Text/PrettyPrint/HughesPJ.hs 1039 --- It should never be called with 'n' < 0, but that can happen for reasons I don't understand --- Here's a test case: --- ncat x y = nest 4 $ cat [ x, y ] --- d1 = foldl1 ncat $ take 50 $ repeat $ char 'a' --- d2 = parens $ sep [ d1, text "+" , d1 ] --- main = print d2 --- I don't feel motivated enough to find the Real Bug, so meanwhile we just test for n<=0 +-- returns the empty string on negative argument. +-- hunk ./Text/PrettyPrint/HughesPJ.hs 1045 -{- Comments from Johannes Waldmann about what the problem might be: +{- +Concerning negative indentation: +If we compose a <> b, and the first line of b is deeply nested, but other lines of b are not, +then, because <> eats the nest, the pretty printer will try to layout some of b's lines with +negative indentation: hunk ./Text/PrettyPrint/HughesPJ.hs 1051 - In the example above, d2 and d1 are deeply nested, but `text "+"' is not, - so the layout function tries to "out-dent" it. - - when I look at the Doc values that are generated, there are lots of - Nest constructors with negative arguments. see this sample output of - d1 (obtained with hugs, :s -u) - - tBeside (TextDetails_Chr 'a') 1 Doc_Empty) (Doc_NilAbove (Doc_Nest - (-241) (Doc_TextBeside (TextDetails_Chr 'a') 1 Doc_Empty))))) - (Doc_NilAbove (Doc_Nest (-236) (Doc_TextBeside (TextDetails_Chr 'a') 1 - (Doc_NilAbove (Doc_Nest (-5) (Doc_TextBeside (TextDetails_Chr 'a') 1 - Doc_Empty)))))))) (Doc_NilAbove (Doc_Nest (-231) (Doc_TextBeside - (TextDetails_Chr 'a') 1 (Doc_NilAbove (Doc_Nest (-5) (Doc_TextBeside - (TextDetails_Chr 'a') 1 (Doc_NilAbove (Doc_Nest (-5) (Doc_TextBeside - (TextDetails_Chr 'a') 1 Doc_Empty))))))))))) (Doc_NilAbove (Doc_Nest +doc |0123345 +------------------ +d1 |a +d2 | b + |c +d1<>d2 |ab + c| } Context: [Fix warnings Ian Lynagh **20080620135156] [TAG 2008-05-28 Ian Lynagh **20080528004408] Patch bundle hash: 307bcd155c028e879e83ee3a20011ab92fbcea33