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

  1. -- Standard value bindings
  2.  
  3. module Prelude (
  4.     PreludeCore.., PreludeRatio.., PreludeComplex.., PreludeList..,
  5.     PreludeArray.., PreludeText.., PreludeMonadicIO.., 
  6.     Maybe(Nothing,Just), Either(Left, Right),
  7.     nullBin, isNullBin, appendBin,
  8.     (&&), (||), not, otherwise,
  9.     minChar, maxChar, ord, chr, 
  10.     isAscii, isControl, isPrint, isSpace, 
  11.     isUpper, isLower, isAlpha, isDigit, isAlphanum,
  12.     toUpper, toLower,
  13.     minInt, maxInt, subtract, gcd, lcm, (^), (^^), 
  14.     fromIntegral, fromRealFrac,
  15.     either, thenMaybe, curry, uncurry,
  16.     fst, snd, id, const, (.), flip, ($), until, asTypeOf, error ) where
  17.  
  18. {-#Prelude#-}  -- Indicates definitions of compiler prelude symbols
  19.  
  20. import PreludePrims
  21.  
  22. import PreludeCore
  23. import PreludeList
  24. import PreludeArray
  25. import PreludeRatio
  26. import PreludeComplex
  27. import PreludeText
  28. import PreludeMonadicIO
  29.  
  30. infixr 9  .
  31. infixr 8  ^, ^^
  32. infixr 3  &&
  33. infixr 2  ||
  34. infixr 0  $
  35.  
  36.  
  37. -- Binary functions
  38.  
  39. nullBin                    :: Bin
  40. nullBin                    =  primNullBin
  41.  
  42. isNullBin                :: Bin -> Bool
  43. isNullBin                =  primIsNullBin
  44.  
  45. appendBin        :: Bin -> Bin -> Bin
  46. appendBin        =  primAppendBin
  47.  
  48. -- Boolean functions
  49.  
  50. (&&), (||)        :: Bool -> Bool -> Bool
  51. True  && x        =  x
  52. False && _        =  False
  53. True  || _        =  True
  54. False || x        =  x
  55.  
  56. not            :: Bool -> Bool
  57. not True        =  False
  58. not False        =  True
  59.  
  60. {-# (&&)  :: AlwaysInline #-}
  61. {-# (||)  :: AlwaysInline #-}
  62. {-# not  :: AlwaysInline #-}
  63.  
  64.  
  65. otherwise        :: Bool
  66. otherwise         =  True
  67.  
  68. -- Character functions
  69.  
  70. minChar, maxChar    :: Char
  71. minChar            = '\0'
  72. maxChar            = '\255'
  73.  
  74. ord            :: Char -> Int
  75. ord             =  primCharToInt
  76.  
  77. chr             :: Int -> Char
  78. chr             =  primIntToChar
  79.  
  80. isAscii, isControl, isPrint, isSpace        :: Char -> Bool
  81. isUpper, isLower, isAlpha, isDigit, isAlphanum    :: Char -> Bool
  82.  
  83. isAscii c         =  ord c < 128
  84. isControl c        =  c < ' ' || c == '\DEL'
  85. isPrint c        =  c >= ' ' && c <= '~'
  86. isSpace c        =  c == ' ' || c == '\t' || c == '\n' || 
  87.                c == '\r' || c == '\f' || c == '\v'
  88. isUpper c        =  c >= 'A' && c <= 'Z'
  89. isLower c        =  c >= 'a' && c <= 'z'
  90. isAlpha c        =  isUpper c || isLower c
  91. isDigit c        =  c >= '0' && c <= '9'
  92. isAlphanum c        =  isAlpha c || isDigit c
  93.  
  94.  
  95. toUpper, toLower    :: Char -> Char
  96. toUpper c | isLower c    = chr ((ord c - ord 'a') + ord 'A')
  97.       | otherwise    = c
  98.  
  99. toLower c | isUpper c    = chr ((ord c - ord 'A') + ord 'a')
  100.       | otherwise    = c
  101.  
  102. -- Numeric functions
  103.  
  104. minInt, maxInt    :: Int
  105. minInt        =  primMinInt
  106. maxInt        =  primMaxInt
  107.  
  108. subtract    :: (Num a) => a -> a -> a
  109. subtract    =  flip (-)
  110.  
  111. gcd        :: (Integral a) => a -> a -> a
  112. gcd 0 0        =  error "gcd{Prelude}: gcd 0 0 is undefined"
  113. gcd x y        =  gcd' (abs x) (abs y)
  114.            where gcd' x 0  =  x
  115.              gcd' x y  =  gcd' y (x `rem` y)
  116.  
  117. lcm        :: (Integral a) => a -> a -> a
  118. lcm _ 0        =  0
  119. lcm 0 _        =  0
  120. lcm x y        =  abs ((x `quot` (gcd x y)) * y)
  121.  
  122. (^)        :: (Num a, Integral b) => a -> b -> a
  123. x ^ 0        =  1
  124. x ^ (n+1)    =  f x n x
  125.            where f _ 0 y = y
  126.                  f x n y = g x n  where
  127.                        g x n | even n  = g (x*x) (n `quot` 2)
  128.                          | otherwise = f x (n-1) (x*y)
  129. _ ^ _        = error "(^){Prelude}: negative exponent"
  130.  
  131. {-# (^) :: Specialize(Int->Int->Int) #-}
  132. {-# (^) :: Specialize((Num a) => a->Int->a) #-}
  133.  
  134.  
  135. (^^)        :: (Fractional a, Integral b) => a -> b -> a
  136. x ^^ n        =  if n >= 0 then x^n else recip (x^(-n))
  137.  
  138. fromIntegral    :: (Integral a, Num b) => a -> b
  139. fromIntegral    =  fromInteger . toInteger
  140.  
  141. fromRealFrac    :: (RealFrac a, Fractional b) => a -> b
  142. fromRealFrac    =  fromRational . toRational
  143.  
  144. -- Some standard functions:
  145. -- component projections for pairs:
  146. fst            :: (a,b) -> a
  147. fst (x,y)        =  x
  148.  
  149. snd            :: (a,b) -> b
  150. snd (x,y)        =  y
  151.  
  152. -- identity function
  153. id            :: a -> a
  154. id x            =  x
  155. {-# id  :: AlwaysInline #-}
  156.  
  157.  
  158. -- constant function
  159. const            :: a -> b -> a
  160. const x _        =  x
  161. {-# const  :: AlwaysInline #-}
  162.  
  163. -- function composition
  164. (.)            :: (b -> c) -> (a -> b) -> a -> c
  165. f . g            =  \ x -> f (g x)
  166. {-# (.)  :: AlwaysInline #-}
  167.  
  168. -- flip f  takes its (first) two arguments in the reverse order of f.
  169. flip            :: (a -> b -> c) -> b -> a -> c
  170. flip f x y        =  f y x
  171. {-# flip  :: AlwaysInline #-}
  172.  
  173. -- right-associating infix application operator (useful in continuation-
  174. -- passing style)
  175. ($)            :: (a -> b) -> a -> b
  176. f $ x            =  f x
  177. {-# ($)  :: AlwaysInline #-}
  178.  
  179. -- until p f  yields the result of applying f until p holds.
  180. until            :: (a -> Bool) -> (a -> a) -> a -> a
  181. until p f x | p x    =  x
  182.         | otherwise =  until p f (f x)
  183.  
  184. -- asTypeOf is a type-restricted version of const.  It is usually used
  185. -- as an infix operator, and its typing forces its first argument
  186. -- (which is usually overloaded) to have the same type as the second.
  187. asTypeOf        :: a -> a -> a
  188. asTypeOf        =  const
  189.  
  190. -- 1.3 Extensions
  191.  
  192. data Either a b = Left a | Right b deriving (Text, Eq, Ord)
  193.  
  194. data Maybe a = Nothing | Just a deriving (Text, Eq, Ord)
  195.  
  196. either :: (a -> c) -> (b -> c) -> Either a b -> c
  197. either f g (Left x) = f x
  198. either f g (Right y) = g y
  199.  
  200. thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
  201. thenMaybe Nothing _ = Nothing
  202. thenMaybe (Just x) f = f x
  203.  
  204. curry   :: ((a,b) -> c) -> a -> b -> c
  205. curry f x y = f (x,y)
  206.  
  207. uncurry :: (a -> b -> c) -> (a,b) -> c
  208. uncurry f (x,y) = f x y
  209.