Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:46:14+00:00 2026-06-10T08:46:14+00:00

Consider the following code: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE NoMonomorphismRestriction #-} import Data.HList.GhcSyntax((.!.),(.=.),(.*.))

  • 0

Consider the following code:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

import Data.HList.GhcSyntax((.!.),(.=.),(.*.))
import Data.HList.Record(emptyRecord)
import Data.HList.TypeCastGeneric1
import Data.HList.TypeEqGeneric1
import Data.HList.Label5

data Hello1 = Hello1
data Hello2 = Hello2

record = (Hello1 .=. "Hello1") .*. (Hello2 .=. "Hello2") .*. emptyRecord

f1 = $([| (\r1 -> (r1 .!. Hello1)) |]) 

main = print $ f1 record

This compiles fine and prints out “Hello1” as expected.

However, adding the following line (GHC 7.4.1) gives a compile error:

f2 = $([| (\r2 -> (r2 .!. Hello2)) |]) 

The error given is:

error.hs:16:1:
    Could not deduce (Data.HList.Record.HasField Hello2 r0 v0)
      arising from the ambiguity check for `main'
    from the context (Data.HList.Record.HasField Hello2 r v)
      bound by the inferred type for `main':
                 Data.HList.Record.HasField Hello2 r v => IO ()
      at error.hs:(16,1)-(20,38)
    Possible fix:
      add an instance declaration for
      (Data.HList.Record.HasField Hello2 r0 v0)
    When checking that `main'
      has the inferred type `forall r v.
                             Data.HList.Record.HasField Hello2 r v =>
                             IO ()'
    Probable cause: the inferred type is ambiguous

error.hs:16:1:
    Could not deduce (Data.HList.Record.HasField Hello2 r0 v0)
      arising from the ambiguity check for `f1'
    from the context (Data.HList.Record.HasField Hello2 r v)
      bound by the inferred type for `f1':
                 Data.HList.Record.HasField Hello2 r v =>
                 Data.HList.Record.Record
                   (Data.HList.HListPrelude.HCons
                      (Data.HList.Record.LVPair Hello1 [Char])
                      (Data.HList.HListPrelude.HCons
                         (Data.HList.Record.LVPair Hello2 [Char])
                         Data.HList.HListPrelude.HNil))
                 -> [Char]
      at error.hs:(16,1)-(20,38)
    Possible fix:
      add an instance declaration for
      (Data.HList.Record.HasField Hello2 r0 v0)
    When checking that `f1'
      has the inferred type `forall r v.
                             Data.HList.Record.HasField Hello2 r v =>
                             Data.HList.Record.Record
                               (Data.HList.HListPrelude.HCons
                                  (Data.HList.Record.LVPair Hello1 [Char])
                                  (Data.HList.HListPrelude.HCons
                                     (Data.HList.Record.LVPair Hello2 [Char])
                                     Data.HList.HListPrelude.HNil))
                             -> [Char]'
    Probable cause: the inferred type is ambiguous

Why does adding the f2 line result in a compile error?

Note: The Template Haskell parts may look silly here, but they are a simplification of more complex Template Haskell which does work on tuples. I’ve posted the simplest example I could construct that still exhibited the error. I realise removing the Template Haskell fixes the issue in this case, but that isn’t an option in my real code.

Edit:

In addition, the following fails to compile. Why is this the case:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

import Data.HList.GhcSyntax((.!.),(.=.),(.*.))
import Data.HList.Record(emptyRecord)
import Data.HList.TypeCastGeneric1
import Data.HList.TypeEqGeneric1
import Data.HList.Label5

data Hello1 = Hello1
data Hello2 = Hello2
data Hello3 = Hello3

record1 = (Hello1 .=. "Hello1") .*. (Hello2 .=. "Hello2") .*. emptyRecord
record2 = (Hello1 .=. "Hello1") .*. (Hello2 .=. "Hello2") .*. (Hello3 .=. "Hello3") .*. emptyRecord

f1 = $([| (\r1 -> (r1 .!. Hello1)) |]) 

main = print $ (f1 record1, f1 record2)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:46:16+00:00Added an answer on June 10, 2026 at 8:46 am

    I’ve found giving your top level functions type signatures fixes any issues. See the code below:

    {-# LANGUAGE TemplateHaskell #-}
    
    module X where
      import Data.HList.GhcSyntax((.!.))
    
      f = [| (\x r -> (r .!. x)) |]
    
    {-# LANGUAGE TemplateHaskell #-}
    {-# LANGUAGE NoMonomorphismRestriction #-}
    {-# LANGUAGE FlexibleContexts #-}
    
    import Data.HList.GhcSyntax((.!.),(.=.),(.*.))
    import Data.HList.Record(emptyRecord)
    import Data.HList.TypeCastGeneric1
    import Data.HList.TypeEqGeneric1
    import Data.HList.Label5
    import X
    import Data.HList.Record (HasField)
    
    data Hello1 = Hello1
    data Hello2 = Hello2
    data Hello3 = Hello3
    
    record1 = (Hello1 .=. "Hello1") .*. (Hello2 .=. "Hello2") .*. emptyRecord
    record2 = (Hello1 .=. "Hello1") .*. (Hello2 .=. "Hello2") .*. (Hello3 .=. "Hello3") .*. emptyRecord
    
    g1 :: (HasField Hello1 a b) => a -> b -- Type signature here
    g1 = $(f) Hello1
    
    g2 :: (HasField Hello2 a b) => a -> b -- Type signature here
    g2 = $(f) Hello2
    
    main = print $ (g1 record1, g2 record1, g1 record2, g2 record2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code snippet import qualified Foreign.Concurrent import Foreign.Ptr (nullPtr) main :: IO
Consider the following code example: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} --
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following code: enum size = 16; double[size] arr1 = [...]; double[size] arr2 =
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: public interface A { public A another(); } public interface
Consider the following code: $xml = <<<XML <root> <region id='thisRegion'></region> <region id='thatRegion'></region> </root> XML;
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.