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__)
132 #if __GLASGOW_HASKELL__ >= 202
139 import Util ( assertPanic )
148 *********************************************************
150 \subsection{CPP magic so that we can compile with both GHC and Hugs}
152 *********************************************************
154 The library uses unboxed types to get a bit more speed, but these CPP macros
155 allow you to use either GHC or Hugs. To get GHC, just set the CPP variable
160 #if defined(__GLASGOW_HASKELL__)
165 -- Disable ASSERT checks; they are expensive!
166 #define LOCAL_ASSERT(x)
170 #define NEGATE negateInt#
175 #define DIV `quotInt#`
178 #if __GLASGOW_HASKELL__ >= 202
181 #define MAXINT maxBound
186 #define MAXINT maxInt
194 #define LOCAL_ASSERT(x)
199 #define NEGATE negate
208 #define MAXINT maxBound
215 *********************************************************
217 \subsection{The interface}
219 *********************************************************
221 The primitive @Doc@ values
225 text :: String -> Doc
228 semi, comma, colon, space, equals :: Doc
229 lparen, rparen, lbrack, rbrack, lbrace, rbrace :: Doc
231 parens, brackets, braces :: Doc -> Doc
232 quotes, doubleQuotes :: Doc -> Doc
235 integer :: Integer -> Doc
236 float :: Float -> Doc
237 double :: Double -> Doc
238 rational :: Rational -> Doc
241 Combining @Doc@ values
244 (<>) :: Doc -> Doc -> Doc -- Beside
245 hcat :: [Doc] -> Doc -- List version of <>
246 (<+>) :: Doc -> Doc -> Doc -- Beside, separated by space
247 hsep :: [Doc] -> Doc -- List version of <+>
249 ($$) :: Doc -> Doc -> Doc -- Above; if there is no
250 -- overlap it "dovetails" the two
251 vcat :: [Doc] -> Doc -- List version of $$
253 cat :: [Doc] -> Doc -- Either hcat or vcat
254 sep :: [Doc] -> Doc -- Either hsep or vcat
255 fcat :: [Doc] -> Doc -- ``Paragraph fill'' version of cat
256 fsep :: [Doc] -> Doc -- ``Paragraph fill'' version of sep
258 nest :: Int -> Doc -> Doc -- Nested
264 hang :: Doc -> Int -> Doc -> Doc
265 punctuate :: Doc -> [Doc] -> [Doc] -- punctuate p [d1, ... dn] = [d1 <> p, d2 <> p, ... dn-1 <> p, dn]
268 Displaying @Doc@ values.
271 instance SHOW Doc where
272 showsPrec prec doc cont = showDoc doc cont
274 render :: Doc -> String -- Uses default style
276 -> Int -- Line length
277 -> Float -- Ribbons per line
278 -> (TextDetails -> a -> a) -- What to do with text
279 -> a -- What to do at the end
283 {- When we start using 1.3
284 renderStyle :: Style -> Doc -> String
285 data Style = Style { lineLength :: Int, -- In chars
286 ribbonsPerLine :: Float, -- Ratio of ribbon length to line length
289 style :: Style -- The default style
290 style = Style { lineLength = 100, ribbonsPerLine = 2.5, mode = PageMode }
293 data Mode = PageMode -- Normal
294 | ZigZagMode -- With zig-zag cuts
295 | LeftMode -- No indentation, infinitely long lines
296 | OneLineMode -- All on one line
301 *********************************************************
303 \subsection{The @Doc@ calculus}
305 *********************************************************
307 The @Doc@ combinators satisfy the following laws:
311 <a1> (x $$ y) $$ z = x $$ (y $$ z)
319 <b1> (x <> y) <> z = x <> (y <> z)
320 <b2> empty <> x = empty
327 <t1> text s <> text t = text (s++t)
328 <t2> text "" <> x = x, if x non-empty
333 <n2> nest k (nest k' x) = nest (k+k') x
334 <n3> nest k (x <> y) = nest k z <> nest k y
335 <n4> nest k (x $$ y) = nest k x $$ nest k y
336 <n5> nest k empty = empty
337 <n6> x <> nest k y = x <> y, if x non-empty
339 ** Note the side condition on <n6>! It is this that
340 ** makes it OK for empty to be a left unit for <>.
344 <m1> (text s <> x) $$ y = text s <> ((text "" <> x)) $$
347 <m2> (x $$ y) <> z = x $$ (y <> z)
351 Laws for list versions
352 ~~~~~~~~~~~~~~~~~~~~~~
353 <l1> sep (ps++[empty]++qs) = sep (ps ++ qs)
354 ...ditto hsep, hcat, vcat, fill...
356 <l2> nest k (sep ps) = sep (map (nest k) ps)
357 ...ditto hsep, hcat, vcat, fill...
361 <o1> oneLiner (nest k p) = nest k (oneLiner p)
362 <o2> oneLiner (x <> y) = oneLiner x <> oneLiner y
366 You might think that the following verion of <m1> would
369 <3 NO> (text s <> x) $$ y = text s <> ((empty <> x)) $$
372 But it doesn't work, for if x=empty, we would have
374 text s $$ y = text s <> (empty $$ nest (-length s) y)
375 = text s <> nest (-length s) y
380 *********************************************************
382 \subsection{Simple derived definitions}
384 *********************************************************
399 int n = text (show n)
400 integer n = text (show n)
401 float n = text (show n)
402 double n = text (show n)
403 --ORIG: rational n = text (show n)
404 rational n = text (show (fromRationalX n)) -- _showRational 30 n)
406 quotes p = char '`' <> p <> char '\''
407 doubleQuotes p = char '"' <> p <> char '"'
408 parens p = char '(' <> p <> char ')'
409 brackets p = char '[' <> p <> char ']'
410 braces p = char '{' <> p <> char '}'
413 hcat = foldr (<>) empty
414 hsep = foldr (<+>) empty
415 vcat = foldr ($$) empty
417 hang d1 n d2 = d1 $$ (nest n d2)
420 punctuate p (d:ds) = go d ds
423 go d (e:es) = (d <> p) : go e es
427 *********************************************************
429 \subsection{The @Doc@ data type}
431 *********************************************************
433 A @Doc@ represents a {\em set} of layouts. A @Doc@ with
434 no occurrences of @Union@ or @NoDoc@ represents just one layout.
438 | NilAbove Doc -- text "" $$ x
439 | TextBeside TextDetails INT Doc -- text s <> x
440 | Nest INT Doc -- nest k x
441 | Union Doc Doc -- ul `union` ur
442 | NoDoc -- The empty set of documents
443 | Beside Doc Bool Doc -- True <=> space between
444 | Above Doc Bool Doc -- True <=> never overlap
446 type RDoc = Doc -- RDoc is a "reduced Doc", guaranteed not to have a top-level Above or Beside
449 reduceDoc :: Doc -> RDoc
450 reduceDoc (Beside p g q) = beside p g (reduceDoc q)
451 reduceDoc (Above p g q) = above p g (reduceDoc q)
455 data TextDetails = Chr Char
462 Here are the invariants:
465 The argument of @NilAbove@ is never @Empty@. Therefore
466 a @NilAbove@ occupies at least two lines.
469 The arugment of @TextBeside@ is never @Nest@.
472 The layouts of the two arguments of @Union@ both flatten to the same string.
475 The arguments of @Union@ are either @TextBeside@, or @NilAbove@.
478 The right argument of a union cannot be equivalent to the empty set (@NoDoc@).
479 If the left argument of a union is equivalent to the empty set (@NoDoc@),
480 then the @NoDoc@ appears in the first line.
483 An empty document is always represented by @Empty@.
484 It can't be hidden inside a @Nest@, or a @Union@ of two @Empty@s.
487 The first line of every layout in the left argument of @Union@
488 is longer than the first line of any layout in the right argument.
489 (1) ensures that the left argument has a first line. In view of (3),
490 this invariant means that the right argument must have at least two
495 -- Arg of a NilAbove is always an RDoc
496 nilAbove_ p = LOCAL_ASSERT( ok p ) NilAbove p
501 -- Arg of a TextBeside is always an RDoc
502 textBeside_ s sl p = TextBeside s sl (LOCAL_ASSERT( ok p ) p)
504 ok (Nest _ _) = False
507 -- Arg of Nest is always an RDoc
508 nest_ k p = Nest k (LOCAL_ASSERT( ok p ) p)
513 -- Args of union are always RDocs
514 union_ p q = Union (LOCAL_ASSERT( ok p ) p) (LOCAL_ASSERT( ok q ) q)
516 ok (TextBeside _ _ _) = True
517 ok (NilAbove _) = True
518 ok (Union _ _) = True
523 Notice the difference between
524 * NoDoc (no documents)
525 * Empty (one empty document; no height and no width)
526 * text "" (a document containing the empty string;
527 one line high, but has no width)
531 *********************************************************
533 \subsection{@empty@, @text@, @nest@, @union@}
535 *********************************************************
540 char c = textBeside_ (Chr c) 1# Empty
541 text s = case length s of {IBOX(sl) -> textBeside_ (Str s) sl Empty}
542 ptext s = case _LENGTH_ s of {IBOX(sl) -> textBeside_ (PStr s) sl Empty}
544 nest IBOX(k) p = mkNest k (reduceDoc p) -- Externally callable version
546 -- mkNest checks for Nest's invariant that it doesn't have an Empty inside it
547 mkNest k (Nest k1 p) = mkNest (k PLUS k1) p
548 mkNest k NoDoc = NoDoc
549 mkNest k Empty = Empty
550 mkNest ILIT(0) p = p -- Worth a try!
551 mkNest k p = nest_ k p
553 -- mkUnion checks for an empty document
554 mkUnion Empty q = Empty
555 mkUnion p q = p `union_` q
558 *********************************************************
560 \subsection{Vertical composition @$$@}
562 *********************************************************
566 p $$ q = Above p False q
567 p $+$ q = Above p True q
569 above :: Doc -> Bool -> RDoc -> RDoc
570 above (Above p g1 q1) g2 q2 = above p g1 (above q1 g2 q2)
571 above p@(Beside _ _ _) g q = aboveNest (reduceDoc p) g ILIT(0) (reduceDoc q)
572 above p g q = aboveNest p g ILIT(0) (reduceDoc q)
574 aboveNest :: RDoc -> Bool -> INT -> RDoc -> RDoc
575 -- Specfication: aboveNest p g k q = p $g$ (nest k q)
577 aboveNest NoDoc g k q = NoDoc
578 aboveNest (p1 `Union` p2) g k q = aboveNest p1 g k q `union_`
581 aboveNest Empty g k q = mkNest k q
582 aboveNest (Nest k1 p) g k q = nest_ k1 (aboveNest p g (k MINUS k1) q)
583 -- p can't be Empty, so no need for mkNest
585 aboveNest (NilAbove p) g k q = nilAbove_ (aboveNest p g k q)
586 aboveNest (TextBeside s sl p) g k q = textBeside_ s sl rest
590 Empty -> nilAboveNest g k1 q
591 other -> aboveNest p g k1 q
595 nilAboveNest :: Bool -> INT -> RDoc -> RDoc
596 -- Specification: text s <> nilaboveNest g k q
597 -- = text s <> (text "" $g$ nest k q)
599 nilAboveNest g k Empty = Empty -- Here's why the "text s <>" is in the spec!
600 nilAboveNest g k (Nest k1 q) = nilAboveNest g (k PLUS k1) q
602 nilAboveNest g k q | (not g) && (k GR ILIT(0)) -- No newline if no overlap
603 = textBeside_ (Str (spaces k)) k q
604 | otherwise -- Put them really above
605 = nilAbove_ (mkNest k q)
609 *********************************************************
611 \subsection{Horizontal composition @<>@}
613 *********************************************************
616 p <> q = Beside p False q
617 p <+> q = Beside p True q
619 beside :: Doc -> Bool -> RDoc -> RDoc
620 -- Specification: beside g p q = p <g> q
622 beside NoDoc g q = NoDoc
623 beside (p1 `Union` p2) g q = (beside p1 g q) `union_` (beside p2 g q)
625 beside (Nest k p) g q = nest_ k (beside p g q) -- p non-empty
626 beside p@(Beside p1 g1 q1) g2 q2
627 {- (A `op1` B) `op2` C == A `op1` (B `op2` C) iff op1 == op2
628 [ && (op1 == <> || op1 == <+>) ] -}
629 | g1 == g2 = beside p1 g1 (beside q1 g2 q2)
630 | otherwise = beside (reduceDoc p) g2 q2
631 beside p@(Above _ _ _) g q = beside (reduceDoc p) g q
632 beside (NilAbove p) g q = nilAbove_ (beside p g q)
633 beside (TextBeside s sl p) g q = textBeside_ s sl rest
636 Empty -> nilBeside g q
637 other -> beside p g q
641 nilBeside :: Bool -> RDoc -> RDoc
642 -- Specification: text "" <> nilBeside g p
645 nilBeside g Empty = Empty -- Hence the text "" in the spec
646 nilBeside g (Nest _ p) = nilBeside g p
647 nilBeside g p | g = textBeside_ space_text ILIT(1) p
651 *********************************************************
653 \subsection{Separate, @sep@, Hughes version}
655 *********************************************************
658 -- Specification: sep ps = oneLiner (hsep ps)
662 sep = sepX True -- Separate with spaces
663 cat = sepX False -- Don't
666 sepX x (p:ps) = sep1 x (reduceDoc p) ILIT(0) ps
669 -- Specification: sep1 g k ys = sep (x : map (nest k) ys)
670 -- = oneLiner (x <g> nest k (hsep ys))
671 -- `union` x $$ nest k (vcat ys)
673 sep1 :: Bool -> RDoc -> INT -> [Doc] -> RDoc
674 sep1 g NoDoc k ys = NoDoc
675 sep1 g (p `Union` q) k ys = sep1 g p k ys
677 (aboveNest q False k (reduceDoc (vcat ys)))
679 sep1 g Empty k ys = mkNest k (sepX g ys)
680 sep1 g (Nest n p) k ys = nest_ n (sep1 g p (k MINUS n) ys)
682 sep1 g (NilAbove p) k ys = nilAbove_ (aboveNest p False k (reduceDoc (vcat ys)))
683 sep1 g (TextBeside s sl p) k ys = textBeside_ s sl (sepNB g p (k MINUS sl) ys)
685 -- Specification: sepNB p k ys = sep1 (text "" <> p) k ys
686 -- Called when we have already found some text in the first item
687 -- We have to eat up nests
689 sepNB g (Nest _ p) k ys = sepNB g p k ys
691 sepNB g Empty k ys = oneLiner (nilBeside g (reduceDoc rest))
693 nilAboveNest False k (reduceDoc (vcat ys))
696 | otherwise = hcat ys
698 sepNB g p k ys = sep1 g p k ys
701 *********************************************************
705 *********************************************************
714 -- fill (p1:p2:ps) = oneLiner p1 <#> nest (length p1)
715 -- (fill (oneLiner p2 : ps))
720 fill g (p:ps) = fill1 g (reduceDoc p) ILIT(0) ps
723 fill1 :: Bool -> RDoc -> INT -> [Doc] -> Doc
724 fill1 g NoDoc k ys = NoDoc
725 fill1 g (p `Union` q) k ys = fill1 g p k ys
727 (aboveNest q False k (fill g ys))
729 fill1 g Empty k ys = mkNest k (fill g ys)
730 fill1 g (Nest n p) k ys = nest_ n (fill1 g p (k MINUS n) ys)
732 fill1 g (NilAbove p) k ys = nilAbove_ (aboveNest p False k (fill g ys))
733 fill1 g (TextBeside s sl p) k ys = textBeside_ s sl (fillNB g p (k MINUS sl) ys)
735 fillNB g (Nest _ p) k ys = fillNB g p k ys
736 fillNB g Empty k [] = Empty
737 fillNB g Empty k (y:ys) = nilBeside g (fill1 g (oneLiner (reduceDoc y)) k1 ys)
739 nilAboveNest False k (fill g (y:ys))
741 k1 | g = k MINUS ILIT(1)
744 fillNB g p k ys = fill1 g p k ys
748 *********************************************************
750 \subsection{Selecting the best layout}
752 *********************************************************
756 -> Int -- Line length
757 -> Int -- Ribbon length
759 -> RDoc -- No unions in here!
761 best OneLineMode IBOX(w) IBOX(r) p
766 get (NilAbove p) = nilAbove_ (get p)
767 get (TextBeside s sl p) = textBeside_ s sl (get p)
768 get (Nest k p) = get p -- Elide nest
769 get (p `Union` q) = first (get p) (get q)
771 best mode IBOX(w) IBOX(r) p
774 get :: INT -- (Remaining) width of line
778 get w (NilAbove p) = nilAbove_ (get w p)
779 get w (TextBeside s sl p) = textBeside_ s sl (get1 w sl p)
780 get w (Nest k p) = nest_ k (get (w MINUS k) p)
781 get w (p `Union` q) = nicest w r (get w p) (get w q)
783 get1 :: INT -- (Remaining) width of line
784 -> INT -- Amount of first line already eaten up
785 -> Doc -- This is an argument to TextBeside => eat Nests
786 -> Doc -- No unions in here!
788 get1 w sl Empty = Empty
789 get1 w sl NoDoc = NoDoc
790 get1 w sl (NilAbove p) = nilAbove_ (get (w MINUS sl) p)
791 get1 w sl (TextBeside t tl p) = textBeside_ t tl (get1 w (sl PLUS tl) p)
792 get1 w sl (Nest k p) = get1 w sl p
793 get1 w sl (p `Union` q) = nicest1 w r sl (get1 w sl p)
796 nicest w r p q = nicest1 w r ILIT(0) p q
797 nicest1 w r sl p q | fits ((w `minn` r) MINUS sl) p = p
800 fits :: INT -- Space available
802 -> Bool -- True if *first line* of Doc fits in space available
804 fits n p | n LT ILIT(0) = False
807 fits n (NilAbove _) = True
808 fits n (TextBeside _ sl p) = fits (n MINUS sl) p
810 minn x y | x LT y = x
814 @first@ and @nonEmptySet@ are similar to @nicest@ and @fits@, only simpler.
815 @first@ returns its first argument if it is non-empty, otherwise its second.
818 first p q | nonEmptySet p = p
821 nonEmptySet NoDoc = False
822 nonEmptySet (p `Union` q) = True
823 nonEmptySet Empty = True
824 nonEmptySet (NilAbove p) = True -- NoDoc always in first line
825 nonEmptySet (TextBeside _ _ p) = nonEmptySet p
826 nonEmptySet (Nest _ p) = nonEmptySet p
829 @oneLiner@ returns the one-line members of the given set of @Doc@s.
832 oneLiner :: Doc -> Doc
833 oneLiner NoDoc = NoDoc
834 oneLiner Empty = Empty
835 oneLiner (NilAbove p) = NoDoc
836 oneLiner (TextBeside s sl p) = textBeside_ s sl (oneLiner p)
837 oneLiner (Nest k p) = nest_ k (oneLiner p)
838 oneLiner (p `Union` q) = oneLiner p
843 *********************************************************
845 \subsection{Displaying the best layout}
847 *********************************************************
852 renderStyle Style{mode, lineLength, ribbonsPerLine} doc
853 = fullRender mode lineLength ribbonsPerLine doc ""
856 render doc = showDoc doc ""
857 showDoc doc rest = fullRender PageMode 100 1.5 string_txt rest doc
859 string_txt (Chr c) s = c:s
860 string_txt (Str s1) s2 = s1 ++ s2
861 string_txt (PStr s1) s2 = _UNPK_ s1 ++ s2
866 fullRender OneLineMode _ _ txt end doc = easy_display space_text txt end (reduceDoc doc)
867 fullRender LeftMode _ _ txt end doc = easy_display nl_text txt end (reduceDoc doc)
869 fullRender mode line_length ribbons_per_line txt end doc
870 = display mode line_length ribbon_length txt end best_doc
872 best_doc = best mode hacked_line_length ribbon_length (reduceDoc doc)
874 hacked_line_length, ribbon_length :: Int
875 ribbon_length = round (fromInt line_length / ribbons_per_line)
876 hacked_line_length = case mode of { ZigZagMode -> MAXINT; other -> line_length }
878 display mode IBOX(page_width) IBOX(ribbon_width) txt end doc
879 = case page_width MINUS ribbon_width of { gap_width ->
880 case gap_width DIV ILIT(2) of { shift ->
882 lay k (Nest k1 p) = lay (k PLUS k1) p
885 lay k (NilAbove p) = nl_text `txt` lay k p
887 lay k (TextBeside s sl p)
889 ZigZagMode | k GREQ gap_width
891 Str (multi_ch shift '/') `txt` (
893 lay1 (k MINUS shift) s sl p)))
897 Str (multi_ch shift '\\') `txt` (
899 lay1 (k PLUS shift) s sl p )))
901 other -> lay1 k s sl p
903 lay1 k s sl p = Str (indent k) `txt` (s `txt` lay2 (k PLUS sl) p)
905 lay2 k (NilAbove p) = nl_text `txt` lay k p
906 lay2 k (TextBeside s sl p) = s `txt` (lay2 (k PLUS sl) p)
907 lay2 k (Nest _ p) = lay2 k p
913 cant_fail = error "easy_display: NoDoc"
914 easy_display nl_text txt end doc
917 lay NoDoc no_doc = no_doc
918 lay (Union p q) no_doc = {- lay p -} (lay q cant_fail) -- Second arg can't be NoDoc
919 lay (Nest k p) no_doc = lay p no_doc
920 lay Empty no_doc = end
921 lay (NilAbove p) no_doc = nl_text `txt` lay p cant_fail -- NoDoc always on first line
922 lay (TextBeside s sl p) no_doc = s `txt` lay p no_doc
924 indent n | n GREQ ILIT(8) = '\t' : indent (n MINUS ILIT(8))
925 | otherwise = spaces n
927 multi_ch ILIT(0) ch = ""
928 multi_ch n ch = ch : multi_ch (n MINUS ILIT(1)) ch
931 spaces n = ' ' : spaces (n MINUS ILIT(1))
934 Doesn't really belong here..
936 -----------------------------------
939 fromRationalX :: (RealFloat a) => Rational -> a
943 h = ceiling (huge `asTypeOf` x)
944 b = toInteger (floatRadix x)
947 let d = denominator r'
950 let e = integerLogBase b (d `div` h) + 1
951 in fromRat (e0-e) (n % (d `div` (b^e)))
952 else if abs n > h then
953 let e = integerLogBase b (abs n `div` h) + 1
954 in fromRat (e0+e) ((n `div` (b^e)) % d)
956 scaleFloat e0 (fromRational r')
959 -- Compute the discrete log of i in base b.
960 -- Simplest way would be just divide i by b until it's smaller then b, but that would
961 -- be very slow! We are just slightly more clever.
962 integerLogBase :: Integer -> Integer -> Int
967 -- Try squaring the base first to cut down the number of divisions.
968 let l = 2 * integerLogBase (b*b) i
970 doDiv :: Integer -> Int -> Int
971 doDiv j k = if j < b then k else doDiv (j `div` b) (k+1)
973 doDiv (i `div` (b^l)) l
978 -- Compute smallest and largest floating point values.
980 tiny :: (RealFloat a) => a
982 let (l, _) = floatRange x
983 x = encodeFloat 1 (l-1)
987 huge :: (RealFloat a) => a
989 let (_, u) = floatRange x
991 x = encodeFloat (floatRadix x ^ d - 1) (u - d)