home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Demos / Lamvar / lambdaNu next >
Text File  |  1994-06-23  |  3KB  |  77 lines

  1. -----------------------------------------------------------------------------
  2. -- Lambda Nu:                                               January 25, 1993
  3. --
  4. -- The definitions in this file provide support for a simple implementation
  5. -- of Lambda Nu -- a generalisation of Lambda Var as described by Odersky,
  6. -- Rabin and Hudak in their POPL paper, January 1993.
  7. --
  8. -- Of course, the implementation of the fuction `begin' is not sound.  You must
  9. -- ensure that you use this function correctly -- the responsibility is on you,
  10. -- the programmer.
  11. --
  12. -- Incidentally, the definitions in this file can only be used if the
  13. -- version of Gofer that you are using has been compiled with the correct
  14. -- set of primitives included.  In addition, there is no support for these
  15. -- primitives in gofc, the Gofer compiler.
  16. --
  17. -- Operator precedence table: -----------------------------------------------
  18.  
  19. infixr 3 =:
  20. infixr 2 >>, >>=, ?
  21.  
  22. -- Lambda nu hacking: -------------------------------------------------------
  23.  
  24. primitive return    "primLnReturn" :: a -> Cmd b a
  25. primitive (>>=)        "primLnBind"   :: Cmd a b -> (b -> Cmd a c) -> Cmd a c
  26. primitive primLnTagEq    "primLnTagEq"  :: Tag a -> Tag a -> Bool
  27. primitive newvar    "primLnNew"    :: Cmd a (Tag b)
  28. primitive assign    "primLnAssign" :: Tag a -> a -> Cmd b ()
  29. primitive (?)        "primLnRead"   :: Tag a -> (a -> Cmd b c) -> Cmd b c
  30. primitive io        "primLnIo"     :: ((a -> b) -> b) -> Cmd b a
  31. primitive begin        "primLnBegin"  :: Cmd a b -> a
  32.  
  33. instance Eq (Tag a) where
  34.     (==) = primLnTagEq
  35.  
  36. (>>)         :: Cmd c a -> Cmd c b -> Cmd c b
  37. f >> g        = f >>= const g
  38.  
  39. seq          :: [Cmd m a] -> Cmd m ()
  40. seq           = foldr (>>) (return ())
  41.  
  42. new          :: (Tag a -> Cmd b c) -> Cmd b c
  43. new           = (>>=) newvar
  44.  
  45. (=:)         :: a -> Tag a -> Cmd b ()
  46. value =: tag  = assign tag value
  47.  
  48. out          :: (a -> a) -> Cmd a ()
  49. out a         = io (\c -> a (c ()))
  50.  
  51. outConst      = out . const
  52.  
  53. pure         :: Cmd a a -> a
  54. pure a        = begin (a >>= outConst)
  55.  
  56. deref        :: Tag a -> Cmd b a
  57. deref t       = t ? return
  58.  
  59. -- Very simple monadic I/O in the Glasgow style: -----------------------------
  60.  
  61. primitive getch           "primLnGetch"    :: Cmd a Char
  62. primitive putchar      "primLnPutchar"  :: Char -> Cmd a ()
  63. primitive system       "primLnSystem"   :: String -> Cmd a Int
  64.  
  65. getchar :: Cmd a Char
  66. getchar  = getch     >>= \c ->
  67.        putchar c >>
  68.        return c
  69.  
  70. puts    :: String -> Cmd a ()
  71. puts     = seq . map putchar
  72.  
  73. -- an abuse of pure to implement hbc's debugging hack:
  74. trace s a = pure (puts s >> return a)
  75.  
  76. -- End of lambdaNu -----------------------------------------------------------
  77.