home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
gofer
/
!Gofer
/
preludes
/
gcwmin
< prev
next >
Wrap
Text File
|
1994-05-20
|
19KB
|
628 lines
-- __________ __________ __________ __________ ________
-- / _______/ / ____ / / _______/ / _______/ / ____ \
-- / / _____ / / / / / /______ / /______ / /___/ /
-- / / /_ / / / / / / _______/ / _______/ / __ __/
-- / /___/ / / /___/ / / / / /______ / / \ \
-- /_________/ /_________/ /__/ /_________/ /__/ \__\
--
-- Functional programming environment, Version 2.28
-- Copyright Mark P Jones 1991-1993.
--
-- Minimal Gofer prelude for experimentation with different approaches
-- to standard operations.
--
-- Any Gofer prelude file should typically include at least the following
-- definitions:
infixr 5 :
infixr 3 &&
infixr 2 ||
(&&), (||) :: Bool -> Bool -> Bool
False && _ = False -- (&&) and (||) names predefined in Gofer
True && x = x
False || x = x
True || _ = True
flip :: (a -> b -> c) -> b -> a -> c
flip f x y = f y x
-- Primitives -----------------------------------------------------------
primitive error "primError" :: String -> a
-- End of minimal prelude ----------------------------------------------
primitive strict "primStrict" :: (a -> b) -> a -> b
-- Format primitives ----------------------------------------------------
primitive primPrint "primPrint" :: Int -> a -> String -> String
primitive primShowsInt "primShowsInt" :: Int -> Int -> String -> String
primitive primShowsFloat "primShowsFloat" ::
Int -> Float -> String -> String
-- Character primitives -------------------------------------------------
primitive primEqChar "primEqChar",
primLeChar "primLeChar" :: Char -> Char -> Bool
primitive ord "primCharToInt" :: Char -> Int
primitive chr "primIntToChar" :: Int -> Char
-- Integer primitives --------------------------------------------------
primitive primEqInt "primEqInt",
primLeInt "primLeInt" :: Int -> Int -> Bool
primitive primPlusInt "primPlusInt",
primMinusInt "primMinusInt",
primDivInt "primDivInt",
primMulInt "primMulInt" :: Int -> Int -> Int
primitive primNegInt "primNegInt" :: Int -> Int
primitive quot "primQuotInt",
rem "primRemInt",
mod "primModInt" :: Int -> Int -> Int
-- Float primitives ---------------------------------------------------
primitive primEqFloat "primEqFloat",
primLeFloat "primLeFloat" :: Float -> Float -> Bool
primitive primPlusFloat "primPlusFloat",
primMinusFloat "primMinusFloat",
primDivFloat "primDivFloat",
primMulFloat "primMulFloat" :: Float -> Float -> Float
primitive primNegFloat "primNegFloat" :: Float -> Float
primitive primIntToFloat "primIntToFloat" :: Int -> Float
primitive truncate "primFloatToInt" :: Float -> Int
-- Trigonometric primitives ------------------------------------
primitive sin "primSinFloat", asin "primAsinFloat",
cos "primCosFloat", acos "primAcosFloat",
tan "primTanFloat", atan "primAtanFloat",
primLogFloat "primLogFloat", log10 "primLog10Float",
primExpFloat "primExpFloat", sqrt "primSqrtFloat"
:: Float -> Float
primitive atan2 "primAtan2Float" :: Float -> Float -> Float
-- IO ------------------------------------------------------------
stdin = "stdin"
stdout = "stdout"
stderr = "stderr"
stdecho = "stdecho"
{- The Dialogue, Request, Response and IOError datatypes are now built-in:
data Request = -- file system requests:
ReadFile String
| WriteFile String String
| AppendFile String String
-- channel system requests:
| ReadChan String
| AppendChan String String
-- environment requests:
| Echo Bool
| GetArgs
| GetProgName
| GetEnv String
data Response = Success
| Str String
| Failure IOError
| StrList [String]
data IOError = WriteError String
| ReadError String
| SearchError String
| FormatError String
| OtherError String
type Dialogue = [Response] -> [Request]
-}
run :: (String -> String) -> Dialogue
run f ~(Success : ~(Str kbd : _))
= [Echo False, ReadChan "stdin", AppendChan "stdout" (f kbd)]
primitive primFopen "primFopen" :: String -> a -> (String -> a) -> a
openfile :: String -> String
openfile f = primFopen f (error ("can't open file "++f)) id
--- Fixities ------------------------------------------------------------
infixl 9 !!
infixr 9 .
infixr 8 ^
infixl 7 *, :/, /
infix 7 `quot`, `rem`, `mod`
infixl 6 +, -, :+!
infixr 5 ++
infix 4 ==, /=, <, <=, >=, >
infixl 2 `bind`, `hcf`
-- Standard synonyms --------------------
type Rel a = a -> a -> Bool
type BinOp a = a -> a -> a
-- Standard type classes: -----------------------------------------------
class Eq a where
(==), (/=) :: Rel a
x /= y = not (x == y)
-- (x == x) === True
-- (x == y) === (y == x)
-- (x == y) && (y == z) ==> (x == z)
class Eq a => Ord a where
(<), (<=), (>), (>=) :: Rel a
max, min :: BinOp a
x < y = x <= y && x /= y
x >= y = y <= x
x > y = y < x
max x y | x >= y = x
| y >= x = y
min x y | x <= y = x
| y <= x = y
-- x <= x === True
-- (x <= y) && (y <= z) ==> (x <= z)
class Ord a => Ix a where
range :: (a,a) -> [a]
index :: (a,a) -> a -> Int
inRange :: (a,a) -> a -> Bool
class Ord a => Enum a where
enumFrom :: a -> [a] -- [n..]
enumFromThen :: a -> a -> [a] -- [n,m..]
enumFromTo :: a -> a -> [a] -- [n..m]
enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
enumFromTo n m = takeWhile (m>=) (enumFrom n)
enumFromThenTo n n' m = takeWhile ((if n'>=n then (>=) else (<=)) m)
(enumFromThen n n')
class LeftMul a b where
(*) :: a -> b -> b
class Add a where
(+),(-) :: BinOp a
negate :: a -> a
zero :: a
negate x = zero - x
-- x + (y + z) === (x + y) + z
-- x + y === y + x
-- zero + x === x
-- x + zero === x
-- x - x === zero
class LeftMul a a => Mult a where
unit :: a
(^) :: a -> Int -> a
x ^ 0 = unit
x ^ 1 = x
x ^ (2*n) = (x*x)^n
x ^ (2*n+1) = x*(x*x)^n
-- x*(y*z) === (x*y)*z
-- unit*x === x
class Div a b where
(/) :: a -> b -> a
class (Div a a, Add a, Mult a, Div a Int, LeftMul Int a) => Exp a where
exp, log, cosh, sinh, tanh :: a -> a
cosh x = (exp(x) + exp(-x))/2
sinh x = (exp(x) - exp(-x))/2
tanh x = (a-unit)/(a+unit) where a = exp(2*x)
class Functor f where
map :: (a -> b) -> (f a -> f b)
-- map (u.v) === (map u).(map v)
-- map id === id
class Functor m => Monad m where
result :: a -> m a
join :: m (m a) -> m a
bind :: m a -> (a -> m b) -> m b
join x = bind x (\y->y)
x `bind` f = join (map f x)
-- (map u).result === result.(map u)
-- (map u).join === join.(map (map u))
-- join.(map result) === id
-- join.result === id
-- join.join === join.(map join)
class Monad m => Monad0 m where
nil :: m a
-- map _ nil === nil
-- join nil === nil
class Monad0 c => MonadPlus c where
(++) :: c a -> c a -> c a
-- nil ++ x === x
-- x ++ (y ++ z) === (x ++ y) ++ z
-- A trimmed down version of the Haskell Text class: ---------------------
type ShowS = String -> String
class Text a where
showsPrec :: Int -> a -> ShowS
showList :: [a] -> ShowS
showsPrec = primPrint
showList [] = showString "[]"
showList (x:xs) = showChar '[' . shows x . showl xs
where showl [] = showChar ']'
showl (x:xs) = showChar ',' . shows x . showl xs
shows :: Text a => a