From a067356625105f50978443d3b0a0a5d8f6391184 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Fri, 24 Apr 2015 14:35:17 +0200 Subject: rare shit --- fp2/week2/mart/oldold/old/Map.dcl | 10 ++++ fp2/week2/mart/oldold/old/Map.icl | 19 ++++++ fp2/week2/mart/oldold/old/Mappen.icl | 9 +++ fp2/week2/mart/oldold/old/Random.dcl | 19 ++++++ fp2/week2/mart/oldold/old/Random.icl | 20 +++++++ fp2/week2/mart/oldold/old/ReturnEnBind.icl | 19 ++++++ fp2/week2/mart/oldold/old/StdIOMonad.dcl | 40 +++++++++++++ fp2/week2/mart/oldold/old/StdIOMonad.icl | 93 +++++++++++++++++++++++++++++ fp2/week2/mart/oldold/old/StdMaybeMonad.dcl | 9 +++ fp2/week2/mart/oldold/old/StdMaybeMonad.icl | 10 ++++ fp2/week2/mart/oldold/old/StdMonad.dcl | 8 +++ fp2/week2/mart/oldold/old/StdMonad.icl | 1 + 12 files changed, 257 insertions(+) create mode 100644 fp2/week2/mart/oldold/old/Map.dcl create mode 100644 fp2/week2/mart/oldold/old/Map.icl create mode 100644 fp2/week2/mart/oldold/old/Mappen.icl create mode 100644 fp2/week2/mart/oldold/old/Random.dcl create mode 100644 fp2/week2/mart/oldold/old/Random.icl create mode 100644 fp2/week2/mart/oldold/old/ReturnEnBind.icl create mode 100644 fp2/week2/mart/oldold/old/StdIOMonad.dcl create mode 100644 fp2/week2/mart/oldold/old/StdIOMonad.icl create mode 100644 fp2/week2/mart/oldold/old/StdMaybeMonad.dcl create mode 100644 fp2/week2/mart/oldold/old/StdMaybeMonad.icl create mode 100644 fp2/week2/mart/oldold/old/StdMonad.dcl create mode 100644 fp2/week2/mart/oldold/old/StdMonad.icl (limited to 'fp2/week2/mart/oldold') diff --git a/fp2/week2/mart/oldold/old/Map.dcl b/fp2/week2/mart/oldold/old/Map.dcl new file mode 100644 index 0000000..4848e1a --- /dev/null +++ b/fp2/week2/mart/oldold/old/Map.dcl @@ -0,0 +1,10 @@ +definition module Map + +//import BinTree +import StdMaybe + +class Map c :: (a -> b) (c a) -> c b + +instance Map [] +instance Map Maybe +//instance Map Tree diff --git a/fp2/week2/mart/oldold/old/Map.icl b/fp2/week2/mart/oldold/old/Map.icl new file mode 100644 index 0000000..d248c66 --- /dev/null +++ b/fp2/week2/mart/oldold/old/Map.icl @@ -0,0 +1,19 @@ +implementation module Map + +//import BinTree +import StdMaybe +import StdList + +class Map c :: (a -> b) (c a) -> c b + +instance Map [] where + Map f [] = [] + Map f [x:xs] = [f x: Map f xs] + +instance Map Maybe where + Map f Nothing = Nothing + Map f (Just x) = Just (f x) + +//instance Map Tree where +// Map f Leaf = Leaf +// Map f (Node x l r) = Node (f x) (mapTree f l) (mapTree f r) diff --git a/fp2/week2/mart/oldold/old/Mappen.icl b/fp2/week2/mart/oldold/old/Mappen.icl new file mode 100644 index 0000000..faca555 --- /dev/null +++ b/fp2/week2/mart/oldold/old/Mappen.icl @@ -0,0 +1,9 @@ +module Mappen + +import StdEnv +import Map + +Start = ( + Map ((+) 1) (Just 42), + Map ((+) 1) [1..10]) +//Map ((+) 1) t7) diff --git a/fp2/week2/mart/oldold/old/Random.dcl b/fp2/week2/mart/oldold/old/Random.dcl new file mode 100644 index 0000000..47a7c18 --- /dev/null +++ b/fp2/week2/mart/oldold/old/Random.dcl @@ -0,0 +1,19 @@ +definition module Random + + // Random number generator voor Linux gebruikers + // interface compatible met Random.dcl (helaas) + // -- mschool@science.ru.nl + +import StdFile + +:: RandomSeed + +// nullRandomSeed generates a fixed RandomSeed +nullRandomSeed :: RandomSeed + +// GetNewRandomSeed generates a good RandomSeed, using /dev/urandom +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env + +// Given a RandomSeed, Random generates a random number and a new RandomSeed. +random :: !RandomSeed -> .(!Int, !RandomSeed) + diff --git a/fp2/week2/mart/oldold/old/Random.icl b/fp2/week2/mart/oldold/old/Random.icl new file mode 100644 index 0000000..b6e0768 --- /dev/null +++ b/fp2/week2/mart/oldold/old/Random.icl @@ -0,0 +1,20 @@ +implementation module Random + +import StdFile, StdList, StdMisc, StdArray, Random + +:: RandomSeed :== Int + +nullRandomSeed :: RandomSeed +nullRandomSeed = 0 + +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env +getNewRandomSeed env +# (ok, src, env) = sfopen "/dev/urandom" FReadData env +| not ok => abort "could not open /dev/urandom" +# (bytes, src) = sfreads src 4 + seed = foldl (\x y->(x<<8)+toInt y) 0 [c \\ c<-:bytes] +| otherwise => (seed, env) + +random :: !RandomSeed -> .(!Int, !RandomSeed) +random seed = (seed>>16 bitand 0xFFFF, seed*0x08088405+1) + diff --git a/fp2/week2/mart/oldold/old/ReturnEnBind.icl b/fp2/week2/mart/oldold/old/ReturnEnBind.icl new file mode 100644 index 0000000..6c3f8e4 --- /dev/null +++ b/fp2/week2/mart/oldold/old/ReturnEnBind.icl @@ -0,0 +1,19 @@ +module ReturnEnBind + +import StdEnv, Random + +Start = (r1, r2, r3, r4, nullRandomSeed, s1, s2, s3) + where + (r1, s1) = som2 nullRandomSeed + (r2, s2) = som2 s1 + (r3, s3) = som2 s2 + (r4, _) = som2 s3 + +(bind1) infix 0 :: (St s a) (a -> (St s b)) -> St s b +(bind1) f g = uncurry g o f + +som2 :: (RandomSeed -> (Int,RandomSeed)) +som2 = (\s -> random s) bind1 (\a -> random (snd a)) + +seqList1 :: [St s a] -> St s [a] +seqList1 ... 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 -- cgit v1.2.3