Use the typeclass parameter tfor the type constructor (for example, BTreeor TTree, and unlike BTree aand TTree a):
class Tree t where
getName :: t a -> a
instance Tree BTree where
getName (BLeaf name) = name
getName (BBranch name lhs rhs) = name
If you want instances to change depending on the type of element a, you need multi-parameter classes:
{-# LANGUAGE MultiParamTypeClasses #-}
class Tree t a where
getName :: t a -> a
instance Tree BTree Int where
getName (BLeaf name) = name+1
getName (BBranch name lhs rhs) = name*2
instance Tree BTree Char where
getName (BLeaf name) = name
getName (BBranch name lhs rhs) = name
You probably don't need to make this so general.
source
share