Skip to content

HasField ambiguity error

Both of these functions fail to compile with the error below:

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

import Control.Monad.Reader
import GHC.Records
import Data.Proxy

askField :: forall x a m r. (HasField x r a, MonadReader r m) => m a
askField =
    asks (getField @x)

askField' :: forall x a m r. (HasField x r a, MonadReader r m) => m a
askField' =
    asks (getFieldWithProxy (Proxy :: Proxy x))
  where
    getFieldWithProxy :: forall proxy. proxy x -> r -> a
    getFieldWithProxy = const getField
Test.hs:12:14: error:
    • Could not deduce (HasField x0 r a)
      from the context: (HasField x r a, MonadReader r m)
        bound by the type signature for:
                   askField' :: forall x a (m :: * -> *) r.
                                (HasField x r a, MonadReader r m) =>
                                m a
        at Test.hs:12:14-69
      The type variable ‘x0’ is ambiguous
    • In the ambiguity check for ‘askField'’
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      In the type signature:
        askField' :: forall x a m r.
                     (HasField x r a, MonadReader r m) => m a
   |
12 | askField' :: forall x a m r. (HasField x r a, MonadReader r m) => m a
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I hope I'm not missing something, but one of these should compile.

Trac metadata
Trac field Value
Version 8.2.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information