1 *********************************************************************************
3 * John Hughes's and Simon Peyton Jones's Pretty Printer Combinators *
5 * based on "The Design of a Pretty-printing Library" *
6 * in Advanced Functional Programming, *
7 * Johan Jeuring and Erik Meijer (eds), LNCS 925 *
8 * http://www.cs.chalmers.se/~rjmh/Papers/pretty.ps *
10 * Heavily modified by Simon Peyton Jones, Dec 96 *
12 *********************************************************************************
14 Version 2.0 24 April 1997
15 * Made empty into a left unit for <> as well as a right unit;
16 it is also now true that
18 which wasn't true before.
20 * Fixed an obscure bug in sep that occassionally gave very wierd behaviour
24 * Corrected and tidied up the laws and invariants
26 ======================================================================
27 Relative to John's original paper, there are the following new features:
29 1. There's an empty document, "empty". It's a left and right unit for
30 both <> and $$, and anywhere in the argument list for
31 sep, hcat, hsep, vcat, fcat etc.
33 It is Really Useful in practice.
35 2. There is a paragraph-fill combinator, fsep, that's much like sep,
36 only it keeps fitting things on one line until itc can't fit any more.
38 3. Some random useful extra combinators are provided.
39 <+> puts its arguments beside each other with a space between them,
40 unless either argument is empty in which case it returns the other
43 hcat is a list version of <>
44 hsep is a list version of <+>
45 vcat is a list version of $$
47 sep (separate) is either like hsep or like vcat, depending on what fits
49 cat is behaves like sep, but it uses <> for horizontal conposition
50 fcat is behaves like fsep, but it uses <> for horizontal conposition
52 These new ones do the obvious things:
53 char, semi, comma, colon, space,
54 parens, brackets, braces,
57 4. The "above" combinator, $$, now overlaps its two arguments if the
58 last line of the top argument stops before the first line of the second begins.
59 For example: text "hi" $$ nest 5 "there"
66 There are two places this is really useful
68 a) When making labelled blocks, like this:
70 Right -> code for right
72 code for longlonglonglabel
73 The block is on the same line as the label if the label is
74 short, but on the next line otherwise.
76 b) When laying out lists like this:
81 which some people like. But if the list fits on one line
82 you want [first, second, third]. You can't do this with
83 John's original combinators, but it's quite easy with the
86 The combinator $+$ gives the original "never-overlap" behaviour.
88 5. Several different renderers are provided:
90 * one that uses cut-marks to avoid deeply-nested documents
91 simply piling up in the right-hand margin
92 * one that ignores indentation (fewer chars output; good for machines)
93 * one that ignores indentation and newlines (ditto, only more so)
95 6. Numerous implementation tidy-ups
96 Use of unboxed data types to speed up the implementation
101 #include "HsVersions.h"
105 Mode(..), TextDetails(..),
110 int, integer, float, double, rational,
111 parens, brackets, braces, quotes, doubleQuotes,
112 semi, comma, colon, space, equals,
113 lparen, rparen, lbrack, rbrack, lbrace, rbrace,
115 (<>), (<+>), hcat, hsep,
122 -- renderStyle, -- Haskell 1.3 only
126 #include "HsVersions.h"
127 #if defined(__GLASGOW_HASKELL__)
131 #if __GLASGOW_HASKELL__ >= 202
137 -- Horrible import to satisfy GHC 0.29
138 import Ubiq ( Unique, Uniquable(..), Name )
143 -- Don't import Util( assertPanic ) because it makes a loop in the module structure
152 *********************************************************
154 \subsection{CPP magic so that we can compile with both GHC and Hugs}
156 *********************************************************
158 The library uses unboxed types to get a bit more speed, but these CPP macros
159 allow you to use either GHC or Hugs. To get GHC, just set the CPP variable
164 #if defined(__GLASGOW_HASKELL__)
169 -- Disable ASSERT checks; they are expensive!
170 #define LOCAL_ASSERT(x)
174 #define NEGATE negateInt#
179 #define DIV `quotInt#`
182 #if __GLASGOW_HASKELL__ >= 202
185 #define MAXINT maxBound
190 #define MAXINT maxInt
198 #define LOCAL_ASSERT(x)
203 #define NEGATE negate
212 #define MAXINT maxBound
219 *********************************************************
221 \subsection{The interface}
223 *********************************************************
225 The primitive @Doc@ values
229 text :: String -> Doc
232 semi, comma, colon, space, equals :: Doc
233 lparen, rparen, lbrack, rbrack, lbrace, rbrace :: Doc
235 parens, brackets, braces :: Doc -> Doc
236 quotes, doubleQuotes :: Doc -> Doc
239 integer :: Integer -> Doc
240 float :: Float -> Doc
241 double :: Double -> Doc
242 rational :: Rational -> Doc
245 Combining @Doc@ values
248 (<>) :: Doc -> Doc -> Doc -- Beside
249 hcat :: [Doc] -> Doc -- List version of <>
250 (<+>) :: Doc -> Doc -> Doc -- Beside, separated by space
251 hsep :: [Doc] -> Doc -- List version of <+>
253 ($$) :: Doc -> Doc -> Doc -- Above; if there is no
254 -- overlap it "dovetails" the two
255 vcat :: [Doc] -> Doc -- List version of $$
257 cat :: [Doc] -> Doc -- Either hcat or vcat
258 sep :: [Doc] -> Doc -- Either hsep or vcat
259 fcat :: [Doc] -> Doc -- ``Paragraph fill'' version of cat
260 fsep :: [Doc] -> Doc -- ``Paragraph fill'' version of sep
262 nest :: Int -> Doc -> Doc -- Nested
268 hang :: Doc -> Int -> Doc -> Doc
269 punctuate :: Doc -> [Doc] -> [Doc] -- punctuate p [d1, ... dn] = [d1 <> p, d2 <> p, ... dn-1 <> p, dn]
272 Displaying @Doc@ values.
275 instance SHOW Doc where
276 showsPrec prec doc cont = showDoc doc cont
278 render :: Doc -> String -- Uses default style
280 -> Int -- Line length
281 -> Float -- Ribbons per line
282 -> (TextDetails -> a -> a) -- What to do with text
283 -> a -- What to do at the end
287 {- When we start using 1.3
288 renderStyle :: Style -> Doc -> String
289 data Style = Style { lineLength :: Int, -- In chars
290 ribbonsPerLine :: Float, -- Ratio of ribbon length to line length
293 style :: Style -- The default style
294 style = Style { lineLength = 100, ribbonsPerLine = 2.5, mode = PageMode }
297 data Mode = PageMode -- Normal
298 | ZigZagMode -- With zig-zag cuts
299 | LeftMode -- No indentation, infinitely long lines
300 | OneLineMode -- All on one line
305 *********************************************************
307 \subsection{The @Doc@ calculus}
309 *********************************************************
311 The @Doc@ combinators satisfy the following laws:
315 <a1> (x $$ y) $$ z = x $$ (y $$ z)
323 <b1> (x <> y) <> z = x <> (y <> z)
324 <b2> empty <> x = empty
331 <t1> text s <> text t = text (s++t)
332 <t2> text "" <> x = x, if x non-empty
337 <n2> nest k (nest k' x) = nest (k+k') x
338 <n3> nest k (x <> y) = nest k z <> nest k y
339 <n4> nest k (x $$ y) = nest k x $$ nest k y
340 <n5> nest k empty = empty
341 <n6> x <> nest k y = x <> y, if x non-empty
343 ** Note the side condition on <n6>! It is this that
344 ** makes it OK for empty to be a left unit for <>.
348 <m1> (text s <> x) $$ y = text s <> ((text "" <> x)) $$
351 <m2> (x $$ y) <> z = x $$ (y <> z)
355 Laws for list versions
356 ~~~~~~~~~~~~~~~~~~~~~~
357 <l1> sep (ps++[empty]++qs) = sep (ps ++ qs)
358 ...ditto hsep, hcat, vcat, fill...
360 <l2> nest k (sep ps) = sep (map (nest k) ps)
361 ...ditto hsep, hcat, vcat, fill...
365 <o1> oneLiner (nest k p) = nest k (oneLiner p)
366 <o2> oneLiner (x <> y) = oneLiner x <> oneLiner y
370 You might think that the following verion of <m1> would
373 <3 NO> (text s <> x) $$ y = text s <> ((empty <> x)) $$
376 But it doesn't work, for if x=empty, we would have
378 text s $$ y = text s <> (empty $$ nest (-length s) y)
379 = text s <> nest (-length s) y
384 *********************************************************
386 \subsection{Simple derived definitions}
388 *********************************************************
403 int n = text (show n)
404 integer n = text (show n)
405 float n = text (show n)
406 double n = text (show n)
407 --ORIG: rational n = text (show n)
408 rational n = text (show (fromRationalX n)) -- _showRational 30 n)
410 quotes p = char '`' <> p <> char '\''
411 doubleQuotes p = char '"' <> p <> char '"'
412 parens p = char '(' <> p <> char ')'
413 brackets p = char '[' <> p <> char ']'
414 braces p = char '{' <> p <> char '}'
417 hcat = foldr (<>) empty
418 hsep = foldr (<+>) empty
419 vcat = foldr ($$) empty
421 hang d1 n d2 = d1 $$ (nest n d2)
424 punctuate p (d:ds) = go d ds
427 go d (e:es) = (d <> p) : go e es
431 *********************************************************
433 \subsection{The @Doc@ data type}
435 *********************************************************
437 A @Doc@ represents a {\em set} of layouts. A @Doc@ with
438 no occurrences of @Union@ or @NoDoc@ represents just one layout.
442 | NilAbove Doc -- text "" $$ x
443 | TextBeside TextDetails INT Doc -- text s <> x
444 | Nest INT Doc -- nest k x
445 | Union Doc Doc -- ul `union` ur
446 | NoDoc -- The empty set of documents
447 | Beside Doc Bool Doc -- True <=> space between
448 | Above Doc Bool Doc -- True <=> never overlap
450 type RDoc = Doc -- RDoc is a "reduced Doc", guaranteed not to have a top-level Above or Beside
453 reduceDoc :: Doc -> RDoc
454 reduceDoc (Beside p g q) = beside p g (reduceDoc q)
455 reduceDoc (Above p g q) = above p g (reduceDoc q)
459 data TextDetails = Chr Char
466 Here are the invariants:
469 The argument of @NilAbove@ is never @Empty@. Therefore
470 a @NilAbove@ occupies at least two lines.
473 The arugment of @TextBeside@ is never @Nest@.
476 The layouts of the two arguments of @Union@ both flatten to the same string.
479 The arguments of @Union@ are either @TextBeside@, or @NilAbove@.
482 The right argument of a union cannot be equivalent to the empty set (@NoDoc@).
483 If the left argument of a union is equivalent to the empty set (@NoDoc@),
484 then the @NoDoc@ appears in the first line.
487 An empty document is always represented by @Empty@.
488 It can't be hidden inside a @Nest@, or a @Union@ of two @Empty@s.
491 The first line of every layout in the left argument of @Union@
492 is longer than the first line of any layout in the right argument.
493 (1) ensures that the left argument has a first line. In view of (3),
494 this invariant means that the right argument must have at least two
499 -- Arg of a NilAbove is always an RDoc
500 nilAbove_ p = LOCAL_ASSERT( ok p ) NilAbove p
505 -- Arg of a TextBeside is always an RDoc
506 textBeside_ s sl p = TextBeside s sl (LOCAL_ASSERT( ok p ) p)
508 ok (Nest _ _) = False
511 -- Arg of Nest is always an RDoc
512 nest_ k p = Nest k (LOCAL_ASSERT( ok p ) p)
517 -- Args of union are always RDocs
518 union_ p q = Union (LOCAL_ASSERT( ok p ) p) (LOCAL_ASSERT( ok q ) q)
520 ok (TextBeside _ _ _) = True
521 ok (NilAbove _) = True
522 ok (Union _ _) = True
527 Notice the difference between
528 * NoDoc (no documents)
529 * Empty (one empty document; no height and no width)
530 * text "" (a document containing the empty string;
531 one line high, but has no width)
535 *********************************************************
537 \subsection{@empty@, @text@, @nest@, @union@}
539 *********************************************************
544 char c = textBeside_ (Chr c) 1# Empty
545 text s = case length s of {IBOX(sl) -> textBeside_ (Str s) sl Empty}
546 ptext s = case _LENGTH_ s of {IBOX(sl) -> textBeside_ (PStr s) sl Empty}
548 nest IBOX(k) p = mkNest k (reduceDoc p) -- Externally callable version
550 -- mkNest checks for Nest's invariant that it doesn't have an Empty inside it
551 mkNest k (Nest k1 p) = mkNest (k PLUS k1) p
552 mkNest k NoDoc = NoDoc
553 mkNest k Empty = Empty
554 mkNest ILIT(0) p = p -- Worth a try!
555 mkNest k p = nest_ k p
557 -- mkUnion checks for an empty document
558 mkUnion Empty q = Empty
559 mkUnion p q = p `union_` q
562 *********************************************************
564 \subsection{Vertical composition @$$@}
566 *********************************************************
570 p $$ q = Above p False q
571 p $+$ q = Above p True q
573 above :: Doc -> Bool -> RDoc -> RDoc
574 above (Above p g1 q1) g2 q2 = above p g1 (above q1 g2 q2)
575 above p@(Beside _ _ _) g q = aboveNest (reduceDoc p) g ILIT(0) (reduceDoc q)
576 above p g q = aboveNest p g ILIT(0) (reduceDoc q)
578 aboveNest :: RDoc -> Bool -> INT -> RDoc -> RDoc
579 -- Specfication: aboveNest p g k q = p $g$ (nest k q)
581 aboveNest NoDoc g k q = NoDoc
582 aboveNest (p1 `Union` p2) g k q = aboveNest p1 g k q `union_`
585 aboveNest Empty g k q = mkNest k q
586 aboveNest (Nest k1 p) g k q = nest_ k1 (aboveNest p g (k MINUS k1) q)
587 -- p can't be Empty, so no need for mkNest
589 aboveNest (NilAbove p) g k q = nilAbove_ (aboveNest p g k q)
590 aboveNest (TextBeside s sl p) g k q = textBeside_ s sl rest
594 Empty -> nilAboveNest g k1 q
595 other -> aboveNest p g k1 q
599 nilAboveNest :: Bool -> INT -> RDoc -> RDoc
600 -- Specification: text s <> nilaboveNest g k q
601 -- = text s <> (text "" $g$ nest k q)
603 nilAboveNest g k Empty = Empty -- Here's why the "text s <>" is in the spec!
604 nilAboveNest g k (Nest k1 q) = nilAboveNest g (k PLUS k1) q
606 nilAboveNest g k q | (not g) && (k GR ILIT(0)) -- No newline if no overlap
607 = textBeside_ (Str (spaces k)) k q
608 | otherwise -- Put them really above
609 = nilAbove_ (mkNest k q)
613 *********************************************************
615 \subsection{Horizontal composition @<>@}
617 *********************************************************
620 p <> q = Beside p False q
621 p <+> q = Beside p True q
623 beside :: Doc -> Bool -> RDoc -> RDoc
624 -- Specification: beside g p q = p <g> q
626 beside NoDoc g q = NoDoc
627 beside (p1 `Union` p2) g q = (beside p1 g q) `union_` (beside p2 g q)
629 beside (Nest k p) g q = nest_ k (beside p g q) -- p non-empty
630 beside p@(Beside p1 g1 q1) g2 q2
631 {- (A `op1` B) `op2` C == A `op1` (B `op2` C) iff op1 == op2
632 [ && (op1 == <> || op1 == <+>) ] -}
633 | g1 == g2 = beside p1 g1 (beside q1 g2 q2)
634 | otherwise = beside (reduceDoc p) g2 q2
635 beside p@(Above _ _ _) g q = beside (reduceDoc p) g q
636 beside (NilAbove p) g q = nilAbove_ (beside p g q)
637 beside (TextBeside s sl p) g q = textBeside_ s sl rest
640 Empty -> nilBeside g q
641 other -> beside p g q
645 nilBeside :: Bool -> RDoc -> RDoc
646 -- Specification: text "" <> nilBeside g p
649 nilBeside g Empty = Empty -- Hence the text "" in the spec
650 nilBeside g (Nest _ p) = nilBeside g p
651 nilBeside g p | g = textBeside_ space_text ILIT(1) p
655 *********************************************************
657 \subsection{Separate, @sep@, Hughes version}
659 *********************************************************
662 -- Specification: sep ps = oneLiner (hsep ps)
666 sep = sepX True -- Separate with spaces
667 cat = sepX False -- Don't
670 sepX x (p:ps) = sep1 x (reduceDoc p) ILIT(0) ps
673 -- Specification: sep1 g k ys = sep (x : map (nest k) ys)
674 -- = oneLiner (x <g> nest k (hsep ys))
675 -- `union` x $$ nest k (vcat ys)
677 sep1 :: Bool -> RDoc -> INT -> [Doc] -> RDoc
678 sep1 g NoDoc k ys = NoDoc
679 sep1 g (p `Union` q) k ys = sep1 g p k ys
681 (aboveNest q False k (reduceDoc (vcat ys)))
683 sep1 g Empty k ys = mkNest k (sepX g ys)
684 sep1 g (Nest n p) k ys = nest_ n (sep1 g p (k MINUS n) ys)
686 sep1 g (NilAbove p) k ys = nilAbove_ (aboveNest p False k (reduceDoc (vcat ys)))
687 sep1 g (TextBeside s sl p) k ys = textBeside_ s sl (sepNB g p (k MINUS sl) ys)
689 -- Specification: sepNB p k ys = sep1 (text "" <> p) k ys
690 -- Called when we have already found some text in the first item
691 -- We have to eat up nests
693 sepNB g (Nest _ p) k ys = sepNB g p k ys
695 sepNB g Empty k ys = oneLiner (nilBeside g (reduceDoc rest))
697 nilAboveNest False k (reduceDoc (vcat ys))
700 | otherwise = hcat ys
702 sepNB g p k ys = sep1 g p k ys
705 *********************************************************
709 *********************************************************
718 -- fill (p1:p2:ps) = oneLiner p1 <#> nest (length p1)
719 -- (fill (oneLiner p2 : ps))
724 fill g (p:ps) = fill1 g (reduceDoc p) ILIT(0) ps
727 fill1 :: Bool -> RDoc -> INT -> [Doc] -> Doc
728 fill1 g NoDoc k ys = NoDoc
729 fill1 g (p `Union` q) k ys = fill1 g p k ys
731 (aboveNest q False k (fill g ys))
733 fill1 g Empty k ys = mkNest k (fill g ys)
734 fill1 g (Nest n p) k ys = nest_ n (fill1 g p (k MINUS n) ys)
736 fill1 g (NilAbove p) k ys = nilAbove_ (aboveNest p False k (fill g ys))
737 fill1 g (TextBeside s sl p) k ys = textBeside_ s sl (fillNB g p (k MINUS sl) ys)
739 fillNB g (Nest _ p) k ys = fillNB g p k ys
740 fillNB g Empty k [] = Empty
741 fillNB g Empty k (y:ys) = nilBeside g (fill1 g (oneLiner (reduceDoc y)) k1 ys)
743 nilAboveNest False k (fill g (y:ys))
745 k1 | g = k MINUS ILIT(1)
748 fillNB g p k ys = fill1 g p k ys
752 *********************************************************
754 \subsection{Selecting the best layout}
756 *********************************************************
760 -> Int -- Line length
761 -> Int -- Ribbon length
763 -> RDoc -- No unions in here!
765 best OneLineMode IBOX(w) IBOX(r) p
770 get (NilAbove p) = nilAbove_ (get p)
771 get (TextBeside s sl p) = textBeside_ s sl (get p)
772 get (Nest k p) = get p -- Elide nest
773 get (p `Union` q) = first (get p) (get q)
775 best mode IBOX(w) IBOX(r) p
778 get :: INT -- (Remaining) width of line
782 get w (NilAbove p) = nilAbove_ (get w p)
783 get w (TextBeside s sl p) = textBeside_ s sl (get1 w sl p)
784 get w (Nest k p) = nest_ k (get (w MINUS k) p)
785 get w (p `Union` q) = nicest w r (get w p) (get w q)
787 get1 :: INT -- (Remaining) width of line
788 -> INT -- Amount of first line already eaten up
789 -> Doc -- This is an argument to TextBeside => eat Nests
790 -> Doc -- No unions in here!
792 get1 w sl Empty = Empty
793 get1 w sl NoDoc = NoDoc
794 get1 w sl (NilAbove p) = nilAbove_ (get (w MINUS sl) p)
795 get1 w sl (TextBeside t tl p) = textBeside_ t tl (get1 w (sl PLUS tl) p)
796 get1 w sl (Nest k p) = get1 w sl p
797 get1 w sl (p `Union` q) = nicest1 w r sl (get1 w sl p)
800 nicest w r p q = nicest1 w r ILIT(0) p q
801 nicest1 w r sl p q | fits ((w `minn` r) MINUS sl) p = p
804 fits :: INT -- Space available
806 -> Bool -- True if *first line* of Doc fits in space available
808 fits n p | n LT ILIT(0) = False
811 fits n (NilAbove _) = True
812 fits n (TextBeside _ sl p) = fits (n MINUS sl) p
814 minn x y | x LT y = x
818 @first@ and @nonEmptySet@ are similar to @nicest@ and @fits@, only simpler.
819 @first@ returns its first argument if it is non-empty, otherwise its second.
822 first p q | nonEmptySet p = p
825 nonEmptySet NoDoc = False
826 nonEmptySet (p `Union` q) = True
827 nonEmptySet Empty = True
828 nonEmptySet (NilAbove p) = True -- NoDoc always in first line
829 nonEmptySet (TextBeside _ _ p) = nonEmptySet p
830 nonEmptySet (Nest _ p) = nonEmptySet p
833 @oneLiner@ returns the one-line members of the given set of @Doc@s.
836 oneLiner :: Doc -> Doc
837 oneLiner NoDoc = NoDoc
838 oneLiner Empty = Empty
839 oneLiner (NilAbove p) = NoDoc
840 oneLiner (TextBeside s sl p) = textBeside_ s sl (oneLiner p)
841 oneLiner (Nest k p) = nest_ k (oneLiner p)
842 oneLiner (p `Union` q) = oneLiner p
847 *********************************************************
849 \subsection{Displaying the best layout}
851 *********************************************************
856 renderStyle Style{mode, lineLength, ribbonsPerLine} doc
857 = fullRender mode lineLength ribbonsPerLine doc ""
860 render doc = showDoc doc ""
861 showDoc doc rest = fullRender PageMode 100 1.5 string_txt rest doc
863 string_txt (Chr c) s = c:s
864 string_txt (Str s1) s2 = s1 ++ s2
865 string_txt (PStr s1) s2 = _UNPK_ s1 ++ s2
870 fullRender OneLineMode _ _ txt end doc = easy_display space_text txt end (reduceDoc doc)
871 fullRender LeftMode _ _ txt end doc = easy_display nl_text txt end (reduceDoc doc)
873 fullRender mode line_length ribbons_per_line txt end doc
874 = display mode line_length ribbon_length txt end best_doc
876 best_doc = best mode hacked_line_length ribbon_length (reduceDoc doc)
878 hacked_line_length, ribbon_length :: Int
879 ribbon_length = round (fromInt line_length / ribbons_per_line)
880 hacked_line_length = case mode of { ZigZagMode -> MAXINT; other -> line_length }
882 display mode IBOX(page_width) IBOX(ribbon_width) txt end doc
883 = case page_width MINUS ribbon_width of { gap_width ->
884 case gap_width DIV ILIT(2) of { shift ->
886 lay k (Nest k1 p) = lay (k PLUS k1) p
889 lay k (NilAbove p) = nl_text `txt` lay k p
891 lay k (TextBeside s sl p)
893 ZigZagMode | k GREQ gap_width
895 Str (multi_ch shift '/') `txt` (
897 lay1 (k MINUS shift) s sl p)))
901 Str (multi_ch shift '\\') `txt` (
903 lay1 (k PLUS shift) s sl p )))
905 other -> lay1 k s sl p
907 lay1 k s sl p = Str (indent k) `txt` (s `txt` lay2 (k PLUS sl) p)
909 lay2 k (NilAbove p) = nl_text `txt` lay k p
910 lay2 k (TextBeside s sl p) = s `txt` (lay2 (k PLUS sl) p)
911 lay2 k (Nest _ p) = lay2 k p
917 cant_fail = error "easy_display: NoDoc"
918 easy_display nl_text txt end doc
921 lay NoDoc no_doc = no_doc
922 lay (Union p q) no_doc = {- lay p -} (lay q cant_fail) -- Second arg can't be NoDoc
923 lay (Nest k p) no_doc = lay p no_doc
924 lay Empty no_doc = end
925 lay (NilAbove p) no_doc = nl_text `txt` lay p cant_fail -- NoDoc always on first line
926 lay (TextBeside s sl p) no_doc = s `txt` lay p no_doc
928 indent n | n GREQ ILIT(8) = '\t' : indent (n MINUS ILIT(8))
929 | otherwise = spaces n
931 multi_ch ILIT(0) ch = ""
932 multi_ch n ch = ch : multi_ch (n MINUS ILIT(1)) ch
935 spaces n = ' ' : spaces (n MINUS ILIT(1))
938 Doesn't really belong here..
940 -----------------------------------
943 fromRationalX :: (RealFloat a) => Rational -> a
947 h = ceiling (huge `asTypeOf` x)
948 b = toInteger (floatRadix x)
951 let d = denominator r'
954 let e = integerLogBase b (d `div` h) + 1
955 in fromRat (e0-e) (n % (d `div` (b^e)))
956 else if abs n > h then
957 let e = integerLogBase b (abs n `div` h) + 1
958 in fromRat (e0+e) ((n `div` (b^e)) % d)
960 scaleFloat e0 (fromRational r')
963 -- Compute the discrete log of i in base b.
964 -- Simplest way would be just divide i by b until it's smaller then b, but that would
965 -- be very slow! We are just slightly more clever.
966 integerLogBase :: Integer -> Integer -> Int
971 -- Try squaring the base first to cut down the number of divisions.
972 let l = 2 * integerLogBase (b*b) i
974 doDiv :: Integer -> Int -> Int
975 doDiv j k = if j < b then k else doDiv (j `div` b) (k+1)
977 doDiv (i `div` (b^l)) l
982 -- Compute smallest and largest floating point values.
984 tiny :: (RealFloat a) => a
986 let (l, _) = floatRange x
987 x = encodeFloat 1 (l-1)
991 huge :: (RealFloat a) => a
993 let (_, u) = floatRange x
995 x = encodeFloat (floatRadix x ^ d - 1) (u - d)