-implementation module StdIOMonad\r
-\r
-//Deze module verpakt StdFile in een monadische jas\r
-\r
-import StdFile\r
-import StdMonad\r
-import StdMaybeMonad\r
-\r
-\r
-:: IO a = IO (*World Filehandle -> *(a, *World, Filehandle))\r
-:: Void = Void\r
-:: Filemode = Lees | Schrijf\r
-:: Filenaam :== String\r
-:: *Filehandle = None | FH *(Bool, Filemode, *File)\r
-\r
-toInt :: Filemode -> Int\r
-toInt Lees = FReadText\r
-toInt Schrijf = FWriteText\r
-\r
-Start world = read\r
-\r
-////voer monadische I/O actie uit op de wereld:\r
-//doIO :: (IO a) *World -> *(a,*World)\r
-//doIO (IO f) world = f (world, Closed)\r
-\r
-// IO is een monad:\r
-instance return IO where\r
- return x = IO (\w fh = (x, w, fh))\r
-//instance >>= IO where\r
-// (>>=) (IO f) g = IO(\w = let (a, w1) = f w in doIO (g a) w1)\r
-\r
-//lees regel van de console:\r
-read:: IO String\r
-read = IO (\w fh -> ("", w, fh))\r
-\r
-/*//schrijf regel naar de console:\r
-write:: String -> IO Void\r
-write s = IO (\w = (Void, write` s w))\r
- where\r
- write`:: String *World -> *World\r
- write` s world\r
- # (io, world) = stdio world\r
- # io = fwrites s world\r
- # (_, world) = fclose io\r
- = world\r
-\r
-//open de file met gegeven filenaam en mode:\r
-open:: Filenaam Filemode -> IO (Maybe Filehandle)\r
-open s m = IO (\w = let (f1, w1) = openfh` s m w in (f1, (w1, f1)))\r
- where\r
- openfh`:: Filenaam Filemode *World -> (Maybe Filehandle, *World)\r
- openfh` s m world\r
- # (ok, file, world) = fopen s (toInt m) world\r
- | ok = (Just (Open (m, file)), world)\r
- | otherwise = (Nothing, world)\r
-\r
-//sluit de file met gegeven filenaam:\r
-close:: Filehandle -> IO Bool\r
-close fh = IO (\w = let (b1, w1) = close` fh w in (b1, (w1, Closed)))\r
- where\r
- close`:: Filehandle *World -> (Bool, *World)\r
- close` Closed world = (False, world)\r
- close` (Open (_, file)) world = fclose file world\r
-\r
-//bepaal of het lezen van de file klaar is:\r
-eof:: Filehandle -> IO Bool\r
-eof fh = IO (\w = let (b1, w1) = eof` fh w in (b1, (w1, Closed)))\r
- where\r
- eof`:: Filehandle *World -> (Bool, *World)\r
- eof` Closed world = (world, False)\r
- eaf`(Open (_, file)) world = fclose file\r
-\r
-//lees een regel van een file:\r
-readline:: Filehandle -> IO (Maybe String)\r
-readline fh = IO (\w = let r1 = readline` fh w in r1)\r
- where\r
- readline`:: Filehandle *World -> (Maybe String, (*World, Filehandle))\r
- readline` Closed world = (world, Nothing)\r
- readline` (Open (Schrijf, _)) world = (world, Nothing)\r
- readline` (Open (Lees, file)) world\r
- # (line, file) = sfreadline file\r
- = (Just line, (world, Open (Lees, file)))\r
-\r
-//schrijf een regel naar een file:\r
-writeline:: String Filehandle -> IO Bool\r
-writeline s fh = IO (\w = let r1 = writeline` s fh w in r1)\r
- where\r
- writeline`:: String Filehandle *World -> (Bool, (*World, Filehandle))\r
- writeline` line Closed world = (False, (world, Closed))\r
- writeline` line (Open (Lees, file)) world = (False, (world, (Open (Lees, file))))\r
- writeline` line (Open (Schrijf, file)) world\r
- # file = fwrites line file\r
- = (True, (world, file))*/\r