3 * Charlie Gerhardus, s3050009
4 * Mart Lubbers s4109503
10 Course I00032 Advanced Programming 2014
11 Skeleton for assignment 1
17 /**************** Prelude: *******************************/
19 :: Color = Red | Yellow | Blue
20 :: Tree a = Tip | Bin a (Tree a) (Tree a)
21 :: Rose a = Rose a [Rose a]
23 // Binary sums and products (in generic prelude)
25 :: PAIR a b = PAIR a b
26 :: EITHER a b = LEFT a | RIGHT b
28 // Generic type representations
29 :: RoseG a :== PAIR a [Rose a]
32 fromRose :: (Rose a) -> RoseG a
33 fromRose (Rose a l) = PAIR a l
37 :: Ordering = Smaller | Equal | Bigger
39 class (><) infix 4 a :: !a !a -> Ordering
41 instance >< Int where // Standard ordering for Int
47 instance >< Char where // Standard ordering for Char
53 instance >< String where // Standard lexicographical ordering
59 instance >< Bool where // False is smaller than True
60 (><) False True = Smaller
61 (><) True False = Bigger
64 /**************** End Prelude *************************/
66 /* compare ordering */
67 instance == Ordering where
68 (==) Equal Equal = True
69 (==) Smaller Smaller = True
70 (==) Bigger Bigger = True
73 /* color to rgb encoding
79 color2RGB :: Color -> Int
80 color2RGB Blue = 0x0000FF
81 color2RGB Red = 0xFF0000
82 color2RGB Yellow = 0xFFFF00
84 /* list operator instance */
85 instance >< [a] | >< a where
90 | ( (x >< y) == Equal ) = xs >< ys
94 instance >< (a, b) | >< a & >< b where
95 (><) (x1, y1) (x2, y2)
96 | ( (x1 >< x2) == Equal ) = y1 >< y2
97 | otherwise = x1 >< x2
99 /* color comparison */
100 instance >< Color where
101 (><) x y = color2RGB x >< color2RGB y
103 /* tree comparison */
104 instance >< (Tree a) | >< a where
108 (><) (Bin x xl xr) (Bin y yl yr)
109 | ( (x >< y) == Equal ) = (xl, xr) >< (yl, yr)
112 /* rose comparison */
113 instance >< (Rose a) | >< a where
114 (><) (Rose x xs) (Rose y ys) = (x, xs) >< (y , ys)
116 /* we take >-< as the generic ordering operator */
117 class (>-<) infix 4 a :: !a !a -> Ordering
119 /* instances for Int, Char, String and Bool
121 * instance >-< Int Char String Bool where
126 instance >-< Int where
128 instance >-< Char where
130 instance >-< String where
132 instance >-< Bool where
136 instance >-< UNIT where
137 (>-<) UNIT UNIT = Equal
140 instance >-< (PAIR a b) | >-< a & >-< b where
141 (>-<) (PAIR x1 x2) (PAIR y1 y2)
142 | ( (x1 >-< y1) == Equal ) = x2 >-< y2
143 | otherwise = x1 >-< y1
146 instance >-< (EITHER a b) | >-< a & >-< b where
147 (>-<) (LEFT x) (LEFT y) = x >-< y
148 (>-<) (RIGHT x) (RIGHT y) = x >-< y
149 (>-<) (LEFT _) (RIGHT _) = Bigger
150 (>-<) (RIGHT _) (LEFT _) = Smaller
152 /* generic representations */
153 :: ColorG :== EITHER (EITHER UNIT UNIT) UNIT
154 :: ListG a :== EITHER (PAIR a [a]) UNIT
155 :: TupleG a b :== PAIR a b
156 :: TreeG a :== EITHER ( PAIR a ( TupleG (Tree a) (Tree a) ) ) UNIT
158 /* convert color to generic representation */
159 fromColor :: Color -> ColorG
160 fromColor Yellow = LEFT (LEFT UNIT)
161 fromColor Red = LEFT (RIGHT UNIT)
162 fromColor Blue = RIGHT UNIT
164 /* convert list to generatic representation */
165 fromList :: [a] -> ListG a
166 fromList [] = RIGHT UNIT
167 fromList [x:xs] = LEFT (PAIR x xs)
169 /* convert tuple to generic representation */
170 fromTuple :: (a, b) -> TupleG a b
171 fromTuple (x, y) = PAIR x y
173 /* convert tree to generic representation */
174 fromTree :: (Tree a) -> TreeG a
175 fromTree Tip = RIGHT UNIT
176 fromTree (Bin x l r) = LEFT ( PAIR x ( fromTuple (l, r) ) )
178 /* generic conversion for >-< operator */
179 instance >-< Color where
180 (>-<) x y = fromColor x >-< fromColor y
182 instance >-< (a, b) | >-< a & >-< b where
183 (>-<) x y = fromTuple x >-< fromTuple y
185 instance >-< [a] | >-< a where
186 (>-<) x y = fromList x >-< fromList y
188 instance >-< (Rose a) | >-< a where
189 (>-<) x y = fromRose x >-< fromRose y
191 instance >-< (Tree a) | >-< a where
192 (>-<) x y = fromTree x >-< fromTree y
196 tree1 = Bin 1 (Bin 5 Tip Tip) (Bin 6 Tip Tip)
198 tree2 = Bin 1 (Bin 5 Tip Tip) (Bin 8 Tip Tip)
202 rose1 = Rose 2 [ Rose 1 [], Rose 2 [], Rose 5 [] ]
204 rose2 = Rose 2 [ Rose 1 [], Rose 2 [], Rose 8 [] ]
206 /* our two comparison lists */
208 cmp1 = [[1..3] >< [1..2], [1..2] >< [1..5], (1,2) >< (1,2), (1,3) >-< (1,2), Red >< Yellow, Yellow >< Blue, tree1 >< tree1, tree1 >< tree2, tree2 >< tree1, rose1 >< rose1, rose1 >< rose2, rose2 >< rose1]
210 cmp2 = [[1..3] >-< [1..2], [1..2] >-< [1..5], (1,2) >-< (1,2), (1,3) >-< (1,2), Red >-< Yellow, Yellow >-< Blue, tree1 >-< tree1, tree1 >-< tree2, tree2 >-< tree1, rose1 >-< rose1, rose1 >-< rose2, rose2 >-< rose1]
213 Start = ([cmp1, cmp2], cmp1 == cmp2)