5753ddc42ad0a8f35a7ec91cb3364a3eddd329de
[ap2015.git] / a3 / mart / skeleton3b.icl
1 module skeleton3b
2
3 /*
4 Advanced Programming.
5 Skeleton for exercise 3.3 and 3.4.
6 To be used in a project with the environment Everything,
7 or StdEnv with an import of StdMaybe from StdLib
8
9 Pieter Koopman, pieter@cs.ru.nl
10 */
11
12 import StdEnv, StdGeneric, StdMaybe, GenEq
13
14 //------------------ show --------------
15 generic show_ a :: a [String] -> [String]
16
17 show_{|Int|} i c = [toString i:c]
18 show_{|Bool|} b c = [toString b:c]
19
20 show a = show_{|*|} a []
21
22 //------------------ parse --------------
23
24 :: Result a :== Maybe (a, [String])
25
26 generic parse a :: [String] -> Result a
27
28 parse{|Bool|} ["True" :r] = Just (True ,r)
29 parse{|Bool|} ["False":r] = Just (False,r)
30 parse{|Bool|} _ = Nothing
31
32 //------------------ some data types --------------
33
34 :: T = C
35 :: Color = Red | Yellow | Blue
36 :: Tree a = Tip | Bin a (Tree a) (Tree a)
37
38 //------------------ general useful --------------
39
40 instance + String where (+) s t = s+++t
41 derive bimap Maybe, []
42
43 //------------------ to test if parse and show work properly --------------
44
45 test :: t -> Bool | gEq{|*|}, show_{|*|}, parse{|*|} t
46 test x
47 = case parse{|*|} (show x) of
48 Just (y,[]) = x === y
49 _ = False
50
51 /**************** End Prelude, add all new code below this line *************************/
52
53 //------------------ tests --------------
54
55 Start = and [test b \\ b <- [False, True]]