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

  1. -----------------------------------------------------------------------------
  2. -- Lambda Var:                                              November 3, 1992
  3. --
  4. -- The definitions in this file provide support for a simple implementation
  5. -- of Lambda Var -- as described by Odersky, Rabin and Hudak in their POPL
  6. -- paper, January 1993.  Note however, that the implementation of the
  7. -- fuction `pure' is not sound.  You must ensure that you use this function
  8. -- correctly -- the responsibility is on you, the programmer.
  9. --
  10. -- This file must be loaded with the constructor classes prelude, which is
  11. -- usually called `cc.prelude'.
  12. --
  13. -- Incidentally, the definitions in this file can only be used if the
  14. -- version of Gofer that you are using has been compiled with the correct
  15. -- set of primitives included.  In addition, there is no support for these
  16. -- primitives in gofc, the Gofer compiler.
  17. --
  18. -- Operator precedence table: -----------------------------------------------
  19.  
  20. infixr 1 =:
  21. infixr 0 >>, >>=, ?
  22.  
  23. -- Lambda var hacking: ------------------------------------------------------
  24.  
  25. primitive pure         "primLvPure"   :: Proc a -> a
  26. primitive (?)          "primLvRead"   :: Var a -> (a -> Proc b) -> Proc b
  27. primitive primLvReturn "primLvReturn" :: a -> Proc a
  28. primitive primLvBind   "primLvBind"   :: Proc a -> (a -> Proc b) -> Proc b
  29. primitive (=:)           "primLvAssign" :: a -> Var a -> Proc b
  30. primitive var           "primLvVar"    :: (Var a -> Proc b) -> Proc b
  31. primitive newvar       "primLvNewvar" :: Proc (Var a)
  32. primitive primLvVarEq  "primLvVarEq"  :: Var a -> Var a -> Bool
  33.  
  34. type Prog      = Proc ()
  35.  
  36. deref            :: Var a -> Proc a
  37. deref v           = v ? result
  38.  
  39. (>>=)             :: Monad m => m a -> (a -> m b) -> m b
  40. (>>=)              = bind
  41.  
  42. (>>)             :: Monad m => m a -> m b -> m b
  43. f >> g            = f >>= const g
  44.  
  45. seq              :: Monad m => [m a] -> m ()
  46. seq          = foldr (>>) (result ())
  47.  
  48. instance Eq (Var a)   where (==)    = primLvVarEq
  49.  
  50. instance Functor Proc where map f x = [ f y | y <- x ]
  51.  
  52. instance Monad Proc   where result  = primLvReturn
  53.                 bind    = primLvBind
  54.  
  55. -- Very simple monadic I/O in the Glasgow style: -----------------------------
  56.  
  57. primitive getch           "primLvGetch"    :: Proc Char
  58. primitive putchar      "primLvPutchar"  :: Char -> Proc ()
  59.  
  60. getchar :: Proc Char
  61. getchar  = getch     >>= \c ->
  62.        putchar c >>
  63.        result c
  64.  
  65. puts    :: String -> Proc ()
  66. puts     = seq . map putchar
  67.  
  68. -- an abuse of pure to implement hbc's debugging hack:
  69. trace s a = pure (puts s >> result a)
  70.  
  71. -- End of lambdaVr -----------------------------------------------------------
  72.