overlapping instances and constraints

Niklas Broberg niklas.broberg at gmail.com
Tue Feb 28 17:16:56 EST 2006


On 2/28/06, Ben Rudiak-Gould <Benjamin.Rudiak-Gould at cl.cam.ac.uk> wrote:
> Simon Peyton-Jones wrote:
> > - A program that type checks can have its meaning changed by adding an
> > instance declaration
> >
> > - Similarly adding "import M()" can change the meaning of a program (by
> > changing which instances are visible
> >
> > - Haskell would need to be a lot more specific about exactly where
> > context reduction takes place.
>
> I think all of these problems would go away if overlap was permitted within
> a module but forbidden across modules. Are there uses of overlapping
> instances for which this isn't flexible enough?

Certainly! In HSP [1] there is a class (simplified here)

class IsXML xml where
  toXML :: xml -> XML

data XML = Element .... | CDATA String

that deals with how things should be represented as XML. There are a
number of basic instances for this, such as

instance IsXML String where
 toXML = CDATA

instance (Show a) => IsXML a where
 toXML = toXML . show

The intention of the latter is to be a default instance unless another
instance is specified. These instances can be found in the base HSP
module, but the idea is that HSP users should be able to work with
their own datatypes and only need to define the translation into XML
via instanciating IsXML. This would have to be done in the user
modules, so overlap across module boundaries are essential for this to
work. :-)

/Niklas

[1] http://www.cs.chalmers.se/~d00nibro/hsp/


More information about the Haskell-prime mailing list