Created
December 8, 2009 08:25
-
-
Save 1tgr/251520 to your computer and use it in GitHub Desktop.
Example code for http://stackoverflow.com/questions/1865351/ambiguous-type-variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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