From: Mart Lubbers Date: Fri, 24 Apr 2015 12:35:17 +0000 (+0200) Subject: rare shit X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=a067356625105f50978443d3b0a0a5d8f6391184;p=fp1415.git rare shit --- diff --git a/fp2/week2/mart/EchoMonad.icl b/fp2/week2/mart/EchoMonad.icl new file mode 100644 index 0000000..3bcab11 --- /dev/null +++ b/fp2/week2/mart/EchoMonad.icl @@ -0,0 +1,14 @@ +module EchoMonad + +import StdString // expliciete import uit StdEnv om nameclash met StdFunc / StdIOMonad te voorkomen +import StdIOMonad +import StdMaybeMonad + +Start :: *World -> (Void,*World) +Start world = doIO echo world + +echo :: IO Void +echo = read >>= \regel -> + if (regel == "\n") + (return Void) + (write regel >>= \_ -> echo) diff --git a/fp2/week2/mart/StdIOMonad.dcl b/fp2/week2/mart/StdIOMonad.dcl new file mode 100644 index 0000000..0d451d2 --- /dev/null +++ b/fp2/week2/mart/StdIOMonad.dcl @@ -0,0 +1,39 @@ +definition module StdIOMonad + +// Deze module verpakt een aantal StdFile functies in een monadische jas + +import StdMonad, StdMaybeMonad + +:: IO a +:: Void = Void +:: Filemode = Lees | Schrijf +:: Filenaam :== String +:: Filehandle + +// voer monadische I/O actie uit op de wereld: +//doIO :: (IO a) *World -> (a,*World) + +// IO is een monad: +//instance return IO +//instance >>= IO +// +//// lees regel van de console: +//read :: IO String +// +//// schrijf regel naar de console: +//write :: String -> IO Void +// +//// open de file met gegeven filenaam en mode: +//open :: Filenaam Filemode -> IO (Maybe Filehandle) +// +//// sluit de file met gegeven filenaam: +//close :: Filehandle -> IO Bool +// +//// bepaal of het lezen van de file klaar is: +//eof :: Filehandle -> IO Bool +// +//// lees een regel van een file: +//readline :: Filehandle -> IO (Maybe String) +// +//// schrijf een regel naar een file: +//writeline :: String Filehandle -> IO Bool diff --git a/fp2/week2/mart/StdIOMonad.icl b/fp2/week2/mart/StdIOMonad.icl index 7f112a7..1d4b24e 100644 --- a/fp2/week2/mart/StdIOMonad.icl +++ b/fp2/week2/mart/StdIOMonad.icl @@ -1,22 +1,16 @@ -module StdIOMonad - -import StdEnv, StdMaybe, StdMonad, StdFile - -:: IO a = IO (*World -> *(a, *World)) - -read :: *World -> (IO String, *World) -read world -# (io, world) = stdio world -# (line, io) = freadline io -# (ok, world) = fclose io -| not ok = abort "Couldn't close console" -| otherwise = line - -instance return IO where - return x = IO (\w = (x, w)) - -instance >>= IO where - >>= (IO f) g = (IO f) - -Start :: *World -> (IO String, *World) -Start world = read +implementation module StdIOMonad + +// Deze module verpakt StdFile in een monadische jas + +import StdFile +import StdMonad +//import StdOverloaded + +:: IO a = IO [a] // kies een geschikte representatie voor IO +:: Filemode = Lees | Schrijf +:: Filenaam :== String +:: Filehandle :== [] // kies een geschikte representatie voor Filehandle + +//instance toInt Filemode where +// toInt Lees = FReadText +// toInt Schrijf = FWriteText diff --git a/fp2/week2/mart/StdListMonad.dcl b/fp2/week2/mart/StdListMonad.dcl new file mode 100644 index 0000000..576ef55 --- /dev/null +++ b/fp2/week2/mart/StdListMonad.dcl @@ -0,0 +1,7 @@ +definition module StdListMonad + +import StdMonad + +instance return [] +instance >>= [] +instance fail [] diff --git a/fp2/week2/mart/StdListMonad.icl b/fp2/week2/mart/StdListMonad.icl new file mode 100644 index 0000000..b3b3d62 --- /dev/null +++ b/fp2/week2/mart/StdListMonad.icl @@ -0,0 +1,7 @@ +implementation module StdListMonad + +import StdMonad + +instance return [] where return x = [x] +instance >>= [] where >>= xs f = [y \\ x <- xs, y <- f x] +instance fail [] where fail = [] diff --git a/fp2/week2/mart/StdMaybeMonad.dcl b/fp2/week2/mart/StdMaybeMonad.dcl new file mode 100644 index 0000000..e9ebec1 --- /dev/null +++ b/fp2/week2/mart/StdMaybeMonad.dcl @@ -0,0 +1,9 @@ +definition module StdMaybeMonad + +import StdMonad + +:: Maybe a = Nothing | Just a + +instance return Maybe +instance >>= Maybe +instance fail Maybe diff --git a/fp2/week2/mart/StdMaybeMonad.icl b/fp2/week2/mart/StdMaybeMonad.icl new file mode 100644 index 0000000..1c6277d --- /dev/null +++ b/fp2/week2/mart/StdMaybeMonad.icl @@ -0,0 +1,10 @@ +implementation module StdMaybeMonad + +import StdMonad + +:: Maybe a = Nothing | Just a + +instance return Maybe where return x = Just x +instance >>= Maybe where >>= (Just x) f = f x + >>= Nothing f = Nothing +instance fail Maybe where fail = Nothing diff --git a/fp2/week2/mart/ToonFileMonad.icl b/fp2/week2/mart/ToonFileMonad.icl new file mode 100644 index 0000000..acd62c2 --- /dev/null +++ b/fp2/week2/mart/ToonFileMonad.icl @@ -0,0 +1,30 @@ +module ToonFileMonad + +import StdArray, StdInt, StdString // expliciete imports van StdEnv om name-clash met StdFunc en StdIOMonad te voorkomen +import StdIOMonad +import StdMaybeMonad + +Start :: *World -> (Void,*World) +Start world = doIO toon world + +toon :: IO Void +toon = write "Voer een filenaam in: " >>= \_ -> + read >>= \filenaam -> + open (filenaam%(0,size filenaam-2)) Lees >>= \misschien_filehandle -> + case misschien_filehandle of + Nothing + = write ("Kon " +++ filenaam +++ " niet openen.\n") >>= \_ -> + return Void + Just filehandle + = toon_inhoud filehandle + +toon_inhoud :: Filehandle -> IO Void +toon_inhoud filehandle + = eof filehandle >>= \einde -> + if einde + (return Void) + (readline filehandle >>= \misschien_regel -> + case misschien_regel of + Nothing = return Void + Just regel = write regel >>= \_ -> toon_inhoud filehandle + ) diff --git a/fp2/week2/mart/Map.dcl b/fp2/week2/mart/oldold/old/Map.dcl similarity index 100% rename from fp2/week2/mart/Map.dcl rename to fp2/week2/mart/oldold/old/Map.dcl diff --git a/fp2/week2/mart/Map.icl b/fp2/week2/mart/oldold/old/Map.icl similarity index 100% rename from fp2/week2/mart/Map.icl rename to fp2/week2/mart/oldold/old/Map.icl diff --git a/fp2/week2/mart/Mappen.icl b/fp2/week2/mart/oldold/old/Mappen.icl similarity index 100% rename from fp2/week2/mart/Mappen.icl rename to fp2/week2/mart/oldold/old/Mappen.icl diff --git a/fp2/week2/mart/Random.dcl b/fp2/week2/mart/oldold/old/Random.dcl similarity index 100% rename from fp2/week2/mart/Random.dcl rename to fp2/week2/mart/oldold/old/Random.dcl diff --git a/fp2/week2/mart/Random.icl b/fp2/week2/mart/oldold/old/Random.icl similarity index 100% rename from fp2/week2/mart/Random.icl rename to fp2/week2/mart/oldold/old/Random.icl diff --git a/fp2/week2/mart/ReturnEnBind.icl b/fp2/week2/mart/oldold/old/ReturnEnBind.icl similarity index 100% rename from fp2/week2/mart/ReturnEnBind.icl rename to fp2/week2/mart/oldold/old/ReturnEnBind.icl diff --git a/fp2/week2/mart/oldold/old/StdIOMonad.dcl b/fp2/week2/mart/oldold/old/StdIOMonad.dcl new file mode 100644 index 0000000..375f077 --- /dev/null +++ b/fp2/week2/mart/oldold/old/StdIOMonad.dcl @@ -0,0 +1,40 @@ +definition module StdIOMonad + +// Deze module verpakt een aantal StdFile functies in een monadische jas + +import StdMonad, StdMaybeMonad + +:: IO a +:: Void = Void +:: Filemode = Lees | Schrijf +:: Filenaam :== String +:: *Filehandle + +// voer monadische I/O actie uit op de wereld: +//doIO :: (IO a) *World -> *(a,*World) + +// IO is een monad: +//instance return IO +//instance >>= IO + +// lees regel van de console: +read :: IO String + +/* +// schrijf regel naar de console: +write :: String -> IO Void + +// open de file met gegeven filenaam en mode: +open :: Filenaam Filemode -> IO (Maybe Filehandle) + +// sluit de file met gegeven filenaam: +close :: Filehandle -> IO Bool + +// bepaal of het lezen van de file klaar is: +eof :: Filehandle -> IO Bool + +// lees een regel van een file: +readline :: Filehandle -> IO (Maybe String) + +// schrijf een regel naar een file: +writeline :: String Filehandle -> IO Bool*/ diff --git a/fp2/week2/mart/oldold/old/StdIOMonad.icl b/fp2/week2/mart/oldold/old/StdIOMonad.icl new file mode 100644 index 0000000..0338a44 --- /dev/null +++ b/fp2/week2/mart/oldold/old/StdIOMonad.icl @@ -0,0 +1,93 @@ +implementation module StdIOMonad + +//Deze module verpakt StdFile in een monadische jas + +import StdFile +import StdMonad +import StdMaybeMonad + + +:: IO a = IO (*World Filehandle -> *(a, *World, Filehandle)) +:: Void = Void +:: Filemode = Lees | Schrijf +:: Filenaam :== String +:: *Filehandle = None | FH *(Bool, Filemode, *File) + +toInt :: Filemode -> Int +toInt Lees = FReadText +toInt Schrijf = FWriteText + +Start world = read + +////voer monadische I/O actie uit op de wereld: +//doIO :: (IO a) *World -> *(a,*World) +//doIO (IO f) world = f (world, Closed) + +// IO is een monad: +instance return IO where + return x = IO (\w fh = (x, w, fh)) +//instance >>= IO where +// (>>=) (IO f) g = IO(\w = let (a, w1) = f w in doIO (g a) w1) + +//lees regel van de console: +read:: IO String +read = IO (\w fh -> ("", w, fh)) + +/*//schrijf regel naar de console: +write:: String -> IO Void +write s = IO (\w = (Void, write` s w)) + where + write`:: String *World -> *World + write` s world + # (io, world) = stdio world + # io = fwrites s world + # (_, world) = fclose io + = world + +//open de file met gegeven filenaam en mode: +open:: Filenaam Filemode -> IO (Maybe Filehandle) +open s m = IO (\w = let (f1, w1) = openfh` s m w in (f1, (w1, f1))) + where + openfh`:: Filenaam Filemode *World -> (Maybe Filehandle, *World) + openfh` s m world + # (ok, file, world) = fopen s (toInt m) world + | ok = (Just (Open (m, file)), world) + | otherwise = (Nothing, world) + +//sluit de file met gegeven filenaam: +close:: Filehandle -> IO Bool +close fh = IO (\w = let (b1, w1) = close` fh w in (b1, (w1, Closed))) + where + close`:: Filehandle *World -> (Bool, *World) + close` Closed world = (False, world) + close` (Open (_, file)) world = fclose file world + +//bepaal of het lezen van de file klaar is: +eof:: Filehandle -> IO Bool +eof fh = IO (\w = let (b1, w1) = eof` fh w in (b1, (w1, Closed))) + where + eof`:: Filehandle *World -> (Bool, *World) + eof` Closed world = (world, False) + eaf`(Open (_, file)) world = fclose file + +//lees een regel van een file: +readline:: Filehandle -> IO (Maybe String) +readline fh = IO (\w = let r1 = readline` fh w in r1) + where + readline`:: Filehandle *World -> (Maybe String, (*World, Filehandle)) + readline` Closed world = (world, Nothing) + readline` (Open (Schrijf, _)) world = (world, Nothing) + readline` (Open (Lees, file)) world + # (line, file) = sfreadline file + = (Just line, (world, Open (Lees, file))) + +//schrijf een regel naar een file: +writeline:: String Filehandle -> IO Bool +writeline s fh = IO (\w = let r1 = writeline` s fh w in r1) + where + writeline`:: String Filehandle *World -> (Bool, (*World, Filehandle)) + writeline` line Closed world = (False, (world, Closed)) + writeline` line (Open (Lees, file)) world = (False, (world, (Open (Lees, file)))) + writeline` line (Open (Schrijf, file)) world + # file = fwrites line file + = (True, (world, file))*/ diff --git a/fp2/week2/mart/oldold/old/StdMaybeMonad.dcl b/fp2/week2/mart/oldold/old/StdMaybeMonad.dcl new file mode 100644 index 0000000..e9ebec1 --- /dev/null +++ b/fp2/week2/mart/oldold/old/StdMaybeMonad.dcl @@ -0,0 +1,9 @@ +definition module StdMaybeMonad + +import StdMonad + +:: Maybe a = Nothing | Just a + +instance return Maybe +instance >>= Maybe +instance fail Maybe diff --git a/fp2/week2/mart/oldold/old/StdMaybeMonad.icl b/fp2/week2/mart/oldold/old/StdMaybeMonad.icl new file mode 100644 index 0000000..1c6277d --- /dev/null +++ b/fp2/week2/mart/oldold/old/StdMaybeMonad.icl @@ -0,0 +1,10 @@ +implementation module StdMaybeMonad + +import StdMonad + +:: Maybe a = Nothing | Just a + +instance return Maybe where return x = Just x +instance >>= Maybe where >>= (Just x) f = f x + >>= Nothing f = Nothing +instance fail Maybe where fail = Nothing diff --git a/fp2/week2/mart/oldold/old/StdMonad.dcl b/fp2/week2/mart/oldold/old/StdMonad.dcl new file mode 100644 index 0000000..cd1c654 --- /dev/null +++ b/fp2/week2/mart/oldold/old/StdMonad.dcl @@ -0,0 +1,8 @@ +definition module StdMonad + +class return c :: a -> c a +class (>>=) infix 0 c :: (c a) (a -> c b) -> c b +class fail c :: c a + +class Monad c | return, >>= c +class MonadFail c | Monad, fail c diff --git a/fp2/week2/mart/oldold/old/StdMonad.icl b/fp2/week2/mart/oldold/old/StdMonad.icl new file mode 100644 index 0000000..db193ab --- /dev/null +++ b/fp2/week2/mart/oldold/old/StdMonad.icl @@ -0,0 +1 @@ +implementation module StdMonad