Added semistandard libs
authorCamil Staps <info@camilstaps.nl>
Mon, 20 Apr 2015 20:17:14 +0000 (22:17 +0200)
committerCamil Staps <info@camilstaps.nl>
Mon, 20 Apr 2015 20:17:14 +0000 (22:17 +0200)
fp2/week1/camil/Random.dcl [new file with mode: 0644]
fp2/week1/camil/Random.icl [new file with mode: 0644]
fp2/week1/camil/StdMaybe.dcl [new file with mode: 0644]
fp2/week1/camil/StdMaybe.icl [new file with mode: 0644]

diff --git a/fp2/week1/camil/Random.dcl b/fp2/week1/camil/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/week1/camil/Random.icl b/fp2/week1/camil/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/week1/camil/StdMaybe.dcl b/fp2/week1/camil/StdMaybe.dcl
new file mode 100644 (file)
index 0000000..2403683
--- /dev/null
@@ -0,0 +1,41 @@
+definition module StdMaybe
+
+//     ********************************************************************************
+//     Clean StdLib library module, version 1.0
+//     ********************************************************************************
+
+from StdFunc import :: St;
+from StdOverloaded import class ==(..);
+
+::     Maybe x
+       =       Just x
+       |       Nothing
+
+isJust         :: !(Maybe .x) -> Bool          // case @1 of (Just _) -> True; _ -> False
+isNothing      :: !(Maybe .x) -> Bool          // not o isJust
+fromJust       :: !(Maybe .x) -> .x            // \(Just x) -> x
+
+// for possibly unique elements:
+u_isJust :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
+u_isNothing :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
+
+accMaybe :: .(St .x .a) !u:(Maybe .x) -> (!Maybe .a,!u:Maybe .x)
+// accMaybe f (Just x) = (Just (fst (f x)),Just (snd (f x)))
+// accMaybe f Nothing  = (Nothing,Nothing)
+
+mapMaybe       :: .(.x -> .y) !(Maybe .x) -> Maybe .y
+// mapMaybe f (Just x) = Just (f x)
+// mapMaybe f Nothing  = Nothing
+
+instance ==       (Maybe x) | == x
+//     Nothing==Nothing
+//     Just a ==Just b <= a==b
+
+maybeToList :: !(Maybe .a) -> [.a];
+//     returns list with no or one element
+
+listToMaybe :: ![.a] -> .Maybe .a;
+//     returns Just head of list if possible
+
+catMaybes :: ![Maybe .a] -> .[.a];
+//     catMaybes ms =  [ m \\ Just m <- ms ]
diff --git a/fp2/week1/camil/StdMaybe.icl b/fp2/week1/camil/StdMaybe.icl
new file mode 100644 (file)
index 0000000..4eed325
--- /dev/null
@@ -0,0 +1,65 @@
+implementation module StdMaybe
+
+//     ********************************************************************************
+//     Clean StdLib library module, version 1.0
+//     ********************************************************************************
+
+from StdFunc import :: St;
+from StdOverloaded import class ==(..);
+
+::     Maybe x
+       =       Just x
+       |       Nothing
+
+isJust :: !(Maybe .x) -> Bool
+isJust Nothing = False
+isJust _               = True
+
+isNothing :: !(Maybe .x) -> Bool
+isNothing Nothing      = True
+isNothing _            = False
+
+u_isJust :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
+u_isJust nothing=:Nothing
+       = (False, nothing)
+u_isJust just
+       = (True, just)
+
+u_isNothing :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
+u_isNothing nothing=:Nothing
+       = (True, nothing)
+u_isNothing just
+       = (False,just)
+
+fromJust :: !(Maybe .x) -> .x
+fromJust (Just x) = x
+
+accMaybe :: .(St .x .a) !u:(Maybe .x) -> (!Maybe .a,!u:Maybe .x)
+accMaybe f (Just x)
+       # (a,x) = f x
+       = (Just a,Just x)
+accMaybe _ nothing
+       = (Nothing,nothing)
+
+mapMaybe :: .(.x -> .y) !(Maybe .x) -> Maybe .y
+mapMaybe f (Just x) = Just (f x)
+mapMaybe _ nothing  = Nothing
+
+instance == (Maybe x) | == x where
+       (==) Nothing  maybe     = case maybe of
+                                                       Nothing -> True
+                                                       just    -> False
+       (==) (Just a) maybe     = case maybe of
+                                                       Just b  -> a==b
+                                                       nothing -> False
+
+maybeToList :: !(Maybe .a) -> [.a];
+maybeToList Nothing    =  []
+maybeToList (Just a)   =  [a]
+
+listToMaybe :: ![.a] -> .Maybe .a;
+listToMaybe []         =  Nothing
+listToMaybe [a:_]      =  Just a
+catMaybes :: ![Maybe .a] -> .[.a];
+catMaybes ms           =  [ m \\ Just m <- ms ]