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

  1. ------------------------------------------------------------------------------
  2. -- This file contains a Gofer implementation of Lazy state threads, as
  3. -- described in the PLDI '94 paper by John Launchbury and Simon Peyton
  4. -- Jones, using new Gofer primitives added in Gofer 2.30.
  5. --
  6. -- This file is included for the benefit of those interested in
  7. -- experimenting with the use of lazy functional state threads.
  8. -- You may expect to see changes to the definitions in this file,
  9. -- to track future proposals for monadic I/O in Haskell.
  10. --
  11. -- This file requires the standard, or cc prelude.
  12. -- You will not be able to use this file unless the version of Gofer that
  13. -- is installed on your machine has been compiled with the IO_MONAD flag
  14. -- set to 1.
  15. --
  16. -- Mark P Jones, 1994
  17. ------------------------------------------------------------------------------
  18.  
  19. module LazyStateThd( thenST, thenST_, returnST, newVar, readVar, WriteVar,
  20.              mutVarEq, getch, putchar, thenIO, seqST, putString,
  21.              getchar, interleaveST
  22.            ) where
  23.  
  24. infixr `thenST_`, `thenST`
  25.  
  26. primitive returnST     "primSTReturn"   :: a -> ST s a
  27. primitive thenST       "primSTBind"     :: ST s a -> (a -> ST s b) -> ST s b
  28. primitive newVar       "primSTNew"      :: a -> ST s (MutVar s a)
  29. primitive readVar      "primSTDeref"    :: MutVar s a -> ST s a
  30. primitive writeVar     "primSTAssign"   :: MutVar s a -> a -> ST s ()
  31. primitive mutvarEq     "primSTMutVarEq" :: MutVar s a -> MutVar s a -> Bool
  32. primitive getch        "primIOGetch"    :: IO Char
  33. primitive putchar      "primIOPutchar"  :: Char -> IO ()
  34. primitive thenIO       "primIOBind"     :: IO a -> (a -> IO b) -> IO b
  35. primitive interleaveST "primSTInter"    :: ST s a -> ST s a
  36.  
  37. instance Eq (MutVar s a) where (==) = mutvarEq
  38.  
  39. thenST_       :: ST s () -> ST s b -> ST s b
  40. p `thenST_` q  = p `thenST` \() -> q
  41.  
  42. seqST         :: [ST s ()] -> ST s ()
  43. seqST          = foldr thenST_ (returnST ())
  44.  
  45. putString     :: String -> IO ()
  46. putString      = seqST . map putchar
  47.  
  48. getchar = getch       `thenST` \c ->
  49.           putchar c   `thenST_`
  50.           returnST c
  51.  
  52. ------------------------------------------------------------------------------
  53.