Skip to content

Change definition of XRec type family

Following discussions in !2315 (closed) I'm opening a ticket for a change proposal of the XRec type family:

Motivation

The current state of TTG for source locations is documented as 'Solution D' (solution 4) in the wiki. What the Wiki calls WrapL is called XRec in GHC source at the moment and looks like this:

type family XRec p a
type instance XRec (GhcPass p) a = Located a

(GHC/Hs/Extension.hs)

As far as I understand it (and the wiki page seems to suggest this, too), the usage of XRec is supposed to remove all Located usages from the AST. (For example to potentially allow a TH-esque AST without source locations).

In practice, this is no quite possible with its current definition.

Consider the definition of HsDeriving:

type HsDeriving pass = Located [LHsDerivingClause pass]

It would be necessary to set up a newtype for the second parameter to XRec.

In some cases this newtype would only be there to introduce a phantom type parameter, see the dd_cType field in HsDataDef:

data HsDataDefn pass =
    HsDataDefn { -- ...
                 dd_cType  :: Maybe (Located CType),
                 -- ...
               }

These newtypes sometimes require more invasive changes than necessary, for example to generate the appropriate structure in the parser.

Proposal

1. Simpler Definition

I would instead suggest defining XRec like this:

type family XLocated p a = r | r -> p a
type instance XLocated (GhcPass p) a = Located a

This diff would be fairly minimal, as we would only need to change some XRec pass Pat to XRec pass (Pat pass), but opens up XRec to many more parts of the codebase.

2. Rename

To me XRec was confusing:

  • It took a * -> * parameter and was named after 'Recursion'. I was expecting it to do something like Fix from recursion-schemes.
  • It is not only supposed to be used at recursion points in the future: See dd_cType in HsDataDef above.

I would suggest renaming it to XLocated, but I'm open to other suggestions!

The diff for this proposal would be basically exactly the same lines as the 1. Proposal, so it would go very well hand-in-hand.

Wiki Pages

As a result of the acceptance of these changes the Wiki page for handling source locations in TTG should be updated. Solutions A-C aren't up-to-date anymore and if/once this change gets accepted, Solution D won't up-to-date as well.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information