{-# OPTIONS -fglasgow-exts #-}
module HOPat (tests) where
import Test.HUnit
import Data.Generics
data T1 = T1a Int | T1b Float
deriving (Show, Eq, Typeable, Data)
data T2 = T2a T1 T2 | T2b
deriving (Show, Eq, Typeable, Data)
elim' :: (Data y, Data x) => Constr -> y -> Maybe x
elim' c y = if toConstr y == c
then unwrap y
else Nothing
unwrap :: (Data y, Data x) => y -> Maybe x
unwrap y = case gmapQ (Nothing `mkQ` Just) y of
[Just x] -> Just x
_ -> Nothing
elim :: forall x y. (Data y, Data x) => (x -> y) -> y -> Maybe x
elim c y = elim' (toConstr (c (undefined::x))) y
visitor :: (Data x, Data y, Data z)
=> (x -> y) -> (x -> x) -> z -> z
visitor c f = everywhere (mkT g)
where
g y = case elim c y of
Just x -> c (f x)
Nothing -> y
tests = ( ( elim' (toConstr t1a) t1a) :: Maybe Int
, ( (elim' (toConstr t1a) t1b) :: Maybe Int
, ( (elim T1a t1a) :: Maybe Int
, ( (elim T1a t1b) :: Maybe Int
, ( (visitor T1a ((+) 46) t2) :: T2
))))) ~=? output
where
t1a = T1a 42
t1b = T1b 3.14
t2 = T2a t1a (T2a t1a T2b)
output = (Just 42,(Nothing,(Just 42,(Nothing,T2a (T1a 88) (T2a (T1a 88) T2b)))))