Declared Overloaded Record Fields (DORF)

Thumbnail Sketch

This proposal is addressing the narrow issue of namespacing for record field names by allowing more than one record in the same module to share a field name. Specifically each field name is overloaded so that:

Within the same module, many record types can be declared to share the field name.

The field name can be exported so that records in other modules can share it.

Furthermore, other modules can create records using that field name, and share it.

The export/import is under usual H98 namespace and module/qualification control, so that for the record type in an importing module:

Some fields are both readable and updatable;

Some are read-only;

Some are completely hidden.

In case of 'unintended' clash (another module using the same name 'by accident'), usual H98 controls apply to protect encapsulation and representation hiding.

This proposal introduces several new elements of syntax, all of which desugar to use well-established extensions of ghc. The approach has been prototyped in ghc v 7.2.1. In particular:

The field name overloading is implemented through usual class and instance mechanisms.

Field selectors are ordinary functions named for the field (but overloaded rather than H98's monomorphic), so field selection is regular function application. (There is no need for syntactically-based disambiguation at point of use.)

Implementation: the Has class, with methods get and set

Record declarations generate a Has instance for each record type/field combination. As well as type arguments for the record and field, there is a third argument for the field's resulting type. This is set at the instance level using equality constraints in a functional-dependencies style. Here is the Has class (r is the record, fld is the proxy type for the field, t is the field's type), with an example record declaration, its Has instance, and examples of use:

(Admittedly, this could get onerous to declare a fieldLabel for every field, even the ones that appear in a single record type. See "Option Three: Mixed In-situ and DeclaredORF: " further down this page for a suggestion of using the DORF mechanism to generate one-off H98-style fields.)

Virtual or pseudo- fields are easy to create and use, because field selection is merely function application. Virtual fields look like ordinary fields (but can't be updated, because there is no Has instance):

Parametric polymorphic fields can be applied in polymorphic contexts, and can be set including changing the type of the record.

Higher-ranked polymorphic fields can be applied in polymorphic contexts, but cannot be set -- for the same reasons as under SORF.The instances use equality constraints to 'improve' types up to polymorphic.

Has uses type family functions to manage type-changing update, which adds complexity -- see Implementer's view.

Multiple fields can be updated in a single expression (using familiar H98 syntax), but this desugars to nested updates, which is inefficient.

Pattern matching and record creation using the data constructor prefixed to { ... } work as per H98 (using DisambiguateRecordFields and friends).

Application Programmer's view

This proposal is addressing the "narrow issue" of namespacing for record field names.
Records

I'm avoiding giving implementation details here -- see:

The Implementer's view; and Comparison to SORF (links above)

I'm not saying anything about field selection via pattern matching or record construction using explicit data constructors -- those are to behave as currently (using the approach per ‑XDisambiguateRecordFields and friends).

Currently in Haskell two records in the same module can't share a field name. This is because declaring a field name within a data decl creates a selector function bound to that record type; and if it's single-record, we can only have one. I think the wiki is characterising the problem incorrectly:

it's not that the field name appearing in different record decls is ambiguous between the two record types
so we need some (syntactical) way of choosing between the different definitions;

rather, we have one field name, and we lack the syntax/semantics for sharing it between different records.

An example: let's say I have a database application with a field (meaning type) customer_id. Then it appears in records for name and address, pricing, order entry, etc. This is not a name 'clash', it's 'intended sharing'. (It really galls me to even put it that way for explanatory purposes. Really it's the samecustomer_id.)

In data model design you'd typically go about identifying all the fields (types aka attributes) and putting them in a data dictionary. Then you'd construct your records from them. You might (possibly) put the data dictionary in a distinct module, for easy maintenance. But you'd certainly want all the customer-related records in the same module. So a data decl:

data Customer_NameAddress = Cust_NA{ customer_id :: Int, ... }

is not declaring customer_id, it's using (or instancing) an already-declared field for customer_id.
Similarly, if I have a family of objects, all with a reset method, that's not umpteen methods with a 'clash' of names, it's one method with umpteen instances. (And I might create a family of record structures to describe each object, and store the reset method within it.)

What's more, the Haskell 98 field selector (auto-created from the data decl) is half-way to what we want. It's a function:

customer_id :: Customer_NameAddress -> Int

The DORF proposal generalises that signature: if you want to share a field across different records, its selector function needs to be overloaded to this type:

customer_id :: r{ customer_id :: Int } => r -> Int

The r{ ... } is syntactic sugar for the constraint meaning "record r has field customer_id at type Int".

We need a way to declare that a name is available as an overloadable field name (roughly speaking, a class/method definition), proposed syntax:

The field selector's result type -> Int means the field's domain (type) is Int -- it's just a type.
We might also want to constrain the record -- for example to be sure it is savable to persistent storage:

From here upwards, the r{ ... } constraint is just a constraint, and gets merged with other constraints. For example, you could define a function:

fullName r = (firstName r) ++ " " ++ (lastName r) -- per SPJ

The type inferred would be:

fullName :: r{ firstName, lastName :: String} => r -> String -- could declare this for yourself
-- note this is __not__ like a field label decl (Option Two)
-- because the function's name is different to the field(s)

And if you think that's very close to the type of a field selector function, you'd be right. Here's some more examples of field selection using pseudo- or 'virtual' fields, with dot notation:

customer.fullName
shape.area
date.dayOfWeek -- not a field: calculated from the date
name.middleInitial -- extract from the name field
tuple.fst -- Prelude functions
list.head
list.length

[Since they're just functions, they can use dot notation -- or not: personal preference.]

Modules and qualified names for records

Do these field selector functions have a special scope in some way? No! They're just functions. They can be exported/imported.

We can't stop some other developer creating an application/package with a field customer_id which is incompatible with ours. (Say a Sales Order entry application where customer_id is a String, to merge with our Accounts Receivable.) So do we have a problem if someone wants to import both?

No! This is regular business-as-usual familiar name clash, and it's what the module system is designed to handle. The field selectors are just functions, we can use them qualified:

then you are exporting the x field within record type T, but not the field selector x (nor the generated type 'peg' Proxy_x).

Type T and field label x are exported, but not data constructor MkT, so x is unusable. (It can't even be used to update an existing record using syntax: r{ x = 57 }, because that syntax now has a different semantics.)

... custAddr1.firstName ... -- ==> No instance for (Has Customer_Contact Proxy_firstName t0)
-- tried to use the local fieldLabel against an imported record type
... contact1.fullName ... -- ==> No instances for (Has Customer_Contact CUST.Proxy_firstName t0,
-- Has Customer_Contact CUST.Proxy_lastName t10)
-- arising from a use of `fullName'
-- tried to use an imported virtual field (used unqualified) against a local record type

because fullName is overloaded against the fieldLabel in module CUST, not the local module.

Normal type inference/instance resolution will find the record type for myPrice, and therefore the correct instance to apply the update.

You can update multiple fields at the same time:

myCustNA{ firstName = "Fred", lastName = "Dagg" }

[There's a poor story to tell here in implementation terms: we split into two calls to set, one nested inside the other. It's wasteful to build the intermediate record. Worse, the two fields' types might be parametric in the record type or polymorphically related (perhaps one is a method to apply to the other), then we get a type failure on the intermediate record.]

Note that where there is a genuine business-as-usual name clash you'd need qualified names in polymorphic update syntax, as currently:

someCust2 = someCust{ My.customer_id = 57, ... }

That is, there'd be no inference from the type of someCust to figure out which field label you're using. (That's because in general we can't infer the type of the expression prefixing the { ... } update.)

Some discussion threads have argued that Haskell's current record update syntax is awkward. The DORF proposal is to implement field update using a polymorphic function. Once this is implemented, alternative syntax could be explored, providing it desugars to a call to set.

Posted 18-Feb-2012, Anthony Clayden. [Apologies for my wiki formatting and cross-linking -- in haste! and a novice to trac.]