[m-dev.] Type class instance constraints

Simon Taylor staylr at gmail.com
Tue May 22 14:43:35 AEST 2007


On 22-May-2007, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> We currently have a number of restrictions on what can appear in an
> instance declaration, namely that
> 
> - each parameter takes the form type_name(TypeVar1, TypeVar2, ...)
> - all TypeVars are distinct.
> 
> These restrictions are there to prevent ambiguity with overlapping
> instance declarations.  In practice we can only check for overlapping
> instance declarations in different modules at link-time.  Given that
> is the case, why can't we lift both restrictions and simply check for
> ambiguity at link time?  It would make the type class system much more
> useful.  Currently the first restriction means you have to define no-tag
> types all over the place and the second restriction prevents much useful
> polymorphism.

I think the problem is the amount of work needed to alter the RTTI system
throughout the compiler and runtime.  Removing the restrictions will cause
big changes in the way we extract type-infos from typeclass-infos at runtime,
although that doesn't always work properly now even with the restrictions.

Simon.


Estimated hours taken: 0.1

compiler/notes/todo.html:
	Add some wishlist items for the typeclass system.

Index: todo.html
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/notes/todo.html,v
retrieving revision 1.18
diff -u -u -r1.18 todo.html
--- todo.html	15 Sep 2006 09:11:22 -0000	1.18
+++ todo.html	22 May 2007 04:35:33 -0000
@@ -162,6 +162,21 @@
 
 <li> constructor classes
 
+<li> relax restrictions on instances, requires much work on the RTTI system.
+
+<li> equivalence typeclasses, for example:
+
+<pre>	:- typeclass bitmap_reader(Stream, State, Error) ==
+     		stream.bulk_reader(Stream, bit_index, bitmap, State, Error).
+</pre>
+
+<li> allow types to have class constraints, rather than requiring the
+     constraints on every predicate that uses the type, for example:
+
+<pre>	:- type read_buffer(Stream, State, Error)
+		<= bitmap_reader(Stream, State, Error).
+</pre>
+
 <li> allow a module exporting an abstract type to specify that other modules
      should not be allowed to test two values of that type for equality (similar
      to Ada's limited private types). This would be useful for e.g. sets
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the developers mailing list