[Haskell] extensible records using associated types

Barney Hilken b.hilken at ntlworld.com
Sun Jun 25 15:17:36 EDT 2006


Sorry if people are really bored with this thread, but I thought of  
another advantage of this approach.

Trex (and similar systems) have a limitation that stops you using  
them to define functions with default values. In systems like R (the  
stats package) most functions have a huge number of named parameters  
which usually take default values, but can be changed if necessary.  
In order to define such functions, what you want is an operation  
'update r s' which takes the value of the record 's' (the non-default  
values) for each field which 's' has, and the value of 'r' (the  
default values) for all other fields. The type of 's' should be such  
that it can only have fields which 'r' has, and each such field must  
have the same type, but it doesn't need all the fields of 'r'. The  
type of the result is the same as the type of 'r'.


This can't be typed in Hugs because you can only update particular  
fields, but we can easily add it to (at least the quadratic version  
of) AT-records.

Introduce a class 'subRecord r s' which means that every field of 's'  
is a field of 'r' with the same type:

 >	class subRecord r s where
 >		update :: r -> s -> r

The type 'Empty' is a subRecord of everything, and updating by it  
changes nothing:

 >	instance subRecord r Empty where
 >		update t Empty = t

For each label 'N' define:

 >	instance subRecord r s => subRecord (N a r) (N a s) where
 >		update (N x t) (N y u) = N y (update t u)

For each previous label 'M' define:

 >	instance subRecord r (M b s) => subRecord (N a r) (M b s) where
 >		update (N x t) (M y u) = N x (update t (M y u))


There are probably other useful additions to the records system which  
could be coded up in a similar way. In fact, the class 'Disjoint' of  
my original proposal already goes beyond hugs. Until it is clear what  
is useful and what is not, a system like this which requires little  
compiler support has the great advantage that it is easy to change.

Barney.



More information about the Haskell mailing list