Haskell implementation of generic binary-tree code