home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsf / hugs / !Hugs / prelude < prev    next >
Text File  |  1995-02-14  |  64KB  |  1,692 lines

  1. -----------------------------------------------------------------------------
  2. --        ___    ___   ___    ___   __________   __________           --
  3. --       /  /   /  /  /  /   /  /  /  _______/  /  _______/   Version 1.0  --
  4. --      /  /___/  /  /  /   /  /  /  / _____   /  /______           --
  5. --     /  ____   /  /  /   /  /  /  / /_   /  /______   /     Copyright    --
  6. --    /  /   /  /  /  /___/  /  /  /___/  /  _______/  /      Mark P Jones --
  7. --   /__/   /__/  /_________/  /_________/  /_________/       1994, 1995   --
  8. --                                       --
  9. --   The Haskell User's Gofer System.   Derived from Gofer 2.30b.       --
  10. --                                       --
  11. --   This is the Hugs Standard Prelude, based very closely on the Standard --
  12. --   Prelude for Haskell 1.2.                           --
  13. --                                       --
  14. --   Hugs is subject to conditions of use and distribution; see the file   --
  15. --   "NOTICE" included with the main distribution for further details.     --
  16. --                                                                         --
  17. --   WARNING: This file is an integral part of the Hugs source code.       --
  18. --   Changes to the definitions in this file without corresponding         --
  19. --   modifications in other parts of the program may cause the interpreter --
  20. --   to fail unexpectedly.  Under normal circumstances, you should not       --
  21. --   attempt to modify this file in any way!  If you want to use a system  --
  22. --   where the prelude file can be changed, try Gofer instead.           --
  23. --                                       --
  24. -----------------------------------------------------------------------------
  25.  
  26. -- Standard value bindings {Prelude} ----------------------------------------
  27.  
  28. infixr 9  .
  29. infixl 9  !!, !, //
  30. infixr 8  ^, ^^, **
  31.           -- Fixities for the following operators are taken from the
  32.           -- prelude listing in Appendix A of the Haskell report.
  33.           -- Note that there are some discrepancies w.r.t. Section 5.7.
  34. infixl 7  *, /, `quot`, `rem`, `div`, `mod`, :%, %
  35. infix  6  :+
  36. infixl 6  +, -
  37. infix  5  \\
  38. infixr 5  :, ++
  39. infix  4  ==, /=, <, <=, >=, >, `elem`, `notElem`
  40. infixr 3  &&
  41. infixr 2  ||
  42. infix  1  :=
  43. infixr 0  $
  44.  
  45. -- Binary functions ---------------------------------------------------------
  46.  
  47. nullBin   :: Bin
  48. nullBin    = noBinTypeInHugs
  49.  
  50. isNullBin :: Bin -> Bool
  51. isNullBin  = noBinTypeInHugs
  52.  
  53. appendBin :: Bin -> Bin -> Bin
  54. appendBin  = noBinTypeInHugs
  55.  
  56. noBinTypeInHugs = error "There is no Bin type in Hugs"
  57.  
  58. -- Boolean functions --------------------------------------------------------
  59.  
  60. (&&), (||)     :: Bool -> Bool -> Bool
  61. False && x      = False
  62. True  && x      = x
  63. False || x      = x
  64. True  || x      = True
  65.  
  66. not            :: Bool -> Bool
  67. not True        = False
  68. not False       = True
  69.  
  70. otherwise      :: Bool
  71. otherwise       = True
  72.  
  73. -- Character functions ------------------------------------------------------
  74.  
  75. minChar, maxChar      :: Char
  76. minChar                = '\0'
  77. maxChar                = '\255'
  78.  
  79. primitive ord "primCharToInt" :: Char -> Int
  80. primitive chr "primIntToChar" :: Int -> Char
  81.  
  82. isAscii, isControl, isPrint, isSpace            :: Char -> Bool
  83. isUpper, isLower, isAlpha, isDigit, isAlphanum  :: Char -> Bool
  84.  
  85. isAscii c              =  ord c < 128
  86. isControl c            =  c < ' ' ||  c == '\DEL'
  87. isPrint c              =  c >= ' ' &&  c <= '~'
  88. isSpace c              =  c == ' ' || c == '\t' || c == '\n' ||
  89.                   c == '\r' || c == '\f' || c == '\v'
  90. isUpper c              =  c >= 'A'   &&  c <= 'Z'
  91. isLower c              =  c >= 'a'   &&  c <= 'z'
  92. isAlpha c              =  isUpper c  ||  isLower c
  93. isDigit c              =  c >= '0'   &&  c <= '9'
  94. isAlphanum c           =  isAlpha c  ||  isDigit c
  95.  
  96. toUpper, toLower      :: Char -> Char
  97. toUpper c | isLower c  = chr (ord c - ord 'a' + ord 'A')
  98.           | otherwise  = c
  99.  
  100. toLower c | isUpper c  = chr (ord c - ord 'A' + ord 'a')
  101.           | otherwise  = c
  102.  
  103. -- Numeric functions --------------------------------------------------------
  104.  
  105. primitive minInt "primMinInt", maxInt "primMaxInt" :: Int
  106.  
  107. subtract       :: Num a => a -> a -> a
  108. subtract        = flip (-)
  109.  
  110. gcd            :: Integral a => a -> a -> a
  111. gcd 0 0         = error "gcd{Prelude}: gcd 0 0 is undefined"
  112. gcd x y         = gcd' (abs x) (abs y)
  113.                   where gcd' x 0 = x
  114.                         gcd' x y = gcd' y (x `rem` y)
  115.  
  116. lcm            :: (Integral a) => a -> a -> a
  117. lcm _ 0         = 0
  118. lcm 0 _         = 0
  119. lcm x y         = abs ((x `quot` gcd x y) * y)
  120.  
  121. (^)            :: (Num a, Integral b) => a -> b -> a
  122. x ^ 0           = 1
  123. x ^ (n+1)       = f x n x
  124.                   where f _ 0 y = y
  125.                         f x n y = g x n where
  126.                                   g x n | even n    = g (x*x) (n`quot`2)
  127.                                         | otherwise = f x (n-1) (x*y)
  128. _ ^ _           = error "(^){Prelude}: negative exponent"
  129.  
  130. (^^)           :: (Fractional a, Integral b) => a -> b -> a
  131. x ^^ n          = if n >= 0 then x ^ n else recip (x^(-n))
  132.  
  133. fromIntegral   :: (Integral a, Num b) => a -> b
  134. fromIntegral    = fromInteger . toInteger
  135.  
  136. fromRealFrac   :: (RealFrac a, Fractional b) => a -> b
  137. fromRealFrac    = fromRational . toRational
  138.  
  139. atan2          :: (RealFloat a) => a -> a -> a
  140. atan2 y x       = case (signum y, signum x) of
  141.                     ( 0, 1) ->  0
  142.                     ( 1, 0) ->  pi/2
  143.                     ( 0,-1) ->  pi
  144.                     (-1, 0) -> -pi/2
  145.             ( _, 1) -> atan (y/x)
  146.             ( _,-1) -> atan (y/x) + pi
  147.             ( 0, 0) -> error "atan2{Prelude}: atan2 of origin"
  148.  
  149. -- Some standard functions --------------------------------------------------
  150. -- component projections for pairs:
  151. fst            :: (a,b) -> a
  152. fst (x,_)       = x
  153.  
  154. snd            :: (a,b) -> b
  155. snd (_,y)       = y
  156.  
  157. -- identity function
  158. id             :: a -> a
  159. id    x         = x
  160.  
  161. -- constant function
  162. const          :: a -> b -> a
  163. const k _       = k
  164.  
  165. -- function composition
  166. (.)           :: (b -> c) -> (a -> b) -> (a -> c)
  167. (f . g) x       = f (g x)
  168.  
  169. -- flip f takes its (first) two arguuments in the reverse order of f.
  170. flip           :: (a -> b -> c) -> b -> a -> c
  171. flip  f x y     = f y x
  172.  
  173. -- right associative infix application operator (useful in continuation-
  174. -- passing style)
  175. ($)            :: (a -> b) -> a -> b     -- pronounced as `apply' elsewhere
  176. f $ x           = f x
  177.  
  178. -- until p f  yields the result of applying f until p holds
  179. until                  :: (a -> Bool) -> (a -> a) -> a -> a
  180. until p f x | p x       = x
  181.             | otherwise = until p f (f x)
  182.  
  183. -- asTypeOf is a type restricted version of const.  It is usually used
  184. -- as an infix operator, and its typing forces its first argument
  185. -- (which is usually overloaded) to have the same type as the second.
  186. asTypeOf               :: a -> a -> a
  187. asTypeOf                = const
  188.  
  189. -- error is applied to a string, returns any type, and is everywhere
  190. -- undefined.  Operationally, the intent is that its application
  191. -- terminates execution of the program and displays the argument
  192. -- string in some appropriate way.
  193. primitive error "primError" :: String -> a
  194.  
  195. -- strict is not defined in the Haskell prelude, but Hugs doesn't have a
  196. -- strictness analyzer and it's occasionally useful to be able to exercise
  197. -- some added degree over the order of evaluation.
  198. primitive strict "primStrict" :: (a -> b) -> a -> b
  199.  
  200. -- Standard types, classes and instances {PreludeCore} ----------------------
  201.  
  202. -- Equality and Ordered classes ---------------------------------------------
  203.  
  204. class Eq a where
  205.     (==), (/=) :: a -> a -> Bool
  206.     x /= y      = not (x==y)
  207.  
  208. -- ordcmp is a new variation on an old idea;  ordcmp x y r  returns
  209. -- True if x>y, False if x<y and r otherwise.  The conventional ordering
  210. -- operators are defined in terms of ordcmp, but a default definition of
  211. -- ordcmp is also provided just in case.  It is an error (but not detected
  212. -- by the compiler) for the programmer to omit definitions both for <=
  213. -- and for ordcmp.  It will also be assumed that the ordering is consistent
  214. -- with the equality.
  215. --        e.g. ordcmp (x:xs) (y:ys) = ordcmp x y . ordcmp xs ys
  216. --
  217. -- Unlike Haskell 1.2, we now assume that orderings are total.
  218.  
  219. class (Eq a) => Ord a where
  220.     ordcmp               :: a -> a -> Bool -> Bool
  221.     (<), (<=), (>=), (>) :: a -> a -> Bool
  222.