Home

Awesome

record-dot-preprocessor Hackage version Stackage version Build status

In almost every programming language a.b will get the b field from the a data type, and many different data types can have a b field. The reason this feature is ubiquitous is because it's useful. The record-dot-preprocessor brings this feature to modern GHC versions. This feature has been proposed for Haskell as RecordDotSyntax. Since GHC 9.2 the OverloadedRecordDot and OverloadedRecordUpdate extensions implement much the same functionality. Some examples:

data Company = Company {name :: String, owner :: Person}
data Person = Person {name :: String, age :: Int}

display :: Company -> String
display c = c.name ++ " is run by " ++ c.owner.name

nameAfterOwner :: Company -> Company
nameAfterOwner c = c{name = c.owner.name ++ "'s Company"}

Here we declare two records both with name as a field, then write c.name and c.owner.name to get those fields. We can also write c{name = x} as a record update, which still works even though name is no longer unique.

How do I use this magic?

First install record-dot-preprocessor with either stack install record-dot-preprocessor or cabal update && cabal install record-dot-preprocessor. Then at the top of the file add:

The GHC plugin only runs on GHC 8.6 or higher, has some issues on Windows and has much better error messages. In contrast, the preprocessor runs everywhere and has more features.

You must make sure that the OPTIONS_GHC is applied both to the file where your records are defined, and where the record syntax is used. The resulting program will require the record-hasfield library.

What magic is available, precisely?

Using the preprocessor or the GHC plugin you can write:

Using the preprocessor, but not the GHC plugin:

These forms combine to offer the identities:

How does this magic compare to other magic?

Records in Haskell are well known to be pretty lousy. There are many proposals that aim to make Haskell records more powerful using dark arts taken from type systems and category theory. This preprocessor aims for simplicity - combining existing elements into a coherent story. The aim is to do no worse than Java, not achieve perfection.

Any advice for using this magic?

The most important consideration is that all records used by a.b or a{b=c} syntax must have HasField instances, which requires either running the preprocessor/plugin over the module defining them, or writing orphan instances by hand. To use records which don't have such instances use normal selector functions (e.g. b a) and insert a space before the { (e.g. a {b=c}).

Limitations