[Haskell] Classes with no data type

Brian Hulley brianh at metamilk.com
Tue Oct 10 11:19:12 EDT 2006

Thomas Davie wrote:
> Hi,
>   I've met an interesting problem in terms of how to type a data
> structure and the functions that operate upon it.
> The problem centres around a single data type.  This data type can be
> constructed in multiple ways using different functions, depending on
> the options the user specifies.  That's all simple enough.  The
> problem really comes later on.  Depending on the function used
> generate the data structure I want to use different functions later
> on for example, to display the data.
> Thus I have a typical classes problem, in that I have several
> implementations of essentially the same function for different
> circumstances.  The problem is, they must all operate on the same
> data type, so I cannot define them as seperate instances.
> Anyone got any ideas how to type this?

You could add a field to the data type to store a record of closures to 
operate on it eg

    data T = T {_ops :: Ops, ...}

    data Ops = Ops{_print :: IO ()}

    print :: T -> IO ()
    print = _print . _ops

so the different smart constructors for T would fill in the Ops member as 
required - in other words you can make your own dictionary explicitly to get 
more flexibility instead of using the instance/class mechanism.

Regards, Brian.
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell mailing list