[Haskell-cafe] RE: generics question 2

Ralf Lammel Ralf.Lammel at microsoft.com
Mon Apr 3 20:41:55 EDT 2006


> Hi Ralf,
> 
> I'm looking for a function like extT but with more general type:
> 
> (t a -> s a) -> (t b -> s b) -> (t a -> s a)
> 
> Is there such a thing in the generics library?

Hi Frederik,

Not sure how you are exactly going to use such an operation ...
But here is its implementation anyhow.
Thanks for the riddle.

Ralf

import Data.Generics

-- Frederik's weird ext operation :-)
ext' :: (Data (t a), Data (s a), Data (t b), Data (s b))
     => (t a -> s a) -> (t b -> s b) -> (t a -> s a)
ext' f g ta = case cast g of
               Just g' -> g' ta
               Nothing -> f ta

-- A generic default
f (Just x) = [x]
f Nothing  = []

-- A type-specific case
g (Just True)  = [True]
g (Just False) = []
g Nothing      = []

-- A composition using our new type-extension operator
test :: Data a => Maybe a -> [a]
test = ext' f g

-- Let's see whether it works ...
main = do 
          print $ test (Just (1::Int))
          print $ test (Just False)




More information about the Haskell-Cafe mailing list