Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eta reduce and DuplicateRecordFields language extension

Tags:

haskell

Issue is about having 2 data types, Transaction and FormatModel, both having formatId field. To prevent adding type signatures to get formatId from a transaction or formatModel, I have created type class HasFormat:

class HasFormat a where
  formatId_ :: a -> FormatId

instance HasFormat Transaction where
   formatId_  x = formatId x -- gives error because ambiguous occurrence ‘formatId’

instance HasFormat FormatModel where
  formatId_  = formatId -- this works

Can some explain why the instance which has eta reduced implementation is working and the other one not?

like image 760
Rik Avatar asked Mar 01 '17 13:03

Rik


1 Answers

Disambiguation of duplicate record fields is necessarily a best-effort kind of thing because it needs to occur before type checking (you can't generally type check an expression before you know what identifiers the names in it refer to; which is what the disambiguation is doing).

Your non-working example is equivalent to this non-working example from the documentation:

data S = MkS { x :: Int }
data T = MkT { x :: Bool }
bad :: S -> Int
bad s = x s
like image 133
Reid Barton Avatar answered Nov 10 '22 18:11

Reid Barton