home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / Prelude.hs < prev    next >
Encoding:
Text File  |  2000-09-21  |  52.7 KB  |  1,662 lines

  1. {----------------------------------------------------------------------------
  2. __   __ __  __  ____   ___    _______________________________________________
  3. ||   || ||  || ||  || ||__    Hugs 98: The Nottingham and Yale Haskell system
  4. ||___|| ||__|| ||__||  __||   Copyright (c) 1994-1999
  5. ||---||         ___||         World Wide Web: http://haskell.org/hugs
  6. ||   ||                       Report bugs to: hugs-bugs@haskell.org
  7. ||   || Version: February 1999_______________________________________________
  8.  
  9.  This is the Hugs 98 Standard Prelude, based very closely on the Standard
  10.  Prelude for Haskell 98.
  11.  
  12.  WARNING: This file is an integral part of the Hugs source code.  Changes to
  13.  the definitions in this file without corresponding modifications in other
  14.  parts of the program may cause the interpreter to fail unexpectedly.  Under
  15.  normal circumstances, you should not attempt to modify this file in any way!
  16.  
  17. -----------------------------------------------------------------------------
  18.  The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
  19.  Yale Haskell Group, and the Oregon Graduate Institute of Science and
  20.  Technology, 1994-1999, All rights reserved.  It is distributed as
  21.  free software under the license in the file "License", which is
  22.  included in the distribution.
  23. ----------------------------------------------------------------------------}
  24.  
  25. module Prelude (
  26. --  module PreludeList,
  27.     map, (++), concat, filter,
  28.     head, last, tail, init, null, length, (!!),
  29.     foldl, foldl1, scanl, scanl1, foldr, foldr1, scanr, scanr1,
  30.     iterate, repeat, replicate, cycle,
  31.     take, drop, splitAt, takeWhile, dropWhile, span, break,
  32.     lines, words, unlines, unwords, reverse, and, or,
  33.     any, all, elem, notElem, lookup,
  34.     sum, product, maximum, minimum, concatMap, 
  35.     zip, zip3, zipWith, zipWith3, unzip, unzip3,
  36. --  module PreludeText, 
  37.     ReadS, ShowS,
  38.     Read(readsPrec, readList),
  39.     Show(show, showsPrec, showList),
  40.     reads, shows, read, lex,
  41.     showChar, showString, readParen, showParen,
  42. --  module PreludeIO,
  43.     FilePath, IOError, ioError, userError, catch,
  44.     putChar, putStr, putStrLn, print,
  45.     getChar, getLine, getContents, interact,
  46.     readFile, writeFile, appendFile, readIO, readLn,
  47. --  module Ix,
  48.     Ix(range, index, inRange, rangeSize),
  49. --  module Char,
  50.     isAscii, isControl, isPrint, isSpace, isUpper, isLower,
  51.     isAlpha, isDigit, isOctDigit, isHexDigit, isAlphaNum,
  52.     digitToInt, intToDigit,
  53.     toUpper, toLower,
  54.     ord, chr,
  55.     readLitChar, showLitChar, lexLitChar,
  56. --  module Numeric
  57.     showSigned, showInt,
  58.     readSigned, readInt,
  59.     readDec, readOct, readHex, readSigned,
  60.     readFloat, lexDigits, 
  61. --  module Ratio,
  62.     Ratio, Rational, (%), numerator, denominator, approxRational,
  63. --  Non-standard exports
  64.     IO(..), IOResult(..), primExitWith, Addr,
  65.  
  66.     Bool(False, True),
  67.     Maybe(Nothing, Just),
  68.     Either(Left, Right),
  69.     Ordering(LT, EQ, GT),
  70.     Char, String, Int, Integer, Float, Double, IO,
  71. --  List type: []((:), [])
  72.     (:),
  73. --  Tuple types: (,), (,,), etc.
  74. --  Trivial type: ()
  75. --  Functions: (->)
  76.     Rec, EmptyRec, EmptyRow, -- non-standard, should only be exported if TREX
  77.     Eq((==), (/=)),
  78.     Ord(compare, (<), (<=), (>=), (>), max, min),
  79.     Enum(succ, pred, toEnum, fromEnum, enumFrom, enumFromThen,
  80.          enumFromTo, enumFromThenTo),
  81.     Bounded(minBound, maxBound),
  82. --  Num((+), (-), (*), negate, abs, signum, fromInteger),
  83.     Num((+), (-), (*), negate, abs, signum, fromInteger, fromInt),
  84.     Real(toRational),
  85. --  Integral(quot, rem, div, mod, quotRem, divMod, toInteger),
  86.     Integral(quot, rem, div, mod, quotRem, divMod, even, odd, toInteger, toInt),
  87. --  Fractional((/), recip, fromRational),
  88.     Fractional((/), recip, fromRational, fromDouble),
  89.     Floating(pi, exp, log, sqrt, (**), logBase, sin, cos, tan,
  90.              asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh),
  91.     RealFrac(properFraction, truncate, round, ceiling, floor),
  92.     RealFloat(floatRadix, floatDigits, floatRange, decodeFloat,
  93.               encodeFloat, exponent, significand, scaleFloat, isNaN,
  94.               isInfinite, isDenormalized, isIEEE, isNegativeZero, atan2),
  95.     Monad((>>=), (>>), return, fail),
  96.     Functor(fmap),
  97.     mapM, mapM_, sequence, sequence_, (=<<),
  98.     maybe, either,
  99.     (&&), (||), not, otherwise,
  100.     subtract, even, odd, gcd, lcm, (^), (^^), 
  101.     fromIntegral, realToFrac,
  102.     fst, snd, curry, uncurry, id, const, (.), flip, ($), until,
  103.     asTypeOf, error, undefined,
  104.     seq, ($!)
  105.   ) where
  106.  
  107. -- Standard value bindings {Prelude} ----------------------------------------
  108.  
  109. infixr 9  .
  110. infixl 9  !!
  111. infixr 8  ^, ^^, **
  112. infixl 7  *, /, `quot`, `rem`, `div`, `mod`, :%, %
  113. infixl 6  +, -
  114. --infixr 5  :    -- this fixity declaration is hard-wired into Hugs
  115. infixr 5  ++
  116. infix  4  ==, /=, <, <=, >=, >, `elem`, `notElem`
  117. infixr 3  &&
  118. infixr 2  ||
  119. infixl 1  >>, >>=
  120. infixr 1  =<<
  121. infixr 0  $, $!, `seq`
  122.  
  123. -- Equality and Ordered classes ---------------------------------------------
  124.  
  125. class Eq a where
  126.     (==), (/=) :: a -> a -> Bool
  127.  
  128.     -- Minimal complete definition: (==) or (/=)
  129.     x == y      = not (x/=y)
  130.     x /= y      = not (x==y)
  131.  
  132. class (Eq a) => Ord a where
  133.     compare                :: a -> a -> Ordering
  134.     (<), (<=), (>=), (>)   :: a -> a -> Bool
  135.     max, min               :: a -> a -> a
  136.  
  137.     -- Minimal complete definition: (<=) or compare
  138.     -- using compare can be more efficient for complex types
  139.     compare x y | x==y      = EQ
  140.         | x<=y      = LT
  141.         | otherwise = GT
  142.  
  143.     x <= y                  = compare x y /= GT
  144.     x <  y                  = compare x y == LT
  145.     x >= y                  = compare x y /= LT
  146.     x >  y                  = compare x y == GT
  147.  
  148.     max x y   | x >= y      = x
  149.           | otherwise   = y
  150.     min x y   | x <= y      = x
  151.           | otherwise   = y
  152.  
  153. class Bounded a where
  154.     minBound, maxBound :: a
  155.     -- Minimal complete definition: All
  156.  
  157. -- Numeric classes ----------------------------------------------------------
  158.  
  159. class (Eq a, Show a) => Num a where
  160.     (+), (-), (*)  :: a -> a -> a
  161.     negate         :: a -> a
  162.     abs, signum    :: a -> a
  163.     fromInteger    :: Integer -> a
  164.     fromInt        :: Int -> a
  165.  
  166.     -- Minimal complete definition: All, except negate or (-)
  167.     x - y           = x + negate y
  168.     fromInt         = fromIntegral
  169.     negate x        = 0 - x
  170.  
  171. class (Num a, Ord a) => Real a where
  172.     toRational     :: a -> Rational
  173.  
  174. class (Real a, Enum a) => Integral a where
  175.     quot, rem, div, mod :: a -> a -> a
  176.     quotRem, divMod     :: a -> a -> (a,a)
  177.     even, odd           :: a -> Bool
  178.     toInteger           :: a -> Integer
  179.     toInt               :: a -> Int
  180.  
  181.     -- Minimal complete definition: quotRem and toInteger
  182.     n `quot` d           = q where (q,r) = quotRem n d
  183.     n `rem` d            = r where (q,r) = quotRem n d
  184.     n `div` d            = q where (q,r) = divMod n d
  185.     n `mod` d            = r where (q,r) = divMod n d
  186.     divMod n d           = if signum r == - signum d then (q-1, r+d) else qr
  187.                where qr@(q,r) = quotRem n d
  188.     even n               = n `rem` 2 == 0
  189.     odd                  = not . even
  190.     toInt                = toInt . toInteger
  191.  
  192. class (Num a) => Fractional a where
  193.     (/)          :: a -> a -> a
  194.     recip        :: a -> a
  195.     fromRational :: Rational -> a
  196.     fromDouble   :: Double -> a
  197.  
  198.     -- Minimal complete definition: fromRational and ((/) or recip)
  199.     recip x       = 1 / x
  200.     fromDouble    = fromRational . toRational
  201.     x / y         = x * recip y
  202.  
  203.  
  204. class (Fractional a) => Floating a where
  205.     pi                  :: a
  206.     exp, log, sqrt      :: a -> a
  207.     (**), logBase       :: a -> a -> a
  208.     sin, cos, tan       :: a -> a
  209.     asin, acos, atan    :: a -> a
  210.     sinh, cosh, tanh    :: a -> a
  211.     asinh, acosh, atanh :: a -> a
  212.  
  213.     -- Minimal complete definition: pi, exp, log, sin, cos, sinh, cosh,
  214.     --                    asinh, acosh, atanh
  215.     pi                   = 4 * atan 1
  216.     x ** y               = exp (log x * y)
  217.     logBase x y          = log y / log x
  218.     sqrt x               = x ** 0.5
  219.     tan x                = sin x / cos x
  220.     sinh x               = (exp x - exp (-x)) / 2
  221.     cosh x               = (exp x + exp (-x)) / 2
  222.     tanh x               = sinh x / cosh x
  223.     asinh x              = log (x + sqrt (x*x + 1))
  224.     acosh x              = log (x + sqrt (x*x - 1))
  225.     atanh x              = (log (1 + x) - log (1 - x)) / 2
  226.  
  227. class (Real a, Fractional a) => RealFrac a where
  228.     properFraction   :: (Integral b) => a -> (b,a)
  229.     truncate, round  :: (Integral b) => a -> b
  230.     ceiling, floor   :: (Integral b) => a -> b
  231.  
  232.     -- Minimal complete definition: properFraction
  233.     truncate x        = m where (m,_) = properFraction x
  234.  
  235.     round x           = let (n,r) = properFraction x
  236.                 m     = if r < 0 then n - 1 else n + 1
  237.             in case signum (abs r - 0.5) of
  238.                 -1 -> n
  239.                 0  -> if even n then n else m
  240.                 1  -> m
  241.  
  242.     ceiling x         = if r > 0 then n + 1 else n
  243.             where (n,r) = properFraction x
  244.  
  245.     floor x           = if r < 0 then n - 1 else n
  246.             where (n,r) = properFraction x
  247.  
  248. class (RealFrac a, Floating a) => RealFloat a where
  249.     floatRadix       :: a -> Integer
  250.     floatDigits      :: a -> Int
  251.     floatRange       :: a -> (Int,Int)
  252.     decodeFloat      :: a -> (Integer,Int)
  253.     encodeFloat      :: Integer -> Int -> a
  254.     exponent         :: a -> Int
  255.     significand      :: a -> a
  256.     scaleFloat       :: Int -> a -> a
  257.     isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE
  258.              :: a -> Bool
  259.     atan2         :: a -> a -> a
  260.  
  261.     -- Minimal complete definition: All, except exponent, signficand,
  262.     --                    scaleFloat, atan2
  263.     exponent x        = if m==0 then 0 else n + floatDigits x
  264.             where (m,n) = decodeFloat x
  265.     significand x     = encodeFloat m (- floatDigits x)
  266.             where (m,_) = decodeFloat x
  267.     scaleFloat k x    = encodeFloat m (n+k)
  268.             where (m,n) = decodeFloat x
  269.     atan2 y x
  270.       | x>0           = atan (y/x)
  271.       | x==0 && y>0   = pi/2
  272.       | x<0 && y>0    = pi + atan (y/x)
  273.       | (x<=0 && y<0) ||
  274.         (x<0 && isNegativeZero y) ||
  275.         (isNegativeZero x && isNegativeZero y)
  276.               = - atan2 (-y) x
  277.       | y==0 && (x<0 || isNegativeZero x)
  278.               = pi    -- must be after the previous test on zero y
  279.       | x==0 && y==0  = y     -- must be after the other double zero tests
  280.       | otherwise     = x + y -- x or y is a NaN, return a NaN (via +)
  281.  
  282. -- Numeric functions --------------------------------------------------------
  283.  
  284. subtract       :: Num a => a -> a -> a
  285. subtract        = flip (-)
  286.  
  287. gcd            :: Integral a => a -> a -> a
  288. gcd 0 0         = error "Prelude.gcd: gcd 0 0 is undefined"
  289. gcd x y         = gcd' (abs x) (abs y)
  290.           where gcd' x 0 = x
  291.             gcd' x y = gcd' y (x `rem` y)
  292.  
  293. lcm            :: (Integral a) => a -> a -> a
  294. lcm _ 0         = 0
  295. lcm 0 _         = 0
  296. lcm x y         = abs ((x `quot` gcd x y) * y)
  297.  
  298. (^)            :: (Num a, Integral b) => a -> b -> a
  299. x ^ 0           = 1
  300. x ^ n  | n > 0  = f x (n-1) x
  301.           where f _ 0 y = y
  302.             f x n y = g x n where
  303.                   g x n | even n    = g (x*x) (n`quot`2)
  304.                     | otherwise = f x (n-1) (x*y)
  305. _ ^ _           = error "Prelude.^: negative exponent"
  306.  
  307. (^^)           :: (Fractional a, Integral b) => a -> b -> a
  308. x ^^ n          = if n >= 0 then x ^ n else recip (x^(-n))
  309.  
  310. fromIntegral   :: (Integral a, Num b) => a -> b
  311. fromIntegral    = fromInteger . toInteger
  312.  
  313. realToFrac     :: (Real a, Fractional b) => a -> b
  314. realToFrac      = fromRational . toRational
  315.  
  316. -- Index and Enumeration classes --------------------------------------------
  317.  
  318. class (Ord a) => Ix a where
  319.     range                :: (a,a) -> [a]
  320.     index                :: (a,a) -> a -> Int
  321.     inRange              :: (a,a) -> a -> Bool
  322.     rangeSize            :: (a,a) -> Int
  323.  
  324.     rangeSize r@(l,u)
  325.              | l > u      = 0
  326.              | otherwise  = index r u + 1
  327.  
  328. class Enum a where
  329.     succ, pred           :: a -> a
  330.     toEnum               :: Int -> a
  331.     fromEnum             :: a -> Int
  332.     enumFrom             :: a -> [a]              -- [n..]
  333.     enumFromThen         :: a -> a -> [a]         -- [n,m..]
  334.     enumFromTo           :: a -> a -> [a]         -- [n..m]
  335.     enumFromThenTo       :: a -> a -> a -> [a]    -- [n,n'..m]
  336.  
  337.     -- Minimal complete definition: toEnum, fromEnum
  338.     succ                  = toEnum . (1+)       . fromEnum
  339.     pred                  = toEnum . subtract 1 . fromEnum
  340.     enumFromTo x y        = map toEnum [ fromEnum x .. fromEnum y ]
  341.     enumFromThenTo x y z  = map toEnum [ fromEnum x, fromEnum y .. fromEnum z ]
  342.  
  343. -- Read and Show classes ------------------------------------------------------
  344.  
  345. type ReadS a = String -> [(a,String)]
  346. type ShowS   = String -> String
  347.  
  348. class Read a where
  349.     readsPrec :: Int -> ReadS a
  350.     readList  :: ReadS [a]
  351.  
  352.     -- Minimal complete definition: readsPrec
  353.     readList   = readParen False (\r -> [pr | ("[",s) <- lex r,
  354.                           pr      <- readl s ])
  355.          where readl  s = [([],t)   | ("]",t) <- lex s] ++
  356.                   [(x:xs,u) | (x,t)   <- reads s,
  357.                           (xs,u)  <- readl' t]
  358.                readl' s = [([],t)   | ("]",t) <- lex s] ++
  359.                   [(x:xs,v) | (",",t) <- lex s,
  360.                           (x,u)   <- reads t,
  361.                           (xs,v)  <- readl' u]
  362.  
  363. class Show a where
  364.     show      :: a -> String
  365.     showsPrec :: Int -> a -> ShowS
  366.     showList  :: [a] -> ShowS
  367.  
  368.     -- Minimal complete definition: show or showsPrec
  369.     show x          = showsPrec 0 x ""
  370.     showsPrec _ x s = show x ++ s
  371.     showList []     = showString "[]"
  372.     showList (x:xs) = showChar '[' . shows x . showl xs
  373.               where showl []     = showChar ']'
  374.                 showl (x:xs) = showChar ',' . shows x . showl xs
  375.  
  376. -- Monad classes ------------------------------------------------------------
  377.  
  378. class Functor f where
  379.     fmap :: (a -> b) -> (f a -> f b)
  380.  
  381. class Monad m where
  382.     return :: a -> m a
  383.     (>>=)  :: m a -> (a -> m b) -> m b
  384.     (>>)   :: m a -> m b -> m b
  385.     fail   :: String -> m a
  386.  
  387.     -- Minimal complete definition: (>>=), return
  388.     p >> q  = p >>= \ _ -> q
  389.     fail s  = error s
  390.  
  391. sequence       :: Monad m => [m a] -> m [a]
  392. sequence []     = return []
  393. sequence (c:cs) = do x  <- c
  394.              xs <- sequence cs
  395.              return (x:xs)
  396.  
  397. sequence_        :: Monad m => [m a] -> m ()
  398. sequence_         = foldr (>>) (return ())
  399.  
  400. mapM             :: Monad m => (a -> m b) -> [a] -> m [b]
  401. mapM f            = sequence . map f
  402.  
  403. mapM_            :: Monad m => (a -> m b) -> [a] -> m ()
  404. mapM_ f           = sequence_ . map f
  405.  
  406. (=<<)            :: Monad m => (a -> m b) -> m a -> m b
  407. f =<< x           = x >>= f
  408.  
  409. -- Evaluation and strictness ------------------------------------------------
  410.  
  411. primitive seq           :: a -> b -> b
  412.  
  413. primitive ($!) "strict" :: (a -> b) -> a -> b
  414. -- f $! x                = x `seq` f x
  415.  
  416. -- Trivial type -------------------------------------------------------------
  417.  
  418. -- data () = () deriving (Eq, Ord, Ix, Enum, Read, Show, Bounded)
  419.  
  420. instance Eq () where
  421.     () == ()  =  True
  422.  
  423. instance Ord () where
  424.     compare () () = EQ
  425.  
  426. instance Ix () where
  427.     range ((),())      = [()]
  428.     index ((),()) ()   = 0
  429.     inRange ((),()) () = True
  430.  
  431. instance Enum () where
  432.     toEnum 0           = ()
  433.     fromEnum ()        = 0
  434.     enumFrom ()        = [()]
  435.     enumFromThen () () = [()]
  436.  
  437. instance Read () where
  438.     readsPrec p = readParen False (\r -> [((),t) | ("(",s) <- lex r,
  439.                            (")",t) <- lex s ])
  440.  
  441. instance Show () where
  442.     showsPrec p () = showString "()"
  443.  
  444. instance Bounded () where
  445.     minBound = ()
  446.     maxBound = ()
  447.  
  448. -- Boolean type -------------------------------------------------------------
  449.  
  450. data Bool    = False | True
  451.            deriving (Eq, Ord, Ix, Enum, Read, Show, Bounded)
  452.  
  453. (&&), (||)  :: Bool -> Bool -> Bool
  454. False && x   = False
  455. True  && x   = x
  456. False || x   = x
  457. True  || x   = True
  458.  
  459. not         :: Bool -> Bool
  460. not True     = False
  461. not False    = True
  462.  
  463. otherwise   :: Bool
  464. otherwise    = True
  465.  
  466. -- Character type -----------------------------------------------------------
  467.  
  468. data Char               -- builtin datatype of ISO Latin characters
  469. type String = [Char]    -- strings are lists of characters
  470.  
  471. primitive primEqChar    :: Char -> Char -> Bool
  472. primitive primCmpChar   :: Char -> Char -> Ordering
  473.  
  474. instance Eq Char  where (==)    = primEqChar
  475. instance Ord Char where compare = primCmpChar
  476.  
  477. primitive primCharToInt :: Char -> Int
  478. primitive primIntToChar :: Int -> Char
  479.  
  480. instance Enum Char where
  481.     toEnum           = primIntToChar
  482.     fromEnum         = primCharToInt
  483.     enumFrom c       = map toEnum [fromEnum c .. fromEnum (maxBound::Char)]
  484.     enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (lastChar::Char)]
  485.                where lastChar = if d < c then minBound else maxBound
  486.  
  487. instance Ix Char where
  488.     range (c,c')      = [c..c']
  489.     index b@(c,c') ci
  490.        | inRange b ci = fromEnum ci - fromEnum c
  491.        | otherwise    = error "Ix.index: Index out of range."
  492.     inRange (c,c') ci = fromEnum c <= i && i <= fromEnum c'
  493.             where i = fromEnum ci
  494.  
  495. instance Read Char where
  496.     readsPrec p      = readParen False
  497.                 (\r -> [(c,t) | ('\'':s,t) <- lex r,
  498.                         (c,"\'")   <- readLitChar s ])
  499.     readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
  500.                            (l,_)      <- readl s ])
  501.            where readl ('"':s)      = [("",s)]
  502.              readl ('\\':'&':s) = readl s
  503.              readl s            = [(c:cs,u) | (c ,t) <- readLitChar s,
  504.                               (cs,u) <- readl t ]
  505. instance Show Char where
  506.     showsPrec p '\'' = showString "'\\''"
  507.     showsPrec p c    = showChar '\'' . showLitChar c . showChar '\''
  508.  
  509.     showList cs   = showChar '"' . showl cs
  510.             where showl ""       = showChar '"'
  511.               showl ('"':cs) = showString "\\\"" . showl cs
  512.               showl (c:cs)   = showLitChar c . showl cs
  513.  
  514. instance Bounded Char where
  515.     minBound = '\0'
  516.     maxBound = '\255'
  517.  
  518. isAscii, isControl, isPrint, isSpace            :: Char -> Bool
  519. isUpper, isLower, isAlpha, isDigit, isAlphaNum  :: Char -> Bool
  520.  
  521. isAscii c              =  fromEnum c < 128
  522. isControl c            =  c < ' ' ||  c == '\DEL'
  523. isPrint c              =  c >= ' ' &&  c <= '~'
  524. isSpace c              =  c == ' ' || c == '\t' || c == '\n' ||
  525.               c == '\r' || c == '\f' || c == '\v'
  526. isUpper c              =  c >= 'A'   &&  c <= 'Z'
  527. isLower c              =  c >= 'a'   &&  c <= 'z'
  528. isAlpha c              =  isUpper c  ||  isLower c
  529. isDigit c              =  c >= '0'   &&  c <= '9'
  530. isAlphaNum c           =  isAlpha c  ||  isDigit c
  531.  
  532. -- Digit conversion operations
  533. digitToInt :: Char -> Int
  534. digitToInt c
  535.   | isDigit c            =  fromEnum c - fromEnum '0'
  536.   | c >= 'a' && c <= 'f' =  fromEnum c - fromEnum 'a' + 10
  537.   | c >= 'A' && c <= 'F' =  fromEnum c - fromEnum 'A' + 10
  538.   | otherwise            =  error "Char.digitToInt: not a digit"
  539.  
  540. intToDigit :: Int -> Char
  541. intToDigit i
  542.   | i >= 0  && i <=  9   =  toEnum (fromEnum '0' + i)
  543.   | i >= 10 && i <= 15   =  toEnum (fromEnum 'a' + i - 10)
  544.   | otherwise            =  error "Char.intToDigit: not a digit"
  545.  
  546. toUpper, toLower      :: Char -> Char
  547. toUpper c | isLower c  = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
  548.       | otherwise  = c
  549.  
  550. toLower c | isUpper c  = toEnum (fromEnum c - fromEnum 'A' + fromEnum 'a')
  551.       | otherwise  = c
  552.  
  553. ord                   :: Char -> Int
  554. ord                    = fromEnum
  555.  
  556. chr                   :: Int -> Char
  557. chr                    = toEnum
  558.  
  559. -- Maybe type ---------------------------------------------------------------
  560.  
  561. data Maybe a = Nothing | Just a
  562.            deriving (Eq, Ord, Read, Show)
  563.  
  564. maybe             :: b -> (a -> b) -> Maybe a -> b
  565. maybe n f Nothing  = n
  566. maybe n f (Just x) = f x
  567.  
  568. instance Functor Maybe where
  569.     fmap f Nothing  = Nothing
  570.     fmap f (Just x) = Just (f x)
  571.  
  572. instance Monad Maybe where
  573.     Just x  >>= k = k x
  574.     Nothing >>= k = Nothing
  575.     return        = Just
  576.     fail s        = Nothing
  577.  
  578. -- Either type --------------------------------------------------------------
  579.  
  580. data Either a b = Left a | Right b
  581.           deriving (Eq, Ord, Read, Show)
  582.  
  583. either              :: (a -> c) -> (b -> c) -> Either a b -> c
  584. either l r (Left x)  = l x
  585. either l r (Right y) = r y
  586.  
  587. -- Ordering type ------------------------------------------------------------
  588.  
  589. data Ordering = LT | EQ | GT
  590.         deriving (Eq, Ord, Ix, Enum, Read, Show, Bounded)
  591.  
  592. -- Lists --------------------------------------------------------------------
  593.  
  594. -- data [a] = [] | a : [a] deriving (Eq, Ord)
  595.  
  596. instance Eq a => Eq [a] where
  597.     []     == []     =  True
  598.     (x:xs) == (y:ys) =  x==y && xs==ys
  599.     _      == _      =  False
  600.  
  601. instance Ord a => Ord [a] where
  602.     compare []     (_:_)  = LT
  603.     compare []     []     = EQ
  604.     compare (_:_)  []     = GT
  605.     compare (x:xs) (y:ys) = primCompAux x y (compare xs ys)
  606.  
  607. instance Functor [] where
  608.     fmap = map
  609.  
  610. instance Monad [ ] where
  611.     (x:xs) >>= f = f x ++ (xs >>= f)
  612.     []     >>= f = []
  613.     return x     = [x]
  614.     fail s       = []
  615.  
  616. instance Read a => Read [a]  where
  617.     readsPrec p = readList
  618.  
  619. instance Show a => Show [a]  where
  620.     showsPrec p = showList
  621.  
  622. -- Tuples -------------------------------------------------------------------
  623.  
  624. -- data (a,b) = (a,b) deriving (Eq, Ord, Ix, Read, Show)
  625. -- etc..
  626.  
  627. -- Standard Integral types --------------------------------------------------
  628.  
  629. data Int      -- builtin datatype of fixed size integers
  630. data Integer  -- builtin datatype of arbitrary size integers
  631.  
  632. primitive primEqInt      :: Int -> Int -> Bool
  633. primitive primCmpInt     :: Int -> Int -> Ordering
  634. primitive primEqInteger  :: Integer -> Integer -> Bool
  635. primitive primCmpInteger :: Integer -> Integer -> Ordering
  636.  
  637. instance Eq  Int     where (==)    = primEqInt
  638. instance Eq  Integer where (==)    = primEqInteger
  639. instance Ord Int     where compare = primCmpInt
  640. instance Ord Integer where compare = primCmpInteger
  641.  
  642. primitive primPlusInt,
  643.       primMinusInt,
  644.       primMulInt       :: Int -> Int -> Int
  645. primitive primNegInt       :: Int -> Int
  646. primitive primIntegerToInt :: Integer -> Int
  647.  
  648. instance Num Int where
  649.     (+)           = primPlusInt
  650.     (-)           = primMinusInt
  651.     negate        = primNegInt
  652.     (*)           = primMulInt
  653.     abs           = absReal
  654.     signum        = signumReal
  655.     fromInteger   = primIntegerToInt
  656.     fromInt x     = x
  657.  
  658. primitive primMinInt, primMaxInt :: Int
  659.  
  660. instance Bounded Int where
  661.     minBound = primMinInt
  662.     maxBound = primMaxInt
  663.  
  664. primitive primPlusInteger,
  665.       primMinusInteger,
  666.       primMulInteger   :: Integer -> Integer -> Integer
  667. primitive primNegInteger   :: Integer -> Integer
  668. primitive primIntToInteger :: Int -> Integer
  669.  
  670. instance Num Integer where
  671.     (+)           = primPlusInteger
  672.     (-)           = primMinusInteger
  673.     negate        = primNegInteger
  674.     (*)           = primMulInteger
  675.     abs           = absReal
  676.     signum        = signumReal
  677.     fromInteger x = x
  678.     fromInt       = primIntToInteger
  679.  
  680. absReal x    | x >= 0    = x
  681.          | otherwise = -x
  682.  
  683. signumReal x | x == 0    =  0
  684.          | x > 0     =  1
  685.          | otherwise = -1
  686.  
  687. instance Real Int where
  688.     toRational x = toInteger x % 1
  689.  
  690. instance Real Integer where
  691.     toRational x = x % 1
  692.  
  693. primitive primDivInt,
  694.       primQuotInt,
  695.       primRemInt,
  696.       primModInt  :: Int -> Int -> Int
  697. primitive primQrmInt  :: Int -> Int -> (Int,Int)
  698. primitive primEvenInt :: Int -> Bool
  699.  
  700. instance Integral Int where
  701.     div       = primDivInt
  702.     quot      = primQuotInt
  703.     rem       = primRemInt
  704.     mod       = primModInt
  705.     quotRem   = primQrmInt
  706.     even      = primEvenInt
  707.     toInteger = primIntToInteger
  708.     toInt x   = x
  709.  
  710. primitive primQrmInteger  :: Integer -> Integer -> (Integer,Integer)
  711. primitive primEvenInteger :: Integer -> Bool
  712.  
  713. instance Integral Integer where
  714.     quotRem     = primQrmInteger
  715.     even        = primEvenInteger
  716.     toInteger x = x
  717.     toInt       = primIntegerToInt
  718.  
  719. instance Ix Int where
  720.     range (m,n)          = [m..n]
  721.     index b@(m,n) i
  722.        | inRange b i = i - m
  723.        | otherwise   = error "index: Index out of range"
  724.     inRange (m,n) i      = m <= i && i <= n
  725.  
  726. instance Ix Integer where
  727.     range (m,n)          = [m..n]
  728.     index b@(m,n) i
  729.        | inRange b i = fromInteger (i - m)
  730.        | otherwise   = error "index: Index out of range"
  731.     inRange (m,n) i      = m <= i && i <= n
  732.  
  733. instance Enum Int where
  734.     toEnum               = id
  735.     fromEnum             = id
  736.     enumFrom       = numericEnumFrom
  737.     enumFromTo     = numericEnumFromTo
  738.     enumFromThen   = numericEnumFromThen
  739.     enumFromThenTo = numericEnumFromThenTo
  740.  
  741. instance Enum Integer where
  742.     toEnum         = primIntToInteger
  743.     fromEnum       = primIntegerToInt
  744.     enumFrom       = numericEnumFrom
  745.     enumFromTo     = numericEnumFromTo
  746.     enumFromThen   = numericEnumFromThen
  747.     enumFromThenTo = numericEnumFromThenTo
  748.  
  749. numericEnumFrom        :: Real a => a -> [a]
  750. numericEnumFromThen    :: Real a => a -> a -> [a]
  751. numericEnumFromTo      :: Real a => a -> a -> [a]
  752. numericEnumFromThenTo  :: Real a => a -> a -> a -> [a]
  753. numericEnumFrom n            = n : (numericEnumFrom $! (n+1))
  754. numericEnumFromThen n m      = iterate ((m-n)+) n
  755. numericEnumFromTo n m        = takeWhile (<= m) (numericEnumFrom n)
  756. numericEnumFromThenTo n n' m = takeWhile p (numericEnumFromThen n n')
  757.                                where p | n' >= n   = (<= m)
  758.                        | otherwise = (>= m)
  759.  
  760. primitive primShowsInt :: Int -> Int -> ShowS
  761.  
  762. instance Read Int where
  763.     readsPrec p = readSigned readDec
  764.  
  765. instance Show Int where
  766.     showsPrec   = primShowsInt
  767.  
  768. primitive primShowsInteger :: Int -> Integer -> ShowS
  769.  
  770. instance Read Integer where
  771.     readsPrec p = readSigned readDec
  772.  
  773. instance Show Integer where
  774.     showsPrec   = primShowsInteger
  775.  
  776. -- Standard Floating types --------------------------------------------------
  777.  
  778. data Float     -- builtin datatype of single precision floating point numbers
  779. data Double    -- builtin datatype of double precision floating point numbers
  780.  
  781. primitive primEqFloat   :: Float -> Float -> Bool
  782. primitive primCmpFloat  :: Float -> Float -> Ordering
  783. primitive primEqDouble  :: Double -> Double -> Bool
  784. primitive primCmpDouble :: Double -> Double -> Ordering
  785.  
  786. instance Eq  Float  where (==) = primEqFloat
  787. instance Eq  Double where (==) = primEqDouble
  788.  
  789. instance Ord Float  where compare = primCmpFloat
  790. instance Ord Double where compare = primCmpDouble
  791.  
  792. primitive primPlusFloat,
  793.       primMinusFloat,
  794.       primMulFloat       :: Float -> Float -> Float
  795. primitive primNegFloat       :: Float -> Float
  796. primitive primIntToFloat     :: Int -> Float
  797. primitive primIntegerToFloat :: Integer -> Float
  798.  
  799. instance Num Float where
  800.     (+)           = primPlusFloat
  801.     (-)           = primMinusFloat
  802.     negate        = primNegFloat
  803.     (*)           = primMulFloat
  804.     abs           = absReal
  805.     signum        = signumReal
  806.     fromInteger   = primIntegerToFloat
  807.     fromInt       = primIntToFloat
  808.  
  809. primitive primPlusDouble,
  810.       primMinusDouble,
  811.       primMulDouble       :: Double -> Double -> Double
  812. primitive primNegDouble       :: Double -> Double
  813. primitive primIntToDouble     :: Int -> Double
  814. primitive primIntegerToDouble :: Integer -> Double
  815.  
  816. instance Num Double where
  817.     (+)         = primPlusDouble
  818.     (-)         = primMinusDouble
  819.     negate      = primNegDouble
  820.     (*)         = primMulDouble
  821.     abs         = absReal
  822.     signum      = signumReal
  823.     fromInteger = primIntegerToDouble
  824.     fromInt     = primIntToDouble
  825.  
  826. instance Real Float where
  827.     toRational = floatToRational
  828.  
  829. instance Real Double where
  830.     toRational = doubleToRational
  831.  
  832. -- Calls to these functions are optimised when passed as arguments to
  833. -- fromRational.
  834. floatToRational  :: Float  -> Rational
  835. doubleToRational :: Double -> Rational
  836. floatToRational  x = realFloatToRational x 
  837. doubleToRational x = realFloatToRational x
  838.  
  839. realFloatToRational x = (m%1)*(b%1)^^n
  840.             where (m,n) = decodeFloat x
  841.                   b     = floatRadix x
  842.  
  843. primitive primDivFloat      :: Float -> Float -> Float
  844. primitive doubleToFloat     :: Double -> Float
  845.  
  846. instance Fractional Float where
  847.     (/)          = primDivFloat
  848.     fromRational = primRationalToFloat
  849.     fromDouble   = doubleToFloat
  850.  
  851. primitive primDivDouble :: Double -> Double -> Double
  852.  
  853. instance Fractional Double where
  854.     (/)          = primDivDouble
  855.     fromRational = primRationalToDouble
  856.     fromDouble x = x
  857.  
  858. -- These primitives are equivalent to (and are defined using) 
  859. -- rationalTo{Float,Double}.  The difference is that they test to see
  860. -- if their argument is of the form (fromDouble x) - which allows a much
  861. -- more efficient implementation.
  862. primitive primRationalToFloat  :: Rational -> Float
  863. primitive primRationalToDouble :: Rational -> Double
  864.  
  865. -- These functions are used by Hugs - don't change their types.
  866. rationalToFloat  :: Rational -> Float
  867. rationalToDouble :: Rational -> Double
  868. rationalToFloat  = rationalToRealFloat
  869. rationalToDouble = rationalToRealFloat
  870.  
  871. rationalToRealFloat x = x'
  872.  where x'    = f e
  873.        f e   = if e' == e then y else f e'
  874.            where y      = encodeFloat (round (x * (1%b)^^e)) e
  875.              (_,e') = decodeFloat y
  876.        (_,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
  877.                  / fromInteger (denominator x))
  878.        b     = floatRadix x'
  879.  
  880. primitive primSinFloat,  primAsinFloat, primCosFloat,
  881.       primAcosFloat, primTanFloat,  primAtanFloat,
  882.       primLogFloat,  primExpFloat,  primSqrtFloat :: Float -> Float
  883.  
  884. instance Floating Float where
  885.     exp   = primExpFloat
  886.     log   = primLogFloat
  887.     sqrt  = primSqrtFloat
  888.     sin   = primSinFloat
  889.     cos   = primCosFloat
  890.     tan   = primTanFloat
  891.     asin  = primAsinFloat
  892.     acos  = primAcosFloat
  893.     atan  = primAtanFloat
  894.  
  895. primitive primSinDouble,  primAsinDouble, primCosDouble,
  896.       primAcosDouble, primTanDouble,  primAtanDouble,
  897.       primLogDouble,  primExpDouble,  primSqrtDouble :: Double -> Double
  898.  
  899. instance Floating Double where
  900.     exp   = primExpDouble
  901.     log   = primLogDouble
  902.     sqrt  = primSqrtDouble
  903.     sin   = primSinDouble
  904.     cos   = primCosDouble
  905.     tan   = primTanDouble
  906.     asin  = primAsinDouble
  907.     acos  = primAcosDouble
  908.     atan  = primAtanDouble
  909.  
  910. instance RealFrac Float where
  911.     properFraction = floatProperFraction
  912.  
  913. instance RealFrac Double where
  914.     properFraction = floatProperFraction
  915.  
  916. floatProperFraction x
  917.    | n >= 0      = (fromInteger m * fromInteger b ^ n, 0)
  918.    | otherwise   = (fromInteger w, encodeFloat r n)
  919.            where (m,n) = decodeFloat x
  920.              b     = floatRadix x
  921.              (w,r) = quotRem m (b^(-n))
  922.  
  923. primitive primFloatRadix  :: Integer
  924. primitive primFloatDigits :: Int
  925. primitive primFloatMinExp,
  926.           primFloatMaxExp :: Int
  927. primitive primFloatEncode :: Integer -> Int -> Float
  928. primitive primFloatDecode :: Float -> (Integer, Int)
  929.  
  930. instance RealFloat Float where
  931.     floatRadix  _ = primFloatRadix
  932.     floatDigits _ = primFloatDigits
  933.     floatRange  _ = (primFloatMinExp, primFloatMaxExp)
  934.     encodeFloat = primFloatEncode
  935.     decodeFloat = primFloatDecode
  936.     isNaN       _ = False
  937.     isInfinite  _ = False
  938.     isDenormalized _ = False
  939.     isNegativeZero _ = False
  940.     isIEEE      _ = False
  941.  
  942. primitive primDoubleRadix  :: Integer
  943. primitive primDoubleDigits :: Int
  944. primitive primDoubleMinExp,
  945.           primDoubleMaxExp :: Int
  946. primitive primDoubleEncode :: Integer -> Int -> Double
  947. primitive primDoubleDecode :: Double -> (Integer, Int)
  948.  
  949. instance RealFloat Double where
  950.     floatRadix  _ = primDoubleRadix
  951.     floatDigits _ = primDoubleDigits
  952.     floatRange  _ = (primDoubleMinExp, primDoubleMaxExp)
  953.     encodeFloat   = primDoubleEncode
  954.     decodeFloat   = primDoubleDecode
  955.     isNaN       _ = False
  956.     isInfinite  _ = False
  957.     isDenormalized _ = False
  958.     isNegativeZero _ = False
  959.     isIEEE      _ = False
  960.  
  961. instance Enum Float where
  962.     toEnum          = primIntToFloat
  963.     fromEnum          = truncate
  964.     enumFrom          = numericEnumFrom
  965.     enumFromThen      = numericEnumFromThen
  966.     enumFromTo n m      = numericEnumFromTo n (m+1/2)
  967.     enumFromThenTo n n' m = numericEnumFromThenTo n n' (m + (n'-n)/2)
  968.  
  969. instance Enum Double where
  970.     toEnum          = primIntToDouble
  971.     fromEnum          = truncate
  972.     enumFrom          = numericEnumFrom
  973.     enumFromThen      = numericEnumFromThen
  974.     enumFromTo n m      = numericEnumFromTo n (m+1/2)
  975.     enumFromThenTo n n' m = numericEnumFromThenTo n n' (m + (n'-n)/2)
  976.  
  977. primitive primShowsFloat :: Int -> Float -> ShowS
  978.  
  979. instance Read Float where
  980.     readsPrec p = readSigned readFloat
  981.  
  982. -- Note that showFloat in Numeric isn't used here
  983. instance Show Float where
  984.     showsPrec   = primShowsFloat
  985.  
  986. primitive primShowsDouble :: Int -> Double -> ShowS
  987.  
  988. instance Read Double where
  989.     readsPrec p = readSigned readFloat
  990.  
  991. -- Note that showFloat in Numeric isn't used here
  992. instance Show Double where
  993.     showsPrec   = primShowsDouble
  994.  
  995. -- Some standard functions --------------------------------------------------
  996.  
  997. fst            :: (a,b) -> a
  998. fst (x,_)       = x
  999.  
  1000. snd            :: (a,b) -> b
  1001. snd (_,y)       = y
  1002.  
  1003. curry          :: ((a,b) -> c) -> (a -> b -> c)
  1004. curry f x y     = f (x,y)
  1005.  
  1006. uncurry        :: (a -> b -> c) -> ((a,b) -> c)
  1007. uncurry f p     = f (fst p) (snd p)
  1008.  
  1009. id             :: a -> a
  1010. id    x         = x
  1011.  
  1012. const          :: a -> b -> a
  1013. const k _       = k
  1014.  
  1015. (.)            :: (b -> c) -> (a -> b) -> (a -> c)
  1016. (f . g) x       = f (g x)
  1017.  
  1018. flip           :: (a -> b -> c) -> b -> a -> c
  1019. flip f x y      = f y x
  1020.  
  1021. ($)            :: (a -> b) -> a -> b
  1022. f $ x           = f x
  1023.  
  1024. until          :: (a -> Bool) -> (a -> a) -> a -> a
  1025. until p f x     = if p x then x else until p f (f x)
  1026.  
  1027. asTypeOf       :: a -> a -> a
  1028. asTypeOf        = const
  1029.  
  1030. primitive error  :: String -> a
  1031.  
  1032. undefined        :: a
  1033. undefined | False = undefined
  1034.  
  1035. -- Standard functions on rational numbers {PreludeRatio} --------------------
  1036.  
  1037. data Integral a => Ratio a = a :% a deriving (Eq)
  1038. type Rational              = Ratio Integer
  1039.  
  1040. (%)                       :: Integral a => a -> a -> Ratio a
  1041. x % y                      = reduce (x * signum y) (abs y)
  1042.  
  1043. reduce                    :: Integral a => a -> a -> Ratio a
  1044. reduce x y | y == 0        = error "Ratio.%: zero denominator"
  1045.        | otherwise     = (x `quot` d) :% (y `quot` d)
  1046.                  where d = gcd x y
  1047.  
  1048. numerator, denominator    :: Integral a => Ratio a -> a
  1049. numerator (x :% y)         = x
  1050. denominator (x :% y)       = y
  1051.  
  1052. instance Integral a => Ord (Ratio a) where
  1053.     compare (x:%y) (x':%y') = compare (x*y') (x'*y)
  1054.  
  1055. instance Integral a => Num (Ratio a) where
  1056.     (x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
  1057.     (x:%y) * (x':%y') = reduce (x*x') (y*y')
  1058.     negate (x :% y)   = negate x :% y
  1059.     abs (x :% y)      = abs x :% y
  1060.     signum (x :% y)   = signum x :% 1
  1061.     fromInteger x     = fromInteger x :% 1
  1062.     fromInt           = intToRatio
  1063.  
  1064. -- Hugs optimises code of the form fromRational (intToRatio x)
  1065. intToRatio :: Integral a => Int -> Ratio a
  1066. intToRatio x = fromInt x :% 1
  1067.  
  1068. instance Integral a => Real (Ratio a) where
  1069.     toRational (x:%y) = toInteger x :% toInteger y
  1070.  
  1071. instance Integral a => Fractional (Ratio a) where
  1072.     (x:%y) / (x':%y')   = (x*y') % (y*x')
  1073.     recip (x:%y)        = if x < 0 then (-y) :% (-x) else y :% x
  1074.     fromRational (x:%y) = fromInteger x :% fromInteger y
  1075.     fromDouble         = doubleToRatio
  1076.  
  1077. -- Hugs optimises code of the form fromRational (doubleToRatio x)
  1078. doubleToRatio :: Integral a => Double -> Ratio a
  1079. doubleToRatio x
  1080.         | n>=0      = (fromInteger m * fromInteger b ^ n) % 1
  1081.         | otherwise = fromInteger m % (fromInteger b ^ (-n))
  1082.               where (m,n) = decodeFloat x
  1083.                 b     = floatRadix x
  1084.  
  1085. instance Integral a => RealFrac (Ratio a) where
  1086.     properFraction (x:%y) = (fromIntegral q, r:%y)
  1087.                 where (q,r) = quotRem x y
  1088.  
  1089. instance Integral a => Enum (Ratio a) where
  1090.     toEnum       = fromInt
  1091.     fromEnum     = truncate
  1092.     enumFrom     = numericEnumFrom
  1093.     enumFromThen = numericEnumFromThen
  1094.  
  1095. instance (Read a, Integral a) => Read (Ratio a) where
  1096.     readsPrec p = readParen (p > 7)
  1097.                 (\r -> [(x%y,u) | (x,s)   <- reads r,
  1098.                           ("%",t) <- lex s,
  1099.                           (y,u)   <- reads t ])
  1100.  
  1101. instance Integral a => Show (Ratio a) where
  1102.     showsPrec p (x:%y) = showParen (p > 7)
  1103.                  (shows x . showString " % " . shows y)
  1104.  
  1105. approxRational      :: RealFrac a => a -> a -> Rational
  1106. approxRational x eps = simplest (x-eps) (x+eps)
  1107.  where simplest x y | y < x     = simplest y x
  1108.             | x == y    = xr
  1109.             | x > 0     = simplest' n d n' d'
  1110.             | y < 0     = - simplest' (-n') d' (-n) d
  1111.             | otherwise = 0 :% 1
  1112.                   where xr@(n:%d) = toRational x
  1113.                     (n':%d')  = toRational y
  1114.        simplest' n d n' d'        -- assumes 0 < n%d < n'%d'
  1115.             | r == 0    = q :% 1
  1116.             | q /= q'   = (q+1) :% 1
  1117.             | otherwise = (q*n''+d'') :% n''
  1118.                   where (q,r)      = quotRem n d
  1119.                     (q',r')    = quotRem n' d'
  1120.                     (n'':%d'') = simplest' d' r' d r
  1121.  
  1122. -- Standard list functions {PreludeList} ------------------------------------
  1123.  
  1124. head             :: [a] -> a
  1125. head (x:_)        = x
  1126.  
  1127. last             :: [a] -> a
  1128. last [x]          = x
  1129. last (_:xs)       = last xs
  1130.  
  1131. tail             :: [a] -> [a]
  1132. tail (_:xs)       = xs
  1133.  
  1134. init             :: [a] -> [a]
  1135. init [x]          = []
  1136. init (x:xs)       = x : init xs
  1137.  
  1138. null             :: [a] -> Bool
  1139. null []           = True
  1140. null (_:_)        = False
  1141.  
  1142. (++)             :: [a] -> [a] -> [a]
  1143. []     ++ ys      = ys
  1144. (x:xs) ++ ys      = x : (xs ++ ys)
  1145.  
  1146. map              :: (a -> b) -> [a] -> [b]
  1147. map f xs          = [ f x | x <- xs ]
  1148.  
  1149. filter           :: (a -> Bool) -> [a] -> [a]
  1150. filter p xs       = [ x | x <- xs, p x ]
  1151.  
  1152. concat           :: [[a]] -> [a]
  1153. concat            = foldr (++) []
  1154.  
  1155. length           :: [a] -> Int
  1156. length            = foldl' (\n _ -> n + 1) 0
  1157.  
  1158. (!!)             :: [b] -> Int -> b
  1159. (x:_)  !! 0       = x
  1160. (_:xs) !! n | n>0 = xs !! (n-1)
  1161. (_:_)  !! _       = error "Prelude.!!: negative index"
  1162. []     !! _       = error "Prelude.!!: index too large"
  1163.  
  1164. foldl            :: (a -> b -> a) -> a -> [b] -> a
  1165. foldl f z []      = z
  1166. foldl f z (x:xs)  = foldl f (f z x) xs
  1167.  
  1168. foldl'           :: (a -> b -> a) -> a -> [b] -> a
  1169. foldl' f a []     = a
  1170. foldl' f a (x:xs) = (foldl' f $! f a x) xs
  1171.  
  1172. foldl1           :: (a -> a -> a) -> [a] -> a
  1173. foldl1 f (x:xs)   = foldl f x xs
  1174.  
  1175. scanl            :: (a -> b -> a) -> a -> [b] -> [a]
  1176. scanl f q xs      = q : (case xs of
  1177.              []   -> []
  1178.              x:xs -> scanl f (f q x) xs)
  1179.  
  1180. scanl1           :: (a -> a -> a) -> [a] -> [a]
  1181. scanl1 f (x:xs)   = scanl f x xs
  1182.  
  1183. foldr            :: (a -> b -> b) -> b -> [a] -> b
  1184. foldr f z []      = z
  1185. foldr f z (x:xs)  = f x (foldr f z xs)
  1186.  
  1187. foldr1           :: (a -> a -> a) -> [a] -> a
  1188. foldr1 f [x]      = x
  1189. foldr1 f (x:xs)   = f x (foldr1 f xs)
  1190.  
  1191. scanr            :: (a -> b -> b) -> b -> [a] -> [b]
  1192. scanr f q0 []     = [q0]
  1193. scanr f q0 (x:xs) = f x q : qs
  1194.             where qs@(q:_) = scanr f q0 xs
  1195.  
  1196. scanr1           :: (a -> a -> a) -> [a] -> [a]
  1197. scanr1 f [x]      = [x]
  1198. scanr1 f (x:xs)   = f x q : qs
  1199.             where qs@(q:_) = scanr1 f xs
  1200.  
  1201. iterate          :: (a -> a) -> a -> [a]
  1202. iterate f x       = x : iterate f (f x)
  1203.  
  1204. repeat           :: a -> [a]
  1205. repeat x          = xs where xs = x:xs
  1206.  
  1207. replicate        :: Int -> a -> [a]
  1208. replicate n x     = take n (repeat x)
  1209.  
  1210. cycle            :: [a] -> [a]
  1211. cycle []          = error "Prelude.cycle: empty list"
  1212. cycle xs          = xs' where xs'=xs++xs'
  1213.  
  1214. take                :: Int -> [a] -> [a]
  1215. take 0 _             = []
  1216. take _ []            = []
  1217. take n (x:xs) | n>0  = x : take (n-1) xs
  1218. take _ _             = error "Prelude.take: negative argument"
  1219.  
  1220. drop                :: Int -> [a] -> [a]
  1221. drop 0 xs            = xs
  1222. drop _ []            = []
  1223. drop n (_:xs) | n>0  = drop (n-1) xs
  1224. drop _ _             = error "Prelude.drop: negative argument"
  1225.  
  1226. splitAt               :: Int -> [a] -> ([a], [a])
  1227. splitAt 0 xs           = ([],xs)
  1228. splitAt _ []           = ([],[])
  1229. splitAt n (x:xs) | n>0 = (x:xs',xs'') where (xs',xs'') = splitAt (n-1) xs
  1230. splitAt _ _            = error "Prelude.splitAt: negative argument"
  1231.  
  1232. takeWhile           :: (a -> Bool) -> [a] -> [a]
  1233. takeWhile p []       = []
  1234. takeWhile p (x:xs)
  1235.      | p x       = x : takeWhile p xs
  1236.      | otherwise = []
  1237.  
  1238. dropWhile           :: (a -> Bool) -> [a] -> [a]
  1239. dropWhile p []       = []
  1240. dropWhile p xs@(x:xs')
  1241.      | p x       = dropWhile p xs'
  1242.      | otherwise = xs
  1243.  
  1244. span, break         :: (a -> Bool) -> [a] -> ([a],[a])
  1245. span p []            = ([],[])
  1246. span p xs@(x:xs')
  1247.      | p x       = (x:ys, zs)
  1248.      | otherwise = ([],xs)
  1249.                        where (ys,zs) = span p xs'
  1250. break p              = span (not . p)
  1251.  
  1252. lines     :: String -> [String]
  1253. lines ""   = []
  1254. lines s    = let (l,s') = break ('\n'==) s
  1255.              in l : case s' of []      -> []
  1256.                                (_:s'') -> lines s''
  1257.  
  1258. words     :: String -> [String]
  1259. words s    = case dropWhile isSpace s of
  1260.           "" -> []
  1261.           s' -> w : words s''
  1262.             where (w,s'') = break isSpace s'
  1263.  
  1264. unlines   :: [String] -> String
  1265. unlines    = concatMap (\l -> l ++ "\n")
  1266.  
  1267. unwords   :: [String] -> String
  1268. unwords [] = []
  1269. unwords ws = foldr1 (\w s -> w ++ ' ':s) ws
  1270.  
  1271. reverse   :: [a] -> [a]
  1272. reverse    = foldl (flip (:)) []
  1273.  
  1274. and, or   :: [Bool] -> Bool
  1275. and        = foldr (&&) True
  1276. or         = foldr (||) False
  1277.  
  1278. any, all  :: (a -> Bool) -> [a] -> Bool
  1279. any p      = or  . map p
  1280. all p      = and . map p
  1281.  
  1282. elem, notElem    :: Eq a => a -> [a] -> Bool
  1283. elem              = any . (==)
  1284. notElem           = all . (/=)
  1285.  
  1286. lookup           :: Eq a => a -> [(a,b)] -> Maybe b
  1287. lookup k []       = Nothing
  1288. lookup k ((x,y):xys)
  1289.       | k==x      = Just y
  1290.       | otherwise = lookup k xys
  1291.  
  1292. sum, product     :: Num a => [a] -> a
  1293. sum               = foldl' (+) 0
  1294. product           = foldl' (*) 1
  1295.  
  1296. maximum, minimum :: Ord a => [a] -> a
  1297. maximum           = foldl1 max
  1298. minimum           = foldl1 min
  1299.  
  1300. concatMap        :: (a -> [b]) -> [a] -> [b]
  1301. concatMap f       = concat . map f
  1302.  
  1303. zip              :: [a] -> [b] -> [(a,b)]
  1304. zip               = zipWith  (\a b -> (a,b))
  1305.  
  1306. zip3             :: [a] -> [b] -> [c] -> [(a,b,c)]
  1307. zip3              = zipWith3 (\a b c -> (a,b,c))
  1308.  
  1309. zipWith                  :: (a->b->c) -> [a]->[b]->[c]
  1310. zipWith z (a:as) (b:bs)   = z a b : zipWith z as bs
  1311. zipWith _ _      _        = []
  1312.  
  1313. zipWith3                 :: (a->b->c->d) -> [a]->[b]->[c]->[d]
  1314. zipWith3 z (a:as) (b:bs) (c:cs)
  1315.               = z a b c : zipWith3 z as bs cs
  1316. zipWith3 _ _ _ _          = []
  1317.  
  1318. unzip                    :: [(a,b)] -> ([a],[b])
  1319. unzip                     = foldr (\(a,b) ~(as,bs) -> (a:as, b:bs)) ([], [])
  1320.  
  1321. unzip3                   :: [(a,b,c)] -> ([a],[b],[c])
  1322. unzip3                    = foldr (\(a,b,c) ~(as,bs,cs) -> (a:as,b:bs,c:cs))
  1323.                   ([],[],[])
  1324.  
  1325. -- PreludeText ----------------------------------------------------------------
  1326.  
  1327. reads        :: Read a => ReadS a
  1328. reads         = readsPrec 0
  1329.  
  1330. shows        :: Show a => a -> ShowS
  1331. shows         = showsPrec 0
  1332.  
  1333. read         :: Read a => String -> a
  1334. read s        =  case [x | (x,t) <- reads s, ("","") <- lex t] of
  1335.               [x] -> x
  1336.               []  -> error "Prelude.read: no parse"
  1337.               _   -> error "Prelude.read: ambiguous parse"
  1338.  
  1339. showChar     :: Char -> ShowS
  1340. showChar      = (:)
  1341.  
  1342. showString   :: String -> ShowS
  1343. showString    = (++)
  1344.  
  1345. showParen    :: Bool -> ShowS -> ShowS
  1346. showParen b p = if b then showChar '(' . p . showChar ')' else p
  1347.  
  1348. showField    :: Show a => String -> a -> ShowS
  1349. showField m v = showString m . showChar '=' . shows v
  1350.  
  1351. readParen    :: Bool -> ReadS a -> ReadS a
  1352. readParen b g = if b then mandatory else optional
  1353.         where optional r  = g r ++ mandatory r
  1354.               mandatory r = [(x,u) | ("(",s) <- lex r,
  1355.                          (x,t)   <- optional s,
  1356.                          (")",u) <- lex t    ]
  1357.  
  1358. readField    :: Read a => String -> ReadS a
  1359. readField m s0 = [ r | (t,  s1) <- lex s0, t == m,
  1360.                        ("=",s2) <- lex s1,
  1361.                        r        <- reads s2 ]
  1362.  
  1363. lex                    :: ReadS String
  1364. lex ""                  = [("","")]
  1365. lex (c:s) | isSpace c   = lex (dropWhile isSpace s)
  1366. lex ('\'':s)            = [('\'':ch++"'", t) | (ch,'\'':t)  <- lexLitChar s,
  1367.                            ch /= "'"                ]
  1368. lex ('"':s)             = [('"':str, t)      | (str,t) <- lexString s]
  1369.               where
  1370.               lexString ('"':s) = [("\"",s)]
  1371.               lexString s = [(ch++str, u)
  1372.                         | (ch,t)  <- lexStrItem s,
  1373.                           (str,u) <- lexString t  ]
  1374.  
  1375.               lexStrItem ('\\':'&':s) = [("\\&",s)]
  1376.               lexStrItem ('\\':c:s) | isSpace c
  1377.                   = [("",t) | '\\':t <- [dropWhile isSpace s]]
  1378.               lexStrItem s            = lexLitChar s
  1379.  
  1380. lex (c:s) | isSingle c  = [([c],s)]
  1381.       | isSym c     = [(c:sym,t)         | (sym,t) <- [span isSym s]]
  1382.       | isAlpha c   = [(c:nam,t)         | (nam,t) <- [span isIdChar s]]
  1383.       | isDigit c   = [(c:ds++fe,t)      | (ds,s)  <- [span isDigit s],
  1384.                            (fe,t)  <- lexFracExp s     ]
  1385.       | otherwise   = []    -- bad character
  1386.         where
  1387.         isSingle c  =  c `elem` ",;()[]{}_`"
  1388.         isSym c     =  c `elem` "!@#$%&*+./<=>?\\^|:-~"
  1389.         isIdChar c  =  isAlphaNum c || c `elem` "_'"
  1390.  
  1391.         lexFracExp ('.':s) = [('.':ds++e,u) | (ds,t) <- lexDigits s,
  1392.                               (e,u)  <- lexExp t    ]
  1393.         lexFracExp s       = [("",s)]
  1394.  
  1395.         lexExp (e:s) | e `elem` "eE"
  1396.              = [(e:c:ds,u) | (c:t)  <- [s], c `elem` "+-",
  1397.                            (ds,u) <- lexDigits t] ++
  1398.                [(e:ds,t)   | (ds,t) <- lexDigits s]
  1399.         lexExp s = [("",s)]
  1400.  
  1401. lexDigits               :: ReadS String
  1402. lexDigits               =  nonnull isDigit
  1403.  
  1404. nonnull                 :: (Char -> Bool) -> ReadS String
  1405. nonnull p s             =  [(cs,t) | (cs@(_:_),t) <- [span p s]]
  1406.  
  1407. lexLitChar              :: ReadS String
  1408. lexLitChar ('\\':s)     =  [('\\':esc, t) | (esc,t) <- lexEsc s] 
  1409.     where
  1410.     lexEsc (c:s)     | c `elem` "abfnrtv\\\"'" = [([c],s)]
  1411.         lexEsc ('^':c:s) | c >= '@' && c <= '_'    = [(['^',c],s)]
  1412.     lexEsc s@(d:_)   | isDigit d               = lexDigits s
  1413.         lexEsc s@(c:_)   | isUpper c
  1414.                           = let table = ('\DEL',"DEL") : asciiTab
  1415.                 in case [(mne,s') | (c, mne) <- table,
  1416.                              ([],s') <- [lexmatch mne s]]
  1417.                    of (pr:_) -> [pr]
  1418.                       []     -> []
  1419.     lexEsc _                                   = []
  1420. lexLitChar (c:s)        =  [([c],s)]
  1421. lexLitChar ""           =  []
  1422.  
  1423. isOctDigit c  =  c >= '0' && c <= '7'
  1424. isHexDigit c  =  isDigit c || c >= 'A' && c <= 'F'
  1425.                || c >= 'a' && c <= 'f'
  1426.  
  1427. lexmatch                   :: (Eq a) => [a] -> [a] -> ([a],[a])
  1428. lexmatch (x:xs) (y:ys) | x == y  =  lexmatch xs ys
  1429. lexmatch xs     ys               =  (xs,ys)
  1430.  
  1431. asciiTab = zip ['\NUL'..' ']
  1432.        ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
  1433.         "BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI",
  1434.         "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
  1435.         "CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US",
  1436.         "SP"]
  1437.  
  1438. readLitChar            :: ReadS Char
  1439. readLitChar ('\\':s)    = readEsc s
  1440.  where
  1441.        readEsc ('a':s)  = [('\a',s)]
  1442.        readEsc ('b':s)  = [('\b',s)]
  1443.        readEsc ('f':s)  = [('\f',s)]
  1444.        readEsc ('n':s)  = [('\n',s)]
  1445.        readEsc ('r':s)  = [('\r',s)]
  1446.        readEsc ('t':s)  = [('\t',s)]
  1447.        readEsc ('v':s)  = [('\v',s)]
  1448.        readEsc ('\\':s) = [('\\',s)]
  1449.        readEsc ('"':s)  = [('"',s)]
  1450.        readEsc ('\'':s) = [('\'',s)]
  1451.        readEsc ('^':c:s) | c >= '@' && c <= '_'
  1452.             = [(toEnum (fromEnum c - fromEnum '@'), s)]
  1453.        readEsc s@(d:_) | isDigit d
  1454.             = [(toEnum n, t) | (n,t) <- readDec s]
  1455.        readEsc ('o':s)  = [(toEnum n, t) | (n,t) <- readOct s]
  1456.        readEsc ('x':s)  = [(toEnum n, t) | (n,t) <- readHex s]
  1457.        readEsc s@(c:_) | isUpper c
  1458.             = let table = ('\DEL',"DEL") : asciiTab
  1459.               in case [(c,s') | (c, mne) <- table,
  1460.                         ([],s') <- [lexmatch mne s]]
  1461.                  of (pr:_) -> [pr]
  1462.                 []     -> []
  1463.        readEsc _        = []
  1464. readLitChar (c:s)       = [(c,s)]
  1465.  
  1466. showLitChar               :: Char -> ShowS
  1467. showLitChar c | c > '\DEL' = showChar '\\' .
  1468.                  protectEsc isDigit (shows (fromEnum c))
  1469. showLitChar '\DEL'         = showString "\\DEL"
  1470. showLitChar '\\'           = showString "\\\\"
  1471. showLitChar c | c >= ' '   = showChar c
  1472. showLitChar '\a'           = showString "\\a"
  1473. showLitChar '\b'           = showString "\\b"
  1474. showLitChar '\f'           = showString "\\f"
  1475. showLitChar '\n'           = showString "\\n"
  1476. showLitChar '\r'           = showString "\\r"
  1477. showLitChar '\t'           = showString "\\t"
  1478. showLitChar '\v'           = showString "\\v"
  1479. showLitChar '\SO'          = protectEsc ('H'==) (showString "\\SO")
  1480. showLitChar c              = showString ('\\' : snd (asciiTab!!fromEnum c))
  1481.  
  1482. protectEsc p f             = f . cont
  1483.  where cont s@(c:_) | p c  = "\\&" ++ s
  1484.        cont s              = s
  1485.  
  1486. -- Unsigned readers for various bases
  1487. readDec, readOct, readHex :: Integral a => ReadS a
  1488. readDec = readInt 10 isDigit (\d -> fromEnum d - fromEnum '0')
  1489. readOct = readInt  8 isOctDigit (\d -> fromEnum d - fromEnum '0')
  1490. readHex = readInt 16 isHexDigit hex
  1491.       where hex d = fromEnum d -
  1492.             (if isDigit d
  1493.                then fromEnum '0'
  1494.                else fromEnum (if isUpper d then 'A' else 'a') - 10)
  1495.  
  1496. -- readInt reads a string of digits using an arbitrary base.  
  1497. -- Leading minus signs must be handled elsewhere.
  1498.  
  1499. readInt :: Integral a => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
  1500. readInt radix isDig digToInt s =
  1501.     [(foldl1 (\n d -> n * radix + d) (map (fromIntegral . digToInt) ds), r)
  1502.     | (ds,r) <- nonnull isDig s ]
  1503.  
  1504. -- showInt is used for positive numbers only
  1505. showInt    :: Integral a => a -> ShowS
  1506. showInt n r | n < 0 = error "Numeric.showInt: can't show negative numbers"
  1507.             | otherwise =
  1508.               let (n',d) = quotRem n 10
  1509.           r'     = toEnum (fromEnum '0' + fromIntegral d) : r
  1510.           in  if n' == 0 then r' else showInt n' r'
  1511.  
  1512. readSigned:: Real a => ReadS a -> ReadS a
  1513. readSigned readPos = readParen False read'
  1514.              where read' r  = read'' r ++
  1515.                       [(-x,t) | ("-",s) <- lex r,
  1516.                         (x,t)   <- read'' s]
  1517.                read'' r = [(n,s)  | (str,s) <- lex r,
  1518.                         (n,"")  <- readPos str]
  1519.  
  1520. showSigned    :: Real a => (a -> ShowS) -> Int -> a -> ShowS
  1521. showSigned showPos p x = if x < 0 then showParen (p > 6)
  1522.                          (showChar '-' . showPos (-x))
  1523.                   else showPos x
  1524.  
  1525. readFloat     :: RealFloat a => ReadS a
  1526. readFloat r    = [(fromRational ((n%1)*10^^(k-d)),t) | (n,d,s) <- readFix r,
  1527.                                (k,t)   <- readExp s]
  1528.          where readFix r = [(read (ds++ds'), length ds', t)
  1529.                     | (ds, s) <- lexDigits r
  1530.                                         , (ds',t) <- lexFrac s   ]
  1531.  
  1532.                        lexFrac ('.':s) = lexDigits s
  1533.                lexFrac s       = [("",s)]
  1534.  
  1535.                readExp (e:s) | e `elem` "eE" = readExp' s
  1536.                readExp s                     = [(0,s)]
  1537.  
  1538.                readExp' ('-':s) = [(-k,t) | (k,t) <- readDec s]
  1539.                readExp' ('+':s) = readDec s
  1540.                readExp' s       = readDec s
  1541.  
  1542. -- Monadic I/O: --------------------------------------------------------------
  1543.  
  1544. --data IO a             -- builtin datatype of IO actions
  1545. data IOError            -- builtin datatype of IO error codes
  1546. type FilePath = String  -- file pathnames are represented by strings
  1547.  
  1548. instance Show (IO a) where
  1549.     showsPrec p f = showString "<<IO action>>"
  1550.  
  1551. primitive primbindIO   "rbindIO" :: IO a -> (a -> IO b) -> IO b
  1552. primitive primretIO    "runitIO" :: a -> IO a
  1553. primitive catch        "lbindIO" :: IO a -> (IOError -> IO a) -> IO a
  1554. primitive ioError      "lunitIO" :: IOError -> IO a
  1555. primitive putChar         :: Char -> IO ()
  1556. primitive putStr         :: String -> IO ()
  1557. primitive getChar            :: IO Char
  1558. primitive userError             :: String -> IOError
  1559.  
  1560. print     :: Show a => a -> IO ()
  1561. print      = putStrLn . show
  1562.  
  1563. putStrLn  :: String -> IO ()
  1564. putStrLn s = do putStr s
  1565.         putChar '\n'
  1566.  
  1567. getLine   :: IO String
  1568. getLine    = do c <- getChar
  1569.         if c=='\n' then return ""
  1570.                else do cs <- getLine
  1571.                    return (c:cs)
  1572.  
  1573. -- raises an exception instead of an error
  1574. readIO          :: Read a => String -> IO a
  1575. readIO s         = case [x | (x,t) <- reads s, ("","") <- lex t] of
  1576.                         [x] -> return x
  1577.                         []  -> ioError (userError "PreludeIO.readIO: no parse")
  1578.                         _   -> ioError (userError 
  1579.                                        "PreludeIO.readIO: ambiguous parse")
  1580.  
  1581. readLn          :: Read a => IO a
  1582. readLn           = do l <- getLine
  1583.                       r <- readIO l
  1584.                       return r
  1585.  
  1586. primitive getContents          :: IO String
  1587. primitive writeFile            :: FilePath -> String -> IO ()
  1588. primitive appendFile           :: FilePath -> String -> IO ()
  1589. primitive readFile             :: FilePath -> IO String
  1590.  
  1591. interact  :: (String -> String) -> IO ()
  1592. interact f = getContents >>= (putStr . f)
  1593.  
  1594. instance Functor IO where
  1595.     fmap f x = x >>= (return . f)
  1596.  
  1597. instance Monad IO where
  1598.     (>>=)  = primbindIO
  1599.     return = primretIO
  1600.  
  1601.  
  1602. -- Hooks for primitives: -----------------------------------------------------
  1603. -- Do not mess with these!
  1604.  
  1605. data Addr     -- builtin datatype of C pointers
  1606.  
  1607. newtype IO a = IO ((IOError -> IOResult a) -> (a -> IOResult a) -> IOResult a)
  1608. data IOResult a 
  1609.   = Hugs_ExitWith Int
  1610.   | Hugs_SuspendThread
  1611.   | Hugs_Error    IOError
  1612.   | Hugs_Return   a
  1613.  
  1614. hugsPutStr :: String -> IO ()
  1615. hugsPutStr  = putStr
  1616.  
  1617. hugsIORun  :: IO a -> Either Int a
  1618. hugsIORun m = performIO (runAndShowError m)
  1619.  where
  1620.   performIO       :: IO a -> Either Int a
  1621.   performIO (IO m) = case m Hugs_Error Hugs_Return of
  1622.                  Hugs_Return a   -> Right a
  1623.              Hugs_ExitWith e -> Left  e
  1624.              _               -> Left  1
  1625.  
  1626.   runAndShowError :: IO a -> IO a
  1627.   runAndShowError m =
  1628.     m `catch` \err -> do 
  1629.     putChar '\n'
  1630.     putStr (ioeGetErrorString err)
  1631.     primExitWith 1 -- alternatively: (IO (\f s -> Hugs_SuspendThread))
  1632.  
  1633. primExitWith     :: Int -> IO a
  1634. primExitWith c    = IO (\ f s -> Hugs_ExitWith c)
  1635.  
  1636. primitive ioeGetErrorString "primShowIOError" :: IOError -> String
  1637.  
  1638. instance Show IOError where
  1639.   showsPrec p x = showString (ioeGetErrorString x)
  1640.  
  1641. primCompAux      :: Ord a => a -> a -> Ordering -> Ordering
  1642. primCompAux x y o = case compare x y of EQ -> o; LT -> LT; GT -> GT
  1643.  
  1644. primPmInt        :: Num a => Int -> a -> Bool
  1645. primPmInt n x     = fromInt n == x
  1646.  
  1647. primPmInteger    :: Num a => Integer -> a -> Bool
  1648. primPmInteger n x = fromInteger n == x
  1649.  
  1650. primPmFlt        :: Fractional a => Double -> a -> Bool
  1651. primPmFlt n x     = fromDouble n == x
  1652.  
  1653. -- The following primitives are only needed if (n+k) patterns are enabled:
  1654. primPmNpk        :: Integral a => Int -> a -> Maybe a
  1655. primPmNpk n x     = if n'<=x then Just (x-n') else Nothing
  1656.             where n' = fromInt n
  1657.  
  1658. primPmSub        :: Integral a => Int -> a -> a
  1659. primPmSub n x     = x - fromInt n
  1660.  
  1661. -- End of Hugs standard prelude ----------------------------------------------
  1662.