home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities / _gofer / !Gofer / archives / Demos / Lamvar / lambdaVr < prev    next >
Text File  |  1993-02-12  |  3KB  |  69 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. ($=)             :: Monad m => m a -> (a -> m b) -> m b
  37. ($=)              = bind
  38.  
  39. ($$)             :: Monad m => m a -> m b -> m b
  40. f $$ g            = f $= const g
  41.  
  42. seq              :: Monad m => [m a] -> m ()
  43. seq          = foldr ($$) (result ())
  44.  
  45. instance Eq (Var a)   where (==)    = primLvVarEq
  46.  
  47. instance Functor Proc where map f x = [ f y | y <- x ]
  48.  
  49. instance Monad Proc   where result  = primLvReturn
  50.                 bind    = primLvBind
  51.  
  52. -- Very simple monadic I/O in the Glasgow style: -----------------------------
  53.  
  54. primitive getch           "primLvGetch"    :: Proc Char
  55. primitive putchar      "primLvPutchar"  :: Char -> Proc ()
  56.  
  57. getchar :: Proc Char
  58. getchar  = getch     $= \c ->
  59.        putchar c $$
  60.        result c
  61.  
  62. puts    :: String -> Proc ()
  63. puts     = seq . map putchar
  64.  
  65. -- an abuse of pure to implement hbc's debugging hack:
  66. trace s a = pure (puts s $$ result a)
  67.  
  68. -- End of lambdaVr -----------------------------------------------------------
  69.