Version 29 (modified by claus, 9 years ago) (diff)

pulling out of this discussion. removed my comments/questions

Extensible Records

There seems to be widespread agreement that the current situation with regards to records is unacceptable, but the official GHC policy is that there are too many good ideas to choose from - so nothing gets done!

The purpose of this page is to collect and discuss proposals for adding extensible records to GHC. Ticketed at #1872.



Purely for the sake of argument on this page, I propose the following syntax. Feel free to change or extend this if you can think of something better. Many of these conflict with existing Haskell operators, so can't be used in any concrete proposal.

  • {L1 = v1, L2 = v2, ...} the constant record with field labels L1, L2, ... and corresponding values v1, v2, ...
  • {L1 :: t1, L2 :: t2, ...} the type of the constant record with field labels L1, L2, ... and corresponding values of types t1, t2, ...
  • r . L the value of the field labelled L in record r
  • t ::. L the type of the field labelled L in record type t
  • r - L the record r with field L deleted
  • t ::- L the record type t with field L deleted
  • r + s record r extended by adding all the fields of s. Many systems restrict to the case where s has constant shape.
  • t ::+ u record type t extended by adding all the fields of type u. Many systems restrict to the case where u has constant shape.
  • r <- s record r updated by replacing the all fields of s. Many type systems restrict to the case where s has constant shape.

By constant shape we mean that the field names of a record are given literally, though the values and types of the fields could be variables.

Constant Record Types

An important difference between the various proposals is what constitutes a valid record, and similarly a valid record type. The key points are:

  • Permutativity:: Are {X :: Int, Y :: Int} and {Y :: Int, X :: Int} the same type? The Poor Man's Records system distinguishes these two, which makes implementation much simpler, but means that any function which accepts permuted records must be polymorphic.
  • Repeated Fields:: Is {X :: Int, X :: Int} a valid record type? Both Poor Man's Records and Scoped Labels allow this type, but other systems consider this an error.
Ok, opinion (since you asked for it!)
The problem with scoping is that, in most cases, repeated fields are a programmer error, and the point of types is to catch such errors at compile time. At first sight, the ability to scope fields in this way looks like extra power to the programmer, but is this actually useful? I've seen no convincing examples where scoping allows a more clearly structured program, whereas there are plenty of cases where it will mean an error goes uncaught. If you have a good example of scoping, please add it to the examples section on this page.
The problem with unpermuted records is illustrated in the example at the bottom of this page: it forces you to make functions polymorphic when they "ought" to be monomorphic. This means that no-one can use records in a straightforward way without understanding the details of all the predicates.
In both cases, the usual approach (permutation, no repeats) is what most programmers expect. We have to remember that this proposal is supposed to be the records system for Haskell. It must be the right system for simple problems as well as complex ones.
I totally disagree. More operations aren't always better! What the language needs is a records system which allows and encourages you to structure your code in the clearest possible way. In my opinion, choosing a type system which behaves as expected is an important part of that, but I would quickly be convinced otherwise by some examples which show that these extra features help to write better code. Personal comments, on the other hand, don't help.
The way forward (as I see it) is to implement all the various possible designs as libraries (with the necessary compiler support) and then see which library is best in practice.

Label Namespace

The proposals which are implemented as libraries put labels in conid (at the value level) and tycon (at the type level). In other words they must begin with capital letters, not clash with any other constructor or type, and be declared before use. If we want to support labels as first-class objects, this is essential so that we can distinguish them from other objects.

The other proposals allow labels to be arbitrary strings, and distinguish them from other objects by context.

This is related to the problem of Label Sharing: if the label L is declared in two different modules M1 and M2, both of which are imported, do we have one label L or two labels M1.L and M2.L? Should there be a mechanism for identifying labels on import?

The Heterogeneous Collections system introduces user defined Label Namespaces. All the labels in a record must live in the same namespace, and within a namespace, labels are defined in a way which forces a linear ordering. This gives a kind of modularity which interacts with the module system in a complex way: is not clear (for example) what happens when two imported modules extend a namespace independently.

Type Systems

The most important difference between the various record proposals seems to be the expressive power of their type systems. Most systems depend on special predicates in the type context to govern the possible forms of record polymorphism. As usual there is a trade-off between power and simplicity.

No predicates
You can get away without any predicates if you are prepared to allow records to have multiple fields with the same label. In the scoped labels system, the syntactic form of the type matches that of the record, so you can type the operators by
  • (. L) :: a ::+ {L :: b} -> b
  • (- L) :: a ::+ {L :: b} -> a
  • (+ {L = v}) :: a -> a ::+ {L :: b}
  • (<- {L = v}) :: a ::+ {L :: b} -> a ::+ {L :: b}
Positive predicates
If you allow only positive information about records in the context, you can only type some of the operators. In A proposal for records in Haskell the condition a <: {L1::t1, L2 :: t2, ...} means that a is a record type with at least the fields L1, L2, ... of types t1, t2, ... respectively. You can type the following operators:
  • (. L) :: a <: {L :: b} => a -> b
  • (<- {L = v}) :: a <: {L :: b} => a -> a
Lacks predicates
In order to type the other operators, we need negative information about records. In the Hugs Trex system the condition a \ L means that a is a record type without a field L. You can type all the operators if you restrict the right-hand sides to constant shape records:
  • (. L) :: a \ L => a ::+ {L :: b} -> b
  • (- L) :: a \ L => a ::+ {L :: b} -> a
  • (+ {L = v}) :: a \ L => a -> a ::+ {L :: b}
  • (<- {L = v}) :: a \ L => a ::+ {L :: b} -> a ::+ {L :: b}
General predicates, using type families
The Type Families system uses three predicates: a `Contains` L means that a is a record type with a field labelled L; a `Disjoint` b means that a and b are record types with no fields in common; and a `Subrecord` b means that a and b are record types, and every field of a also occurs in b (with the same type). You can type all the operators:
  • (. L) :: a `Contains` L => a -> a ::. L
  • (- L) :: a `Contains` L => a -> a ::- L
  • (+) :: a `Disjoint` b => a -> b -> a ::+ b
  • (<-) :: a `Subrecord` b => b -> a -> b
General predicates, using functional dependencies
The Heterogeneous Collections and Poor Man's Records systems achieve the same as the Type Families system, using the relational style of functional dependencies. In Poor Man's Records, Select L a b means that a is a record type with a field labelled L, and b = a ::. L; Remove L a b means that a is a record type with a field labelled L, and b = a ::- L; and Concat a b c means that a and b are record types with no fields in common, and c = a ::+ b. You can type the operators:
  • (. L) :: Select L a b => a -> b
  • (- L) :: Remove L a b => a -> b
  • (+) :: Concat a b c => a -> b -> c

The Subrecord predicate and <- operator could easily be added. The difference between Heterogeneous Collections and Poor Man's Records is that Poor Man's Records makes no attempt to sort labels or remove duplicates, so for example {X = 3, Y = 4} and {Y = 4, X = 3} have different types, so are certainly not equal (the updated version of November 2007 supports record projection and permutation, among most other operations).

Implementation and Language support

As it seems possible to implement most of the functionality in a library, there might be no need for a complex extensible records feature. Nevertheless, there are issues which are common to most proposals and which would best be addressed at the language and implementation level:

  • type sharing: not specific to records, but crucial for record programming practice. If multiple modules introduce the "same" labels, means are needed to specify the equivalence of these types (cf Haskell prime ticket 92).
  • partial evaluation of type class programs: to achieve constant time record field access. Again, this feature is not specific to records, but crucial for record programming practice.
  • portability: it would be nice if extensible records libraries were portable over multiple Haskell implementations. That not only means that these implementations need to support the same features, but that they need to interpret these features in the same way (this is currently not the case for the interaction of functional dependencies and type class overlap resolution in GHC and Hugs).
  • permutativity: The easiest way to implement permutativity of field labels is to sort them by some total ordering. Although this can be implemented using functional dependencies, it's complex and inefficient. Compiler support for a global order on tycons (based on fully qualified name, perhaps) would be very helpful. I have submitted a feature request #1894. Does this conflict with type sharing?


Please put examples here, if possible using the above notation. The aim is to find out which features of the various systems are important in practice, so uncontrived examples which illustrate differences between the systems are wanted!

An example to show the need for extra polymorphism in unpermuted records:

type Point = {X :: Float, Y :: Float}

norm :: Point -> Float
norm p = sqrt (p.X * p.X + p.Y * p.Y)

norm {Y = 3.0, X = 4.0}    -- this is a type error, because X and Y are in the wrong order

norm' :: (Select X a Float, Select Y a Float) => a -> Float
norm' p = sqrt (p.X * p.X + p.Y * p.Y)

norm' {Y = 3.0, X = 4.0}    -- this is OK, because norm' is polymorphic

Here is the example translated into Data.Record, and slightly expanded to demonstrate the flexibility with which one may specify precisely as much detail as wanted, without requiring a global label ordering (if that is wanted, we simply permute to the locally expected order). norm1 has the type asked for above (no extra fields, specific order for expected fields), norm2 has the most flexible type (only expected fields are specified), norm3 shows a middle ground (no extra fields permitted, but order is irrelevant):

data X = X deriving Show; instance Label X where label = X
data Y = Y deriving Show; instance Label Y where label = Y
data Z = Z deriving Show; instance Label Z where label = Z

type Point = (X := Float) :# (Y := Float) :# ()
-- a record is a list of fields, sorted to match expected order
record r = r !#& undefined
-- a point is a Point, if re-ordered
point = record ((Y := (3.0::Float)) :# (X := (4.0::Float)) :# ()) :: Point

norm1 :: Point -> Float
norm1 p = sqrt (p #? X * p #? X + p #? Y * p #? Y)

-- this is a type error, because X and Y are not in the expected order
-- test1a = norm1 ((Y := 3.0) :# (X := 4.0) :# ())
-- this works, because X and Y will be permuted into the expected order
test1b = norm1 (record $ (Y := (3.0::Float)) :# (X := (4.0::Float)) :# ())

norm2 :: (Select X Float rec, Select Y Float rec) => rec -> Float
norm2 p = sqrt ((p #? X) * (p #? X) + (p #? Y) * (p #? Y))

-- this is OK, because norm2 doesn't care about field order
test2a = norm2 ((Y := 3.0) :# (X := 4.0) :# ())
-- this is OK, because norm2 doesn't care about unused fields
test2b = norm2 ((Y := 3.0) :# (X := 4.0) :# (Z := True) :# ())

norm3 :: Project' rec Point => rec -> Float
norm3 p' = sqrt ((p #? X) * (p #? X) + (p #? Y) * (p #? Y)) where p = (record p')::Point

-- this is OK, because norm3 is doesn't care about field order
test3a = norm3 ((Y := (3.0::Float)) :# (X := (4.0::Float)) :# ())
-- this is a type error, because norm3 doesn't accept unused fields
-- test3b = norm3 ((Y := 3.0) :# (X := 4.0) :# (Z := True) :# ())

The more complex systems support first class labels. Here is an example using the Type Families system:

labelZip :: ({n :: a} `Disjoint` {m :: b}) => n -> m -> [a] -> [b] -> [{n :: a, m :: b}]
labelZip n m = zipWith (\x y -> {n = x, m = y})