module skeleton1\r
-\r
-/*\r
- Course I00032 Advanced Programming 2014\r
- Skeleton for assignment 1\r
- Pieter Koopman\r
-*/\r
+//Charlie Gerhardus (s000000)\r
+//Mart Lubbers (s4109503)\r
\r
import StdEnv\r
\r
:: Rose a = Rose a [Rose a]\r
\r
// Binary sums and products (in generic prelude)\r
-:: UNIT = UNIT\r
+:: UNIT = UNIT\r
:: PAIR a b = PAIR a b\r
:: EITHER a b = LEFT a | RIGHT b\r
\r
:: RoseG a :== PAIR a [Rose a]\r
\r
// Conversions\r
-fromRose :: (Rose a) -> RoseG a\r
-fromRose (Rose a l) = PAIR a l\r
-\r
-// Oerdering\r
+fromRose :: (Rose a) -> RoseG a\r
+fromRose (Rose a l)= PAIR a l\r
\r
-:: Ordering = Smaller | Equal | Bigger\r
+// Ordering\r
+:: Ordering = Smaller | Equal | Bigger\r
\r
class (><) infix 4 a :: !a !a -> Ordering\r
\r
(><) _ _ = Equal\r
\r
/**************** End Prelude *************************/\r
+// 1. Ordering by overloading\r
+instance >< Color where\r
+ (><) Red Red = Equal\r
+ (><) Red _ = Bigger\r
+ (><) Yellow Yellow = Equal\r
+ (><) Yellow Red = Smaller\r
+ (><) Yellow Blue = Bigger\r
+ (><) Blue Blue = Equal\r
+ (><) _ _ = Smaller\r
+\r
+instance >< (Tree a) | >< a & == a where\r
+ (><) Tip Tip = Equal\r
+ (><) (Bin _ _ _) Tip = Bigger\r
+ (><) Tip (Bin _ _ _) = Smaller\r
+ (><) (Bin x ltx rtx) (Bin y lty rty)\r
+ | x == y = case ltx >< lty of\r
+ Equal = rtx >< rty\r
+ otherwise = ltx >< lty\r
+ | otherwise = x >< y\r
+\r
+instance >< (Rose a) | >< a & == a where\r
+ (><) (Rose _ _) (Rose _ []) = Bigger\r
+ (><) (Rose _ []) (Rose _ _) = Smaller\r
+ (><) (Rose x xs) (Rose y ys)\r
+ | x == y = xs >< ys\r
+ | otherwise = x >< y\r
+\r
+instance >< [a] | Ord a & == a where\r
+ (><) [] [] = Equal\r
+ (><) [] _ = Smaller\r
+ (><) _ [] = Bigger\r
+ (><) [x:xs] [y:ys]\r
+ | x == y = xs >< ys\r
+ | x < y = Smaller\r
+ | otherwise = Bigger\r
+\r
+instance >< (a, b) | >< a & >< b & == a where\r
+ (><) (xa, xb) (ya, yb)\r
+ | xa == ya = xb >< yb\r
+ | otherwise = xa >< ya\r
+\r
+//2. Generic representation\r
+//2.1\r
+:: ColorG :== EITHER UNIT (EITHER UNIT UNIT)\r
+:: ListG a :== EITHER UNIT (PAIR a [a])\r
+\r
+//2.2\r
+listToGen :: [a] -> ListG a\r
+listToGen [] = LEFT UNIT\r
+listToGen [x:xs] = RIGHT (PAIR x xs)\r
+\r
+//2.3. EITHER (PAIR 1 (PAIR 2 3)) UNIT \r
+// Nope, it will leave the xs to be so it will be: EITHER (PAIR 1 [2,3])\r
+//2.4. Nope, not possible since they are basic types and they can't be\r
+// converted to UNIT, EITHER or PAIR.\r
+\r
+//3. Ordering via generic representation\r
+instance >< UNIT where\r
+ (><) _ _ = Equal\r
+\r
+instance >< (PAIR a b) | >< a & >< b\r
+ (><) (PAIR x1 y1) (PAIR x2 y2) = case x1 >< x2 of\r
+ Equal = y1 >< y2\r
+ otherwise = x1 >< x2\r
+\r
+instance >< (EITHER a b) | >< a & >< b\r
+ (><) (LEFT _) (RIGHT _) = Bigger\r
+ (><) (RIGHT _) (LEFT _) = Smaller\r
+ (><) (RIGHT x) (RIGHT y) = x >< y\r
+ (><) (LEFT x) (LEFT y) = x >< y\r