Why can't DuplicateRecordFields have an output type?

Associated entry: How to eliminate the selection function?

https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/DuplicateRecordFields

However, we do not derive the type of the argument to determine the data type or cannot defer the choice to the constraint resolver.

In fact, it is annoying that this function is not implemented. I tried to find several sources, but I could not find the reason why they decided not to output types.

Does anyone know why this is necessary? Is this due to a limitation of the current type of system?

+7
types haskell ghc records
source share
1 answer

You will be interested in OverloadedRecordFields , which is still running.


The current implementation is intentionally crippled so as not to introduce too much new material at once. The inference of the types of projection of the record allows you to open an unpleasant bank of worms (which are mentioned above).

Consider the following GHCi interaction

 ghci> data Record1 = Record1 { field :: Int } ghci> data Record2 = Record2 { field :: Bool } ghci> :t field 

What should the field ? One way or another, we need a way to capture the concept of "any record with a field called field ." To this end, OverloadedRecordFields introduces a new built-in class type.

The new GHC.Records module defines the following:

 class HasField (x :: k) ra | xr -> a where getField :: r -> a 

A HasField xra means that x is a field of type a belonging to record type r . The getField method records the selection function.

Then, from our example above, it is as if the magical forms of GHC were magical (in fact, this is not what will happen, but it is a good first approximation).

 instance HasField "field" Record1 Int where getField (Record1 f) = f instance HasField "field" Record2 Bool where getField (Record2 f) = f 

If you are interested, I recommend reading the sentence. Another feature that I did not mention is the IsLabel class. After all this has been implemented (and a few more for updating records), I look forward to receiving my lenses for free (so I can stop declaring field names starting from underlining and including TemplateHaskell for makeLenses ).

+9
source share

All Articles