home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacHaskell 2.2 / progs / prelude / PreludeCore.hs < prev    next >
Encoding:
Text File  |  1994-09-27  |  19.3 KB  |  705 lines  |  [TEXT/YHS2]

  1. -- Standard types, classes, and instances
  2.  
  3. module PreludeCore (
  4.     Eq((==), (/=)),
  5.     Ord((<), (<=), (>=), (>), max, min),
  6.     Num((+), (-), (*), negate, abs, signum, fromInteger),
  7.     Integral(quot, rem, div, mod, quotRem, divMod, even, odd, toInteger),
  8.     Fractional((/), recip, fromRational),
  9.     Floating(pi, exp, log, sqrt, (**), logBase,
  10.          sin, cos, tan, asin, acos, atan,
  11.          sinh, cosh, tanh, asinh, acosh, atanh),
  12.     Real(toRational),
  13.     RealFrac(properFraction, truncate, round, ceiling, floor),
  14.     RealFloat(floatRadix, floatDigits, floatRange,
  15.           encodeFloat, decodeFloat, exponent, significand, scaleFloat,
  16.           atan2),
  17.     Ix(range, index, inRange),
  18.     Enum(enumFrom, enumFromThen, enumFromTo, enumFromThenTo),
  19.     Text(readsPrec, showsPrec, readList, showList), ReadS(..), ShowS(..),
  20.     Binary(readBin, showBin),
  21. --  List type: [_]((:), [])
  22. --  Tuple types: (_,_), (_,_,_), etc.
  23. --  Trivial type: () 
  24.     Bool(True, False),
  25.     Char, Int, Integer, Float, Double, Bin,
  26.     Ratio, Complex((:+)), Array,
  27.     PreludeC.. , PreludeDerivings..,
  28.     String(..), Rational(..) )  where
  29.  
  30. {-#Prelude#-}  -- Indicates definitions of compiler prelude symbols
  31.  
  32. import PreludePrims
  33. import PreludeText
  34. import PreludeRatio(Ratio, Rational(..))
  35. import PreludeComplex(Complex((:+)))
  36. import PreludeArray(Array)
  37. --import PreludeIO({-Request, Response,-} IOError,
  38. --         Dialogue(..), SuccCont(..), StrCont(..), 
  39. --         StrListCont(..), BinCont(..), FailCont(..))
  40. import PreludeC
  41. import PreludeDerivings
  42.  
  43. infixr 8  **
  44. infixl 7  *, /, `quot`, `rem`, `div`, `mod`
  45. infixl 6  +, -
  46. infix  4  ==, /=, <, <=, >=, >
  47.  
  48.  
  49. infixr 5 :
  50.  
  51. data Int = MkInt
  52. data Integer = MkInteger
  53. data Float = MkFloat
  54. data Double   = MkDouble
  55. data Char = MkChar
  56. data Bin = MkBin
  57. data List a = a : (List a) | Nil  deriving (Eq, Ord)
  58. {-# ImportLispType (List ((:)("pair?","cons","car","cdr"),
  59.                           Nil("null?","'()"))) #-}
  60.  
  61.  
  62. data Arrow a b = MkArrow a b
  63. data UnitType = UnitConstructor deriving (Eq, Ord, Ix, Enum, Binary)
  64.  
  65. -- Equality and Ordered classes
  66.  
  67. class  Eq a  where
  68.     (==), (/=)        :: a -> a -> Bool
  69.  
  70.     x /= y        =  not (x == y)
  71.  
  72.     {-# (/=) :: Inline #-}
  73.  
  74.  
  75. class  (Eq a) => Ord a  where
  76.     (<), (<=), (>=), (>):: a -> a -> Bool
  77.     max, min        :: a -> a -> a
  78.  
  79.     x <     y        =  x <= y && x /= y
  80.     x >= y        =  y <= x
  81.     x >     y        =  y <    x
  82.  
  83.     -- The following default methods are appropriate for partial orders.
  84.     -- Note that the second guards in each function can be replaced
  85.     -- by "otherwise" and the error cases, eliminated for total orders.
  86.     max x y | x >= y    =  x
  87.         | y >= x    =  y
  88.         |otherwise    =  error "max{PreludeCore}: no ordering relation"
  89.     min x y | x <= y    =  x
  90.         | y <= x    =  y
  91.         |otherwise    =  error "min{PreludeCore}: no ordering relation"
  92.  
  93.     {-# (<) :: Inline #-}
  94.     {-# (>=) :: Inline #-}
  95.     {-# (>) :: Inline #-}
  96.     {-# max :: Inline #-}
  97.     {-# min :: Inline #-}
  98.  
  99.  
  100.  
  101. -- Numeric classes
  102.  
  103. class  (Eq a, Text a) => Num a  where
  104.     (+), (-), (*)    :: a -> a -> a
  105.     negate        :: a -> a
  106.     abs, signum        :: a -> a
  107.     fromInteger        :: Integer -> a
  108.  
  109.     x - y        =  x + negate y
  110.  
  111.     {-# (-) :: Inline #-}
  112.  
  113.  
  114. class  (Num a, Enum a) => Real a  where
  115.     toRational        ::  a -> Rational
  116.  
  117. class  (Real a, Ix a) => Integral a  where
  118.     quot, rem, div, mod    :: a -> a -> a
  119.     quotRem, divMod    :: a -> a -> (a,a)
  120.     even, odd        :: a -> Bool
  121.     toInteger        :: a -> Integer
  122.  
  123.     n `quot` d        =  q  where (q,r) = quotRem n d
  124.     n `rem` d        =  r  where (q,r) = quotRem n d
  125.     n `div` d        =  q  where (q,r) = divMod n d
  126.     n `mod` d        =  r  where (q,r) = divMod n d
  127.     divMod n d         =  if signum r == - signum d then (q-1, r+d) else qr
  128.                where qr@(q,r) = quotRem n d
  129.     even n        =  n `rem` 2 == 0
  130.     odd            =  not . even
  131.  
  132.     {-# quot :: Inline #-}
  133.     {-# rem  :: Inline #-}
  134.     {-# div  :: Inline #-}
  135.     {-# mod  :: Inline #-}
  136.     {-# divMod :: Inline #-}
  137.     {-# even :: Inline #-}
  138.     {-# odd  :: Inline #-}
  139.  
  140.  
  141.  
  142. class  (Num a) => Fractional a  where
  143.     (/)            :: a -> a -> a
  144.     recip        :: a -> a
  145.     fromRational    :: Rational -> a
  146.  
  147.     recip x        =  1 / x
  148.  
  149.     {-# recip  :: Inline #-}
  150.  
  151. class  (Fractional a) => Floating a  where
  152.     pi            :: a
  153.     exp, log, sqrt    :: a -> a
  154.     (**), logBase    :: a -> a -> a
  155.     sin, cos, tan    :: a -> a
  156.     asin, acos, atan    :: a -> a
  157.     sinh, cosh, tanh    :: a -> a
  158.     asinh, acosh, atanh :: a -> a
  159.  
  160.     x ** y        =  exp (log x * y)
  161.     logBase x y        =  log y / log x
  162.     sqrt x        =  x ** 0.5
  163.     tan  x        =  sin  x / cos  x
  164.     tanh x        =  sinh x / cosh x
  165.  
  166.  
  167.  
  168. class  (Real a, Fractional a) => RealFrac a  where
  169.     properFraction    :: (Integral b) => a -> (b,a)
  170.     truncate, round    :: (Integral b) => a -> b
  171.     ceiling, floor    :: (Integral b) => a -> b
  172.  
  173.     truncate x        =  m  where (m,_) = properFraction x
  174.     
  175.     round x        =  let (n,r) = properFraction x
  176.                        m     = if r < 0 then n - 1 else n + 1
  177.                    in case signum (abs r - 0.5) of
  178.                     -1 -> n
  179.                      0  -> if even n then n else m
  180.                     1  -> m
  181.     
  182.     ceiling x        =  if r > 0 then n + 1 else n
  183.                    where (n,r) = properFraction x
  184.     
  185.     floor x        =  if r < 0 then n - 1 else n
  186.                    where (n,r) = properFraction x
  187.  
  188. class  (RealFrac a, Floating a) => RealFloat a  where
  189.     floatRadix        :: a -> Integer
  190.     floatDigits        :: a -> Int
  191.     floatRange        :: a -> (Int,Int)
  192.     decodeFloat        :: a -> (Integer,Int)
  193.     encodeFloat        :: Integer -> Int -> a
  194.     exponent        :: a -> Int
  195.     significand        :: a -> a
  196.     scaleFloat        :: Int -> a -> a
  197.     atan2               :: a -> a -> a
  198.  
  199.  
  200.     exponent x        =  if m == 0 then 0 else n + floatDigits x
  201.                where (m,n) = decodeFloat x
  202.  
  203.     significand x    =  encodeFloat m (- floatDigits x)
  204.                where (m,_) = decodeFloat x
  205.  
  206.     scaleFloat k x    =  encodeFloat m (n+k)
  207.                where (m,n) = decodeFloat x
  208.  
  209.     atan2 y x    =  case (signum y, signum x) of
  210.             ( 0, 1) ->  0
  211.             ( 1, 0) ->  pi/2
  212.             ( 0,-1) ->  pi
  213.             (-1, 0) -> -pi/2
  214.             ( _, 1) ->  atan (y/x)
  215.             ( _,-1) ->  atan (y/x) + pi
  216.             ( 0, 0) ->  error "atan2{Prelude}: atan2 of origin"
  217.  
  218.  
  219. -- Index and Enumeration classes
  220.  
  221. class  (Ord a, Text a) => Ix a  where   -- This is a Yale modification
  222.     range        :: (a,a) -> [a]
  223.     index        :: (a,a) -> a -> Int
  224.     inRange        :: (a,a) -> a -> Bool
  225.  
  226. indexError :: Text a => a -> a -> a -> b
  227. indexError i m n =
  228.   error ("Index " ++ show i ++ " is outside the range (" ++
  229.                      show m ++ "," ++ show n ++ ")\n")
  230.  
  231. class  (Ord a) => Enum a    where
  232.     enumFrom        :: a -> [a]        -- [n..]
  233.     enumFromThen    :: a -> a -> [a]    -- [n,n'..]
  234.     enumFromTo        :: a -> a -> [a]    -- [n..m]
  235.     enumFromThenTo    :: a -> a -> a -> [a]    -- [n,n'..m]
  236.  
  237.     enumFromTo          = defaultEnumFromTo
  238.     enumFromThenTo      = defaultEnumFromThenTo
  239.  
  240. defaultEnumFromTo n m    =  takeWhile (<= m) (enumFrom n)
  241. defaultEnumFromThenTo n n' m
  242.             =  takeWhile (if n' >= n then (<= m) else (>= m))
  243.                      (enumFromThen n n')
  244. {-# defaultEnumFromTo :: Inline #-}
  245. {-# defaultEnumFromThenTo :: Inline #-}
  246.  
  247. -- Text class
  248.  
  249. type  ReadS a = String -> [(a,String)]
  250. type  ShowS   = String -> String
  251.  
  252. class  Text a  where
  253.     readsPrec :: Int -> ReadS a
  254.     showsPrec :: Int -> a -> ShowS
  255.     readList  :: ReadS [a]
  256.     showList  :: [a] -> ShowS
  257.  
  258.     readList    = readParen False (\r -> [pr | ("[",s)    <- lex r,
  259.                            pr    <- readl s])
  260.               where readl  s = [([],t)   | ("]",t)  <- lex s] ++
  261.                    [(x:xs,u) | (x,t)    <- reads s,
  262.                            (xs,u)   <- readl' t]
  263.             readl' s = [([],t)   | ("]",t)  <- lex s] ++
  264.                        [(x:xs,v) | (",",t)  <- lex s,
  265.                            (x,u)    <- reads t,
  266.                            (xs,v)   <- readl' u]
  267.     showList []    = showString "[]"
  268.     showList (x:xs)
  269.         = showChar '[' . shows x . showl xs
  270.           where showl []     = showChar ']'
  271.             showl (x:xs) = showString ", " . shows x . showl xs
  272.  
  273.  
  274.  
  275. -- Binary class
  276.  
  277. class  Binary a  where
  278.     readBin        :: Bin -> (a,Bin)
  279.     showBin        :: a -> Bin -> Bin
  280.  
  281.  
  282. -- Trivial type
  283.  
  284. -- data  ()  =  ()  deriving (Eq, Ord, Ix, Enum, Binary)
  285.  
  286. instance  Text ()  where
  287.     readsPrec p    = readParen False
  288.                             (\r -> [((),t) | ("(",s) <- lex r,
  289.                          (")",t) <- lex s ] )
  290.     showsPrec p () = showString "()"
  291.  
  292.  
  293. -- Binary type
  294.  
  295. instance  Text Bin  where
  296.     readsPrec p s  =  error "readsPrec{PreludeText}: Cannot read Bin."
  297.     showsPrec p b  =  showString "<<Bin>>"
  298.  
  299.  
  300. -- Boolean type
  301.  
  302. data  Bool  =  False | True  deriving (Eq, Ord, Ix, Enum, Text, Binary)
  303. {-# ImportLispType (Bool ( False("'#f"), True("'#t"))) #-}
  304.  
  305.  
  306. -- Character type
  307.  
  308. instance  Eq Char  where
  309.     (==)        =  primEqChar
  310.     (/=)                =  primNeqChar
  311.  
  312. instance  Ord Char  where
  313.     (<)                 =  primLsChar
  314.     (<=)        =  primLeChar
  315.     (>)                 =  primGtChar
  316.     (>=)                =  primGeChar
  317.  
  318. instance  Ix Char  where
  319.     range (c,c')    =  [c..c']
  320.     index b@(c,c') ci
  321.     | inRange b ci    =  ord ci - ord c
  322.     | otherwise    =  indexError ci c c'
  323.     inRange (c,c') ci    =  ord c <= i && i <= ord c'
  324.                where i = ord ci
  325.     {-# range :: Inline #-}
  326.  
  327. instance  Enum Char  where
  328.     enumFrom        = charEnumFrom
  329.     enumFromThen        = charEnumFromThen
  330.     enumFromTo          = defaultEnumFromTo
  331.     enumFromThenTo      = defaultEnumFromThenTo
  332.     {-# enumFrom :: Inline #-}
  333.     {-# enumFromThen :: Inline #-}
  334.     {-# enumFromTo :: Inline #-}
  335.     {-# enumFromThenTo :: Inline #-}
  336.  
  337. charEnumFrom c        =  map chr [ord c .. ord maxChar]
  338. charEnumFromThen c c'    =  map chr [ord c, ord c' .. ord lastChar]
  339.                where lastChar = if c' < c then minChar else maxChar
  340. {-# charEnumFrom :: Inline #-}
  341. {-# charEnumFromThen :: Inline #-}
  342.  
  343. instance  Text Char  where
  344.     readsPrec p      = readParen False
  345.                             (\r -> [(c,t) | ('\'':s,t)<- lex r,
  346.                         (c,_)     <- readLitChar s])
  347.  
  348.     showsPrec p '\'' = showString "'\\''"
  349.     showsPrec p c    = showChar '\'' . showLitChar c . showChar '\''
  350.  
  351.     readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
  352.                            (l,_)      <- readl s ])
  353.            where readl ('"':s)    = [("",s)]
  354.              readl ('\\':'&':s)    = readl s
  355.              readl s        = [(c:cs,u) | (c ,t) <- readLitChar s,
  356.                               (cs,u) <- readl t          ]
  357.  
  358.     showList cs = showChar '"' . showl cs
  359.          where showl ""       = showChar '"'
  360.                showl ('"':cs) = showString "\\\"" . showl cs
  361.                showl (c:cs)   = showLitChar c . showl cs
  362.  
  363. type  String = [Char]
  364.  
  365.  
  366. -- Standard Integral types
  367.  
  368. instance  Eq Int  where
  369.     (==)        =  primEqInt
  370.     (/=)                =  primNeqInt
  371.  
  372. instance  Eq Integer  where
  373.     (==)        =  primEqInteger
  374.     (/=)                =  primNeqInteger
  375.  
  376. instance  Ord Int  where
  377.     (<)                 =  primLsInt
  378.     (<=)        =  primLeInt
  379.     (>)                 =  primGtInt
  380.     (>=)                =  primGeInt
  381.     max                 =  primIntMax
  382.     min                 =  primIntMin
  383.  
  384. instance  Ord Integer  where
  385.     (<)                 =  primLsInteger
  386.     (<=)        =  primLeInteger
  387.     (>)                 =  primGtInteger
  388.     (>=)                =  primGeInteger
  389.     max                 =  primIntegerMax
  390.     min                 =  primIntegerMin
  391.  
  392. instance  Num Int  where
  393.     (+)            =  primPlusInt
  394.     (-)                 =  primMinusInt
  395.     negate        =  primNegInt
  396.     (*)            =  primMulInt
  397.     abs            =  primAbsInt
  398.     signum        =  signumReal
  399.     fromInteger        =  primIntegerToInt
  400.  
  401. instance  Num Integer  where
  402.     (+)            =  primPlusInteger
  403.     (-)                 =  primMinusInteger
  404.     negate        =  primNegInteger
  405.     (*)            =  primMulInteger
  406.     abs            =  primAbsInteger
  407.     signum        =  signumReal
  408.     fromInteger x    =  x
  409.     
  410. signumReal x | x == 0     =  0
  411.             | x > 0     =  1
  412.          | otherwise = -1
  413.  
  414. instance  Real Int  where
  415.     toRational x    =  toInteger x % 1
  416.  
  417. instance  Real Integer    where
  418.     toRational x    =  x % 1
  419.  
  420. instance  Integral Int    where
  421.     quotRem        =  primQuotRemInt
  422.     toInteger        =  primIntToInteger
  423.  
  424. instance  Integral Integer  where
  425.     quotRem        =  primQuotRemInteger
  426.     toInteger x        =  x
  427.  
  428. instance  Ix Int  where
  429.     range (m,n)        =  [m..n]
  430.     index b@(m,n) i
  431.     | inRange b i    =  i - m
  432.     | otherwise    =  indexError i m n
  433.     inRange (m,n) i    =  m <= i && i <= n
  434.     {-# range :: Inline #-}
  435.  
  436. instance  Ix Integer  where
  437.     range (m,n)        =  [m..n]
  438.     index b@(m,n) i
  439.     | inRange b i    =  fromInteger (i - m)
  440.     | otherwise    =  indexError i m n
  441.     inRange (m,n) i    =  m <= i && i <= n
  442.     {-# range :: Inline #-}
  443.  
  444. instance  Enum Int  where
  445.     enumFrom        =  numericEnumFrom
  446.     enumFromThen    =  numericEnumFromThen
  447.     enumFromTo          = defaultEnumFromTo
  448.     enumFromThenTo      = defaultEnumFromThenTo
  449.     {-# enumFrom :: Inline #-}
  450.     {-# enumFromThen :: Inline #-}
  451.     {-# enumFromTo :: Inline #-}
  452.     {-# enumFromThenTo :: Inline #-}
  453.  
  454. instance  Enum Integer  where
  455.     enumFrom        =  numericEnumFrom
  456.     enumFromThen    =  numericEnumFromThen
  457.     enumFromTo          = defaultEnumFromTo
  458.     enumFromThenTo      = defaultEnumFromThenTo
  459.     {-# enumFrom :: Inline #-}
  460.     {-# enumFromThen :: Inline #-}
  461.     {-# enumFromTo :: Inline #-}
  462.     {-# enumFromThenTo :: Inline #-}
  463.  
  464. numericEnumFrom        :: (Real a) => a -> [a]
  465. numericEnumFromThen    :: (Real a) => a -> a -> [a]
  466. numericEnumFrom        =  iterate (+1)
  467. numericEnumFromThen n m    =  iterate (+(m-n)) n
  468.  
  469. {-# numericEnumFrom :: Inline #-}
  470. {-# numericEnumFromThen :: Inline #-}
  471.  
  472.  
  473. instance  Text Int  where
  474.     readsPrec p        = readSigned readDec
  475.     showsPrec       = showSigned showInt
  476.  
  477. instance  Text Integer  where
  478.     readsPrec p     = readSigned readDec
  479.     showsPrec        = showSigned showInt
  480.  
  481.  
  482. -- Standard Floating types
  483.  
  484. instance  Eq Float  where
  485.     (==)        =  primEqFloat
  486.     (/=)                =  primNeqFloat
  487.  
  488. instance  Eq Double  where
  489.     (==)        =  primEqDouble
  490.     (/=)                =  primNeqDouble
  491.  
  492. instance  Ord Float  where
  493.     (<)                 =  primLsFloat
  494.     (<=)        =  primLeFloat
  495.     (>)                 =  primGtFloat
  496.     (>=)                =  primGeFloat
  497.     max                 =  primFloatMax
  498.     min                 =  primFloatMin
  499.  
  500. instance  Ord Double  where
  501.     (<)                 =  primLsDouble
  502.     (<=)        =  primLeDouble
  503.     (>)                 =  primGtDouble
  504.     (>=)                =  primGeDouble
  505.     max                 =  primDoubleMax
  506.     min                 =  primDoubleMax
  507.  
  508. instance  Num Float  where
  509.     (+)            =  primPlusFloat
  510.     (-)                 =  primMinusFloat
  511.     negate        =  primNegFloat
  512.     (*)            =  primMulFloat
  513.     abs            =  primAbsFloat
  514.     signum        =  signumReal
  515.     fromInteger n    =  encodeFloat n 0
  516.  
  517. instance  Num Double  where
  518.     (+)            =  primPlusDouble
  519.     (-)                 =  primMinusDouble
  520.     negate        =  primNegDouble
  521.     (*)            =  primMulDouble
  522.     abs            =  primAbsDouble
  523.     signum        =  signumReal
  524.     fromInteger n    =  encodeFloat n 0
  525.  
  526. instance  Real Float  where
  527.     toRational        =  primFloatToRational
  528.  
  529. instance  Real Double  where
  530.     toRational        =  primDoubleToRational
  531.  
  532. -- realFloatToRational x    =  (m%1)*(b%1)^^n
  533. --                where (m,n) = decodeFloat x
  534. --                  b     = floatRadix  x
  535.  
  536. instance  Fractional Float  where
  537.     (/)            =  primDivFloat
  538.     fromRational        =  primRationalToFloat
  539. --    fromRational    =  rationalToRealFloat
  540.  
  541. instance  Fractional Double  where
  542.     (/)            =  primDivDouble
  543.     fromRational        =  primRationalToDouble
  544. --    fromRational    =  rationalToRealFloat
  545.  
  546. -- rationalToRealFloat x    = x'
  547. --         where x'    = f e
  548. --               f e   = if e' == e then y else f e'
  549. --                       where y      = encodeFloat (round (x * (1%b)^^e)) e
  550. --                             (_,e') = decodeFloat y
  551. --               (_,e) = decodeFloat (fromInteger (numerator x) `asTypeOf` x'
  552. --                                         / fromInteger (denominator x))
  553. --               b     = floatRadix x'
  554.  
  555. instance  Floating Float  where
  556.     pi            =  primPiFloat
  557.     exp            =  primExpFloat
  558.     log            =  primLogFloat
  559.     sqrt        =  primSqrtFloat
  560.     sin            =  primSinFloat
  561.     cos            =  primCosFloat
  562.     tan            =  primTanFloat
  563.     asin        =  primAsinFloat
  564.     acos        =  primAcosFloat
  565.     atan        =  primAtanFloat
  566.     sinh        =  primSinhFloat
  567.     cosh        =  primCoshFloat
  568.     tanh        =  primTanhFloat
  569.     asinh        =  primAsinhFloat
  570.     acosh        =  primAcoshFloat
  571.     atanh        =  primAtanhFloat
  572.  
  573. instance  Floating Double  where
  574.     pi            =  primPiDouble
  575.     exp            =  primExpDouble
  576.     log            =  primLogDouble
  577.     sqrt        =  primSqrtDouble
  578.     sin            =  primSinDouble
  579.     cos            =  primCosDouble
  580.     tan            =  primTanDouble
  581.     asin        =  primAsinDouble
  582.     acos        =  primAcosDouble
  583.     atan        =  primAtanDouble
  584.     sinh        =  primSinhDouble
  585.     cosh        =  primCoshDouble
  586.     tanh        =  primTanhDouble
  587.     asinh        =  primAsinhDouble
  588.     acosh        =  primAcoshDouble
  589.     atanh        =  primAtanhDouble
  590.  
  591.  
  592. instance  RealFrac Float  where
  593.     properFraction    =  floatProperFraction
  594.  
  595. instance  RealFrac Double  where
  596.     properFraction    =  floatProperFraction
  597.  
  598. floatProperFraction x
  599.     | n >= 0    =  (fromInteger m * fromInteger b ^ n, 0)
  600.     | otherwise    =  (fromInteger w, encodeFloat r n)
  601.             where (m,n) = decodeFloat x
  602.                   b     = floatRadix x
  603.                   (w,r) = quotRem m (b^(-n))
  604.  
  605. instance  RealFloat Float  where
  606.     floatRadix _    =  primFloatRadix
  607.     floatDigits _    =  primFloatDigits
  608.     floatRange _    =  (primFloatMinExp,primFloatMaxExp)
  609.     decodeFloat        =  primDecodeFloat
  610.     encodeFloat        =  primEncodeFloat
  611.     atan2               =  primAtan2Float
  612.  
  613. instance  RealFloat Double  where
  614.     floatRadix _    =  primDoubleRadix
  615.     floatDigits    _    =  primDoubleDigits
  616.     floatRange _    =  (primDoubleMinExp,primDoubleMaxExp)
  617.     decodeFloat        =  primDecodeDouble
  618.     encodeFloat        =  primEncodeDouble
  619.     atan2               =  primAtan2Double
  620.  
  621. instance  Enum Float  where
  622.     enumFrom        =  numericEnumFrom
  623.     enumFromThen    =  numericEnumFromThen
  624.     enumFromTo          = defaultEnumFromTo
  625.     enumFromThenTo      = defaultEnumFromThenTo
  626.     {-# enumFrom :: Inline #-}
  627.     {-# enumFromThen :: Inline #-}
  628.     {-# enumFromTo :: Inline #-}
  629.     {-# enumFromThenTo :: Inline #-}
  630.  
  631. instance  Enum Double  where
  632.     enumFrom        =  numericEnumFrom
  633.     enumFromThen    =  numericEnumFromThen
  634.     enumFromTo          = defaultEnumFromTo
  635.     enumFromThenTo      = defaultEnumFromThenTo
  636.     {-# enumFrom :: Inline #-}
  637.     {-# enumFromThen :: Inline #-}
  638.     {-# enumFromTo :: Inline #-}
  639.     {-# enumFromThenTo :: Inline #-}
  640.  
  641. instance  Text Float  where
  642.     readsPrec p        = readSigned readFloat
  643.     showsPrec       = showSigned showFloat
  644.  
  645. instance  Text Double  where
  646.     readsPrec p        = readSigned readFloat
  647.     showsPrec       = showSigned showFloat
  648.  
  649.  
  650. -- Lists
  651.  
  652. -- data  [a]  =  [] | a : [a]  deriving (Eq, Ord, Binary)
  653.  
  654. instance  (Text a) => Text [a]  where
  655.     readsPrec p        = readList
  656.     showsPrec p        = showList
  657.  
  658. -- Functions
  659.  
  660. instance  Text (a -> b)  where
  661.     readsPrec p s  =  error "readsPrec{PreludeCore}: Cannot read functions."
  662.     showsPrec p f  =  showString "<<function>>"
  663.  
  664. -- Support for class Bin
  665.  
  666. instance Binary Int where
  667.   showBin i b = primShowBinInt i b
  668.   readBin b = primReadBinInt b
  669.  
  670. instance Binary Integer where
  671.   showBin i b = primShowBinInteger i b
  672.   readBin b = primReadBinInteger b
  673.  
  674. instance Binary Float where
  675.   showBin f b = primShowBinFloat f b
  676.   readBin b = primReadBinFloat b
  677.  
  678. instance Binary Double where
  679.   showBin d b = primShowBinDouble d b
  680.   readBin b = primReadBinDouble b
  681.  
  682. instance Binary Char where
  683.   showBin c b = primShowBinInt (ord c) b
  684.   readBin b = (chr i,b') where
  685.      (i,b') = primReadBinSmallInt b primMaxChar 
  686.  
  687. instance (Binary a) => Binary [a]  where
  688.     showBin l b = showBin (length l :: Int) (sb1 l b) where
  689.       sb1 [] b = b
  690.       sb1 (h:t) b = showBin h (sb1 t b)
  691.     readBin bin = rbl len bin' where
  692.        len :: Int
  693.        (len,bin') = readBin bin
  694.        rbl 0 b = ([],b)
  695.        rbl n b = (h:t,b'') where
  696.          (h,b') = readBin b
  697.          (t,b'') = rbl (n-1) b'
  698.  
  699. instance  (Ix a, Binary a, Binary b) => Binary (Array a b)  where
  700.     showBin a = showBin (bounds a) . showBin (elems a)
  701.     readBin bin = (listArray b vs, bin'')
  702.          where (b,bin')   = readBin bin
  703.                (vs,bin'') = readBin bin'
  704.  
  705.