Added semistandard libs
[fp1415.git] / fp2 / week1 / camil / StdMaybe.icl
1 implementation module StdMaybe
2
3 // ********************************************************************************
4 // Clean StdLib library module, version 1.0
5 // ********************************************************************************
6
7 from StdFunc import :: St;
8 from StdOverloaded import class ==(..);
9
10 :: Maybe x
11 = Just x
12 | Nothing
13
14 isJust :: !(Maybe .x) -> Bool
15 isJust Nothing = False
16 isJust _ = True
17
18 isNothing :: !(Maybe .x) -> Bool
19 isNothing Nothing = True
20 isNothing _ = False
21
22 u_isJust :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
23 u_isJust nothing=:Nothing
24 = (False, nothing)
25 u_isJust just
26 = (True, just)
27
28 u_isNothing :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
29 u_isNothing nothing=:Nothing
30 = (True, nothing)
31 u_isNothing just
32 = (False,just)
33
34 fromJust :: !(Maybe .x) -> .x
35 fromJust (Just x) = x
36
37 accMaybe :: .(St .x .a) !u:(Maybe .x) -> (!Maybe .a,!u:Maybe .x)
38 accMaybe f (Just x)
39 # (a,x) = f x
40 = (Just a,Just x)
41 accMaybe _ nothing
42 = (Nothing,nothing)
43
44 mapMaybe :: .(.x -> .y) !(Maybe .x) -> Maybe .y
45 mapMaybe f (Just x) = Just (f x)
46 mapMaybe _ nothing = Nothing
47
48 instance == (Maybe x) | == x where
49 (==) Nothing maybe = case maybe of
50 Nothing -> True
51 just -> False
52 (==) (Just a) maybe = case maybe of
53 Just b -> a==b
54 nothing -> False
55
56 maybeToList :: !(Maybe .a) -> [.a];
57 maybeToList Nothing = []
58 maybeToList (Just a) = [a]
59
60 listToMaybe :: ![.a] -> .Maybe .a;
61 listToMaybe [] = Nothing
62 listToMaybe [a:_] = Just a
63
64 catMaybes :: ![Maybe .a] -> .[.a];
65 catMaybes ms = [ m \\ Just m <- ms ]