started with week2
authorMart Lubbers <mart@martlubbers.net>
Thu, 23 Apr 2015 13:10:52 +0000 (15:10 +0200)
committerMart Lubbers <mart@martlubbers.net>
Thu, 23 Apr 2015 13:10:52 +0000 (15:10 +0200)
fp2/week2/mart/Map.dcl [new file with mode: 0644]
fp2/week2/mart/Map.icl [new file with mode: 0644]
fp2/week2/mart/Mappen.icl [new file with mode: 0644]
fp2/week2/mart/Random.dcl [new file with mode: 0644]
fp2/week2/mart/Random.icl [new file with mode: 0644]
fp2/week2/mart/ReturnEnBind.icl [new file with mode: 0644]
fp2/week2/mart/StdIOMonad.icl [new file with mode: 0644]
fp2/week2/mart/StdMonad.dcl [new file with mode: 0644]
fp2/week2/mart/StdMonad.icl [new file with mode: 0644]

diff --git a/fp2/week2/mart/Map.dcl b/fp2/week2/mart/Map.dcl
new file mode 100644 (file)
index 0000000..4848e1a
--- /dev/null
@@ -0,0 +1,10 @@
+definition module Map\r
+\r
+//import BinTree\r
+import StdMaybe\r
+\r
+class Map c :: (a -> b) (c a) -> c b\r
+\r
+instance Map []\r
+instance Map Maybe\r
+//instance Map Tree\r
diff --git a/fp2/week2/mart/Map.icl b/fp2/week2/mart/Map.icl
new file mode 100644 (file)
index 0000000..d248c66
--- /dev/null
@@ -0,0 +1,19 @@
+implementation module Map\r
+\r
+//import BinTree\r
+import StdMaybe\r
+import StdList\r
+\r
+class Map c :: (a -> b) (c a) -> c b\r
+\r
+instance Map [] where \r
+       Map f [] = []\r
+       Map f [x:xs] = [f x: Map f xs]\r
+\r
+instance Map Maybe where\r
+       Map f Nothing = Nothing\r
+       Map f (Just x) = Just (f x)\r
+\r
+//instance Map Tree  where\r
+//     Map f Leaf = Leaf\r
+//     Map f (Node x l r) = Node (f x) (mapTree f l) (mapTree f r)\r
diff --git a/fp2/week2/mart/Mappen.icl b/fp2/week2/mart/Mappen.icl
new file mode 100644 (file)
index 0000000..faca555
--- /dev/null
@@ -0,0 +1,9 @@
+module Mappen\r
+\r
+import StdEnv\r
+import Map\r
+\r
+Start = (\r
+       Map ((+) 1) (Just 42), \r
+       Map ((+) 1) [1..10])\r
+//Map ((+) 1) t7)\r
diff --git a/fp2/week2/mart/Random.dcl b/fp2/week2/mart/Random.dcl
new file mode 100644 (file)
index 0000000..47a7c18
--- /dev/null
@@ -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/Random.icl b/fp2/week2/mart/Random.icl
new file mode 100644 (file)
index 0000000..b6e0768
--- /dev/null
@@ -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/ReturnEnBind.icl b/fp2/week2/mart/ReturnEnBind.icl
new file mode 100644 (file)
index 0000000..6c3f8e4
--- /dev/null
@@ -0,0 +1,19 @@
+module ReturnEnBind\r
+\r
+import StdEnv, Random\r
+\r
+Start = (r1, r2, r3, r4, nullRandomSeed, s1, s2, s3)\r
+       where\r
+               (r1, s1) = som2 nullRandomSeed\r
+               (r2, s2) = som2 s1\r
+               (r3, s3) = som2 s2\r
+               (r4, _) = som2 s3\r
+\r
+(bind1) infix 0 :: (St s a) (a -> (St s b)) -> St s b\r
+(bind1) f g = uncurry g o f\r
+\r
+som2 :: (RandomSeed -> (Int,RandomSeed))\r
+som2 = (\s -> random s) bind1 (\a -> random (snd a))\r
+\r
+seqList1 :: [St s a] -> St s [a]\r
+seqList1 ...\r
diff --git a/fp2/week2/mart/StdIOMonad.icl b/fp2/week2/mart/StdIOMonad.icl
new file mode 100644 (file)
index 0000000..7f112a7
--- /dev/null
@@ -0,0 +1,22 @@
+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 
diff --git a/fp2/week2/mart/StdMonad.dcl b/fp2/week2/mart/StdMonad.dcl
new file mode 100644 (file)
index 0000000..cd1c654
--- /dev/null
@@ -0,0 +1,8 @@
+definition module StdMonad\r
+\r
+class return        c :: a -> c a\r
+class (>>=) infix 0    c :: (c a) (a -> c b) -> c b\r
+class fail          c :: c a\r
+\r
+class Monad            c | return, >>= c\r
+class MonadFail            c | Monad, fail c\r
diff --git a/fp2/week2/mart/StdMonad.icl b/fp2/week2/mart/StdMonad.icl
new file mode 100644 (file)
index 0000000..db193ab
--- /dev/null
@@ -0,0 +1 @@
+implementation module StdMonad\r