home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
utilities
/
utilsf
/
hugs
/
!Hugs
/
prelude
< prev
next >
Wrap
Text File
|
1995-02-14
|
64KB
|
1,692 lines
-----------------------------------------------------------------------------
-- ___ ___ ___ ___ __________ __________ --
-- / / / / / / / / / _______/ / _______/ Version 1.0 --
-- / /___/ / / / / / / / _____ / /______ --
-- / ____ / / / / / / / /_ / /______ / Copyright --
-- / / / / / /___/ / / /___/ / _______/ / Mark P Jones --
-- /__/ /__/ /_________/ /_________/ /_________/ 1994, 1995 --
-- --
-- The Haskell User's Gofer System. Derived from Gofer 2.30b. --
-- --
-- This is the Hugs Standard Prelude, based very closely on the Standard --
-- Prelude for Haskell 1.2. --
-- --
-- Hugs is subject to conditions of use and distribution; see the file --
-- "NOTICE" included with the main distribution for further details. --
-- --
-- WARNING: This file is an integral part of the Hugs source code. --
-- Changes to the definitions in this file without corresponding --
-- modifications in other parts of the program may cause the interpreter --
-- to fail unexpectedly. Under normal circumstances, you should not --
-- attempt to modify this file in any way! If you want to use a system --
-- where the prelude file can be changed, try Gofer instead. --
-- --
-----------------------------------------------------------------------------
-- Standard value bindings {Prelude} ----------------------------------------
infixr 9 .
infixl 9 !!, !, //
infixr 8 ^, ^^, **
-- Fixities for the following operators are taken from the
-- prelude listing in Appendix A of the Haskell report.
-- Note that there are some discrepancies w.r.t. Section 5.7.
infixl 7 *, /, `quot`, `rem`, `div`, `mod`, :%, %
infix 6 :+
infixl 6 +, -
infix 5 \\
infixr 5 :, ++
infix 4 ==, /=, <, <=, >=, >, `elem`, `notElem`
infixr 3 &&
infixr 2 ||
infix 1 :=
infixr 0 $
-- Binary functions ---------------------------------------------------------
nullBin :: Bin
nullBin = noBinTypeInHugs
isNullBin :: Bin -> Bool
isNullBin = noBinTypeInHugs
appendBin :: Bin -> Bin -> Bin
appendBin = noBinTypeInHugs
noBinTypeInHugs = error "There is no Bin type in Hugs"
-- Boolean functions --------------------------------------------------------
(&&), (||) :: Bool -> Bool -> Bool
False && x = False
True && x = x
False || x = x
True || x = True
not :: Bool -> Bool
not True = False
not False = True
otherwise :: Bool
otherwise = True
-- Character functions ------------------------------------------------------
minChar, maxChar :: Char
minChar = '\0'
maxChar = '\255'
primitive ord "primCharToInt" :: Char -> Int
primitive chr "primIntToChar" :: Int -> Char
isAscii, isControl, isPrint, isSpace :: Char -> Bool
isUpper, isLower, isAlpha, isDigit, isAlphanum :: Char -> Bool
isAscii c = ord c < 128
isControl c = c < ' ' || c == '\DEL'
isPrint c = c >= ' ' && c <= '~'
isSpace c = c == ' ' || c == '\t' || c == '\n' ||
c == '\r' || c == '\f' || c == '\v'
isUpper c = c >= 'A' && c <= 'Z'
isLower c = c >= 'a' && c <= 'z'
isAlpha c = isUpper c || isLower c
isDigit c = c >= '0' && c <= '9'
isAlphanum c = isAlpha c || isDigit c
toUpper, toLower :: Char -> Char
toUpper c | isLower c = chr (ord c - ord 'a' + ord 'A')
| otherwise = c
toLower c | isUpper c = chr (ord c - ord 'A' + ord 'a')
| otherwise = c
-- Numeric functions --------------------------------------------------------
primitive minInt "primMinInt", maxInt "primMaxInt" :: Int
subtract :: Num a => a -> a -> a
subtract = flip (-)
gcd :: Integral a => a -> a -> a
gcd 0 0 = error "gcd{Prelude}: gcd 0 0 is undefined"
gcd x y = gcd' (abs x) (abs y)
where gcd' x 0 = x
gcd' x y = gcd' y (x `rem` y)
lcm :: (Integral a) => a -> a -> a
lcm _ 0 = 0
lcm 0 _ = 0
lcm x y = abs ((x `quot` gcd x y) * y)
(^) :: (Num a, Integral b) => a -> b -> a
x ^ 0 = 1
x ^ (n+1) = f x n x
where f _ 0 y = y
f x n y = g x n where
g x n | even n = g (x*x) (n`quot`2)
| otherwise = f x (n-1) (x*y)
_ ^ _ = error "(^){Prelude}: negative exponent"
(^^) :: (Fractional a, Integral b) => a -> b -> a
x ^^ n = if n >= 0 then x ^ n else recip (x^(-n))
fromIntegral :: (Integral a, Num b) => a -> b
fromIntegral = fromInteger . toInteger
fromRealFrac :: (RealFrac a, Fractional b) => a -> b
fromRealFrac = fromRational . toRational
atan2 :: (RealFloat a) => a -> a -> a
atan2 y x = case (signum y, signum x) of
( 0, 1) -> 0
( 1, 0) -> pi/2
( 0,-1) -> pi
(-1, 0) -> -pi/2
( _, 1) -> atan (y/x)
( _,-1) -> atan (y/x) + pi
( 0, 0) -> error "atan2{Prelude}: atan2 of origin"
-- Some standard functions --------------------------------------------------
-- component projections for pairs:
fst :: (a,b) -> a
fst (x,_) = x
snd :: (a,b) -> b
snd (_,y) = y
-- identity function
id :: a -> a
id x = x
-- constant function
const :: a -> b -> a
const k _ = k
-- function composition
(.) :: (b -> c) -> (a -> b) -> (a -> c)
(f . g) x = f (g x)
-- flip f takes its (first) two arguuments in the reverse order of f.
flip :: (a -> b -> c) -> b -> a -> c
flip f x y = f y x
-- right associative infix application operator (useful in continuation-
-- passing style)
($) :: (a -> b) -> a -> b -- pronounced as `apply' elsewhere
f $ x = f x
-- until p f yields the result of applying f until p holds
until :: (a -> Bool) -> (a -> a) -> a -> a
until p f x | p x = x
| otherwise = until p f (f x)
-- asTypeOf is a type restricted version of const. It is usually used
-- as an infix operator, and its typing forces its first argument
-- (which is usually overloaded) to have the same type as the second.
asTypeOf :: a -> a -> a
asTypeOf = const
-- error is applied to a string, returns any type, and is everywhere
-- undefined. Operationally, the intent is that its application
-- terminates execution of the program and displays the argument
-- string in some appropriate way.
primitive error "primError" :: String -> a
-- strict is not defined in the Haskell prelude, but Hugs doesn't have a
-- strictness analyzer and it's occasionally useful to be able to exercise
-- some added degree over the order of evaluation.
primitive strict "primStrict" :: (a -> b) -> a -> b
-- Standard types, classes and instances {PreludeCore} ----------------------
-- Equality and Ordered classes ---------------------------------------------
class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x==y)
-- ordcmp is a new variation on an old idea; ordcmp x y r returns
-- True if x>y, False if x<y and r otherwise. The conventional ordering
-- operators are defined in terms of ordcmp, but a default definition of
-- ordcmp is also provided just in case. It is an error (but not detected
-- by the compiler) for the programmer to omit definitions both for <=
-- and for ordcmp. It will also be assumed that the ordering is consistent
-- with the equality.
-- e.g. ordcmp (x:xs) (y:ys) = ordcmp x y . ordcmp xs ys
--
-- Unlike Haskell 1.2, we now assume that orderings are total.
class (Eq a) => Ord a where
ordcmp :: a -> a -> Bool -> Bool
(<), (<=), (>=), (>) :: a -> a -> Bool