home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Lib / simple.prelude < prev    next >
Text File  |  1994-06-23  |  19KB  |  610 lines

  1. --         __________   __________   __________   __________   ________
  2. --        /  _______/  /  ____   /  /  _______/  /  _______/  /  ____  \
  3. --       /  / _____   /  /   /  /  /  /______   /  /______   /  /___/  /
  4. --      /  / /_   /  /  /   /  /  /  _______/  /  _______/  /  __   __/
  5. --     /  /___/  /  /  /___/  /  /  /         /  /______   /  /  \  \ 
  6. --    /_________/  /_________/  /__/         /_________/  /__/    \__\
  7. --
  8. --    Functional programming environment, Version 2.30
  9. --    Copyright Mark P Jones 1991-1994.
  10. --
  11. --    Simplified prelude, without any type classes and overloaded values
  12. --    Based on the Haskell standard prelude version 1.2.
  13. --
  14. --    This prelude file shows one approach to using Gofer without the
  15. --    use of overloaded implementations of show, <=, == etc.
  16. --
  17. --    Needless to say, some (most) of the Gofer demonstration programs
  18. --    cannot be used inconnection with this prelude ... but a wide
  19. --    family of programs can be used without needing to worry about
  20. --    type classes at all.
  21. --
  22.  
  23. help = "press :? for a list of commands"
  24. quit = help ++ ", :q to quit"
  25.  
  26. -- Operator precedence table: -----------------------------------------------
  27.  
  28. infixl 9 !!
  29. infixr 9 .
  30. infixr 8 ^
  31. infixl 7 *
  32. infix  7 /, `div`, `quot`, `rem`, `mod`
  33. infixl 6 +, -
  34. infix  5 \\
  35. infixr 5 ++, :
  36. infix  4 ==, /=, <, <=, >=, >
  37. infix  4 `elem`, `notElem`
  38. infixr 3 &&
  39. infixr 2 ||
  40. infixr 0 $
  41.  
  42. -- Standard combinators: ----------------------------------------------------
  43.  
  44. primitive strict "primStrict" :: (a -> b) -> a -> b
  45.  
  46. const          :: a -> b -> a
  47. const k x       = k
  48.  
  49. id             :: a -> a
  50. id    x         = x
  51.  
  52. curry          :: ((a,b) -> c) -> a -> b -> c
  53. curry f a b     =  f (a,b)
  54.  
  55. uncurry        :: (a -> b -> c) -> (a,b) -> c
  56. uncurry f (a,b) = f a b
  57.  
  58. fst            :: (a,b) -> a
  59. fst (x,_)       = x
  60.  
  61. snd            :: (a,b) -> b
  62. snd (_,y)       = y
  63.  
  64. fst3           :: (a,b,c) -> a
  65. fst3 (x,_,_)    = x
  66.  
  67. snd3           :: (a,b,c) -> b
  68. snd3 (_,x,_)    = x
  69.  
  70. thd3           :: (a,b,c) -> c
  71. thd3 (_,_,x)    = x
  72.  
  73. (.)           :: (b -> c) -> (a -> b) -> (a -> c)
  74. (f . g) x       = f (g x)
  75.  
  76. flip           :: (a -> b -> c) -> b -> a -> c
  77. flip  f x y     = f y x
  78.  
  79. ($)            :: (a -> b) -> a -> b     -- pronounced as `apply' elsewhere
  80. f $ x           = f x
  81.  
  82. -- Boolean functions: -------------------------------------------------------
  83.  
  84. (&&), (||)     :: Bool -> Bool -> Bool
  85. False && x      = False
  86. True  && x      = x
  87.  
  88. False || x      = x
  89. True  || x      = True
  90.  
  91. not            :: Bool -> Bool
  92. not True        = False
  93. not False       = True
  94.  
  95. and, or        :: [Bool] -> Bool
  96. and             = foldr (&&) True
  97. or              = foldr (||) False
  98.  
  99. any, all       :: (a -> Bool) -> [a] -> Bool
  100. any p           = or  . map p
  101. all p           = and . map p
  102.  
  103. otherwise      :: Bool
  104. otherwise       = True
  105.  
  106. -- Essentials and builtin primitives: --------------------------------------
  107.  
  108. primitive (==) "primGenericEq",
  109.           (/=) "primGenericNe",
  110.           (<=) "primGenericLe",
  111.           (<)  "primGenericLt",
  112.           (>=) "primGenericGe",
  113.           (>)  "primGenericGt"   :: a -> a -> Bool
  114.  
  115. max x y | x >= y    = x
  116.         | otherwise = y
  117. min x y | x <= y    = x
  118.         | otherwise = y
  119.  
  120. enumFrom n           = iterate (1+) n                   -- [n..]
  121. enumFromThen n m     = iterate ((m-n)+) n               -- [n,m..]
  122. enumFromTo n m       = takeWhile (m>=) (enumFrom n)           -- [n..m]
  123. enumFromThenTo n o m = takeWhile ((if o>=n then (>=) else (<=)) m) -- [n,o..m]
  124.                                  (enumFromThen n o)
  125.  
  126. primitive (+)    "primPlusInt",
  127.       (-)    "primMinusInt",
  128.           (/)    "primDivInt",
  129.       div    "primDivInt",
  130.       quot     "primQuotInt",
  131.           rem    "primRemInt",
  132.           mod    "primModInt",
  133.       (*)    "primMulInt"    :: Int -> Int -> Int
  134. primitive negate "primNegInt"    :: Int -> Int
  135.  
  136. primitive primPrint "primPrint"  :: Int -> a -> String -> String
  137.  
  138. show                ::  a -> String
  139. show x               =  primPrint 0 x []
  140.  
  141. -- Character functions: -----------------------------------------------------
  142.  
  143. primitive ord "primCharToInt" :: Char -> Int
  144. primitive chr "primIntToChar" :: Int -> Char
  145.  
  146. isAscii, isControl, isPrint, isSpace            :: Char -> Bool
  147. isUpper, isLower, isAlpha, isDigit, isAlphanum  :: Char -> Bool
  148.  
  149. isAscii c     =  ord c < 128
  150.  
  151. isControl c   =  c < ' '    ||  c == '\DEL'
  152.  
  153. isPrint c     =  c >= ' '   &&  c <= '~'
  154.  
  155. isSpace c     =  c == ' '   || c == '\t'  || c == '\n'  || c == '\r'  ||
  156.                                c == '\f'  || c == '\v'
  157.  
  158. isUpper c     =  c >= 'A'   &&  c <= 'Z'
  159. isLower c     =  c >= 'a'   &&  c <= 'z'
  160.  
  161. isAlpha c     =  isUpper c  ||  isLower c
  162. isDigit c     =  c >= '0'   &&  c <= '9'
  163. isAlphanum c  =  isAlpha c  ||  isDigit c
  164.  
  165.  
  166. toUpper, toLower      :: Char -> Char
  167.  
  168. toUpper c | isLower c  = chr (ord c - ord 'a' + ord 'A')
  169.           | otherwise  = c
  170.  
  171. toLower c | isUpper c  = chr (ord c - ord 'A' + ord 'a')
  172.           | otherwise  = c
  173.  
  174. minChar, maxChar      :: Char
  175. minChar                = chr 0
  176. maxChar                = chr 255
  177.  
  178. -- Standard numerical functions: --------------------------------------------
  179.  
  180. subtract  :: Int -> Int -> Int
  181. subtract   = flip (-)
  182.  
  183. even, odd :: Int -> Bool
  184. even x     = x `rem` 2 == 0
  185. odd        = not . even
  186.  
  187. gcd       :: Int -> Int -> Int
  188. gcd x y    = gcd' (abs x) (abs y)
  189.              where gcd' x 0 = x
  190.                    gcd' x y = gcd' y (x `rem` y)
  191.  
  192. lcm       :: Int -> Int -> Int
  193. lcm _ 0    = 0
  194. lcm 0 _    = 0
  195. lcm x y    = abs ((x `quot` gcd x y) * y)
  196.  
  197. (^)       :: Int -> Int -> Int
  198. x ^ 0      = 1
  199. x ^ (n+1)  = f x n x
  200.              where f _ 0 y = y
  201.                    f x n y = g x n where
  202.                              g x n | even n    = g (x*x) (n`quot`2)
  203.                                    | otherwise = f x (n-1) (x*y)
  204.  
  205. abs :: Int -> Int
  206. abs x    | x >= 0  = x
  207.          | x <  0  = - x
  208.  
  209. signum :: Int -> Int
  210. signum x | x == 0  = 0
  211.          | x > 0   = 1
  212.          | x < 0   = -1
  213.  
  214. sum, product    :: [Int] -> Int
  215. sum              = foldl' (+) 0
  216. product          = foldl' (*) 1
  217.  
  218. sums, products    :: [Int] -> [Int]
  219. sums             = scanl (+) 0
  220. products         = scanl (*) 1
  221.  
  222. -- Standard list processing functions: --------------------------------------
  223.  
  224. head             :: [a] -> a
  225. head (x:_)        = x
  226.  
  227. last             :: [a] -> a
  228. last [x]          = x
  229. last (_:xs)       = last xs
  230.  
  231. tail             :: [a] -> [a]
  232. tail (_:xs)       = xs
  233.  
  234. init             :: [a] -> [a]
  235. init [x]          = []
  236. init (x:xs)       = x : init xs
  237.  
  238. (++)             :: [a] -> [a] -> [a]    -- append lists.  Associative with
  239. []     ++ ys      = ys                   -- left and right identity [].
  240. (x:xs) ++ ys      = x:(xs++ys)
  241.  
  242. length         :: [a] -> Int           -- calculate length of list
  243. length            = foldl' (\n _ -> n+1) 0
  244.  
  245. (!!)             :: [a] -> Int -> a      -- xs!!n selects the nth element of
  246. (x:_)  !! 0       = x                    -- the list xs (first element xs!!0)
  247. (_:xs) !! (n+1)   = xs !! n              -- for any n < length xs.
  248.  
  249. iterate          :: (a -> a) -> a -> [a] -- generate the infinite list
  250. iterate f x       = x : iterate f (f x)  -- [x, f x, f (f x), ...
  251.  
  252. repeat           :: a -> [a]             -- generate the infinite list
  253. repeat x          = xs where xs = x:xs   -- [x, x, x, x, ...
  254.  
  255. cycle            :: [a] -> [a]           -- generate the infinite list
  256. cycle xs          = xs' where xs'=xs++xs'-- xs ++ xs ++ xs ++ ...
  257.  
  258. copy             :: Int -> a -> [a]      -- make list of n copies of x
  259. copy n x          = take n xs where xs = x:xs
  260.  
  261. nub              :: [a] -> [a]           -- remove duplicates from list
  262. nub []            = []
  263. nub (x:xs)        = x : nub (filter (x/=) xs)
  264.  
  265. reverse          :: [a] -> [a]           -- reverse elements of list
  266. reverse           = foldl (flip (:)) []
  267.  
  268. elem, notElem    :: a -> [a] -> Bool
  269. elem              = any . (==)           -- test for membership in list
  270. notElem           = all . (/=)           -- test for non-membership
  271.  
  272. maximum, minimum :: [a] -> a
  273. maximum           = foldl1 max          -- max element in non-empty list
  274. minimum           = foldl1 min          -- min element in non-empty list
  275.  
  276. concat           :: [[a]] -> [a]        -- concatenate list of lists
  277. concat            = foldr (++) []
  278.  
  279. transpose        :: [[a]] -> [[a]]      -- transpose list of lists
  280. transpose         = foldr
  281.                       (\xs xss -> zipWith (:) xs (xss ++ repeat []))
  282.                       []
  283.  
  284. -- null provides a simple and efficient way of determining whether a given
  285. -- list is empty, without using (==) and hence avoiding a constraint of the
  286. -- form Eq [a] in the full standard prelude.
  287.  
  288. null             :: [a] -> Bool
  289. null []           = True
  290. null (_:_)        = False
  291.  
  292. -- (\\) is used to remove the first occurrence of each element in the second
  293. -- list from the first list.  It is a kind of inverse of (++) in the sense
  294. -- that  (xs ++ ys) \\ xs = ys for any finite list xs of proper values xs.
  295.  
  296. (\\)             :: [a] -> [a] -> [a]
  297. (\\)              = foldl del
  298.                     where []     `del` _  = []
  299.                           (x:xs) `del` y
  300.                              | x == y     = xs
  301.                              | otherwise  = x : xs `del` y
  302.  
  303.  
  304. -- map f xs applies the function f to each element of the list xs returning
  305. -- the corresponding list of results.  filter p xs returns the sublist of xs
  306. -- containing those elements which satisfy the predicate p.
  307.  
  308. map              :: (a -> b) -> [a] -> [b]
  309. map f []          = []
  310. map f (x:xs)      = f x : map f xs
  311.  
  312. filter           :: (a -> Bool) -> [a] -> [a]
  313. filter _ []       = []
  314. filter p (x:xs)
  315.     | p x         = x : xs'
  316.     | otherwise   = xs'
  317.                   where xs' = filter p xs
  318.  
  319. -- Fold primitives:  The foldl and scanl functions, variants foldl1 and
  320. -- scanl1 for non-empty lists, and strict variants foldl' scanl' describe
  321. -- common patterns of recursion over lists.  Informally:
  322. --
  323. --  foldl f a [x1, x2, ..., xn]  = f (...(f (f a x1) x2)...) xn
  324. --                               = (...((a `f` x1) `f` x2)...) `f` xn
  325. -- etc...
  326. --
  327. -- The functions foldr, scanr and variants foldr1, scanr1 are duals of these
  328. -- functions:
  329. -- e.g.  foldr f a xs = foldl (flip f) a (reverse xs)  for finite lists xs.
  330.  
  331. foldl            :: (a -> b -> a) -> a -> [b] -> a
  332. foldl f z []      = z
  333. foldl f z (x:xs)  = foldl f (f z x) xs
  334.  
  335. foldl1           :: (a -> a -> a) -> [a] -> a
  336. foldl1 f (x:xs)   = foldl f x xs
  337.  
  338. foldl'           :: (a -> b -> a) -> a -> [b] -> a
  339. foldl' f a []     =  a
  340. foldl' f a (x:xs) =  strict (foldl' f) (f a x) xs
  341.  
  342. scanl            :: (a -> b -> a) -> a -> [b] -> [a]
  343. scanl f q xs      = q : (case xs of
  344.                          []   -> []
  345.                          x:xs -> scanl f (f q x) xs)
  346.  
  347. scanl1           :: (a -> a -> a) -> [a] -> [a]
  348. scanl1 f (x:xs)   = scanl f x xs
  349.  
  350. scanl'           :: (a -> b -> a) -> a -> [b] -> [a]
  351. scanl' f q xs     = q : (case xs of
  352.                          []   -> []
  353.                          x:xs -> strict (scanl' f) (f q x) xs)
  354.  
  355. foldr            :: (a -> b -> b) -> b -> [a] -> b
  356. foldr f z []      = z
  357. foldr f z (x:xs)  = f x (foldr f z xs)
  358.  
  359. foldr1           :: (a -> a -> a) -> [a] -> a
  360. foldr1 f [x]      = x
  361. foldr1 f (x:xs)   = f x (foldr1 f xs)
  362.  
  363. scanr            :: (a -> b -> b) -> b -> [a] -> [b]
  364. scanr f q0 []     = [q0]
  365. scanr f q0 (x:xs) = f x q : qs
  366.                     where qs@(q:_) = scanr f q0 xs
  367.  
  368. scanr1           :: (a -> a -> a) -> [a] -> [a]
  369. scanr1 f [x]      = [x]
  370. scanr1 f (x:xs)   = f x q : qs
  371.                     where qs@(q:_) = scanr1 f xs
  372.  
  373. -- List breaking functions:
  374. --
  375. --   take n xs       returns the first n elements of xs
  376. --   drop n xs       returns the remaining elements of xs
  377. --   splitAt n xs    = (take n xs, drop n xs)
  378. --
  379. --   takeWhile p xs  returns the longest initial segment of xs whose
  380. --                   elements satisfy p
  381. --   dropWhile p xs  returns the remaining portion of the list
  382. --   span p xs       = (takeWhile p xs, dropWhile p xs)
  383. --
  384. --   takeUntil p xs  returns the list of elements upto and including the
  385. --                   first element of xs which satisfies p
  386.  
  387. take                :: Int -> [a] -> [a]
  388. take 0     _         = []
  389. take _     []        = []
  390. take (n+1) (x:xs)    = x : take n xs
  391.  
  392. drop                :: Int -> [a] -> [a]
  393. drop 0     xs        = xs
  394. drop _     []        = []
  395. drop (n+1) (_:xs)    = drop n xs
  396.  
  397. splitAt             :: Int -> [a] -> ([a], [a])
  398. splitAt 0     xs     = ([],xs)
  399. splitAt _     []     = ([],[])
  400. splitAt (n+1) (x:xs) = (x:xs',xs'') where (xs',xs'') = splitAt n xs
  401.  
  402. takeWhile           :: (a -> Bool) -> [a] -> [a]
  403. takeWhile p []       = []
  404. takeWhile p (x:xs)
  405.          | p x       = x : takeWhile p xs
  406.          | otherwise = []
  407.  
  408. takeUntil           :: (a -> Bool) -> [a] -> [a]
  409. takeUntil p []       = []
  410. takeUntil p (x:xs)
  411.        | p x         = [x]
  412.        | otherwise   = x : takeUntil p xs
  413.  
  414. dropWhile           :: (a -> Bool) -> [a] -> [a]
  415. dropWhile p []       = []
  416. dropWhile p xs@(x:xs')
  417.          | p x       = dropWhile p xs'
  418.          | otherwise = xs
  419.  
  420. span, break         :: (a -> Bool) -> [a] -> ([a],[a])
  421. span p []            = ([],[])
  422. span p xs@(x:xs')
  423.          | p x       = let (ys,zs) = span p xs' in (x:ys,zs)
  424.          | otherwise = ([],xs)
  425. break p              = span (not . p)
  426.  
  427. -- Text processing:
  428. --   lines s     returns the list of lines in the string s.
  429. --   words s     returns the list of words in the string s.
  430. --   unlines ls  joins the list of lines ls into a single string
  431. --               with lines separated by newline characters.
  432. --   unwords ws  joins the list of words ws into a single string
  433. --               with words separated by spaces.
  434.  
  435. lines     :: String -> [String]
  436. lines ""   = []
  437. lines s    = l : (if null s' then [] else lines (tail s'))
  438.              where (l, s') = break ('\n'==) s
  439.  
  440. words     :: String -> [String]
  441. words s    = case dropWhile isSpace s of
  442.                   "" -> []
  443.                   s' -> w : words s''
  444.                         where (w,s'') = break isSpace s'
  445.  
  446. unlines   :: [String] -> String
  447. unlines    = concat . map (\l -> l ++ "\n")
  448.  
  449. unwords   :: [String] -> String
  450. unwords [] = []
  451. unwords ws = foldr1 (\w s -> w ++ ' ':s) ws
  452.  
  453. -- Merging and sorting lists:
  454.  
  455. merge               :: [a] -> [a] -> [a] 
  456. merge []     ys      = ys
  457. merge xs     []      = xs
  458. merge (x:xs) (y:ys)
  459.         | x <= y     = x : merge xs (y:ys)
  460.         | otherwise  = y : merge (x:xs) ys
  461.  
  462. sort                :: [a] -> [a]
  463. sort                 = foldr insert []
  464.  
  465. insert              :: a -> [a] -> [a]
  466. insert x []          = [x]
  467. insert x (y:ys)
  468.         | x <= y     = x:y:ys
  469.         | otherwise  = y:insert x ys
  470.  
  471. qsort               :: [a] -> [a]
  472. qsort []             = []
  473. qsort (x:xs)         = qsort [ u | u<-xs, u<x ] ++
  474.                              [ x ] ++
  475.                        qsort [ u | u<-xs, u>=x ]
  476.  
  477. -- zip and zipWith families of functions:
  478.  
  479. zip  :: [a] -> [b] -> [(a,b)]
  480. zip   = zipWith  (\a b -> (a,b))
  481.  
  482. zip3 :: [a] -> [b] -> [c] -> [(a,b,c)]
  483. zip3  = zipWith3 (\a b c -> (a,b,c))
  484.  
  485. zip4 :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
  486. zip4  = zipWith4 (\a b c d -> (a,b,c,d))
  487.  
  488. zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
  489. zip5  = zipWith5 (\a b c d e -> (a,b,c,d,e))
  490.  
  491. zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [(a,b,c,d,e,f)]
  492. zip6  = zipWith6 (\a b c d e f -> (a,b,c,d,e,f))
  493.  
  494. zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [(a,b,c,d,e,f,g)]
  495. zip7  = zipWith7 (\a b c d e f g -> (a,b,c,d,e,f,g))
  496.  
  497.  
  498. zipWith                  :: (a->b->c) -> [a]->[b]->[c]
  499. zipWith z (a:as) (b:bs)   = z a b : zipWith z as bs
  500. zipWith _ _      _        = []
  501.  
  502. zipWith3                 :: (a->b->c->d) -> [a]->[b]->[c]->[d]
  503. zipWith3 z (a:as) (b:bs) (c:cs)
  504.                           = z a b c : zipWith3 z as bs cs
  505. zipWith3 _ _ _ _          = []
  506.  
  507. zipWith4                 :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
  508. zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
  509.                           = z a b c d : zipWith4 z as bs cs ds
  510. zipWith4 _ _ _ _ _        = []
  511.  
  512. zipWith5                 :: (a->b->c->d->e->f) -> [a]->[b]->[c]->[d]->[e]->[f]
  513. zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
  514.                           = z a b c d e : zipWith5 z as bs cs ds es
  515. zipWith5 _ _ _ _ _ _      = []
  516.  
  517. zipWith6                 :: (a->b->c->d->e->f->g)
  518.                             -> [a]->[b]->[c]->[d]->[e]->[f]->[g]
  519. zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
  520.                           = z a b c d e f : zipWith6 z as bs cs ds es fs
  521. zipWith6 _ _ _ _ _ _ _    = []
  522.  
  523. zipWith7                 :: (a->b->c->d->e->f->g->h)
  524.                              -> [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
  525. zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
  526.                           = z a b c d e f g : zipWith7 z as bs cs ds es fs gs
  527. zipWith7 _ _ _ _ _ _ _ _  = []
  528.  
  529. unzip                    :: [(a,b)] -> ([a],[b])
  530. unzip                     = foldr (\(a,b) ~(as,bs) -> (a:as, b:bs)) ([], [])
  531.  
  532. -- Formatted output: --------------------------------------------------------
  533.  
  534. cjustify, ljustify, rjustify :: Int -> String -> String
  535.  
  536. cjustify n s = space halfm ++ s ++ space (m - halfm)
  537.                where m     = n - length s
  538.                      halfm = m `div` 2
  539. ljustify n s = s ++ space (n - length s)
  540. rjustify n s = space (n - length s) ++ s
  541.  
  542. space       :: Int -> String
  543. space n      = copy n ' '
  544.  
  545. layn        :: [String] -> String
  546. layn         = lay 1 where lay _ []     = []
  547.                            lay n (x:xs) = rjustify 4 (show n) ++ ") "
  548.                                            ++ x ++ "\n" ++ lay (n+1) xs
  549.  
  550. -- Miscellaneous: -----------------------------------------------------------
  551.  
  552. until                  :: (a -> Bool) -> (a -> a) -> a -> a
  553. until p f x | p x       = x
  554.             | otherwise = until p f (f x)
  555.  
  556. until'                 :: (a -> Bool) -> (a -> a) -> a -> [a]
  557. until' p f              = takeUntil p . iterate f
  558.  
  559. primitive error "primError" :: String -> a
  560.  
  561. undefined              :: a
  562. undefined | False       = undefined
  563.  
  564. asTypeOf               :: a -> a -> a
  565. x `asTypeOf` _          = x
  566.  
  567. -- I/O functions and definitions: -------------------------------------------
  568. -- This is the minimum required for bootstrapping and execution of
  569. -- interactive programs.
  570.  
  571. {- The Dialogue, Request, Response and IOError datatypes are now builtin:
  572. data Request  =  -- file system requests:
  573.                 ReadFile      String         
  574.               | WriteFile     String String
  575.               | AppendFile    String String
  576.                  -- channel system requests:
  577.               | ReadChan      String 
  578.               | AppendChan    String String
  579.                  -- environment requests:
  580.               | Echo          Bool
  581.           | GetArgs
  582.           | GetProgName
  583.           | GetEnv        String
  584.  
  585. data Response = Success
  586.               | Str String 
  587.               | Failure IOError
  588.  
  589. data IOError  = WriteError   String
  590.               | ReadError    String
  591.               | SearchError  String
  592.               | FormatError  String
  593.               | OtherError   String
  594.  
  595. -- Continuation-based I/O:
  596.  
  597. type Dialogue    =  [Response] -> [Request]
  598. -}
  599.  
  600. run        :: (String -> String) -> Dialogue
  601. run f ~(Success : ~(Str kbd : _))
  602.                  = [Echo False, ReadChan "stdin", AppendChan "stdout" (f kbd)]
  603.  
  604. primitive primFopen "primFopen" :: String -> a -> (String -> a) -> a
  605.  
  606. openfile        :: String -> String
  607. openfile f       = primFopen f (error ("can't open file "++f)) id
  608.  
  609. -- End of Gofer simplified prelude: ------------------------------------------
  610.