[Haskell-cafe] Why can't GHC infer record fields?

Clinton Mead clintonmead at gmail.com
Fri Jun 23 09:43:10 UTC 2017


I asked this below question on Stack Overflow
<https://stackoverflow.com/questions/44714711/silly-duplicated-record-fields-error>
and
I got an answer suggesting that GHC doesn't support type inference of
overloaded record fields. But I'm wondering why this is the case.

Consider the following:

{-# LANGUAGE DuplicateRecordFields #-}
data A = A { name :: String }
data B = B { name :: String }

main = print $ name (A "Alice")

When compiled, I get the following message (on GHC 8.0.2)

duplicatedrecords.hs:7:16: error:
    Ambiguous occurrence ‘name’
    It could refer to either the field ‘name’,
                             defined at duplicatedrecords.hs:5:14
                          or the field ‘name’, defined at
duplicatedrecords.hs:3:14

But if I modify the main line as follows:

main = print $ name ((A "Alice") :: A)

Compilation proceeds successfully. I'm not sure why I need the :: A here,
it seems to be clear that (A "Alice") is of type A due to the A constructor.

It's worth noting that the following compiles fine:

data A = A { a_name :: String }data B = B { b_name :: String }
class Name t where
  name :: t -> String
instance Name A where name = a_nameinstance Name B where name = b_name

main = print $ name (A "Alice")

We can even go further as follows, allowing different result types:

{-# LANGUAGE TypeFamilies #-}
data A = A { a_name :: String }data B = B { b_name :: Int }
class Name t where
  type family T t
  name :: t -> T t
instance Name A where
  type T A = String
  name = a_name
instance Name B where
  type T B = Int
  name = b_name

main = print $ name (A "Alice")

It seems like GHC just has to mechanically add a class for each unique
record name and an instance for each record in each data type. This will
mean however that name x == name y not implying that the types of x and y are
the same but I'd expect that when using this extension anyway.

Just wondering if there's anything tricky I'm missing here regarding the
implementation or that it just needs someone to implement it?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170623/fab83512/attachment.html>


More information about the Haskell-Cafe mailing list