[Haskell-cafe] Proposal: deriving ShallowEq?

Stefan Holdermans stefan at cs.uu.nl
Tue Jul 19 03:19:11 EDT 2005


Ben,

> I often find it useful to determine whether two objects are using the 
> same constructor, without worrying about the constructors' arguments.

In Generic Haskell, you can define shallowEq, well ;), generically:

   shallowEq {| a :: * |} :: (shallowEq {| a |}) => a -> a -> Bool

   shallowEq {| Unit |} Unit Unit               = True
   shallowEq {| Sum a b |} (Inl _) (Inl _)      = True
   shallowEq {| Sum a b |} (Inr _) (Inr _)      = True
   shallowEq {| Sum a b |} _ _                  = False
   shallowEq {| Prod a b |} (_ :*: _) (_ :*: _) = True
   shallowEq {| Int |} n1 n2                    = n1 == n2
   shallowEq {| Char |} c1 c2                   = c1 == c2

There are some more lightweight variations of this style of programming 
that can be embedded in Haskell, but they require some additional 
effort per data type.

I'm not sure how this can be done with the Scrap Your Boilerplate 
approach, i.e., I have not give it too much thought yet, but I'm sure 
something can be done there too.

Regards,

Stefan



More information about the Haskell-Cafe mailing list