diff options
author | Mart Lubbers | 2015-04-24 17:22:27 +0200 |
---|---|---|
committer | Mart Lubbers | 2015-04-24 17:22:27 +0200 |
commit | 5235f58efa84ebe137a932b7ca547efd7d73a205 (patch) | |
tree | 0f5a7773c542e1452b4bb7c84d8bb7469c9e8dbb /fp2/week2/mart/StdIOMonad.icl | |
parent | rare shit (diff) |
______________
< Lekker bezig >
--------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Diffstat (limited to 'fp2/week2/mart/StdIOMonad.icl')
-rw-r--r-- | fp2/week2/mart/StdIOMonad.icl | 120 |
1 files changed, 114 insertions, 6 deletions
diff --git a/fp2/week2/mart/StdIOMonad.icl b/fp2/week2/mart/StdIOMonad.icl index 1d4b24e..b0f2b4f 100644 --- a/fp2/week2/mart/StdIOMonad.icl +++ b/fp2/week2/mart/StdIOMonad.icl @@ -2,15 +2,123 @@ implementation module StdIOMonad // Deze module verpakt StdFile in een monadische jas
+import StdBool
+import StdEnum
import StdFile
+import StdList
+import StdMaybe
+import StdMisc
import StdMonad
-//import StdOverloaded
+import StdOverloaded
+import StdString
+import StdTuple
-:: IO a = IO [a] // kies een geschikte representatie voor IO
+:: IO a = IO (*W -> *(a, *W))
+:: *W :== *(*World, *[*(Filehandle, *File)])
:: Filemode = Lees | Schrijf
:: Filenaam :== String
-:: Filehandle :== [] // kies een geschikte representatie voor Filehandle
+:: Filehandle :== String
-//instance toInt Filemode where
-// toInt Lees = FReadText
-// toInt Schrijf = FWriteText
+instance toInt Filemode where
+ toInt Lees = FReadText
+ toInt Schrijf = FWriteText
+
+//voer monadische I/O actie uit op de wereld:
+doIO:: (IO a) *World -> *(a, *W)
+doIO (IO f) w = f (w, [])
+
+unIO:: (IO a) -> *W -> *(a, *W)
+unIO (IO f) = f
+
+// IO is een monad:
+instance return IO where
+ return x = IO (\w -> (x, w))
+instance >>= IO where
+ (>>=) (IO f) g = IO (\w = let (a, w1) = f w in unIO (g a) w1)
+
+//Start world = doIO (read >>= (\w = return w)) (world, "")
+//Start world = doIO (open "camilt.txt" Lees) (world, "")
+//Start world = doIO (read >>= (\w = read)) (world, "")
+
+read:: IO String
+read = IO read`
+ where
+ read`:: *W -> *(String, *W)
+ read` (world, s)
+ # (io, world) = stdio world
+ # (line, io) = freadline io
+ # (_, world) = fclose io world
+ = (line, (world, s))
+
+// schrijf regel naar de console:
+write :: String -> IO Void
+write s = IO (write` s)
+ where
+ write`:: String *W -> *(Void, *W)
+ write` line (world, s)
+ # (io, world) = stdio world
+ # io = io <<< line
+ # (_, world) = fclose io world
+ = (Void, (world, s))
+
+
+Start world = doIO (
+ open "camil.txt" Lees >>=
+ \y = eof "camil.txt") world
+
+// open de file met gegeven filenaam en mode:
+find:: Filehandle *[*(Filehandle, *File)] -> (Maybe *(Filehandle, *File), *[*(Filehandle, *File)])
+find fh fs
+# (fhs, fis) = unzip fs
+# fhsC = zip2 [0..length fhs] fhs
+# index = [(i, h) \\ (i, h) <- fhsC | h == fh]
+| length index == 0 = (Nothing, zip2 fhs fis)
+# index = fst (hd index)
+# (fis1, fis2) = splitAt index fis
+# (fhs1, fhs2) = splitAt index fhs
+# (thefile, fis2) = splitAt 1 fis2
+# (thehandle, fhs2) = splitAt 1 fhs2
+= (Just (hd thehandle, hd thefile), zip2 (fhs1 ++ fhs2) (fis1 ++ fis2))
+
+
+open:: Filenaam Filemode -> IO (Maybe Filehandle)
+open s m = IO (open` s m)
+ where
+ open`:: String Filemode *W -> *(Maybe Filehandle, *W)
+ open` fp m (world, fs)
+ | any (\l = fp == fst l) fs = (Nothing, (world, fs))
+ # (ok, file, world) = fopen fp (toInt m) world
+ = (Just fp, (world, [(fp, file):fs]))
+
+// sluit de file met gegeven filenaam:
+close:: Filehandle -> IO Bool
+close fh = IO (close` fh)
+ where
+ close`:: Filehandle *W -> *(Bool, *W)
+ close` fp (world, fs)
+ # (currentfiletuple, fs) = find fp fs
+ | isNothing currentfiletuple = (False, (world, fs))
+ # (currentfh, currentfile) = fromJust currentfiletuple
+ # (ok, world) = fclose currentfile world
+ | not ok = abort "File can't be closed"
+ | otherwise = (True, (world, fs))
+
+
+// bepaal of het lezen van de file klaar is:
+eof :: Filehandle -> IO Bool
+eof fh = IO (eof` fh)
+ where
+ eof`:: Filehandle *W -> *(Bool, *W)
+ eof` fp (world, fs)
+ # (currentfiletuple, fs) = find fp fs
+ | isNothing currentfiletuple = abort "Can't do eof on non-existing file"
+ # (currentfh, currentfile) = fromJust currentfiletuple
+ # (ok, file) = fend currentfile
+ = (ok, (world, [(currentfh, file):fs]))
+
+// lees een regel van een file:
+//readline :: Filehandle -> IO (Maybe String)
+
+
+// schrijf een regel naar een file:
+writeline :: String Filehandle -> IO Bool
|