instance show_ (a, b) | show_ a & show_ b where\r
show_ x c = show_ (fromTuple x) c\r
\r
-Start = [\r
- show [1, 2, 3],\r
- show (Bin Tip 5 Tip),\r
- show ([1, 2], 5) ]\r
-\r
///**************** Part 4 *******************************/\r
:: Result a = Fail | Match a [String]\r
class parse a :: [String] -> Result a\r
instance parse UNIT where\r
parse ["UNIT" : r] = Match UNIT r\r
parse _ = Fail\r
-instance parse (PAIR a b) where\r
-// parse ["PAIR":r] = Match r\r
+\r
+instance parse (PAIR a b) | parse a & parse b where\r
+ parse ["PAIR":r] = case parse r of\r
+ Match a rs = case parse rs of\r
+ Match a2 rs2 = Match (PAIR a a2) rs2\r
+ _ = Fail\r
+ _ = Fail\r
parse _ = Fail\r
-instance parse (EITHER a b) where\r
-// parse ["EITHER":r] = Match r\r
+\r
+instance parse (EITHER a b) | parse a & parse b where\r
+ parse ["LEFT":r] = case parse r of\r
+ Match a rs = Match (LEFT a) rs\r
+ _ = Fail\r
+ parse ["RIGHT":r] = case parse r of\r
+ Match a rs = Match (RIGHT a) rs\r
+ _ = Fail\r
+ parse _ = Fail\r
+\r
+instance parse (CONS a) | parse a where\r
+ parse ["CONS",s:r] = case parse r of\r
+ Match a rs = Match (CONS s a) rs\r
+ _ = Fail\r
parse _ = Fail\r
-instance parse (CONS a) where\r
-// parse ["CONS",s:r]\r
- parse_ = Fail\r
-\r
-instance parse (Tree a) | parse a where parse list = Fail // should be improved\r
-\r
-:: T = C\r
-\r
-///**************** Starts *******************************/\r
-//\r
-//Start = ("add your own Start rule!\n", Start4)\r
-//\r
-//// Possible tests:\r
-////Start1 :: ([String],Result T)\r
-////Start1 = (strings,parse strings) where strings = show C\r
-//\r
-////Start2 :: ([String],Result (Int,Bool))\r
-////Start2 = (strings,parse strings) where strings = show (1,False)\r
-//\r
-////Start3 :: ([String],Result [Int])\r
-////Start3 = (strings,parse strings) where strings = show l; l :: [Int]; l = [1..4]\r
-//\r
-//Start4 :: ([String],Result (Tree Int))\r
-//Start4 = (strings,parse strings)\r
-//where\r
-// strings = show t\r
-// \r
-// t :: Tree Int\r
-// t = Bin (Bin Tip 2 (Bin Tip 3 Tip)) 4 (Bin (Bin Tip 5 Tip) 6 Tip)\r
+\r
+toTree :: (TreeG a) -> Tree a\r
+toTree (LEFT (CONS _ UNIT)) = Tip\r
+toTree (RIGHT (CONS _ (PAIR a (PAIR l r)))) = Bin l a r\r
+\r
+instance parse (Tree a) | parse a where\r
+ parse x = case parse x of\r
+ Match a r = Match (toTree a) r\r
+ _ = Fail\r
+\r
+instance parse [a] | parse a where\r
+ parse x = case parse x of\r
+ Match a r = Match (toList a) r\r
+ _ = Fail\r
+\r
+toTuple :: (TupG a b) -> (a, b)\r
+toTuple (CONS "Tuple" (PAIR a b)) = (a, b)\r
+\r
+instance parse (a, b) | parse a & parse b where\r
+ parse x = case parse x of\r
+ Match a r = Match (toTuple a) r\r
+ _ = Fail\r