A new feature for the next Open Quark release will be special support
for implementing record instance functions. Record instance function
are needed to make records an instance of a type class, for example Eq
{r}, Ord {r}, or Show {r}. Until now this has been quite hard to do,
often requiring primitive function support.
The Record module will have extensions that provide access to record
dictionaries. A record dictionary contains specialized instances of a
type class function for each element in a record type. Record
dictionaries can be applied to record instances using new functions
that create records or lists.
For example, the following shows how to obtain the Debug.show
dictionary for a compatible record:
showDict :: Show r => {r} -> Dictionary {r};
showDict r = Record.dictionary r "show";
The show function (a -> String) can be applied to record fields to
create a homogeneous list:
recordToListOfSrings :: Show r => {r} -> [String];
recordToListOfSrings r = unsafeBuildListFromRecord (Record.dictionary
r "show") r;
Instead of creating a list, it is also possible to create records. For
example:
addRecords :: Num r => {r} -> {r} -> {r};
addRecords r1 r2 =
unsafeBuildRecord (Record.dictionary r "add") (r1, r2);
At present the functions for building records and lists are prefixed
unsafe as they do not guarantee type safety, and therefore must be
used with special care. However, functions that are implemented using
them can guarantee complete type safety - this is the case for the
example functions, recordToListOfStrings and addRecords given above.
The new functionality is primarily intended for library creators, and
is very useful for implementing record instance functions without
recourse to primitive functions. All of the existing 8 record type
class instances in the standard platform could be implemented using
this approach. The updated Record_Tests module contains a number of
examples.
I hope this feature will be of some interest,
Magnus.