Skip to content

Instantly share code, notes, and snippets.

@1tgr
Created December 8, 2009 08:25
Show Gist options
  • Select an option

  • Save 1tgr/251520 to your computer and use it in GitHub Desktop.

Select an option

Save 1tgr/251520 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data
import Data.Generics.PlateData
data Child1 a = Child1 a Int
deriving (Data, Show, Typeable)
data Child2 a = Child2 a String
deriving (Data, Show, Typeable)
data Child3 a = Child3 a
deriving (Data, Show, Typeable)
data Parent a = Leaf (Child1 a) (Child2 a) (Child3 a)
| Tree [ Parent a ]
deriving (Data, Show, Typeable)
modify :: (Parent Bool -> Parent Bool) -> String -> String
modify f s1 = show $ f tree
where tree = Tree [ Leaf (Child1 False 42) (Child2 False s1) (Child3 False),
Tree [ Leaf (Child1 True 0) (Child2 True "x") (Child3 True) ] ]
doInt :: Child1 l -> Child1 l
doInt (Child1 l n) = Child1 l (n + 1)
doString :: Child2 l -> Child2 l
doString (Child2 l (_:s)) = Child2 l ('j' : s)
replace :: Data l => Parent l -> Parent l
replace = transformBi doInt
. transformBi doString
main :: IO ()
main = putStrLn $ modify replace "hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment