[Haskell-beginners] type variables

Thomas haskell at phirho.com
Tue Aug 24 07:09:56 EDT 2010


Hello!

I have a question about type variables.
The following works:

type XMLCallback a = (XMLTreeNode -> a -> [Int] -> [XMLTreeNode] -> a)

xmlTreeFold :: XMLCallback a -> a -> Maybe XMLTreeNode -> Maybe a
xmlTreeFold _ _ Nothing = Nothing
xmlTreeFold func acc (Just tree) =
   Just (xmlTreeWalkerWithContext func acc tree [] [])

testFold :: XMLCallback [(XMLTreeNode, [Int], [XMLTreeNode])]
testFold node a is ns =
	if (length is) > 1 then ((node, is, ns):a) else a

=> xmlTreeFold testFold [] tree

But if I change the type declaration of 'testFold' to:
testFold :: XMLCallback a
it will not compile.

I thought that 'a' is a type /variable/ thus able to hold any type, for 
example, but not limited to '[(XMLTreeNode, [Int], [XMLTreeNode])]'. Why 
do I need to be that much more specific in the declaration for 'testFold'?
Especially since in the declaration of 'xmlTreeFold' the 'XMLCallback a' 
is well received.

Thanks for any insights,
Thomas






More information about the Beginners mailing list