[Haskell-cafe] A Foldable binary search tree

Brad Larsen brad.larsen at gmail.com
Sat Dec 22 23:15:57 EST 2007


On Sat, 22 Dec 2007 22:41:37 -0500, Neil Mitchell <ndmitchell at gmail.com>  
wrote:

> Hi
>
>> data (Ord a) => BST a = Empty | BST (BST a) a (BST a)
>
> Experience has taught me to _never_ put class contexts on data
> definitions. Now you can't write something as simple as "Empty" - you
> have to give it a class context. This is just plain annoying.

With the class context in the BST definition, ghc gives no complaints when  
I evaluate "Empty":

*BST> Empty
Empty
*BST> :t Empty
Empty :: BST a

I assume I misunderstand you.

> I would accept this pain if it meant I could write:
>
> insert :: a -> BST a -> BST a
>
> and have the Ord silently inserted - but you can't. You still have to
> write the Ord a => explicitly.

I see what you mean here.  To get ghc to accept my code (where BST is  
Foldable), I omit the class context from the data definition, but add the  
Ord class context to the signatures of the BST functions that require it  
(insert, for example).

> As a result, I see no advantage to adding the class constraint, and
> plenty of disadvantages. If there are some advantages to this context
> I would be interested to know what they are.
>
> Thanks
>
> Neil

Thanks!

Brad



More information about the Haskell-Cafe mailing list