{-# LANGUAGE DeriveDataTypeable #-}
module Ext2 (tests) where
import Test.HUnit
import Data.Generics
data List a = Nil | Cons a (List a) deriving (Data, Typeable, Show, Eq)
l1, l2 :: List Int
l1 = Cons 1 (Cons 2 Nil)
l2 = Cons 0 l1
data Pair a b = Pair1 a b | Pair2 a b deriving (Data, Typeable, Show, Eq)
p1, p2 :: Pair Int Char
p1 = Pair1 2 'p'
p2 = Pair2 3 'q'
s1 :: [Pair Int Char]
s1 = [p1, p2]
s2 :: (Pair Int Char, List Int)
s2 = (p2, l2)
unifyPair :: Pair a b -> Pair a b -> Bool
unifyPair (Pair1 _ _) (Pair1 _ _) = True
unifyPair (Pair2 _ _) (Pair2 _ _) = True
unifyPair _ _ = False
flipPair :: Pair a b -> Pair a b
flipPair (Pair1 a b) = Pair2 a b
flipPair (Pair2 a b) = Pair1 a b
t1 = everywhere (id `ext2T` flipPair) (s1,s2)
t2 = let f :: (Data a) => a -> Maybe a
f = (const Nothing) `ext2M` (Just . flipPair)
in (f p1, f l1)
t3 = everything (+) ( const 0
`ext1Q` (const 1 :: List a -> Int)
`ext2Q` (const 10 :: Pair a b -> Int))
$ s2
t4 = unifyPair (t4' :: Pair Int Char) t4' where
t4' :: Data a => a
t4' = undefined `ext1B` Nil `ext2B` (Pair1 undefined undefined)
tests = (t1, t2, t3, t4) ~=? output
output = ((map flipPair s1, (flipPair p2, l2))
,(Just (flipPair p1),Nothing)
,14
,True)